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

resolve aliases in subqueries in function arguments #1838

Merged
merged 11 commits into from
Jun 21, 2023
Merged

resolve aliases in subqueries in function arguments #1838

merged 11 commits into from
Jun 21, 2023

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Jun 20, 2023

The rule reorderProjection also replaces subqueries with getfields in projections when they are used by subqueries, but it did not check for function expressions.

This meant that aliases in subqueries as arguments to functions threw a "x" could not be found error.
This PR just has the section of reorderProjection that is supposed to find deferredColumns also look at the arguments of functions recursively (because we can nest functions).

Additionally, there was another schema type bug:

tmp> select 0 as foo, if((select foo), 123, 456);
+-----+----------------------------+
| foo | if((select foo), 123, 456) |
+-----+----------------------------+
| 0   | 127                        |
+-----+----------------------------+
1 row in set (0.00 sec)

MySQL returns an Integer type for if statement, and if either argument is a String, it always returns a String.

fix for: dolthub/dolt#6174

{
Query: `SELECT if(0, "abc", 456)`,
Expected: []sql.Row{
{456},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be a string, but the TestEvaluation doesn't go through the schema[0].Convert code path.

Copy link
Contributor

Choose a reason for hiding this comment

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

should we move to brokenQueries?

Copy link
Contributor

Choose a reason for hiding this comment

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

or is it just bad test setup

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's just bad test setup, when the results are written to the shell we use the writerIters, but not for evaluation.
I can add it in a separate PR, and see how many failures we get

@@ -123,6 +123,30 @@ func reorderProjection(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
})
}

// findDeferredColumns is a function that searches through a projection's expressions and checks if there are any
// subqueries that reference the deferred column. Additionally, we have to check for subqueries in functions arguments
func findDeferredColumns(exprs ...sql.Expression) []column {
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any reason not to just transform.Expr for each of these?

t.Run("if() type is correct", func(t *testing.T) {
handler.ComInitDB(dummyConn, "test")
var result *sqltypes.Result
err := handler.ComQuery(dummyConn, "select if(1, 123, 'def')", func(res *sqltypes.Result, more bool) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

should be do (0, "def", 123) -> "123?

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 old code had return n.ifTrue.Type(), which was already Text, so it didn't actually test the new code I added.

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.

LGTM, could probably add more tests, but we need a dedicated expression typing module before realistically fixing the long tail

@jycor jycor merged commit 2c47dac into main Jun 21, 2023
@jycor jycor deleted the james/alias branch July 21, 2023 17:27
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