BigQuery: explicitly define data types (#7305)

Co-authored-by: Annebelle Olminkhof <20630578+annebelleo@users.noreply.github.com>
This commit is contained in:
Annebelle Olminkhof
2025-12-12 20:44:06 +01:00
committed by GitHub
parent 6a49fdd4fc
commit e683a71d58
5 changed files with 53 additions and 9 deletions

View File

@@ -290,6 +290,40 @@ bigquery_dialect.replace(
casefold=str.upper,
)
),
DatatypeIdentifierSegment=SegmentGenerator(
lambda dialect: MultiStringParser(
[
"INT64",
"INT",
"SMALLINT",
"INTEGER",
"BIGINT",
"TINYINT",
"BYTEINT",
"FLOAT64",
"NUMERIC",
"DECIMAL",
"BIGNUMERIC",
"BIGDECIMAL",
"BOOL",
"BOOLEAN",
"STRING",
"BYTES",
"DATE",
"DATETIME",
"TIME",
"TIMESTAMP",
"GEOGRAPHY",
"INTERVAL",
"JSON",
"RANGE",
"ARRAY",
"STRUCT",
],
CodeSegment,
type="data_type_identifier",
)
),
FunctionContentsExpressionGrammar=OneOf(
Ref("DatetimeUnitSegment"),
Ref("DatePartWeekSegment"),

View File

@@ -4,7 +4,7 @@ import hypothesis.strategies as st
import pytest
from hypothesis import example, given, note, settings
from sqlfluff.core import FluffConfig
from sqlfluff.core import FluffConfig, Linter
from sqlfluff.core.parser import Lexer, Parser
@@ -90,3 +90,11 @@ def test_bigquery_table_reference_segment_iter_raw_references(
orp.part for orp in table_reference.iter_raw_references()
]
assert reference_parts == actual_reference_parts
def test_cast_as_float_fails():
"""CAST(... AS FLOAT) should fail for BigQuery (only FLOAT64 allowed)."""
sql = "SELECT CAST('4.0' AS FLOAT)"
parsed = Linter(dialect="bigquery").parse_string(sql)
# Parsing produces a parse error for the unsupported `FLOAT` type.
assert parsed.violations

View File

@@ -4,7 +4,7 @@ qs(
foo2 ARRAY<STRING>,
foo3 STRUCT<x INT64>,
foo4 STRUCT<x INT64, y INT64>,
foo5 STRUCT<a ARRAY<FLOAT>, b STRUCT<x INT64, y INT64>>
foo5 STRUCT<a ARRAY<FLOAT64>, b STRUCT<x INT64, y INT64>>
)
RETURNS STRUCT<product_id ARRAY<INT64>>
LANGUAGE js AS """

View File

@@ -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: d1fa0d72cc2af0530c5f21c4cea3da882175a110632f386d1752c6136a1bb15f
_hash: 09fea1147a6721122baff61b940cabd21edd638edb25e3829479fea628e1b460
file:
statement:
create_function_statement:
@@ -66,7 +66,7 @@ file:
keyword: ARRAY
start_angle_bracket: <
data_type:
data_type_identifier: FLOAT
data_type_identifier: FLOAT64
end_angle_bracket: '>'
- comma: ','
- parameter: b

View File

@@ -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: b3eb84334d4a93f639f727dd59f0833c1332eab98414140b0076dae106703690
_hash: bfa63bceb292ee595dc0f2d449ee416a35c43b97ae6289ac6d6844c8bfdd3d3b
file:
- statement:
select_statement:
@@ -87,8 +87,9 @@ file:
- data_type:
data_type_identifier: STRING
- expression:
data_type:
data_type_identifier: FORMAT
column_reference:
naked_identifier: FORMAT
- expression:
quoted_literal: "'ASCII'"
- end_bracket: )
alias_expression:
@@ -114,8 +115,9 @@ file:
- data_type:
data_type_identifier: STRING
- expression:
data_type:
data_type_identifier: FORMAT
column_reference:
naked_identifier: FORMAT
- expression:
quoted_literal: "'YYYY-MM-DD HH24:MI:SS TZH:TZM'"
- end_bracket: )
alias_expression: