diff --git a/src/sqlfluff/dialects/dialect_bigquery.py b/src/sqlfluff/dialects/dialect_bigquery.py index bf027176a..a9517f425 100644 --- a/src/sqlfluff/dialects/dialect_bigquery.py +++ b/src/sqlfluff/dialects/dialect_bigquery.py @@ -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"), diff --git a/test/dialects/bigquery_test.py b/test/dialects/bigquery_test.py index ffde7ffe6..b92ec3364 100644 --- a/test/dialects/bigquery_test.py +++ b/test/dialects/bigquery_test.py @@ -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 diff --git a/test/fixtures/dialects/bigquery/create_js_function_complex_types.sql b/test/fixtures/dialects/bigquery/create_js_function_complex_types.sql index 29b1493c0..f82ba09c2 100644 --- a/test/fixtures/dialects/bigquery/create_js_function_complex_types.sql +++ b/test/fixtures/dialects/bigquery/create_js_function_complex_types.sql @@ -4,7 +4,7 @@ qs( foo2 ARRAY, foo3 STRUCT, foo4 STRUCT, - foo5 STRUCT, b STRUCT> + foo5 STRUCT, b STRUCT> ) RETURNS STRUCT> LANGUAGE js AS """ diff --git a/test/fixtures/dialects/bigquery/create_js_function_complex_types.yml b/test/fixtures/dialects/bigquery/create_js_function_complex_types.yml index b7d326cb5..49262a700 100644 --- a/test/fixtures/dialects/bigquery/create_js_function_complex_types.yml +++ b/test/fixtures/dialects/bigquery/create_js_function_complex_types.yml @@ -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 diff --git a/test/fixtures/dialects/bigquery/select_with_cast.yml b/test/fixtures/dialects/bigquery/select_with_cast.yml index 6c288cbbe..243abdd04 100644 --- a/test/fixtures/dialects/bigquery/select_with_cast.yml +++ b/test/fixtures/dialects/bigquery/select_with_cast.yml @@ -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: