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

fix update <table> set <column> = default #2165

Merged
merged 5 commits into from
Nov 28, 2023
Merged

fix update <table> set <column> = default #2165

merged 5 commits into from
Nov 28, 2023

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Nov 27, 2023

This PR fixes a bug where attempting to update a column to its default would throw an unresolved error.

Copy link
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

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

Mostly good, but the way this is implemented created a few more questions for me than it answered. DML node resolving lifecycle isn't very orderly right now

if colIdx >= 0 && tableSch[colIdx].Generated != nil {
err := sql.ErrGeneratedColumnValue.New(tableSch[colIdx].Name, inScope.node.(sql.NameableNode).Name())
b.handleErr(err)
}
// Replace update expressions with default
Copy link
Contributor

Choose a reason for hiding this comment

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

the ordering/nesting here seems weird, can we resolve the default during buildScalar? or separately from this if block? or does it really depend on colName?

Copy link
Contributor Author

@jycor jycor Nov 28, 2023

Choose a reason for hiding this comment

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

I just reused logic from InsertDestination. We seem to treat ast.Default as a placeholder to be replace with whatever is stored in the schema.

BuildScalar would need the resolved schema

I could separate this into its own block, but it would look the same as the block it's already in

@@ -1032,6 +1032,7 @@ func (b *Builder) tableSpecToSchema(inScope, outScope *scope, db sql.Database, t

defaults := make([]ast.Expr, len(tableSpec.Columns))
generated := make([]ast.Expr, len(tableSpec.Columns))
updates := make([]ast.Expr, len(tableSpec.Columns))
Copy link
Contributor

Choose a reason for hiding this comment

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

did we only need this for update default? should we have a separate test for this if there is a non-default query that errors without it?

Copy link
Contributor Author

@jycor jycor Nov 28, 2023

Choose a reason for hiding this comment

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

Oops this was leftover from an attempt to implement OnUpdate functionality; will remove

@@ -220,23 +220,29 @@ func (b *Builder) assignmentExprsToExpressions(inScope *scope, e ast.AssignmentE
startWinCnt = len(inScope.windowFuncs)
}

tableSch := inScope.node.Schema()
tableSch := b.resolveSchemaDefaults(inScope, inScope.node.Schema())
Copy link
Contributor

Choose a reason for hiding this comment

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

doing this here again smells weird. It doesn't look like DML nodes implement TargetSchema, but should they implement something similar and do this sooner? Or do we really not want the node schemas to resolve defaults for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The schema defaults for updates were unresolved, so we definitely need to call this somewhere.
It doesn't seem like Update nodes need TargetSchema, as long as the update expressions are resolved and correct.

@jycor jycor merged commit dcb38e6 into main Nov 28, 2023
7 checks passed
@jycor jycor deleted the james/update-default branch November 28, 2023 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants