You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/10/18 04:52:51 UTC
[shardingsphere] branch master updated:
ParameterMarkerExpressionSegment cannot be cast to class
LiteralExpressionSegment (#13064)
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 9255267 ParameterMarkerExpressionSegment cannot be cast to class LiteralExpressionSegment (#13064)
9255267 is described below
commit 925526789e37c7737104bb69739fb65cc564e814
Author: liguoping <xd...@163.com>
AuthorDate: Mon Oct 18 12:52:14 2021 +0800
ParameterMarkerExpressionSegment cannot be cast to class LiteralExpressionSegment (#13064)
* ParameterMarkerExpressionSegment cannot be cast to class LiteralExpressionSegment
* ParameterMarkerProjection describe
* unit test
---
.../select/projection/engine/ProjectionEngine.java | 9 +++++
.../projection/impl/ParameterMarkerProjection.java | 44 +++++++++++++++++-----
.../projection/engine/ProjectionEngineTest.java | 12 ++++++
.../statement/impl/MySQLStatementSQLVisitor.java | 2 +
.../impl/SQL92DMLStatementSQLVisitor.java | 6 +++
.../impl/SQLServerDMLStatementSQLVisitor.java | 6 +++
.../simple/ParameterMarkerExpressionSegment.java | 15 +++++++-
7 files changed, 83 insertions(+), 11 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
index d4f312c..b7dc0a9 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
@@ -24,11 +24,13 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.Agg
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ExpressionProjection;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ParameterMarkerProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ShorthandProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.SubqueryProjection;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
@@ -90,9 +92,16 @@ public final class ProjectionEngine {
if (projectionSegment instanceof SubqueryProjectionSegment) {
return Optional.of(createProjection((SubqueryProjectionSegment) projectionSegment));
}
+ if (projectionSegment instanceof ParameterMarkerExpressionSegment) {
+ return Optional.of(createProjection((ParameterMarkerExpressionSegment) projectionSegment));
+ }
// TODO subquery
return Optional.empty();
}
+
+ private ParameterMarkerProjection createProjection(final ParameterMarkerExpressionSegment projectionSegment) {
+ return new ParameterMarkerProjection(projectionSegment.getParameterMarkerIndex(), projectionSegment.getAlias().orElse(null));
+ }
private SubqueryProjection createProjection(final SubqueryProjectionSegment projectionSegment) {
return new SubqueryProjection(projectionSegment.getText(), projectionSegment.getAlias().orElse(null));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ParameterMarkerProjection.java
similarity index 53%
copy from shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
copy to shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ParameterMarkerProjection.java
index 2cd3fbd..305fc80 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ParameterMarkerProjection.java
@@ -15,26 +15,50 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple;
+package org.apache.shardingsphere.infra.binder.segment.select.projection.impl;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
+
+import java.util.Optional;
/**
- * Parameter marker expression segment.
+ * ParameterMarker projection.
*/
@RequiredArgsConstructor
@Getter
-@ToString
@EqualsAndHashCode
-public class ParameterMarkerExpressionSegment implements SimpleExpressionSegment, ProjectionSegment {
-
- private final int startIndex;
-
- private final int stopIndex;
-
+@ToString
+public final class ParameterMarkerProjection implements Projection {
+
private final int parameterMarkerIndex;
+
+ private final String alias;
+
+ @Override
+ public String getExpression() {
+ return String.valueOf(parameterMarkerIndex);
+ }
+
+ @Override
+ public String getColumnLabel() {
+ return getAlias().orElse(String.valueOf(parameterMarkerIndex));
+ }
+
+ @Override
+ public Optional<String> getAlias() {
+ return Optional.ofNullable(alias);
+ }
+
+ /**
+ * Get expression with alias.
+ *
+ * @return expression with alias
+ */
+ public String getExpressionWithAlias() {
+ return getExpression() + (null == alias ? "" : " AS " + alias);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
index 6d48084..137f478 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
@@ -22,11 +22,13 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.Agg
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ExpressionProjection;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ParameterMarkerProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ShorthandProjection;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
@@ -141,4 +143,14 @@ public final class ProjectionEngineTest {
assertTrue(actual.isPresent());
assertThat(actual.get(), instanceOf(AggregationProjection.class));
}
+
+ @Test
+ public void assertCreateProjectionWhenProjectionSegmentInstanceOfParameterMarkerExpressionSegment() {
+ ParameterMarkerExpressionSegment parameterMarkerExpressionSegment = new ParameterMarkerExpressionSegment(7, 7, 0);
+ parameterMarkerExpressionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("alias")));
+ Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), parameterMarkerExpressionSegment);
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), instanceOf(ParameterMarkerProjection.class));
+ assertThat(actual.get().getAlias().orElse(null), is("alias"));
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
index 72d8c9a..351e26f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
@@ -1310,6 +1310,8 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
return result;
}
if (projection instanceof ParameterMarkerExpressionSegment) {
+ ParameterMarkerExpressionSegment result = (ParameterMarkerExpressionSegment) projection;
+ result.setAlias(alias);
return projection;
}
LiteralExpressionSegment column = (LiteralExpressionSegment) projection;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
index d9b8f6f..c2f3951f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
@@ -65,6 +65,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOp
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
@@ -343,6 +344,11 @@ public final class SQL92DMLStatementSQLVisitor extends SQL92StatementSQLVisitor
result.setAlias(alias);
return result;
}
+ if (projection instanceof ParameterMarkerExpressionSegment) {
+ ParameterMarkerExpressionSegment result = (ParameterMarkerExpressionSegment) projection;
+ result.setAlias(alias);
+ return projection;
+ }
LiteralExpressionSegment column = (LiteralExpressionSegment) projection;
ExpressionProjectionSegment result = null == alias ? new ExpressionProjectionSegment(column.getStartIndex(), column.getStopIndex(), String.valueOf(column.getLiterals()))
: new ExpressionProjectionSegment(column.getStartIndex(), ctx.alias().stop.getStopIndex(), String.valueOf(column.getLiterals()));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
index 0da1074..d925a5f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
@@ -67,6 +67,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonTableExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
@@ -422,6 +423,11 @@ public final class SQLServerDMLStatementSQLVisitor extends SQLServerStatementSQL
result.setAlias(alias);
return result;
}
+ if (projection instanceof ParameterMarkerExpressionSegment) {
+ ParameterMarkerExpressionSegment result = (ParameterMarkerExpressionSegment) projection;
+ result.setAlias(alias);
+ return projection;
+ }
LiteralExpressionSegment column = (LiteralExpressionSegment) projection;
ExpressionProjectionSegment result = null == alias ? new ExpressionProjectionSegment(column.getStartIndex(), column.getStopIndex(), String.valueOf(column.getLiterals()))
: new ExpressionProjectionSegment(column.getStartIndex(), ctx.alias().stop.getStopIndex(), String.valueOf(column.getLiterals()));
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
index 2cd3fbd..1cbd810 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/ParameterMarkerExpressionSegment.java
@@ -20,8 +20,13 @@ package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasAvailable;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
+
+import java.util.Optional;
/**
* Parameter marker expression segment.
@@ -30,11 +35,19 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Projecti
@Getter
@ToString
@EqualsAndHashCode
-public class ParameterMarkerExpressionSegment implements SimpleExpressionSegment, ProjectionSegment {
+public class ParameterMarkerExpressionSegment implements SimpleExpressionSegment, ProjectionSegment, AliasAvailable {
private final int startIndex;
private final int stopIndex;
private final int parameterMarkerIndex;
+
+ @Setter
+ private AliasSegment alias;
+
+ @Override
+ public Optional<String> getAlias() {
+ return null == alias ? Optional.empty() : Optional.ofNullable(alias.getIdentifier().getValue());
+ }
}