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

error msg for invalid reference to non-existent table or column in existing view #2038

Merged
merged 4 commits into from
Sep 27, 2023

Conversation

jennifersp
Copy link
Contributor

@jennifersp jennifersp commented Sep 27, 2023

It catches invalid reference to non-existent table or column in existing view. This includes SELECT queries on a view that references table or column that was removed or renamed.

Note: For now, It does not catch references to invalid functions or users without appropriate privilege cases and queries other than SELECT queries.

Fixes: dolthub/dolt#6691

@jennifersp jennifersp requested a review from fulghum September 27, 2023 16:38
Copy link
Contributor

@fulghum fulghum left a comment

Choose a reason for hiding this comment

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

Looks very close! That string prefix match feels like something we could tighten up a bit more, but everything else looks good.

Comment on lines +3772 to +3774
Skip: true,
Query: "SELECT * FROM b;",
ExpectedErr: sql.ErrInvalidRefInView,
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting! I just tried this on MySQL and was surprised that it generated the "view reference invalid..." error. I expected MySQL would just remember the *, but it totally does expand * to the columns in the table at the time when the view is created. As a direct result of that... if you alter the base table and add more columns, those new columns won't show up when you query the view.

// TODO: Need better way to determine the view usage here.
// Creating new view or updating existing view to reference non-existent table/view do not apply here.
// Need to account for non-existing functions or users without appropriate privilege to the referenced table/column/function.
if (sql.ErrTableNotFound.Is(err) || sql.ErrColumnNotFound.Is(err)) && strings.HasPrefix(strings.ToLower(b.ctx.Query()), "select") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good point about the error needing to be different for creating a new view with a bad column references, versus querying an existing view that has a bad column reference. Relying on the prefix match for SELECT works okay, but won't cover all cases and feels like there should be a better way...

Does this code get invoked when creating a new view? It seems like it wouldn't, but I haven't tested. Any other ideas on how to avoid the string prefix matching check there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this error message is not returned when creating a new view referencing invalid table. This error message is returned only when querying existing view that its referenced tables, columns, functions or user privileges were altered to become invalid.
Creating a new view or updating existing view to have invalid references to these will not cause this issue.

I'll think of ideas to avoid prefix matching :)

Copy link
Contributor

@fulghum fulghum 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 think we can simplify one block a bit, but after that, I think this is ready to go.

sql/planbuilder/from.go Outdated Show resolved Hide resolved
@jennifersp jennifersp merged commit a76a0da into main Sep 27, 2023
@jennifersp jennifersp deleted the jennifer/view-invalid-ref branch September 27, 2023 22:22
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.

Renaming a table breaks views using that table
2 participants