Skip to content

Commit

Permalink
fix/#637/db variant (#661)
Browse files Browse the repository at this point in the history
* fix #637: no space between a colon and square bracket

* chore: update changelog
  • Loading branch information
tconbeer authored Jan 24, 2025
1 parent 7caa359 commit 04bf374
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ All notable changes to this project will be documented in this file.
### Formatting Changes and Bug Fixes

- sqlfmt no longer adds a space between the `*` and `columns` in DuckDB `*columns` expressions ([#657](https://github.com/tconbeer/sqlfmt/issues/657) - thank you [@aersam](https://github.com/aersam)!)
- sqlfmt no longer adds a space between the `:` and `['name']` in a DataBricks escaped variant expression like `foo:['bar.baz']` ([#637](https://github.com/tconbeer/sqlfmt/issues/637) - thank you [@aersam](https://github.com/aersam)!)

### Testing
- sqlfmt is now tested against and fully supports Python 3.13

## [0.24.0] - 2024-11-22
Expand Down
9 changes: 9 additions & 0 deletions src/sqlfmt/node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ def whitespace(
)
):
return NO_SPACE
# open square brackets that follow colons are escaped databricks
# variant cols
elif (
token.type is TokenType.BRACKET_OPEN
and token.token == "["
and previous_token
and previous_token.type is TokenType.COLON
):
return NO_SPACE
# need a space before any other open bracket
elif token.type is TokenType.BRACKET_OPEN:
return SPACE
Expand Down
6 changes: 6 additions & 0 deletions tests/data/unformatted/136_databricks_variant.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://github.com/tconbeer/sqlfmt/issues/637
SELECT e.col:['attr_ID']::string as id
FROM expl e
)))))__SQLFMT_OUTPUT__(((((
-- https://github.com/tconbeer/sqlfmt/issues/637
select e.col:['attr_ID']::string as id from expl e
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 @@ -56,6 +56,7 @@
"unformatted/133_for_else.sql",
"unformatted/134_databricks_type_hints.sql",
"unformatted/135_star_columns.sql",
"unformatted/136_databricks_variant.sql",
"unformatted/200_base_model.sql",
"unformatted/201_basic_snapshot.sql",
"unformatted/202_unpivot_macro.sql",
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/test_node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def test_identifier_whitespace(default_mode: Mode, source_string: str) -> None:
"([])\n",
"()[] + foo()[offset(1)]\n",
"using (id)\n",
"foo:['bar.baz']\n",
],
)
def test_bracket_whitespace(default_mode: Mode, source_string: str) -> None:
Expand Down

0 comments on commit 04bf374

Please sign in to comment.