tconbeer / sqlfmt

sqlfmt formats your dbt SQL files so you don't have to
https://sqlfmt.com
Apache License 2.0
402 stars 16 forks source link

Broken sql with quote generating macro #476

Closed a0th closed 1 year ago

a0th commented 1 year ago

Describe the bug The order of IF statements is altered in the example below. Im not sure what is causing it.

To Reproduce

The code below:

{% macro to_set(list) %}
  {% set output = [] %}
  {% for item in list %}
    {% if item not in output %}
      {% do output.append(item) %}
    {% endif %}
  {% endfor %}
  {{ return(output) }}
{% endmacro %}

{% macro convert_tz(date, from_tz, to_tz, from_tz_is_str=false, to_tz_is_str=false) %}
    {% if from_tz_is_str %} {% set from_tz = "'" + from_tz + "'" %} {% endif %}
    {% if to_tz_is_str %} {% set to_tz = "'" + to_tz + "'" %} {% endif %}

    {% if to_tz == "UTC" %} datetime(timestamp(date, {{ from_tz }}))
    {% else %} datetime(timestamp({{ date }}, {{ from_tz }}), {{ to_tz }})
    {% endif %}
{% endmacro %}

Once formatted turns into this:

{% macro to_set(list) %}
    {% set output = [] %}
    {% for item in list %}
        {% if item not in output %} {% do output.append(item) %} {% endif %}
    {% endfor %}
    {{ return(output) }}
{% endmacro %}

{% macro convert_tz(date, from_tz, to_tz, from_tz_is_str=false, to_tz_is_str=false) %}
    {% if from_tz_is_str %} {% set from_tz = "'" + from_tz + "'" %} {% endif %}
    {% if to_tz == "UTC" %} datetime(timestamp(date, {{ from_tz }}))
    {% if to_tz_is_str %} {% set to_tz = "'" + to_tz + "'" %} {% endif %}
    {% else %} datetime(timestamp({{ date }}, {{ from_tz }}), {{ to_tz }})

    {% endif %}
{% endmacro %}

The first portion defining the to_set macro is fine, but the second macro, convert_tz, is broken. Notice the second IF in the original macro is if to_tz_is_str, it then turns into the third IF instead. It also removes the endif

Moreover, if we remove the to_set macro, the formatter does nothing.

Additional context

What is the output of sqlfmt --version? sqlfmt, version 0.19.0

tconbeer commented 1 year ago

Can you run pip list for me? I'm wondering if you may have installed the package sqlfmt, instead of this one (shandy-sqlfmt). I can't reproduce on sqlfmt.com

a0th commented 1 year ago

Youre right, I cant reproduce this on the site, nor using the CLI. I'm using dbt power user extension on VSCode to format the code, so thats probably where the issue is.

I'll close this and #477 , thank you for the help, appreciate it.

This is the pip list:

Package                  Version
------------------------ --------
agate                    1.6.3
annotated-types          0.5.0
appdirs                  1.4.4
attrs                    23.1.0
Babel                    2.12.1
black                    23.3.0
cachetools               5.3.1
certifi                  2023.5.7
cffi                     1.15.1
chardet                  5.1.0
charset-normalizer       3.1.0
click                    8.1.3
colorama                 0.4.6
dbt-bigquery             1.5.1
dbt-core                 1.5.3
dbt-extractor            0.4.1
diff-cover               7.6.0
exceptiongroup           1.1.2
future                   0.18.3
google-api-core          2.11.1
google-auth              2.20.0
google-cloud-bigquery    3.11.1
google-cloud-core        2.3.2
google-cloud-dataproc    5.4.1
google-cloud-storage     2.9.0
google-crc32c            1.5.0
google-resumable-media   2.5.0
googleapis-common-protos 1.59.1
grpc-google-iam-v1       0.12.6
grpcio                   1.54.2
grpcio-status            1.54.2
hologram                 0.0.16
idna                     3.4
iniconfig                2.0.0
isodate                  0.6.1
Jinja2                   3.1.2
jinja2-simple-tags       0.5.0
jsonschema               4.17.3
leather                  0.3.4
Logbook                  1.5.3
markdown-it-py           3.0.0
MarkupSafe               2.1.3
mashumaro                3.6
mdurl                    0.1.2
minimal-snowplow-tracker 0.0.2
msgpack                  1.0.5
mypy-extensions          1.0.0
networkx                 2.8.8
packaging                23.1
parsedatetime            2.4
pathspec                 0.11.1
pip                      23.1.2
platformdirs             3.5.3
pluggy                   1.2.0
proto-plus               1.22.2
protobuf                 4.23.3
pyasn1                   0.5.0
pyasn1-modules           0.3.0
pycparser                2.21
pydantic                 2.0.2
pydantic_core            2.1.2
Pygments                 2.15.1
pyrsistent               0.19.3
pytest                   7.4.0
python-dateutil          2.8.2
python-slugify           8.0.1
pytimeparse              1.1.8
pytz                     2023.3
PyYAML                   6.0
regex                    2023.6.3
requests                 2.31.0
rich                     13.4.2
rsa                      4.9
ruamel.yaml              0.17.32
ruamel.yaml.clib         0.2.7
setuptools               67.8.0
shandy-sqlfmt            0.19.0
six                      1.16.0
sqlfluff                 2.1.2
sqlfluff-templater-dbt   2.1.2
sqlparse                 0.4.3
tblib                    2.0.0
text-unidecode           1.3
toml                     0.10.2
tomli                    2.0.1
tqdm                     4.65.0
typing_extensions        4.6.3
urllib3                  1.26.16
Werkzeug                 2.3.6
wheel                    0.38.4