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

Issue 1083: Support BigQuery named arguments #1111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Contributors:
- Disabled models in the dbt templater are now skipped enitrely rather than
returning an untemplated file.
- Add a changelog check to SQLFluff continuous integration.
- Fix bug [#1083](https://github.com/sqlfluff/sqlfluff/issues/1083), adding
support for BigQuery named function arguments, used with functions such as
[ST_GEOGFROMGEOJSON()](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_geogfromgeojson)

## [0.6.0a1] - 2021-05-15
### Added
Expand Down
28 changes: 28 additions & 0 deletions src/sqlfluff/dialects/dialect_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Indent,
SymbolSegment,
RegexLexer,
StringLexer,
CodeSegment,
NamedParser,
StringParser,
Expand All @@ -32,6 +33,14 @@
ansi_dialect = load_raw_dialect("ansi")
bigquery_dialect = ansi_dialect.copy_as("bigquery")

bigquery_dialect.insert_lexer_matchers(
# JSON Operators: https://www.postgresql.org/docs/9.5/functions-json.html
[
StringLexer("right_arrow", "=>", CodeSegment),
],
before="equals",
)

bigquery_dialect.patch_lexer_matchers(
[
# Quoted literals can have r or b (case insensitive) prefixes, in any order, to
Expand Down Expand Up @@ -68,6 +77,9 @@
EndAngleBracketSegment=StringParser(
">", SymbolSegment, name="end_angle_bracket", type="end_angle_bracket"
),
RightArrowSegment=StringParser(
"=>", SymbolSegment, name="right_arrow", type="right_arrow"
),
)


Expand All @@ -78,6 +90,7 @@
Ref("ExpressionSegment"),
Sequence(OneOf("IGNORE", "RESPECT"), "NULLS", optional=True),
),
Ref("NamedArgumentSegment"),
),
SimpleArrayTypeGrammar=Sequence(
"ARRAY",
Expand Down Expand Up @@ -364,6 +377,21 @@ class TypelessStructSegment(BaseSegment):
)


@bigquery_dialect.segment()
class NamedArgumentSegment(BaseSegment):
"""Named argument to a function.

https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_geogfromgeojson
"""

type = "named_argument"
match_grammar = Sequence(
Ref("NakedIdentifierSegment"),
Ref("RightArrowSegment"),
Ref("ExpressionSegment"),
)


@bigquery_dialect.segment()
class LiteralCoercionSegment(BaseSegment):
"""A casting operation with a type name preceding a string literal.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT ST_GEOGFROMGEOJSON('{"type":"LineString","coordinates":[[1,2],[4,5]]}', make_valid => true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
file:
statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: ST_GEOGFROMGEOJSON
bracketed:
start_bracket: (
expression:
literal: "'{\"type\":\"LineString\",\"coordinates\":[[1,2],[4,5]]}'"
comma: ','
named_argument:
identifier: make_valid
right_arrow: =>
expression:
literal: 'true'
end_bracket: )