Skip to content

Commit

Permalink
[fix](createTableStmt)fix bug that createTableStmt toSql (apache#22750)
Browse files Browse the repository at this point in the history
Issue Number: apache#22749
  • Loading branch information
hubgeter authored and airborne12 committed Aug 21, 2023
1 parent f65227c commit fd38add
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ public String toSql() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void analyze(List<ColumnDef> cols) throws AnalysisException {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void testCreateTableDuplicateWithoutKeys() throws UserException {
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",
Expand Down Expand Up @@ -443,4 +443,68 @@ public void testOdbcString() throws AnalysisException {
"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);

}
}

0 comments on commit fd38add

Please sign in to comment.