Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/dbt-labs/dbt-adapters into …
Browse files Browse the repository at this point in the history
…mcknight/ct-2819
  • Loading branch information
McKnight-42 committed Jul 8, 2024
2 parents 3c922d4 + 7e9731c commit 8d8bad6
Show file tree
Hide file tree
Showing 29 changed files with 262 additions and 101 deletions.
6 changes: 6 additions & 0 deletions .changes/1.3.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## dbt-adapters 1.3.2 - July 02, 2024

### Under the Hood

* Fix query timer resolution
* Add optional release_connection parameter to connection_named method
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240702-161935.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix scenario where using the `--empty` flag causes metadata queries to contain limit clauses
time: 2024-07-02T16:19:35.457997-04:00
custom:
Author: mikealfare
Issue: "213"
6 changes: 0 additions & 6 deletions .changes/unreleased/Under the Hood-20240621-150837.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions .changes/unreleased/Under the Hood-20240624-161108.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20240624-231955.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Under the Hood
body: --limit flag no longer subshells the query. This resolves the dbt Cloud experience
issue where limit prevents ordering elements..
time: 2024-06-24T23:19:55.611142-07:00
custom:
Author: versusfacit
Issue: "207"
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,13 @@ cython_debug/

# PyCharm
.idea/

# MacOS
.DS_Store

# vscode
.vscode/
.venv/

# Vim
*.swp
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ and is generated by [Changie](https://github.com/miniscruff/changie).

* Update Clone test to reflect core change removing `deferred` attribute from nodes

## dbt-adapters 1.3.2 - July 02, 2024

### Under the Hood

* Fix query timer resolution
* Add optional release_connection parameter to connection_named method

## dbt-adapters 1.3.1 - June 20, 2024

## dbt-adapters 1.3.0 - June 18, 2024
Expand Down
17 changes: 17 additions & 0 deletions dbt-tests-adapter/dbt/tests/adapter/dbt_show/test_dbt_show.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from dbt_common.exceptions import DbtRuntimeError
from dbt.tests.adapter.dbt_show import fixtures
from dbt.tests.util import run_dbt

Expand Down Expand Up @@ -47,9 +48,25 @@ def test_sql_header(self, project):
run_dbt(["show", "--select", "sql_header", "--vars", "timezone: Asia/Kolkata"])


class BaseShowDoesNotHandleDoubleLimit:
"""see issue: https://github.com/dbt-labs/dbt-adapters/issues/207"""

DATABASE_ERROR_MESSAGE = 'syntax error at or near "limit"'

def test_double_limit_throws_syntax_error(self, project):
with pytest.raises(DbtRuntimeError) as e:
run_dbt(["show", "--limit", "1", "--inline", "select 1 limit 1"])

assert self.DATABASE_ERROR_MESSAGE in str(e)


class TestPostgresShowSqlHeader(BaseShowSqlHeader):
pass


class TestPostgresShowLimit(BaseShowLimit):
pass


class TestShowDoesNotHandleDoubleLimit(BaseShowDoesNotHandleDoubleLimit):
pass
111 changes: 111 additions & 0 deletions dbt-tests-adapter/dbt/tests/adapter/empty/_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
model_input_sql = """
select 1 as id
"""

ephemeral_model_input_sql = """
{{ config(materialized='ephemeral') }}
select 2 as id
"""

raw_source_csv = """id
3
"""


model_sql = """
select *
from {{ ref('model_input') }}
union all
select *
from {{ ref('ephemeral_model_input') }}
union all
select *
from {{ source('seed_sources', 'raw_source') }}
"""


model_inline_sql = """
select * from {{ source('seed_sources', 'raw_source') }} as raw_source
"""

schema_sources_yml = """
sources:
- name: seed_sources
schema: "{{ target.schema }}"
tables:
- name: raw_source
"""


SEED = """
my_id,my_value
1,a
2,b
3,c
""".strip()


SCHEMA = """
version: 2
seeds:
- name: my_seed
description: "This is my_seed"
columns:
- name: id
description: "This is my_seed.my_id"
"""

CONTROL = """
select * from {{ ref("my_seed") }}
"""


GET_COLUMNS_IN_RELATION = """
{{ config(materialized="table") }}
{% set columns = adapter.get_columns_in_relation(ref("my_seed")) %}
select * from {{ ref("my_seed") }}
"""


ALTER_COLUMN_TYPE = """
{{ config(materialized="table") }}
{{ alter_column_type(ref("my_seed"), "MY_VALUE", "varchar") }}
select * from {{ ref("my_seed") }}
"""


ALTER_RELATION_COMMENT = """
{{ config(
materialized="table",
persist_docs={"relations": True},
) }}
select * from {{ ref("my_seed") }}
"""


ALTER_COLUMN_COMMENT = """
{{ config(
materialized="table",
persist_docs={"columns": True},
) }}
select * from {{ ref("my_seed") }}
"""


ALTER_RELATION_ADD_REMOVE_COLUMNS = """
{{ config(materialized="table") }}
{% set my_seed = adapter.Relation.create(this.database, this.schema, "my_seed", "table") %}
{% set my_column = api.Column("my_column", "varchar") %}
{% do alter_relation_add_remove_columns(my_seed, [my_column], none) %}
{% do alter_relation_add_remove_columns(my_seed, none, [my_column]) %}
select * from {{ ref("my_seed") }}
"""


TRUNCATE_RELATION = """
{{ config(materialized="table") }}
{% set my_seed = adapter.Relation.create(this.database, this.schema, "my_seed", "table") %}
{{ truncate_relation(my_seed) }}
select * from {{ ref("my_seed") }}
"""
99 changes: 52 additions & 47 deletions dbt-tests-adapter/dbt/tests/adapter/empty/test_empty.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,23 @@
import pytest

from dbt.tests.util import relation_from_name, run_dbt
import pytest


model_input_sql = """
select 1 as id
"""

ephemeral_model_input_sql = """
{{ config(materialized='ephemeral') }}
select 2 as id
"""

raw_source_csv = """id
3
"""


model_sql = """
select *
from {{ ref('model_input') }}
union all
select *
from {{ ref('ephemeral_model_input') }}
union all
select *
from {{ source('seed_sources', 'raw_source') }}
"""


schema_sources_yml = """
sources:
- name: seed_sources
schema: "{{ target.schema }}"
tables:
- name: raw_source
"""
from dbt.tests.adapter.empty import _models


class BaseTestEmpty:
@pytest.fixture(scope="class")
def seeds(self):
return {
"raw_source.csv": raw_source_csv,
"raw_source.csv": _models.raw_source_csv,
}

@pytest.fixture(scope="class")
def models(self):
return {
"model_input.sql": model_input_sql,
"ephemeral_model_input.sql": ephemeral_model_input_sql,
"model.sql": model_sql,
"sources.yml": schema_sources_yml,
"model_input.sql": _models.model_input_sql,
"ephemeral_model_input.sql": _models.ephemeral_model_input_sql,
"model.sql": _models.model_sql,
"sources.yml": _models.schema_sources_yml,
}

def assert_row_count(self, project, relation_name: str, expected_row_count: int):
Expand All @@ -75,13 +41,9 @@ def test_run_with_empty(self, project):
class BaseTestEmptyInlineSourceRef(BaseTestEmpty):
@pytest.fixture(scope="class")
def models(self):
model_sql = """
select * from {{ source('seed_sources', 'raw_source') }} as raw_source
"""

return {
"model.sql": model_sql,
"sources.yml": schema_sources_yml,
"model.sql": _models.model_inline_sql,
"sources.yml": _models.schema_sources_yml,
}

def test_run_with_empty(self, project):
Expand All @@ -92,4 +54,47 @@ def test_run_with_empty(self, project):


class TestEmpty(BaseTestEmpty):
"""
Though we don't create these classes anymore, we need to keep this one in case an adapter wanted to import the test as-is to automatically run it.
We should consider adding a deprecation warning that suggests moving this into the concrete adapter and importing `BaseTestEmpty` instead.
"""

pass


class MetadataWithEmptyFlag:
@pytest.fixture(scope="class")
def seeds(self):
return {"my_seed.csv": _models.SEED}

@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": _models.SCHEMA,
"control.sql": _models.CONTROL,
"get_columns_in_relation.sql": _models.GET_COLUMNS_IN_RELATION,
"alter_column_type.sql": _models.ALTER_COLUMN_TYPE,
"alter_relation_comment.sql": _models.ALTER_RELATION_COMMENT,
"alter_column_comment.sql": _models.ALTER_COLUMN_COMMENT,
"alter_relation_add_remove_columns.sql": _models.ALTER_RELATION_ADD_REMOVE_COLUMNS,
"truncate_relation.sql": _models.TRUNCATE_RELATION,
}

@pytest.fixture(scope="class", autouse=True)
def setup(self, project):
run_dbt(["seed"])

@pytest.mark.parametrize(
"model",
[
"control",
"get_columns_in_relation",
"alter_column_type",
"alter_relation_comment",
"alter_column_comment",
"alter_relation_add_remove_columns",
"truncate_relation",
],
)
def test_run(self, project, model):
run_dbt(["run", "--empty", "--select", model])
2 changes: 1 addition & 1 deletion dbt/adapters/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.3.1"
version = "1.3.2"
8 changes: 4 additions & 4 deletions dbt/include/global_project/macros/adapters/apply_grants.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
{% endmacro %}

{% macro default__get_show_grant_sql(relation) %}
show grants on {{ relation }}
show grants on {{ relation.render() }}
{% endmacro %}


Expand All @@ -70,7 +70,7 @@
{% endmacro %}

{%- macro default__get_grant_sql(relation, privilege, grantees) -%}
grant {{ privilege }} on {{ relation }} to {{ grantees | join(', ') }}
grant {{ privilege }} on {{ relation.render() }} to {{ grantees | join(', ') }}
{%- endmacro -%}


Expand All @@ -79,7 +79,7 @@
{% endmacro %}

{%- macro default__get_revoke_sql(relation, privilege, grantees) -%}
revoke {{ privilege }} on {{ relation }} from {{ grantees | join(', ') }}
revoke {{ privilege }} on {{ relation.render() }} from {{ grantees | join(', ') }}
{%- endmacro -%}


Expand Down Expand Up @@ -147,7 +147,7 @@
{% set needs_granting = diff_of_two_dicts(grant_config, current_grants_dict) %}
{% set needs_revoking = diff_of_two_dicts(current_grants_dict, grant_config) %}
{% if not (needs_granting or needs_revoking) %}
{{ log('On ' ~ relation ~': All grants are in place, no revocation or granting needed.')}}
{{ log('On ' ~ relation.render() ~': All grants are in place, no revocation or granting needed.')}}
{% endif %}
{% else %}
{#-- We don't think there's any chance of previous grants having carried over. --#}
Expand Down
Loading

0 comments on commit 8d8bad6

Please sign in to comment.