Skip to content

Commit

Permalink
Support oracle create view parse (#28432)
Browse files Browse the repository at this point in the history
* Support oracle create view parse

* Add parameter marker

* Format code
  • Loading branch information
zihaoAK47 authored Sep 15, 2023
1 parent c17ec12 commit 74ce2f4
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2058,3 +2058,7 @@ multisetOperator
| INTERSECT
| UNION
;

superview
: identifier
;
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ grammar DDLStatement;

import BaseRule, DCLStatement, DMLStatement;

createView
: CREATE (OR REPLACE)? (NO? FORCE)? (EDITIONING | EDITIONABLE EDITIONING? | NONEDITIONABLE)? VIEW viewName
( SHARING EQ_ (METADATA | DATA | EXTENDED DATA | NONE))?
( LP_ (alias (VISIBLE | INVISIBLE)? inlineConstraint* (COMMA_ alias (VISIBLE | INVISIBLE)? inlineConstraint*)*
| outOfLineConstraint) RP_ | objectViewClause | xmlTypeViewClause)?
( DEFAULT COLLATION collationName)? (BEQUEATH (CURRENT_USER | DEFINER))? AS select subqueryRestrictionClause?
( CONTAINER_MAP | CONTAINERS_DEFAULT)?
;

createTable
: CREATE createTableSpecification TABLE tableName createSharingClause createDefinitionClause createMemOptimizeClause createParentClause
;
Expand Down Expand Up @@ -47,6 +56,11 @@ objectTypeDef
: OBJECT LP_ dataTypeDefinition (COMMA_ dataTypeDefinition)* RP_ finalClause? instantiableClause? persistableClause?
;

objectViewClause
: OF typeName (WITH OBJECT (IDENTIFIER | ID) (DEFAULT | LP_ attribute (COMMA_ attribute)* RP_) | UNDER (schemaName DOT_)? superview)
( LP_ outOfLineConstraint | attribute inlineConstraint* (COMMA_ outOfLineConstraint | attribute inlineConstraint*)* RP_)?
;

finalClause
: NOT? FINAL
;
Expand Down Expand Up @@ -215,6 +229,15 @@ xmlTypeVirtualColumnsClause
: VIRTUAL COLUMNS LP_ (columnName AS LP_ expr RP_ (COMMA_ columnName AS LP_ expr RP_)+) RP_
;

xmlTypeViewClause
: OF XMLTYPE xmlSchemaSpec? WITH OBJECT (IDENTIFIER | ID) (DEFAULT | LP_ expr (COMMA_ expr)* RL_)
;

xmlSchemaSpec
: (XMLSCHEMA xmlSchemaURLName)? ELEMENT (elementName | xmlSchemaURLName POUND_ elementName)
( STORE ALL VARRAYS AS (LOBS | TABLES))? ((ALLOW | DISALLOW) NONSCHEMA)? ((ALLOW | DISALLOW) ANYSCHEMA)?
;

oidClause
: OBJECT IDENTIFIER IS (SYSTEM GENERATED | PRIMARY KEY)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ lexer grammar OracleKeyword;

import Alphabet;

BEQUEATH
: B E Q U E A T H
;

BINARY
: B I N A R Y
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ execute
| insert
| update
| delete
| createView
| createTable
| alterTable
| dropTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
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.CreateViewContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DisassociateStatisticsContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DropClusterContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DropColumnClauseContext;
Expand Down Expand Up @@ -179,6 +180,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleAlterAnalyticViewStatement;
Expand Down Expand Up @@ -248,6 +250,7 @@
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.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;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleDropClusterStatement;
Expand Down Expand Up @@ -301,6 +304,18 @@
*/
public final class OracleDDLStatementVisitor extends OracleStatementVisitor implements DDLStatementVisitor {

@Override
public ASTNode visitCreateView(final CreateViewContext ctx) {
OracleCreateViewStatement result = new OracleCreateViewStatement();
OracleDMLStatementVisitor visitor = new OracleDMLStatementVisitor();
visitor.getParameterMarkerSegments().addAll(getParameterMarkerSegments());
result.setView((SimpleTableSegment) visit(ctx.viewName()));
result.setSelect((SelectStatement) visitor.visit(ctx.select()));
result.setViewDefinition(getOriginalText(ctx.select()));
result.addParameterMarkerSegments(getParameterMarkerSegments());
return result;
}

@SuppressWarnings("unchecked")
@Override
public ASTNode visitCreateTable(final CreateTableContext ctx) {
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.ddl.CreateViewStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;

/**
* Oracle create view statement.
*/
public final class OracleCreateViewStatement extends CreateViewStatement implements OracleStatement {
}
75 changes: 75 additions & 0 deletions test/it/parser/src/main/resources/case/ddl/create-view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,79 @@
</from>
</select>
</create-view>

<create-view sql-case-id="create_or_replace_view_with_select" view-definition="select p.name planet, p.class, m.name moon, m.radius from planets p, moons m where p.name = m.planet_name(+) and m.name(+) not like 'S/%'">
<view name="named_moons" start-index="23" stop-index="33" />
<select>
<projections start-index="45" stop-index="89">
<column-projection name="name" alias="planet" start-index="45" stop-index="57">
<owner name="p" start-index="45" stop-index="45" />
</column-projection>
<column-projection name="class" start-index="60" stop-index="66">
<owner name="p" start-index="60" stop-index="60" />
</column-projection>
<column-projection name="name" alias="moon" start-index="69" stop-index="79">
<owner name="m" start-index="69" stop-index="69" />
</column-projection>
<column-projection name="radius" start-index="82" stop-index="89">
<owner name="m" start-index="82" stop-index="82" />
</column-projection>
</projections>
<from>
<join-table join-type="COMMA">
<left>
<simple-table name="planets" alias="p" start-index="96" stop-index="104" />
</left>
<right>
<simple-table name="moons" alias="m" start-index="107" stop-index="113" />
</right>
</join-table>
</from>
<where start-index="115" stop-index="174">
<expr>
<binary-operation-expression start-index="121" stop-index="174">
<left>
<binary-operation-expression start-index="121" stop-index="145">
<left>
<column name="name" start-index="121" stop-index="126">
<owner name="p" start-index="121" stop-index="121" />
</column>
</left>
<right>
<colum-with-join-operator-segment>
<column name="planet_name" start-index="130" stop-index="142">
<owner name="m" start-index="130" stop-index="130" />
</column>
<join-operator>(+)</join-operator>
</colum-with-join-operator-segment>
</right>
<operator>=</operator>
</binary-operation-expression>
</left>
<right>
<binary-operation-expression start-index="151" stop-index="174">
<left>
<colum-with-join-operator-segment>
<column name="name" start-index="151" stop-index="156">
<owner name="m" start-index="151" stop-index="151" />
</column>
<join-operator>(+)</join-operator>
</colum-with-join-operator-segment>
</left>
<right>
<list-expression start-index="170" stop-index="174">
<items>
<literal-expression value="S/%" start-index="170" stop-index="174" />
</items>
</list-expression>
</right>
<operator>NOT LIKE</operator>
</binary-operation-expression>
</right>
<operator>and</operator>
</binary-operation-expression>
</expr>
</where>
</select>
</create-view>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<sql-case id="create_view_with_check_option" value="CREATE VIEW universal_comedies AS SELECT * FROM comedies WHERE classification = 'U' WITH LOCAL CHECK OPTION" db-types="PostgreSQL,openGauss" />
<sql-case id="create_view_with_recursive" value="CREATE RECURSIVE VIEW public.nums_1_100 (n) AS VALUES (1) UNION ALL SELECT n+1 FROM nums_1_100 WHERE n = 100" db-types="PostgreSQL,openGauss" />
<sql-case id="create_view_with_option" value="CREATE OR REPLACE TEMP view order_view (order_id,user_id) WITH (security_barrier=TRUE) AS SELECT * FROM t_order" db-types="PostgreSQL,openGauss" />
<sql-case id="create_or_replace_view_with_select" value="create or replace view named_moons as select p.name planet, p.class, m.name moon, m.radius from planets p, moons m where p.name = m.planet_name(+) and m.name(+) not like 'S/%'" db-types="Oracle" />
</sql-cases>

0 comments on commit 74ce2f4

Please sign in to comment.