Skip to content

Commit 63a6959

Browse files
authored
feat: add support for pragma, set, call (#668)
1 parent 436c39e commit 63a6959

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Formatting Changes and Bug Fixes
88

99
- sqlfmt will no longer attempt to format quoted names in blocks of unsupported DDL ([#653](https://github.com/tconbeer/sqlfmt/issues/653) - thank you [@stiabh](https://github.com/stiabh)!)
10+
- adds support for formatting `pragma`, `set`, and `call` statements([#656](https://github.com/tconbeer/sqlfmt/issues/656) - thank you [@aersam](https://github.com/aersam)!)
1011

1112
## [0.25.0] - 2025-01-24
1213

src/sqlfmt/rules/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
CREATE_CLONABLE,
1010
CREATE_FUNCTION,
1111
CREATE_WAREHOUSE,
12+
PRAGMA_SET_CALL,
1213
group,
1314
)
1415
from sqlfmt.rules.core import CORE as CORE
1516
from sqlfmt.rules.function import FUNCTION as FUNCTION
1617
from sqlfmt.rules.grant import GRANT as GRANT
1718
from sqlfmt.rules.jinja import JINJA as JINJA # noqa
19+
from sqlfmt.rules.pragma import PRAGMA as PRAGMA
1820
from sqlfmt.rules.unsupported import UNSUPPORTED as UNSUPPORTED
1921
from sqlfmt.rules.warehouse import WAREHOUSE as WAREHOUSE
2022
from sqlfmt.tokens import TokenType
@@ -262,6 +264,15 @@
262264
),
263265
),
264266
),
267+
Rule(
268+
name="pragma",
269+
priority=2005,
270+
pattern=group(PRAGMA_SET_CALL) + group(r"\W", r"$"),
271+
action=partial(
272+
actions.handle_nonreserved_top_level_keyword,
273+
action=partial(actions.lex_ruleset, new_ruleset=PRAGMA),
274+
),
275+
),
265276
Rule(
266277
name="grant",
267278
priority=2010,

src/sqlfmt/rules/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ def group(*choices: str) -> str:
5353
)
5454
+ r"(\s+if\s+not\s+exists)?"
5555
)
56+
57+
PRAGMA_SET_CALL = group(r"pragma", r"set", r"call")

src/sqlfmt/rules/pragma.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from functools import partial
2+
3+
from sqlfmt import actions
4+
from sqlfmt.rule import Rule
5+
from sqlfmt.rules.common import PRAGMA_SET_CALL, group
6+
from sqlfmt.rules.core import CORE
7+
from sqlfmt.tokens import TokenType
8+
9+
# Covers simple PRAGMA, SET, and CALL statements, e.g.:
10+
# PRAGMA database_list;
11+
# PRAGMA storage_info('table_name');
12+
# CALL pragma_storage_info('table_name');
13+
# SET default_collation = 'nocase';
14+
PRAGMA = [
15+
*CORE,
16+
Rule(
17+
name="unterm_keyword",
18+
priority=1300,
19+
pattern=group(PRAGMA_SET_CALL) + group(r"\W", r"$"),
20+
action=partial(
21+
actions.handle_reserved_keyword,
22+
action=partial(
23+
actions.add_node_to_buffer, token_type=TokenType.UNTERM_KEYWORD
24+
),
25+
),
26+
),
27+
]

tests/data/unformatted/412_pragma.sql

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- source: https://duckdb.org/docs/configuration/pragmas.html
2+
PRAGMA collations;
3+
SET threads = 4;
4+
SET default_collation = 'nocase';
5+
SET default_null_order = 'NULLS_FIRST';
6+
SET default_null_order = 'NULLS_LAST_ON_ASC_FIRST_ON_DESC';
7+
PRAGMA default_collation = 'nocase';
8+
PRAGMA default_null_order = 'NULLS_FIRST';
9+
PRAGMA default_null_order = 'NULLS_LAST_ON_ASC_FIRST_ON_DESC';
10+
PRAGMA order_by_non_integer_literal = true;
11+
PRAGMA version;
12+
CALL pragma_version();
13+
PRAGMA enable_progress_bar;
14+
PRAGMA explain_output = 'physical_only';
15+
PRAGMA table_info('table_name');
16+
CALL pragma_table_info('table_name');
17+
)))))__SQLFMT_OUTPUT__(((((
18+
-- source: https://duckdb.org/docs/configuration/pragmas.html
19+
pragma collations
20+
;
21+
set threads = 4
22+
;
23+
set default_collation = 'nocase'
24+
;
25+
set default_null_order = 'NULLS_FIRST'
26+
;
27+
set default_null_order = 'NULLS_LAST_ON_ASC_FIRST_ON_DESC'
28+
;
29+
pragma default_collation = 'nocase'
30+
;
31+
pragma default_null_order = 'NULLS_FIRST'
32+
;
33+
pragma default_null_order = 'NULLS_LAST_ON_ASC_FIRST_ON_DESC'
34+
;
35+
pragma order_by_non_integer_literal = true
36+
;
37+
pragma version
38+
;
39+
call pragma_version()
40+
;
41+
pragma enable_progress_bar
42+
;
43+
pragma explain_output = 'physical_only'
44+
;
45+
pragma table_info('table_name')
46+
;
47+
call pragma_table_info('table_name')
48+
;

tests/functional_tests/test_general_formatting.py

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"unformatted/409_create_external_function.sql",
9191
"unformatted/410_create_warehouse.sql",
9292
"unformatted/411_create_clone.sql",
93+
"unformatted/412_pragma.sql",
9394
"unformatted/900_create_view.sql",
9495
"unformatted/999_unsupported_ddl.sql",
9596
],

0 commit comments

Comments
 (0)