Skip to content

Commit

Permalink
fix #326: do not match unsupported ddl followed by open parens
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer committed Nov 28, 2022
1 parent bd08b42 commit 6af6de9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- sqlfmt now supports `alter function` and `drop function` statements ([#310](https://github.com/tconbeer/sqlfmt/issues/310), [#311](https://github.com/tconbeer/sqlfmt/issues/311)), and Snowflake's `create external function` statements ([#322](https://github.com/tconbeer/sqlfmt/issues/322)).
- sqlfmt better supports numeric constants (number literals), including those using scientific notation (e.g., `1.5e-9`) and the unary `+` or `-` operators (e.g., `+3`), and is now smarter about when the `-` symbol is the unary negative or binary subtraction operator. ([#321](https://github.com/tconbeer/sqlfmt/issues/321) - thank you [@liaopeiyuan](https://github.com/liaopeiyuan)!).
- fixed a bug where we added extra whitespace to the end of empty comment lines ([#319](https://github.com/tconbeer/sqlfmt/issues/319) - thank you [@eherde](https://github.com/eherde)!).
- fixed an issue where wrapping unsupported DDL in jinja would cause a parsing error ([#326](https://github.com/tconbeer/sqlfmt/issues/326) - thank you [@ETG-msimons](https://github.com/ETG-msimons)!).
- fixed a bug where we could have unsafely run *black* against jinja that contained Python keywords and their safe alternatives (e.g., `return(return_())`).

## [0.13.0] - 2022-11-01
Expand Down
5 changes: 3 additions & 2 deletions src/sqlfmt/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
# spark: "insert overwrite" without the trailing "into"
# redshift/pg: "insert into" only
# bigquery: bare "insert" is okay
r"insert(\s+overwrite)?(\s+into)?(?!\()",
r"insert(\s+overwrite)?(\s+into)?",
r"list",
r"lock",
r"merge",
Expand All @@ -286,7 +286,8 @@
r"update",
r"validate",
)
+ rf"\b({SQL_COMMENT}|{SQL_QUOTED_EXP}|[^'`\"$;])*?"
+ r"(?!\()"
+ rf"\b({SQL_COMMENT}|{SQL_QUOTED_EXP}|[^'`\"$;w])*?"
)
+ rf"{NEWLINE}*"
+ group(r";", r"$"),
Expand Down
54 changes: 54 additions & 0 deletions tests/data/unformatted/214_get_unique_attributes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{# Source: https://github.com/tconbeer/sqlfmt/issues/326 #}
{% macro get_unique_attributes(source_table, node_col) %}

{% set attribute_query %}
select
distinct x.key as attributes
from {{ source_table }} x
where startswith(x.key, '@') -- attributes get parsed as keys that start with '@'
and length(x.key) > 1 -- but keys of just '@' designate the node itself
and regexp_count(x.path, '\\[') > 1 -- we don't need the attributes from the xml root node
and not startswith(x.key, '@xmlns') -- we don't need attributed data about xml namespaces
{% endset %}

{% set results = run_query(attribute_query) %}

{% if execute %}
{% set results_list = results.columns[0].values() %}
{% else %}
{% set results_list = [] %}
{% endif %}

{% for attribute in results_list %}
, get({{ node_col }}, '{{ attribute }}')::varchar(256) as attribute_{{ dbt_utils.slugify(attribute) | replace("@", "") }}
{% endfor %}

{% endmacro %}
)))))__SQLFMT_OUTPUT__(((((
{# Source: https://github.com/tconbeer/sqlfmt/issues/326 #}
{% macro get_unique_attributes(source_table, node_col) %}

{% set attribute_query %}
select
distinct x.key as attributes
from {{ source_table }} x
where startswith(x.key, '@') -- attributes get parsed as keys that start with '@'
and length(x.key) > 1 -- but keys of just '@' designate the node itself
and regexp_count(x.path, '\\[') > 1 -- we don't need the attributes from the xml root node
and not startswith(x.key, '@xmlns') -- we don't need attributed data about xml namespaces
{% endset %}

{% set results = run_query(attribute_query) %}

{% if execute %} {% set results_list = results.columns[0].values() %}
{% else %} {% set results_list = [] %}
{% endif %}

{% for attribute in results_list %}
,
get({{ node_col }}, '{{ attribute }}')::varchar(
256
) as attribute_{{ dbt_utils.slugify(attribute) | replace("@", "") }}
{% endfor %}

{% endmacro %}
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 @@ -53,6 +53,7 @@
"unformatted/211_http_2019_cdn_17_20.sql",
"unformatted/212_http_2019_cms_14_02.sql",
"unformatted/213_gitlab_fct_sales_funnel_target.sql",
"unformatted/214_get_unique_attributes.sql",
"unformatted/300_jinjafmt.sql",
"unformatted/400_create_fn_and_select.sql",
"unformatted/401_explain_select.sql",
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def test_regex_exact_match(
(MAIN, "unterm_keyword", "MAINion"),
(MAIN, "unterm_keyword", "delete"),
(MAIN, "unsupported_ddl", "insert('abc', 1, 2, 'Z')"),
(MAIN, "unsupported_ddl", "get(foo, 'bar')"),
(JINJA, "jinja_set_block_start", "{% set foo = 'baz' %}"),
(GRANT, "unterm_keyword", "select"),
(FUNCTION, "unterm_keyword", "secure"),
Expand Down

0 comments on commit 6af6de9

Please sign in to comment.