Skip to content

Commit

Permalink
[fix](mtmv) Fix partition trace wrong when partition name is same fro…
Browse files Browse the repository at this point in the history
…m both side of join (#40485)

This is brought by #35562
if partition mv def is as following:

        CREATE MATERIALIZED VIEW mv1
        BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
        PARTITION BY (upgrade_day)
        DISTRIBUTED BY RANDOM BUCKETS 2
        PROPERTIES ('replication_num' = '1') 
        AS
       select t1.upgrade_day, t2.batch_no, count(*) 
       from test2 t2 join test1 t1 on
       t1.upgrade_day = t2.upgrade_day
       group by t1.upgrade_day, t2.batch_no;

the mv related partition table should `test1`, but now is `test2`, this pr fix this.
  • Loading branch information
seawinde authored Sep 13, 2024
1 parent 3af0943 commit 18a374f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,20 @@ public Void visitLogicalRelation(LogicalRelation relation, IncrementCheckerConte
+ "but now is %s", relation.getClass().getSimpleName()));
return null;
}
SlotReference contextPartitionColumn = getContextPartitionColumn(context);
if (contextPartitionColumn == null) {
context.addFailReason(String.format("mv partition column is not from table when relation check, "
+ "mv partition column is %s", context.getMvPartitionColumn()));
return null;
}
// Check the table which mv partition column belonged to is same as the current check relation or not
if (!((LogicalCatalogRelation) relation).getTable().getFullQualifiers().equals(
contextPartitionColumn.getTable().map(TableIf::getFullQualifiers).orElse(ImmutableList.of()))) {
context.addFailReason(String.format("mv partition column name is not belonged to current check , "
+ "table, current table is %s",
((LogicalCatalogRelation) relation).getTable().getFullQualifiers()));
return null;
}
LogicalCatalogRelation logicalCatalogRelation = (LogicalCatalogRelation) relation;
TableIf table = logicalCatalogRelation.getTable();
// if self join, self join can not partition track now, remove the partition column correspondingly
Expand Down Expand Up @@ -457,10 +471,6 @@ public Void visitLogicalRelation(LogicalRelation relation, IncrementCheckerConte
return null;
}
Set<Column> partitionColumnSet = new HashSet<>(relatedTable.getPartitionColumns());
SlotReference contextPartitionColumn = getContextPartitionColumn(context);
if (contextPartitionColumn == null) {
return null;
}
Column mvReferenceColumn = contextPartitionColumn.getColumn().get();
Expr definExpr = mvReferenceColumn.getDefineExpr();
if (definExpr instanceof SlotRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ private void analyzeExpressions(Plan plan, Map<String, String> mvProperties) {
List<Expression> functionCollectResult = MaterializedViewUtils.extractNondeterministicFunction(plan);
if (!CollectionUtils.isEmpty(functionCollectResult)) {
throw new AnalysisException(String.format(
"can not contain invalid expression, the expression is %s",
"can not contain nonDeterministic expression, the expression is %s. "
+ "Should add 'enable_nondeterministic_function' = 'true' property "
+ "when create materialized view if you know the property real meaning entirely",
functionCollectResult.stream().map(Expression::toString).collect(Collectors.joining(","))));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,6 @@ public void computeUniform(DataTrait.Builder builder) {

@Override
public void computeEqualSet(DataTrait.Builder builder) {
if (getTable() instanceof MTMV && getTable().getName().equals("mv1")) {
System.out.println();
}
if (getTable() instanceof MTMV) {
MTMV mtmv = (MTMV) getTable();
MTMVCache cache = mtmv.getCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,26 @@ protected void runBeforeAll() throws Exception {
connectContext.getSessionVariable().setDisableNereidsRules("OLAP_SCAN_PARTITION_PRUNE,PRUNE_EMPTY_PARTITION");
}

// Test when join both side are all partition table and partition column name is same
@Test
public void joinPartitionNameSameTest() {
PlanChecker.from(connectContext)
.checkExplain("select t1.upgrade_day, t2.batch_no, count(*) "
+ "from test2 t2 join test1 t1 on "
+ "t1.upgrade_day = t2.upgrade_day "
+ "group by t1.upgrade_day, t2.batch_no;",
nereidsPlanner -> {
Plan rewrittenPlan = nereidsPlanner.getRewrittenPlan();
RelatedTableInfo relatedTableInfo =
MaterializedViewUtils.getRelatedTableInfo("upgrade_day", null,
rewrittenPlan, nereidsPlanner.getCascadesContext());
checkRelatedTableInfo(relatedTableInfo,
"test1",
"upgrade_day",
true);
});
}

@Test
public void getRelatedTableInfoWhenAutoPartitionTest() {
PlanChecker.from(connectContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ suite("test_enable_date_non_deterministic_function_mtmv","mtmv") {
Assert.fail();
} catch (Exception e) {
logger.info(e.getMessage())
assertTrue(e.getMessage().contains("can not contain invalid expression"));
assertTrue(e.getMessage().contains("can not contain nonDeterministic expression"));
}
sql """drop materialized view if exists ${mvName};"""

Expand All @@ -75,7 +75,7 @@ suite("test_enable_date_non_deterministic_function_mtmv","mtmv") {
Assert.fail();
} catch (Exception e) {
logger.info(e.getMessage())
assertTrue(e.getMessage().contains("can not contain invalid expression"));
assertTrue(e.getMessage().contains("can not contain nonDeterministic expression"));
}
sql """drop materialized view if exists ${mvName};"""

Expand Down Expand Up @@ -128,7 +128,7 @@ suite("test_enable_date_non_deterministic_function_mtmv","mtmv") {
Assert.fail();
} catch (Exception e) {
logger.info(e.getMessage())
assertTrue(e.getMessage().contains("can not contain invalid expression"));
assertTrue(e.getMessage().contains("can not contain nonDeterministic expression"));
}

sql """drop table if exists `${tableName}`"""
Expand Down

0 comments on commit 18a374f

Please sign in to comment.