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

Clickhouse: allow FINAL modifier #3534

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
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: ;