Skip to content

Commit

Permalink
fix: support spark type-hinted number literals (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Nov 22, 2024
1 parent 15abb00 commit d37dfbe
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Formatting Changes and Bug Fixes

- sqlfmt no longer adds a space between the function name and parens for `filter()` and `isnull()` (but it also permits `filter ()` and `isnull ()` to support dialects where those are operators, not function names) ([#641](https://github.com/tconbeer/sqlfmt/issues/641) - thank you [@williamscs](https://github.com/williamscs) and [@hongtron](https://github.com/hongtron)!).
- sqlfmt now supports Spark type-hinted numeric literals like `32y` and `+3.2e6bd` and will not introduce a space between the digits and their type suffix ([#640](https://github.com/tconbeer/sqlfmt/issues/640) - thank you [@ShaneMazur](https://github.com/ShaneMazur)!).

## [0.23.3] - 2024-11-12

### Bug Fixes
Expand Down
15 changes: 13 additions & 2 deletions src/sqlfmt/rules/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,22 @@
action=actions.raise_sqlfmt_bracket_error,
),
Rule(
# see https://spark.apache.org/docs/latest/sql-ref-literals.html#integral-literal-syntax
name="spark_int_literals",
priority=349,
pattern=group(
r"(\+|-)?\d+(l|s|y)",
),
action=actions.handle_number,
),
Rule(
# the (bd|d|f) groups add support for Spark fractional literals
# https://spark.apache.org/docs/latest/sql-ref-literals.html#fractional-literals-syntax
name="number",
priority=350,
pattern=group(
r"(\+|-)?\d+(\.\d*)?(e(\+|-)?\d+)?",
r"(\+|-)?\.\d+(e(\+|-)?\d+)?",
r"(\+|-)?\d+(\.\d*)?(e(\+|-)?\d+)?(bd|d|f)?",
r"(\+|-)?\.\d+(e(\+|-)?\d+)?(bd|d|f)?",
),
action=actions.handle_number,
),
Expand Down
54 changes: 54 additions & 0 deletions tests/data/unformatted/132_spark_number_literals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- source: https://spark.apache.org/docs/latest/sql-ref-literals.html#integral-literal-syntax
-- see: https://github.com/tconbeer/sqlfmt/issues/640
SELECT -2147483648 AS col;
SELECT 9223372036854775807l AS col;
SELECT -32Y AS col;
SELECT 482S AS col;
SELECT 12.578 AS col;
SELECT -0.1234567 AS col;
SELECT -.1234567 AS col;
SELECT 123. AS col;
SELECT 123.BD AS col;
SELECT 5E2 AS col;
SELECT 5D AS col;
SELECT -5BD AS col;
SELECT 12.578e-2d AS col;
SELECT -.1234567E+2BD AS col;
SELECT +3.e+3 AS col;
SELECT -3.E-3D AS col;
)))))__SQLFMT_OUTPUT__(((((
-- source:
-- https://spark.apache.org/docs/latest/sql-ref-literals.html#integral-literal-syntax
-- see: https://github.com/tconbeer/sqlfmt/issues/640
select -2147483648 as col
;
select 9223372036854775807l as col
;
select -32Y as col
;
select 482S as col
;
select 12.578 as col
;
select -0.1234567 as col
;
select -.1234567 as col
;
select 123. as col
;
select 123.BD as col
;
select 5E2 as col
;
select 5D as col
;
select -5BD as col
;
select 12.578e-2d as col
;
select -.1234567E+2BD as col
;
select +3.e+3 as col
;
select -3.E-3D as col
;
1 change: 1 addition & 0 deletions tests/functional_tests/test_general_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"unformatted/129_duckdb_joins.sql",
"unformatted/130_athena_data_types.sql",
"unformatted/131_assignment_statement.sql",
"unformatted/132_spark_number_literals.sql",
"unformatted/133_for_else.sql",
"unformatted/200_base_model.sql",
"unformatted/201_basic_snapshot.sql",
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ def get_rule(ruleset: List[Rule], rule_name: str) -> Rule:
(MAIN, "statement_start", "case"),
(MAIN, "statement_end", "END"),
(CORE, "star", "*"),
(CORE, "spark_int_literals", "145y"),
(CORE, "spark_int_literals", "-145s"),
(CORE, "spark_int_literals", "5l"),
(CORE, "number", "145.8"),
(CORE, "number", "-.58"),
(CORE, "number", "+145.8"),
(CORE, "number", "+.58"),
(CORE, "number", "1e9"),
(CORE, "number", "1e-9"),
(CORE, "number", "1.55e-9"),
(CORE, "number", "145.8bd"),
(CORE, "number", "-.58d"),
(CORE, "number", "+145.8f"),
(CORE, "number", "+.58bd"),
(CORE, "number", "1e9d"),
(CORE, "number", "1e-9f"),
(CORE, "number", "1.55e-9bd"),
(CORE, "bracket_open", "["),
(CORE, "bracket_close", ")"),
(CORE, "double_colon", "::"),
Expand Down

0 comments on commit d37dfbe

Please sign in to comment.