Skip to content

Commit

Permalink
Addressed issue #249,#240,#238,#233,#180,#168,#186,#52
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed Dec 23, 2024
1 parent dd918ed commit 3725fcb
Show file tree
Hide file tree
Showing 17 changed files with 740 additions and 969 deletions.
2 changes: 1 addition & 1 deletion dbt/adapters/fabric/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.8.8"
version = "1.8.9"
3 changes: 3 additions & 0 deletions dbt/adapters/fabric/fabric_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def get_rows_different_sql(
names = sorted((self.quote(n) for n in column_names))
columns_csv = ", ".join(names)

if columns_csv == "":
columns_csv = "*"

sql = COLUMNS_EQUAL_SQL.format(
columns=columns_csv,
relation_a=str(relation_a),
Expand Down
7 changes: 4 additions & 3 deletions dbt/adapters/fabric/fabric_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def open(cls, connection: Connection) -> Connection:
con_str.append(f"SERVER={credentials.host}")

con_str.append(f"Database={credentials.database}")
con_str.append("Pooling=true")

# Enabling trace flag
if credentials.trace_flag:
Expand Down Expand Up @@ -395,8 +396,8 @@ def open(cls, connection: Connection) -> Connection:
con_str.append(f"APP={application_name}")

try:
if int(credentials.retries) > 0:
con_str.append(f"ConnectRetryCount={credentials.retries}")
con_str.append("ConnectRetryCount=3")
con_str.append("ConnectRetryInterval=10")

except Exception as e:
logger.debug(
Expand Down Expand Up @@ -427,7 +428,7 @@ def open(cls, connection: Connection) -> Connection:

def connect():
logger.debug(f"Using connection string: {con_str_display}")

pyodbc.pooling = True
if credentials.authentication == "ActiveDirectoryAccessToken":
attrs_before = get_pyodbc_attrs_before_accesstoken(credentials.access_token)
else:
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/fabric/fabric_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FabricCredentials(Credentials):
authentication: Optional[str] = "ActiveDirectoryServicePrincipal"
encrypt: Optional[bool] = True # default value in MS ODBC Driver 18 as well
trust_cert: Optional[bool] = False # default value in MS ODBC Driver 18 as well
retries: int = 1
retries: int = 3
schema_authorization: Optional[str] = None
login_timeout: Optional[int] = 0
query_timeout: Optional[int] = 0
Expand Down
260 changes: 135 additions & 125 deletions dbt/include/fabric/macros/adapters/catalog.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% macro fabric__get_catalog(information_schemas, schemas) -%}

{% set query_label = apply_label() %}
{%- call statement('catalog', fetch_result=True) -%}

Expand Down Expand Up @@ -126,144 +127,153 @@
{%- endmacro %}

{% macro fabric__get_catalog_relations(information_schema, relations) -%}

{% set query_label = apply_label() %}
{%- call statement('catalog', fetch_result=True) -%}
{%- set distinct_databases = relations | map(attribute='database') | unique | list -%}

with
principals as (
select
name as principal_name,
principal_id as principal_id
from
sys.database_principals {{ information_schema_hints() }}
),
{%- if distinct_databases | length == 1 -%}
{%- call statement('catalog', fetch_result=True) -%}
{{ get_use_database_sql(distinct_databases[0]) }}
with
principals as (
select
name as principal_name,
principal_id as principal_id
from
sys.database_principals {{ information_schema_hints() }}
),

schemas as (
select
name as schema_name,
schema_id as schema_id,
principal_id as principal_id
from
sys.schemas {{ information_schema_hints() }}
),
schemas as (
select
name as schema_name,
schema_id as schema_id,
principal_id as principal_id
from
sys.schemas {{ information_schema_hints() }}
),

tables as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'BASE TABLE' as table_type
from
sys.tables {{ information_schema_hints() }}
),
tables as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'BASE TABLE' as table_type
from
sys.tables {{ information_schema_hints() }}
),

tables_with_metadata as (
select
object_id,
table_name,
schema_name,
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
table_type
from
tables
join schemas on tables.schema_id = schemas.schema_id
),
tables_with_metadata as (
select
object_id,
table_name,
schema_name,
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
table_type
from
tables
join schemas on tables.schema_id = schemas.schema_id
),

views as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'VIEW' as table_type
from
sys.views {{ information_schema_hints() }}
),
views as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'VIEW' as table_type
from
sys.views {{ information_schema_hints() }}
),

views_with_metadata as (
select
object_id,
table_name,
schema_name,
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
table_type
from
views
join schemas on views.schema_id = schemas.schema_id
),
views_with_metadata as (
select
object_id,
table_name,
schema_name,
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
table_type
from
views
join schemas on views.schema_id = schemas.schema_id
),

tables_and_views as (
select
object_id,
table_name,
schema_name,
principal_name,
table_type
from
tables_with_metadata
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
union all
select
object_id,
table_name,
schema_name,
principal_name,
table_type
from
views_with_metadata
join principals on views_with_metadata.owner_principal_id = principals.principal_id
),
tables_and_views as (
select
object_id,
table_name,
schema_name,
principal_name,
table_type
from
tables_with_metadata
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
union all
select
object_id,
table_name,
schema_name,
principal_name,
table_type
from
views_with_metadata
join principals on views_with_metadata.owner_principal_id = principals.principal_id
),

cols as (
cols as (

select
c.object_id,
c.name as column_name,
c.column_id as column_index,
t.name as column_type
from sys.columns as c {{ information_schema_hints() }}
left join sys.types as t on c.system_type_id = t.system_type_id
)
select
c.object_id,
c.name as column_name,
c.column_id as column_index,
t.name as column_type
from sys.columns as c {{ information_schema_hints() }}
left join sys.types as t on c.system_type_id = t.system_type_id
)

select
DB_NAME() as table_database,
tv.schema_name as table_schema,
tv.table_name,
tv.table_type,
null as table_comment,
tv.principal_name as table_owner,
cols.column_name,
cols.column_index,
cols.column_type,
null as column_comment
from tables_and_views tv
join cols on tv.object_id = cols.object_id
where (
{%- for relation in relations -%}
{% if relation.schema and relation.identifier %}
(
upper(tv.schema_name) = upper('{{ relation.schema }}')
and upper(tv.table_name) = upper('{{ relation.identifier }}')
)
{% elif relation.schema %}
(
upper(tv.schema_name) = upper('{{ relation.schema }}')
)
{% else %}
{% do exceptions.raise_compiler_error(
'`get_catalog_relations` requires a list of relations, each with a schema'
) %}
{% endif %}
select
DB_NAME() as table_database,
tv.schema_name as table_schema,
tv.table_name,
tv.table_type,
null as table_comment,
tv.principal_name as table_owner,
cols.column_name,
cols.column_index,
cols.column_type,
null as column_comment
from tables_and_views tv
join cols on tv.object_id = cols.object_id
where (
{%- for relation in relations -%}
{% if relation.schema and relation.identifier %}
(
upper(tv.schema_name) = upper('{{ relation.schema }}')
and upper(tv.table_name) = upper('{{ relation.identifier }}')
)
{% elif relation.schema %}
(
upper(tv.schema_name) = upper('{{ relation.schema }}')
)
{% else %}
{% do exceptions.raise_compiler_error(
'`get_catalog_relations` requires a list of relations, each with a schema'
) %}
{% endif %}

{%- if not loop.last %} or {% endif -%}
{%- endfor -%}
)
{%- if not loop.last %} or {% endif -%}
{%- endfor -%}
)

order by column_index
{{ query_label }}
{%- endcall -%}
order by column_index
{{ query_label }}

{{ return(load_result('catalog').table) }}
{%- endcall -%}
{{ return(load_result('catalog').table) }}
{% else %}
{% do exceptions.raise_compiler_error(
'`get_catalog_relations` can catalog one database at a time'
) %}
{% endif %}

{%- endmacro %}
3 changes: 2 additions & 1 deletion dbt/include/fabric/macros/adapters/metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
{% endmacro %}

{%- macro fabric__get_use_database_sql(database) -%}
USE [{{database}}];
USE [{{database | replace('"', '')}}];
{%- endmacro -%}

{% macro fabric__list_schemas(database) %}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
{{ get_use_database_sql(database) }}
select name as [schema]
from sys.schemas {{ information_schema_hints() }} {{ apply_label() }}
{% endcall %}
Expand Down
3 changes: 2 additions & 1 deletion dbt/include/fabric/macros/adapters/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{%- endfor %}

{% call statement('drop_schema') -%}
EXEC('DROP SCHEMA IF EXISTS {{ relation.schema }}')
{{ get_use_database_sql(relation.database) }}
EXEC('DROP SCHEMA IF EXISTS {{ relation.schema }}')
{% endcall %}
{% endmacro %}
36 changes: 28 additions & 8 deletions dbt/include/fabric/macros/materializations/tests/helpers.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
{% macro fabric__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
{% macro get_fabric_test_sql(database, schema, main_sql, fail_calc, warn_if, error_if, limit) -%}

With dbt_internal_test AS (
{{ main_sql }}
)
{% set testview %}
[{{ schema }}.testview_{{ range(1300, 19000) | random }}]
{% endset %}

{% set sql = main_sql.replace("'", "''")%}
{{ get_use_database_sql(database) }}
EXEC('create view {{testview}} as {{ sql }};')
select
COUNT(*) AS failures,
CASE WHEN COUNT(*) != 0 THEN 'true' ELSE 'false' END AS should_warn,
CASE WHEN COUNT(*) != 0 THEN 'true' ELSE 'false' END AS should_error
FROM dbt_internal_test
{{ fail_calc }} as failures,
case when {{ fail_calc }} {{ warn_if }}
then 'true' else 'false' end as should_warn,
case when {{ fail_calc }} {{ error_if }}
then 'true' else 'false' end as should_error
from (
select {{ "top (" ~ limit ~ ')' if limit != none }} * from {{testview}}
) dbt_internal_test;

{{ get_use_database_sql(database) }}
EXEC('drop view {{testview}};')
{%- endmacro %}

{% macro fabric__generate_schema_name(custom_schema_name, node) -%}

{%- set default_schema = target.schema -%}
{%- if custom_schema_name is none -%}
{{ default_schema }}
{%- else -%}
{{ custom_schema_name | trim }}
{%- endif -%}
{%- endmacro %}
Loading

0 comments on commit 3725fcb

Please sign in to comment.