Skip to content

Commit

Permalink
Merge pull request #402 from dbt-msft/nolock-per-adapter
Browse files Browse the repository at this point in the history
make nolock overridable for child adapters
  • Loading branch information
sdebruyn authored May 22, 2023
2 parents f9cbe48 + 56152bc commit d4ff3ac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/apply_grants.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
select
GRANTEE as grantee,
PRIVILEGE_TYPE as privilege_type
from INFORMATION_SCHEMA.TABLE_PRIVILEGES with (nolock)
from INFORMATION_SCHEMA.TABLE_PRIVILEGES {{ information_schema_hints() }}
where TABLE_CATALOG = '{{ relation.database }}'
and TABLE_SCHEMA = '{{ relation.schema }}'
and TABLE_NAME = '{{ relation.identifier }}'
Expand Down
4 changes: 2 additions & 2 deletions dbt/include/sqlserver/macros/adapters/columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
c.max_length as character_maximum_length,
c.precision as numeric_precision,
c.scale as numeric_scale
from [{{ 'tempdb' if '#' in relation.identifier else relation.database }}].sys.columns c with (nolock)
inner join sys.types t with (nolock)
from [{{ 'tempdb' if '#' in relation.identifier else relation.database }}].sys.columns c {{ information_schema_hints() }}
inner join sys.types t {{ information_schema_hints() }}
on c.user_type_id = t.user_type_id
where c.object_id = object_id('{{ 'tempdb..' ~ relation.include(database=false, schema=false) if '#' in relation.identifier else relation }}')
)
Expand Down
18 changes: 9 additions & 9 deletions dbt/include/sqlserver/macros/adapters/indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use [{{ relation.database }}];
if EXISTS (
SELECT *
FROM sys.indexes with (nolock)
FROM sys.indexes {{ information_schema_hints() }}
WHERE name = '{{cci_name}}'
AND object_id=object_id('{{relation_name}}')
)
Expand Down Expand Up @@ -33,8 +33,8 @@
declare @drop_xml_indexes nvarchar(max);
select @drop_xml_indexes = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes with (nolock)
inner join sys.tables with (nolock)
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and sys.indexes.type_desc = 'XML'
Expand All @@ -54,8 +54,8 @@ select @drop_xml_indexes = (
declare @drop_spatial_indexes nvarchar(max);
select @drop_spatial_indexes = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes with (nolock)
inner join sys.tables with (nolock)
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and sys.indexes.type_desc = 'Spatial'
Expand Down Expand Up @@ -119,8 +119,8 @@ select @drop_pk_constraints = (
declare @drop_remaining_indexes_last nvarchar(max);
select @drop_remaining_indexes_last = (
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
from sys.indexes with (nolock)
inner join sys.tables with (nolock)
from sys.indexes {{ information_schema_hints() }}
inner join sys.tables {{ information_schema_hints() }}
on sys.indexes.object_id = sys.tables.object_id
where sys.indexes.[name] is not null
and sys.tables.[name] = '{{ this.table }}'
Expand All @@ -137,7 +137,7 @@ select @drop_remaining_indexes_last = (
{% set idx_name = "clustered_" + local_md5(columns | join("_")) %}

if not exists(select *
from sys.indexes with (nolock)
from sys.indexes {{ information_schema_hints() }}
where name = '{{ idx_name }}'
and object_id = OBJECT_ID('{{ this }}')
)
Expand Down Expand Up @@ -170,7 +170,7 @@ end
{% endif %}

if not exists(select *
from sys.indexes with (nolock)
from sys.indexes {{ information_schema_hints() }}
where name = '{{ idx_name }}'
and object_id = OBJECT_ID('{{ this }}')
)
Expand Down
20 changes: 13 additions & 7 deletions dbt/include/sqlserver/macros/adapters/metadata.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{% macro information_schema_hints() %}
{{ return(adapter.dispatch('information_schema_hints')()) }}
{% endmacro %}

{% macro default__information_schema_hints() %}{% endmacro %}
{% macro sqlserver__information_schema_hints() %}with (nolock){% endmacro %}

{% macro sqlserver__get_catalog(information_schemas, schemas) -%}

Expand All @@ -9,7 +15,7 @@
name as principal_name,
principal_id as principal_id
from
sys.database_principals with (nolock)
sys.database_principals {{ information_schema_hints() }}
),

schemas as (
Expand All @@ -18,7 +24,7 @@
schema_id as schema_id,
principal_id as principal_id
from
sys.schemas with (nolock)
sys.schemas {{ information_schema_hints() }}
),

tables as (
Expand All @@ -28,7 +34,7 @@
principal_id as principal_id,
'BASE TABLE' as table_type
from
sys.tables with (nolock)
sys.tables {{ information_schema_hints() }}
),

tables_with_metadata as (
Expand All @@ -49,7 +55,7 @@
principal_id as principal_id,
'VIEW' as table_type
from
sys.views with (nolock)
sys.views {{ information_schema_hints() }}
),

views_with_metadata as (
Expand Down Expand Up @@ -92,7 +98,7 @@
column_name,
ordinal_position as column_index,
data_type as column_type
from INFORMATION_SCHEMA.COLUMNS with (nolock)
from INFORMATION_SCHEMA.COLUMNS {{ information_schema_hints() }}

)

Expand Down Expand Up @@ -129,7 +135,7 @@
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
USE {{ database }};
select name as [schema]
from sys.schemas with (nolock)
from sys.schemas {{ information_schema_hints() }}
{% endcall %}
{{ return(load_result('list_schemas').table) }}
{% endmacro %}
Expand All @@ -153,7 +159,7 @@
else table_type
end as table_type

from [{{ schema_relation.database }}].INFORMATION_SCHEMA.TABLES with (nolock)
from [{{ schema_relation.database }}].INFORMATION_SCHEMA.TABLES {{ information_schema_hints() }}
where table_schema = '{{ schema_relation.schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/sqlserver/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
IF EXISTS(
SELECT *
FROM sys.indexes with (nolock)
FROM sys.indexes {{ information_schema_hints() }}
WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation.schema }}.{{ to_relation.identifier }}'))
EXEC sp_rename N'{{ from_relation.schema }}.{{ to_relation.identifier }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX'
{%- endcall %}
Expand Down

0 comments on commit d4ff3ac

Please sign in to comment.