Skip to content

Commit 7d998d1

Browse files
committed
add ut and case
1 parent 4f1353d commit 7d998d1

File tree

5 files changed

+661
-0
lines changed

5 files changed

+661
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands.info;
19+
20+
import org.apache.doris.alter.AlterOpType;
21+
import org.apache.doris.analysis.AlterTableClause;
22+
import org.apache.doris.analysis.DropPartitionFieldClause;
23+
import org.apache.doris.common.UserException;
24+
import org.apache.doris.qe.ConnectContext;
25+
26+
import java.util.Collections;
27+
import java.util.Map;
28+
29+
/**
30+
* DropPartitionFieldOp for Iceberg partition evolution
31+
*/
32+
public class DropPartitionFieldOp extends AlterTableOp {
33+
private final String transformName;
34+
private final Integer transformArg;
35+
private final String columnName;
36+
37+
public DropPartitionFieldOp(String transformName, Integer transformArg, String columnName) {
38+
super(AlterOpType.SCHEMA_CHANGE);
39+
this.transformName = transformName;
40+
this.transformArg = transformArg;
41+
this.columnName = columnName;
42+
}
43+
44+
public String getTransformName() {
45+
return transformName;
46+
}
47+
48+
public Integer getTransformArg() {
49+
return transformArg;
50+
}
51+
52+
public String getColumnName() {
53+
return columnName;
54+
}
55+
56+
@Override
57+
public void validate(ConnectContext ctx) throws UserException {
58+
// Validation will be done in IcebergMetadataOps
59+
}
60+
61+
@Override
62+
public AlterTableClause translateToLegacyAlterClause() {
63+
return new DropPartitionFieldClause(transformName, transformArg, columnName);
64+
}
65+
66+
@Override
67+
public Map<String, String> getProperties() {
68+
return Collections.emptyMap();
69+
}
70+
71+
@Override
72+
public boolean allowOpMTMV() {
73+
return false;
74+
}
75+
76+
@Override
77+
public boolean needChangeMTMVState() {
78+
return false;
79+
}
80+
81+
@Override
82+
public String toSql() {
83+
StringBuilder sb = new StringBuilder();
84+
sb.append("DROP PARTITION FIELD ");
85+
if (transformName != null) {
86+
sb.append(transformName);
87+
if (transformArg != null) {
88+
sb.append("(").append(transformArg);
89+
if (columnName != null) {
90+
sb.append(", ").append(columnName);
91+
}
92+
sb.append(")");
93+
} else if (columnName != null) {
94+
sb.append("(").append(columnName).append(")");
95+
}
96+
} else if (columnName != null) {
97+
sb.append(columnName);
98+
}
99+
return sb.toString();
100+
}
101+
}
102+

fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/AlterTableCommandTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package org.apache.doris.nereids.trees.plans.commands;
1919

2020
import org.apache.doris.info.TableNameInfo;
21+
import org.apache.doris.nereids.trees.plans.commands.info.AddPartitionFieldOp;
2122
import org.apache.doris.nereids.trees.plans.commands.info.AlterTableOp;
23+
import org.apache.doris.nereids.trees.plans.commands.info.DropPartitionFieldOp;
2224
import org.apache.doris.nereids.trees.plans.commands.info.EnableFeatureOp;
2325

2426
import org.junit.jupiter.api.Assertions;
@@ -49,5 +51,52 @@ void testEnableFeatureOp() {
4951
alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
5052
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` ENABLE FEATURE \"SEQUENCE_LOAD\" WITH PROPERTIES (\"function_column.sequence_type\" = \"int\")");
5153
}
54+
55+
@Test
56+
void testAddPartitionFieldOp() {
57+
List<AlterTableOp> ops = new ArrayList<>();
58+
ops.add(new AddPartitionFieldOp("bucket", 16, "id"));
59+
AlterTableCommand alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
60+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` ADD PARTITION FIELD bucket(16, id)");
61+
62+
ops.clear();
63+
ops.add(new AddPartitionFieldOp("year", null, "ts"));
64+
alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
65+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` ADD PARTITION FIELD year(ts)");
66+
67+
ops.clear();
68+
ops.add(new AddPartitionFieldOp(null, null, "category"));
69+
alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
70+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` ADD PARTITION FIELD category");
71+
}
72+
73+
@Test
74+
void testDropPartitionFieldOp() {
75+
List<AlterTableOp> ops = new ArrayList<>();
76+
ops.add(new DropPartitionFieldOp("bucket", 16, "id"));
77+
AlterTableCommand alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
78+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` DROP PARTITION FIELD bucket(16, id)");
79+
80+
ops.clear();
81+
ops.add(new DropPartitionFieldOp("year", null, "ts"));
82+
alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
83+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` DROP PARTITION FIELD year(ts)");
84+
85+
ops.clear();
86+
ops.add(new DropPartitionFieldOp(null, null, "category"));
87+
alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
88+
Assertions.assertEquals(alterTableCommand.toSql(), "ALTER TABLE `db`.`test` DROP PARTITION FIELD category");
89+
}
90+
91+
@Test
92+
void testMultiplePartitionFieldOps() {
93+
List<AlterTableOp> ops = new ArrayList<>();
94+
ops.add(new AddPartitionFieldOp("day", null, "ts"));
95+
ops.add(new AddPartitionFieldOp("bucket", 8, "id"));
96+
AlterTableCommand alterTableCommand = new AlterTableCommand(new TableNameInfo("db", "test"), ops);
97+
String sql = alterTableCommand.toSql();
98+
Assertions.assertTrue(sql.contains("ADD PARTITION FIELD day(ts)"));
99+
Assertions.assertTrue(sql.contains("ADD PARTITION FIELD bucket(8, id)"));
100+
}
52101
}
53102

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands.info;
19+
20+
import org.apache.doris.analysis.AddPartitionFieldClause;
21+
import org.apache.doris.alter.AlterOpType;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.Map;
27+
28+
public class AddPartitionFieldOpTest {
29+
30+
@Test
31+
public void testIdentityTransform() {
32+
AddPartitionFieldOp op = new AddPartitionFieldOp(null, null, "category");
33+
Assertions.assertEquals(AlterOpType.SCHEMA_CHANGE, op.getOpType());
34+
Assertions.assertNull(op.getTransformName());
35+
Assertions.assertNull(op.getTransformArg());
36+
Assertions.assertEquals("category", op.getColumnName());
37+
Assertions.assertEquals("ADD PARTITION FIELD category", op.toSql());
38+
39+
AddPartitionFieldClause clause = (AddPartitionFieldClause) op.translateToLegacyAlterClause();
40+
Assertions.assertNotNull(clause);
41+
Assertions.assertNull(clause.getTransformName());
42+
Assertions.assertNull(clause.getTransformArg());
43+
Assertions.assertEquals("category", clause.getColumnName());
44+
}
45+
46+
@Test
47+
public void testTimeTransform() {
48+
AddPartitionFieldOp op = new AddPartitionFieldOp("year", null, "ts");
49+
Assertions.assertEquals("year", op.getTransformName());
50+
Assertions.assertNull(op.getTransformArg());
51+
Assertions.assertEquals("ts", op.getColumnName());
52+
Assertions.assertEquals("ADD PARTITION FIELD year(ts)", op.toSql());
53+
54+
AddPartitionFieldClause clause = (AddPartitionFieldClause) op.translateToLegacyAlterClause();
55+
Assertions.assertEquals("year", clause.getTransformName());
56+
Assertions.assertNull(clause.getTransformArg());
57+
Assertions.assertEquals("ts", clause.getColumnName());
58+
}
59+
60+
@Test
61+
public void testBucketTransform() {
62+
AddPartitionFieldOp op = new AddPartitionFieldOp("bucket", 16, "id");
63+
Assertions.assertEquals("bucket", op.getTransformName());
64+
Assertions.assertEquals(16, op.getTransformArg());
65+
Assertions.assertEquals("id", op.getColumnName());
66+
Assertions.assertEquals("ADD PARTITION FIELD bucket(16, id)", op.toSql());
67+
68+
AddPartitionFieldClause clause = (AddPartitionFieldClause) op.translateToLegacyAlterClause();
69+
Assertions.assertEquals("bucket", clause.getTransformName());
70+
Assertions.assertEquals(16, clause.getTransformArg());
71+
Assertions.assertEquals("id", clause.getColumnName());
72+
}
73+
74+
@Test
75+
public void testTruncateTransform() {
76+
AddPartitionFieldOp op = new AddPartitionFieldOp("truncate", 10, "name");
77+
Assertions.assertEquals("truncate", op.getTransformName());
78+
Assertions.assertEquals(10, op.getTransformArg());
79+
Assertions.assertEquals("name", op.getColumnName());
80+
Assertions.assertEquals("ADD PARTITION FIELD truncate(10, name)", op.toSql());
81+
}
82+
83+
@Test
84+
public void testMonthTransform() {
85+
AddPartitionFieldOp op = new AddPartitionFieldOp("month", null, "created_date");
86+
Assertions.assertEquals("ADD PARTITION FIELD month(created_date)", op.toSql());
87+
}
88+
89+
@Test
90+
public void testDayTransform() {
91+
AddPartitionFieldOp op = new AddPartitionFieldOp("day", null, "ts");
92+
Assertions.assertEquals("ADD PARTITION FIELD day(ts)", op.toSql());
93+
}
94+
95+
@Test
96+
public void testHourTransform() {
97+
AddPartitionFieldOp op = new AddPartitionFieldOp("hour", null, "ts");
98+
Assertions.assertEquals("ADD PARTITION FIELD hour(ts)", op.toSql());
99+
}
100+
101+
@Test
102+
public void testProperties() {
103+
AddPartitionFieldOp op = new AddPartitionFieldOp("bucket", 32, "id");
104+
Map<String, String> properties = op.getProperties();
105+
Assertions.assertNotNull(properties);
106+
Assertions.assertTrue(properties.isEmpty());
107+
}
108+
109+
@Test
110+
public void testAllowOpMTMV() {
111+
AddPartitionFieldOp op = new AddPartitionFieldOp("bucket", 16, "id");
112+
Assertions.assertFalse(op.allowOpMTMV());
113+
}
114+
115+
@Test
116+
public void testNeedChangeMTMVState() {
117+
AddPartitionFieldOp op = new AddPartitionFieldOp("bucket", 16, "id");
118+
Assertions.assertFalse(op.needChangeMTMVState());
119+
}
120+
}
121+

0 commit comments

Comments
 (0)