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

Implement temporal comparisons #17826

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dbussink
Copy link
Contributor

This is currently missing and leads to incorrect types to be returned for LEAST & GREATEST as comparison functions.

There's a little mismatch here in behavior compared to MySQL which I argue is actually a bug in MySQL. In MySQL, a temporal type always has the binary collation:

mysql> select NOW(6), collation(NOW(6));
+----------------------------+-------------------+
| NOW(6)                     | collation(NOW(6)) |
+----------------------------+-------------------+
| 2025-02-19 15:33:21.732301 | binary            |
+----------------------------+-------------------+
1 row in set (0.00 sec)

On MySQL 8.4, this results in:

mysql> select GREATEST(NOW(6), NOW(6)), collation(GREATEST(NOW(6), NOW(6)));
+----------------------------+-------------------------------------+
| GREATEST(NOW(6), NOW(6))   | collation(GREATEST(NOW(6), NOW(6))) |
+----------------------------+-------------------------------------+
| 2025-02-19 15:35:00.921308 | latin1_swedish_ci                   |
+----------------------------+-------------------------------------+
1 row in set (0.00 sec)

But on MySQL 8.0, it returns:

mysql> select GREATEST(NOW(6), NOW(6)), collation(GREATEST(NOW(6), NOW(6)));
+----------------------------+-------------------------------------+
| GREATEST(NOW(6), NOW(6))   | collation(GREATEST(NOW(6), NOW(6))) |
+----------------------------+-------------------------------------+
| 2025-02-19 15:35:00.921308 | utf8mb4_0900_ai_ci                  |
+----------------------------+-------------------------------------+
1 row in set (0.00 sec)

Neither of these collations make sense, because it really should not change the collation and return binary still. That is what Vitess still does with the changes here (hence the addition to the test framework to allow skipping the collation check).

I'll also report the issue upstream to make it behave correctly there as well.

Marked this also for backporting since using these types today leads to a panic on queries.

Related Issue(s)

Fixes #17750

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

@dbussink dbussink added Type: Bug Component: Evalengine changes to the evaluation engine Backport to: release-19.0 Needs to be back ported to release-19.0 Backport to: release-20.0 Needs to be backport to release-20.0 Backport to: release-21.0 Needs to be backport to release-21.0 labels Feb 19, 2025
@dbussink dbussink requested review from vmg and systay as code owners February 19, 2025 15:32
Copy link
Contributor

vitess-bot bot commented Feb 19, 2025

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 Feb 19, 2025
@dbussink dbussink 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 Feb 19, 2025
@github-actions github-actions bot added this to the v22.0.0 milestone Feb 19, 2025
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

Attention: Patch coverage is 80.55191% with 148 lines in your changes missing coverage. Please review.

Project coverage is 67.42%. Comparing base (2118bc3) to head (4171a5e).

Files with missing lines Patch % Lines
go/vt/vtgate/evalengine/eval_temporal.go 17.72% 65 Missing ⚠️
go/vt/vtgate/evalengine/fn_compare.go 89.03% 25 Missing ⚠️
go/vt/vtgate/evalengine/compiler_asm_push.go 0.00% 21 Missing ⚠️
go/vt/vtgate/evalengine/compiler_asm.go 60.97% 16 Missing ⚠️
go/vt/vtgate/evalengine/compiler.go 44.44% 15 Missing ⚠️
go/vt/vtgate/evalengine/expr_bvar.go 0.00% 3 Missing ⚠️
go/vt/vtgate/evalengine/expr_column.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17826      +/-   ##
==========================================
- Coverage   67.45%   67.42%   -0.03%     
==========================================
  Files        1592     1592              
  Lines      258167   258554     +387     
==========================================
+ Hits       174143   174338     +195     
- Misses      84024    84216     +192     

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

@dbussink dbussink force-pushed the dbussink/fix-multi-comparison-temporal branch from 766be25 to 39089b4 Compare February 19, 2025 16:32
This is currently missing and leads to incorrect types to be returned
for `LEAST` & `GREATEST` as comparison functions.

There's a little mismatch here in behavior compared to MySQL which I
argue is actually a bug in MySQL. In MySQL, a temporal type always has
the binary collation:

```
mysql> select NOW(6), collation(NOW(6));
+----------------------------+-------------------+
| NOW(6)                     | collation(NOW(6)) |
+----------------------------+-------------------+
| 2025-02-19 15:33:21.732301 | binary            |
+----------------------------+-------------------+
1 row in set (0.00 sec)

```

On MySQL 8.4, this results in:

```
mysql> select GREATEST(NOW(6), NOW(6)), collation(GREATEST(NOW(6), NOW(6)));
+----------------------------+-------------------------------------+
| GREATEST(NOW(6), NOW(6))   | collation(GREATEST(NOW(6), NOW(6))) |
+----------------------------+-------------------------------------+
| 2025-02-19 15:35:00.921308 | latin1_swedish_ci                   |
+----------------------------+-------------------------------------+
1 row in set (0.00 sec)
```

But on MySQL 8.0, it returns:

```
mysql> select GREATEST(NOW(6), NOW(6)), collation(GREATEST(NOW(6), NOW(6)));
+----------------------------+-------------------------------------+
| GREATEST(NOW(6), NOW(6))   | collation(GREATEST(NOW(6), NOW(6))) |
+----------------------------+-------------------------------------+
| 2025-02-19 15:35:00.921308 | utf8mb4_0900_ai_ci                  |
+----------------------------+-------------------------------------+
1 row in set (0.00 sec)
```

Neither of these collations make sense, because it really should not
change the collation and return `binary` still. That is what Vitess
still does with the changes here (hence the addition to the test
framework to allow skipping the collation check).

I'll also report the issue upstream to make it behave correctly there as
well.

Signed-off-by: Dirkjan Bussink <[email protected]>
@@ -119,7 +119,7 @@ func TestCompilerReference(t *testing.T) {
var supported, total int
env := evalengine.EmptyExpressionEnv(venv)

tc.Run(func(query string, row []sqltypes.Value) {
tc.Run(func(query string, row []sqltypes.Value, skipCollationCheck bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't we use skipCollationCheck here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@systay This currently doesn't do collations checks anyway, so I don't think it really matters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@systay This did remind me that we actually should validate the collation. We still ignore this flag though, since our evalengine and compiler should always match.

The ignore collation flag is for where explicitly don't 100% match MySQL (due to bugs, arguably broken behavior etc).

Signed-off-by: Dirkjan Bussink <[email protected]>
@@ -22,6 +22,7 @@ import (
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/mysql/collations/charset"
"vitess.io/vitess/go/mysql/collations/colldata"
datetime2 "vitess.io/vitess/go/mysql/datetime"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: another solution would be to use the variable name datetimes in the functions, and keep import named datetime

Copy link
Collaborator

@systay systay left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -82,12 +82,12 @@ func normalizeValue(v sqltypes.Value, coll collations.ID) sqltypes.Value {
return v
}

func compareRemoteExprEnv(t *testing.T, collationEnv *collations.Environment, env *evalengine.ExpressionEnv, conn *mysql.Conn, expr string, fields []*querypb.Field, cmp *testcases.Comparison) {
func compareRemoteExprEnv(t *testing.T, collationEnv *collations.Environment, env *evalengine.ExpressionEnv, conn *mysql.Conn, expr string, fields []*querypb.Field, cmp *testcases.Comparison, skipCollationCheck bool) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

skipCollationCheck is needed because of the issue in MySQL described in mysql/mysql-server#602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backport to: release-19.0 Needs to be back ported to release-19.0 Backport to: release-20.0 Needs to be backport to release-20.0 Backport to: release-21.0 Needs to be backport to release-21.0 Component: Evalengine changes to the evaluation engine Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug Report: GREATEST() function call returns results in wrong datatype (compared to MySQL)
2 participants