This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new f99d700  Create sql node engine for sql optimization (#10895)
f99d700 is described below

commit f99d700fb2ff4133576854dd9eea2e3917198391
Author: Juan Pan(Trista) <panj...@apache.org>
AuthorDate: Mon Jun 21 20:20:59 2021 +0800

    Create sql node engine for sql optimization (#10895)
---
 ...odeConverter.java => SqlNodeConvertEngine.java} | 21 +++++++----
 .../convert/{ => converter}/SqlNodeConverter.java  | 15 +++-----
 .../impl/DistinctSqlNodeConverter.java}            | 27 +++++++-------
 .../impl/SelectStatementSqlNodeConverter.java      | 41 ++++++++++++++++++++++
 4 files changed, 75 insertions(+), 29 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConvertEngine.java
similarity index 59%
copy from 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
copy to 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConvertEngine.java
index b084ec3..06cdab1 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConvertEngine.java
@@ -20,22 +20,31 @@ package 
org.apache.shardingsphere.infra.optimize.core.convert;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.calcite.sql.SqlNode;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import 
org.apache.shardingsphere.infra.optimize.core.convert.converter.impl.SelectStatementSqlNodeConverter;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 import java.util.Optional;
 
 /**
- * SqlNode converter.
+ * SqlNode convert engine.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class SqlNodeConverter {
+public final class SqlNodeConvertEngine {
     
     /**
      *  Convert.
-     * @param statementContext statement context
+     * @param statement statement
      * @return sqlNode optional
      */
-    public static Optional<SqlNode> convert(final SQLStatementContext<?> 
statementContext) {
-        return Optional.empty();
+    public static Optional<SqlNode> convert(final SQLStatement statement) {
+        try {
+            if (statement instanceof SelectStatement) {
+                return new 
SelectStatementSqlNodeConverter().convert((SelectStatement) statement);
+            }
+            return Optional.empty();
+        } catch (final UnsupportedOperationException ex) {
+            return Optional.empty();
+        }
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/SqlNodeConverter.java
similarity index 67%
copy from 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
copy to 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/SqlNodeConverter.java
index b084ec3..c0c38d3 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/SqlNodeConverter.java
@@ -15,27 +15,22 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.infra.optimize.core.convert;
+package org.apache.shardingsphere.infra.optimize.core.convert.converter;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
 import org.apache.calcite.sql.SqlNode;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 
 import java.util.Optional;
 
 /**
  * SqlNode converter.
  */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class SqlNodeConverter {
+public interface SqlNodeConverter<T extends ASTNode> {
     
     /**
      *  Convert.
-     * @param statementContext statement context
+     * @param astNode ast node
      * @return sqlNode optional
      */
-    public static Optional<SqlNode> convert(final SQLStatementContext<?> 
statementContext) {
-        return Optional.empty();
-    }
+    Optional<SqlNode> convert(T astNode);
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/DistinctSqlNodeConverter.java
similarity index 51%
rename from 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
rename to 
shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/DistinctSqlNodeConverter.java
index b084ec3..fc2e57a 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/SqlNodeConverter.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/DistinctSqlNodeConverter.java
@@ -15,27 +15,28 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.infra.optimize.core.convert;
+package org.apache.shardingsphere.infra.optimize.core.convert.converter.impl;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
 import org.apache.calcite.sql.SqlNode;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlSelectKeyword;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import 
org.apache.shardingsphere.infra.optimize.core.convert.converter.SqlNodeConverter;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
 
+import java.util.Collections;
 import java.util.Optional;
 
 /**
- * SqlNode converter.
+ * Distinct sql node converter.
  */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class SqlNodeConverter {
+public final class DistinctSqlNodeConverter implements 
SqlNodeConverter<ProjectionsSegment> {
     
-    /**
-     *  Convert.
-     * @param statementContext statement context
-     * @return sqlNode optional
-     */
-    public static Optional<SqlNode> convert(final SQLStatementContext<?> 
statementContext) {
+    @Override
+    public Optional<SqlNode> convert(final ProjectionsSegment 
projectionsSegment) {
+        if (projectionsSegment.isDistinctRow()) {
+            return Optional.of(new 
SqlNodeList(Collections.singletonList(SqlSelectKeyword.DISTINCT.symbol(SqlParserPos.ZERO)),
 SqlParserPos.ZERO));
+        }
         return Optional.empty();
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/SelectStatementSqlNodeConverter.java
 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/SelectStatementSqlNodeConverter.java
new file mode 100644
index 0000000..17a2346
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/SelectStatementSqlNodeConverter.java
@@ -0,0 +1,41 @@
+/*
+ * 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.core.convert.converter.impl;
+
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlSelect;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import 
org.apache.shardingsphere.infra.optimize.core.convert.converter.SqlNodeConverter;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+
+import java.util.Optional;
+
+/**
+ * Select statement sql node converter.
+ */
+public final class SelectStatementSqlNodeConverter implements 
SqlNodeConverter<SelectStatement> {
+    
+    @Override
+    public Optional<SqlNode> convert(final SelectStatement selectStatement) {
+        Optional<SqlNode> distinct = new 
DistinctSqlNodeConverter().convert(selectStatement.getProjections());
+        // TODO : prepare other sqlNodes referring to `distinct`.
+        return Optional.of(new SqlSelect(SqlParserPos.ZERO, (SqlNodeList) 
distinct.orElse(null), null, null, null, null, null,
+                null, null, null, null, null));
+    }
+}

Reply via email to