-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Copy expression types to avoid weight_strings and derived tables #15069
Conversation
… into derived tables. Signed-off-by: wangweicugw <[email protected]>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
ctx.SemTable.CopySemanticInfo(rae.Expr, col) | ||
ctx.SemTable.CopySemanticInfo(lae.Expr, 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.
I don't think this is correct. This works when both the left and right columns have the same type, but if they have different types, we should coerce the resulting type and not just copy it.
Here is an example query:
mysql> select 1 union select now();
Field 1: `1`
Catalog: `def`
Database: ``
Table: ``
Org_table: ``
Type: VAR_STRING
Collation: utf8mb4_0900_ai_ci (255)
Length: 76
Max_length: 19
Decimals: 0
Flags: NOT_NULL
+---------------------+
| 1 |
+---------------------+
| 1 |
| 2024-01-30 07:54:24 |
+---------------------+
2 rows in set (0.00 sec)
As you can see when you run the mysql client with the option --column-type-info
, we have two columns with different types - int and datetime, and they are coerced into varchar.
Fortunately, we already have this functionality today. Here is my suggestion:
ctx.SemTable.CopySemanticInfo(rae.Expr, col) | |
ctx.SemTable.CopySemanticInfo(lae.Expr, col) | |
rt, foundR := ctx.SemTable.TypeForExpr(rae.Expr) | |
lt, foundL := ctx.SemTable.TypeForExpr(lae.Expr) | |
if foundR && foundL { | |
types := []sqltypes.Type{rt.Type(), lt.Type()} | |
t := evalengine.AggregateTypes(types) | |
ctx.SemTable.ExprTypes[col] = evalengine.NewType(t, collations.Unknown) | |
} |
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.
Yes, I overlooked the case where the left and right columns have different types. However, in some cases, you may not be able to retrieve the evalengine.Type
through ctx.SemTable.TypeForExpr()
method, especially when dealing with syntax tree nodes like now()
that cannot provide the evalengine.Type
after processing it with ctx.SemTable.TypeForExpr()
.
So, I added some additional logic with an "else" condition to handle cases where the evalengine.Type
cannot be obtained.
We run the end-to-end tests on older binaries as well. Since this fix hasn't made it to the older versions, we need to stop the test from running if we are not on utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate") This will stop the |
Signed-off-by: wangweicugw <[email protected]>
Ok👌🏻 |
types := []sqltypes.Type{rt.Type(), lt.Type()} | ||
t := evalengine.AggregateTypes(types) | ||
ctx.SemTable.ExprTypes[col] = evalengine.NewType(t, collations.Unknown) | ||
} else { |
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.
I don't think we should have the else clause here. It's not correct to use either of the two types as the common type. Like my example showed, you can have a situation where one side is an int, the other side is a datetime, but the common type is varchar.
If we don't have both types available, I think we need to use the weight_string
function to be sure we can compare values correctly.
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.
I understand now. There shouldn't be an "else" processing logic.
If it is a syntax tree node for the now() function, we can save its corresponding evalengine.Type
in ExprTypes
during the analysis phase in the analyzer.
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.
Yeah, we could/should update typer.go
and add more expressions there. Or, even better, start using the excellent typing logic we have in the evalengine. One of these days...
Signed-off-by: wangweicugw <[email protected]>
if foundR && foundL { | ||
types := []sqltypes.Type{rt.Type(), lt.Type()} | ||
t := evalengine.AggregateTypes(types) | ||
ctx.SemTable.ExprTypes[col] = evalengine.NewType(t, collations.Unknown) |
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.
@systay Shouldn't this have used AggregateEvalTypes
so that the collation is also known and not collations.Unknown
?
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.
Ah, #15122 answers my question here.
…oid weight_strings and derived tables (vitessio#15069) Signed-off-by: wangweicugw <[email protected]>
Description
The query
was transformed into
for execution, but by copying the types, it can be simplified and avoid the generation of a derived table. We can now just use the original query.
Related Issue(s)
#15020
Checklist
Deployment Notes