Skip to content

Commit

Permalink
Feat: Support parsing Oracle CREATE MATERIALIZED sql (#27994)
Browse files Browse the repository at this point in the history
* Feat: Support parsing Oracle CREATE MATERIALIZED sql

* add final

* refactor ddl g4 to inline grammer

* refactor ddl and dml g4

* add back additional ddl g4 rules
  • Loading branch information
zhangfengcdt authored Sep 19, 2023
1 parent 3cbd841 commit 2a8cc40
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3397,6 +3397,56 @@ arryDMLSubClause
: LP_ typeName (COMMA_ varrayType)? RP_
;

createMaterializedView
: CREATE MATERIALIZED VIEW materializedViewName (OF typeName )? materializedViewColumnClause? materializedViewPrebuiltClause materializedViewUsingClause? createMvRefresh? (FOR UPDATE)? ( (DISABLE | ENABLE) QUERY REWRITE )? AS selectSubquery
;

materializedViewColumnClause
: ( LP_ (scopedTableRefConstraint | mvColumnAlias) (COMMA_ (scopedTableRefConstraint | mvColumnAlias))* RP_ )
;

materializedViewPrebuiltClause
: ( ON PREBUILT TABLE ( (WITH | WITHOUT) REDUCED PRECISION)? | physicalProperties? (CACHE | NOCACHE)? parallelClause? buildClause?)
;

materializedViewUsingClause
: ( USING INDEX ( (physicalAttributesClause | TABLESPACE tablespaceName)+ )* | USING NO INDEX)
;

mvColumnAlias
: (identifier | quotedString) (ENCRYPT encryptionSpecification)?
;

createMvRefresh
: ( NEVER REFRESH | REFRESH createMvRefreshOptions+)
;

createMvRefreshOptions
: ( (FAST | COMPLETE | FORCE) | ON (DEMAND | COMMIT) | (START WITH | NEXT) | WITH (PRIMARY KEY | ROWID) | USING ( DEFAULT (MASTER LOCAL)? ROLLBACK SEGMENT | (MASTER | LOCAL)? ROLLBACK SEGMENT rb_segment=REGULAR_ID ) | USING (ENFORCED | TRUSTED) CONSTRAINTS)
;

quotedString
: variableName
| CHAR_STRING
| NATIONAL_CHAR_STRING_LIT
;

buildClause
: BUILD (IMMEDIATE | DEFERRED)
;

createMaterializedViewLog
: CREATE MATERIALIZED VIEW LOG ON tableName materializedViewLogAttribute? parallelClause? ( WITH ( COMMA_? ( OBJECT ID | PRIMARY KEY | ROWID | SEQUENCE | COMMIT SCN ) )* (LP_ ( COMMA_? identifier )+ RP_ newViewValuesClause? )? mvLogPurgeClause? )*
;

materializedViewLogAttribute
: ( ( physicalAttributesClause | TABLESPACE tablespaceName | loggingClause | (CACHE | NOCACHE))+ )
;

newViewValuesClause
: (INCLUDING | EXCLUDING ) NEW VALUES
;

alterMaterializedView
: ALTER MATERIALIZED VIEW materializedViewName materializedViewAttribute? alterIotClauses? (USING INDEX physicalAttributesClause)?
((MODIFY scopedTableRefConstraint) | alterMvRefresh)? evaluationEditionClause?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ execute
| alterRollbackSegment
| alterDiskgroup
| alterIndexType
| createMaterializedView
| createMaterializedViewLog
| alterMaterializedView
| alterMaterializedViewLog
| alterFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateSynonymContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateTableContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateTablespaceContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateMaterializedViewContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateMaterializedViewLogContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateTypeContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateViewContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DataTypeDefinitionContext;
Expand Down Expand Up @@ -256,6 +258,8 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateSynonymStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateTablespaceStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateMaterializedViewStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateMaterializedViewLogStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateViewStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateVarrayTypeStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleDisassociateStatisticsStatement;
Expand Down Expand Up @@ -1299,6 +1303,16 @@ public ASTNode visitCreateTablespace(final CreateTablespaceContext ctx) {
return new OracleCreateTablespaceStatement();
}

@Override
public ASTNode visitCreateMaterializedView(final CreateMaterializedViewContext ctx) {
return new OracleCreateMaterializedViewStatement();
}

@Override
public ASTNode visitCreateMaterializedViewLog(final CreateMaterializedViewLogContext ctx) {
return new OracleCreateMaterializedViewLogStatement();
}

@Override
public ASTNode visitCreateCluster(final CreateClusterContext ctx) {
return new OracleCreateClusterStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ public enum SQLVisitorRule {

CREATE_MATERIALIZED_VIEW("CreateMaterializedView", SQLStatementType.DDL),

CREATE_MATERIALIZED_VIEW_LOG("CreateMaterializedViewLog", SQLStatementType.DDL),

CREATE_OPERATOR("CreateOperator", SQLStatementType.DDL),

CREATE_POLICY("CreatePolicy", SQLStatementType.DDL),
Expand Down
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.oracle.ddl;

import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;

/**
* Oracle create materialized view log statement.
*/
public final class OracleCreateMaterializedViewLogStatement extends AbstractSQLStatement implements OracleStatement {
}
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.oracle.ddl;

import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateMaterializedViewStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;

/**
* OracleSQL create materialized view statement.
*/
public final class OracleCreateMaterializedViewStatement extends CreateMaterializedViewStatement implements OracleStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateInmemoryJoinGroupStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateLanguageStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateLockdownProfileStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateMaterializedViewLogStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateMaterializedViewStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreateOperatorStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.ddl.CreatePFileStatementTestCase;
Expand Down Expand Up @@ -1666,6 +1667,9 @@ public final class RootSQLParserTestCases {
@XmlElement(name = "create-materialized-view")
private final List<CreateMaterializedViewStatementTestCase> createMaterializedViewStatementTestCases = new LinkedList<>();

@XmlElement(name = "create-materialized-view-log")
private final List<CreateMaterializedViewLogStatementTestCase> createMaterializedViewLogStatementTestCases = new LinkedList<>();

@XmlElement(name = "create-operator")
private final List<CreateOperatorStatementTestCase> createOperatorStatementTestCases = new LinkedList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;

/**
* Create materialized view statement test case.
*/
public final class CreateMaterializedViewLogStatementTestCase extends SQLParserTestCase {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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>
<create-materialized-view-log sql-case-id="create_materialized_view_log_with" />
<create-materialized-view-log sql-case-id="create_materialized_view_log_with_including_new" />
<create-materialized-view-log sql-case-id="create_materialized_view_log_with_tablespace" />
<create-materialized-view-log sql-case-id="create_materialized_view_log_with_table_schema" />
<create-materialized-view-log sql-case-id="create_materialized_view_log_with_object_id" />
<create-materialized-view-log sql-case-id="create_materialized_view_log_with_row_id" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
<create-materialized-view sql-case-id="create_materialized_view_with_using" />
<create-materialized-view sql-case-id="create_materialized_view_with_no_data" />
<create-materialized-view sql-case-id="create_materialized_view_with_data" />
<create-materialized-view sql-case-id="create_materialized_view_with_refresh_fast" />
<create-materialized-view sql-case-id="create_materialized_view_with_refresh_fast_query_rewrite" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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-cases>
<sql-case id="create_materialized_view_log_with" value="CREATE MATERIALIZED VIEW LOG ON inventories WITH (quantity_on_hand);" db-types="Oracle" />
<sql-case id="create_materialized_view_log_with_including_new" value="CREATE MATERIALIZED VIEW
LOG ON employees WITH PRIMARY KEY INCLUDING NEW VALUES;" db-types="Oracle" />
<sql-case id="create_materialized_view_log_with_tablespace" value="CREATE MATERIALIZED VIEW
LOG ON emp_data PCTFREE 5 PCTUSED 60 TABLESPACE example STORAGE (INITIAL 50K)
REFRESH FAST NEXT sysdate + 7 AS SELECT * FROM employees;" db-types="Oracle" />
<sql-case id="create_materialized_view_log_with_table_schema" value="CREATE MATERIALIZED VIEW
LOG ON &quot;SH&quot;.&quot;CUSTOMERS&quot; WITH ROWID, SEQUENCE(&quot;CUST_ID&quot;)
INCLUDING NEW VALUES;" db-types="Oracle" />
<sql-case id="create_materialized_view_log_with_object_id" value="CREATE MATERIALIZED VIEW LOG ON oe.categories_tab_sys WITH OBJECT ID;" db-types="Oracle" />
<sql-case id="create_materialized_view_log_with_row_id" value="CREATE MATERIALIZED VIEW LOG ON sales WITH ROWID;" db-types="Oracle" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@
<sql-case id="create_materialized_view_with_using" value="CREATE MATERIALIZED VIEW mat_view_heap_psql USING heap_psql AS SELECT f1 from tbl_heap_psql;" db-types="PostgreSQL" />
<sql-case id="create_materialized_view_with_no_data" value="CREATE MATERIALIZED VIEW matview_schema.mv_nodata1 (a) AS SELECT generate_series(1, 10) WITH NO DATA;" db-types="PostgreSQL" />
<sql-case id="create_materialized_view_with_data" value="CREATE MATERIALIZED VIEW matview_schema.mv_withdata1 (a) AS SELECT generate_series(1, 10) WITH DATA;" db-types="PostgreSQL" />
<sql-case id="create_materialized_view_with_refresh_fast" value="CREATE MATERIALIZED VIEW warranty_orders REFRESH FAST AS
SELECT order_id, line_item_id, product_id FROM order_items WHERE EXISTS
(SELECT * FROM inventories i WHERE o.product_id = i.product_id AND i.quantity_on_hand IS NOT NULL)
UNION
SELECT order_id, line_item_id, product_id FROM order_items WHERE quantity > 5;" db-types="Oracle" />
<sql-case id="create_materialized_view_with_refresh_fast_query_rewrite" value="CREATE MATERIALIZED VIEW SH.CUST_MV$SUB1
REFRESH FAST WITH ROWID ON COMMIT ENABLE QUERY REWRITE
AS SELECT SH.SALES.PROD_ID C1, SH.CUSTOMERS.CUST_ID C2,
SUM(SH.SALES.AMOUNT_SOLD) M1, COUNT(SH.SALES.AMOUNT_SOLD) M2, COUNT(*) M3 FROM SH.SALES, SH.CUSTOMERS
WHERE SH.CUSTOMERS.CUST_ID = SH.SALES.CUST_ID AND (SH.SALES.CUST_ID IN (1012, 1010, 1005))
GROUP BY SH.SALES.PROD_ID, SH.CUSTOMERS.CUST_ID;" db-types="Oracle" />
</sql-cases>

0 comments on commit 2a8cc40

Please sign in to comment.