1414
1515package com .starrocks .planner ;
1616
17+ import com .google .common .collect .Lists ;
18+ import com .starrocks .catalog .Column ;
1719import com .starrocks .catalog .Database ;
20+ import com .starrocks .catalog .DistributionInfo ;
21+ import com .starrocks .catalog .HashDistributionInfo ;
1822import com .starrocks .catalog .OlapTable ;
1923import com .starrocks .catalog .Partition ;
2024import com .starrocks .catalog .PartitionInfo ;
2630import com .starrocks .thrift .TOlapTablePartition ;
2731import com .starrocks .thrift .TOlapTablePartitionParam ;
2832import com .starrocks .thrift .TWriteQuorumType ;
33+ import com .starrocks .type .IntegerType ;
2934import com .starrocks .utframe .StarRocksAssert ;
3035import com .starrocks .utframe .UtFrameUtils ;
3136import mockit .Mock ;
3439import org .junit .jupiter .api .BeforeAll ;
3540import org .junit .jupiter .api .Test ;
3641
42+ import java .util .List ;
43+ import java .util .stream .Collectors ;
44+
3745public class OlapTableSinkTest2 {
3846 private static StarRocksAssert starRocksAssert ;
3947 private static ConnectContext connectContext ;
@@ -47,6 +55,22 @@ public static void beforeClass() throws Exception {
4755 starRocksAssert = new StarRocksAssert (connectContext );
4856 starRocksAssert .withDatabase ("db2" );
4957 starRocksAssert .withTable (createTblStmtStr );
58+
59+ String createRangeTableStmt = "create table db2.tbl_range(k1 int, k2 int, v1 int) " +
60+ "DUPLICATE KEY(k1) " +
61+ "PARTITION BY RANGE(k1) (" +
62+ " PARTITION p1 VALUES LESS THAN ('10')," +
63+ " PARTITION p2 VALUES LESS THAN ('20')" +
64+ ") DISTRIBUTED BY HASH(k1) BUCKETS 3 PROPERTIES('replication_num' = '1');" ;
65+ starRocksAssert .withTable (createRangeTableStmt );
66+
67+ String createListTableStmt = "create table db2.tbl_list(k1 int, k2 int, v1 int) " +
68+ "DUPLICATE KEY(k1) " +
69+ "PARTITION BY LIST(k1) (" +
70+ " PARTITION p1 VALUES IN ('1', '2', '3')," +
71+ " PARTITION p2 VALUES IN ('4', '5', '6')" +
72+ ") DISTRIBUTED BY HASH(k1) BUCKETS 3 PROPERTIES('replication_num' = '1');" ;
73+ starRocksAssert .withTable (createListTableStmt );
5074 }
5175
5276 @ Test
@@ -79,4 +103,69 @@ public int getQuorumNum(long partitionId, TWriteQuorumType writeQuorum) {
79103 }
80104 Assertions .fail ("must throw UserException" );
81105 }
106+
107+ @ Test
108+ public void testRangePartitionWithDifferentDistributionColumns () {
109+ Database db = GlobalStateMgr .getCurrentState ().getLocalMetastore ().getDb ("db2" );
110+ OlapTable olapTable = (OlapTable ) GlobalStateMgr .getCurrentState ().getLocalMetastore ()
111+ .getTable (db .getFullName (), "tbl_range" );
112+
113+ // Get two partitions
114+ Partition p1 = olapTable .getPartition ("p1" );
115+ Partition p2 = olapTable .getPartition ("p2" );
116+ Assertions .assertNotNull (p1 );
117+ Assertions .assertNotNull (p2 );
118+
119+ // Save original distribution info for p2 to restore later
120+ DistributionInfo originalDistInfo = p2 .getDistributionInfo ();
121+
122+ try {
123+ // Change p2's distribution columns to k2 (different from p1's k1)
124+ HashDistributionInfo differentDistInfo = new HashDistributionInfo (
125+ 3 , Lists .newArrayList (new Column ("k2" , IntegerType .INT )));
126+ p2 .setDistributionInfo (differentDistInfo );
127+
128+ List <Long > partitionIds = olapTable .getPartitions ().stream ()
129+ .map (Partition ::getId ).collect (Collectors .toList ());
130+
131+ StarRocksException exception = Assertions .assertThrows (StarRocksException .class , () -> {
132+ OlapTableSink .createPartition (db .getId (), olapTable , null ,
133+ false , 0 , partitionIds , null );
134+ });
135+ Assertions .assertTrue (exception .getMessage ().contains ("different distribute columns" ));
136+ } finally {
137+ p2 .setDistributionInfo (originalDistInfo );
138+ }
139+ }
140+
141+ @ Test
142+ public void testListPartitionWithDifferentDistributionColumns () {
143+ Database db = GlobalStateMgr .getCurrentState ().getLocalMetastore ().getDb ("db2" );
144+ OlapTable olapTable = (OlapTable ) GlobalStateMgr .getCurrentState ().getLocalMetastore ()
145+ .getTable (db .getFullName (), "tbl_list" );
146+
147+ Partition p1 = olapTable .getPartition ("p1" );
148+ Partition p2 = olapTable .getPartition ("p2" );
149+ Assertions .assertNotNull (p1 );
150+ Assertions .assertNotNull (p2 );
151+
152+ DistributionInfo originalDistInfo = p2 .getDistributionInfo ();
153+
154+ try {
155+ HashDistributionInfo differentDistInfo = new HashDistributionInfo (
156+ 3 , Lists .newArrayList (new Column ("k2" , IntegerType .INT )));
157+ p2 .setDistributionInfo (differentDistInfo );
158+
159+ List <Long > partitionIds = olapTable .getPartitions ().stream ()
160+ .map (Partition ::getId ).collect (Collectors .toList ());
161+
162+ StarRocksException exception = Assertions .assertThrows (StarRocksException .class , () -> {
163+ OlapTableSink .createPartition (db .getId (), olapTable , null ,
164+ false , 0 , partitionIds , null );
165+ });
166+ Assertions .assertTrue (exception .getMessage ().contains ("different distribute columns" ));
167+ } finally {
168+ p2 .setDistributionInfo (originalDistInfo );
169+ }
170+ }
82171}
0 commit comments