Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/223 relationship test with limit #245

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#### Bug Fix
- Fixed an issue where Materialize Views would break with a custom schema. Thanks to [Rory Sawyer](https://github.com/SoryRawyer)
for the PR!
- A few tests with LIMIT clause were broken due to parsing error when having settings in the query ([issue](https://github.com/ClickHouse/dbt-clickhouse/issues/223)). We added a dedicated limit placer, that takes into account the settings section (using a comment flag `-- settings_section` within the query).

### Release [1.7.1], 2023-12-13
#### Bug Fixes
Expand Down
19 changes: 17 additions & 2 deletions dbt/adapters/clickhouse/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,30 @@ def get_model_settings(self, model):
res = []
for key in settings:
res.append(f' {key}={settings[key]}')
return '' if len(res) == 0 else 'SETTINGS ' + ', '.join(res) + '\n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's always add the -- end_of_sql marker comment regardless of whether we have actual settings to add? That way we can check for it in other circumstances where we need to do stuff after the "main" sql.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to add that at the end of every macro?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be the same macros where we add settings now, just always add -- end_of_sql instead of conditionally.

I think the settings thing is fairly unique to ClickHouse so I feel like being able to consistently determine "this is the end of regular SQL" from "this might be ClickHouse doing nonstandard stuff" will lead to more straightforward code, even if we don't actually add the settings. Note that there are so many possible query and table settings that we'll probably end up with them most of the time anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a new PR in #249

if len(res) == 0:
return ''
else:
settings_str = 'SETTINGS ' + ', '.join(res) + '\n'
return f"""
-- end_of_sql
{settings_str}
"""

@available
def get_model_query_settings(self, model):
settings = model['config'].get('query_settings', {})
res = []
for key in settings:
res.append(f' {key}={settings[key]}')
return '' if len(res) == 0 else 'SETTINGS ' + ', '.join(res) + '\n'

if len(res) == 0:
return ''
else:
settings_str = 'SETTINGS ' + ', '.join(res) + '\n'
return f"""
-- settings_section
{settings_str}
"""

@available.parse_none
def get_column_schema_from_query(self, sql: str, *_) -> List[ClickHouseColumn]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ left join parent
on child.from_field = parent.to_field

where parent.to_field is null
-- end_of_sql
settings join_use_nulls = 1

{% endmacro %}
30 changes: 30 additions & 0 deletions dbt/include/clickhouse/macros/utils/utils.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
{% macro clickhouse__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
{% set main_sql_formatted = clickhouse__place_limit(main_sql, limit) if limit !=None else main_sql%}
select
{{ fail_calc }} as failures,
{{ fail_calc }} {{ warn_if }} as should_warn,
{{ fail_calc }} {{ error_if }} as should_error
from (
{{ main_sql_formatted }}
) dbt_internal_test

{%- endmacro %}


-- This macro is designed to add a LIMIT clause to a ClickHouse SQL query while preserving any ClickHouse settings specified in the query.
-- When multiple queries are nested, the limit will be attached to the outer query
{% macro clickhouse__place_limit(query, limit) -%}
{% if 'settings' in query.lower()%}
{% if '-- end_of_sql' not in query.lower()%}
{{exceptions.raise_compiler_error("-- end_of_sql must be set when using ClickHouse settings")}}
{% endif %}
{% set split_by_settings_sections = query.split("-- end_of_sql")%}
{% set split_by_settings_sections_with_limit = split_by_settings_sections[-2] + "\n LIMIT " + limit|string + "\n" %}
{% set query_with_limit = "-- end_of_sql".join(split_by_settings_sections[:-2] + [split_by_settings_sections_with_limit, split_by_settings_sections[-1]])%}
{{query_with_limit}}
{% else %}
{{query}}
{{"limit " ~ limit}}
{% endif %}
{%- endmacro %}

{% macro clickhouse__any_value(expression) -%}
any({{ expression }})
{%- endmacro %}
Expand Down
Loading