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

Copy expression types to avoid weight_strings and derived tables #15069

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

wangweicugw
Copy link
Contributor

@wangweicugw wangweicugw commented Jan 29, 2024

Description

The query

select 1 from user 
union 
select 2 from user

was transformed into

select `1`, weight_string(`1`) from (
  select 1 from `user` 
  union 
  select 2 from `user`) as dt

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

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Copy link
Contributor

vitess-bot bot commented Jan 29, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jan 29, 2024
@github-actions github-actions bot added this to the v19.0.0 milestone Jan 29, 2024
@systay systay added Type: Bug Component: Query Serving Backport to: release-18.0 and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 30, 2024
@systay

This comment was marked as resolved.

@systay

This comment was marked as resolved.

Comment on lines 202 to 203
ctx.SemTable.CopySemanticInfo(rae.Expr, col)
ctx.SemTable.CopySemanticInfo(lae.Expr, col)
Copy link
Collaborator

@systay systay Jan 30, 2024

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:

Suggested change
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)
}

Copy link
Contributor Author

@wangweicugw wangweicugw Jan 31, 2024

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.

Copy link

codecov bot commented Jan 30, 2024

Codecov Report

Attention: 488 lines in your changes are missing coverage. Please review.

Comparison is base (eddb39e) 47.29% compared to head (f1b01ea) 47.71%.
Report is 86 commits behind head on main.

Files Patch % Lines
...vt/vtgate/planbuilder/operators/sharded_routing.go 0.00% 42 Missing ⚠️
go/vt/vtgate/planbuilder/operators/delete.go 0.00% 32 Missing ⚠️
go/vt/vtgate/evalengine/cached_size.go 0.00% 31 Missing ⚠️
.../vtgate/planbuilder/operators/delete_with_input.go 0.00% 31 Missing ⚠️
go/vt/vtctl/workflow/traffic_switcher.go 0.00% 25 Missing ⚠️
go/vt/vtgate/engine/delete_with_input.go 53.70% 23 Missing and 2 partials ⚠️
...tgate/planbuilder/operators/aggregation_pushing.go 0.00% 25 Missing ⚠️
...vt/vtgate/planbuilder/operators/queryprojection.go 0.00% 20 Missing ⚠️
.../vt/vtgate/planbuilder/operators/query_planning.go 0.00% 19 Missing ⚠️
go/vt/vtgate/evalengine/expr_tuple_bvar.go 62.50% 14 Missing and 4 partials ⚠️
... and 48 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15069      +/-   ##
==========================================
+ Coverage   47.29%   47.71%   +0.41%     
==========================================
  Files        1137     1155      +18     
  Lines      238684   240236    +1552     
==========================================
+ Hits       112895   114621    +1726     
+ Misses     117168   117009     -159     
+ Partials     8621     8606      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@systay systay changed the title copying the types, some Union operators do not need to be transformed into derived tables. Copy expression types to avoid weight_strings and derived tables Jan 30, 2024
@systay
Copy link
Collaborator

systay commented Jan 30, 2024

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 main. You need to extract the new test lines into it's own test method, and start this method with:

utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate")

This will stop the Query Serving (Queries) - Upgrade Downgrade Testing / Run Upgrade Downgrade Test CI step from failing

@wangweicugw
Copy link
Contributor Author

wangweicugw commented Jan 31, 2024

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 main. You need to extract the new test lines into it's own test method, and start this method with:

utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate")

This will stop the Query Serving (Queries) - Upgrade Downgrade Testing / Run Upgrade Downgrade Test CI step from failing

Ok👌🏻

types := []sqltypes.Type{rt.Type(), lt.Type()}
t := evalengine.AggregateTypes(types)
ctx.SemTable.ExprTypes[col] = evalengine.NewType(t, collations.Unknown)
} else {
Copy link
Collaborator

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.

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 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.

Copy link
Collaborator

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]>
@systay systay merged commit ea8a90d into vitessio:main Feb 2, 2024
102 checks passed
if foundR && foundL {
types := []sqltypes.Type{rt.Type(), lt.Type()}
t := evalengine.AggregateTypes(types)
ctx.SemTable.ExprTypes[col] = evalengine.NewType(t, collations.Unknown)
Copy link
Contributor

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?

Copy link
Contributor

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.

@wangweicugw wangweicugw deleted the fix_union branch February 5, 2024 00:50
wangweicugw added a commit to wangweicugw/vitess that referenced this pull request Feb 5, 2024
…oid weight_strings and derived tables (vitessio#15069)

Signed-off-by: wangweicugw <[email protected]>
systay pushed a commit that referenced this pull request Feb 5, 2024
…oid weight_strings and derived tables (#15069) (#15129)

Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants