Skip to content

Commit

Permalink
branch-3.0: [Fix](DDL) Fix wrong show create table stmt for auto list…
Browse files Browse the repository at this point in the history
… multi-columns partition #43967 (#44005)

Cherry-picked from #43967

Co-authored-by: zclllhhjj <[email protected]>
  • Loading branch information
github-actions[bot] and zclllyybb authored Nov 16, 2024
1 parent d44c236 commit 6fb32f5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.doris.analysis.PartitionKeyDesc;
import org.apache.doris.analysis.PartitionValue;
import org.apache.doris.analysis.SinglePartitionDesc;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.FeMetaVersion;
Expand Down Expand Up @@ -181,31 +180,19 @@ public static void checkPartitionColumn(Column column) throws AnalysisException
@Override
public String toSql(OlapTable table, List<Long> partitionId) {
StringBuilder sb = new StringBuilder();
int idx = 0;
if (enableAutomaticPartition()) {
sb.append("AUTO PARTITION BY LIST ");
for (Expr e : partitionExprs) {
boolean isSlotRef = (e instanceof SlotRef);
if (isSlotRef) {
sb.append("(");
}
sb.append(e.toSql());
if (isSlotRef) {
sb.append(")");
}
}
sb.append("\n(");
} else {
sb.append("PARTITION BY LIST(");
for (Column column : partitionColumns) {
if (idx != 0) {
sb.append(", ");
}
sb.append("`").append(column.getName()).append("`");
idx++;
sb.append("AUTO ");
}
sb.append("PARTITION BY LIST (");
int idx = 0;
for (Column column : partitionColumns) {
if (idx != 0) {
sb.append(", ");
}
sb.append(")\n(");
sb.append("`").append(column.getName()).append("`");
idx++;
}
sb.append(")\n(");

// sort list
List<Map.Entry<Long, PartitionItem>> entries = new ArrayList<>(this.idToItem.entrySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void testPartition() throws Exception {
+ " `username` varchar(255) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
+ "PARTITION BY LIST(`userId`)\n"
+ "PARTITION BY LIST (`userId`)\n"
+ "(PARTITION p1 VALUES IN (\"CA\",\"GB\",\"US\",\"ZH\"))\n"
+ "DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n"
+ "PROPERTIES (\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package org.apache.doris.catalog;

import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.PartitionKeyDesc;
import org.apache.doris.analysis.PartitionValue;
import org.apache.doris.analysis.SinglePartitionDesc;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.TableName;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;

Expand Down Expand Up @@ -223,4 +226,26 @@ public void testMultiPartitionKeys() throws AnalysisException, DdlException {
Assert.assertEquals("beijing", ((ListPartitionItem) partitionItem).getItems().get(0).getKeys().get(0).getRealValue());
Assert.assertEquals(100, ((ListPartitionItem) partitionItem).getItems().get(0).getKeys().get(1).getLongValue());
}

@Test
public void testMultiAutotoSql() throws AnalysisException, DdlException {
Column k1 = new Column("k1", new ScalarType(PrimitiveType.VARCHAR), true, null, "", "");
Column k2 = new Column("k2", new ScalarType(PrimitiveType.INT), true, null, "", "");
partitionColumns.add(k1);
partitionColumns.add(k2);

ArrayList<Expr> partitionExprs = new ArrayList<>();
SlotRef s1 = new SlotRef(new TableName("tbl"), "k1");
SlotRef s2 = new SlotRef(new TableName("tbl"), "k2");
partitionExprs.add(s1);
partitionExprs.add(s2);

partitionInfo = new ListPartitionInfo(true, partitionExprs, partitionColumns);
OlapTable table = new OlapTable();

String sql = partitionInfo.toSql(table, null);

String expected = "AUTO PARTITION BY LIST (`k1`, `k2`)";
Assert.assertTrue("got: " + sql + ", should have: " + expected, sql.contains(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package org.apache.doris.catalog;

import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.FunctionCallExpr;
import org.apache.doris.analysis.PartitionKeyDesc;
import org.apache.doris.analysis.PartitionKeyDesc.PartitionKeyValueType;
import org.apache.doris.analysis.PartitionValue;
import org.apache.doris.analysis.SinglePartitionDesc;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.analysis.TableName;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.io.Text;
Expand Down Expand Up @@ -474,4 +479,28 @@ public void testSerialization() throws IOException, AnalysisException, DdlExcept
in.close();
Files.delete(path);
}

@Test
public void testAutotoSql() throws AnalysisException, DdlException {
Column k1 = new Column("k1", new ScalarType(PrimitiveType.DATEV2), true, null, "", "");
partitionColumns.add(k1);

ArrayList<Expr> params = new ArrayList<>();
SlotRef s1 = new SlotRef(new TableName("tbl"), "k1");
params.add(s1);
params.add(new StringLiteral("day"));

FunctionCallExpr f1 = new FunctionCallExpr("date_trunc", params);

ArrayList<Expr> partitionExprs = new ArrayList<>();
partitionExprs.add(f1);

partitionInfo = new RangePartitionInfo(true, partitionExprs, partitionColumns);
OlapTable table = new OlapTable();

String sql = partitionInfo.toSql(table, null);

String expected = "AUTO PARTITION BY RANGE (date_trunc(`tbl`.`k1`, 'day'))";
Assert.assertTrue("got: " + sql + ", should have: " + expected, sql.contains(expected));
}
}

0 comments on commit 6fb32f5

Please sign in to comment.