This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 5aa046446f3 Add mysql column not null sql parsing (#32333)
5aa046446f3 is described below
commit 5aa046446f35f4e3efd42c16f799d6f9ad71a5cc
Author: ZhangCheng <[email protected]>
AuthorDate: Wed Jul 31 09:16:11 2024 +0800
Add mysql column not null sql parsing (#32333)
* Add mysql column not null sql parsing
* Add table names judgment for EncryptIndexColumnTokenGenerator
* Add table names judgment for EncryptIndexColumnTokenGenerator
* Add table names judgment for EncryptIndexColumnTokenGenerator
---
.../doris/visitor/statement/type/DorisDDLStatementVisitor.java | 4 ++--
.../mysql/visitor/statement/type/MySQLDDLStatementVisitor.java | 4 ++--
.../asserts/segment/definition/ColumnDefinitionAssert.java | 3 +++
.../jaxb/segment/impl/definition/ExpectedColumnDefinition.java | 3 +++
test/it/parser/src/main/resources/case/ddl/create-table.xml | 8 ++++----
.../parser/src/main/resources/sql/supported/ddl/create-table.xml | 2 +-
6 files changed, 15 insertions(+), 9 deletions(-)
diff --git
a/parser/sql/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/doris/visitor/statement/type/DorisDDLStatementVisitor.java
b/parser/sql/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/doris/visitor/statement/type/DorisDDLStatementVisitor.java
index cf2e5d274e8..288bc0e98f2 100644
---
a/parser/sql/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/doris/visitor/statement/type/DorisDDLStatementVisitor.java
+++
b/parser/sql/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/doris/visitor/statement/type/DorisDDLStatementVisitor.java
@@ -578,8 +578,8 @@ public final class DorisDDLStatementVisitor extends
DorisStatementVisitor implem
DataTypeSegment dataTypeSegment = (DataTypeSegment)
visit(ctx.fieldDefinition().dataType());
boolean isPrimaryKey =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.KEY() && null == each.UNIQUE());
boolean isAutoIncrement =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.AUTO_INCREMENT());
- // TODO parse not null
- ColumnDefinitionSegment result = new
ColumnDefinitionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), column, dataTypeSegment, isPrimaryKey, false);
+ boolean isNotNull =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.NOT() && null != each.NULL());
+ ColumnDefinitionSegment result = new
ColumnDefinitionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), column, dataTypeSegment, isPrimaryKey, isNotNull);
result.getReferencedTables().addAll(getReferencedTables(ctx));
result.setAutoIncrement(isAutoIncrement);
return result;
diff --git
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java
index a0555365bba..820ea3b780c 100644
---
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java
+++
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java
@@ -580,8 +580,8 @@ public final class MySQLDDLStatementVisitor extends
MySQLStatementVisitor implem
DataTypeSegment dataTypeSegment = (DataTypeSegment)
visit(ctx.fieldDefinition().dataType());
boolean isPrimaryKey =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.KEY() && null == each.UNIQUE());
boolean isAutoIncrement =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.AUTO_INCREMENT());
- // TODO parse not null
- ColumnDefinitionSegment result = new
ColumnDefinitionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), column, dataTypeSegment, isPrimaryKey, false);
+ boolean isNotNull =
ctx.fieldDefinition().columnAttribute().stream().anyMatch(each -> null !=
each.NOT() && null != each.NULL());
+ ColumnDefinitionSegment result = new
ColumnDefinitionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), column, dataTypeSegment, isPrimaryKey, isNotNull);
result.getReferencedTables().addAll(getReferencedTables(ctx));
result.setAutoIncrement(isAutoIncrement);
if (null != ctx.fieldDefinition().dataType().charsetWithOptBinary()) {
diff --git
a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/definition/ColumnDefinitionAssert.java
b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/definition/ColumnDefinitionAssert.java
index 9d508ebdc64..9eb025e7fc4 100644
---
a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/definition/ColumnDefinitionAssert.java
+++
b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/definition/ColumnDefinitionAssert.java
@@ -55,5 +55,8 @@ public final class ColumnDefinitionAssert {
TableAssert.assertIs(assertContext, actual.getReferencedTables(),
expected.getReferencedTables());
assertThat(assertContext.getText("Column definition start index
assertion error: "), actual.getStartIndex(), is(expected.getStartIndex()));
assertThat(assertContext.getText("Column definition stop index
assertion error: "), actual.getStopIndex(), is(expected.getStopIndex()));
+ if (expected.isNotNull()) {
+ assertThat(assertContext.getText("Column definition not null
assertion error: "), actual.isNotNull(), is(expected.isNotNull()));
+ }
}
}
diff --git
a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/definition/ExpectedColumnDefinition.java
b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/definition/ExpectedColumnDefinition.java
index 8f42dfad3ef..d4a3abe69a2 100644
---
a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/definition/ExpectedColumnDefinition.java
+++
b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/definition/ExpectedColumnDefinition.java
@@ -47,6 +47,9 @@ public final class ExpectedColumnDefinition extends
AbstractExpectedSQLSegment {
@XmlElement
private ExpectedColumn column;
+ @XmlAttribute(name = "not-null")
+ private boolean notNull;
+
@XmlElement(name = "referenced-table")
private final List<ExpectedSimpleTable> referencedTables = new
LinkedList<>();
}
diff --git a/test/it/parser/src/main/resources/case/ddl/create-table.xml
b/test/it/parser/src/main/resources/case/ddl/create-table.xml
index 438a74b8b66..acce89c02a6 100644
--- a/test/it/parser/src/main/resources/case/ddl/create-table.xml
+++ b/test/it/parser/src/main/resources/case/ddl/create-table.xml
@@ -22,16 +22,16 @@
<column-definition type="int" auto-increment="true" start-index="19"
stop-index="39">
<column name="id" />
</column-definition>
- <column-definition type="dec" start-index="42" stop-index="68">
+ <column-definition type="dec" not-null="true" start-index="42"
stop-index="68">
<column name="col1" />
</column-definition>
- <column-definition type="DEC" start-index="71" stop-index="102">
+ <column-definition type="DEC" not-null="true" start-index="71"
stop-index="102">
<column name="col2" />
</column-definition>
- <column-definition type="FLOAT" start-index="105" stop-index="133">
+ <column-definition type="FLOAT" not-null="true" start-index="105"
stop-index="133">
<column name="col3" />
</column-definition>
- <column-definition type="DECIMAL" start-index="136" stop-index="171">
+ <column-definition type="DECIMAL" not-null="true" start-index="136"
stop-index="171">
<column name="col4" />
</column-definition>
<constraint-definition constraint-name="test3_pk" start-index="174"
stop-index="209">
diff --git
a/test/it/parser/src/main/resources/sql/supported/ddl/create-table.xml
b/test/it/parser/src/main/resources/sql/supported/ddl/create-table.xml
index 76b1422aebe..13da0253883 100644
--- a/test/it/parser/src/main/resources/sql/supported/ddl/create-table.xml
+++ b/test/it/parser/src/main/resources/sql/supported/ddl/create-table.xml
@@ -17,7 +17,7 @@
-->
<sql-cases>
- <sql-case id="create_table_with_column_dec" value="create table test3(id
int auto_increment, col1 dec default 0 not null, col2 DEC(6,2) default 0 not
null, col3 FLOAT default 0 not null, col4 DECIMAL(8,3) default 0 not null,
constraint test3_pk primary key (id))" db-types="MySQL" />
+ <sql-case id="create_table_with_column_dec" value="create table test3(id
int auto_increment, col1 dec default 0 not null, col2 DEC(6,2) default 0 not
null, col3 FLOAT default 0 not null, col4 DECIMAL(8,3) default 0 not null,
constraint test3_pk primary key (id))" db-types="MySQL, Doris" />
<sql-case id="create_table_with_backtick_engine_with_string" value="create
table `t``1`(a int) engine='Innodb'" db-types="MySQL" />
<sql-case id="create_table_with_backtick_engine_innodb" value="create
table `t``1`(a int) engine=INNODB" db-types="MySQL" />
<sql-case id="create_table_with_backtick_engine_myisam" value="create
table `t``1`(a int) engine=myisam" db-types="MySQL" />