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 5668cfde7d8 Support select order by index statement convert to SqlNode
(#20052)
5668cfde7d8 is described below
commit 5668cfde7d8a852dd2a686e251e3945d5a3927c9
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Aug 10 15:12:53 2022 +0800
Support select order by index statement convert to SqlNode (#20052)
* Support select order by index statement convert to SqlNode
* optimize checkstyle
* modify java doc
---
.../orderby/item/IndexOrderByItemConverter.java | 56 ++++++++++++++++++++++
.../orderby/item/OrderByItemConverterUtil.java | 4 +-
.../core/job/importer/DefaultImporterCreator.java | 4 +-
.../core/spi/fixture/ScalingEntryFixture.java | 2 +-
.../data/pipeline/mysql/MySQLScalingEntry.java | 2 +-
.../pipeline/opengauss/OpenGaussScalingEntry.java | 2 +-
.../postgresql/PostgreSQLScalingEntry.java | 2 +-
.../shardingsphere-optimize-test/pom.xml | 5 ++
.../SQLNodeConvertEngineParameterizedTest.java | 2 +-
.../core/fixture/FixtureImporterCreator.java | 2 +-
10 files changed, 72 insertions(+), 9 deletions(-)
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
new file mode 100644
index 00000000000..df1728be40e
--- /dev/null
+++
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
@@ -0,0 +1,56 @@
+/*
+ * 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.federation.optimizer.converter.segment.orderby.item;
+
+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;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
+
+import java.util.Collections;
+import java.util.Optional;
+
+/**
+ * Index order by item converter.
+ */
+public final class IndexOrderByItemConverter implements
SQLSegmentConverter<IndexOrderByItemSegment, SqlNode> {
+
+ @Override
+ public Optional<SqlNode> convertToSQLNode(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 074d42f71bf..b110f54b7b5 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
@@ -51,7 +51,7 @@ public final class OrderByItemConverterUtil {
} else if (each instanceof ExpressionOrderByItemSegment) {
throw new UnsupportedOperationException("unsupported
ExpressionOrderByItemSegment");
} else if (each instanceof IndexOrderByItemSegment) {
- throw new UnsupportedOperationException("unsupported
IndexOrderByItemSegment");
+ new
IndexOrderByItemConverter().convertToSQLNode((IndexOrderByItemSegment)
each).ifPresent(result::add);
} else if (each instanceof TextOrderByItemSegment) {
throw new UnsupportedOperationException("unsupported
TextOrderByItemSegment");
}
@@ -70,6 +70,8 @@ public final class OrderByItemConverterUtil {
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-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/scaling/core/job/importer/DefaultImporterCreator.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/scaling/core/job/importer/DefaultImporterCreator.java
index 0aaed77b0f5..2e407671b8c 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/scaling/core/job/importer/DefaultImporterCreator.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/scaling/core/job/importer/DefaultImporterCreator.java
@@ -37,12 +37,12 @@ public final class DefaultImporterCreator implements
ImporterCreator {
final PipelineJobProgressListener
jobProgressListener) {
return new DefaultImporter(importerConfig, dataSourceManager, channel,
jobProgressListener);
}
-
+
@Override
public String getType() {
return "MySQL";
}
-
+
@Override
public Collection<String> getTypeAliases() {
Collection<String> aliases = new LinkedList<>();
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/scaling/core/spi/fixture/ScalingEntryFixture.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/scaling/core/spi/fixture/ScalingEntryFixture.java
index 800563bb8d8..68ab8ffa9eb 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/scaling/core/spi/fixture/ScalingEntryFixture.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/scaling/core/spi/fixture/ScalingEntryFixture.java
@@ -32,7 +32,7 @@ public final class ScalingEntryFixture implements
ScalingEntry {
public Class<? extends IncrementalDumper> getIncrementalDumperClass() {
return IncrementalDumper.class;
}
-
+
@Override
public String getType() {
return "FIXTURE";
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/MySQLScalingEntry.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/MySQLScalingEntry.java
index c0c5b2d3878..71c4c23fac0 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/MySQLScalingEntry.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/MySQLScalingEntry.java
@@ -35,7 +35,7 @@ public final class MySQLScalingEntry implements ScalingEntry {
public Class<MySQLIncrementalDumper> getIncrementalDumperClass() {
return MySQLIncrementalDumper.class;
}
-
+
@Override
public String getType() {
return "MySQL";
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/OpenGaussScalingEntry.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/OpenGaussScalingEntry.java
index 294364f41f0..36a072b9e57 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/OpenGaussScalingEntry.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/OpenGaussScalingEntry.java
@@ -35,7 +35,7 @@ public final class OpenGaussScalingEntry implements
ScalingEntry {
public Class<OpenGaussWalDumper> getIncrementalDumperClass() {
return OpenGaussWalDumper.class;
}
-
+
@Override
public String getType() {
return "openGauss";
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/PostgreSQLScalingEntry.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/PostgreSQLScalingEntry.java
index 89b3f415732..b21636c5743 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/PostgreSQLScalingEntry.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/PostgreSQLScalingEntry.java
@@ -35,7 +35,7 @@ public final class PostgreSQLScalingEntry implements
ScalingEntry {
public Class<PostgreSQLWalDumper> getIncrementalDumperClass() {
return PostgreSQLWalDumper.class;
}
-
+
@Override
public String getType() {
return "PostgreSQL";
diff --git a/shardingsphere-test/shardingsphere-optimize-test/pom.xml
b/shardingsphere-test/shardingsphere-optimize-test/pom.xml
index a8626dee4e4..e7829de9717 100644
--- a/shardingsphere-test/shardingsphere-optimize-test/pom.xml
+++ b/shardingsphere-test/shardingsphere-optimize-test/pom.xml
@@ -67,5 +67,10 @@
<artifactId>shardingsphere-sql-parser-sqlserver</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-sql-parser-opengauss</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</project>
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 5f488a58f67..bec3faad72c 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
@@ -90,13 +90,13 @@ public final class SQLNodeConvertEngineParameterizedTest {
SUPPORTED_SQL_CASE_IDS.add("select_distinct_with_single_count_group_by");
SUPPORTED_SQL_CASE_IDS.add("select_bit_xor");
SUPPORTED_SQL_CASE_IDS.add("select_position");
- SUPPORTED_SQL_CASE_IDS.add("select_constant_without_table");
SUPPORTED_SQL_CASE_IDS.add("select_with_schema");
SUPPORTED_SQL_CASE_IDS.add("select_with_union");
SUPPORTED_SQL_CASE_IDS.add("select_with_union_all");
SUPPORTED_SQL_CASE_IDS.add("select_cast_function");
SUPPORTED_SQL_CASE_IDS.add("select_with_same_table_name_and_alias");
SUPPORTED_SQL_CASE_IDS.add("select_count_like_concat");
+ SUPPORTED_SQL_CASE_IDS.add("select_order_by_asc_and_index_desc");
}
private final String sqlCaseId;
diff --git
a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/fixture/FixtureImporterCreator.java
b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/fixture/FixtureImporterCreator.java
index 0413a02845a..36484d23195 100644
---
a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/fixture/FixtureImporterCreator.java
+++
b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/fixture/FixtureImporterCreator.java
@@ -35,7 +35,7 @@ public final class FixtureImporterCreator implements
ImporterCreator {
final PipelineJobProgressListener
jobProgressListener) {
return new FixtureImporter(importerConfig, dataSourceManager, channel,
jobProgressListener);
}
-
+
@Override
public String getType() {
return "H2";