diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java index c182377557..b8755639fb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java @@ -201,6 +201,10 @@ public class Column implements Writable { } public void setIsKey(boolean isKey) { + // column is key, aggregationType is always null, isAggregationTypeImplicit is always false. + if (isKey) { + setAggregationType(null, false); + } this.isKey = isKey; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/alter/RollupJobV2Test.java b/fe/fe-core/src/test/java/org/apache/doris/alter/RollupJobV2Test.java index 8a6f8f5d56..81db108576 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/alter/RollupJobV2Test.java +++ b/fe/fe-core/src/test/java/org/apache/doris/alter/RollupJobV2Test.java @@ -426,4 +426,24 @@ public class RollupJobV2Test { Assert.assertEquals("to_bitmap", resultFunctionCall.getFnName().getFunction()); } + + @Test + public void testAddRollupForDupTable() throws UserException { + fakeCatalog = new FakeCatalog(); + fakeEditLog = new FakeEditLog(); + FakeCatalog.setCatalog(masterCatalog); + MaterializedViewHandler materializedViewHandler = Catalog.getCurrentCatalog().getRollupHandler(); + Database db = masterCatalog.getDbOrDdlException(CatalogTestUtil.testDbId1); + OlapTable olapTable = (OlapTable) db.getTableOrDdlException(CatalogTestUtil.testTableId2); + + AddRollupClause addRollupClause = new AddRollupClause("r1", Lists.newArrayList("k1", "v1", "v2"), null, CatalogTestUtil.testIndex2, null); + + List columns = materializedViewHandler.checkAndPrepareMaterializedView(addRollupClause, olapTable, CatalogTestUtil.testIndexId2, false); + for (Column column : columns) { + if (column.nameEquals("v1", true)) { + Assert.assertNull(column.getAggregationType()); + break; + } + } + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/catalog/CatalogTestUtil.java b/fe/fe-core/src/test/java/org/apache/doris/catalog/CatalogTestUtil.java index b540ec68ab..93eaa190c6 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/catalog/CatalogTestUtil.java +++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/CatalogTestUtil.java @@ -47,12 +47,23 @@ public class CatalogTestUtil { public static String testDb1 = "testDb1"; public static long testDbId1 = 1; public static String testTable1 = "testTable1"; + public static String testTable2 = "testTable2"; + public static long testTableId1 = 2; + public static long testTableId2 = 15; + public static String testPartition1 = "testPartition1"; + public static String testPartition2 = "testPartition2"; + public static long testPartitionId1 = 3; + public static long testPartitionId2 = 16; + public static String testIndex1 = "testIndex1"; public static String testIndex2 = "testIndex2"; + public static long testIndexId1 = 2; // the base indexId == tableId + public static long testIndexId2 = 15; // the base indexId == tableId + public static int testSchemaHash1 = 93423942; public static long testBackendId1 = 5; public static long testBackendId2 = 6; @@ -60,7 +71,11 @@ public class CatalogTestUtil { public static long testReplicaId1 = 8; public static long testReplicaId2 = 9; public static long testReplicaId3 = 10; + public static long testReplicaId4 = 17; + public static long testTabletId1 = 11; + public static long testTabletId2 = 18; + public static long testStartVersion = 12; public static long testStartVersionHash = 12312; public static long testPartitionCurrentVersionHash = 12312; @@ -230,13 +245,62 @@ public class CatalogTestUtil { // add a es table to catalog try { createEsTable(db); + createDupTable(db); } catch (DdlException e) { // TODO Auto-generated catch block // e.printStackTrace(); } return db; } - + + public static void createDupTable(Database db) { + + // replica + Replica replica = new Replica(testReplicaId4, testBackendId1, testStartVersion, testStartVersionHash, 0, 0L, 0L, + ReplicaState.NORMAL, -1, 0, 0, 0); + + // tablet + Tablet tablet = new Tablet(testTabletId2); + + // index + MaterializedIndex index = new MaterializedIndex(testIndexId2, IndexState.NORMAL); + TabletMeta tabletMeta = new TabletMeta(testDbId1, testTableId2, testPartitionId2, testIndexId2, 0, TStorageMedium.HDD); + index.addTablet(tablet, tabletMeta); + + tablet.addReplica(replica); + + // partition + RandomDistributionInfo distributionInfo = new RandomDistributionInfo(1); + Partition partition = new Partition(testPartitionId2, testPartition2, index, distributionInfo); + partition.updateVisibleVersionAndVersionHash(testStartVersion, testStartVersionHash); + partition.setNextVersion(testStartVersion + 1); + partition.setNextVersionHash(testPartitionNextVersionHash, testPartitionCurrentVersionHash); + + // columns + List columns = new ArrayList(); + Column temp = new Column("k1", PrimitiveType.INT); + temp.setIsKey(true); + columns.add(temp); + temp = new Column("k2", PrimitiveType.INT); + temp.setIsKey(true); + columns.add(temp); + columns.add(new Column("v1", ScalarType.createType(PrimitiveType.VARCHAR), false, AggregateType.NONE, "0", "")); + columns.add(new Column("v2", ScalarType.createType(PrimitiveType.VARCHAR), false, AggregateType.NONE, "0", "")); + + // table + PartitionInfo partitionInfo = new SinglePartitionInfo(); + partitionInfo.setDataProperty(testPartitionId2, DataProperty.DEFAULT_DATA_PROPERTY); + partitionInfo.setReplicaAllocation(testPartitionId2, new ReplicaAllocation((short) 1)); + OlapTable table = new OlapTable(testTableId2, testTable2, columns, KeysType.DUP_KEYS, partitionInfo, + distributionInfo); + table.addPartition(partition); + table.setIndexMeta(testIndexId2, testIndex2, columns, 0, testSchemaHash1, (short) 3, + TStorageType.COLUMN, KeysType.DUP_KEYS); + table.setBaseIndexId(testIndexId2); + // db + db.createTable(table); + } + public static void createEsTable(Database db) throws DdlException { // columns List columns = new ArrayList<>();