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 date, time and timestamp literals #10921

Merged

Conversation

dbussink
Copy link
Contributor

@dbussink dbussink commented Aug 3, 2022

This implements the date, time and timestamp literals in the parser and ensures that bind variables also can be generated using these literals.

Related Issue(s)

Part of #8604

Checklist

  • "Backport me!" label has been added if this change should be backported
  • Tests were added or are not required
  • Documentation was added or is not required

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Aug 3, 2022

Review Checklist

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

General

  • Ensure that the Pull Request has a descriptive title.
  • If this is a change that users need to know about, please apply the release notes (needs details) label so that merging is blocked unless the summary release notes document is included.
  • If a new flag is being introduced, review whether it is really needed. The flag names should be clear and intuitive (as far as possible), and the flag's help should be descriptive.
  • If a workflow is added or modified, each items in Jobs should be named in order to mark it as required. If the workflow should be required, the GitHub Admin should be notified.

Bug fixes

  • There should be at least one unit or end-to-end test.
  • The Pull Request description should either include a link to an issue that describes the bug OR an actual description of the bug and how to reproduce, along with a description of the fix.

Non-trivial changes

  • There should be some code comments as to why things are implemented the way they are.

New/Existing features

  • Should be documented, either by modifying the existing documentation or creating new documentation.
  • New features should have a link to a feature request issue or an RFC that documents the use cases, corner cases and test cases.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • vtctl command output order should be stable and awk-able.

@dbussink
Copy link
Contributor Author

dbussink commented Aug 3, 2022

@GuptaManan100 This adds a number of shift / reduce conflicts and I was wondering if you think it's possible to fix those or not here.

@dbussink
Copy link
Contributor Author

dbussink commented Aug 3, 2022

The other main question I have here is how to deal with values that aren't valid date, time or timestamps. What is the general approach we should take for those?

Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

The shift-reduce conflicts have been fixed using precedence rules.
STRING_TYPE_PREFIX_NON_KEYWORD is used to resolve shift-reduce conflicts occurring due to column_name symbol and being able to use keywords like DATE and TIME as prefixes to strings to denote their type. The shift-reduce conflict occurs because after seeing one of these non-reserved keywords, if we see a STRING, then we can either shift to use the STRING typed rule in literal or reduce the non-reserved keyword into column_name and eventually use a rule from simple_expr. The way to fix this conflict is to give shifting higher precedence than reducing.

@dbussink dbussink added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving labels Aug 3, 2022
@dbussink dbussink force-pushed the parse-date-time-timestamp-literals branch from 32731aa to f6f7e20 Compare August 6, 2022 16:07
@dbussink
Copy link
Contributor Author

dbussink commented Aug 6, 2022

@GuptaManan100 @systay I've also added logic here to validate the actual literals and tried to match MySQL behavior within reason.

@dbussink dbussink marked this pull request as ready for review August 6, 2022 18:35
@dbussink
Copy link
Contributor Author

dbussink commented Aug 6, 2022

The CI failure here in endtoend is unrelated to the changes here, see also #10953

dbussink and others added 5 commits August 6, 2022 21:56
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
… column names

Signed-off-by: Manan Gupta <manan@planetscale.com>
…used to type cast STRINGS

Signed-off-by: Manan Gupta <manan@planetscale.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
@dbussink dbussink force-pushed the parse-date-time-timestamp-literals branch from f6f7e20 to 5106666 Compare August 6, 2022 19:56
This adds additional validation of date, time and timestamp literals. It
uses the same validation the evalengine already uses today which is more
restricted than MySQL itself but seems good enough for now.

It consolidates the parsing into one place so if the syntax allowed is
extended, we only have to update one location.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
@dbussink dbussink force-pushed the parse-date-time-timestamp-literals branch from 5106666 to a48a161 Compare August 7, 2022 08:08
@@ -541,6 +541,7 @@ const (
ERInvalidCastToJSON = 3147
ERJSONValueTooBig = 3150
ERJSONDocumentTooDeep = 3157
ERWrongValue = 1525
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 is the same error type as MySQL:

mysql> select date'foo';
ERROR 1525 (HY000): Incorrect DATE value: 'foo'

@@ -189,6 +189,7 @@ var stateToMysqlCode = map[vterrors.State]struct {
vterrors.WrongNumberOfColumnsInSelect: {num: ERWrongNumberOfColumnsInSelect, state: SSWrongNumberOfColumns},
vterrors.WrongTypeForVar: {num: ERWrongTypeForVar, state: SSClientError},
vterrors.WrongValueForVar: {num: ERWrongValueForVar, state: SSClientError},
vterrors.WrongValue: {num: ERWrongValue, state: SSUnknownSQLState},
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 SQL state here is also in MySQL unknown (HY000):

mysql> select date'foo';
ERROR 1525 (HY000): Incorrect DATE value: 'foo'

@@ -146,6 +163,11 @@ func (nz *normalizer) convertLiteralDedup(node *Literal, cursor *Cursor) {

// convertLiteral converts an Literal without the dedup.
func (nz *normalizer) convertLiteral(node *Literal, cursor *Cursor) {
err := validateLiteral(node)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried various approaches, but injecting this in the normalization step seemed the easiest but it needs to be checked then from here and also in convertLiteralDedup.

go/vt/sqlparser/parse_date.go Outdated Show resolved Hide resolved
@@ -235,39 +235,27 @@ func compareNumeric(v1, v2 *EvalResult) (int, error) {
func parseDate(expr *EvalResult) (t time.Time, err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unified what this function depends on also into the single parsing logic in the sqlparser package.

}
t, err = sqlparser.ParseTime(expr.string())
return
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 new logic above should be equivalent to trying things in the loop. The error message depends on the last type checked, but that already was the case anyway.

Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

I like this approach 🥇

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
@dbussink dbussink force-pushed the parse-date-time-timestamp-literals branch from 57046c3 to 865d610 Compare August 8, 2022 08:08
@GuptaManan100 GuptaManan100 merged commit 6450e49 into vitessio:main Aug 8, 2022
@dbussink dbussink deleted the parse-date-time-timestamp-literals branch August 8, 2022 18:25
systay pushed a commit to planetscale/vitess that referenced this pull request Aug 19, 2022
…o#929)

* Implement date, time and timestamp literals

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>

* feat: add a test that verifies we can use date, timestamp and time as column names

Signed-off-by: Manan Gupta <manan@planetscale.com>

* feat: add precedence rule for non-reserved-keywords that can also be used to type cast STRINGS

Signed-off-by: Manan Gupta <manan@planetscale.com>

* test: fix tpch test to not expect syntax error

Signed-off-by: Manan Gupta <manan@planetscale.com>

* feat: make parser

Signed-off-by: Manan Gupta <manan@planetscale.com>

* Add validate of date style literals

This adds additional validation of date, time and timestamp literals. It
uses the same validation the evalengine already uses today which is more
restricted than MySQL itself but seems good enough for now.

It consolidates the parsing into one place so if the syntax allowed is
extended, we only have to update one location.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>

* Implement more complete TIME parsing support

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>

Co-authored-by: Manan Gupta <manan@planetscale.com>

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants