Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added temporal indexes #547

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2024-01-07 (5.25.0)

* Added temporal indexes

# 2024-01-06 (5.24.1)

* Fix bug in exporter where a loop through dataset tables was prematurely
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = amsterdam-schema-tools
version = 5.24.1
version = 5.25.0
url = https://github.com/amsterdam/schema-tools
license = Mozilla Public 2.0
author = Team Data Diensten, van het Dataplatform onder de Directie Digitale Voorzieningen (Gemeente Amsterdam)
Expand Down
31 changes: 31 additions & 0 deletions src/schematools/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ def index_factory(
_build_identifier_index(table, dataset_table, db_table_name),
*_build_fk_indexes(table, dataset_table, db_table_name),
*_build_geo_indexes(table, dataset_table, db_table_name),
*_build_temporal_indexes(table, dataset_table, db_table_name),
]

through_indexes = _build_m2m_indexes(
Expand Down Expand Up @@ -407,6 +408,36 @@ def _build_geo_indexes(
]


def _build_temporal_indexes(
barrydaniels-nl marked this conversation as resolved.
Show resolved Hide resolved
table_object: Table, dataset_table: DatasetTableSchema, db_table_name: str
) -> list[Index]:
"""Creates an index on temporal fields."""
if dataset_table._temporal_range_field_ids:
barrydaniels-nl marked this conversation as resolved.
Show resolved Hide resolved
fields = []
if dataset_table._temporal_range_field_ids:
for field in dataset_table._temporal_range_field_ids:
barrydaniels-nl marked this conversation as resolved.
Show resolved Hide resolved
try:
fields.append(table_object.c[to_snake_case(field)])
except KeyError:
logger.warning(
"Field '%s' not found...skipping temporal index creation", field
)
return []

combined_index = Index(
_format_index_name(f"{db_table_name}_temporal{TABLE_INDEX_POSTFIX}"), *fields
)
fields_index = [
Index(
_format_index_name(f"{db_table_name}_{field.db_name}{TABLE_INDEX_POSTFIX}"),
table_object.c[field.db_name],
)
for field in dataset_table.temporal.temporal_fields
]
return [combined_index, *fields_index]
return []


def _build_m2m_indexes(
metadata: MetaData,
dataset_table: DatasetTableSchema,
Expand Down
5 changes: 5 additions & 0 deletions src/schematools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,11 @@ class Temporal:
identifier_field: DatasetFieldSchema
dimensions: dict[str, TemporalDimensionFields] = dataclasses.field(default_factory=dict)

@cached_property
def temporal_fields(self) -> list[DatasetFieldSchema]:
"""Return the fields that are required for this temporal property."""
return [field for fields in self.dimensions.values() for field in fields]


def _normalize_scopes(auth: None | str | list | tuple) -> frozenset[str]:
"""Make sure the auth field has a consistent type."""
Expand Down
Loading