Skip to content

Commit

Permalink
Support PostgreSQL Open statement parse and small ANTLR change for Po…
Browse files Browse the repository at this point in the history
…stgreSQL Declare statement (#31396)

* Support PostgreSQL OPEN and small ANTLR change for DECLARE

* Fix checkstyle issues
  • Loading branch information
ThanoshanMV authored May 26, 2024
1 parent 2a9dabb commit 0a4f183
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ patternMatchingOperator
;

cursorName
: name
: name | hostVariable
;

aExpr
Expand Down Expand Up @@ -1934,3 +1934,7 @@ ifExists
booleanValue
: TRUE | ON | FALSE | OFF | NUMBER_
;

hostVariable
: (COLON_)? identifier
;
Original file line number Diff line number Diff line change
Expand Up @@ -1790,21 +1790,32 @@ importQualificationType
: LIMIT TO | EXCEPT
;


declare
: DECLARE cursorName cursorOptions CURSOR (WITH HOLD | WITHOUT HOLD)? FOR select
: DECLARE cursorName cursorOption CURSOR ((WITH | WITHOUT) HOLD)? FOR select
;

cursorOptions
: cursorOption*
cursorOption
: BINARY? (ASENSITIVE | INSENSITIVE)? (NO? SCROLL)?
;

cursorOption
: NO SCROLL
| SCROLL
| BINARY
| ASENSITIVE
| INSENSITIVE
open
: OPEN cursorName (usingValueClause | usingSqlDescriptorClause)?
;

usingValueClause
: USING value (COMMA_ value)*
;

value
: aexprConst | hostVariable
;

usingSqlDescriptorClause
: USING SQL DESCRIPTOR descriptorName
;

descriptorName
: identifier | hostVariable
;

move
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1460,3 +1460,7 @@ NOBYPASSRLS
ASENSITIVE
: A S E N S I T I V E
;

DESCRIPTOR
: D E S C R I P T O R
;
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,6 @@ execute
| alterTrigger
| createPublication
| emptyStatement
| open
) SEMI_? EOF
;
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.GroupByItemContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.GroupClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.HavingClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.HostVariableContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.InExprContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexNameContext;
Expand Down Expand Up @@ -1392,6 +1393,11 @@ public ASTNode visitName(final NameContext ctx) {
return visit(ctx.identifier());
}

@Override
public ASTNode visitHostVariable(final HostVariableContext ctx) {
return visit(ctx.identifier());
}

@Override
public ASTNode visitSignedIconst(final SignedIconstContext ctx) {
return new NumberLiteralValue(ctx.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.NameListContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.NextContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.NotifyStmtContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.OpenContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.PrepareContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.PriorContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.RefreshMatViewStmtContext;
Expand Down Expand Up @@ -312,6 +313,7 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLListenStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLMoveStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLNotifyStmtStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLOpenStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLPrepareStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLRefreshMatViewStmtStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLReindexStatement;
Expand Down Expand Up @@ -1233,7 +1235,8 @@ public ASTNode visitClose(final CloseContext ctx) {

@Override
public ASTNode visitCursorName(final CursorNameContext ctx) {
return new CursorNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.name()));
return null != ctx.name() ? new CursorNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.name()))
: new CursorNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.hostVariable()));
}

@Override
Expand Down Expand Up @@ -1438,4 +1441,11 @@ public ASTNode visitCreatePolicy(final CreatePolicyContext ctx) {
public ASTNode visitCreatePublication(final CreatePublicationContext ctx) {
return new PostgreSQLCreatePublicationStatement();
}

@Override
public ASTNode visitOpen(final OpenContext ctx) {
PostgreSQLOpenStatement result = new PostgreSQLOpenStatement();
result.setCursorName((CursorNameSegment) visit(ctx.cursorName()));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ public enum SQLVisitorRule {

SPOOL("Spool", SQLStatementType.DAL),

START_REPLICA("StartReplica", SQLStatementType.RL);
START_REPLICA("StartReplica", SQLStatementType.RL),

OPEN("Open", SQLStatementType.DDL);

private final String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.common.statement.ddl;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.cursor.CursorNameSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;

/**
* Open statement.
*/
@Getter
@Setter
public abstract class OpenStatement extends AbstractSQLStatement implements DDLStatement {

private CursorNameSegment cursorName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl;

import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.OpenStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;

/**
* PostgreSQL open statement.
*/
public final class PostgreSQLOpenStatement extends OpenStatement implements PostgreSQLStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.ListenStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.MoveStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.NotifyStmtStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.OpenStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.RefreshMatViewStmtStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.ReindexStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.RenameTableStatement;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.MoveStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.NoAuditStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.NotifyStmtStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.OpenStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.RefreshMatViewStmtStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.ReindexStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl.RenameTableStatementAssert;
Expand Down Expand Up @@ -120,6 +122,7 @@
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.MoveStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.NoAuditStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.NotifyStmtStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.OpenStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.RefreshMatViewStmtStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.ReindexStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.RenameTableStatementTestCase;
Expand Down Expand Up @@ -212,6 +215,8 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final DDLS
CreateSequenceStatementAssert.assertIs(assertContext, (CreateSequenceStatement) actual, (CreateSequenceStatementTestCase) expected);
} else if (actual instanceof SQLServerUpdateStatisticsStatement) {
SQLServerUpdateStatisticsStatementAssert.assertIs(assertContext, (SQLServerUpdateStatisticsStatement) actual, (UpdateStatisticsStatementTestCase) expected);
} else if (actual instanceof OpenStatement) {
OpenStatementAssert.assertIs(assertContext, (OpenStatement) actual, (OpenStatementTestCase) expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.ddl.impl;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.OpenStatement;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.SQLSegmentAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.segment.identifier.IdentifierValueAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.OpenStatementTestCase;

/**
* Open statement assert.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OpenStatementAssert {

/**
* Assert open statement is correct with expected parser result.
*
* @param assertContext assert context
* @param actual actual declare statement
* @param expected expected declare statement test case
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final OpenStatement actual, final OpenStatementTestCase expected) {
assertCursorName(assertContext, actual, expected);
}

private static void assertCursorName(final SQLCaseAssertContext assertContext, final OpenStatement actual, final OpenStatementTestCase expected) {
IdentifierValueAssert.assertIs(assertContext, actual.getCursorName().getIdentifier(), expected.getCursorName(), "Cursor");
SQLSegmentAssert.assertIs(assertContext, actual.getCursorName(), expected.getCursorName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.MoveStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.NoAuditStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.NotifyStmtStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.OpenStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.PreparedStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.PurgeStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.RefreshMatViewStmtStatementTestCase;
Expand Down Expand Up @@ -1757,6 +1758,9 @@ public final class RootSQLParserTestCases {
@XmlElement(name = "create-outline")
private final List<CreateOutlineStatementTestCase> createOutlineStatementTestCases = new LinkedList<>();

@XmlElement(name = "open")
private final List<OpenStatementTestCase> openTestCases = new LinkedList<>();

/**
* Get all SQL parser test cases.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.cursor.ExpectedCursorName;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;

import javax.xml.bind.annotation.XmlElement;

/**
* Open statement test case.
*/
@Getter
@Setter
public final class OpenStatementTestCase extends SQLParserTestCase {

@XmlElement(name = "cursor-name")
private ExpectedCursorName cursorName;
}
35 changes: 35 additions & 0 deletions test/it/parser/src/main/resources/case/ddl/open.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<sql-parser-test-cases>
<open sql-case-id="open_cursor">
<cursor-name name="t_order_cursor" start-index="5" stop-index="18" />
</open>

<open sql-case-id="open_cursor_using">
<cursor-name name="t_order_cursor" start-index="5" stop-index="18" />
</open>

<open sql-case-id="open_cursor_using_sql">
<cursor-name name="t_order_cursor" start-index="5" stop-index="18" />
</open>

<open sql-case-id="open_cursor_with_host_variable">
<cursor-name name="t_order_cursor" start-index="5" stop-index="19" />
</open>
</sql-parser-test-cases>
Loading

0 comments on commit 0a4f183

Please sign in to comment.