Skip to content

Commit

Permalink
5944: Add support for databricks named parameters (sqlfluff#5946)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Weeden <adam.weeden@encoura.org>
  • Loading branch information
TheCleric and TheCleric authored Jun 5, 2024
1 parent 5976a2c commit dc9f74e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/sqlfluff/dialects/dialect_databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
OneOf,
Ref,
Sequence,
StringLexer,
StringParser,
SymbolSegment,
TypedParser,
)
from sqlfluff.dialects import dialect_ansi as ansi
Expand All @@ -39,6 +42,15 @@
databricks_dialect.sets("date_part_function_name").update(["TIMEDIFF"])


databricks_dialect.insert_lexer_matchers(
# Named Function Parameters:
# https://docs.databricks.com/en/sql/language-manual/sql-ref-function-invocation.html#named-parameter-invocation
[
StringLexer("right_arrow", "=>", CodeSegment),
],
before="equals",
)

databricks_dialect.add(
DoubleQuotedUDFBody=TypedParser(
"double_quote",
Expand All @@ -58,6 +70,14 @@
type="udf_body",
trim_chars=("$",),
),
RightArrowSegment=StringParser("=>", SymbolSegment, type="right_arrow"),
)

databricks_dialect.replace(
FunctionContentsExpressionGrammar=OneOf(
Ref("ExpressionSegment"),
Ref("NamedArgumentSegment"),
),
)


Expand Down Expand Up @@ -302,3 +322,17 @@ class CreateDatabricksFunctionStatementSegment(BaseSegment):
),
Ref("FunctionDefinitionGrammar"),
)


class NamedArgumentSegment(BaseSegment):
"""Named argument to a function.
https://docs.databricks.com/en/sql/language-manual/sql-ref-function-invocation.html#named-parameter-invocation
"""

type = "named_argument"
match_grammar = Sequence(
Ref("NakedIdentifierSegment"),
Ref("RightArrowSegment"),
Ref("ExpressionSegment"),
)
7 changes: 7 additions & 0 deletions test/fixtures/dialects/databricks/named_argument.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--https://docs.databricks.com/en/sql/language-manual/sql-ref-function-invocation.html#named-parameter-invocation

select my_function(arg1 => 3, arg2 => 4) from dual;

select my_function(3, arg2 => 4) from dual;

select my_function(arg1 => 3, 4) from dual;
91 changes: 91 additions & 0 deletions test/fixtures/dialects/databricks/named_argument.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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: 96a8a90d5cb19753570b96e0b15c0390b1676bb54dd1a8c226a75f998e31ea54
file:
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
function:
function_name:
function_name_identifier: my_function
bracketed:
- start_bracket: (
- named_argument:
naked_identifier: arg1
right_arrow: =>
expression:
numeric_literal: '3'
- comma: ','
- named_argument:
naked_identifier: arg2
right_arrow: =>
expression:
numeric_literal: '4'
- end_bracket: )
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:
function:
function_name:
function_name_identifier: my_function
bracketed:
start_bracket: (
expression:
numeric_literal: '3'
comma: ','
named_argument:
naked_identifier: arg2
right_arrow: =>
expression:
numeric_literal: '4'
end_bracket: )
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:
function:
function_name:
function_name_identifier: my_function
bracketed:
start_bracket: (
named_argument:
naked_identifier: arg1
right_arrow: =>
expression:
numeric_literal: '3'
comma: ','
expression:
numeric_literal: '4'
end_bracket: )
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: dual
- statement_terminator: ;

0 comments on commit dc9f74e

Please sign in to comment.