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 2022/06/01 03:36:52 UTC
[shardingsphere] branch master updated: parse aggregationProjectionSegment with expressionSegment (#18045)
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 131b190525a parse aggregationProjectionSegment with expressionSegment (#18045)
131b190525a is described below
commit 131b190525a10e3953a0b516c23a0624b52a9a39
Author: cheese8 <yi...@163.com>
AuthorDate: Wed Jun 1 11:36:47 2022 +0800
parse aggregationProjectionSegment with expressionSegment (#18045)
* parse AggregationProjectionSegment with ColumnSegment
* deal with AggregationDistinctProjectionSegment
* recover AggregationDistinctProjectionSegment
* align to checkstyle
* optimize parse
* add UT
* correct way to parse columnsegment
* fix oracle no expr array
* convert columnSegment as ExpressionSegment
* enhance AggregationDistinctProjectionSegment
* align to review
* align on review
* remove unused imports
* Update SQLServerStatementSQLVisitor.java
* Update OracleStatementSQLVisitor.java
* Update OpenGaussStatementSQLVisitor.java
* align to review
* fix for pg & og
* revert PG & OG
* Update ExpectedAggregationProjection.java
* align to review
---
.../statement/impl/MySQLStatementSQLVisitor.java | 24 ++++++++++++++++++----
.../statement/impl/OracleStatementSQLVisitor.java | 18 +++++++++++++---
.../statement/impl/SQL92StatementSQLVisitor.java | 22 +++++++++++++++++---
.../impl/SQLServerStatementSQLVisitor.java | 22 +++++++++++++++++---
.../dml/item/AggregationProjectionSegment.java | 4 ++++
.../aggregation/ExpectedAggregationProjection.java | 7 +++++++
.../main/resources/case/dml/select-aggregate.xml | 15 ++++++++++++++
.../sql/supported/dml/select-aggregate.xml | 1 +
8 files changed, 100 insertions(+), 13 deletions(-)
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 3c78ca48ed9..6c354bb2d13 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
@@ -187,9 +187,9 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.union.UnionSe
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeLengthSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
@@ -773,10 +773,26 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
private ASTNode createAggregationSegment(final AggregationFunctionContext ctx, final String aggregationType) {
AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase());
String innerExpression = ctx.start.getInputStream().getText(new Interval(ctx.LP_().getSymbol().getStartIndex(), ctx.stop.getStopIndex()));
- if (null == ctx.distinct()) {
- return new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ if (null != ctx.distinct()) {
+ AggregationDistinctProjectionSegment result = new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
+ type, innerExpression, getDistinctExpression(ctx));
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+ AggregationProjectionSegment result = new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+
+ private Collection<ExpressionSegment> getExpressions(final AggregationFunctionContext ctx) {
+ if (null == ctx.expr()) {
+ return Collections.emptyList();
}
- return new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression, getDistinctExpression(ctx));
+ Collection<ExpressionSegment> result = new LinkedList<>();
+ for (ExprContext each : ctx.expr()) {
+ result.add((ExpressionSegment) visit(each));
+ }
+ return result;
}
private String getDistinctExpression(final AggregationFunctionContext ctx) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
index 843e00ca83d..806c65e2bb4 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleStatementSQLVisitor.java
@@ -503,10 +503,22 @@ public abstract class OracleStatementSQLVisitor extends OracleStatementBaseVisit
private ASTNode createAggregationSegment(final AggregationFunctionContext ctx, final String aggregationType) {
AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase());
String innerExpression = ctx.start.getInputStream().getText(new Interval(ctx.LP_().get(0).getSymbol().getStartIndex(), ctx.stop.getStopIndex()));
- if (null == ctx.DISTINCT()) {
- return new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ if (null != ctx.DISTINCT()) {
+ AggregationDistinctProjectionSegment result = new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
+ type, innerExpression, getDistinctExpression(ctx));
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+ AggregationProjectionSegment result = new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+
+ private Collection<ExpressionSegment> getExpressions(final AggregationFunctionContext ctx) {
+ if (null == ctx.expr()) {
+ return Collections.emptyList();
}
- return new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression, getDistinctExpression(ctx));
+ return Collections.singletonList((ExpressionSegment) visit(ctx.expr()));
}
private String getDistinctExpression(final AggregationFunctionContext ctx) {
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/SQL92StatementSQLVisitor.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/SQL92StatementSQLVisitor.java
index 298af22ae11..98a799c07b5 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/SQL92StatementSQLVisitor.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/SQL92StatementSQLVisitor.java
@@ -446,10 +446,26 @@ public abstract class SQL92StatementSQLVisitor extends SQL92StatementBaseVisitor
private ASTNode createAggregationSegment(final AggregationFunctionContext ctx, final String aggregationType) {
AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase());
String innerExpression = ctx.start.getInputStream().getText(new Interval(ctx.LP_().getSymbol().getStartIndex(), ctx.stop.getStopIndex()));
- if (null == ctx.distinct()) {
- return new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ if (null != ctx.distinct()) {
+ AggregationDistinctProjectionSegment result = new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
+ type, innerExpression, getDistinctExpression(ctx));
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+ AggregationProjectionSegment result = new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+
+ private Collection<ExpressionSegment> getExpressions(final AggregationFunctionContext ctx) {
+ if (null == ctx.expr()) {
+ return Collections.emptyList();
}
- return new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression, getDistinctExpression(ctx));
+ Collection<ExpressionSegment> result = new LinkedList<>();
+ for (ExprContext each : ctx.expr()) {
+ result.add((ExpressionSegment) visit(each));
+ }
+ return result;
}
private String getDistinctExpression(final AggregationFunctionContext ctx) {
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/SQLServerStatementSQLVisitor.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/SQLServerStatementSQLVisitor.java
index 18e2fd76b5b..f9bcb3b3540 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/SQLServerStatementSQLVisitor.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/SQLServerStatementSQLVisitor.java
@@ -568,10 +568,26 @@ public abstract class SQLServerStatementSQLVisitor extends SQLServerStatementBas
private ASTNode createAggregationSegment(final AggregationFunctionContext ctx, final String aggregationType) {
AggregationType type = AggregationType.valueOf(aggregationType.toUpperCase());
String innerExpression = ctx.start.getInputStream().getText(new Interval(ctx.LP_().getSymbol().getStartIndex(), ctx.stop.getStopIndex()));
- if (null == ctx.distinct()) {
- return new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ if (null != ctx.distinct()) {
+ AggregationDistinctProjectionSegment result = new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
+ type, innerExpression, getDistinctExpression(ctx));
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+ AggregationProjectionSegment result = new AggregationProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression);
+ result.getParameters().addAll(getExpressions(ctx));
+ return result;
+ }
+
+ private Collection<ExpressionSegment> getExpressions(final AggregationFunctionContext ctx) {
+ if (null == ctx.expr()) {
+ return Collections.emptyList();
+ }
+ Collection<ExpressionSegment> result = new LinkedList<>();
+ for (ExprContext each : ctx.expr()) {
+ result.add((ExpressionSegment) visit(each));
}
- return new AggregationDistinctProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), type, innerExpression, getDistinctExpression(ctx));
+ return result;
}
private String getDistinctExpression(final AggregationFunctionContext ctx) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/AggregationProjectionSegment.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/AggregationProjectionSegment.java
index e91306a3d45..098c323c9e2 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/AggregationProjectionSegment.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/AggregationProjectionSegment.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
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.Collection;
+import java.util.LinkedList;
import java.util.Optional;
/**
@@ -40,6 +42,8 @@ public class AggregationProjectionSegment implements ProjectionSegment, AliasAva
private final String innerExpression;
+ private final Collection<ExpressionSegment> parameters = new LinkedList<>();
+
@Setter
private AliasSegment alias;
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/projection/impl/aggregation/ExpectedAggregationProjection.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/projection/impl/aggregation/ExpectedAggregationProjection.java
index 7a4297e9e97..cb472859294 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/projection/impl/aggregation/ExpectedAggregationProjection.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/projection/impl/aggregation/ExpectedAggregationProjection.java
@@ -20,9 +20,13 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domai
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.AbstractExpectedSQLSegment;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.expr.ExpectedExpression;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.projection.ExpectedProjection;
import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import java.util.LinkedList;
+import java.util.List;
@Getter
@Setter
@@ -36,4 +40,7 @@ public class ExpectedAggregationProjection extends AbstractExpectedSQLSegment im
@XmlAttribute
private String alias;
+
+ @XmlElement(name = "parameters")
+ private List<ExpectedExpression> parameters = new LinkedList<>();
}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
index 3c6da3d2944..ae9bcbb865f 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
@@ -26,6 +26,21 @@
</projections>
</select>
+ <select sql-case-id="select_sum_column">
+ <from>
+ <simple-table name="t_order" start-index="48" stop-index="54" />
+ </from>
+ <projections start-index="7" stop-index="41">
+ <aggregation-projection type="SUM" inner-expression="(t_order.user_id)" alias="user_id_sum" start-index="7" stop-index="26">
+ <paramters>
+ <column name="user_id" start-index="17" stop-index="25">
+ <owner name="t_order" start-index="8" stop-index="16"/>
+ </column>
+ </paramters>
+ </aggregation-projection>
+ </projections>
+ </select>
+
<select sql-case-id="select_count">
<from>
<simple-table name="t_order" start-index="37" stop-index="43" />
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
index a4ba5633ea3..5abe97537d5 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
@@ -18,6 +18,7 @@
<sql-cases>
<sql-case id="select_sum" value="SELECT SUM(user_id) AS user_id_sum FROM t_order" />
+ <sql-case id="select_sum_column" value="SELECT SUM(t_order.user_id) AS user_id_sum FROM t_order" />
<sql-case id="select_count" value="SELECT COUNT(*) AS orders_count FROM t_order" />
<sql-case id="select_count_with_sub" value="SELECT COUNT(*) AS orders_count FROM t_order WHERE order_id > 1-1" />
<sql-case id="select_count_with_sub_with_whitespace" value="SELECT COUNT(*) AS orders_count FROM t_order WHERE order_id > 1 - 1" />