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 2020/10/28 03:40:11 UTC
[shardingsphere] branch master updated: Improve the definition of
select (#7938)
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 2a14ec6 Improve the definition of select (#7938)
2a14ec6 is described below
commit 2a14ec65021e77f4176ead469a0e876c6e11663a
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Wed Oct 28 11:36:32 2020 +0800
Improve the definition of select (#7938)
* Improve the definition of select
* fix
---
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 4 +
.../src/main/antlr4/imports/mysql/DALStatement.g4 | 2 +-
.../src/main/antlr4/imports/mysql/DMLStatement.g4 | 83 +++++++++++---
.../src/main/antlr4/imports/mysql/MySQLKeyword.g4 | 6 +-
.../statement/impl/MySQLStatementSQLVisitor.java | 127 +++++++++++++--------
5 files changed, 153 insertions(+), 69 deletions(-)
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 1307d02..a76d4c6 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -648,3 +648,7 @@ cursorName
conditionName
: identifier
;
+
+unionOption
+ : ALL | DISTINCT
+ ;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DALStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DALStatement.g4
index 8c7fba7..ca6dbae 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DALStatement.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DALStatement.g4
@@ -381,7 +381,7 @@ explainType
;
explainableStatement
- : select | tableStatement | delete | insert | replace | update
+ : select | delete | insert | replace | update
;
formatName
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DMLStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DMLStatement.g4
index 7130b2f..b6493fd 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DMLStatement.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DMLStatement.g4
@@ -105,15 +105,46 @@ singleTableClause
;
multipleTablesClause
- : multipleTableNames FROM tableReferences | FROM multipleTableNames USING tableReferences
+ : tableAliasRefList FROM tableReferences | FROM tableAliasRefList USING tableReferences
;
multipleTableNames
: tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)*
;
-select
- : withClause? unionClause
+select
+ : queryExpression lockClauseList?
+ | queryExpressionParens
+ | selectWithInto
+ ;
+
+selectWithInto
+ : LP_ selectWithInto RP_
+ | queryExpression selectIntoExpression lockClauseList?
+ | queryExpression lockClauseList selectIntoExpression
+ ;
+
+queryExpression
+ : withClause? (queryExpressionBody | queryExpressionParens) orderByClause? limitClause?
+ ;
+
+queryExpressionBody
+ : queryPrimary
+ | queryExpressionParens UNION unionOption? (queryPrimary | queryExpressionParens)
+ ;
+
+queryExpressionParens
+ : LP_ (queryExpressionParens | queryExpression lockClauseList?) RP_
+ ;
+
+queryPrimary
+ : querySpecification
+ | tableValueConstructor
+ | explicitTable
+ ;
+
+querySpecification
+ : SELECT selectSpecification* projections selectIntoExpression? fromClause? whereClause? groupByClause? havingClause? windowClause?
;
call
@@ -177,12 +208,12 @@ loadXmlStatement
(setAssignmentsClause)?
;
-tableStatement
- : TABLE tableName (ORDER BY columnName)? (LIMIT NUMBER_ (OFFSET NUMBER_)?)?
+explicitTable
+ : TABLE tableName
;
-valuesStatement
- : VALUES rowConstructorList (ORDER BY columnDesignator)? (LIMIT BY NUMBER_)?
+tableValueConstructor
+ : VALUES rowConstructorList
;
columnDesignator
@@ -201,14 +232,6 @@ cteClause
: ignoredIdentifier columnNames? AS subquery
;
-unionClause
- : selectClause (UNION (ALL | DISTINCT)? selectClause)*
- ;
-
-selectClause
- : LP_? SELECT selectSpecification* projections selectIntoExpression? fromClause? whereClause? groupByClause? havingClause? windowClause? orderByClause? limitClause? selectIntoExpression? lockClause? RP_?
- ;
-
selectSpecification
: duplicateSpecification | HIGH_PRIORITY | STRAIGHT_JOIN | SQL_SMALL_RESULT | SQL_BIG_RESULT | SQL_BUFFER_RESULT | (SQL_CACHE | SQL_NO_CACHE) | SQL_CALC_FOUND_ROWS
;
@@ -308,7 +331,7 @@ windowItem
;
subquery
- : LP_ unionClause RP_
+ : queryExpressionParens
;
selectLinesInto
@@ -325,5 +348,31 @@ selectIntoExpression
;
lockClause
- : FOR UPDATE | LOCK IN SHARE MODE
+ : FOR lockStrength lockedRowAction?
+ | FOR lockStrength
+ | LOCK IN SHARE MODE
+ ;
+
+lockClauseList
+ : lockClause+
+ ;
+
+lockStrength
+ : UPDATE | SHARE
+ ;
+
+lockedRowAction
+ : SKIP_SYMBOL LOCKED | NOWAIT
+ ;
+
+tableLockingList
+ : OF tableAliasRefList
+ ;
+
+tableIdentOptWild
+ : tableName DOT_ASTERISK_?
+ ;
+
+tableAliasRefList
+ : tableIdentOptWild+
;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
index 4b15ffc..c04197d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
@@ -2259,9 +2259,9 @@ SIMPLE
: S I M P L E
;
-//SKIP
-// : S K I P
-// ;
+SKIP_SYMBOL
+ : S K I P
+ ;
SLAVE
: S L A V E
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 46c810d..67c18d9 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
@@ -498,9 +498,72 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
@Override
public ASTNode visitSubquery(final SubqueryContext ctx) {
- return visit(ctx.unionClause());
+ return visit(ctx.queryExpressionParens());
}
-
+
+ @Override
+ public ASTNode visitQueryExpressionParens(final MySQLStatementParser.QueryExpressionParensContext ctx) {
+ if (null != ctx.queryExpressionParens()) {
+ return visit(ctx.queryExpressionParens());
+ }
+ MySQLSelectStatement result = (MySQLSelectStatement) visit(ctx.queryExpression());
+ if (null != ctx.lockClauseList()) {
+ result.setLock((LockSegment) visit(ctx.lockClauseList()));
+ }
+ result.setParameterCount(getCurrentParameterIndex());
+ return result;
+ }
+
+ @Override
+ public ASTNode visitLockClauseList(final MySQLStatementParser.LockClauseListContext ctx) {
+ return new LockSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
+ }
+
+ @Override
+ public ASTNode visitQueryExpression(final MySQLStatementParser.QueryExpressionContext ctx) {
+ MySQLSelectStatement result;
+ if (null != ctx.queryExpressionBody()) {
+ result = (MySQLSelectStatement) visit(ctx.queryExpressionBody());
+ } else {
+ result = (MySQLSelectStatement) visit(ctx.queryExpressionParens());
+ }
+ if (null != ctx.orderByClause()) {
+ result.setOrderBy((OrderBySegment) visit(ctx.orderByClause()));
+ }
+ if (null != ctx.limitClause()) {
+ result.setLimit((LimitSegment) visit(ctx.limitClause()));
+ }
+ return result;
+ }
+
+ @Override
+ public ASTNode visitQueryExpressionBody(final MySQLStatementParser.QueryExpressionBodyContext ctx) {
+ if (1 == ctx.getChildCount() && ctx.getChild(0) instanceof MySQLStatementParser.QueryPrimaryContext) {
+ return visit(ctx.queryPrimary());
+ }
+ throw new IllegalStateException("union select is not supported yet.");
+ }
+
+ @Override
+ public ASTNode visitQuerySpecification(final MySQLStatementParser.QuerySpecificationContext ctx) {
+ MySQLSelectStatement result = new MySQLSelectStatement();
+ result.setProjections((ProjectionsSegment) visit(ctx.projections()));
+ if (null != ctx.selectSpecification()) {
+ result.getProjections().setDistinctRow(isDistinct(ctx));
+ }
+ if (null != ctx.fromClause()) {
+ TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
+ result.setFrom(tableSource);
+ }
+ if (null != ctx.whereClause()) {
+ result.setWhere((WhereSegment) visit(ctx.whereClause()));
+ }
+ if (null != ctx.groupByClause()) {
+ result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
+ }
+ return result;
+ }
+
@Override
public final ASTNode visitIntervalExpression(final IntervalExpressionContext ctx) {
calculateParameterCount(Collections.singleton(ctx.intervalValue().expr()));
@@ -950,14 +1013,14 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
DeleteMultiTableSegment result = new DeleteMultiTableSegment();
TableSegment relateTableSource = (TableSegment) visit(ctx.tableReferences());
result.setRelationTable(relateTableSource);
- result.setActualDeleteTables(generateTablesFromTableMultipleTableNames(ctx.multipleTableNames()));
+ result.setActualDeleteTables(generateTablesFromTableMultipleTableNames(ctx.tableAliasRefList()));
return result;
}
- private List<SimpleTableSegment> generateTablesFromTableMultipleTableNames(final MySQLStatementParser.MultipleTableNamesContext ctx) {
+ private List<SimpleTableSegment> generateTablesFromTableMultipleTableNames(final MySQLStatementParser.TableAliasRefListContext ctx) {
List<SimpleTableSegment> result = new LinkedList<>();
- for (TableNameContext each : ctx.tableName()) {
- result.add((SimpleTableSegment) visit(each));
+ for (MySQLStatementParser.TableIdentOptWildContext each : ctx.tableIdentOptWild()) {
+ result.add((SimpleTableSegment) visit(each.tableName()));
}
return result;
}
@@ -965,47 +1028,20 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
@Override
public ASTNode visitSelect(final MySQLStatementParser.SelectContext ctx) {
// TODO :Unsupported for withClause.
- MySQLSelectStatement result = (MySQLSelectStatement) visit(ctx.unionClause());
- result.setParameterCount(getCurrentParameterIndex());
- return result;
- }
-
- @Override
- public ASTNode visitUnionClause(final MySQLStatementParser.UnionClauseContext ctx) {
- // TODO :Unsupported for union SQL.
- return visit(ctx.selectClause(0));
- }
-
- @Override
- public ASTNode visitSelectClause(final MySQLStatementParser.SelectClauseContext ctx) {
- MySQLSelectStatement result = new MySQLSelectStatement();
- result.setProjections((ProjectionsSegment) visit(ctx.projections()));
- if (null != ctx.selectSpecification()) {
- result.getProjections().setDistinctRow(isDistinct(ctx));
- }
- if (null != ctx.fromClause()) {
- TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
- result.setFrom(tableSource);
- }
- if (null != ctx.whereClause()) {
- result.setWhere((WhereSegment) visit(ctx.whereClause()));
- }
- if (null != ctx.groupByClause()) {
- result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
- }
- if (null != ctx.orderByClause()) {
- result.setOrderBy((OrderBySegment) visit(ctx.orderByClause()));
- }
- if (null != ctx.limitClause()) {
- result.setLimit((LimitSegment) visit(ctx.limitClause()));
- }
- if (null != ctx.lockClause()) {
- result.setLock((LockSegment) visit(ctx.lockClause()));
+ MySQLSelectStatement result;
+ if (null != ctx.queryExpression()) {
+ result = (MySQLSelectStatement) visit(ctx.queryExpression());
+ if (null != ctx.lockClauseList()) {
+ result.setLock((LockSegment) visit(ctx.lockClauseList()));
+ }
+ } else {
+ result = (MySQLSelectStatement) visit(ctx.getChild(0));
}
+ result.setParameterCount(getCurrentParameterIndex());
return result;
}
- private boolean isDistinct(final MySQLStatementParser.SelectClauseContext ctx) {
+ private boolean isDistinct(final MySQLStatementParser.QuerySpecificationContext ctx) {
for (MySQLStatementParser.SelectSpecificationContext each : ctx.selectSpecification()) {
if (((BooleanLiteralValue) visit(each)).getValue()) {
return true;
@@ -1258,9 +1294,4 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
}
return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
}
-
- @Override
- public ASTNode visitLockClause(final MySQLStatementParser.LockClauseContext ctx) {
- return new LockSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
- }
}