You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/11/17 11:29:26 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #13661: Support more query statements to convert between SQLStatement and SqlNode

strongduanmu commented on a change in pull request #13661:
URL: https://github.com/apache/shardingsphere/pull/13661#discussion_r751149934



##########
File path: shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/converter/segment/projection/impl/ColumnProjectionConverter.java
##########
@@ -17,25 +17,51 @@
 
 package org.apache.shardingsphere.infra.optimize.converter.segment.projection.impl;
 
+import org.apache.calcite.sql.SqlAsOperator;
+import org.apache.calcite.sql.SqlBasicCall;
 import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.parser.SqlParserPos;
 import org.apache.shardingsphere.infra.optimize.converter.segment.SQLSegmentConverter;
 import org.apache.shardingsphere.infra.optimize.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.List;
 import java.util.Optional;
 
 /**
  * Column projection converter. 
  */
-public final class ColumnProjectionConverter implements SQLSegmentConverter<ColumnProjectionSegment, SqlIdentifier> {
+public final class ColumnProjectionConverter implements SQLSegmentConverter<ColumnProjectionSegment, SqlNode> {
     
     @Override
-    public Optional<SqlIdentifier> convertToSQLNode(final ColumnProjectionSegment segment) {
+    public Optional<SqlNode> convertToSQLNode(final ColumnProjectionSegment segment) {
+        if (segment.getAlias().isPresent()) {
+            Optional<SqlNode> sqlIdentifier = new ColumnConverter().convertToSQLNode(segment.getColumn());
+            SqlIdentifier sqlIdentifier1 = new SqlIdentifier(segment.getAlias().get(), SqlParserPos.ZERO);
+            return Optional.of(new SqlBasicCall(new SqlAsOperator(), new SqlNode[]{sqlIdentifier.get(), sqlIdentifier1}, SqlParserPos.ZERO));
+        }
         return new ColumnConverter().convertToSQLNode(segment.getColumn());
     }
     
     @Override
-    public Optional<ColumnProjectionSegment> convertToSQLSegment(final SqlIdentifier sqlNode) {
+    public Optional<ColumnProjectionSegment> convertToSQLSegment(final SqlNode sqlNode) {
+        if (sqlNode instanceof SqlBasicCall) {
+            List<SqlNode> operands = ((SqlBasicCall) sqlNode).getOperandList();
+            Optional<ColumnSegment> columnSegment = new ColumnConverter().convertToSQLSegment(operands.get(0));
+            if (!columnSegment.isPresent()) {
+                return Optional.empty();
+            }
+            ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment(columnSegment.get());
+            if (operands.size() == 2) {

Review comment:
       @LeeGuoPing Please move `2` to the left condition.

##########
File path: shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/converter/statement/SelectStatementConverter.java
##########
@@ -90,10 +92,30 @@ public SelectStatement convertToSQLStatement(final SqlNode sqlNode) {
             new OrderByConverter().convertToSQLSegment(sqlOrderBy.orderList).ifPresent(result::setOrderBy);
             createLimitSegment(sqlOrderBy, context).ifPresent(result::setLimit);
         }
+        calculateParamCount(result, context);
         result.setParameterCount(context.getParameterCount().get());
         return result;
     }
     
+    private void calculateParamCount(final MySQLSelectStatement result, final ConverterContext context) {

Review comment:
       @LeeGuoPing Why add this logic?

##########
File path: shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/converter/segment/expression/impl/ParameterMarkerExpressionConverter.java
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.optimize.converter.segment.expression.impl;
+
+import org.apache.calcite.sql.SqlDynamicParam;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.shardingsphere.infra.optimize.converter.segment.SQLSegmentConverter;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+
+import java.util.Optional;
+
+public final class ParameterMarkerExpressionConverter implements SQLSegmentConverter<ParameterMarkerExpressionSegment, SqlNode> {

Review comment:
       @LeeGuoPing Please add javadoc for ParameterMarkerExpressionConverter




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org