Skip to content

Commit

Permalink
Add support for date operations with intervals in Oracle (#5262)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaostorrer committed Oct 1, 2023
1 parent 595ad1f commit 8e14e5a
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/sqlfluff/dialects/dialect_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CommentSegment,
Delimited,
IdentifierSegment,
LiteralSegment,
Matchable,
OneOf,
OptionallyBracketed,
Expand All @@ -28,6 +29,7 @@
StringLexer,
StringParser,
SymbolSegment,
TypedParser,
WordSegment,
)
from sqlfluff.dialects import dialect_ansi as ansi
Expand Down Expand Up @@ -128,6 +130,7 @@
Ref("ColumnReferenceSegment"),
),
),
IntervalUnitsGrammar=OneOf("YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND"),
)

oracle_dialect.replace(
Expand Down Expand Up @@ -246,6 +249,14 @@
Ref("AccessorGrammar", optional=True),
allow_gaps=True,
),
DateTimeLiteralGrammar=Sequence(
OneOf("DATE", "TIME", "TIMESTAMP", "INTERVAL"),
TypedParser("single_quote", LiteralSegment, type="date_constructor_literal"),
Sequence(
Ref("IntervalUnitsGrammar"),
Sequence("TO", Ref("IntervalUnitsGrammar"), optional=True),
),
),
)


Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/dialects/oracle/interval_operations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
select 1
from dual
where sysdate > sysdate - interval '2' hour;

select sysdate - interval '3' year
from dual;

select interval '2 3:04:11.333' day to second
from dual;

select 1
from dual
where sysdate > to_date('01/01/1970', 'dd/mm/yyyy') + interval '600' month;

select sysdate + interval '10' minute
from dual;
123 changes: 123 additions & 0 deletions test/fixtures/dialects/oracle/interval_operations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 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: 75c92164df465af4705b75019cbed005252cff6f553ba67ca21c682c1568788b
file:
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
numeric_literal: '1'
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
where_clause:
keyword: where
expression:
- bare_function: sysdate
- comparison_operator:
raw_comparison_operator: '>'
- bare_function: sysdate
- binary_operator: '-'
- keyword: interval
- date_constructor_literal: "'2'"
- keyword: hour
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
expression:
- bare_function: sysdate
- binary_operator: '-'
- keyword: interval
- date_constructor_literal: "'3'"
- keyword: year
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
- keyword: interval
- date_constructor_literal: "'2 3:04:11.333'"
- keyword: day
- keyword: to
- keyword: second
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
numeric_literal: '1'
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
where_clause:
keyword: where
expression:
- bare_function: sysdate
- comparison_operator:
raw_comparison_operator: '>'
- function:
function_name:
function_name_identifier: to_date
bracketed:
- start_bracket: (
- expression:
quoted_literal: "'01/01/1970'"
- comma: ','
- expression:
quoted_literal: "'dd/mm/yyyy'"
- end_bracket: )
- binary_operator: +
- keyword: interval
- date_constructor_literal: "'600'"
- keyword: month
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
expression:
- bare_function: sysdate
- binary_operator: +
- keyword: interval
- date_constructor_literal: "'10'"
- keyword: minute
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
- statement_terminator: ;

0 comments on commit 8e14e5a

Please sign in to comment.