Skip to content

Commit 005273f

Browse files
authored
fix(planner): Fix materialized view planning when data table has hidden columns (#26545)
## Description This PR fixes a bug when using the planning-oriented materialized view framework (introduced in PR #26492) in Hive connector. Unlike the tables in the Memory connector, the underlying hive tables of the materialized view contain hidden columns. Therefore, for the comparison with the number of output variables in the view query, we should use the underlying data table's visible field count instead of its total field count. ## Motivation and Context - Fix a bug when using the planning-oriented materialized view framework in Hive connector ## Impact N/A ## Test Plans - New test case in `TestHiveMaterializedViewLogicalPlanner` which would fail without this change ## Contributor checklist - [ ] Please make sure your submission complies with our [contributing guide](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md), in particular [code style](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#code-style) and [commit standards](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#commit-standards). - [ ] PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced. - [ ] Documented new properties (with its default value), SQL syntax, functions, or other functionality. - [ ] If release notes are required, they follow the [release notes guidelines](https://github.com/prestodb/presto/wiki/Release-Notes-Guidelines). - [ ] Adequate tests were added if applicable. - [ ] CI passed. - [ ] If adding new dependencies, verified they have an [OpenSSF Scorecard](https://securityscorecards.dev/#the-checks) score of 5.0 or higher (or obtained explicit TSC approval for lower scores). ## Release Notes ``` == NO RELEASE NOTE == ```
1 parent 02073ea commit 005273f

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

presto-hive/src/test/java/com/facebook/presto/hive/TestHiveMaterializedViewLogicalPlanner.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.node;
7373
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project;
7474
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.singleGroupingSet;
75+
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan;
7576
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.unnest;
7677
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values;
7778
import static com.facebook.presto.testing.TestingAccessControlManager.TestingPrivilegeType.INSERT_TABLE;
@@ -2921,6 +2922,46 @@ public void testAutoRefreshMaterializedViewAfterInsertion()
29212922
}
29222923
}
29232924

2925+
public void testMaterializedViewNotRefreshedInNonLegacyMode()
2926+
{
2927+
Session nonLegacySession = Session.builder(getSession())
2928+
.setSystemProperty("legacy_materialized_views", "false")
2929+
.build();
2930+
try {
2931+
assertUpdate("CREATE TABLE base_table (id BIGINT, name VARCHAR, part_key BIGINT) WITH (partitioned_by = ARRAY['part_key'])");
2932+
assertUpdate("INSERT INTO base_table VALUES (1, 'Alice', 100), (2, 'Bob', 200)", 2);
2933+
assertUpdate("CREATE MATERIALIZED VIEW simple_mv WITH (partitioned_by = ARRAY['part_key']) AS SELECT id, name, part_key FROM base_table");
2934+
2935+
assertPlan(nonLegacySession, "SELECT * FROM simple_mv",
2936+
anyTree(tableScan("base_table")));
2937+
}
2938+
finally {
2939+
assertUpdate("DROP TABLE base_table");
2940+
assertUpdate("DROP MATERIALIZED VIEW simple_mv");
2941+
}
2942+
}
2943+
2944+
@Test
2945+
public void testMaterializedViewRefreshedInNonLegacyMode()
2946+
{
2947+
Session nonLegacySession = Session.builder(getSession())
2948+
.setSystemProperty("legacy_materialized_views", "false")
2949+
.build();
2950+
try {
2951+
assertUpdate("CREATE TABLE base_table (id BIGINT, name VARCHAR, part_key BIGINT) WITH (partitioned_by = ARRAY['part_key'])");
2952+
assertUpdate("INSERT INTO base_table VALUES (1, 'Alice', 100), (2, 'Bob', 200)", 2);
2953+
assertUpdate("CREATE MATERIALIZED VIEW simple_mv WITH (partitioned_by = ARRAY['part_key']) AS SELECT id, name, part_key FROM base_table");
2954+
assertUpdate("REFRESH MATERIALIZED VIEW simple_mv where part_key > 0", 2);
2955+
2956+
assertPlan(nonLegacySession, "SELECT * FROM simple_mv",
2957+
anyTree(tableScan("simple_mv")));
2958+
}
2959+
finally {
2960+
assertUpdate("DROP TABLE base_table");
2961+
assertUpdate("DROP MATERIALIZED VIEW simple_mv");
2962+
}
2963+
}
2964+
29242965
private void setReferencedMaterializedViews(DistributedQueryRunner queryRunner, String tableName, List<String> referencedMaterializedViews)
29252966
{
29262967
appendTableParameter(replicateHiveMetastore(queryRunner),

presto-main-base/src/main/java/com/facebook/presto/sql/planner/RelationPlanner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ private RelationPlan planMaterializedView(Table node, Analysis.MaterializedViewI
325325
List<VariableReferenceExpression> viewQueryVariables = viewQueryPlan.getFieldMappings();
326326

327327
checkArgument(
328-
dataTableVariables.size() == viewQueryVariables.size(),
329-
"Materialized view %s has mismatched field counts: data table has %s fields but view query has %s fields",
328+
dataTableDescriptor.getVisibleFieldCount() == viewQueryVariables.size(),
329+
"Materialized view %s has mismatched field counts: data table has %s visible fields but view query has %s fields",
330330
materializedViewName,
331-
dataTableVariables.size(),
331+
dataTableDescriptor.getVisibleFieldCount(),
332332
viewQueryVariables.size());
333333

334334
ImmutableList.Builder<VariableReferenceExpression> outputVariablesBuilder = ImmutableList.builder();

0 commit comments

Comments
 (0)