From af5587bb6b53037842288bee84a8e5f1a0fa9f79 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 13 Feb 2020 17:26:08 +0800 Subject: [PATCH] Process LIMIT ALL for PostgreSQL (#4285) * Process LIMIT ALL for PostgreSQL * Add test case --- .../sql/supported/dql/select-pagination.xml | 1 + .../antlr4/imports/postgresql/DMLStatement.g4 | 8 ++-- .../visitor/impl/PostgreSQLDMLVisitor.java | 17 +++---- .../resources/sql/dml/select-pagination.xml | 44 +++++++++++++++++++ .../visitor/dml/select-pagination.xml | 44 +++++++++++++++++++ 5 files changed, 102 insertions(+), 12 deletions(-) diff --git a/sharding-sql-test/src/main/resources/sql/supported/dql/select-pagination.xml b/sharding-sql-test/src/main/resources/sql/supported/dql/select-pagination.xml index cdbae50779..d790b3cc93 100644 --- a/sharding-sql-test/src/main/resources/sql/supported/dql/select-pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/supported/dql/select-pagination.xml @@ -24,6 +24,7 @@ + diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DMLStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DMLStatement.g4 index 844cdbd5fb..2dcf5cd338 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DMLStatement.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DMLStatement.g4 @@ -150,11 +150,11 @@ havingClause ; limitClause - : limitRowCountSyntax_ limitOffsetSyntax_? - | limitOffsetSyntax_ limitRowCountSyntax_? + : limitRowCountSyntax limitOffsetSyntax? + | limitOffsetSyntax limitRowCountSyntax? ; -limitRowCountSyntax_ +limitRowCountSyntax : LIMIT (ALL | limitRowCount) ; @@ -162,7 +162,7 @@ limitRowCount : numberLiterals | parameterMarker ; -limitOffsetSyntax_ +limitOffsetSyntax : OFFSET limitOffset (ROW | ROWS)? ; diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/impl/PostgreSQLDMLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/impl/PostgreSQLDMLVisitor.java index 2ad3a8db60..830075fa8d 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/impl/PostgreSQLDMLVisitor.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/impl/PostgreSQLDMLVisitor.java @@ -406,31 +406,32 @@ public final class PostgreSQLDMLVisitor extends PostgreSQLVisitor { @Override public ASTNode visitLimitClause(final LimitClauseContext ctx) { - if (null != ctx.limitRowCountSyntax_() && null != ctx.limitOffsetSyntax_()) { + if (null != ctx.limitRowCountSyntax() && null != ctx.limitOffsetSyntax()) { return isRowCountBeforeOffset(ctx) ? createLimitSegmentWhenRowCountBeforeOffset(ctx) : createLimitSegmentWhenRowCountAfterOffset(ctx); } return createLimitSegmentWhenRowCountOrOffsetAbsent(ctx); } private boolean isRowCountBeforeOffset(final LimitClauseContext ctx) { - return ctx.limitRowCountSyntax_().getStart().getStartIndex() < ctx.limitOffsetSyntax_().getStart().getStartIndex(); + return ctx.limitRowCountSyntax().getStart().getStartIndex() < ctx.limitOffsetSyntax().getStart().getStartIndex(); } private LimitSegment createLimitSegmentWhenRowCountBeforeOffset(final LimitClauseContext ctx) { - LimitValueSegment rowCount = (LimitValueSegment) visit(ctx.limitRowCountSyntax_().limitRowCount()); - LimitValueSegment offset = (LimitValueSegment) visit(ctx.limitOffsetSyntax_().limitOffset()); + LimitValueSegment rowCount = null == ctx.limitRowCountSyntax().limitRowCount() ? null : (LimitValueSegment) visit(ctx.limitRowCountSyntax().limitRowCount()); + LimitValueSegment offset = (LimitValueSegment) visit(ctx.limitOffsetSyntax().limitOffset()); return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, rowCount); } private LimitSegment createLimitSegmentWhenRowCountAfterOffset(final LimitClauseContext ctx) { - LimitValueSegment offset = (LimitValueSegment) visit(ctx.limitOffsetSyntax_().limitOffset()); - LimitValueSegment rowCount = (LimitValueSegment) visit(ctx.limitRowCountSyntax_().limitRowCount()); + LimitValueSegment offset = (LimitValueSegment) visit(ctx.limitOffsetSyntax().limitOffset()); + LimitValueSegment rowCount = null == ctx.limitRowCountSyntax().limitRowCount() ? null : (LimitValueSegment) visit(ctx.limitRowCountSyntax().limitRowCount()); return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, rowCount); } private LimitSegment createLimitSegmentWhenRowCountOrOffsetAbsent(final LimitClauseContext ctx) { - LimitValueSegment rowCount = null == ctx.limitRowCountSyntax_() ? null : (LimitValueSegment) visit(ctx.limitRowCountSyntax_().limitRowCount()); - LimitValueSegment offset = null == ctx.limitOffsetSyntax_() ? null : (LimitValueSegment) visit(ctx.limitOffsetSyntax_().limitOffset()); + LimitValueSegment rowCount = null == ctx.limitRowCountSyntax() || null == ctx.limitRowCountSyntax().limitRowCount() + ? null : (LimitValueSegment) visit(ctx.limitRowCountSyntax().limitRowCount()); + LimitValueSegment offset = null == ctx.limitOffsetSyntax() ? null : (LimitValueSegment) visit(ctx.limitOffsetSyntax().limitOffset()); return new LimitSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), offset, rowCount); } diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/dml/select-pagination.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/dml/select-pagination.xml index 982365ee3b..28c65a64f4 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/dml/select-pagination.xml +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/dml/select-pagination.xml @@ -296,6 +296,50 @@ + + + +