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/06 09:41:44 UTC
[shardingsphere] branch master updated: Refactor UnionSegment (#18195)
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 c0be0f13d3f Refactor UnionSegment (#18195)
c0be0f13d3f is described below
commit c0be0f13d3f24084b9cde0ad1662eb412c7fe546
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jun 6 17:41:38 2022 +0800
Refactor UnionSegment (#18195)
---
.../converter/SQLNodeConverterEngine.java | 4 ++--
.../statement/impl/MySQLStatementSQLVisitor.java | 2 +-
.../impl/OpenGaussStatementSQLVisitor.java | 23 +++++++++++-----------
.../impl/PostgreSQLStatementSQLVisitor.java | 17 ++++++++--------
.../sql/common/segment/dml/union/UnionSegment.java | 9 ++-------
5 files changed, 24 insertions(+), 31 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
index c96f1c531a6..3800d2b894c 100644
--- a/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-federation/shardingsphere-infra-federation-optimizer/src/main/java/org/apache/shardingsphere/infra/federation/optimizer/converter/SQLNodeConverterEngine.java
@@ -88,8 +88,8 @@ public final class SQLNodeConverterEngine {
SqlNode rightSqlNode = ((SqlBasicCall) sqlNode).getOperandList().get(1);
SelectStatement leftSelectStatement = (SelectStatement) convertToSQLStatement(leftSqlNode);
SelectStatement rightSelectStatement = (SelectStatement) convertToSQLStatement(rightSqlNode);
- leftSelectStatement.getUnionSegments().add(new UnionSegment(UnionType.UNION_DISTINCT, rightSelectStatement, rightSqlNode.getParserPosition().getColumnNum() - 7,
- rightSqlNode.getParserPosition().getEndColumnNum() - 1));
+ leftSelectStatement.getUnionSegments().add(
+ new UnionSegment(rightSqlNode.getParserPosition().getColumnNum() - 7, rightSqlNode.getParserPosition().getEndColumnNum() - 1, UnionType.UNION_DISTINCT, rightSelectStatement));
return leftSelectStatement;
}
throw new UnsupportedOperationException("Unsupported SQL statement conversion.");
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 6c354bb2d13..c77a35af678 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
@@ -695,7 +695,7 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
public ASTNode visitUnionClause(final UnionClauseContext ctx) {
UnionType unionType = (null != ctx.unionOption() && null != ctx.unionOption().ALL()) ? UnionType.UNION_ALL : UnionType.UNION_DISTINCT;
MySQLSelectStatement statement = null != ctx.queryPrimary() ? (MySQLSelectStatement) visit(ctx.queryPrimary()) : (MySQLSelectStatement) visit(ctx.queryExpressionParens());
- return new UnionSegment(unionType, statement, ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
+ return new UnionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), unionType, statement);
}
@Override
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
index bb74402f98f..f75b7f4d652 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussStatementSQLVisitor.java
@@ -716,9 +716,8 @@ public abstract class OpenGaussStatementSQLVisitor extends OpenGaussStatementBas
new IdentifierValue(ctx.optIndirection().indirectionEl().attrName().getText()));
result.setOwner(new OwnerSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText())));
return result;
- } else {
- return new ColumnSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
}
+ return new ColumnSegment(ctx.colId().start.getStartIndex(), ctx.colId().stop.getStopIndex(), new IdentifierValue(ctx.colId().getText()));
}
private Collection<InsertValuesSegment> createInsertValuesSegments(final ValuesClauseContext ctx) {
@@ -861,28 +860,28 @@ public abstract class OpenGaussStatementSQLVisitor extends OpenGaussStatementBas
public ASTNode visitSelectClauseN(final SelectClauseNContext ctx) {
if (null != ctx.simpleSelect()) {
return visit(ctx.simpleSelect());
- } else if (null != ctx.selectClauseN() && !ctx.selectClauseN().isEmpty()) {
+ }
+ if (null != ctx.selectClauseN() && !ctx.selectClauseN().isEmpty()) {
OpenGaussSelectStatement result = (OpenGaussSelectStatement) visit(ctx.selectClauseN(0));
- UnionSegment unionSegment = new UnionSegment(getUnionType(ctx), (OpenGaussSelectStatement) visit(ctx.selectClauseN(1)),
- ((TerminalNode) ctx.getChild(1)).getSymbol().getStartIndex(), ctx.getStop().getStopIndex());
- result.getUnionSegments().add(unionSegment);
+ result.getUnionSegments().add(new UnionSegment(
+ ((TerminalNode) ctx.getChild(1)).getSymbol().getStartIndex(), ctx.getStop().getStopIndex(), getUnionType(ctx), (OpenGaussSelectStatement) visit(ctx.selectClauseN(1))));
return result;
- } else {
- return visit(ctx.selectWithParens());
}
+ return visit(ctx.selectWithParens());
}
private UnionType getUnionType(final SelectClauseNContext ctx) {
boolean isDistinct = null == ctx.allOrDistinct() || null != ctx.allOrDistinct().DISTINCT();
if (null != ctx.UNION()) {
return isDistinct ? UnionType.UNION_DISTINCT : UnionType.UNION_ALL;
- } else if (null != ctx.INTERSECT()) {
+ }
+ if (null != ctx.INTERSECT()) {
return isDistinct ? UnionType.INTERSECT_DISTINCT : UnionType.INTERSECT_ALL;
- } else if (null != ctx.MINUS()) {
+ }
+ if (null != ctx.MINUS()) {
return isDistinct ? UnionType.MINUS_DISTINCT : UnionType.MINUS_ALL;
- } else {
- return isDistinct ? UnionType.EXCEPT_DISTINCT : UnionType.EXCEPT_ALL;
}
+ return isDistinct ? UnionType.EXCEPT_DISTINCT : UnionType.EXCEPT_ALL;
}
@Override
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
index d86b1bb2a88..f47a2740d84 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
@@ -828,26 +828,25 @@ public abstract class PostgreSQLStatementSQLVisitor extends PostgreSQLStatementP
public ASTNode visitSelectClauseN(final SelectClauseNContext ctx) {
if (null != ctx.simpleSelect()) {
return visit(ctx.simpleSelect());
- } else if (null != ctx.selectClauseN() && !ctx.selectClauseN().isEmpty()) {
+ }
+ if (null != ctx.selectClauseN() && !ctx.selectClauseN().isEmpty()) {
PostgreSQLSelectStatement result = (PostgreSQLSelectStatement) visit(ctx.selectClauseN(0));
- UnionSegment unionSegment = new UnionSegment(getUnionType(ctx), (PostgreSQLSelectStatement) visit(ctx.selectClauseN(1)),
- ((TerminalNode) ctx.getChild(1)).getSymbol().getStartIndex(), ctx.getStop().getStopIndex());
- result.getUnionSegments().add(unionSegment);
+ result.getUnionSegments().add(new UnionSegment(
+ ((TerminalNode) ctx.getChild(1)).getSymbol().getStartIndex(), ctx.getStop().getStopIndex(), getUnionType(ctx), (PostgreSQLSelectStatement) visit(ctx.selectClauseN(1))));
return result;
- } else {
- return visit(ctx.selectWithParens());
}
+ return visit(ctx.selectWithParens());
}
private UnionType getUnionType(final SelectClauseNContext ctx) {
boolean isDistinct = null == ctx.allOrDistinct() || null != ctx.allOrDistinct().DISTINCT();
if (null != ctx.UNION()) {
return isDistinct ? UnionType.UNION_DISTINCT : UnionType.UNION_ALL;
- } else if (null != ctx.INTERSECT()) {
+ }
+ if (null != ctx.INTERSECT()) {
return isDistinct ? UnionType.INTERSECT_DISTINCT : UnionType.INTERSECT_ALL;
- } else {
- return isDistinct ? UnionType.EXCEPT_DISTINCT : UnionType.EXCEPT_ALL;
}
+ return isDistinct ? UnionType.EXCEPT_DISTINCT : UnionType.EXCEPT_ALL;
}
@Override
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/union/UnionSegment.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/union/UnionSegment.java
index ff9121ca76d..ae911625af8 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/union/UnionSegment.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/union/UnionSegment.java
@@ -18,10 +18,12 @@
package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.union;
import lombok.Getter;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.constant.UnionType;
import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+@RequiredArgsConstructor
@Getter
public class UnionSegment implements SQLSegment {
@@ -32,11 +34,4 @@ public class UnionSegment implements SQLSegment {
private final UnionType unionType;
private final SelectStatement selectStatement;
-
- public UnionSegment(final UnionType unionType, final SelectStatement selectStatement, final int startIndex, final int stopIndex) {
- this.unionType = unionType;
- this.selectStatement = selectStatement;
- this.startIndex = startIndex;
- this.stopIndex = stopIndex;
- }
}