This is an automated email from the ASF dual-hosted git repository.
terrymanu 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 639c9176be0 Handle nullable projection text without throwing (#40229)
639c9176be0 is described below
commit 639c9176be0243f2682ebd8502271b1736aba788
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Oct 19 11:14:59 2026 +0800
Handle nullable projection text without throwing (#40229)
* Handle nullable projection text without throwing
* Skip derived projections with missing expressions
---
.../projection/engine/ProjectionsContextEngine.java | 15 ++++++++++-----
.../engine/ProjectionsContextEngineTest.java | 18 ++++++++----------
2 files changed, 18 insertions(+), 15 deletions(-)
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngine.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngine.java
index 8e84a039726..939c37a6000 100644
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngine.java
+++
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngine.java
@@ -27,7 +27,6 @@ import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.DerivedProjection;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ShorthandProjection;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
@@ -88,10 +87,14 @@ public final class ProjectionsContextEngine {
Collection<Projection> result = new LinkedList<>();
int derivedColumnOffset = 0;
for (OrderByItem each : orderItems) {
- if (!containsProjection(each.getSegment(), projections)) {
- result.add(new DerivedProjection(((TextOrderByItemSegment)
each.getSegment()).getText(), new
IdentifierValue(derivedColumn.getDerivedColumnAlias(derivedColumnOffset++)),
- each.getSegment()));
+ if (containsProjection(each.getSegment(), projections)) {
+ continue;
}
+ String expression = ((TextOrderByItemSegment)
each.getSegment()).getText();
+ if (null == expression) {
+ continue;
+ }
+ result.add(new DerivedProjection(expression, new
IdentifierValue(derivedColumn.getDerivedColumnAlias(derivedColumnOffset++)),
each.getSegment()));
}
return result;
}
@@ -101,7 +104,9 @@ public final class ProjectionsContextEngine {
return true;
}
String normalizedText =
SQLUtils.getExactlyValue(((TextOrderByItemSegment) orderByItem).getText());
- ShardingSpherePreconditions.checkNotNull(normalizedText, () -> new
IllegalStateException("Order item text must not be null."));
+ if (null == normalizedText) {
+ return false;
+ }
for (Projection each : projections) {
if (orderByItem instanceof ColumnOrderByItemSegment &&
isSameColumn(each, ((ColumnOrderByItemSegment) orderByItem).getColumn(),
normalizedText)) {
return true;
diff --git
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java
index dffafabc4df..83d3183b649 100644
---
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java
+++
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java
@@ -17,6 +17,7 @@
package
org.apache.shardingsphere.infra.binder.context.segment.select.projection.engine;
+import com.google.common.collect.Iterables;
import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.binder.context.segment.select.groupby.GroupByContext;
@@ -45,7 +46,7 @@ import java.util.Collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class ProjectionsContextEngineTest {
@@ -184,9 +185,8 @@ class ProjectionsContextEngineTest {
projectionsSegment.getProjections().add(new
ShorthandProjectionSegment(0, 0));
OrderByItem orderByItem = new OrderByItem(new
ExpressionOrderByItemSegment(0, 0, null, OrderDirection.ASC,
NullsOrderType.FIRST));
ProjectionsContextEngine engine = new
ProjectionsContextEngine(databaseType);
- IllegalStateException actual =
assertThrows(IllegalStateException.class, () ->
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.emptyList()),
- new OrderByContext(Collections.singleton(orderByItem),
false)));
- assertThat(actual.getMessage(), is("Order item text must not be
null."));
+ ProjectionsContext actual =
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.emptyList()), new
OrderByContext(Collections.singleton(orderByItem), false));
+
assertThat(Iterables.getOnlyElement(actual.getProjections()).getExpression(),
is("*"));
}
@Test
@@ -195,9 +195,8 @@ class ProjectionsContextEngineTest {
projectionsSegment.getProjections().add(new
ShorthandProjectionSegment(0, 0));
OrderByItem groupByItem = new OrderByItem(new
ExpressionOrderByItemSegment(0, 0, null, OrderDirection.ASC,
NullsOrderType.FIRST));
ProjectionsContextEngine engine = new
ProjectionsContextEngine(databaseType);
- IllegalStateException actual =
assertThrows(IllegalStateException.class, () ->
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.singleton(groupByItem)),
- new OrderByContext(Collections.emptyList(), false)));
- assertThat(actual.getMessage(), is("Order item text must not be
null."));
+ ProjectionsContext actual =
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.singleton(groupByItem)), new
OrderByContext(Collections.emptyList(), false));
+
assertThat(Iterables.getOnlyElement(actual.getProjections()).getExpression(),
is("*"));
}
@Test
@@ -205,9 +204,8 @@ class ProjectionsContextEngineTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
OrderByItem orderByItem = new OrderByItem(new
ExpressionOrderByItemSegment(0, 0, null, OrderDirection.ASC,
NullsOrderType.FIRST));
ProjectionsContextEngine engine = new
ProjectionsContextEngine(databaseType);
- IllegalStateException actual =
assertThrows(IllegalStateException.class, () ->
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.emptyList()),
- new OrderByContext(Collections.singleton(orderByItem),
false)));
- assertThat(actual.getMessage(), is("Order item text must not be
null."));
+ ProjectionsContext actual =
engine.createProjectionsContext(projectionsSegment, new
GroupByContext(Collections.emptyList()), new
OrderByContext(Collections.singleton(orderByItem), false));
+ assertTrue(actual.getProjections().isEmpty());
}
@Test