Skip to content

Commit 038f51e

Browse files
authored
Ensure that temporary tables are not accessed with database clause (#515)
* Adds missing tests from DBT for unit-tests. * Don't create temporary tables with database. * Don't use database to query temporary tables.
1 parent a89fc59 commit 038f51e

File tree

8 files changed

+83
-3
lines changed

8 files changed

+83
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### Release [1.9.4], 2025-XX-XX
22

33
#### Bugs
4-
* Fix Materialized View not dropped when a model's materialization is changed from materialized_view to view ([#516](https://github.com/ClickHouse/dbt-clickhouse/pull/516))
4+
* Fix Materialized View not dropped when a model's materialization is changed from materialized_view to view ([#516](https://github.com/ClickHouse/dbt-clickhouse/pull/516)).
5+
* Ensure that temporary tables are not accessed with database clause ([#515](https://github.com/ClickHouse/dbt-clickhouse/pull/515)).
56

67

78
### Release [1.9.3], 2025-09-08
@@ -27,6 +28,7 @@
2728
* Check for Shared database engine in can_exchange ([#460](https://github.com/ClickHouse/dbt-clickhouse/pull/460))
2829
* Tests were broken because of docker compose version `2.35` and fixed in ([#468](https://github.com/ClickHouse/dbt-clickhouse/pull/468))
2930

31+
3032
### Release [1.9.1], 2025-04-28
3133

3234
#### Bugs

dbt/adapters/clickhouse/relation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ClickHouseRelation(BaseRelation):
4343
quote_character: str = '`'
4444
can_exchange: bool = False
4545
can_on_cluster: bool = False
46+
is_temporary: bool = False
4647

4748
def __post_init__(self):
4849
if self.database != self.schema and self.database:

dbt/include/clickhouse/macros/adapters.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
{% call statement('get_columns', fetch_result=True) %}
5151
select name, type from system.columns where table = '{{ relation.identifier }}'
5252
{% if relation.schema %}
53-
and database = '{{ relation.schema }}'
53+
{% if not relation.is_temporary %}
54+
and database = '{{ relation.schema }}'
55+
{% endif %}
5456
{% else %}
5557
{% do exceptions.warn("Relations should always come with a defined schema. Missing schema for " ~ relation.identifier) %}
5658
{% endif %}

dbt/include/clickhouse/macros/materializations/table.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
{{ sql_header if sql_header is not none }}
195195

196196
{% if temporary -%}
197-
create temporary table {{ relation }}
197+
create temporary table {{ relation.identifier }}
198198
engine Memory
199199
{{ adapter.get_model_settings(model, 'Memory') }}
200200
as (
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{%- materialization unit, adapter='clickhouse' -%}
2+
3+
{% set relations = [] %}
4+
5+
{% set expected_rows = config.get('expected_rows') %}
6+
{% set expected_sql = config.get('expected_sql') %}
7+
{% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length ) > 0 else get_columns_in_query(sql) %} %}
8+
9+
{%- set target_relation = this.incorporate(type='table') -%}
10+
{%- set temp_relation = make_temp_relation(target_relation, '__dbt_tmp')-%}
11+
{%- set temp_relation = temp_relation.incorporate(is_temporary=True)-%} {# file overwrite to ensure this is marked as temporary #}
12+
{% do run_query(get_create_table_as_sql(True, temp_relation, get_empty_subquery_sql(sql))) %}
13+
{%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%}
14+
{%- set column_name_to_data_types = {} -%}
15+
{%- for column in columns_in_relation -%}
16+
{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
17+
{%- endfor -%}
18+
19+
{% if not expected_sql %}
20+
{% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types) %}
21+
{% endif %}
22+
{% set unit_test_sql = get_unit_test_sql(sql, expected_sql, tested_expected_column_names) %}
23+
24+
{% call statement('main', fetch_result=True) -%}
25+
26+
{{ unit_test_sql }}
27+
28+
{%- endcall %}
29+
30+
{% do adapter.drop_relation(temp_relation) %}
31+
32+
{{ return({'relations': relations}) }}
33+
34+
{%- endmaterialization -%}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from dbt.tests.adapter.unit_testing.test_case_insensitivity import BaseUnitTestCaseInsensivity
2+
3+
4+
class TestCaseInsensivity(BaseUnitTestCaseInsensivity):
5+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from dbt.tests.adapter.unit_testing.test_invalid_input import BaseUnitTestInvalidInput
2+
3+
4+
class TestInvalidInput(BaseUnitTestInvalidInput):
5+
pass
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
from dbt.tests.adapter.unit_testing.test_types import BaseUnitTestingTypes
3+
4+
5+
class TestingTypes(BaseUnitTestingTypes):
6+
@pytest.fixture
7+
def data_types(self):
8+
# sql_value, yaml_value
9+
return [
10+
["1", "1"],
11+
["'1'", "1"],
12+
["true", "true"],
13+
["DATE '2020-01-02'", "2020-01-02"],
14+
[
15+
"TIMESTAMP '2013-11-03 00:00:00'",
16+
"2013-11-03 00:00:00",
17+
], # Overridden: no "-0" support
18+
[
19+
"'2023-10-29 01:30:00'::DateTime('UTC')",
20+
"2013-11-03 00:00:00",
21+
], # Overridden: no "-0" support and timezones expressed differently
22+
["'1'::numeric", "1"],
23+
# Not fully supported types:
24+
# [
25+
# """'{"bar": "baz", "balance": 7.77, "active": false}'::json""",
26+
# """'{"bar": "baz", "balance": 7.77, "active": false}'""",
27+
# ],
28+
# TODO: support complex types <-- this TODO came from the DBT test.
29+
# ["ARRAY['a','b','c']", """'{"a", "b", "c"}'"""],
30+
# ["ARRAY[1,2,3]", """'{1, 2, 3}'"""],
31+
]

0 commit comments

Comments
 (0)