Skip to content

HIVE-30227: Iceberg: a V3 materialized view rebuild in insert-delete mode writes two DVs per data file - #6986

Open
kasakrisz wants to merge 2 commits into
apache:masterfrom
kasakrisz:HIVE-30227-master-mv-icebeeg-v3-dv
Open

kasakrisz wants to merge 2 commits into
apache:masterfrom
kasakrisz:HIVE-30227-master-mv-icebeeg-v3-dv

Conversation

@kasakrisz

@kasakrisz kasakrisz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

An incremental rebuild of a materialized view that has aggregates is rewritten into a MERGE-like multi-insert. This PR is a follow-up of #6924 and makes that multi-insert follow the same rules as the rewritten MERGE statement.

  1. One delete branch in insert-delete mode. Insert-delete mode is used when the view has count(*) and a native ACID source table had deletes. Its multi-insert used to have four branches:
    • insert the new version of the updated rows
    • delete the old version of the updated rows
    • insert new rows
    • delete the rows of the groups that became empty (countStar = 0)

The two deletes are now merged into one branch with the predicate

<update condition> OR <empty group condition>. 

The two conditions never match the same row, so each row is still deleted at most once.
2. Get DISTRIBUTE BY columns from the storage handler.
- Iceberg V3 MVs: DISTRIBUTE BY FILE__PATH.
- Iceberg V1/V2 MVs: no DISTRIBUTE BY.
- Native ACID MVs: no DISTRIBUTE BY. The deleted rows are already distributed by the bucket id in ROW__ID, as for any ACID DELETE/UPDATE/MERGE.
3. No empty SORT BY. The SORT BY clause is added only when the storage handler returns sort columns. Iceberg V3 views and fanout writers return none.

Why are the changes needed?

See #6924 and HIVE-30112

Does this PR introduce any user-facing change?

No.

How was this patch tested?

mvn test -Dtest.output.overwrite -Dtest=TestIcebergCliDriver -Dqfile=mv_iceberg_orc10.q -pl itests/qtest-iceberg -Pitests
mvn test -Dtest.output.overwrite -Dtest=TestMiniLlapLocalCliDriver -Dqfile=materialized_view_create_rewrite_4.q,materialized_view_create_rewrite_6.q,materialized_view_create_rewrite_6_aggr_2joins.q,materialized_view_create_rewrite_6_aggr_3joins.q,materialized_view_create_rewrite_9.q -pl itests/qtest -Pitests

@kasakrisz
kasakrisz marked this pull request as draft November 25, 2026 14:59
@kasakrisz
kasakrisz force-pushed the HIVE-30227-master-mv-icebeeg-v3-dv branch from 7aae0bc to dd54192 Compare November 26, 2026 07:28
@kasakrisz
kasakrisz marked this pull request as ready for review November 26, 2026 07:41
Comment on lines 85 to 88
@@ -90,14 +87,28 @@
* WHEN source.s IS NULL THEN mv.s
* ELSE mv.s + source.s END countStar,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the PR: shouldn't this block for the countStar say '.c' instead of '.s'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I updated the comment.

Comment on lines 81 to 82
* CASE WHEN mv.s IS NULL AND source.s IS NULL THEN NULL &lt;- (use case expression to handle nulls from both sides)
* WHEN mv.s IS NULL THEN source.s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the PR: Couldn't we simplify this to CASE WHEN mv.s IS NULL THEN source.s? I haven't found yet whether that's just an explanatory comment, or whether (and where) the CASE statement is actually generated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is simplified

// Note: If both are null, we will fall into branch WHEN leftNull THEN rightRef
RexNode leftNull = rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, leftRef);
RexNode rightNull = rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, rightRef);
RexNode caseExpression = rexBuilder.makeCall(SqlStdOperatorTable.CASE,
leftNull, rightRef,
rightNull, leftRef,
elseReturn);

@kasakrisz
kasakrisz force-pushed the HIVE-30227-master-mv-icebeeg-v3-dv branch from dd54192 to a0bd8a4 Compare November 30, 2026 08:14
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants