Skip to content

Commit

Permalink
Fix Clickhouse identifiers format (#5890)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Strybuk <[email protected]>
Co-authored-by: Greg Finley <[email protected]>
  • Loading branch information
3 people committed May 20, 2024
1 parent a2b55e7 commit 3aae379
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
NakedIdentifierSegment=SegmentGenerator(
# Generate the anti template from the set of reserved keywords
lambda dialect: RegexParser(
r"[A-Z0-9_]*[A-Z][A-Z0-9_]*",
r"[a-zA-Z_][0-9a-zA-Z_]*",
IdentifierSegment,
type="naked_identifier",
anti_template=r"^(" + r"|".join(dialect.sets("reserved_keywords")) + r")$",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dialects/clickhouse/create_database.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
CREATE DATABASE db_name;
CREATE DATABASE _123;
CREATE DATABASE db_name COMMENT 'SingleQuoted';
CREATE DATABASE db_name COMMENT "DoubleQuoted";
CREATE DATABASE db_name COMMENT 'SingleQuoted three words';
CREATE DATABASE db_name COMMENT "DoubleQuoted three words";
CREATE DATABASE db_name COMMENT 'Weird characters: !@#$%^&*()_+{}|:"<>?';
CREATE DATABASE db_name ON CLUSTER cluster;
CREATE DATABASE db_name ON CLUSTER _123;
CREATE DATABASE db_name ON CLUSTER "cluster";
CREATE DATABASE db_name ON CLUSTER "underscore_cluster";
CREATE DATABASE db_name ON CLUSTER 'cluster';
Expand Down
20 changes: 19 additions & 1 deletion test/fixtures/dialects/clickhouse/create_database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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: 9e78f284bcd9f6853ca4d74d9fba0c6e2ad5e76d795a87763c4cf732346c8b8b
_hash: 6f61dcb5b9c9926b99cc4981bf90c36d88637f220c3ec6a5db6f2b47c66262f5
file:
- statement:
create_database_statement:
Expand All @@ -12,6 +12,13 @@ file:
- database_reference:
naked_identifier: db_name
- statement_terminator: ;
- statement:
create_database_statement:
- keyword: CREATE
- keyword: DATABASE
- database_reference:
naked_identifier: _123
- statement_terminator: ;
- statement:
create_database_statement:
- keyword: CREATE
Expand Down Expand Up @@ -68,6 +75,17 @@ file:
- keyword: CLUSTER
- naked_identifier: cluster
- statement_terminator: ;
- statement:
create_database_statement:
- keyword: CREATE
- keyword: DATABASE
- database_reference:
naked_identifier: db_name
- on_cluster_clause:
- keyword: 'ON'
- keyword: CLUSTER
- naked_identifier: _123
- statement_terminator: ;
- statement:
create_database_statement:
- keyword: CREATE
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dialects/clickhouse/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ CREATE TABLE WatchLog_new(date Date, UserId Int64, EventType String, Cnt UInt64)

CREATE TABLE WatchLog as WatchLog_old ENGINE=Merge(currentDatabase(), '^WatchLog');

CREATE TABLE _2 as _1 ENGINE=Merge(currentDatabase(), '^WatchLog');

CREATE TABLE hits_all AS hits
ENGINE = Distributed(logs, default, hits)
SETTINGS
Expand Down
32 changes: 31 additions & 1 deletion test/fixtures/dialects/clickhouse/create_table.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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: 612f5a6269c6f3b3edfb7afeee15e095dce9d4e4d6adfaeaab1ccab45eb525ce
_hash: d1ae6fd707a9460859df0a7f42811f1bf45148a3ca3dd4c540fe7cffbc8b8100
file:
- statement:
create_table_statement:
Expand Down Expand Up @@ -246,6 +246,36 @@ file:
quoted_literal: "'^WatchLog'"
- end_bracket: )
- statement_terminator: ;
- statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
naked_identifier: _2
- keyword: as
- table_reference:
naked_identifier: _1
- engine:
keyword: ENGINE
comparison_operator:
raw_comparison_operator: '='
table_engine_function:
function_name:
function_name_identifier: Merge
bracketed:
- start_bracket: (
- expression:
function:
function_name:
function_name_identifier: currentDatabase
bracketed:
start_bracket: (
end_bracket: )
- comma: ','
- expression:
quoted_literal: "'^WatchLog'"
- end_bracket: )
- statement_terminator: ;
- statement:
create_table_statement:
- keyword: CREATE
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/dialects/clickhouse/identifier.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT * FROM _1.Table;

SELECT * FROM _1._2;

SELECT bar AS _1 FROM foo;

SELECT * FROM foo.bar _1;

SELECT * FROM foo.bar AS _1;
102 changes: 102 additions & 0 deletions test/fixtures/dialects/clickhouse/identifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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: 6e05286924da8f64b190fb0d8d1ceec52aa5437535002e0aefd97cb12452b851
file:
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- naked_identifier: _1
- dot: .
- naked_identifier: Table
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- naked_identifier: _1
- dot: .
- naked_identifier: _2
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
column_reference:
naked_identifier: bar
alias_expression:
keyword: AS
naked_identifier: _1
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: foo
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- naked_identifier: foo
- dot: .
- naked_identifier: bar
alias_expression:
naked_identifier: _1
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
wildcard_expression:
wildcard_identifier:
star: '*'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- naked_identifier: foo
- dot: .
- naked_identifier: bar
alias_expression:
keyword: AS
naked_identifier: _1
- statement_terminator: ;

0 comments on commit 3aae379

Please sign in to comment.