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 indexes + index creation macro #755

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions macros/commands/create_indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% macro get_existing_indexes(relation) %}
{% set existing_indexes = [] %}
{% set existing_indexes_table = elementary.run_query(get_show_indexes_sql(relation)) %}
{% for index in elementary.agate_to_dicts(existing_indexes_table) %}
{% do existing_indexes.append({
"name": index.name,
"method": index.method,
"unique": index.unique,
"columns": index.column_names.split(","),
}) %}
{% endfor %}
{% do return(existing_indexes) %}
{% endmacro %}

{% macro does_index_exist(index, existing_indexes) %}
{% for existing_index in existing_indexes %}
{% if index.columns | sort == existing_index.columns | sort %}
{% do return(true) %}
{% endif %}
{% endfor %}
{% do return(false) %}
{% endmacro %}

{% macro get_index_creation_query(table_nodes, existing_indexes) %}
{% set all_queries = [] %}
{% for table_node in table_nodes %}
{% set relation = api.Relation.create(table_node.database, table_node.schema, table_node.alias) %}
{% set indexes = table_node.config.get("indexes", []) %}
{% for _index_dict in indexes %}
{% if not elementary.does_index_exist(_index_dict, existing_indexes) %}
{% do all_queries.append(get_create_index_sql(relation, _index_dict)) %}
{% endif %}
{% endfor %}
{% endfor %}
{% do return(";\n".join(all_queries)) %}
{% endmacro %}

{% macro create_elementary_indexes() %}
{% if target.type != 'postgres' %}
{% do exceptions.raise_compiler_error('Indexes only supported for postgres') %}
{% endif %}
{% set table_materializations = ["table", "incremental"] %}
{% set table_nodes = graph.nodes.values() | selectattr('package_name', '==', 'elementary') | selectattr('config.materialized', 'in', table_materializations) %}
{% for table_node in table_nodes %}
{% set relation = api.Relation.create(table_node.database, table_node.schema, table_node.alias) %}
{% set existing_indexes = elementary.get_existing_indexes(relation) %}
{% set index_creation_query = elementary.get_index_creation_query([table_node], existing_indexes) %}
{% if index_creation_query %}
{% do run_query(index_creation_query) %}
{% endif %}
{% endfor %}
{% endmacro %}
2 changes: 1 addition & 1 deletion models/edr/run_results/test_result_rows.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
materialized = 'incremental',
unique_key = 'elementary_test_results_id',
on_schema_change = 'append_new_columns',
indexes=[{'columns': ['created_at']}] if target.type == "postgres" else [],
indexes=[{'columns': ['created_at']}, {'columns': ['elementary_test_results_id']}] if target.type == "postgres" else [],
full_refresh=elementary.get_config_var('elementary_full_refresh'),
meta={
"timestamp_column": "created_at",
Expand Down
Loading