-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] indexing fixes for hypertables
- Loading branch information
Showing
5 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, Set | ||
|
||
import agate | ||
|
||
from dbt.adapters.contracts.relation import RelationConfig | ||
from dbt.adapters.postgres.relation import PostgresRelation | ||
from dbt.adapters.postgres.relation_configs import ( | ||
PostgresIndexConfig, | ||
PostgresIndexConfigChange, | ||
) | ||
from dbt.adapters.relation_configs import ( | ||
RelationResults, | ||
) | ||
|
||
|
||
@dataclass(frozen=True, eq=False, repr=False) | ||
class TimescaleDBRelation(PostgresRelation): | ||
def get_hypertable_index_changes( | ||
self, relation_results: RelationResults, relation_config: RelationConfig | ||
) -> Optional[Set[PostgresIndexConfigChange]]: | ||
if not relation_results: | ||
return None | ||
|
||
index_rows: agate.Table = relation_results.get("indexes", agate.Table(rows={})) | ||
index_dicts = [PostgresIndexConfig.parse_relation_results(index) for index in index_rows.rows] | ||
index_list = [PostgresIndexConfig.from_dict(index) for index in index_dicts] | ||
filtered_list = [ | ||
index | ||
for index in index_list | ||
if not ( | ||
not index.unique | ||
and index.method == "btree" | ||
and len(index.column_names) == 1 | ||
and index.name.endswith("_idx") | ||
) | ||
] | ||
index_set = frozenset(filtered_list) | ||
|
||
indexes_from_config = relation_config.config.get("indexes", []) | ||
parsed_from_config = [PostgresIndexConfig.parse_model_node(index) for index in indexes_from_config] | ||
set_from_config = frozenset(parsed_from_config) | ||
|
||
return self._get_index_config_changes(index_set, set_from_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/functional/adapter/virtual_hypertable/test_virtual_hypertable_index_updates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from dbt.tests.fixtures.project import TestProjInfo | ||
from dbt.tests.util import run_dbt | ||
|
||
|
||
class TestVirtualHypertableIndexUpdates: | ||
@pytest.fixture(scope="class") | ||
def models(self) -> dict[str, Any]: | ||
return { | ||
"vht.sql": """ | ||
{% if var("add_index", false) %} | ||
{{ config(indexes=[{'columns': ['col_1'], 'type': 'hash'}]) }} | ||
{% endif %} | ||
-- | ||
""" | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self) -> dict[str, Any]: | ||
return { | ||
"name": "virtual_hypertable_tests", | ||
"models": {"virtual_hypertable_tests": {"vht": {"+materialized": "virtual_hypertable"}}}, | ||
} | ||
|
||
def find_indexes(self, project: TestProjInfo, unique_schema: str) -> list[str]: | ||
indexes = project.run_sql( | ||
f""" | ||
select * | ||
from pg_indexes | ||
where tablename = 'vht' | ||
and schemaname = '{unique_schema}'""", | ||
fetch="all", | ||
) | ||
table_names = [job[2] for job in indexes] | ||
return table_names | ||
|
||
def test_virtual_hypertable_index_updates(self, project: TestProjInfo, unique_schema: str) -> None: | ||
project.run_sql(f""" | ||
create table {unique_schema}.vht (time_column timestamp, col_1 int); | ||
select create_hypertable('{unique_schema}.vht', by_range('time_column'));""") | ||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
assert len(self.find_indexes(project, unique_schema)) == 1 | ||
|
||
run_enable_results = run_dbt(["run", "--vars", "add_index: true"]) | ||
assert len(run_enable_results) == 1 | ||
assert len(self.find_indexes(project, unique_schema)) == 2 | ||
|
||
run_disable_results = run_dbt(["run"]) | ||
assert len(run_disable_results) == 1 | ||
assert len(self.find_indexes(project, unique_schema)) == 1 | ||
|
||
run_enable_results = run_dbt(["run", "--vars", "add_index: true"]) | ||
assert len(run_enable_results) == 1 | ||
assert len(self.find_indexes(project, unique_schema)) == 2 | ||
|
||
run_disable_results = run_dbt(["run"]) | ||
assert len(run_disable_results) == 1 | ||
assert len(self.find_indexes(project, unique_schema)) == 1 |