Skip to content

Commit

Permalink
Merge branch 'main' into parent_dir_config
Browse files Browse the repository at this point in the history
  • Loading branch information
j-svensmark authored Jun 10, 2024
2 parents 90b35f0 + 69b4f96 commit 72dfbc2
Show file tree
Hide file tree
Showing 12 changed files with 534 additions and 74 deletions.
19 changes: 16 additions & 3 deletions src/sqlfluff/core/rules/config_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,25 @@
"definition": "The capitalisation policy to enforce.",
},
"extended_capitalisation_policy": {
"validation": ["consistent", "upper", "lower", "pascal", "capitalise", "snake"],
"validation": [
"consistent",
"upper",
"lower",
"pascal",
"capitalise",
"snake",
"camel",
],
"definition": (
"The capitalisation policy to enforce, extended with PascalCase "
"and snake_case. "
"The capitalisation policy to enforce, extended with PascalCase, "
"snake_case, and camelCase. "
"This is separate from ``capitalisation_policy`` as it should not be "
"applied to keywords."
"Camel, Pascal, and Snake will never be inferred when the policy is set"
"to consistent. This is because snake can cause destructive changes to"
"the identifier, and unlinted code is too easily mistaken for camel and "
"pascal. If, when set to consistent, no consistent case is found, it will"
"default to upper."
),
},
"select_clause_trailing_comma": {
Expand Down
21 changes: 21 additions & 0 deletions src/sqlfluff/dialects/dialect_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,27 @@ class CreateTableStatementSegment(ansi.CreateTableStatementSegment):
)


class CreateViewStatementSegment(BaseSegment):
"""A `CREATE VIEW` statement.
https://clickhouse.com/docs/en/sql-reference/statements/create/view
"""

type = "create_view_statement"

match_grammar = Sequence(
"CREATE",
Ref("OrReplaceGrammar", optional=True),
"VIEW",
Ref("IfNotExistsGrammar", optional=True),
Ref("TableReferenceSegment"),
Ref("OnClusterClauseSegment", optional=True),
"AS",
Ref("SelectableGrammar"),
Ref("TableEndClauseSegment", optional=True),
)


class CreateMaterializedViewStatementSegment(BaseSegment):
"""A `CREATE MATERIALIZED VIEW` statement.
Expand Down
23 changes: 0 additions & 23 deletions src/sqlfluff/dialects/dialect_exasol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,6 @@ class InsertStatementSegment(BaseSegment):
Ref.keyword("INTO", optional=True),
Ref("TableReferenceSegment"),
AnyNumberOf(
Ref("ValuesInsertClauseSegment"),
Ref("ValuesRangeClauseSegment"),
Sequence("DEFAULT", "VALUES"),
Ref("SelectableGrammar"),
Expand All @@ -1539,28 +1538,6 @@ class InsertStatementSegment(BaseSegment):
)


class ValuesInsertClauseSegment(BaseSegment):
"""A `VALUES` clause like in `INSERT`."""

type = "values_insert_clause"
match_grammar = Sequence(
"VALUES",
Delimited(
Bracketed(
Delimited(
Ref("LiteralGrammar"),
Ref("IntervalExpressionSegment"),
Ref("FunctionSegment"),
Ref("BareFunctionSegment"),
"DEFAULT",
Ref("SelectableGrammar"),
),
parse_mode=ParseMode.GREEDY,
),
),
)


############################
# UPDATE
############################
Expand Down
29 changes: 17 additions & 12 deletions src/sqlfluff/rules/capitalisation/CP01.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ def _handle_segment(self, segment: BaseSegment, context: RuleContext) -> LintRes
refuted_cases = memory.get("refuted_cases", set())

# Which cases are definitely inconsistent with the segment?
first_letter_is_lowercase = False
for character in segment.raw:
if is_capitalizable(character):
first_letter_is_lowercase = character != character.upper()
break
# If none of the characters are letters there will be a parsing
# error, so not sure we need this statement
first_letter_is_lowercase = False

# We refute inference of camel, pascal, and snake case.
# snake, if not explicitly set, can be destructive to
# variable names, adding underscores.
# camel and Pascal could allow poorly linted code in,
# so must be explicitly chosen.
refuted_cases.update(["camel", "pascal", "snake"])
if first_letter_is_lowercase:
# snake added here as it cannot be inferred (presents as lower)
refuted_cases.update(["upper", "capitalise", "pascal", "snake"])
refuted_cases.update(["upper", "capitalise"])
if segment.raw != segment.raw.lower():
refuted_cases.update(["lower"])
else:
Expand All @@ -155,8 +158,6 @@ def _handle_segment(self, segment: BaseSegment, context: RuleContext) -> LintRes
refuted_cases.update(["upper"])
if segment.raw != segment.raw.capitalize():
refuted_cases.update(["capitalise"])
if not segment.raw.isalnum():
refuted_cases.update(["pascal", "snake"])

# Update the memory
memory["refuted_cases"] = refuted_cases
Expand Down Expand Up @@ -219,6 +220,14 @@ def _handle_segment(self, segment: BaseSegment, context: RuleContext) -> LintRes
lambda match: match.group(1) + match.group(2).upper() + match.group(3),
segment.raw,
)
elif concrete_policy == "camel":
# Similar to Pascal, for Camel, we can only do a best efforts approach.
# This presents as us never changing case mid-string.
fixed_raw = regex.sub(
"([^a-zA-Z0-9]+|^)([a-zA-Z0-9])([a-zA-Z0-9]*)",
lambda match: match.group(1) + match.group(2).lower() + match.group(3),
segment.raw,
)
elif concrete_policy == "snake":
if segment.raw.isupper():
fixed_raw = segment.raw.lower()
Expand All @@ -240,14 +249,10 @@ def _handle_segment(self, segment: BaseSegment, context: RuleContext) -> LintRes
# build description based on the policy in use
consistency = "consistently " if cap_policy == "consistent" else ""

if concrete_policy in ["upper", "lower"]:
if concrete_policy in ["upper", "lower", "pascal", "camel", "snake"]:
policy = f"{concrete_policy} case."
elif concrete_policy == "capitalise":
policy = "capitalised."
elif concrete_policy == "pascal":
policy = "pascal case."
elif concrete_policy == "snake":
policy = "snake case."

# Return the fixed segment
self.logger.debug(
Expand Down
44 changes: 44 additions & 0 deletions test/fixtures/dialects/clickhouse/create_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE VIEW db.view_mv
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE VIEW db.view_mv
ON CLUSTER mycluster
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE OR REPLACE VIEW db.view_mv
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE OR REPLACE VIEW db.view_mv
ON CLUSTER mycluster
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE VIEW IF NOT EXISTS db.view_mv
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE VIEW IF NOT EXISTS db.view_mv
AS SELECT
column1,
column2
FROM db.table_kafka;

CREATE VIEW IF NOT EXISTS db.view_mv
ON CLUSTER mycluster
AS SELECT
column1,
column2
FROM db.table_kafka;
Loading

0 comments on commit 72dfbc2

Please sign in to comment.