You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/06/23 12:27:06 UTC

[shardingsphere] branch master updated: add offset and rowcount sql converter (#10949)

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

wuweijie 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 70faca8  add offset and rowcount sql converter (#10949)
70faca8 is described below

commit 70faca805c5de893336ec3db3d721b08a1759c81
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Wed Jun 23 20:26:37 2021 +0800

    add offset and rowcount sql converter (#10949)
---
 ...eConverter.java => OffsetSqlNodeConverter.java} | 19 ++++------
 .../impl/PaginationValueSqlConverter.java          | 43 ++++++++++++++++++++++
 ...onverter.java => RowCountSqlNodeConverter.java} | 19 ++++------
 .../impl/SelectStatementSqlNodeConverter.java      |  5 ++-
 4 files changed, 63 insertions(+), 23 deletions(-)

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/OffsetSqlNodeConverter.java
similarity index 57%
copy from shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/SelectStatementSqlNodeConverter.java
copy to shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/OffsetSqlNodeConverter.java
index 17a2346..a7a7c0f 100644
--- 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/OffsetSqlNodeConverter.java
@@ -18,24 +18,21 @@
 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 org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
 
 import java.util.Optional;
 
 /**
- * Select statement sql node converter.
+ * Offset sql node converter.
  */
-public final class SelectStatementSqlNodeConverter implements SqlNodeConverter<SelectStatement> {
+public final class OffsetSqlNodeConverter implements SqlNodeConverter<LimitSegment> {
     
     @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));
+    public Optional<SqlNode> convert(final LimitSegment limitSegment) {
+        if (null == limitSegment || !limitSegment.getOffset().isPresent()) {
+            return Optional.empty();
+        }
+        return new PaginationValueSqlConverter().convert(limitSegment.getOffset().get());
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/PaginationValueSqlConverter.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/PaginationValueSqlConverter.java
new file mode 100644
index 0000000..bee8086
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/PaginationValueSqlConverter.java
@@ -0,0 +1,43 @@
+/*
+ * 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.SqlDynamicParam;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+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.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.ParameterMarkerLimitValueSegment;
+
+import java.util.Optional;
+
+public final class PaginationValueSqlConverter implements SqlNodeConverter<PaginationValueSegment> {
+    
+    @Override
+    public Optional<SqlNode> convert(final PaginationValueSegment paginationValue) {
+        if (paginationValue instanceof NumberLiteralPaginationValueSegment) {
+            NumberLiteralPaginationValueSegment offsetValue = (NumberLiteralPaginationValueSegment) paginationValue;
+            return Optional.of(SqlLiteral.createExactNumeric(String.valueOf(offsetValue.getValue()), SqlParserPos.ZERO));
+        } else {
+            ParameterMarkerLimitValueSegment offsetParam = (ParameterMarkerLimitValueSegment) paginationValue;
+            return Optional.of(new SqlDynamicParam(offsetParam.getParameterIndex(), SqlParserPos.ZERO));
+        }
+    }
+}
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/RowCountSqlNodeConverter.java
similarity index 57%
copy from shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/SelectStatementSqlNodeConverter.java
copy to shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/convert/converter/impl/RowCountSqlNodeConverter.java
index 17a2346..ad50960 100644
--- 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/RowCountSqlNodeConverter.java
@@ -18,24 +18,21 @@
 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 org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
 
 import java.util.Optional;
 
 /**
- * Select statement sql node converter.
+ * Row count sql node converter.
  */
-public final class SelectStatementSqlNodeConverter implements SqlNodeConverter<SelectStatement> {
+public final class RowCountSqlNodeConverter implements SqlNodeConverter<LimitSegment> {
     
     @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));
+    public Optional<SqlNode> convert(final LimitSegment limitSegment) {
+        if (null == limitSegment || !limitSegment.getRowCount().isPresent()) {
+            return Optional.empty();
+        }
+        return new PaginationValueSqlConverter().convert(limitSegment.getRowCount().get());
     }
 }
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
index 17a2346..69333c7 100644
--- 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
@@ -23,6 +23,7 @@ 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 org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
 
 import java.util.Optional;
 
@@ -34,8 +35,10 @@ public final class SelectStatementSqlNodeConverter implements SqlNodeConverter<S
     @Override
     public Optional<SqlNode> convert(final SelectStatement selectStatement) {
         Optional<SqlNode> distinct = new DistinctSqlNodeConverter().convert(selectStatement.getProjections());
+        Optional<SqlNode> offset = new OffsetSqlNodeConverter().convert(SelectStatementHandler.getLimitSegment(selectStatement).orElse(null));
+        Optional<SqlNode> rowCount = new RowCountSqlNodeConverter().convert(SelectStatementHandler.getLimitSegment(selectStatement).orElse(null));
         // 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));
+                null, null, offset.orElse(null), rowCount.orElse(null), null));
     }
 }