Skip to content

Commit 2ac4550

Browse files
authored
Fixed column schemas not being grabbed properly (dremio#282)
### Summary Fixes the column schema not being properly grabbed from query results ### Description Previously dbt was not able to get the schema of the columns interacted with in a query, these changes fix that and we are now able to get information about the columns (names and data types) ### Test Results 3 expected failures fixed 2 skipped tests that were also failing are not skipped anymore ### Changelog - [x] Added a summary of what this PR accomplishes to CHANGELOG.md ### Contributor License Agreement - [x] Please make sure you have signed our [Contributor License Agreement](https://www.dremio.com/legal/contributor-agreement/), which enables Dremio to distribute your contribution without restriction. ### Related Issue Fixes dremio#20 and also some other tests
1 parent dac8fd5 commit 2ac4550

File tree

8 files changed

+42
-5
lines changed

8 files changed

+42
-5
lines changed

.github/expected_failures.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
tests/component/test_profile_template.py::TestProfileTemplate::test_cloud_options
2-
tests/functional/adapter/dbt_clone/test_dbt_clone.py::TestCloneNotPossibleDremio::test_can_clone_false
32
tests/functional/adapter/dremio_specific/test_drop_temp_table.py::TestDropTempTableDremio::test_drop_temp_table
43
tests/functional/adapter/dremio_specific/test_schema_parsing.py::TestSchemaParsingDremio::test_schema_with_dots
54
tests/functional/adapter/dremio_specific/test_verify_ssl.py::TestVerifyCertificateDremio::test_insecure_request_warning_not_exist
65
tests/functional/adapter/unit_testing/test_unit_testing.py::TestDremioUnitTestingTypes::test_unit_test_data_type
76
tests/functional/adapter/unit_testing/test_unit_testing.py::TestDremioUnitTestCaseInsensitivity::test_case_insensitivity
87
tests/functional/adapter/unit_testing/test_unit_testing.py::TestDremioUnitTestInvalidInput::test_invalid_input
9-
tests/hooks/test_model_hooks.py::TestPrePostModelHooksOnSnapshotsDremio::test_hooks_on_snapshots
108
tests/hooks/test_model_hooks.py::TestPrePostModelHooksInConfigDremio::test_pre_and_post_model_hooks_model
119
tests/hooks/test_model_hooks.py::TestPrePostModelHooksInConfigWithCountDremio::test_pre_and_post_model_hooks_model_and_project
1210
tests/hooks/test_model_hooks.py::TestPrePostModelHooksInConfigKwargsDremio::test_pre_and_post_model_hooks_model
13-
tests/hooks/test_model_hooks.py::TestPrePostSnapshotHooksInConfigKwargsDremio::test_hooks_on_snapshots
1411
tests/hooks/test_run_hooks.py::TestPrePostRunHooksDremio::test_pre_and_post_run_hooks
1512
tests/hooks/test_run_hooks.py::TestPrePostRunHooksDremio::test_pre_and_post_seed_hooks

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
- Adds `BaseIncrementalOnSchemaChange` test to test_incremental.py
99
- Changed logic for partitioning when materializing tables. Double quoting issue has been removed, now letting the user decide the quoting
1010
- New example: `partition_by=['month("datetime_utc")']`
11+
- Fixed a bug with grabbing the column schemas from queries (names and data types)
12+
- Invisible to the users but it was the cause for some other bugs users were facing that have now been fixed
1113
## Features
1214

1315
- [#259](https://github.com/dremio/dbt-dremio/pull/259) Added support for roles in grants
1416
- [#273](https://github.com/dremio/dbt-dremio/pull/273) Fix issue with on_schema_change config
17+
- [#282](https://github.com/dremio/dbt-dremio/pull/282) Fix issue with get_column_schema_from_query
1518

1619
# dbt-dremio v1.8.1
1720

dbt/adapters/dremio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from dbt.adapters.dremio.column import DremioColumn
1516
from dbt.adapters.dremio.connections import DremioConnectionManager
1617
from dbt.adapters.dremio.credentials import DremioCredentials
1718
from dbt.adapters.dremio.impl import DremioAdapter

dbt/adapters/dremio/column.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dataclasses import dataclass
2+
3+
from dbt.adapters.base.column import Column
4+
5+
@dataclass(init=False)
6+
class DremioColumn(Column):
7+
TYPE_LABELS = {
8+
"TEXT": "VARCHAR",
9+
"STRING": "VARCHAR",
10+
}

dbt/adapters/dremio/connections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ def get_response(cls, cursor: DremioCursor) -> AdapterResponse:
174174
message = "OK" if rows == -1 else str(rows)
175175
return AdapterResponse(_message=message, rows_affected=rows)
176176

177+
@classmethod
178+
def data_type_code_to_name(cls, type_code) -> str:
179+
return type_code
180+
177181
def execute(
178182
self,
179183
sql: str,

dbt/adapters/dremio/impl.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
import agate
1616
from dbt.adapters.sql import SQLAdapter
1717
from dbt.adapters.dremio import DremioConnectionManager
18+
from dbt.adapters.dremio.column import DremioColumn
1819
from dbt.adapters.dremio.relation import DremioRelation
1920
from typing import Dict
2021

2122
from typing import List
2223
from typing import Optional
24+
from dbt.adapters.base.column import Column as BaseColumn
2325
from dbt.adapters.base.meta import available
2426
from dbt.adapters.base.relation import BaseRelation
2527

@@ -38,6 +40,7 @@
3840
class DremioAdapter(SQLAdapter):
3941
ConnectionManager = DremioConnectionManager
4042
Relation = DremioRelation
43+
Column = DremioColumn
4144

4245
_capabilities = CapabilityDict(
4346
{
@@ -157,6 +160,17 @@ def standardize_grants_dict(self, grants_table: agate.Table) -> dict:
157160
grants_dict.update({privilege: [f"{grantee_type}:{grantee}"]})
158161
return grants_dict
159162

163+
def get_column_schema_from_query(self, sql: str) -> List[BaseColumn]:
164+
"""Get a list of the Columns with names and data types from the given sql."""
165+
_, cursor = self.connections.add_select_query(sql)
166+
columns = [
167+
self.Column.create(
168+
entry['name'], self.connections.data_type_code_to_name(entry['type']['name'])
169+
)
170+
for entry in cursor.job_results()['schema']
171+
]
172+
return columns
173+
160174
# This is for use in the test suite
161175
# Need to override to add fetch to the execute method
162176
def run_sql_for_tests(self, sql, fetch, conn):

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@ limitations under the License.*/
118118
{{ return(result) }}
119119

120120
{% endmacro %}
121+
122+
{% macro dremio__get_empty_subquery_sql(select_sql, select_sql_header=none) %}
123+
{%- if select_sql_header is not none -%}
124+
{{ select_sql_header }}
125+
{%- endif -%}
126+
select * from (
127+
{{ select_sql }}
128+
) as __dbt_sbq
129+
limit 1
130+
{% endmacro %}

tests/functional/adapter/basic/test_snapshots.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from tests.utils.util import BUCKET
2323

2424

25-
@pytest.mark.skip(reason="https://github.com/dremio/dbt-dremio/issues/20")
2625
class TestSnapshotCheckColsDremio(BaseSnapshotCheckCols):
2726
@pytest.fixture(scope="class")
2827
def unique_schema(self, request, prefix) -> str:
@@ -61,7 +60,6 @@ def project_config_update(self):
6160
"name": "snapshot_strategy_check_cols",
6261
}
6362

64-
@pytest.mark.skip(reason="https://github.com/dremio/dbt-dremio/issues/20")
6563
class TestSnapshotTimestampDremio(BaseSnapshotTimestamp):
6664
@pytest.fixture(scope="class")
6765
def unique_schema(self, request, prefix) -> str:

0 commit comments

Comments
 (0)