Skip to content

Commit

Permalink
Clickhouse: allow FINAL modifier (#3534)
Browse files Browse the repository at this point in the history
* allow final modifier in clickhouse dialect

* Update src/sqlfluff/dialects/dialect_clickhouse.py

Co-authored-by: Barry Pollard <barrypollard@google.com>

* Update test/fixtures/dialects/clickhouse/final.sql

Co-authored-by: Barry Pollard <barrypollard@google.com>

Co-authored-by: Barry Pollard <barrypollard@google.com>
Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>
  • Loading branch information
3 people authored Jul 1, 2022
1 parent 153c438 commit e42f60c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/sqlfluff/dialects/dialect_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
https://clickhouse.com/
"""

from sqlfluff.core.dialects import load_raw_dialect
from sqlfluff.core.parser import (
Bracketed,
Matchable,
OneOf,
OptionallyBracketed,
Ref,
Sequence,
)

from sqlfluff.core.dialects import load_raw_dialect
from sqlfluff.dialects import dialect_ansi as ansi

ansi_dialect = load_raw_dialect("ansi")
Expand Down Expand Up @@ -46,3 +46,31 @@ class CTEDefinitionSegment(ansi.CTEDefinitionSegment):
Ref("SingleIdentifierGrammar"),
),
)


class FromExpressionElementSegment(ansi.FromExpressionElementSegment):
"""A table expression.
Overridden from ANSI to allow FINAL modifier.
https://clickhouse.com/docs/en/sql-reference/statements/select/from#final-modifier
"""

type = "from_expression_element"
match_grammar: Matchable = Sequence(
Ref("PreTableFunctionKeywordsGrammar", optional=True),
OptionallyBracketed(Ref("TableExpressionSegment")),
Ref(
"AliasExpressionSegment",
exclude=OneOf(
Ref("SamplingExpressionSegment"),
Ref("JoinLikeClauseGrammar"),
Ref.keyword("Final"),
),
optional=True,
),
Ref.keyword("FINAL", optional=True),
# https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays#flattening_arrays
Sequence("WITH", "OFFSET", Ref("AliasExpressionSegment"), optional=True),
Ref("SamplingExpressionSegment", optional=True),
Ref("PostTableExpressionGrammar", optional=True),
)
7 changes: 7 additions & 0 deletions test/fixtures/dialects/clickhouse/final.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select a
from my_table final
where a > 0;

SELECT sum(bytes)
FROM system.parts as table_alias final
WHERE active;
64 changes: 64 additions & 0 deletions test/fixtures/dialects/clickhouse/final.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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: 1d3370a426ccdd899c3d108235a7fb028f8644a7b30c867df3912357ed9231b9
file:
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
column_reference:
identifier: a
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: my_table
keyword: final
where_clause:
keyword: where
expression:
column_reference:
identifier: a
comparison_operator:
raw_comparison_operator: '>'
literal: '0'
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: sum
bracketed:
start_bracket: (
expression:
column_reference:
identifier: bytes
end_bracket: )
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- identifier: system
- dot: .
- identifier: parts
alias_expression:
keyword: as
identifier: table_alias
keyword: final
where_clause:
keyword: WHERE
expression:
column_reference:
identifier: active
- statement_terminator: ;

0 comments on commit e42f60c

Please sign in to comment.