Skip to content

Commit

Permalink
Fix oracle insert and alter table parse error (#28467)
Browse files Browse the repository at this point in the history
* Fix oracle insert and alter table parse error

* Change addColumnSpecification define

* Add modify constraint definition test
  • Loading branch information
zihaoAK47 authored Sep 24, 2023
1 parent 73a6c78 commit a902754
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ unreservedWord1
| DO | DEFINER | CURRENT_USER | CASCADED | CLOSE | OPEN | NEXT | NAME | NAMES
| COLLATION | REAL | TYPE | FIRST | RANK | SAMPLE | SYSTIMESTAMP | MINUTE | ANY
| LENGTH | SINGLE_C | TIME_UNIT | TARGET | PUBLIC | ID | STATE | PRIORITY
| CONSTRAINT | PRIMARY | FOREIGN | KEY | POSITION | PRECISION | FUNCTION | PROCEDURE | SPECIFICATION | CASE
| PRIMARY | FOREIGN | KEY | POSITION | PRECISION | FUNCTION | PROCEDURE | SPECIFICATION | CASE
| WHEN | CAST | TRIM | SUBSTRING | FULL | INNER | OUTER | LEFT | RIGHT | CROSS
| USING | FALSE | SAVEPOINT | BODY | CHARACTER | ARRAY | TIME | TIMEOUT | TIMESTAMP | LOCALTIME
| DAY | ENABLE | DISABLE | CALL | INSTANCE | CLOSE | NEXT | NAME | NUMERIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ operateColumnClause
;

addColumnSpecification
: ADD LP_ columnOrVirtualDefinitions RP_ columnProperties?
: ADD (LP_ columnOrVirtualDefinitions RP_ | columnOrVirtualDefinitions) columnProperties?
;

columnOrVirtualDefinitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.ModifyCollectionRetrievalSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.RenameColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.alter.AddConstraintDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.alter.ModifyConstraintDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.RenameIndexDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.table.ConvertTableDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
Expand All @@ -46,6 +47,7 @@
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedAddColumnDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedChangeColumnDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedColumnDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedConstraintDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedModifyColumnDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedRenameIndexDefinition;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.definition.ExpectedRenameColumnDefinition;
Expand Down Expand Up @@ -81,6 +83,7 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final Alte
assertRenameTable(assertContext, actual, expected);
assertAddColumnDefinitions(assertContext, actual, expected);
assertAddConstraintDefinitions(assertContext, actual, expected);
assertModifyConstraintDefinitions(assertContext, actual, expected);
assertModifyColumnDefinitions(assertContext, actual, expected);
assertChangeColumnDefinitions(assertContext, actual, expected);
assertDropColumns(assertContext, actual, expected);
Expand Down Expand Up @@ -152,6 +155,22 @@ private static void assertAddConstraintDefinitions(final SQLCaseAssertContext as
}
}

private static void assertModifyConstraintDefinitions(final SQLCaseAssertContext assertContext, final AlterTableStatement actual, final AlterTableStatementTestCase expected) {
assertThat(assertContext.getText("Modify constraint definitions size assertion error: "), actual.getModifyConstraintDefinitions().size(), is(expected.getModifyConstraints().size()));
int count = 0;
for (ModifyConstraintDefinitionSegment each : actual.getModifyConstraintDefinitions()) {
ExpectedConstraintDefinition expectedConstraintDefinition = expected.getModifyConstraints().get(count);
if (null == expectedConstraintDefinition.getConstraintName()) {
assertNull(each.getConstraintName(), "Actual modify constraint name should not exist.");
} else {
assertNotNull(each.getConstraintName(), "Actual modify constraint name should exist.");
assertThat(assertContext.getText("Actual modify constraint name assertion error."),
each.getConstraintName().getIdentifier().getValue(), is(expectedConstraintDefinition.getConstraintName()));
}
count++;
}
}

private static void assertModifyColumnDefinitions(final SQLCaseAssertContext assertContext, final AlterTableStatement actual, final AlterTableStatementTestCase expected) {
assertThat(assertContext.getText("Modify column definitions size assertion error: "), actual.getModifyColumnDefinitions().size(), is(expected.getModifyColumns().size()));
int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public final class AlterTableStatementTestCase extends SQLParserTestCase {
@XmlElement(name = "add-constraint")
private final List<ExpectedConstraintDefinition> addConstraints = new LinkedList<>();

@XmlElement(name = "modify-constraint")
private final List<ExpectedConstraintDefinition> modifyConstraints = new LinkedList<>();

@XmlElement(name = "modify-column")
private final List<ExpectedModifyColumnDefinition> modifyColumns = new LinkedList<>();

Expand Down
16 changes: 11 additions & 5 deletions test/it/parser/src/main/resources/case/ddl/alter-table.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@

<alter-table sql-case-id="alter_table_modify7">
<table name="locations" start-index="12" stop-index="20" />
<modify-constraint start-index="22" stop-index="55" />
</alter-table>

<alter-table sql-case-id="alter_table_modify8">
Expand All @@ -154,11 +155,7 @@

<alter-table sql-case-id="alter_table_modify9">
<table name="sales" start-index="12" stop-index="16" />
<modify-column>
<column-definition type="time_view_fk" start-index="25" stop-index="52">
<column name="CONSTRAINT" />
</column-definition>
</modify-column>
<modify-constraint start-index="18" stop-index="52" constraint-name="time_view_fk" />
</alter-table>

<alter-table sql-case-id="alter_table_modify10">
Expand Down Expand Up @@ -1702,4 +1699,13 @@
</column-definition>
</add-column>
</alter-table>

<alter-table sql-case-id="alter_table_add_column_no_parentheses">
<table name="T_MASK" start-index="12" stop-index="17" />
<add-column>
<column-definition type="VARCHAR2" start-index="23" stop-index="46">
<column name="new_column" />
</column-definition>
</add-column>
</alter-table>
</sql-parser-test-cases>
107 changes: 106 additions & 1 deletion test/it/parser/src/main/resources/case/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@
</value>
</values>
</insert>

<insert sql-case-id="insert_with_oracle_datetime_type">
<table name="t_order" start-index="12" stop-index="18" />
<columns start-index="20" stop-index="93">
Expand Down Expand Up @@ -2615,4 +2615,109 @@
</value>
</values>
</insert>

<insert sql-case-id="insert_all_into">
<multi-table-insert-type value="ALL" />
<multi-table-insert-into start-index="11" stop-index="213">
<insert-statement>
<table name="T_MASK" start-index="16" stop-index="21" />
<columns start-index="22" stop-index="50">
<column name="ID" start-index="23" stop-index="24" />
<column name="EMAIL" start-index="26" stop-index="30" />
<column name="NAME" start-index="32" stop-index="35" />
<column name="PHONE" start-index="37" stop-index="41" />
<column name="ADDRESS" start-index="43" stop-index="49" />
</columns>
<values>
<value>
<assignment-value>
<literal-expression value="1" start-index="60" stop-index="60" />
</assignment-value>
<assignment-value>
<literal-expression value="2" start-index="62" stop-index="64" />
</assignment-value>
<assignment-value>
<literal-expression value="3" start-index="66" stop-index="68" />
</assignment-value>
<assignment-value>
<literal-expression value="4" start-index="70" stop-index="72" />
</assignment-value>
<assignment-value>
<literal-expression value="5" start-index="74" stop-index="76" />
</assignment-value>
</value>
</values>
</insert-statement>
<insert-statement>
<table name="T_MASK" start-index="84" stop-index="89" />
<columns start-index="90" stop-index="118">
<column name="ID" start-index="91" stop-index="92" />
<column name="EMAIL" start-index="94" stop-index="98" />
<column name="NAME" start-index="100" stop-index="103" />
<column name="PHONE" start-index="105" stop-index="109" />
<column name="ADDRESS" start-index="111" stop-index="117" />
</columns>
<values>
<value>
<assignment-value>
<literal-expression value="2" start-index="128" stop-index="128" />
</assignment-value>
<assignment-value>
<literal-expression value="2" start-index="130" stop-index="132" />
</assignment-value>
<assignment-value>
<literal-expression value="3" start-index="134" stop-index="136" />
</assignment-value>
<assignment-value>
<literal-expression value="4" start-index="138" stop-index="140" />
</assignment-value>
<assignment-value>
<literal-expression value="5" start-index="142" stop-index="144" />
</assignment-value>
</value>
</values>
</insert-statement>
<insert-statement>
<table name="T_MASK" start-index="152" stop-index="157" />
<columns start-index="158" stop-index="186">
<column name="ID" start-index="159" stop-index="160" />
<column name="EMAIL" start-index="162" stop-index="166" />
<column name="NAME" start-index="168" stop-index="171" />
<column name="PHONE" start-index="173" stop-index="177" />
<column name="ADDRESS" start-index="179" stop-index="185" />
</columns>
<values>
<value>
<assignment-value>
<literal-expression value="3" start-index="196" stop-index="196" />
</assignment-value>
<assignment-value>
<literal-expression value="2" start-index="198" stop-index="200" />
</assignment-value>
<assignment-value>
<literal-expression value="3" start-index="202" stop-index="204" />
</assignment-value>
<assignment-value>
<literal-expression value="4" start-index="206" stop-index="208" />
</assignment-value>
<assignment-value>
<literal-expression value="5" start-index="210" stop-index="212" />
</assignment-value>
</value>
</values>
</insert-statement>
</multi-table-insert-into>
<select>
<projections start-index="222" stop-index="222">
<expression-projection start-index="222" stop-index="222" text="1">
<expr>
<literal-expression start-index="222" stop-index="222" value="1" />
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="DUAL" start-index="229" stop-index="232" />
</from>
</select>
</insert>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@
<sql-case id="alter_table_move_subpartition_tablespace_parallel" value="ALTER TABLE scuba_gear MOVE SUBPARTITION bcd_types TABLESPACE tbs23 PARALLEL (DEGREE 2)" db-types="Oracle" />
<sql-case id="alter_table_add_ref_with_rowid" value="ALTER TABLE staff ADD (REF(dept) WITH ROWID)" db-types="Oracle" />
<sql-case id="alter_table_add_scope_for_is" value="ALTER TABLE staff ADD (SCOPE FOR (dept) IS offices)" db-types="Oracle" />
<sql-case id="alter_table_add_column_no_parentheses" value="ALTER TABLE T_MASK ADD new_column VARCHAR2(100)" db-types="Oracle" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@
<sql-case id="insert_with_connect_by_and_prior" value="Insert Into t (c1,c2,c3,c4,c5) select c1,c2,regexp_substr(c3, '[^,]+', 1, l) c3,c4,c5 from t where id=1 connect by l &lt;= regexp_count(c3, ',') + 1 and ID = prior ID and prior dbms_random.value is not null;" db-types="Oracle" />
<sql-case id="insert_with_national_character_set" value="INSERT INTO customers VALUES (1000, TO_NCHAR('John Smith'),N'500 Oracle Parkway',sysdate);" db-types="Oracle" />
<sql-case id="insert_with_oracle_datetime_type" value="INSERT INTO t_order (create_date, create_timestamp, create_interval_year, create_interval_day) VALUES (TO_DATE('2009', 'YYYY'), TO_DATE('2009', 'YYYY'), (TO_DATE('2009', 'YYYY') - TO_DATE('2009', 'YYYY')) year to MONTH, (TO_DATE('2009', 'YYYY') - TO_DATE('2009', 'YYYY')) DAY TO SECOND);" db-types="Oracle" />
<sql-case id="insert_all_into" value="INSERT ALL INTO T_MASK(ID,EMAIL,NAME,PHONE,ADDRESS) VALUES (1,'2','3','4','5') INTO T_MASK(ID,EMAIL,NAME,PHONE,ADDRESS) VALUES (2,'2','3','4','5') INTO T_MASK(ID,EMAIL,NAME,PHONE,ADDRESS) VALUES (3,'2','3','4','5') SELECT 1 FROM DUAL" db-types="Oracle" />
</sql-cases>

0 comments on commit a902754

Please sign in to comment.