Skip to content

Commit

Permalink
Redefine MySQL Interval segment (#2351)
Browse files Browse the repository at this point in the history
* fix #2224 by redefining interval segment for mysql

* fix based on PR comments

Co-authored-by: Barry Pollard <barry@tunetheweb.com>
  • Loading branch information
rpr-ableton and tunetheweb authored Jan 19, 2022
1 parent 3196b6c commit a50ccd8
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/sqlfluff/dialects/dialect_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,28 @@ class TableConstraintSegment(BaseSegment):
)


@mysql_dialect.segment(replace=True)
class IntervalExpressionSegment(BaseSegment):
"""An interval expression segment.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_adddate
"""

type = "interval_expression"
match_grammar = Sequence(
"INTERVAL",
OneOf(
# The Numeric Version
Sequence(
Ref("ExpressionSegment"),
OneOf(Ref("QuotedLiteralSegment"), Ref("DatetimeUnitSegment")),
),
# The String version
Ref("QuotedLiteralSegment"),
),
)


mysql_dialect.add(
DoubleForwardSlashSegment=StringParser(
"//", SymbolSegment, name="doubleforwardslash", type="statement_terminator"
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/dialects/mysql/interval.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SELECT DATE_ADD(CURDATE(), INTERVAL -30 DAY);

SELECT SUBDATE('2008-01-02', INTERVAL 31 DAY);

SELECT ADDDATE(CURDATE(), INTERVAL -30 DAY);

SELECT DATE_SUB('1992-12-31 23:59:59.000002', INTERVAL '1.999999' SECOND_MICROSECOND);

SELECT DATE_ADD('2100-12-31 23:59:59', INTERVAL '1:1' MINUTE_SECOND);

SELECT DATE_ADD(CURDATE(), INTERVAL 7 * 4 DAY);

SELECT
ADDDATE(CURDATE(), INTERVAL col1 DAY)
FROM
tbl1
;

SELECT
SUBDATE(CURDATE(), INTERVAL col1 + col2 DAY)
FROM
tbl1
;
225 changes: 225 additions & 0 deletions test/fixtures/dialects/mysql/interval.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 16fde14028919875d259e244f9620226a249cd1f78ee8a5eeaaa97160399107a
file:
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: DATE_ADD
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: CURDATE
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
numeric_literal:
binary_operator: '-'
literal: '30'
date_part: DAY
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: SUBDATE
bracketed:
- start_bracket: (
- expression:
literal: "'2008-01-02'"
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
literal: '31'
date_part: DAY
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: ADDDATE
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: CURDATE
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
numeric_literal:
binary_operator: '-'
literal: '30'
date_part: DAY
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: DATE_SUB
bracketed:
- start_bracket: (
- expression:
literal: "'1992-12-31 23:59:59.000002'"
- comma: ','
- expression:
keyword: INTERVAL
literal: "'1.999999'"
- expression:
column_reference:
identifier: SECOND_MICROSECOND
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: DATE_ADD
bracketed:
- start_bracket: (
- expression:
literal: "'2100-12-31 23:59:59'"
- comma: ','
- expression:
keyword: INTERVAL
literal: "'1:1'"
- expression:
column_reference:
identifier: MINUTE_SECOND
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: DATE_ADD
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: CURDATE
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
- literal: '7'
- binary_operator: '*'
- literal: '4'
date_part: DAY
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: ADDDATE
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: CURDATE
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
column_reference:
identifier: col1
date_part: DAY
- end_bracket: )
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: tbl1
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: SUBDATE
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: CURDATE
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
interval_expression:
keyword: INTERVAL
expression:
- column_reference:
identifier: col1
- binary_operator: +
- column_reference:
identifier: col2
date_part: DAY
- end_bracket: )
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: tbl1
- statement_terminator: ;

0 comments on commit a50ccd8

Please sign in to comment.