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

[Vertica] fix gaps for some datatypes, complex alias support, fix group by for DDL #5691

Merged
merged 9 commits into from
Mar 21, 2024
90 changes: 47 additions & 43 deletions src/sqlfluff/dialects/dialect_vertica.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"TO",
Sequence(
Ref("IntervalUnitsGrammar"),
Bracketed(Ref("IntegerSegment"), optional=True),
Ref("BracketedArguments", optional=True),
),
optional=True,
),
Expand Down Expand Up @@ -298,7 +298,7 @@
Ref("JoinLikeClauseGrammar"),
BracketedSegment,
),
PostFunctionGrammar=OneOf(
PostFunctionGrammar=AnySetOf(
# Optional OVER suffix for window functions.
# This is supported in bigquery & postgres (and its derivatives)
# and so is included here for now.
Expand Down Expand Up @@ -465,7 +465,15 @@ class ArrayTypeSegment(ansi.ArrayTypeSegment):
"""Prefix for array literals specifying the type."""

type = "array_type"
match_grammar = Ref.keyword("ARRAY")
match_grammar = Sequence(
Ref.keyword("ARRAY"),
Bracketed(
Ref("DatatypeSegment"),
bracket_type="square",
bracket_pairs_set="bracket_pairs",
optional=True,
),
)


class LimitClauseSegment(ansi.LimitClauseSegment):
Expand Down Expand Up @@ -694,27 +702,9 @@ class PartitionByClauseSegment(BaseSegment):
),
),
),
terminators=[Sequence("GROUP", "BY"), "REORGANIZE"],
),
Sequence(
"GROUP",
"BY",
OneOf(
Ref("FunctionSegment"),
Bracketed(
Delimited(
Sequence(
OneOf(
Ref("ColumnReferenceSegment"),
Ref("NumericLiteralSegment"),
Ref("ExpressionSegment"),
Ref("ShorthandCastSegment"),
),
),
),
),
),
optional=True,
),
Ref("GroupByClauseSegment", optional=True),
Ref.keyword("REORGANIZE", optional=True),
)

Expand Down Expand Up @@ -1243,15 +1233,9 @@ class AlterDefaultPrivilegesGrantSegment(BaseSegment):
Ref("AlterDefaultPrivilegesObjectPrivilegesSegment"),
"ON",
OneOf(
Delimited(Sequence(Ref.keyword("TABLE", optional=True), Ref("TableReferenceSegment"))),
Delimited(
Sequence(
Ref.keyword("TABLE", optional=True), Ref("TableReferenceSegment")
)
),
Delimited(
Sequence(
"ALL", "TABLES", "IN", "SCHEMA", Ref("SchemaReferenceSegment")
),
Sequence("ALL", "TABLES", "IN", "SCHEMA", Ref("SchemaReferenceSegment")),
),
terminators=["WITH"],
),
Expand Down Expand Up @@ -1579,7 +1563,7 @@ class DatatypeSegment(ansi.DatatypeSegment):
"DOUBLE",
"PRECISION",
),
Sequence("FLOAT", Bracketed(Ref("NumericLiteralSegment"), optional=True)),
Sequence("FLOAT", Ref("BracketedArguments", optional=True)),
"FLOAT8",
"REAL",
# Exact Numeric
Expand All @@ -1591,11 +1575,7 @@ class DatatypeSegment(ansi.DatatypeSegment):
"TINYINT",
Sequence(
OneOf("DECIMAL", "NUMERIC", "NUMBER", "MONEY"),
Bracketed(
Ref("IntegerSegment"),
Sequence(Ref("CommaSegment"), Ref("IntegerSegment"), optional=True),
optional=True,
),
Ref("BracketedArguments", optional=True),
),
# Spatial
Sequence(
Expand Down Expand Up @@ -1629,7 +1609,6 @@ class DatatypeSegment(ansi.DatatypeSegment):
),
Ref("ArrayTypeSegment"),
Ref("SizedArrayTypeSegment"),
optional=True,
),
# TODO: add row data type support
Sequence(
Expand Down Expand Up @@ -1821,11 +1800,7 @@ class FunctionSegment(ansi.FunctionSegment):
parse_mode=ParseMode.GREEDY,
),
),
AnyNumberOf(Ref("PostFunctionGrammar")),
# Allow AS clause for some functions at the end
Sequence(
"AS", Bracketed(Delimited(Ref("ColumnReferenceSegment"))), optional=True
),
AnySetOf(Ref("PostFunctionGrammar")),
),
)

Expand Down Expand Up @@ -2029,3 +2004,32 @@ class CreateSchemaStatementSegment(ansi.CreateSchemaStatementSegment):
Ref("DiskQuotaSegment"),
),
)


class AliasExpressionSegment(ansi.AliasExpressionSegment):
"""A reference to an object with an `AS` clause.

The optional AS keyword allows both implicit and explicit aliasing.
"""

match_grammar: Matchable = OneOf(
Sequence(
Ref.keyword("AS", optional=True),
OneOf(
Sequence(
Ref("SingleIdentifierGrammar"),
# Column alias in VALUES clause
Bracketed(Ref("SingleIdentifierListSegment"), optional=True),
),
Sequence(Bracketed(Ref("SingleIdentifierListSegment"), optional=True)),
Ref("SingleQuotedIdentifierSegment"),
),
),
# Some functions alias several columns in brackets () like mapkeys or explode
Sequence(
Indent,
Ref.keyword("AS"),
Bracketed(Delimited(Ref("ColumnReferenceSegment"))),
Dedent,
),
)
17 changes: 9 additions & 8 deletions test/fixtures/dialects/vertica/array.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ CREATE TABLE sal_emp (

SELECT ARRAY[[1, 2], [3, 4]];

SELECT ARRAY[row(1, 2), row(1, 3)];

SELECT
name,
num,
gpa
FROM students
WHERE major = ARRAY[row('Science', 'Physics')];
-- Need to add support for complex datatypes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a regression?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true, but if you try to use row datatype not inside array it will fail because it's not implemented, so it seems better to have more specific array support and later add a row datatype

Current changes in the array segment allow me to parse sections with Jinja like:

select
    array[
    {% for metric_event in funnel_events %}
        {% for window_size in window_sizes %}
            null,
        {% endfor %}
    {% endfor %}
    null
]::array[varchar] as agg_data
from tab;

Without new changes, it will violate rule RF06 Missing quoted keyword identifier varchar

-- SELECT ARRAY[row(1, 2), row(1, 3)];

-- SELECT
-- name,
-- num,
-- gpa
-- FROM students
-- WHERE major = ARRAY[row('Science', 'Physics')];

SELECT (ARRAY['a', 'b', 'c', 'd', 'e'])[1];

Expand Down
125 changes: 18 additions & 107 deletions test/fixtures/dialects/vertica/array.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: cd76fd1b4e1f69f9f686aff180c40eebac49dad5046cfe6fda107e01b6563186
_hash: 1bdbe6f564c74c0888f9154975dc3c898fb5662d890f8f5045930d306f43a7b7
file:
- statement:
select_statement:
Expand Down Expand Up @@ -65,34 +65,27 @@ file:
- column_reference:
naked_identifier: pay_by_quarter
- data_type:
sized_array_type:
array_type:
keyword: ARRAY
array_accessor:
start_square_bracket: '['
expression:
column_reference:
naked_identifier: int
end_square_bracket: ']'
array_type:
keyword: ARRAY
start_square_bracket: '['
data_type:
keyword: int
end_square_bracket: ']'
- comma: ','
- column_reference:
naked_identifier: schedule
- data_type:
sized_array_type:
array_type:
keyword: ARRAY
array_accessor:
start_square_bracket: '['
expression:
function:
function_name:
function_name_identifier: varchar
bracketed:
start_bracket: (
expression:
numeric_literal: '50'
end_bracket: )
end_square_bracket: ']'
array_type:
keyword: ARRAY
start_square_bracket: '['
data_type:
keyword: varchar
bracketed_arguments:
bracketed:
start_bracket: (
numeric_literal: '50'
end_bracket: )
end_square_bracket: ']'
- end_bracket: )
- statement_terminator: ;
- statement:
Expand Down Expand Up @@ -120,88 +113,6 @@ file:
- end_square_bracket: ']'
- end_square_bracket: ']'
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
typed_array_literal:
array_type:
keyword: ARRAY
array_literal:
- start_square_bracket: '['
- function:
function_name:
function_name_identifier: row
bracketed:
- start_bracket: (
- expression:
numeric_literal: '1'
- comma: ','
- expression:
numeric_literal: '2'
- end_bracket: )
- comma: ','
- function:
function_name:
function_name_identifier: row
bracketed:
- start_bracket: (
- expression:
numeric_literal: '1'
- comma: ','
- expression:
numeric_literal: '3'
- end_bracket: )
- end_square_bracket: ']'
- statement_terminator: ;
- statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_element:
column_reference:
naked_identifier: name
- comma: ','
- select_clause_element:
column_reference:
naked_identifier: num
- comma: ','
- select_clause_element:
column_reference:
naked_identifier: gpa
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: students
where_clause:
keyword: WHERE
expression:
column_reference:
naked_identifier: major
comparison_operator:
raw_comparison_operator: '='
typed_array_literal:
array_type:
keyword: ARRAY
array_literal:
start_square_bracket: '['
function:
function_name:
function_name_identifier: row
bracketed:
- start_bracket: (
- expression:
quoted_literal: "'Science'"
- comma: ','
- expression:
quoted_literal: "'Physics'"
- end_bracket: )
end_square_bracket: ']'
- statement_terminator: ;
- statement:
select_statement:
select_clause:
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/dialects/vertica/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ CREATE TABLE orders(
orderdate DATE
);

CREATE TABLE orders(
orderkey INT ENCODING AUTO,
custkey INT,
prodkey ARRAY[VARCHAR(10)],
orderprices ARRAY[DECIMAL(12,2)],
orderdate DATE
)
partition by orderdate::date group by CALENDAR_HIERARCHY_DAY(orderdate::DATE, 3, 2) REORGANIZE;

-- CREATE TABLE inventory
-- (store INT, products ROW(name VARCHAR, code VARCHAR));

Expand Down