From 2c2a39d4a58d70071aad9207360e1c32dbfe8088 Mon Sep 17 00:00:00 2001 From: LotusMoon Date: Tue, 9 Jan 2024 18:30:51 +0800 Subject: [PATCH 1/2] Support parsing SQL Server SUBSTRING function --- .../main/antlr4/imports/sqlserver/BaseRule.g4 | 2 +- .../statement/SQLServerStatementVisitor.java | 2 +- .../expr/simple/LiteralExpressionSegment.java | 2 +- .../src/main/resources/case/dml/insert.xml | 12 + .../main/resources/case/dml/select-join.xml | 285 ++++++++++++++++++ .../src/main/resources/case/dml/select.xml | 36 +++ .../resources/sql/supported/dml/insert.xml | 1 + .../sql/supported/dml/select-join.xml | 3 + .../resources/sql/supported/dml/select.xml | 1 + 9 files changed, 341 insertions(+), 3 deletions(-) diff --git a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 index 266be49cf2259..8e15fda832841 100644 --- a/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 +++ b/parser/sql/dialect/sqlserver/src/main/antlr4/imports/sqlserver/BaseRule.g4 @@ -120,7 +120,7 @@ unreservedWord | DATA_RETENTION | TEMPORAL_HISTORY_RETENTION | EDITION | MIXED_PAGE_ALLOCATION | DISABLED | ALLOWED | HADR | MULTI_USER | RESTRICTED_USER | SINGLE_USER | OFFLINE | EMERGENCY | SUSPEND | DATE_CORRELATION_OPTIMIZATION | ELASTIC_POOL | SERVICE_OBJECTIVE | DATABASE_NAME | ALLOW_CONNECTIONS | GEO | NAMED | DATEFIRST | BACKUP_STORAGE_REDUNDANCY | FORCE_FAILOVER_ALLOW_DATA_LOSS | SECONDARY | FAILOVER | DEFAULT_FULLTEXT_LANGUAGE | DEFAULT_LANGUAGE | INLINE | NESTED_TRIGGERS | TRANSFORM_NOISE_WORDS | TWO_DIGIT_YEAR_CUTOFF | PERSISTENT_LOG_BUFFER | DIRECTORY_NAME | DATEFORMAT | DELAYED_DURABILITY | TRANSFER | SCHEMA | PASSWORD | AUTHORIZATION - | MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER | COLUMNS + | MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER | COLUMNS | SUBSTRING | RETURNS ; databaseName 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 757b1f7d7f9df..7530c626f47b1 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 @@ -670,7 +670,7 @@ public ASTNode visitConvertFunction(final ConvertFunctionContext ctx) { result.getParameters().add((DataTypeSegment) visit(ctx.dataType())); result.getParameters().add((ExpressionSegment) visit(ctx.expr())); if (null != ctx.NUMBER_()) { - result.getParameters().add(new LiteralExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.NUMBER_().getText())); + result.getParameters().add(new LiteralExpressionSegment(ctx.NUMBER_().getSymbol().getStartIndex(), ctx.NUMBER_().getSymbol().getStopIndex(), ctx.NUMBER_().getText())); } return result; } diff --git a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/LiteralExpressionSegment.java b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/LiteralExpressionSegment.java index d926550afa4df..46197f0a63365 100644 --- a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/LiteralExpressionSegment.java +++ b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/simple/LiteralExpressionSegment.java @@ -35,6 +35,6 @@ public class LiteralExpressionSegment implements SimpleExpressionSegment { @Override public String getText() { - return literals.toString(); + return null != literals ? literals.toString() : null; } } 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 14ce407ad7c49..8310d066253a2 100644 --- a/test/it/parser/src/main/resources/case/dml/insert.xml +++ b/test/it/parser/src/main/resources/case/dml/insert.xml @@ -3061,4 +3061,16 @@ + + + + + + + + + + + + diff --git a/test/it/parser/src/main/resources/case/dml/select-join.xml b/test/it/parser/src/main/resources/case/dml/select-join.xml index 479dde0011365..84a575c1436d2 100644 --- a/test/it/parser/src/main/resources/case/dml/select-join.xml +++ b/test/it/parser/src/main/resources/case/dml/select-join.xml @@ -1083,4 +1083,289 @@ + + + + + + diff --git a/test/it/parser/src/main/resources/case/dml/select.xml b/test/it/parser/src/main/resources/case/dml/select.xml index 25a46d41758b9..bd7e16e6b5aac 100644 --- a/test/it/parser/src/main/resources/case/dml/select.xml +++ b/test/it/parser/src/main/resources/case/dml/select.xml @@ -7990,4 +7990,40 @@ + + 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 de9d8a43261f0..f01c9ca187df5 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 @@ -110,4 +110,5 @@ + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-join.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-join.xml index 7a6fae3f09a79..be02e2036154b 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/select-join.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/select-join.xml @@ -39,4 +39,7 @@ + + + diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select.xml b/test/it/parser/src/main/resources/sql/supported/dml/select.xml index fd6938b456c42..f466d50915476 100644 --- a/test/it/parser/src/main/resources/sql/supported/dml/select.xml +++ b/test/it/parser/src/main/resources/sql/supported/dml/select.xml @@ -242,4 +242,5 @@ + From 058f6290fd2e867cb0e2ac4e573b900699ee6977 Mon Sep 17 00:00:00 2001 From: LotusMoon Date: Tue, 9 Jan 2024 18:41:20 +0800 Subject: [PATCH 2/2] fix convert function parser error --- .../src/main/resources/case/dml/select.xml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/it/parser/src/main/resources/case/dml/select.xml b/test/it/parser/src/main/resources/case/dml/select.xml index bd7e16e6b5aac..f7e6678ce3616 100644 --- a/test/it/parser/src/main/resources/case/dml/select.xml +++ b/test/it/parser/src/main/resources/case/dml/select.xml @@ -3885,7 +3885,7 @@ - + @@ -3929,7 +3929,7 @@ - + @@ -3966,7 +3966,7 @@ - + @@ -4000,7 +4000,7 @@ - + @@ -4037,7 +4037,7 @@ - + @@ -4057,7 +4057,7 @@ - + @@ -4077,7 +4077,7 @@ - + @@ -4097,7 +4097,7 @@ - +