diff --git a/CHANGELOG.md b/CHANGELOG.md index 5069bb2d3..f145262aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ## 0.7.7dev +* [Fix] Fix CTE generation when the snippets have trailing semicolons ## 0.7.6 (2023-05-29) diff --git a/src/sql/store.py b/src/sql/store.py index 2f3a35b93..f0aa7303f 100644 --- a/src/sql/store.py +++ b/src/sql/store.py @@ -105,11 +105,11 @@ def __str__(self) -> str: ` (backtick) """ with_clause_template = Template( - """WITH{% for name in with_ %} {{name}} AS ({{saved[name]._query}})\ + """WITH{% for name in with_ %} {{name}} AS ({{rts(saved[name]._query)}})\ {{ "," if not loop.last }}{% endfor %}{{query}}""" ) with_clause_template_backtick = Template( - """WITH{% for name in with_ %} `{{name}}` AS ({{saved[name]._query}})\ + """WITH{% for name in with_ %} `{{name}}` AS ({{rts(saved[name]._query)}})\ {{ "," if not loop.last }}{% endfor %}{{query}}""" ) is_use_backtick = sql.connection.Connection.current.is_use_backtick_template() @@ -118,10 +118,18 @@ def __str__(self) -> str: with_clause_template_backtick if is_use_backtick else with_clause_template ) return template.render( - query=self._query, saved=self._store._data, with_=with_all + query=self._query, + saved=self._store._data, + with_=with_all, + rts=_remove_trailing_semicolon, ) +def _remove_trailing_semicolon(query): + query_ = query.rstrip() + return query_[:-1] if query_[-1] == ";" else query + + def _get_dependencies(store, keys): """Get a list of all dependencies to reconstruct the CTEs in keys""" # get the dependencies for each key diff --git a/src/tests/test_magic_cte.py b/src/tests/test_magic_cte.py new file mode 100644 index 000000000..95e96c0fe --- /dev/null +++ b/src/tests/test_magic_cte.py @@ -0,0 +1,34 @@ +def test_trailing_semicolons_removed_from_cte(ip): + ip.run_cell( + """%%sql --save positive_x +SELECT * FROM number_table WHERE x > 0; + + +""" + ) + + ip.run_cell( + """%%sql --save positive_y +SELECT * FROM number_table WHERE y > 0; +""" + ) + + cell_execution = ip.run_cell( + """%%sql --save final --with positive_x --with positive_y +SELECT * FROM positive_x +UNION +SELECT * FROM positive_y; +""" + ) + + cell_final_query = ip.run_cell( + "%sqlrender final --with positive_x --with positive_y" + ) + + assert cell_execution.success + assert cell_final_query.result == ( + "WITH `positive_x` AS (\nSELECT * " + "FROM number_table WHERE x > 0), `positive_y` AS (\nSELECT * " + "FROM number_table WHERE y > 0)\nSELECT * FROM positive_x\n" + "UNION\nSELECT * FROM positive_y;" + )