From 46433fbc4a166d8458564d2aba9f6aa09b8d9859 Mon Sep 17 00:00:00 2001 From: yydeng626 Date: Sat, 17 Feb 2024 15:03:35 +0800 Subject: [PATCH] Support parsing SQL Server SELECT d.name, sql #29181 --- .../antlr4/imports/sqlserver/DMLStatement.g4 | 8 ++- .../statement/SQLServerStatementVisitor.java | 3 + .../dml/SQLServerSelectStatement.java | 12 ++++ .../src/main/resources/case/dml/insert.xml | 59 +++++++++++++++++++ .../resources/sql/supported/dml/insert.xml | 1 + 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/DMLStatement.g4 b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/DMLStatement.g4 index 4df2d9a0a84c5..d93a3299d43b7 100644 --- a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/DMLStatement.g4 +++ b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/DMLStatement.g4 @@ -40,7 +40,7 @@ insertExecClause ; withTableHint - : WITH LP_ (tableHintLimited+) RP_ + : WITH? LP_ (tableHintLimited+) RP_ ; exec @@ -98,7 +98,7 @@ aggregationClause ; selectClause - : selectWithClause? SELECT duplicateSpecification? projections intoClause? fromClause? whereClause? groupByClause? havingClause? orderByClause? forClause? + : selectWithClause? SELECT duplicateSpecification? projections intoClause? (fromClause withTempTable? withTableHint?)? whereClause? groupByClause? havingClause? orderByClause? forClause? ; duplicateSpecification @@ -178,6 +178,10 @@ subquery : LP_ aggregationClause RP_ ; +withTempTable + : WITH LP_ (columnName dataType) (COMMA_ columnName dataType)* RP_ + ; + withClause : WITH cteClauseSet ; diff --git a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java index 0aebdcc885852..d86e3676d0812 100644 --- a/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java +++ b/parser/sql/dialect/sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/SQLServerStatementVisitor.java @@ -938,6 +938,9 @@ public ASTNode visitSelectClause(final SelectClauseContext ctx) { TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences()); result.setFrom(tableSource); } + if (null != ctx.withTableHint()) { + result.setWithTableHintSegment((WithTableHintSegment) visit(ctx.withTableHint())); + } if (null != ctx.whereClause()) { result.setWhere((WhereSegment) visit(ctx.whereClause())); } diff --git a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerSelectStatement.java b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerSelectStatement.java index 2800c7980f29a..5e7f16e441db7 100644 --- a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerSelectStatement.java +++ b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/sqlserver/dml/SQLServerSelectStatement.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment; import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement; +import org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.hint.WithTableHintSegment; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement; import java.util.Optional; @@ -38,6 +39,8 @@ public final class SQLServerSelectStatement extends SelectStatement implements S private TableSegment intoSegment; + private WithTableHintSegment withTableHintSegment; + /** * Get order by segment. * @@ -64,4 +67,13 @@ public Optional getWithSegment() { public Optional getIntoSegment() { return Optional.ofNullable(intoSegment); } + + /** + * Get with table hint segment. + * + * @return with table hint segment. + */ + public Optional getWithTableHintSegment() { + return Optional.ofNullable(withTableHintSegment); + } } diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml b/test/it/parser/src/main/resources/case/dml/insert.xml index d12092c498e36..918aa58877b38 100644 --- a/test/it/parser/src/main/resources/case/dml/insert.xml +++ b/test/it/parser/src/main/resources/case/dml/insert.xml @@ -3711,4 +3711,63 @@ + + + + + + + + + + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml index d230ffc6f6b9c..1812613d93608 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml @@ -141,4 +141,5 @@ +