You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by kx...@apache.org on 2023/08/17 10:03:52 UTC
[doris] 11/41: [fix](createTableStmt)fix bug that createTableStmt toSql (#22750)
This is an automated email from the ASF dual-hosted git repository.
kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
commit 0bf6309541b2e891f0fa689672248cc080066c5f
Author: daidai <20...@qq.com>
AuthorDate: Tue Aug 15 10:35:22 2023 +0800
[fix](createTableStmt)fix bug that createTableStmt toSql (#22750)
Issue Number: https://github.com/apache/doris/issues/22749
---
.../org/apache/doris/analysis/CreateTableStmt.java | 3 +
.../java/org/apache/doris/analysis/KeysDesc.java | 2 +-
.../apache/doris/analysis/CreateTableStmtTest.java | 66 +++++++++++++++++++++-
3 files changed, 69 insertions(+), 2 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableStmt.java
index 8aab65cd6f..6a6ff655fe 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableStmt.java
@@ -703,6 +703,9 @@ public class CreateTableStmt extends DdlStmt {
sb.append("EXTERNAL ");
}
sb.append("TABLE ");
+ if (ifNotExists) {
+ sb.append("IF NOT EXISTS ");
+ }
sb.append(tableName.toSql()).append(" (\n");
int idx = 0;
for (ColumnDef columnDef : columnDefs) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/KeysDesc.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/KeysDesc.java
index 36e9154af1..6f2eb96a7d 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/KeysDesc.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/KeysDesc.java
@@ -104,7 +104,7 @@ public class KeysDesc implements Writable {
public String toSql() {
StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(type.name()).append("(");
+ stringBuilder.append(type.toSql()).append("(");
int i = 0;
for (String columnName : keysColumnNames) {
if (i != 0) {
diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableStmtTest.java
index d9e3b0019e..a44c16b071 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableStmtTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableStmtTest.java
@@ -216,7 +216,7 @@ public class CreateTableStmtTest {
new HashDistributionDesc(10, Lists.newArrayList("col3")), properties, null, "");
expectedEx.expect(AnalysisException.class);
expectedEx.expectMessage("table property 'enable_duplicate_without_keys_by_default' only can "
- + "set 'true' when create olap table by default.");
+ + "set 'true' when create olap table by default.");
stmt1.analyze(analyzer);
CreateTableStmt stmt2 = new CreateTableStmt(false, false, tblName, cols, "olap",
@@ -443,4 +443,68 @@ public class CreateTableStmtTest {
"String Type should not be used in key column[string_col].", () -> col.analyze(true));
col.analyze(false);
}
+
+ @Test
+ public void testToSql() {
+ List<ColumnDef> columnDefs = new ArrayList<>();
+ columnDefs.add(new ColumnDef("a", TypeDef.create(PrimitiveType.BIGINT), false));
+ columnDefs.add(new ColumnDef("b", TypeDef.create(PrimitiveType.INT), false));
+ String engineName = "olap";
+ ArrayList<String> aggKeys = Lists.newArrayList("a");
+ KeysDesc keysDesc = new KeysDesc(KeysType.AGG_KEYS, aggKeys);
+ Map<String, String> properties = new HashMap<String, String>() {
+ {
+ put("replication_num", String.valueOf(Math.max(1,
+ Config.min_replication_num_per_tablet)));
+ }
+ };
+ TableName tableName = new TableName("internal", "demo", "testTosql1");
+ CreateTableStmt createTableStmt = new CreateTableStmt(true, false,
+ tableName, columnDefs, engineName, keysDesc, null, null,
+ properties, null, "", null);
+
+ String createTableSql = "CREATE TABLE IF NOT EXISTS `demo`.`testTosql1` (\n"
+ + " `a` bigint(20) NOT NULL COMMENT \"\",\n"
+ + " `b` int(11) NOT NULL COMMENT \"\"\n"
+ + ") ENGINE = olap\n"
+ + "AGGREGATE KEY(`a`)\n"
+ + "PROPERTIES (\"replication_num\" = \"1\")";
+
+ Assert.assertEquals(createTableStmt.toSql(), createTableSql);
+
+
+ columnDefs.add(new ColumnDef("c", TypeDef.create(PrimitiveType.STRING), true));
+ columnDefs.add(new ColumnDef("d", TypeDef.create(PrimitiveType.DOUBLE), true));
+ columnDefs.add(new ColumnDef("e", TypeDef.create(PrimitiveType.DECIMAL128), false));
+ columnDefs.add(new ColumnDef("f", TypeDef.create(PrimitiveType.DATE), false));
+ columnDefs.add(new ColumnDef("g", TypeDef.create(PrimitiveType.SMALLINT), false));
+ columnDefs.add(new ColumnDef("h", TypeDef.create(PrimitiveType.BOOLEAN), false));
+
+ aggKeys = Lists.newArrayList("a", "d", "f");
+ keysDesc = new KeysDesc(KeysType.DUP_KEYS, aggKeys);
+ properties = new HashMap<String, String>() {
+ {
+ put("replication_num", String.valueOf(Math.max(10,
+ Config.min_replication_num_per_tablet)));
+ }
+ };
+ tableName = new TableName("internal", "demo", "testTosql2");
+ createTableStmt = new CreateTableStmt(false, false,
+ tableName, columnDefs, engineName, keysDesc, null, null,
+ properties, null, "", null);
+ createTableSql = "CREATE TABLE `demo`.`testTosql2` (\n"
+ + " `a` bigint(20) NOT NULL COMMENT \"\",\n"
+ + " `b` int(11) NOT NULL COMMENT \"\",\n"
+ + " `c` text NULL COMMENT \"\",\n"
+ + " `d` double NULL COMMENT \"\",\n"
+ + " `e` decimalv3(38, 0) NOT NULL COMMENT \"\",\n"
+ + " `f` date NOT NULL COMMENT \"\",\n"
+ + " `g` smallint(6) NOT NULL COMMENT \"\",\n"
+ + " `h` boolean NOT NULL COMMENT \"\"\n"
+ + ") ENGINE = olap\n"
+ + "DUPLICATE KEY(`a`, `d`, `f`)\n"
+ + "PROPERTIES (\"replication_num\" = \"10\")";
+ Assert.assertEquals(createTableStmt.toSql(), createTableSql);
+
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org