This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8a5ec8530ce Minor refactor for sql federation engine and
LiteralExpressionConverter (#37387)
8a5ec8530ce is described below
commit 8a5ec8530cebc79b269865aeea0170f9c702f6eb
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Dec 15 18:19:56 2025 +0800
Minor refactor for sql federation engine and LiteralExpressionConverter
(#37387)
---
.../exception/SQLFederationUnsupportedSQLException.java | 4 ++--
.../segment/expression/impl/LiteralExpressionConverter.java | 7 +++++++
.../sqlfederation/engine/SQLFederationEngine.java | 11 +++++++++--
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/exception/SQLFederationUnsupportedSQLException.java
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/exception/SQLFederationUnsupportedSQLException.java
index ef165eeaa57..d3736b5f363 100644
---
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/exception/SQLFederationUnsupportedSQLException.java
+++
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/exception/SQLFederationUnsupportedSQLException.java
@@ -26,7 +26,7 @@ public final class SQLFederationUnsupportedSQLException
extends SQLFederationSQL
private static final long serialVersionUID = -8571244162760408846L;
- public SQLFederationUnsupportedSQLException(final String sql, final
Exception cause) {
- super(XOpenSQLState.SYNTAX_ERROR, 1, cause, "SQL federation does not
support SQL '%s'.", sql);
+ public SQLFederationUnsupportedSQLException(final String sql, final String
reason) {
+ super(XOpenSQLState.SYNTAX_ERROR, 1, reason, "SQL federation does not
support SQL '%s'.", sql);
}
}
diff --git
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/LiteralExpressionConverter.java
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/LiteralExpressionConverter.java
index 0360102f366..478cb99bf66 100644
---
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/LiteralExpressionConverter.java
+++
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/LiteralExpressionConverter.java
@@ -28,6 +28,7 @@ import org.apache.calcite.sql.fun.SqlTrimFunction.Flag;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.DateString;
+import org.apache.calcite.util.NlsString;
import org.apache.calcite.util.TimeString;
import org.apache.calcite.util.TimestampString;
import org.apache.calcite.util.TimestampWithTimeZoneString;
@@ -92,6 +93,9 @@ public final class LiteralExpressionConverter {
if (segment.getLiterals() instanceof String) {
return Optional.of(SqlLiteral.createCharString(literalValue,
SqlParserPos.ZERO));
}
+ if (segment.getLiterals() instanceof NlsString) {
+ return Optional.of(SqlLiteral.createCharString(((NlsString)
segment.getLiterals()).getValue(), SqlParserPos.ZERO));
+ }
if (segment.getLiterals() instanceof Boolean) {
return
Optional.of(SqlLiteral.createBoolean(Boolean.parseBoolean(literalValue),
SqlParserPos.ZERO));
}
@@ -101,6 +105,9 @@ public final class LiteralExpressionConverter {
if (segment.getLiterals() instanceof Date) {
return Optional.of(convertDate(segment, literalValue));
}
+ if (segment.getLiterals() instanceof TimestampString) {
+ return
Optional.of(SqlLiteral.createTimestamp(SqlTypeName.TIMESTAMP, (TimestampString)
segment.getLiterals(), 1, SqlParserPos.ZERO));
+ }
if (segment.getLiterals() instanceof LocalDate) {
return
Optional.of(SqlLiteral.createDate(DateString.fromDaysSinceEpoch((int)
((LocalDate) segment.getLiterals()).toEpochDay()), SqlParserPos.ZERO));
}
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
index 186fac5fbd1..fabb160f964 100644
---
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
@@ -90,6 +90,8 @@ public final class SQLFederationEngine implements
AutoCloseable {
private static final Collection<Class<?>> NEED_THROW_EXCEPTION_TYPES =
Arrays.asList(SQLExecutionInterruptedException.class,
SQLIntegrityConstraintViolationException.class);
+ private static final int MAX_ERROR_MESSAGE_LENGTH = 5000;
+
private final ProcessEngine processEngine = new ProcessEngine();
@SuppressWarnings("rawtypes")
@@ -205,15 +207,20 @@ public final class SQLFederationEngine implements
AutoCloseable {
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
- log.error("SQL Federation execute failed, sql {}, parameters {}",
queryContext.getSql(), queryContext.getParameters(), ex);
+ String errorMessage = splitErrorMessage(ex);
+ log.error("SQL Federation execute failed, sql {}, parameters {},
reason {}", queryContext.getSql(), queryContext.getParameters(), errorMessage);
closeResources(federationContext);
if (NEED_THROW_EXCEPTION_TYPES.stream().anyMatch(each ->
each.isAssignableFrom(ex.getClass()))) {
throw ex;
}
- throw new
SQLFederationUnsupportedSQLException(queryContext.getSql(), ex);
+ throw new
SQLFederationUnsupportedSQLException(queryContext.getSql(), errorMessage);
}
}
+ private String splitErrorMessage(final Exception ex) {
+ return null == ex.getMessage() ? "" : ex.getMessage().substring(0,
Math.min(ex.getMessage().length(), MAX_ERROR_MESSAGE_LENGTH));
+ }
+
private void closeResources(final SQLFederationContext federationContext) {
try {
processEngine.completeSQLExecution(federationContext.getProcessId());