Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify planning of MERGE #23684

Merged
merged 1 commit into from
Oct 23, 2024
Merged

Simplify planning of MERGE #23684

merged 1 commit into from
Oct 23, 2024

Conversation

martint
Copy link
Member

@martint martint commented Oct 4, 2024

Currently, the planner inserts a projection of the following shape to assemble the merged row:

merge_row := (
    CASE
        WHEN ... THEN
            ROW(..., $not((present IS NULL)), <operation>, 0)
        WHEN ... THEN
            ROW(..., $not((present IS NULL)), <operation>, 1)
        ...
        ELSE
            ROW(<nulls>, $not((present IS NULL)), -1, -1)
    END)

This change replaces the ELSE branch to return a single null instead of a synthetic value with nulls. By reducing the size of the projection, it allows for wider tables to be used with MERGE.

Related to #15848

Release notes

(x) No release notes needed

@cla-bot cla-bot bot added the cla-signed label Oct 4, 2024
Copy link
Member

@djsstarburst djsstarburst left a comment

Choose a reason for hiding this comment

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

This looked good to me and it looks like all the CI tests pass. I wondered why the first test in TestDeleteAndInsertMergeProcessor was removed.

@dougb
Copy link

dougb commented Oct 8, 2024

Hi, I tried this PR and it seems to help with one of the tables I was having trouble MERGING, thank you!
Will do some more testing tomorrow.

Copy link
Member

@hashhar hashhar left a comment

Choose a reason for hiding this comment

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

Looks good, I only have questions for my own understanding.

We used to use the synthetic value as a "marker" to signal what operation to do.
Now we rely on the fact that the table scan will not produce a tuple with the value CAST(null AS row(columns..., c1 boolean, c2 tinyint, c3 integer)) ever? Why is this assumption true? Is it true because there will never be a column in the table whose type matches the row we have created?

Or do I misunderstand something?

whenClauses.build(),
new Constant(
RowType.anonymous(ImmutableList.<Type>builder()
.addAll(dataColumnSchemas.stream().map(ColumnSchema::getType).collect(Collectors.toList()))
Copy link
Member

Choose a reason for hiding this comment

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

nit: static import toList

@martint
Copy link
Member Author

martint commented Oct 9, 2024

The columns from the table scan are wrapped in a ROW(...) with some additional fields by a synthetic projection in the plan that handles the branches of the MERGE statement. The row is then consumed by the merge processor to decide if a row is being inserted or deleted. In one of the cases, we were producing a row that was meant to be ignored by the merge processor (i.e., when none of the MERGE branches matched).

Currently, the planner inserts a projection of the following shape
to assemble the merged row:

    merge_row := (
        CASE
            WHEN ... THEN
                ROW(..., $not((present IS NULL)), <operation>, 0)
            WHEN ... THEN
                ROW(..., $not((present IS NULL)), <operation>, 1)
            ...
            ELSE
                ROW(<nulls>, $not((present IS NULL)), -1, -1)
        END)

This change replaces the ELSE branch to return a single null instead of a synthetic
value with nulls. By reducing the size of the projection, it allows for wider
tables to be used with MERGE.
@martint martint merged commit 8820f5a into trinodb:master Oct 23, 2024
93 checks passed
@martint martint deleted the merge-simplify branch October 23, 2024 14:02
@github-actions github-actions bot added this to the 463 milestone Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants