diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java index afae03e99f..cea5925b6a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java @@ -270,7 +270,7 @@ public class ColumnDef { throw new AnalysisException("Array type column default value only support null"); } } - if (isKey() && type.getPrimitiveType() == PrimitiveType.STRING) { + if (isKey() && type.getPrimitiveType() == PrimitiveType.STRING && isOlap) { throw new AnalysisException("String Type should not be used in key column[" + getName() + "]."); } 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 75627d0f0c..5371482345 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 @@ -17,11 +17,13 @@ package org.apache.doris.analysis; +import org.apache.doris.analysis.ColumnDef.DefaultValue; import org.apache.doris.catalog.AggregateType; import org.apache.doris.catalog.KeysType; import org.apache.doris.catalog.PrimitiveType; import org.apache.doris.catalog.ScalarType; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ExceptionChecker; import org.apache.doris.common.UserException; import org.apache.doris.datasource.InternalDataSource; import org.apache.doris.mysql.privilege.MockedAuth; @@ -43,8 +45,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Test for create table. + **/ public class CreateTableStmtTest { private static final Logger LOG = LoggerFactory.getLogger(CreateTableStmtTest.class); + @Rule + public ExpectedException expectedEx = ExpectedException.none(); // used to get default db private TableName tblName; @@ -59,12 +66,12 @@ public class CreateTableStmtTest { private PaloAuth auth; @Mocked private ConnectContext ctx; - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - // set default db is 'db1' - // table name is table1 - // Column: [col1 int; col2 string] + /** + * set default db is 'db1' + * table name is table1 + * Column: [col1 int; col2 string] + **/ @Before public void setUp() { // analyzer @@ -107,8 +114,7 @@ public class CreateTableStmtTest { @Test public void testCreateTableWithRandomDistribution() throws UserException { CreateTableStmt stmt = new CreateTableStmt(false, false, tblName, cols, "olap", - new KeysDesc(KeysType.DUP_KEYS, colsName), null, - new RandomDistributionDesc(6), null, null, ""); + new KeysDesc(KeysType.DUP_KEYS, colsName), null, new RandomDistributionDesc(6), null, null, ""); stmt.analyze(analyzer); Assert.assertEquals("testCluster:db1", stmt.getDbName()); Assert.assertEquals("table1", stmt.getTableName()); @@ -128,7 +134,8 @@ public class CreateTableStmtTest { Assert.assertEquals("testCluster:db1", stmt.getDbName()); Assert.assertEquals("table1", stmt.getTableName()); Assert.assertNull(stmt.getProperties()); - Assert.assertTrue(stmt.toSql().contains("rollup( `index1` (`col1`, `col2`) FROM `table1`, `index2` (`col2`, `col3`) FROM `table1`)")); + Assert.assertTrue(stmt.toSql() + .contains("rollup( `index1` (`col1`, `col2`) FROM `table1`, `index2` (`col2`, `col3`) FROM `table1`)")); } @Test @@ -158,8 +165,7 @@ public class CreateTableStmtTest { } }; CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.AGG_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + new KeysDesc(KeysType.AGG_KEYS, colsName), null, new RandomDistributionDesc(10), null, null, ""); stmt.analyze(noDbAnalyzer); } @@ -167,8 +173,7 @@ public class CreateTableStmtTest { public void testEmptyCol() throws UserException, AnalysisException { // make default db return empty; List emptyCols = Lists.newArrayList(); - CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, emptyCols, "olap", - new KeysDesc(), null, + CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, emptyCols, "olap", new KeysDesc(), null, new RandomDistributionDesc(10), null, null, ""); stmt.analyze(analyzer); } @@ -177,8 +182,7 @@ public class CreateTableStmtTest { public void testDupCol() throws UserException, AnalysisException { // make default db return empty; CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, invalidCols, "olap", - new KeysDesc(KeysType.AGG_KEYS, invalidColsName), null, - new RandomDistributionDesc(10), null, null, ""); + new KeysDesc(KeysType.AGG_KEYS, invalidColsName), null, new RandomDistributionDesc(10), null, null, ""); stmt.analyze(analyzer); } @@ -190,8 +194,7 @@ public class CreateTableStmtTest { colsName.add("col3"); CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.AGG_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + new KeysDesc(KeysType.AGG_KEYS, colsName), null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); expectedEx.expectMessage("Key column can not set bitmap or hll type:col3"); stmt.analyze(analyzer); @@ -200,9 +203,8 @@ public class CreateTableStmtTest { ColumnDef hll = new ColumnDef("col3", new TypeDef(ScalarType.createType(PrimitiveType.HLL))); cols.add(hll); - stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.AGG_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", new KeysDesc(KeysType.AGG_KEYS, colsName), + null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); expectedEx.expectMessage("Key column can not set bitmap or hll type:col3"); stmt.analyze(analyzer); @@ -213,20 +215,20 @@ public class CreateTableStmtTest { ColumnDef bitmap = new ColumnDef("col3", new TypeDef(ScalarType.createType(PrimitiveType.BITMAP))); cols.add(bitmap); CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.DUP_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + new KeysDesc(KeysType.DUP_KEYS, colsName), null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); - expectedEx.expectMessage("Aggregate type `col3` bitmap NONE NOT NULL COMMENT \"\" is not compatible with primitive type bitmap"); + expectedEx.expectMessage( + "Aggregate type `col3` bitmap NONE NOT NULL COMMENT \"\" is not compatible with primitive type bitmap"); stmt.analyze(analyzer); cols.remove(bitmap); ColumnDef hll = new ColumnDef("col3", new TypeDef(ScalarType.createType(PrimitiveType.HLL))); cols.add(hll); - stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.DUP_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", new KeysDesc(KeysType.DUP_KEYS, colsName), + null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); - expectedEx.expectMessage("Aggregate type `col3` hll NONE NOT NULL COMMENT \"\" is not compatible with primitive type hll"); + expectedEx.expectMessage( + "Aggregate type `col3` hll NONE NOT NULL COMMENT \"\" is not compatible with primitive type hll"); stmt.analyze(analyzer); } @@ -237,25 +239,25 @@ public class CreateTableStmtTest { cols.add(bitmap); CreateTableStmt stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.AGG_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + new KeysDesc(KeysType.AGG_KEYS, colsName), null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); - expectedEx.expectMessage(String.format("Aggregate type %s is not compatible with primitive type %s", - bitmap.toString(), bitmap.getTypeDef().getType().toSql())); + expectedEx.expectMessage( + String.format("Aggregate type %s is not compatible with primitive type %s", bitmap.toString(), + bitmap.getTypeDef().getType().toSql())); stmt.analyze(analyzer); cols.remove(bitmap); - ColumnDef hll = new ColumnDef("col3", new TypeDef(ScalarType.createType(PrimitiveType.HLL))); + ColumnDef hll = new ColumnDef("col3", new TypeDef(ScalarType.createType(PrimitiveType.HLL))); hll.setAggregateType(AggregateType.SUM); cols.add(hll); - stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", - new KeysDesc(KeysType.AGG_KEYS, colsName), null, - new RandomDistributionDesc(10), null, null, ""); + stmt = new CreateTableStmt(false, false, tblNameNoDb, cols, "olap", new KeysDesc(KeysType.AGG_KEYS, colsName), + null, new RandomDistributionDesc(10), null, null, ""); expectedEx.expect(AnalysisException.class); - expectedEx.expectMessage(String.format("Aggregate type %s is not compatible with primitive type %s", - hll.toString(), hll.getTypeDef().getType().toSql())); + expectedEx.expectMessage( + String.format("Aggregate type %s is not compatible with primitive type %s", hll.toString(), + hll.getTypeDef().getType().toSql())); stmt.analyze(analyzer); } @@ -268,9 +270,7 @@ public class CreateTableStmtTest { CreateTableStmt stmt = new CreateTableStmt(false, true, tblName, "iceberg", properties, ""); stmt.analyze(analyzer); - Assert.assertEquals("CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" - + "\n" - + ") ENGINE = iceberg\n" + Assert.assertEquals("CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" + "\n" + ") ENGINE = iceberg\n" + "PROPERTIES (\"iceberg.database\" = \"doris\",\n" + "\"iceberg.hive.metastore.uris\" = \"thrift://127.0.0.1:9087\",\n" + "\"iceberg.table\" = \"test\")", stmt.toString()); @@ -285,12 +285,11 @@ public class CreateTableStmtTest { CreateTableStmt stmt = new CreateTableStmt(false, true, tblName, "hudi", properties, ""); stmt.analyze(analyzer); - Assert.assertEquals("CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" - + "\n" - + ") ENGINE = hudi\n" - + "PROPERTIES (\"hudi.database\" = \"doris\",\n" - + "\"hudi.hive.metastore.uris\" = \"thrift://127.0.0.1:9087\",\n" - + "\"hudi.table\" = \"test\")", stmt.toString()); + Assert.assertEquals("CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" + "\n" + ") ENGINE = hudi\n" + + "PROPERTIES (\"hudi.database\" = \"doris\",\n" + + "\"hudi.hive.metastore.uris\" = \"thrift://127.0.0.1:9087\",\n" + + "\"hudi.table\" = \"test\")", + stmt.toString()); } @Test @@ -302,17 +301,25 @@ public class CreateTableStmtTest { CreateTableStmt stmt = new CreateTableStmt(false, true, tblName, "hudi", properties, ""); ColumnDef idCol = new ColumnDef("id", TypeDef.create(PrimitiveType.INT)); stmt.addColumnDef(idCol); - ColumnDef nameCol = new ColumnDef("name", TypeDef.create(PrimitiveType.INT), false, - null, true, ColumnDef.DefaultValue.NOT_SET, ""); + ColumnDef nameCol = new ColumnDef("name", TypeDef.create(PrimitiveType.INT), false, null, true, + ColumnDef.DefaultValue.NOT_SET, ""); stmt.addColumnDef(nameCol); stmt.analyze(analyzer); - Assert.assertEquals("CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" - + " `id` int(11) NOT NULL COMMENT \"\",\n" - + " `name` int(11) NULL COMMENT \"\"\n" - + ") ENGINE = hudi\n" - + "PROPERTIES (\"hudi.database\" = \"doris\",\n" - + "\"hudi.hive.metastore.uris\" = \"thrift://127.0.0.1:9087\",\n" - + "\"hudi.table\" = \"test\")", stmt.toString()); + Assert.assertEquals( + "CREATE EXTERNAL TABLE `testCluster:db1`.`table1` (\n" + " `id` int(11) NOT NULL COMMENT \"\",\n" + + " `name` int(11) NULL COMMENT \"\"\n" + ") ENGINE = hudi\n" + + "PROPERTIES (\"hudi.database\" = \"doris\",\n" + + "\"hudi.hive.metastore.uris\" = \"thrift://127.0.0.1:9087\",\n" + + "\"hudi.table\" = \"test\")", stmt.toString()); + } + + @Test + public void testOdbcString() throws AnalysisException { + ColumnDef col = new ColumnDef("string_col", TypeDef.create(PrimitiveType.STRING), true, null, true, + new DefaultValue(false, null), ""); + ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, + "String Type should not be used in key column[string_col].", () -> col.analyze(true)); + col.analyze(false); } }