You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by tu...@apache.org on 2022/08/15 09:26:42 UTC
[shardingsphere] branch master updated: Remove useless convertToSQLSegment in SQLSegmentConverter and SQLStatementConverter (#20190)
This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin 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 466002ccce6 Remove useless convertToSQLSegment in SQLSegmentConverter and SQLStatementConverter (#20190)
466002ccce6 is described below
commit 466002ccce62b0cdcee2515a1cadc26d53521c2f
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Mon Aug 15 17:26:35 2022 +0800
Remove useless convertToSQLSegment in SQLSegmentConverter and SQLStatementConverter (#20190)
---
.../table/FilterableTableScanExecutor.java | 1 -
.../optimizer/ShardingSphereOptimizer.java | 2 +-
.../converter/SQLNodeConverterEngine.java | 34 +-------
.../converter/segment/SQLSegmentConverter.java | 32 +-------
.../segment/expression/ExpressionConverter.java | 95 +++-------------------
.../impl/BetweenExpressionConverter.java | 21 +----
.../impl/BinaryOperationExpressionConverter.java | 24 +-----
.../segment/expression/impl/ColumnConverter.java | 19 +----
.../impl/ExistsSubqueryExpressionConverter.java | 20 +----
.../segment/expression/impl/FunctionConverter.java | 42 +---------
.../expression/impl/InExpressionConverter.java | 28 +------
.../expression/impl/ListExpressionConverter.java | 20 +----
.../impl/LiteralExpressionConverter.java | 12 +--
.../impl/ParameterMarkerExpressionConverter.java | 9 +-
.../impl/SubqueryExpressionConverter.java | 18 +---
.../converter/segment/from/TableConverter.java | 37 +--------
.../segment/from/impl/JoinTableConverter.java | 22 +----
.../segment/from/impl/SimpleTableConverter.java | 28 +------
.../segment/from/impl/SubqueryTableConverter.java | 20 +----
.../segment/groupby/GroupByConverter.java | 15 +---
.../converter/segment/groupby/HavingConverter.java | 13 +--
.../segment/limit/PaginationValueSQLConverter.java | 16 +---
.../segment/orderby/OrderByConverter.java | 15 +---
.../orderby/item/ColumnOrderByItemConverter.java | 25 +-----
.../item/ExpressionOrderByItemConverter.java | 7 +-
.../orderby/item/IndexOrderByItemConverter.java | 13 +--
.../orderby/item/OrderByItemConverterUtil.java | 26 +-----
.../segment/projection/DistinctConverter.java | 7 +-
.../segment/projection/ProjectionsConverter.java | 71 ++--------------
.../impl/AggregationProjectionConverter.java | 68 +---------------
.../projection/impl/ColumnProjectionConverter.java | 31 +------
.../impl/ExpressionProjectionConverter.java | 30 +------
.../impl/ShorthandProjectionConverter.java | 17 +---
.../impl/SubqueryProjectionConverter.java | 23 +-----
.../converter/segment/where/WhereConverter.java | 16 +---
.../converter/statement/SQLStatementConverter.java | 12 +--
.../statement/select/SelectStatementConverter.java | 73 +++--------------
.../converter/type/CombineOperatorConverter.java | 18 ----
.../SQLNodeConvertEngineParameterizedTest.java | 17 +---
39 files changed, 95 insertions(+), 902 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-executor/src/main/java/org/apache/shardingsphere/infra/federation/executor/original/table/FilterableTableScanExecutor.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-executor/src/main/java/org/apache/shardingsphere/infra/federation/executor/original/table/FilterableTableScanExecutor.java
index c010c0c6ff6..53e11b0a356 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-executor/src/main/java/org/apache/shardingsphere/infra/federation/executor/original/table/FilterableTableScanExecutor.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-executor/src/main/java/org/apache/shardingsphere/infra/federation/executor/original/table/FilterableTableScanExecutor.java
@@ -114,7 +114,6 @@ public final class FilterableTableScanExecutor implements TableScanExecutor {
String schemaName = executorContext.getSchemaName();
DatabaseType databaseType = DatabaseTypeEngine.getTrunkDatabaseType(optimizerContext.getParserContexts().get(databaseName).getDatabaseType().getType());
SqlString sqlString = createSQLString(table, scanContext, SQLDialectFactory.getSQLDialect(databaseType));
- // TODO replace sql parse with sql convert
FederationContext federationContext = executorContext.getFederationContext();
LogicSQL logicSQL = createLogicSQL(federationContext.getDatabases(), sqlString, databaseType);
ShardingSphereDatabase database = federationContext.getDatabases().get(databaseName.toLowerCase());
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/ShardingSphereOptimizer.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/ShardingSphereOptimizer.java
index ae1b595981e..faec5782398 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/ShardingSphereOptimizer.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/ShardingSphereOptimizer.java
@@ -45,7 +45,7 @@ public final class ShardingSphereOptimizer {
*/
public RelNode optimize(final SQLStatement sqlStatement) {
try {
- SqlNode sqlNode = SQLNodeConverterEngine.convertToSQLNode(sqlStatement);
+ SqlNode sqlNode = SQLNodeConverterEngine.convert(sqlStatement);
RelNode logicPlan = converter.convertQuery(sqlNode, true, true).rel;
RelNode bestPlan = optimizeWithRBO(logicPlan, hepPlanner);
return optimizeWithCBO(bestPlan, converter);
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
index 8ab06566eae..2b7f3fceb00 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
@@ -20,14 +20,10 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlOrderBy;
-import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.select.SelectStatementConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.type.CombineOperatorConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.constant.CombineType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
@@ -46,39 +42,15 @@ public final class SQLNodeConverterEngine {
* @param statement SQL statement to be converted
* @return sqlNode converted SQL node
*/
- public static SqlNode convertToSQLNode(final SQLStatement statement) {
+ public static SqlNode convert(final SQLStatement statement) {
if (statement instanceof SelectStatement) {
- SqlNode sqlNode = new SelectStatementConverter().convertToSQLNode((SelectStatement) statement);
+ SqlNode sqlNode = new SelectStatementConverter().convert((SelectStatement) statement);
for (CombineSegment each : ((SelectStatement) statement).getCombines()) {
- SqlNode combineSqlNode = convertToSQLNode(each.getSelectStatement());
+ SqlNode combineSqlNode = convert(each.getSelectStatement());
return new SqlBasicCall(CombineOperatorConverter.convert(each.getCombineType()), Arrays.asList(sqlNode, combineSqlNode), SqlParserPos.ZERO);
}
return sqlNode;
}
throw new UnsupportedOperationException("Unsupported SQL node conversion.");
}
-
- /**
- * Convert SQL node to SQL statement.
- *
- * @param sqlNode sqlNode converted SQL node
- * @return SQL statement to be converted
- */
- public static SQLStatement convertToSQLStatement(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlOrderBy || sqlNode instanceof SqlSelect) {
- return new SelectStatementConverter().convertToSQLStatement(sqlNode);
- }
- if (sqlNode instanceof SqlBasicCall && null != ((SqlBasicCall) sqlNode).getOperator() && SqlKind.UNION == ((SqlBasicCall) sqlNode).getOperator().getKind()) {
- SqlNode leftSqlNode = ((SqlBasicCall) sqlNode).getOperandList().get(0);
- SqlNode rightSqlNode = ((SqlBasicCall) sqlNode).getOperandList().get(1);
- SelectStatement leftSelectStatement = (SelectStatement) convertToSQLStatement(leftSqlNode);
- SelectStatement rightSelectStatement = (SelectStatement) convertToSQLStatement(rightSqlNode);
- CombineType combineType = CombineOperatorConverter.convert(((SqlBasicCall) sqlNode).getOperator());
- int startIndex = rightSqlNode.getParserPosition().getColumnNum() - (((SqlBasicCall) sqlNode).getOperator().getName() + " ").length() - 1;
- int stopIndex = rightSqlNode.getParserPosition().getEndColumnNum() - 1;
- leftSelectStatement.getCombines().add(new CombineSegment(startIndex, stopIndex, combineType, rightSelectStatement));
- return leftSelectStatement;
- }
- throw new UnsupportedOperationException("Unsupported SQL statement conversion.");
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/SQLSegmentConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/SQLSegmentConverter.java
index ef292004fc5..ed1ec4764dc 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/SQLSegmentConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/SQLSegmentConverter.java
@@ -31,38 +31,10 @@ import java.util.Optional;
public interface SQLSegmentConverter<S extends SQLSegment, T extends SqlNode> {
/**
- * Convert SQL segment to SQL node.
+ * Convert.
*
* @param segment SQL segment be to converted
* @return converted SQL node
*/
- Optional<T> convertToSQLNode(S segment);
-
- /**
- * Convert SQL node to SQL segment.
- *
- * @param sqlNode SQL node be to converted
- * @return converted SQL segment
- */
- Optional<S> convertToSQLSegment(T sqlNode);
-
- /**
- * Get start index.
- *
- * @param sqlNode SQL node
- * @return start index
- */
- default int getStartIndex(SqlNode sqlNode) {
- return sqlNode.getParserPosition().getColumnNum() - 1;
- }
-
- /**
- * Get stop index.
- *
- * @param sqlNode SQL node
- * @return stop index
- */
- default int getStopIndex(SqlNode sqlNode) {
- return sqlNode.getParserPosition().getEndColumnNum() - 1;
- }
+ Optional<T> convert(S segment);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/ExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/ExpressionConverter.java
index bf96b9ea88b..59e63d93b4a 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/ExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/ExpressionConverter.java
@@ -17,22 +17,7 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression;
-import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlBinaryOperator;
-import org.apache.calcite.sql.SqlDynamicParam;
-import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNodeList;
-import org.apache.calcite.sql.SqlOperator;
-import org.apache.calcite.sql.SqlSelect;
-import org.apache.calcite.sql.SqlUnresolvedFunction;
-import org.apache.calcite.sql.fun.SqlBetweenOperator;
-import org.apache.calcite.sql.fun.SqlCastFunction;
-import org.apache.calcite.sql.fun.SqlInOperator;
-import org.apache.calcite.sql.fun.SqlLikeOperator;
-import org.apache.calcite.sql.fun.SqlPositionFunction;
-import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.BetweenExpressionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.BinaryOperationExpressionConverter;
@@ -45,7 +30,6 @@ import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.ex
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.ParameterMarkerExpressionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.SubqueryExpressionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl.AggregationProjectionConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
@@ -68,91 +52,36 @@ import java.util.Optional;
public final class ExpressionConverter implements SQLSegmentConverter<ExpressionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ExpressionSegment segment) {
+ public Optional<SqlNode> convert(final ExpressionSegment segment) {
if (null == segment) {
return Optional.empty();
}
if (segment instanceof LiteralExpressionSegment) {
- return new LiteralExpressionConverter().convertToSQLNode((LiteralExpressionSegment) segment);
+ return new LiteralExpressionConverter().convert((LiteralExpressionSegment) segment);
} else if (segment instanceof CommonExpressionSegment) {
// TODO
throw new UnsupportedOperationException("unsupported CommonExpressionSegment");
} else if (segment instanceof ListExpression) {
- return new ListExpressionConverter().convertToSQLNode((ListExpression) segment).map(optional -> optional);
+ return new ListExpressionConverter().convert((ListExpression) segment).map(optional -> optional);
} else if (segment instanceof BinaryOperationExpression) {
- return new BinaryOperationExpressionConverter().convertToSQLNode((BinaryOperationExpression) segment).map(optional -> optional);
+ return new BinaryOperationExpressionConverter().convert((BinaryOperationExpression) segment).map(optional -> optional);
} else if (segment instanceof ColumnSegment) {
- return new ColumnConverter().convertToSQLNode((ColumnSegment) segment).map(optional -> optional);
+ return new ColumnConverter().convert((ColumnSegment) segment).map(optional -> optional);
} else if (segment instanceof ExistsSubqueryExpression) {
- return new ExistsSubqueryExpressionConverter().convertToSQLNode((ExistsSubqueryExpression) segment).map(optional -> optional);
+ return new ExistsSubqueryExpressionConverter().convert((ExistsSubqueryExpression) segment).map(optional -> optional);
} else if (segment instanceof SubqueryExpressionSegment) {
- return new SubqueryExpressionConverter().convertToSQLNode((SubqueryExpressionSegment) segment);
+ return new SubqueryExpressionConverter().convert((SubqueryExpressionSegment) segment);
} else if (segment instanceof InExpression) {
- return new InExpressionConverter().convertToSQLNode((InExpression) segment).map(optional -> optional);
+ return new InExpressionConverter().convert((InExpression) segment).map(optional -> optional);
} else if (segment instanceof BetweenExpression) {
- return new BetweenExpressionConverter().convertToSQLNode((BetweenExpression) segment).map(optional -> optional);
+ return new BetweenExpressionConverter().convert((BetweenExpression) segment).map(optional -> optional);
} else if (segment instanceof ParameterMarkerExpressionSegment) {
- return new ParameterMarkerExpressionConverter().convertToSQLNode((ParameterMarkerExpressionSegment) segment).map(optional -> optional);
+ return new ParameterMarkerExpressionConverter().convert((ParameterMarkerExpressionSegment) segment).map(optional -> optional);
} else if (segment instanceof FunctionSegment) {
- return new FunctionConverter().convertToSQLNode((FunctionSegment) segment).map(optional -> optional);
+ return new FunctionConverter().convert((FunctionSegment) segment).map(optional -> optional);
} else if (segment instanceof AggregationProjectionSegment) {
- return new AggregationProjectionConverter().convertToSQLNode((AggregationProjectionSegment) segment).map(optional -> optional);
+ return new AggregationProjectionConverter().convert((AggregationProjectionSegment) segment).map(optional -> optional);
}
throw new UnsupportedOperationException("unsupported TableSegment type: " + segment.getClass());
}
-
- @Override
- public Optional<ExpressionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- if (sqlNode instanceof SqlIdentifier) {
- return new ColumnConverter().convertToSQLSegment((SqlIdentifier) sqlNode).map(optional -> optional);
- }
- if (sqlNode instanceof SqlBasicCall) {
- return convertToSQLSegment((SqlBasicCall) sqlNode, false);
- }
- if (sqlNode instanceof SqlSelect) {
- return new SubqueryExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- if (sqlNode instanceof SqlLiteral) {
- return new LiteralExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- if (sqlNode instanceof SqlDynamicParam) {
- return new ParameterMarkerExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- if (sqlNode instanceof SqlNodeList) {
- return new ListExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- return Optional.empty();
- }
-
- private Optional<ExpressionSegment> convertToSQLSegment(final SqlBasicCall sqlBasicCall, final boolean not) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- SqlOperator operator = sqlBasicCall.getOperator();
- if (operator.getName().equals(SqlStdOperatorTable.NOT.getName()) && sqlBasicCall.getOperandList().get(0) instanceof SqlBasicCall) {
- return convertToSQLSegment((SqlBasicCall) sqlBasicCall.getOperandList().get(0), true);
- }
- if (operator instanceof SqlInOperator) {
- return new InExpressionConverter(not).convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (operator instanceof SqlBetweenOperator) {
- return new BetweenExpressionConverter(not).convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (operator.getName().equals(SqlStdOperatorTable.EXISTS.getName())) {
- return new ExistsSubqueryExpressionConverter(not).convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (operator instanceof SqlBinaryOperator || operator instanceof SqlLikeOperator) {
- return new BinaryOperationExpressionConverter().convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (AggregationType.isAggregationType(operator.getName())) {
- return new AggregationProjectionConverter().convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (operator instanceof SqlPositionFunction || operator instanceof SqlCastFunction || operator instanceof SqlUnresolvedFunction) {
- return new FunctionConverter().convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BetweenExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BetweenExpressionConverter.java
index ece631faab1..08f74780337 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BetweenExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BetweenExpressionConverter.java
@@ -25,7 +25,6 @@ import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.ExpressionConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import java.util.ArrayList;
import java.util.Collection;
@@ -46,28 +45,16 @@ public final class BetweenExpressionConverter implements SQLSegmentConverter<Bet
}
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final BetweenExpression expression) {
+ public Optional<SqlBasicCall> convert(final BetweenExpression expression) {
if (null == expression) {
return Optional.empty();
}
Collection<SqlNode> sqlNodes = new LinkedList<>();
ExpressionConverter expressionConverter = new ExpressionConverter();
- expressionConverter.convertToSQLNode(expression.getLeft()).ifPresent(sqlNodes::add);
- expressionConverter.convertToSQLNode(expression.getBetweenExpr()).ifPresent(sqlNodes::add);
- expressionConverter.convertToSQLNode(expression.getAndExpr()).ifPresent(sqlNodes::add);
+ expressionConverter.convert(expression.getLeft()).ifPresent(sqlNodes::add);
+ expressionConverter.convert(expression.getBetweenExpr()).ifPresent(sqlNodes::add);
+ expressionConverter.convert(expression.getAndExpr()).ifPresent(sqlNodes::add);
SqlBasicCall sqlNode = new SqlBasicCall(SqlStdOperatorTable.BETWEEN, new ArrayList<>(sqlNodes), SqlParserPos.ZERO);
return expression.isNot() ? Optional.of(new SqlBasicCall(SqlStdOperatorTable.NOT, Collections.singletonList(sqlNode), SqlParserPos.ZERO)) : Optional.of(sqlNode);
}
-
- @Override
- public Optional<BetweenExpression> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- ExpressionConverter expressionConverter = new ExpressionConverter();
- ExpressionSegment between = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(1)).orElseThrow(IllegalStateException::new);
- ExpressionSegment and = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(2)).orElseThrow(IllegalStateException::new);
- ExpressionSegment left = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(0)).orElseThrow(IllegalStateException::new);
- return Optional.of(new BetweenExpression(left.getStartIndex(), and.getStopIndex(), left, between, and, not));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BinaryOperationExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
index a1e8c57df82..8d646f9acf4 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/BinaryOperationExpressionConverter.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.e
import com.google.common.base.Preconditions;
import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
@@ -27,8 +26,6 @@ import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.ExpressionConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
import java.util.Arrays;
import java.util.Map;
@@ -72,28 +69,13 @@ public final class BinaryOperationExpressionConverter implements SQLSegmentConve
}
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final BinaryOperationExpression segment) {
+ public Optional<SqlBasicCall> convert(final BinaryOperationExpression segment) {
SqlOperator operator = convertOperator(segment.getOperator());
- SqlNode left = new ExpressionConverter().convertToSQLNode(segment.getLeft()).orElseThrow(IllegalStateException::new);
- SqlNode right = new ExpressionConverter().convertToSQLNode(segment.getRight()).orElseThrow(IllegalStateException::new);
+ SqlNode left = new ExpressionConverter().convert(segment.getLeft()).orElseThrow(IllegalStateException::new);
+ SqlNode right = new ExpressionConverter().convert(segment.getRight()).orElseThrow(IllegalStateException::new);
return Optional.of(new SqlBasicCall(operator, Arrays.asList(left, right), SqlParserPos.ZERO));
}
- @Override
- public Optional<BinaryOperationExpression> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- ExpressionConverter expressionConverter = new ExpressionConverter();
- ExpressionSegment left = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(0)).orElseThrow(IllegalStateException::new);
- ExpressionSegment right = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(1)).orElseThrow(IllegalStateException::new);
- String operator = sqlBasicCall.getOperator().getName();
- String text = sqlBasicCall.toString();
- if (SqlKind.LIKE == sqlBasicCall.getOperator().getKind()) {
- ListExpression listExpression = new ListExpression(getStartIndex(sqlBasicCall.getOperandList().get(1)), getStopIndex(sqlBasicCall.getOperandList().get(1)));
- listExpression.getItems().add(right);
- return Optional.of(new BinaryOperationExpression(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall), left, listExpression, operator, text));
- }
- return Optional.of(new BinaryOperationExpression(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall), left, right, operator, text));
- }
-
private SqlOperator convertOperator(final String operator) {
Preconditions.checkState(REGISTRY.containsKey(operator), "Unsupported SQL operator: `%s`", operator);
return REGISTRY.get(operator);
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ColumnConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ColumnConverter.java
index 9a665ab85d6..e57abc90bfb 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ColumnConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ColumnConverter.java
@@ -17,13 +17,11 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl;
-import com.google.common.collect.ImmutableList;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Arrays;
import java.util.Optional;
@@ -34,26 +32,11 @@ import java.util.Optional;
public final class ColumnConverter implements SQLSegmentConverter<ColumnSegment, SqlIdentifier> {
@Override
- public Optional<SqlIdentifier> convertToSQLNode(final ColumnSegment segment) {
+ public Optional<SqlIdentifier> convert(final ColumnSegment segment) {
Optional<OwnerSegment> owner = segment.getOwner();
String columnName = segment.getIdentifier().getValue();
SqlIdentifier sqlIdentifier = owner.map(optional -> new SqlIdentifier(Arrays.asList(optional.getIdentifier().getValue(), columnName), SqlParserPos.ZERO))
.orElseGet(() -> new SqlIdentifier(columnName, SqlParserPos.ZERO));
return Optional.of(sqlIdentifier);
}
-
- @Override
- public Optional<ColumnSegment> convertToSQLSegment(final SqlIdentifier sqlIdentifier) {
- if (null == sqlIdentifier) {
- return Optional.empty();
- }
- ImmutableList<String> names = sqlIdentifier.names;
- if (1 == names.size()) {
- return Optional.of(new ColumnSegment(getStartIndex(sqlIdentifier), getStopIndex(sqlIdentifier), new IdentifierValue(names.get(0))));
- }
- ColumnSegment result = new ColumnSegment(getStartIndex(sqlIdentifier), getStopIndex(sqlIdentifier), new IdentifierValue(names.get(1)));
- SqlIdentifier owner = sqlIdentifier.getComponent(0);
- result.setOwner(new OwnerSegment(getStartIndex(owner), getStopIndex(owner), new IdentifierValue(names.get(0))));
- return Optional.of(result);
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ExistsSubqueryExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ExistsSubqueryExpressionConverter.java
index 43165667b72..47c496d811e 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ExistsSubqueryExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ExistsSubqueryExpressionConverter.java
@@ -19,14 +19,11 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.e
import lombok.RequiredArgsConstructor;
import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.select.SelectStatementConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExistsSubqueryExpression;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import java.util.Collections;
import java.util.Optional;
@@ -44,25 +41,12 @@ public final class ExistsSubqueryExpressionConverter implements SQLSegmentConver
}
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final ExistsSubqueryExpression expression) {
+ public Optional<SqlBasicCall> convert(final ExistsSubqueryExpression expression) {
if (null == expression) {
return Optional.empty();
}
SqlBasicCall sqlNode = new SqlBasicCall(SqlStdOperatorTable.EXISTS,
- Collections.singletonList(new SelectStatementConverter().convertToSQLNode(expression.getSubquery().getSelect())), SqlParserPos.ZERO);
+ Collections.singletonList(new SelectStatementConverter().convert(expression.getSubquery().getSelect())), SqlParserPos.ZERO);
return expression.isNot() ? Optional.of(new SqlBasicCall(SqlStdOperatorTable.NOT, Collections.singletonList(sqlNode), SqlParserPos.ZERO)) : Optional.of(sqlNode);
}
-
- @Override
- public Optional<ExistsSubqueryExpression> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- SqlNode subquerySqlNode = sqlBasicCall.getOperandList().get(0);
- SelectStatement selectStatement = new SelectStatementConverter().convertToSQLStatement(subquerySqlNode);
- SubquerySegment subquery = new SubquerySegment(getStartIndex(subquerySqlNode) - 1, getStopIndex(subquerySqlNode) + 1, selectStatement);
- ExistsSubqueryExpression result = new ExistsSubqueryExpression(getStartIndex(sqlBasicCall), subquery.getStopIndex(), subquery);
- result.setNot(not);
- return Optional.of(result);
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/FunctionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/FunctionConverter.java
index 744d3964009..386779de22b 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/FunctionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/FunctionConverter.java
@@ -18,20 +18,17 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl;
import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlCharStringLiteral;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlUnresolvedFunction;
import org.apache.calcite.sql.SqlUserDefinedTypeNameSpec;
import org.apache.calcite.sql.fun.SqlCastFunction;
import org.apache.calcite.sql.fun.SqlPositionFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
-import org.apache.shardingsphere.infra.federation.optimizer.converter.context.ConverterContextHolder;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
@@ -50,7 +47,7 @@ import java.util.Optional;
public final class FunctionConverter implements SQLSegmentConverter<FunctionSegment, SqlBasicCall> {
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final FunctionSegment segment) {
+ public Optional<SqlBasicCall> convert(final FunctionSegment segment) {
if ("POSITION".equalsIgnoreCase(segment.getFunctionName())) {
return Optional.of(new SqlBasicCall(new SqlPositionFunction(), getSqlNodes(segment.getParameters()), SqlParserPos.ZERO));
}
@@ -64,43 +61,6 @@ public final class FunctionConverter implements SQLSegmentConverter<FunctionSegm
return Optional.empty();
}
- @Override
- public Optional<FunctionSegment> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- FunctionSegment functionSegment = new FunctionSegment(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall), sqlBasicCall.getOperator().getName(), getFunctionText(sqlBasicCall));
- functionSegment.getParameters().addAll(getParameters(sqlBasicCall));
- return Optional.of(functionSegment);
- }
-
- private String getFunctionText(final SqlBasicCall sqlBasicCall) {
- SqlOperator operator;
- if (null != (operator = sqlBasicCall.getOperator()) && (operator instanceof SqlCastFunction || operator instanceof SqlUnresolvedFunction)) {
- return sqlBasicCall.toString().replace("`", "");
- }
- return sqlBasicCall.toString();
- }
-
- private List<ExpressionSegment> getParameters(final SqlBasicCall sqlBasicCall) {
- List<ExpressionSegment> result = new ArrayList<>();
- sqlBasicCall.getOperandList().forEach(each -> {
- if (each instanceof SqlDataTypeSpec) {
- DataTypeSegment dataTypeSegment = new DataTypeSegment();
- dataTypeSegment.setStartIndex(getStartIndex(each));
- dataTypeSegment.setStopIndex(getStopIndex(each));
- dataTypeSegment.setDataTypeName(each.toString().replace("`", ""));
- result.add(dataTypeSegment);
- } else if (each instanceof SqlCharStringLiteral) {
- result.add(new LiteralExpressionSegment(getStartIndex(each), getStopIndex(each), each.toString().replace("'", "")));
- } else if (each instanceof SqlDynamicParam) {
- ConverterContextHolder.get().getParameterCount().getAndIncrement();
- result.add(new ParameterMarkerExpressionSegment(getStartIndex(each), getStopIndex(each), ((SqlDynamicParam) each).getIndex()));
- }
- });
- return result;
- }
-
private List<SqlNode> getSqlNodes(final Collection<ExpressionSegment> sqlSegments) {
List<SqlNode> sqlNodes = new ArrayList<>();
sqlSegments.forEach(each -> {
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/InExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/InExpressionConverter.java
index c583b9e518a..963d1e7fcdc 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/InExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/InExpressionConverter.java
@@ -17,16 +17,13 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl;
-import lombok.RequiredArgsConstructor;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
-import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.ExpressionConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import java.util.ArrayList;
@@ -38,24 +35,17 @@ import java.util.Optional;
/**
* In expression converter.
*/
-@RequiredArgsConstructor
public final class InExpressionConverter implements SQLSegmentConverter<InExpression, SqlBasicCall> {
- private final boolean not;
-
- public InExpressionConverter() {
- not = false;
- }
-
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final InExpression expression) {
+ public Optional<SqlBasicCall> convert(final InExpression expression) {
if (null == expression) {
return Optional.empty();
}
Collection<SqlNode> sqlNodes = new LinkedList<>();
ExpressionConverter expressionConverter = new ExpressionConverter();
- expressionConverter.convertToSQLNode(expression.getLeft()).ifPresent(sqlNodes::add);
- expressionConverter.convertToSQLNode(expression.getRight()).ifPresent(optional -> {
+ expressionConverter.convert(expression.getLeft()).ifPresent(sqlNodes::add);
+ expressionConverter.convert(expression.getRight()).ifPresent(optional -> {
if (optional instanceof SqlBasicCall) {
sqlNodes.add(new SqlNodeList(((SqlBasicCall) optional).getOperandList(), SqlParserPos.ZERO));
} else {
@@ -65,16 +55,4 @@ public final class InExpressionConverter implements SQLSegmentConverter<InExpres
SqlBasicCall sqlNode = new SqlBasicCall(SqlStdOperatorTable.IN, new ArrayList<>(sqlNodes), SqlParserPos.ZERO);
return expression.isNot() ? Optional.of(new SqlBasicCall(SqlStdOperatorTable.NOT, Collections.singletonList(sqlNode), SqlParserPos.ZERO)) : Optional.of(sqlNode);
}
-
- @Override
- public Optional<InExpression> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- ExpressionConverter expressionConverter = new ExpressionConverter();
- ExpressionSegment left = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(0)).orElseThrow(IllegalStateException::new);
- ExpressionSegment right = expressionConverter.convertToSQLSegment(sqlBasicCall.getOperandList().get(1)).orElseThrow(IllegalStateException::new);
- return Optional.of(new InExpression(getStartIndex(sqlBasicCall),
- sqlBasicCall.getOperandList().get(1) instanceof SqlSelect ? getStopIndex(sqlBasicCall) + 1 : getStopIndex(sqlBasicCall), left, right, not));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ListExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ListExpressionConverter.java
index 08c62746518..c597d6fd271 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ListExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ListExpressionConverter.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.e
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
@@ -28,7 +27,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
import java.util.Arrays;
-import java.util.List;
import java.util.Optional;
/**
@@ -37,10 +35,10 @@ import java.util.Optional;
public final class ListExpressionConverter implements SQLSegmentConverter<ListExpression, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ListExpression segment) {
+ public Optional<SqlNode> convert(final ListExpression segment) {
SqlNode left = null;
for (ExpressionSegment each : segment.getItems()) {
- Optional<SqlNode> sqlNode = new ExpressionConverter().convertToSQLNode(each);
+ Optional<SqlNode> sqlNode = new ExpressionConverter().convert(each);
if (!sqlNode.isPresent()) {
continue;
}
@@ -52,18 +50,4 @@ public final class ListExpressionConverter implements SQLSegmentConverter<ListEx
}
return Optional.ofNullable(left);
}
-
- @Override
- public Optional<ListExpression> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- if (sqlNode instanceof SqlNodeList) {
- List<SqlNode> items = ((SqlNodeList) sqlNode).getList();
- ListExpression result = new ListExpression(getStartIndex(sqlNode), getStopIndex(sqlNode));
- items.forEach(each -> new ExpressionConverter().convertToSQLSegment(each).ifPresent(result.getItems()::add));
- return Optional.of(result);
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
index b1555fe2fab..26c65716735 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
@@ -22,7 +22,6 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import java.util.Optional;
@@ -32,7 +31,7 @@ import java.util.Optional;
public final class LiteralExpressionConverter implements SQLSegmentConverter<LiteralExpressionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final LiteralExpressionSegment segment) {
+ public Optional<SqlNode> convert(final LiteralExpressionSegment segment) {
if (Integer.class == segment.getLiterals().getClass()) {
return Optional.of(SqlLiteral.createExactNumeric(String.valueOf(segment.getLiterals()), SqlParserPos.ZERO));
}
@@ -41,13 +40,4 @@ public final class LiteralExpressionConverter implements SQLSegmentConverter<Lit
}
return Optional.empty();
}
-
- @Override
- public Optional<LiteralExpressionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlLiteral) {
- SqlLiteral sqlLiteral = (SqlLiteral) sqlNode;
- return Optional.of(new LiteralExpressionSegment(getStartIndex(sqlLiteral), getStopIndex(sqlLiteral), SQLUtil.getExactlyValue(sqlLiteral.toValue())));
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ParameterMarkerExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ParameterMarkerExpressionConverter.java
index d3eea124388..166cd9e9c72 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ParameterMarkerExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/ParameterMarkerExpressionConverter.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.e
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParserPos;
-import org.apache.shardingsphere.infra.federation.optimizer.converter.context.ConverterContextHolder;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
@@ -32,13 +31,7 @@ import java.util.Optional;
public final class ParameterMarkerExpressionConverter implements SQLSegmentConverter<ParameterMarkerExpressionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ParameterMarkerExpressionSegment segment) {
+ public Optional<SqlNode> convert(final ParameterMarkerExpressionSegment segment) {
return Optional.of(new SqlDynamicParam(segment.getParameterMarkerIndex(), SqlParserPos.ZERO));
}
-
- @Override
- public Optional<ParameterMarkerExpressionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- ConverterContextHolder.get().getParameterCount().getAndIncrement();
- return Optional.of(new ParameterMarkerExpressionSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), ((SqlDynamicParam) sqlNode).getIndex()));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/SubqueryExpressionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/SubqueryExpressionConverter.java
index 1aa478f1223..acf4e46d29e 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/SubqueryExpressionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/expression/impl/SubqueryExpressionConverter.java
@@ -21,8 +21,6 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.select.SelectStatementConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import java.util.Optional;
@@ -32,22 +30,10 @@ import java.util.Optional;
public final class SubqueryExpressionConverter implements SQLSegmentConverter<SubqueryExpressionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final SubqueryExpressionSegment expression) {
+ public Optional<SqlNode> convert(final SubqueryExpressionSegment expression) {
if (null == expression) {
return Optional.empty();
}
- return Optional.of(new SelectStatementConverter().convertToSQLNode(expression.getSubquery().getSelect()));
- }
-
- @Override
- public Optional<SubqueryExpressionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- SelectStatement selectStatement = new SelectStatementConverter().convertToSQLStatement(sqlNode);
- // FIXME subquery projection position returned by the CalCite parser does not contain two brackets
- int startIndex = getStartIndex(sqlNode) - 1;
- int stopIndex = getStopIndex(sqlNode) + 1;
- return Optional.of(new SubqueryExpressionSegment(new SubquerySegment(startIndex, stopIndex, selectStatement)));
+ return Optional.of(new SelectStatementConverter().convert(expression.getSubquery().getSelect()));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/TableConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/TableConverter.java
index acd4c21082a..adf4d0a1828 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/TableConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/TableConverter.java
@@ -17,12 +17,7 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.from;
-import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlJoin;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlOrderBy;
-import org.apache.calcite.sql.SqlSelect;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.from.impl.JoinTableConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.from.impl.SimpleTableConverter;
@@ -40,41 +35,17 @@ import java.util.Optional;
public final class TableConverter implements SQLSegmentConverter<TableSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final TableSegment segment) {
+ public Optional<SqlNode> convert(final TableSegment segment) {
if (null == segment) {
return Optional.empty();
}
if (segment instanceof SimpleTableSegment) {
- return new SimpleTableConverter().convertToSQLNode((SimpleTableSegment) segment);
+ return new SimpleTableConverter().convert((SimpleTableSegment) segment);
} else if (segment instanceof JoinTableSegment) {
- return new JoinTableConverter().convertToSQLNode((JoinTableSegment) segment).map(optional -> optional);
+ return new JoinTableConverter().convert((JoinTableSegment) segment).map(optional -> optional);
} else if (segment instanceof SubqueryTableSegment) {
- return new SubqueryTableConverter().convertToSQLNode((SubqueryTableSegment) segment).map(optional -> optional);
+ return new SubqueryTableConverter().convert((SubqueryTableSegment) segment).map(optional -> optional);
}
throw new UnsupportedOperationException("Unsupported segment type: " + segment.getClass());
}
-
- @Override
- public Optional<TableSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- if (sqlNode instanceof SqlIdentifier) {
- return new SimpleTableConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- } else if (sqlNode instanceof SqlJoin) {
- return new JoinTableConverter().convertToSQLSegment((SqlJoin) sqlNode).map(optional -> optional);
- } else if (sqlNode instanceof SqlBasicCall) {
- Optional<SqlNode> existSubquery = ((SqlBasicCall) sqlNode).getOperandList().stream().filter(this::containsSqlSelect).findAny();
- if (existSubquery.isPresent()) {
- return new SubqueryTableConverter().convertToSQLSegment((SqlBasicCall) sqlNode).map(optional -> optional);
- } else {
- return new SimpleTableConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- }
- throw new UnsupportedOperationException("Unsupported sql node type: " + sqlNode.getClass());
- }
-
- private boolean containsSqlSelect(final SqlNode sqlNode) {
- return sqlNode instanceof SqlSelect || sqlNode instanceof SqlOrderBy;
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/JoinTableConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/JoinTableConverter.java
index b7781025daa..89e81cbdfc5 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/JoinTableConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/JoinTableConverter.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQ
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.ExpressionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.from.TableConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
import java.util.Optional;
@@ -45,28 +44,15 @@ public final class JoinTableConverter implements SQLSegmentConverter<JoinTableSe
private static final String JOIN_TYPE_FULL = "FULL";
@Override
- public Optional<SqlJoin> convertToSQLNode(final JoinTableSegment segment) {
- SqlNode left = new TableConverter().convertToSQLNode(segment.getLeft()).orElseThrow(IllegalStateException::new);
- SqlNode right = new TableConverter().convertToSQLNode(segment.getRight()).orElseThrow(IllegalStateException::new);
- Optional<SqlNode> condition = new ExpressionConverter().convertToSQLNode(segment.getCondition());
+ public Optional<SqlJoin> convert(final JoinTableSegment segment) {
+ SqlNode left = new TableConverter().convert(segment.getLeft()).orElseThrow(IllegalStateException::new);
+ SqlNode right = new TableConverter().convert(segment.getRight()).orElseThrow(IllegalStateException::new);
+ Optional<SqlNode> condition = new ExpressionConverter().convert(segment.getCondition());
SqlLiteral conditionType = condition.isPresent() ? JoinConditionType.ON.symbol(SqlParserPos.ZERO) : JoinConditionType.NONE.symbol(SqlParserPos.ZERO);
return Optional.of(
new SqlJoin(SqlParserPos.ZERO, left, SqlLiteral.createBoolean(false, SqlParserPos.ZERO), convertJoinType(segment.getJoinType()), right, conditionType, condition.orElse(null)));
}
- @Override
- public Optional<JoinTableSegment> convertToSQLSegment(final SqlJoin sqlJoin) {
- TableSegment left = new TableConverter().convertToSQLSegment(sqlJoin.getLeft()).orElseThrow(IllegalStateException::new);
- TableSegment right = new TableConverter().convertToSQLSegment(sqlJoin.getRight()).orElseThrow(IllegalStateException::new);
- JoinTableSegment result = new JoinTableSegment();
- result.setStartIndex(getStartIndex(sqlJoin));
- result.setStartIndex(getStopIndex(sqlJoin));
- result.setLeft(left);
- result.setRight(right);
- new ExpressionConverter().convertToSQLSegment(sqlJoin.getCondition()).ifPresent(result::setCondition);
- return Optional.of(result);
- }
-
private SqlLiteral convertJoinType(final String joinType) {
if (null == joinType) {
return JoinType.COMMA.symbol(SqlParserPos.ZERO);
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SimpleTableConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SimpleTableConverter.java
index c37928c5a23..cecde44acb4 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SimpleTableConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SimpleTableConverter.java
@@ -23,11 +23,8 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.ArrayList;
import java.util.Arrays;
@@ -40,7 +37,7 @@ import java.util.Optional;
public final class SimpleTableConverter implements SQLSegmentConverter<SimpleTableSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final SimpleTableSegment segment) {
+ public Optional<SqlNode> convert(final SimpleTableSegment segment) {
TableNameSegment tableName = segment.getTableName();
List<String> names = new ArrayList<>();
if (segment.getOwner().isPresent()) {
@@ -54,27 +51,4 @@ public final class SimpleTableConverter implements SQLSegmentConverter<SimpleTab
}
return Optional.of(tableNameSQLNode);
}
-
- @Override
- public Optional<SimpleTableSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlBasicCall) {
- SqlBasicCall sqlBasicCall = (SqlBasicCall) sqlNode;
- if (sqlBasicCall.getOperator().equals(SqlStdOperatorTable.AS)) {
- String name = sqlBasicCall.getOperandList().get(0).toString();
- SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), new IdentifierValue(name)));
- SqlNode alias = sqlBasicCall.getOperandList().get(1);
- tableSegment.setAlias(new AliasSegment(getStartIndex(alias), getStopIndex(alias), new IdentifierValue(alias.toString())));
- return Optional.of(tableSegment);
- }
- }
- if (sqlNode instanceof SqlIdentifier) {
- List<String> names = ((SqlIdentifier) sqlNode).names;
- SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new TableNameSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), new IdentifierValue(names.get(names.size() - 1))));
- if (2 == names.size()) {
- simpleTableSegment.setOwner(new OwnerSegment(getStartIndex(sqlNode), getStartIndex(sqlNode) + names.get(0).length() - 1, new IdentifierValue(names.get(0))));
- }
- return Optional.of(simpleTableSegment);
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SubqueryTableConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SubqueryTableConverter.java
index 2342fe9db62..e224c96722f 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SubqueryTableConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/from/impl/SubqueryTableConverter.java
@@ -24,11 +24,7 @@ import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.select.SelectStatementConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.ArrayList;
import java.util.Collection;
@@ -41,25 +37,13 @@ import java.util.Optional;
public final class SubqueryTableConverter implements SQLSegmentConverter<SubqueryTableSegment, SqlBasicCall> {
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final SubqueryTableSegment segment) {
+ public Optional<SqlBasicCall> convert(final SubqueryTableSegment segment) {
if (null == segment) {
return Optional.empty();
}
Collection<SqlNode> sqlNodes = new LinkedList<>();
- sqlNodes.add(new SelectStatementConverter().convertToSQLNode(segment.getSubquery().getSelect()));
+ sqlNodes.add(new SelectStatementConverter().convert(segment.getSubquery().getSelect()));
segment.getAlias().ifPresent(optional -> sqlNodes.add(new SqlIdentifier(optional, SqlParserPos.ZERO)));
return Optional.of(new SqlBasicCall(SqlStdOperatorTable.AS, new ArrayList<>(sqlNodes), SqlParserPos.ZERO));
}
-
- @Override
- public Optional<SubqueryTableSegment> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- SqlNode select = sqlBasicCall.getOperandList().get(0);
- SelectStatement selectStatement = new SelectStatementConverter().convertToSQLStatement(select);
- SubqueryTableSegment result = new SubqueryTableSegment(new SubquerySegment(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall), selectStatement));
- if (sqlBasicCall.getOperator().equals(SqlStdOperatorTable.AS)) {
- SqlNode alias = sqlBasicCall.getOperandList().get(1);
- result.setAlias(new AliasSegment(getStartIndex(alias), getStopIndex(alias), new IdentifierValue(alias.toString())));
- }
- return Optional.of(result);
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/GroupByConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/GroupByConverter.java
index 2712e7e7ce9..7c154d40c80 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/GroupByConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/GroupByConverter.java
@@ -22,9 +22,7 @@ import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.orderby.item.OrderByItemConverterUtil;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
-import java.util.Collection;
import java.util.Optional;
/**
@@ -33,18 +31,9 @@ import java.util.Optional;
public final class GroupByConverter implements SQLSegmentConverter<GroupBySegment, SqlNodeList> {
@Override
- public Optional<SqlNodeList> convertToSQLNode(final GroupBySegment segment) {
+ public Optional<SqlNodeList> convert(final GroupBySegment segment) {
return null == segment || segment.getGroupByItems().isEmpty()
? Optional.empty()
- : Optional.of(new SqlNodeList(OrderByItemConverterUtil.convertToSQLNode(segment.getGroupByItems()), SqlParserPos.ZERO));
- }
-
- @Override
- public Optional<GroupBySegment> convertToSQLSegment(final SqlNodeList sqlNodeList) {
- if (null == sqlNodeList || 0 == sqlNodeList.size()) {
- return Optional.empty();
- }
- Collection<OrderByItemSegment> orderByItems = OrderByItemConverterUtil.convertToSQLSegment(sqlNodeList);
- return Optional.of(new GroupBySegment(getStartIndex(sqlNodeList), getStopIndex(sqlNodeList), orderByItems));
+ : Optional.of(new SqlNodeList(OrderByItemConverterUtil.convert(segment.getGroupByItems()), SqlParserPos.ZERO));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/HavingConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/HavingConverter.java
index 76644d2e796..473accca076 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/HavingConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/groupby/HavingConverter.java
@@ -32,16 +32,7 @@ public final class HavingConverter implements SQLSegmentConverter<HavingSegment,
private static final int HAVING_SEGMENT_LENGTH = 7;
@Override
- public Optional<SqlNode> convertToSQLNode(final HavingSegment segment) {
- return null == segment ? Optional.empty() : new ExpressionConverter().convertToSQLNode(segment.getExpr());
- }
-
- @Override
- public Optional<HavingSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- // FIXME Now sqlNode position returned by the calcite parser does not contain HAVING and requires manual calculation
- return new ExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> new HavingSegment(getStartIndex(sqlNode) - HAVING_SEGMENT_LENGTH, getStopIndex(sqlNode), optional));
+ public Optional<SqlNode> convert(final HavingSegment segment) {
+ return null == segment ? Optional.empty() : new ExpressionConverter().convert(segment.getExpr());
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/limit/PaginationValueSQLConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/limit/PaginationValueSQLConverter.java
index 3cd125af31b..ae17bee9435 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/limit/PaginationValueSQLConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/limit/PaginationValueSQLConverter.java
@@ -21,13 +21,11 @@ import lombok.RequiredArgsConstructor;
import org.apache.calcite.sql.SqlDynamicParam;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNumericLiteral;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.context.ConverterContext;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.NumberLiteralPaginationValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.PaginationValueSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
import java.util.Optional;
@@ -41,7 +39,7 @@ public final class PaginationValueSQLConverter implements SQLSegmentConverter<Pa
private final ConverterContext context;
@Override
- public Optional<SqlNode> convertToSQLNode(final PaginationValueSegment segment) {
+ public Optional<SqlNode> convert(final PaginationValueSegment segment) {
return Optional.of(segment instanceof NumberLiteralPaginationValueSegment
? getLiteralSQLNode((NumberLiteralPaginationValueSegment) segment)
: getParameterMarkerSQLNode((ParameterMarkerLimitValueSegment) segment));
@@ -54,16 +52,4 @@ public final class PaginationValueSQLConverter implements SQLSegmentConverter<Pa
private SqlNode getParameterMarkerSQLNode(final ParameterMarkerLimitValueSegment segment) {
return new SqlDynamicParam(segment.getParameterIndex(), SqlParserPos.ZERO);
}
-
- @Override
- public Optional<PaginationValueSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlNumericLiteral) {
- return Optional.of(new NumberLiteralLimitValueSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), ((SqlNumericLiteral) sqlNode).getValueAs(Long.class)));
- }
- if (sqlNode instanceof SqlDynamicParam) {
- context.getParameterCount().incrementAndGet();
- return Optional.of(new ParameterMarkerLimitValueSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), ((SqlDynamicParam) sqlNode).getIndex()));
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/OrderByConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/OrderByConverter.java
index 356848d6bcb..d4d35d03450 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/OrderByConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/OrderByConverter.java
@@ -22,9 +22,7 @@ import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.orderby.item.OrderByItemConverterUtil;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
-import java.util.Collection;
import java.util.Optional;
/**
@@ -33,16 +31,7 @@ import java.util.Optional;
public final class OrderByConverter implements SQLSegmentConverter<OrderBySegment, SqlNodeList> {
@Override
- public Optional<SqlNodeList> convertToSQLNode(final OrderBySegment segment) {
- return null == segment ? Optional.empty() : Optional.of(new SqlNodeList(OrderByItemConverterUtil.convertToSQLNode(segment.getOrderByItems()), SqlParserPos.ZERO));
- }
-
- @Override
- public Optional<OrderBySegment> convertToSQLSegment(final SqlNodeList sqlNodeList) {
- if (null == sqlNodeList || 0 == sqlNodeList.size()) {
- return Optional.empty();
- }
- Collection<OrderByItemSegment> orderByItems = OrderByItemConverterUtil.convertToSQLSegment(sqlNodeList);
- return Optional.of(new OrderBySegment(getStartIndex(sqlNodeList), getStopIndex(sqlNodeList), orderByItems));
+ public Optional<SqlNodeList> convert(final OrderBySegment segment) {
+ return null == segment ? Optional.empty() : Optional.of(new SqlNodeList(OrderByItemConverterUtil.convert(segment.getOrderByItems()), SqlParserPos.ZERO));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ColumnOrderByItemConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ColumnOrderByItemConverter.java
index 4d3fd383574..6cc10f574e5 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ColumnOrderByItemConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ColumnOrderByItemConverter.java
@@ -18,17 +18,13 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.orderby.item;
import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.ColumnConverter;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Collections;
import java.util.Objects;
@@ -40,28 +36,11 @@ import java.util.Optional;
public final class ColumnOrderByItemConverter implements SQLSegmentConverter<ColumnOrderByItemSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ColumnOrderByItemSegment segment) {
- Optional<SqlNode> result = new ColumnConverter().convertToSQLNode(segment.getColumn()).map(optional -> optional);
+ public Optional<SqlNode> convert(final ColumnOrderByItemSegment segment) {
+ Optional<SqlNode> result = new ColumnConverter().convert(segment.getColumn()).map(optional -> optional);
if (result.isPresent() && Objects.equals(OrderDirection.DESC, segment.getOrderDirection())) {
result = Optional.of(new SqlBasicCall(SqlStdOperatorTable.DESC, Collections.singletonList(result.get()), SqlParserPos.ZERO));
}
return result;
}
-
- @Override
- public Optional<ColumnOrderByItemSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (!(sqlNode instanceof SqlIdentifier)) {
- return Optional.empty();
- }
- SqlIdentifier sqlIdentifier = (SqlIdentifier) sqlNode;
- if (sqlIdentifier.names.size() > 1) {
- SqlIdentifier column = sqlIdentifier.getComponent(1);
- SqlIdentifier owner = sqlIdentifier.getComponent(0);
- ColumnSegment columnSegment = new ColumnSegment(getStartIndex(sqlIdentifier), getStopIndex(sqlIdentifier), new IdentifierValue(column.toString()));
- columnSegment.setOwner(new OwnerSegment(getStartIndex(owner), getStopIndex(owner), new IdentifierValue(owner.toString())));
- return Optional.of(new ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC));
- }
- ColumnSegment columnSegment = new ColumnSegment(getStartIndex(sqlIdentifier), getStopIndex(sqlIdentifier), new IdentifierValue(sqlIdentifier.names.get(0)));
- return Optional.of(new ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ExpressionOrderByItemConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ExpressionOrderByItemConverter.java
index 869cc9c7c17..2162bddbd8c 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ExpressionOrderByItemConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/ExpressionOrderByItemConverter.java
@@ -29,13 +29,8 @@ import java.util.Optional;
public final class ExpressionOrderByItemConverter implements SQLSegmentConverter<ExpressionOrderByItemSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ExpressionOrderByItemSegment segment) {
+ public Optional<SqlNode> convert(final ExpressionOrderByItemSegment segment) {
// TODO
return Optional.empty();
}
-
- @Override
- public Optional<ExpressionOrderByItemSegment> convertToSQLSegment(final SqlNode sqlNode) {
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/IndexOrderByItemConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/IndexOrderByItemConverter.java
index df1728be40e..bb6f76e2686 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/IndexOrderByItemConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/IndexOrderByItemConverter.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.o
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNumericLiteral;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
@@ -36,21 +35,11 @@ import java.util.Optional;
public final class IndexOrderByItemConverter implements SQLSegmentConverter<IndexOrderByItemSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final IndexOrderByItemSegment segment) {
+ public Optional<SqlNode> convert(final IndexOrderByItemSegment segment) {
SqlNode sqlNode = SqlLiteral.createExactNumeric(String.valueOf(segment.getColumnIndex()), SqlParserPos.ZERO);
if (OrderDirection.DESC.equals(segment.getOrderDirection())) {
sqlNode = new SqlBasicCall(SqlStdOperatorTable.DESC, Collections.singletonList(sqlNode), SqlParserPos.ZERO);
}
return Optional.of(sqlNode);
}
-
- @Override
- public Optional<IndexOrderByItemSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlBasicCall && SqlStdOperatorTable.DESC.equals(((SqlBasicCall) sqlNode).getOperator())) {
- SqlNumericLiteral sqlNumericLiteral = (SqlNumericLiteral) ((SqlBasicCall) sqlNode).getOperandList().get(0);
- return Optional.of(new IndexOrderByItemSegment(getStartIndex(sqlNumericLiteral),
- getStopIndex(sqlNumericLiteral), sqlNumericLiteral.getValueAs(Integer.class), OrderDirection.DESC));
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/OrderByItemConverterUtil.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/OrderByItemConverterUtil.java
index b110f54b7b5..066b1358646 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/OrderByItemConverterUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/orderby/item/OrderByItemConverterUtil.java
@@ -19,9 +19,7 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.o
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNodeList;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
@@ -43,37 +41,19 @@ public final class OrderByItemConverterUtil {
* @param orderByItems order by item list
* @return SQL nodes converted by order by item
*/
- public static Collection<SqlNode> convertToSQLNode(final Collection<OrderByItemSegment> orderByItems) {
+ public static Collection<SqlNode> convert(final Collection<OrderByItemSegment> orderByItems) {
Collection<SqlNode> result = new LinkedList<>();
for (OrderByItemSegment each : orderByItems) {
if (each instanceof ColumnOrderByItemSegment) {
- new ColumnOrderByItemConverter().convertToSQLNode((ColumnOrderByItemSegment) each).ifPresent(result::add);
+ new ColumnOrderByItemConverter().convert((ColumnOrderByItemSegment) each).ifPresent(result::add);
} else if (each instanceof ExpressionOrderByItemSegment) {
throw new UnsupportedOperationException("unsupported ExpressionOrderByItemSegment");
} else if (each instanceof IndexOrderByItemSegment) {
- new IndexOrderByItemConverter().convertToSQLNode((IndexOrderByItemSegment) each).ifPresent(result::add);
+ new IndexOrderByItemConverter().convert((IndexOrderByItemSegment) each).ifPresent(result::add);
} else if (each instanceof TextOrderByItemSegment) {
throw new UnsupportedOperationException("unsupported TextOrderByItemSegment");
}
}
return result;
}
-
- /**
- * Convert sql node list to order by items.
- *
- * @param sqlNodeList sql node list
- * @return order by items converted by sql node list
- */
- public static Collection<OrderByItemSegment> convertToSQLSegment(final SqlNodeList sqlNodeList) {
- Collection<OrderByItemSegment> result = new LinkedList<>();
- for (SqlNode each : sqlNodeList) {
- if (each instanceof SqlIdentifier) {
- new ColumnOrderByItemConverter().convertToSQLSegment(each).ifPresent(result::add);
- } else {
- new IndexOrderByItemConverter().convertToSQLSegment(each).ifPresent(result::add);
- }
- }
- return result;
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/DistinctConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/DistinctConverter.java
index ec9f90016d0..ac78f9c6a64 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/DistinctConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/DistinctConverter.java
@@ -32,12 +32,7 @@ import java.util.Optional;
public final class DistinctConverter implements SQLSegmentConverter<ProjectionsSegment, SqlNodeList> {
@Override
- public Optional<SqlNodeList> convertToSQLNode(final ProjectionsSegment segment) {
+ public Optional<SqlNodeList> convert(final ProjectionsSegment segment) {
return segment.isDistinctRow() ? Optional.of(new SqlNodeList(Collections.singletonList(SqlSelectKeyword.DISTINCT.symbol(SqlParserPos.ZERO)), SqlParserPos.ZERO)) : Optional.empty();
}
-
- @Override
- public Optional<ProjectionsSegment> convertToSQLSegment(final SqlNodeList sqlNode) {
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/ProjectionsConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/ProjectionsConverter.java
index b52a2bfae10..91515a68a7a 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/ProjectionsConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/ProjectionsConverter.java
@@ -17,15 +17,8 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection;
-import com.google.common.collect.ImmutableList;
-import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
-import org.apache.calcite.sql.SqlNumericLiteral;
-import org.apache.calcite.sql.SqlOrderBy;
-import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl.AggregationProjectionConverter;
@@ -33,7 +26,6 @@ import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.pr
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl.ExpressionProjectionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl.ShorthandProjectionConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl.SubqueryProjectionConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
@@ -42,10 +34,8 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Projecti
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.List;
import java.util.Optional;
/**
@@ -54,7 +44,7 @@ import java.util.Optional;
public final class ProjectionsConverter implements SQLSegmentConverter<ProjectionsSegment, SqlNodeList> {
@Override
- public Optional<SqlNodeList> convertToSQLNode(final ProjectionsSegment segment) {
+ public Optional<SqlNodeList> convert(final ProjectionsSegment segment) {
Collection<SqlNode> projectionSQLNodes = new LinkedList<>();
for (ProjectionSegment each : segment.getProjections()) {
getProjectionSQLNode(each).ifPresent(projectionSQLNodes::add);
@@ -64,66 +54,17 @@ public final class ProjectionsConverter implements SQLSegmentConverter<Projectio
private Optional<SqlNode> getProjectionSQLNode(final ProjectionSegment segment) {
if (segment instanceof ColumnProjectionSegment) {
- return new ColumnProjectionConverter().convertToSQLNode((ColumnProjectionSegment) segment);
+ return new ColumnProjectionConverter().convert((ColumnProjectionSegment) segment);
} else if (segment instanceof ExpressionProjectionSegment) {
- return new ExpressionProjectionConverter().convertToSQLNode((ExpressionProjectionSegment) segment);
+ return new ExpressionProjectionConverter().convert((ExpressionProjectionSegment) segment);
} else if (segment instanceof ShorthandProjectionSegment) {
- return new ShorthandProjectionConverter().convertToSQLNode((ShorthandProjectionSegment) segment).map(optional -> optional);
+ return new ShorthandProjectionConverter().convert((ShorthandProjectionSegment) segment).map(optional -> optional);
} else if (segment instanceof SubqueryProjectionSegment) {
- return new SubqueryProjectionConverter().convertToSQLNode((SubqueryProjectionSegment) segment);
+ return new SubqueryProjectionConverter().convert((SubqueryProjectionSegment) segment);
} else if (segment instanceof AggregationProjectionSegment) {
- return new AggregationProjectionConverter().convertToSQLNode((AggregationProjectionSegment) segment).map(optional -> optional);
+ return new AggregationProjectionConverter().convert((AggregationProjectionSegment) segment).map(optional -> optional);
}
// TODO process other projection
return Optional.empty();
}
-
- @Override
- public Optional<ProjectionsSegment> convertToSQLSegment(final SqlNodeList sqlNodeList) {
- List<ProjectionSegment> projections = new ArrayList<>();
- for (SqlNode each : sqlNodeList) {
- getProjectionSegment(each).ifPresent(projections::add);
- }
- int startIndex = projections.get(0).getStartIndex();
- int stopIndex = getProjectionsSegmentStopIndex(sqlNodeList.get(sqlNodeList.size() - 1), projections.get(projections.size() - 1));
- ProjectionsSegment result = new ProjectionsSegment(startIndex, stopIndex);
- result.getProjections().addAll(projections);
- return Optional.of(result);
- }
-
- private int getProjectionsSegmentStopIndex(final SqlNode lastSqlNode, final ProjectionSegment projectionSegment) {
- int stopIndex = projectionSegment.getStopIndex();
- if (lastSqlNode instanceof SqlBasicCall && SqlKind.AS == ((SqlBasicCall) lastSqlNode).getOperator().getKind()) {
- stopIndex = getStopIndex(((SqlBasicCall) lastSqlNode).getOperandList().get(1));
- }
- return stopIndex;
- }
-
- private Optional<ProjectionSegment> getProjectionSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlIdentifier) {
- SqlIdentifier sqlIdentifier = (SqlIdentifier) sqlNode;
- if (SqlIdentifier.STAR.names.equals(sqlIdentifier.names) || isOwnerShorthandProjection(sqlIdentifier)) {
- return new ShorthandProjectionConverter().convertToSQLSegment(sqlIdentifier).map(optional -> optional);
- }
- return new ColumnProjectionConverter().convertToSQLSegment(sqlIdentifier).map(optional -> optional);
- } else if (sqlNode instanceof SqlBasicCall) {
- SqlBasicCall sqlBasicCall = (SqlBasicCall) sqlNode;
- if (AggregationType.isAggregationType(sqlBasicCall.getOperator().getName()) || AggregationProjectionConverter.isAsOperatorAggregationType(sqlBasicCall)) {
- return new AggregationProjectionConverter().convertToSQLSegment(sqlBasicCall).map(optional -> optional);
- }
- if (null != sqlBasicCall.getOperator() && SqlKind.AS == sqlBasicCall.getOperator().getKind() && !(sqlBasicCall.getOperandList().get(0) instanceof SqlNumericLiteral)) {
- return new ColumnProjectionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- return new ExpressionProjectionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- } else if (sqlNode instanceof SqlSelect || sqlNode instanceof SqlOrderBy) {
- return new SubqueryProjectionConverter().convertToSQLSegment(sqlNode).map(optional -> optional);
- }
- // TODO process other projection
- return Optional.empty();
- }
-
- private boolean isOwnerShorthandProjection(final SqlIdentifier sqlIdentifier) {
- return 2 == sqlIdentifier.names.size()
- && SqlIdentifier.STAR.names.equals(ImmutableList.of(sqlIdentifier.names.get(1)));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/AggregationProjectionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/AggregationProjectionConverter.java
index 0a30f1c80b3..5916f40cfb3 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/AggregationProjectionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/AggregationProjectionConverter.java
@@ -22,20 +22,15 @@ import com.google.common.base.Splitter;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSelectKeyword;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
-import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Arrays;
import java.util.Collections;
@@ -43,7 +38,6 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
-import java.util.stream.Collectors;
/**
* Aggregation projection converter.
@@ -66,7 +60,7 @@ public final class AggregationProjectionConverter implements SQLSegmentConverter
}
@Override
- public Optional<SqlBasicCall> convertToSQLNode(final AggregationProjectionSegment segment) {
+ public Optional<SqlBasicCall> convert(final AggregationProjectionSegment segment) {
if (null == segment) {
return Optional.empty();
}
@@ -85,71 +79,11 @@ public final class AggregationProjectionConverter implements SQLSegmentConverter
Collections.singletonList(createParametersSqlNode(parameters)), SqlParserPos.ZERO, functionQuantifier).withExpanded(false));
}
- @Override
- public Optional<AggregationProjectionSegment> convertToSQLSegment(final SqlBasicCall sqlBasicCall) {
- if (null == sqlBasicCall) {
- return Optional.empty();
- }
- if (isAsOperatorAggregationType(sqlBasicCall)) {
- SqlBasicCall subSqlBasicCall = (SqlBasicCall) sqlBasicCall.getOperandList().get(0);
- AggregationType aggregationType = AggregationType.valueOf(subSqlBasicCall.getOperator().getName().toUpperCase());
- String innerExpression = getInnerExpression(subSqlBasicCall);
- AliasSegment aliasSegment = new AliasSegment(getStartIndex(sqlBasicCall.getOperandList().get(1)), getStopIndex(sqlBasicCall.getOperandList().get(1)),
- new IdentifierValue(((SqlIdentifier) sqlBasicCall.getOperandList().get(1)).names.get(0)));
- if (null != subSqlBasicCall.getFunctionQuantifier() && SqlSelectKeyword.DISTINCT == subSqlBasicCall.getFunctionQuantifier().getValue()) {
- return Optional.of(getAggregationDistinctProjectionSegment(subSqlBasicCall, aggregationType, aliasSegment));
- }
- AggregationProjectionSegment aggregationProjectionSegment = new AggregationProjectionSegment(getStartIndex(subSqlBasicCall), getStopIndex(subSqlBasicCall),
- aggregationType, innerExpression);
- aggregationProjectionSegment.setAlias(aliasSegment);
- return Optional.of(aggregationProjectionSegment);
- }
- AggregationType aggregationType = AggregationType.valueOf(sqlBasicCall.getOperator().getName());
- if (null != sqlBasicCall.getFunctionQuantifier() && SqlSelectKeyword.DISTINCT == sqlBasicCall.getFunctionQuantifier().getValue()) {
- return Optional.of(getAggregationDistinctProjectionSegment(sqlBasicCall, aggregationType, null));
- }
- String innerExpression = getInnerExpression(sqlBasicCall);
- return Optional.of(new AggregationProjectionSegment(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall), aggregationType, innerExpression));
- }
-
- private AggregationDistinctProjectionSegment getAggregationDistinctProjectionSegment(final SqlBasicCall sqlBasicCall, final AggregationType aggregationType, final AliasSegment aliasSegment) {
- String innerExpression = getInnerExpression(sqlBasicCall, SqlSelectKeyword.DISTINCT);
- String distinctParams = sqlBasicCall.getOperandList().stream().map(SqlNode::toString).collect(Collectors.joining(", "));
- AggregationDistinctProjectionSegment aggregationDistinctProjectionSegment = new AggregationDistinctProjectionSegment(getStartIndex(sqlBasicCall), getStopIndex(sqlBasicCall),
- aggregationType, innerExpression, distinctParams);
- aggregationDistinctProjectionSegment.setAlias(aliasSegment);
- return aggregationDistinctProjectionSegment;
- }
-
- private String getInnerExpression(final SqlBasicCall sqlBasicCall, final SqlSelectKeyword selectKeyword) {
- if (selectKeyword == null) {
- return getInnerExpression(sqlBasicCall);
- }
- String params = sqlBasicCall.getOperandList().stream().map(SqlNode::toString).collect(Collectors.joining(", "));
- return QuoteCharacter.PARENTHESES.wrap(selectKeyword + " " + params);
- }
-
- private String getInnerExpression(final SqlBasicCall sqlBasicCall) {
- String params = sqlBasicCall.getOperandList().stream().map(SqlNode::toString).collect(Collectors.joining(", "));
- return QuoteCharacter.PARENTHESES.wrap(params);
- }
-
private SqlAggFunction convertOperator(final String operator) {
Preconditions.checkState(REGISTRY.containsKey(operator), "Unsupported SQL operator: `%s`", operator);
return REGISTRY.get(operator);
}
- /**
- * Judge whether sqlBasicCall is as operator aggregation type or not.
- * @param sqlBasicCall sqlBasicCall
- * @return whether sqlBasicCall is as operator aggregation type or not
- */
- public static boolean isAsOperatorAggregationType(final SqlBasicCall sqlBasicCall) {
- return null != sqlBasicCall.getOperator() && SqlKind.AS == sqlBasicCall.getOperator().getKind()
- && sqlBasicCall.getOperandList().get(0) instanceof SqlBasicCall
- && AggregationType.isAggregationType(((SqlBasicCall) sqlBasicCall.getOperandList().get(0)).getOperator().getName());
- }
-
private SqlNode createParametersSqlNode(final List<String> parameters) {
if (1 == parameters.size()) {
try {
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ColumnProjectionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ColumnProjectionConverter.java
index 925331bc21b..a8d4b6bf2c3 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ColumnProjectionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ColumnProjectionConverter.java
@@ -24,13 +24,9 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.impl.ColumnConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Arrays;
-import java.util.List;
import java.util.Optional;
/**
@@ -39,33 +35,12 @@ import java.util.Optional;
public final class ColumnProjectionConverter implements SQLSegmentConverter<ColumnProjectionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ColumnProjectionSegment segment) {
+ public Optional<SqlNode> convert(final ColumnProjectionSegment segment) {
if (segment.getAlias().isPresent()) {
- Optional<SqlIdentifier> columnSqlIdentifier = new ColumnConverter().convertToSQLNode(segment.getColumn());
+ Optional<SqlIdentifier> columnSqlIdentifier = new ColumnConverter().convert(segment.getColumn());
SqlIdentifier aliasSqlIdentifier = new SqlIdentifier(segment.getAlias().get(), SqlParserPos.ZERO);
return Optional.of(new SqlBasicCall(new SqlAsOperator(), Arrays.asList(columnSqlIdentifier.get(), aliasSqlIdentifier), SqlParserPos.ZERO));
}
- return new ColumnConverter().convertToSQLNode(segment.getColumn()).map(optional -> optional);
- }
-
- @Override
- public Optional<ColumnProjectionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlBasicCall) {
- List<SqlNode> operands = ((SqlBasicCall) sqlNode).getOperandList();
- Optional<ColumnSegment> columnSegment = new ColumnConverter().convertToSQLSegment((SqlIdentifier) operands.get(0));
- if (!columnSegment.isPresent()) {
- return Optional.empty();
- }
- ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment(columnSegment.get());
- if (2 == operands.size()) {
- SqlIdentifier aliasSqlNode = (SqlIdentifier) operands.get(1);
- columnProjectionSegment.setAlias(new AliasSegment(getStartIndex(aliasSqlNode), getStopIndex(aliasSqlNode), new IdentifierValue(aliasSqlNode.names.get(0))));
- }
- return Optional.of(columnProjectionSegment);
- }
- if (sqlNode instanceof SqlIdentifier) {
- return new ColumnConverter().convertToSQLSegment((SqlIdentifier) sqlNode).map(ColumnProjectionSegment::new);
- }
- return Optional.empty();
+ return new ColumnConverter().convert(segment.getColumn()).map(optional -> optional);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ExpressionProjectionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ExpressionProjectionConverter.java
index 913cf628dab..72b92cc7ed5 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ExpressionProjectionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ExpressionProjectionConverter.java
@@ -19,18 +19,12 @@ package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.p
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNumericLiteral;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.expression.ExpressionConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Arrays;
import java.util.Collections;
@@ -42,35 +36,15 @@ import java.util.Optional;
public final class ExpressionProjectionConverter implements SQLSegmentConverter<ExpressionProjectionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final ExpressionProjectionSegment segment) {
+ public Optional<SqlNode> convert(final ExpressionProjectionSegment segment) {
if (null == segment) {
return Optional.empty();
}
- Optional<SqlNode> result = new ExpressionConverter().convertToSQLNode(segment.getExpr());
+ Optional<SqlNode> result = new ExpressionConverter().convert(segment.getExpr());
if (result.isPresent() && segment.getAlias().isPresent()) {
return Optional.of(new SqlBasicCall(SqlStdOperatorTable.AS, Arrays.asList(result.get(),
SqlIdentifier.star(Collections.singletonList(segment.getAlias().get()), SqlParserPos.ZERO, Collections.singletonList(SqlParserPos.ZERO))), SqlParserPos.ZERO));
}
return result;
}
-
- @Override
- public Optional<ExpressionProjectionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlBasicCall) {
- SqlBasicCall sqlBasicCall = (SqlBasicCall) sqlNode;
- if (SqlKind.AS == sqlBasicCall.getOperator().getKind() && sqlBasicCall.getOperandList().get(0) instanceof SqlNumericLiteral) {
- SqlNode exprSqlNode = sqlBasicCall.getOperandList().get(0);
- SqlNode aliasSqlNode = sqlBasicCall.getOperandList().get(1);
- ExpressionSegment expressionSegment = new ExpressionConverter().convertToSQLSegment(exprSqlNode).orElse(null);
- ExpressionProjectionSegment expressionProjectionSegment = new ExpressionProjectionSegment(getStartIndex(sqlBasicCall),
- getStopIndex(sqlBasicCall), exprSqlNode.toString(), expressionSegment);
- expressionProjectionSegment.setAlias(new AliasSegment(getStartIndex(aliasSqlNode), getStopIndex(aliasSqlNode), new IdentifierValue(aliasSqlNode.toString())));
- return Optional.of(expressionProjectionSegment);
- }
- ExpressionSegment expressionSegment = new ExpressionConverter().convertToSQLSegment(sqlNode).orElse(null);
- String text = expressionSegment instanceof FunctionSegment ? ((FunctionSegment) expressionSegment).getText() : sqlNode.toString();
- return Optional.of(new ExpressionProjectionSegment(getStartIndex(sqlNode), getStopIndex(sqlNode), text, expressionSegment));
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ShorthandProjectionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ShorthandProjectionConverter.java
index 3931a5887c5..dd9d448691b 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ShorthandProjectionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/ShorthandProjectionConverter.java
@@ -22,8 +22,6 @@ import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Arrays;
import java.util.Optional;
@@ -34,24 +32,11 @@ import java.util.Optional;
public final class ShorthandProjectionConverter implements SQLSegmentConverter<ShorthandProjectionSegment, SqlIdentifier> {
@Override
- public Optional<SqlIdentifier> convertToSQLNode(final ShorthandProjectionSegment segment) {
+ public Optional<SqlIdentifier> convert(final ShorthandProjectionSegment segment) {
if (null == segment) {
return Optional.empty();
}
return segment.getOwner().map(optional -> Optional.of(SqlIdentifier.star(Arrays.asList(optional.getIdentifier().getValue(), ""),
SqlParserPos.ZERO, ImmutableList.of(SqlParserPos.ZERO)))).orElseGet(() -> Optional.of(SqlIdentifier.star(SqlParserPos.ZERO)));
}
-
- @Override
- public Optional<ShorthandProjectionSegment> convertToSQLSegment(final SqlIdentifier sqlIdentifier) {
- if (null == sqlIdentifier) {
- return Optional.empty();
- }
- ShorthandProjectionSegment result = new ShorthandProjectionSegment(getStartIndex(sqlIdentifier), getStopIndex(sqlIdentifier));
- if (sqlIdentifier.names.size() > 1) {
- SqlIdentifier owner = sqlIdentifier.getComponent(0);
- result.setOwner(new OwnerSegment(getStartIndex(owner), getStopIndex(owner), new IdentifierValue(sqlIdentifier.names.get(0))));
- }
- return Optional.of(result);
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/SubqueryProjectionConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/SubqueryProjectionConverter.java
index 642956326f1..1ec5e90f7f8 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/SubqueryProjectionConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/projection/impl/SubqueryProjectionConverter.java
@@ -17,19 +17,14 @@
package org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.impl;
-import org.apache.calcite.avatica.util.Quoting;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlOrderBy;
-import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.select.SelectStatementConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import java.util.ArrayList;
import java.util.Collection;
@@ -42,11 +37,11 @@ import java.util.Optional;
public final class SubqueryProjectionConverter implements SQLSegmentConverter<SubqueryProjectionSegment, SqlNode> {
@Override
- public Optional<SqlNode> convertToSQLNode(final SubqueryProjectionSegment segment) {
+ public Optional<SqlNode> convert(final SubqueryProjectionSegment segment) {
if (null == segment) {
return Optional.empty();
}
- SqlNode sqlNode = new SelectStatementConverter().convertToSQLNode(segment.getSubquery().getSelect());
+ SqlNode sqlNode = new SelectStatementConverter().convert(segment.getSubquery().getSelect());
return segment.getAlias().isPresent() ? convertToSQLStatement(sqlNode, segment.getAlias().get()) : Optional.of(sqlNode);
}
@@ -56,18 +51,4 @@ public final class SubqueryProjectionConverter implements SQLSegmentConverter<Su
sqlNodes.add(new SqlIdentifier(alias, SqlParserPos.ZERO));
return Optional.of(new SqlBasicCall(SqlStdOperatorTable.AS, new ArrayList<>(sqlNodes), SqlParserPos.ZERO));
}
-
- @Override
- public Optional<SubqueryProjectionSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (sqlNode instanceof SqlSelect || sqlNode instanceof SqlOrderBy) {
- SelectStatement selectStatement = new SelectStatementConverter().convertToSQLStatement(sqlNode);
- // FIXME subquery projection position returned by the CalCite parser does not contain two brackets
- int startIndex = getStartIndex(sqlNode) - 1;
- int stopIndex = getStopIndex(sqlNode) + 1;
- String text = "(" + sqlNode + ")";
- String originalText = text.replace(System.lineSeparator(), " ").replace(Quoting.BACK_TICK.string, "");
- return Optional.of(new SubqueryProjectionSegment(new SubquerySegment(startIndex, stopIndex, selectStatement), originalText));
- }
- return Optional.empty();
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/where/WhereConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/where/WhereConverter.java
index 73f99ef39c7..7363ea8aadc 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/where/WhereConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/segment/where/WhereConverter.java
@@ -29,20 +29,8 @@ import java.util.Optional;
*/
public final class WhereConverter implements SQLSegmentConverter<WhereSegment, SqlNode> {
- private static final int WHERE_SEGMENT_LENGTH = 6;
-
- @Override
- public Optional<SqlNode> convertToSQLNode(final WhereSegment segment) {
- return null == segment ? Optional.empty() : new ExpressionConverter().convertToSQLNode(segment.getExpr());
- }
-
@Override
- public Optional<WhereSegment> convertToSQLSegment(final SqlNode sqlNode) {
- if (null == sqlNode) {
- return Optional.empty();
- }
- // FIXME Now sqlNode position returned by the CalCite parser does not contain WHERE and requires manual calculation
- int startIndex = getStartIndex(sqlNode) - WHERE_SEGMENT_LENGTH;
- return new ExpressionConverter().convertToSQLSegment(sqlNode).map(optional -> new WhereSegment(startIndex, optional.getStopIndex(), optional));
+ public Optional<SqlNode> convert(final WhereSegment segment) {
+ return null == segment ? Optional.empty() : new ExpressionConverter().convert(segment.getExpr());
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/SQLStatementConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/SQLStatementConverter.java
index 3159bde2a7e..0bc6bd4c033 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/SQLStatementConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/SQLStatementConverter.java
@@ -28,18 +28,10 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
public interface SQLStatementConverter<S extends SQLStatement, T extends SqlNode> {
/**
- * Convert SQL statement to SQL node.
+ * Convert.
*
* @param sqlStatement SQL statement be to converted
* @return converted SQL node
*/
- T convertToSQLNode(S sqlStatement);
-
- /**
- * Convert SQL node to SQL statement.
- *
- * @param sqlNode SQL node be to converted
- * @return converted SQL statement
- */
- S convertToSQLStatement(T sqlNode);
+ T convert(S sqlStatement);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/select/SelectStatementConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/select/SelectStatementConverter.java
index 38333fb8e67..cd367c20bf7 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/select/SelectStatementConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/statement/select/SelectStatementConverter.java
@@ -23,7 +23,6 @@ import org.apache.calcite.sql.SqlOrderBy;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.infra.federation.optimizer.converter.context.ConverterContext;
-import org.apache.shardingsphere.infra.federation.optimizer.converter.context.ConverterContextHolder;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.from.TableConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.groupby.GroupByConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.groupby.HavingConverter;
@@ -33,17 +32,10 @@ import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.pr
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.projection.ProjectionsConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.segment.where.WhereConverter;
import org.apache.shardingsphere.infra.federation.optimizer.converter.statement.SQLStatementConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
-import java.util.Comparator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Optional;
/**
@@ -54,67 +46,22 @@ public final class SelectStatementConverter implements SQLStatementConverter<Sel
private static final int LIMIT_SEGMENT_LENGTH = 6;
@Override
- public SqlNode convertToSQLNode(final SelectStatement selectStatement) {
- SqlNodeList distinct = new DistinctConverter().convertToSQLNode(selectStatement.getProjections()).orElse(null);
- SqlNodeList projection = new ProjectionsConverter().convertToSQLNode(selectStatement.getProjections()).orElseThrow(IllegalStateException::new);
- SqlNode from = new TableConverter().convertToSQLNode(selectStatement.getFrom()).orElse(null);
- SqlNode where = selectStatement.getWhere().flatMap(optional -> new WhereConverter().convertToSQLNode(optional)).orElse(null);
- SqlNodeList groupBy = selectStatement.getGroupBy().flatMap(optional -> new GroupByConverter().convertToSQLNode(optional)).orElse(null);
- SqlNode having = selectStatement.getHaving().flatMap(optional -> new HavingConverter().convertToSQLNode(optional)).orElse(null);
- SqlNodeList orderBy = selectStatement.getOrderBy().flatMap(optional -> new OrderByConverter().convertToSQLNode(optional)).orElse(SqlNodeList.EMPTY);
+ public SqlNode convert(final SelectStatement selectStatement) {
+ SqlNodeList distinct = new DistinctConverter().convert(selectStatement.getProjections()).orElse(null);
+ SqlNodeList projection = new ProjectionsConverter().convert(selectStatement.getProjections()).orElseThrow(IllegalStateException::new);
+ SqlNode from = new TableConverter().convert(selectStatement.getFrom()).orElse(null);
+ SqlNode where = selectStatement.getWhere().flatMap(optional -> new WhereConverter().convert(optional)).orElse(null);
+ SqlNodeList groupBy = selectStatement.getGroupBy().flatMap(optional -> new GroupByConverter().convert(optional)).orElse(null);
+ SqlNode having = selectStatement.getHaving().flatMap(optional -> new HavingConverter().convert(optional)).orElse(null);
+ SqlNodeList orderBy = selectStatement.getOrderBy().flatMap(optional -> new OrderByConverter().convert(optional)).orElse(SqlNodeList.EMPTY);
Optional<LimitSegment> limit = SelectStatementHandler.getLimitSegment(selectStatement);
ConverterContext context = new ConverterContext();
SqlSelect sqlSelect = new SqlSelect(SqlParserPos.ZERO, distinct, projection, from, where, groupBy, having, SqlNodeList.EMPTY, null, null, null, SqlNodeList.EMPTY);
if (limit.isPresent()) {
- SqlNode offset = limit.get().getOffset().flatMap(optional -> new PaginationValueSQLConverter(context).convertToSQLNode(optional)).orElse(null);
- SqlNode rowCount = limit.get().getRowCount().flatMap(optional -> new PaginationValueSQLConverter(context).convertToSQLNode(optional)).orElse(null);
+ SqlNode offset = limit.get().getOffset().flatMap(optional -> new PaginationValueSQLConverter(context).convert(optional)).orElse(null);
+ SqlNode rowCount = limit.get().getRowCount().flatMap(optional -> new PaginationValueSQLConverter(context).convert(optional)).orElse(null);
return new SqlOrderBy(SqlParserPos.ZERO, sqlSelect, orderBy, offset, rowCount);
}
return !orderBy.isEmpty() ? new SqlOrderBy(SqlParserPos.ZERO, sqlSelect, orderBy, null, null) : sqlSelect;
}
-
- @Override
- public SelectStatement convertToSQLStatement(final SqlNode sqlNode) {
- MySQLSelectStatement result = new MySQLSelectStatement();
- try {
- SqlSelect sqlSelect = sqlNode instanceof SqlOrderBy ? (SqlSelect) ((SqlOrderBy) sqlNode).query : (SqlSelect) sqlNode;
- ConverterContext context = new ConverterContext();
- ConverterContextHolder.set(context);
- ProjectionsSegment projections = new ProjectionsConverter().convertToSQLSegment(sqlSelect.getSelectList()).orElseThrow(IllegalStateException::new);
- projections.setDistinctRow(sqlSelect.isDistinct());
- // TODO create select statement for different dialect
- result.setProjections(projections);
- new TableConverter().convertToSQLSegment(sqlSelect.getFrom()).ifPresent(result::setFrom);
- new WhereConverter().convertToSQLSegment(sqlSelect.getWhere()).ifPresent(result::setWhere);
- new GroupByConverter().convertToSQLSegment(sqlSelect.getGroup()).ifPresent(result::setGroupBy);
- new HavingConverter().convertToSQLSegment(sqlSelect.getHaving()).ifPresent(result::setHaving);
- if (sqlNode instanceof SqlOrderBy) {
- SqlOrderBy sqlOrderBy = (SqlOrderBy) sqlNode;
- new OrderByConverter().convertToSQLSegment(sqlOrderBy.orderList).ifPresent(result::setOrderBy);
- createLimitSegment(sqlOrderBy, context).ifPresent(result::setLimit);
- }
- result.setParameterCount(context.getParameterCount().get());
- } finally {
- ConverterContextHolder.remove();
- }
- return result;
- }
-
- private Optional<LimitSegment> createLimitSegment(final SqlOrderBy sqlOrderBy, final ConverterContext context) {
- if (null == sqlOrderBy.offset && null == sqlOrderBy.fetch) {
- return Optional.empty();
- }
- Optional<PaginationValueSegment> offset = Optional.ofNullable(sqlOrderBy.offset).flatMap(optional -> new PaginationValueSQLConverter(context).convertToSQLSegment(optional));
- Optional<PaginationValueSegment> rowCount = Optional.ofNullable(sqlOrderBy.fetch).flatMap(optional -> new PaginationValueSQLConverter(context).convertToSQLSegment(optional));
- List<Integer> startIndexes = new LinkedList<>();
- List<Integer> stopIndexes = new LinkedList<>();
- offset.map(SQLSegment::getStartIndex).ifPresent(startIndexes::add);
- rowCount.map(SQLSegment::getStartIndex).ifPresent(startIndexes::add);
- offset.map(SQLSegment::getStopIndex).ifPresent(stopIndexes::add);
- rowCount.map(SQLSegment::getStopIndex).ifPresent(stopIndexes::add);
- // FIXME Now sqlNode position returned by the CalCite parser does not contain LIMIT and requires manual calculation
- int startIndex = startIndexes.stream().min(Comparator.naturalOrder()).orElse(0) - LIMIT_SEGMENT_LENGTH;
- int stopIndex = stopIndexes.stream().max(Comparator.naturalOrder()).orElse(0);
- return Optional.of(new LimitSegment(startIndex, stopIndex, offset.orElse(null), rowCount.orElse(null)));
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
index 37ad8f93d56..13610f0d362 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/type/CombineOperatorConverter.java
@@ -54,22 +54,4 @@ public final class CombineOperatorConverter {
Preconditions.checkState(REGISTRY.containsKey(combineType), "Unsupported combine type: `%s`", combineType);
return REGISTRY.get(combineType);
}
-
- /**
- * Convert to combine type.
- * @param sqlOperator SQL operator to be converted
- * @return converted combine type
- */
- public static CombineType convert(final SqlOperator sqlOperator) {
- CombineType result = null;
- for (CombineType each : REGISTRY.keySet()) {
- SqlOperator currentOperator = REGISTRY.get(each);
- if (currentOperator == sqlOperator) {
- result = each;
- break;
- }
- }
- Preconditions.checkState(result != null, "Unsupported sql operator: `%s`", sqlOperator);
- return result;
- }
}
diff --git a/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java b/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
index 1644bbb0a98..f441aff0b8e 100644
--- a/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
+++ b/shardingsphere-test/shardingsphere-optimize-test/src/test/java/org/apache/shardingsphere/infra/federation/converter/parameterized/engine/SQLNodeConvertEngineParameterizedTest.java
@@ -37,12 +37,9 @@ import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.api.SQLParserEngine;
import org.apache.shardingsphere.sql.parser.api.SQLVisitorEngine;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
-import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.SQLStatementAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.CasesRegistry;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.SQLParserTestCasesRegistry;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.SQLParserTestCasesRegistryFactory;
-import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.sql.SQLCaseType;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.sql.loader.SQLCasesLoader;
import org.junit.Test;
@@ -131,25 +128,15 @@ public final class SQLNodeConvertEngineParameterizedTest {
}
@Test
- public void assertConvertToSQLNode() {
+ public void assertConvert() {
String databaseType = "H2".equals(this.databaseType) ? "MySQL" : this.databaseType;
String sql = SQL_CASES_LOADER.getCaseValue(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES_REGISTRY.get(sqlCaseId).getParameters(), databaseType);
SQLStatement sqlStatement = parseSQLStatement(databaseType, sql);
- SqlNode actual = SQLNodeConverterEngine.convertToSQLNode(sqlStatement);
+ SqlNode actual = SQLNodeConverterEngine.convert(sqlStatement);
SqlNode expected = parseSqlNode(databaseType, sql);
assertTrue(actual.equalsDeep(expected, Litmus.THROW));
}
- @Test
- public void assertConvertToSQLStatement() {
- SQLParserTestCase expected = SQL_PARSER_TEST_CASES_REGISTRY.get(sqlCaseId);
- String databaseType = "H2".equals(this.databaseType) ? "MySQL" : this.databaseType;
- String sql = SQL_CASES_LOADER.getCaseValue(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES_REGISTRY.get(sqlCaseId).getParameters(), databaseType);
- SqlNode sqlNode = parseSqlNode(databaseType, sql);
- SQLStatement actual = SQLNodeConverterEngine.convertToSQLStatement(sqlNode);
- SQLStatementAssert.assertIs(new SQLCaseAssertContext(SQL_CASES_LOADER, sqlCaseId, sqlCaseType, databaseType), actual, expected);
- }
-
@SneakyThrows(SqlParseException.class)
private SqlNode parseSqlNode(final String databaseType, final String sql) {
return SqlParser.create(sql, createConfig(DatabaseTypeFactory.getInstance(databaseType))).parseQuery();