You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/07/15 04:04:43 UTC
[shardingsphere] branch master updated: fix error when execute
select statement with order by expression (#11339)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 3d6efdc fix error when execute select statement with order by expression (#11339)
3d6efdc is described below
commit 3d6efdce5ee1e4f4c4cb7d63fe4028e034449141
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Thu Jul 15 12:04:08 2021 +0800
fix error when execute select statement with order by expression (#11339)
---
.../engine/ProjectionsContextEngine.java | 16 ++++++-------
.../engine/ProjectionsContextEngineTest.java | 28 ++++++++++++++++++++--
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
index 1ae250e..7e94462 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
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;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.TextOrderByItemSegment;
@@ -101,15 +102,14 @@ public final class ProjectionsContextEngine {
}
private boolean containsProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment, final Collection<SimpleTableSegment> tables) {
- return orderByItemSegment instanceof IndexOrderByItemSegment
- || containsItemInShorthandProjection(projections, orderByItemSegment, tables) || containsProjection(projections, orderByItemSegment);
+ return containsItemInShorthandProjection(projections, orderByItemSegment, tables) || containsProjection(projections, orderByItemSegment);
}
private boolean containsProjection(final Collection<Projection> projections, final OrderByItemSegment orderItem) {
+ if (orderItem instanceof IndexOrderByItemSegment) {
+ return true;
+ }
for (Projection each : projections) {
- if (orderItem instanceof IndexOrderByItemSegment) {
- return true;
- }
if (isSameAlias(each, (TextOrderByItemSegment) orderItem) || isSameQualifiedName(each, (TextOrderByItemSegment) orderItem)) {
return true;
}
@@ -118,12 +118,12 @@ public final class ProjectionsContextEngine {
}
private boolean containsItemInShorthandProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment, final Collection<SimpleTableSegment> tables) {
- return isUnqualifiedShorthandProjection(projections) || containsItemWithOwnerInShorthandProjections(projections, orderByItemSegment, tables)
+ return isUnqualifiedShorthandProjection(projections, orderByItemSegment) || containsItemWithOwnerInShorthandProjections(projections, orderByItemSegment, tables)
|| containsItemWithoutOwnerInShorthandProjections(projections, orderByItemSegment, tables);
}
- private boolean isUnqualifiedShorthandProjection(final Collection<Projection> projections) {
- if (1 != projections.size()) {
+ private boolean isUnqualifiedShorthandProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment) {
+ if (1 != projections.size() || orderByItemSegment instanceof ExpressionOrderByItemSegment) {
return false;
}
Projection projection = projections.iterator().next();
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
index 91dcf51..6fb782a 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
@@ -47,19 +47,27 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.dml.SQL92SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+@RunWith(MockitoJUnitRunner.class)
public final class ProjectionsContextEngineTest {
- private final ShardingSphereSchema schema = new ShardingSphereSchema(Collections.emptyMap());
+ @Mock
+ private ShardingSphereSchema schema;
@Test
public void assertProjectionsContextCreatedProperlyForMySQL() {
@@ -221,8 +229,9 @@ public final class ProjectionsContextEngineTest {
private SelectStatementContext createSelectStatementContext(final SelectStatement selectStatement) {
Map<String, ShardingSphereMetaData> metaDataMap = new HashMap<>();
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
- when(metaData.getSchema()).thenReturn(schema);
metaDataMap.put("schema", metaData);
+ when(metaData.getSchema()).thenReturn(schema);
+ when(schema.getAllColumnNames("t_order")).thenReturn(Arrays.asList("order_id", "content"));
return new SelectStatementContext(metaDataMap, Collections.emptyList(), selectStatement, "schema");
}
@@ -449,4 +458,19 @@ public final class ProjectionsContextEngineTest {
.createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
assertNotNull(actual);
}
+
+ @Test
+ public void assertCreateProjectionsContextWithOrderByExpressionForMySQL() {
+ SelectStatement selectStatement = new MySQLSelectStatement();
+ ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+ projectionsSegment.getProjections().addAll(Collections.singletonList(new ShorthandProjectionSegment(0, 0)));
+ selectStatement.setProjections(projectionsSegment);
+ selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+ OrderByItem orderByItem = new OrderByItem(new ExpressionOrderByItemSegment(0, 0, "id + 1", OrderDirection.ASC));
+ OrderByContext orderByContext = new OrderByContext(Collections.singleton(orderByItem), false);
+ SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
+ ProjectionsContext actual = new ProjectionsContextEngine(schema)
+ .createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+ assertThat(actual.getProjections().size(), is(2));
+ }
}