-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
Implement row alias expressions (INSERT ... VALUES (...) AS new_tbl ON DUPLICATE x = new_tbl.x
)
#2534
Conversation
Additional work is required for integration with Dolt. |
c167e84
to
2ce30b5
Compare
…e inserted rows), add that name to the scope instead of the default "__new_ins"
6d4cfc1
to
88f20f3
Compare
Fixes dolthub/dolt#7638 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I don't love making ON DUP EXPRS more complicated, but there aren't a lot of faster practical alternatives. Like most of the complexity around INSERTs, we should probably do something that reorders/normalizes insert value ordering to avoid extra complexity in the rest of analysis.
sql/planbuilder/builder.go
Outdated
insertTableAlias string | ||
insertColumnAliases map[string]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we limit these variables to a single scope? the rest of these objects explicitly outlive a single query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Limited it to two scopes (insert scope and combined scope)
sql/planbuilder/scalar.go
Outdated
v.Name.Qualifier.Name = ast.NewTableIdent(OnDupValuesPrefix) | ||
v.Name.Qualifier.Name = ast.NewTableIdent(b.insertTableAlias) | ||
if len(b.insertColumnAliases) > 0 { | ||
v.Name.Name = ast.NewColIdent(b.insertColumnAliases[v.Name.Name.String()]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strings to lower?
sql/planbuilder/dml.go
Outdated
// The to-be-inserted values can be referenced via the provided alias. | ||
c.table = b.insertTableAlias | ||
if len(b.insertColumnAliases) > 0 { | ||
aliasColumnName := b.insertColumnAliases[c.col] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strings to lower?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lowered the string on insert into the map.
sql/planbuilder/dml.go
Outdated
b.insertColumnAliases = make(map[string]string) | ||
for i, destColumn := range columns { | ||
sourceColumn := aliasedValues.Columns[i].String() | ||
b.insertColumnAliases[destColumn] = sourceColumn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Additional work is required for integration with Dolt. |
1 similar comment
Additional work is required for integration with Dolt. |
When inserting values, the user can specify names for both the source table and columns which are used in
ON DUPLICATE
expressions. It looks like either of the below options:This replaces the previous (now-deprecated) syntax:
Supporting both syntaxes together was non-trivial because it means there's now two different ways to refer to the same column. While he had an existing way to "redirect" one column name to another, this only worked for unqualified names (no table name), and it overrode the normal name resolution rules, which meant we would fail to detect cases that should be seen as ambiguous.
Previously, we would implement references to the inserted values by using a special table named "__new_ins". I implemented this by keeping that as the default, but using the row alias instead of one was provided. We then create a map from the destination table names to column aliases, and use that map to rewrite expressions that appear inside the VALUES() function.