Skip to content

Commit 4401422

Browse files
committed
use object_id for columns
1 parent 5102bbb commit 4401422

File tree

3 files changed

+22
-53
lines changed

3 files changed

+22
-53
lines changed

dbt/include/sqlserver/macros/adapters/columns.sql

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,27 @@
11
{% macro sqlserver__get_columns_in_relation(relation) -%}
22
{% call statement('get_columns_in_relation', fetch_result=True) %}
33

4-
with
5-
regular_db_cols as (
6-
select
7-
ordinal_position,
8-
column_name,
9-
data_type,
10-
character_maximum_length,
11-
numeric_precision,
12-
numeric_scale
13-
from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS
14-
where table_name = '{{ relation.identifier }}'
15-
and table_schema = '{{ relation.schema }}'
16-
),
17-
18-
temp_db_cols as (
19-
select
20-
ordinal_position,
21-
column_name collate database_default as column_name,
22-
data_type collate database_default as data_type,
23-
character_maximum_length,
24-
numeric_precision,
25-
numeric_scale
26-
from tempdb.INFORMATION_SCHEMA.COLUMNS
27-
where table_name like '{{ relation.identifier }}%'
28-
),
29-
30-
all_cols as (
31-
select *
32-
from regular_db_cols
33-
union
34-
select *
35-
from temp_db_cols
36-
)
4+
with mapping as (
5+
select
6+
row_number() over (partition by object_name(c.object_id) order by c.column_id) as ordinal_position,
7+
c.name collate database_default as column_name,
8+
t.name as data_type,
9+
c.max_length as character_maximum_length,
10+
c.precision as numeric_precision,
11+
c.scale as numeric_scale
12+
from [{{ 'tempdb' if '#' in relation.identifier else relation.database }}].sys.columns c
13+
inner join sys.types t
14+
on c.user_type_id = t.user_type_id
15+
where c.object_id = object_id('{{ 'tempdb..' ~ relation.include(database=false, schema=false) if '#' in relation.identifier else relation }}')
16+
)
3717

3818
select
3919
column_name,
4020
data_type,
4121
character_maximum_length,
4222
numeric_precision,
4323
numeric_scale
44-
from
45-
all_cols
24+
from mapping
4625
order by ordinal_position
4726

4827
{% endcall %}

dbt/include/sqlserver/macros/materializations/models/table/create_table_as.sql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
{{ temp_view_sql }}
1919
');
2020

21-
-- drop current version of the table
22-
{{- sqlserver__drop_relation_script(relation) }}
23-
2421
-- select into the table and create it that way
22+
{# TempDB schema is ignored, always goes to dbo #}
2523
SELECT *
26-
INTO {{ relation.include(database=False) }}
24+
INTO {{ relation.include(database=False, schema=(not temporary)) }}
2725
FROM {{ tmp_relation }}
2826

2927
-- drop temp view

tests/functional/adapter/test_basic.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from dbt.tests.adapter.basic.files import incremental_not_schema_change_sql
23
from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
34
from dbt.tests.adapter.basic.test_base import BaseSimpleMaterializations
45
from dbt.tests.adapter.basic.test_empty import BaseEmpty
@@ -43,20 +44,11 @@ class TestIncrementalSQLServer(BaseIncremental):
4344
class TestIncrementalNotSchemaChangeSQLServer(BaseIncrementalNotSchemaChange):
4445
@pytest.fixture(scope="class")
4546
def models(self):
46-
incremental_not_schema_change_sql = """
47-
{{ config(
48-
materialized="incremental",
49-
unique_key="user_id_current_time",
50-
on_schema_change="sync_all_columns") }}
51-
select
52-
1 + '-' + current_timestamp as user_id_current_time,
53-
{% if is_incremental() %}
54-
'thisis18characters' as platform
55-
{% else %}
56-
'okthisis20characters' as platform
57-
{% endif %}
58-
"""
59-
return {"incremental_not_schema_change.sql": incremental_not_schema_change_sql}
47+
return {
48+
"incremental_not_schema_change.sql": incremental_not_schema_change_sql.replace(
49+
"||", "+"
50+
)
51+
}
6052

6153

6254
class TestGenericTestsSQLServer(BaseGenericTests):

0 commit comments

Comments
 (0)