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

MERGE when not matched should not require aliasing the column names in the VALUES clause #14014

Open
mdesmet opened this issue Sep 6, 2022 · 1 comment
Assignees

Comments

@mdesmet
Copy link
Contributor

mdesmet commented Sep 6, 2022

Currently Trino requires the user to alias the to be inserted values in a MERGE when not matched clause.

merge into target as t
        using source as s
        on t.id = s.id
    when matched then update set
...
    when not matched then insert
        (a, b, c)
    values
        (s.a, s.b, s.c)

This seems counter-intuitive as in the not matched clause there are no matched records, therefore there is no ambiguity.

The following query should work, now failing because of ambiguous column names.

merge into target as t
        using source as s
        on t.id = s.id
    when matched then update set
...
    when not matched then insert
        (a, b, c)
    values
        (a, b, c)

Note that above query works in other DBMS. So it is beneficial to solve this issue in order to reduce migration efforts towards Trino.

@mdesmet mdesmet changed the title `MERGE when not matched should not require aliasing the column names in the VALUES clause MERGE when not matched should not require aliasing the column names in the VALUES clause Sep 6, 2022
@djsstarburst
Copy link
Member

@martint and I spent a long time yesterday looking into this.

We concluded that the predicate in a WHEN NOT MATCHED AND predicate THEN INSERT VALUES (a, b, c) must be evaluated in the join scope as the current SQL MERGE implementation does.

We also found, as @mdesmet reported, that the spec says that the inserted values should be scoped to the source, and table name aliases for source table columns should be optional.

@martint spent some time looking at changing the scoping to satisfy the scoping rules for insert values, and concluded that it would take some re-engineering of the scoping mechanisms, and he would have to dedicate some time to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants