Skip to content

Commit

Permalink
CrateDB: Derive from dbt-postgres. Remove PostgreSQL macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Nov 20, 2024
1 parent 040a25e commit 926a551
Show file tree
Hide file tree
Showing 25 changed files with 3 additions and 486 deletions.
1 change: 1 addition & 0 deletions dbt/adapters/cratedb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
adapter=CrateDBAdapter, # type: ignore
credentials=CrateDBCredentials,
include_path=cratedb.PACKAGE_PATH,
dependencies=["postgres"],
)
214 changes: 1 addition & 213 deletions dbt/include/cratedb/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{% endmacro %}


{# Needs an override because CrateDB does not support `CREATE TEMPORARY TABLE` #}
{% macro cratedb__create_table_as(temporary, relation, sql) -%}
{%- set unlogged = config.get('unlogged', default=false) -%}
{%- set sql_header = config.get('sql_header', none) -%}
Expand Down Expand Up @@ -39,21 +40,6 @@
);
{%- endmacro %}

{% macro cratedb__get_create_index_sql(relation, index_dict) -%}
{%- set index_config = adapter.parse_index(index_dict) -%}
{%- set comma_separated_columns = ", ".join(index_config.columns) -%}
{%- set index_name = index_config.render(relation) -%}

create {% if index_config.unique -%}
unique
{%- endif %} index if not exists
"{{ index_name }}"
on {{ relation }} {% if index_config.type -%}
using {{ index_config.type }}
{%- endif %}
({{ comma_separated_columns }})
{%- endmacro %}

{% macro cratedb__create_schema(relation) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
Expand All @@ -73,201 +59,3 @@
SELECT 1
{%- endcall -%}
{% endmacro %}

{% macro cratedb__get_columns_in_relation(relation) -%}
{% call statement('get_columns_in_relation', fetch_result=True) %}
select
column_name,
data_type,
character_maximum_length,
numeric_precision,
numeric_scale

from {{ relation.information_schema('columns') }}
where table_name = '{{ relation.identifier }}'
{% if relation.schema %}
and table_schema = '{{ relation.schema }}'
{% endif %}
order by ordinal_position

{% endcall %}
{% set table = load_result('get_columns_in_relation').table %}
{{ return(sql_convert_columns_in_relation(table)) }}
{% endmacro %}


{% macro cratedb__list_relations_without_caching(schema_relation) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
select
'{{ schema_relation.database }}' as database,
tablename as name,
schemaname as schema,
'table' as type
from pg_tables
where schemaname ilike '{{ schema_relation.schema }}'
union all
select
'{{ schema_relation.database }}' as database,
viewname as name,
schemaname as schema,
'view' as type
from pg_views
where schemaname ilike '{{ schema_relation.schema }}'
union all
select
'{{ schema_relation.database }}' as database,
matviewname as name,
schemaname as schema,
'materialized_view' as type
from pg_matviews
where schemaname ilike '{{ schema_relation.schema }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}

{% macro cratedb__information_schema_name(database) -%}
{% if database_name -%}
{{ adapter.verify_database(database_name) }}
{%- endif -%}
information_schema
{%- endmacro %}

{% macro cratedb__list_schemas(database) %}
{% if database -%}
{{ adapter.verify_database(database) }}
{%- endif -%}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) %}
select distinct nspname from pg_namespace
{% endcall %}
{{ return(load_result('list_schemas').table) }}
{% endmacro %}

{% macro cratedb__check_schema_exists(information_schema, schema) -%}
{% if information_schema.database -%}
{{ adapter.verify_database(information_schema.database) }}
{%- endif -%}
{% call statement('check_schema_exists', fetch_result=True, auto_begin=False) %}
select count(*) from pg_namespace where nspname = '{{ schema }}'
{% endcall %}
{{ return(load_result('check_schema_exists').table) }}
{% endmacro %}

{#
Postgres tables have a maximum length of 63 characters, anything longer is silently truncated.
Temp and backup relations add a lot of extra characters to the end of table names to ensure uniqueness.
To prevent this going over the character limit, the base_relation name is truncated to ensure
that name + suffix + uniquestring is < 63 characters.
#}

{% macro cratedb__make_relation_with_suffix(base_relation, suffix, dstring) %}
{% if dstring %}
{% set dt = modules.datetime.datetime.now() %}
{% set dtstring = dt.strftime("%H%M%S%f") %}
{% set suffix = suffix ~ dtstring %}
{% endif %}
{% set suffix_length = suffix|length %}
{% set relation_max_name_length = base_relation.relation_max_name_length() %}
{% if suffix_length > relation_max_name_length %}
{% do exceptions.raise_compiler_error('Relation suffix is too long (' ~ suffix_length ~ ' characters). Maximum length is ' ~ relation_max_name_length ~ ' characters.') %}
{% endif %}
{% set identifier = base_relation.identifier[:relation_max_name_length - suffix_length] ~ suffix %}

{{ return(base_relation.incorporate(path={"identifier": identifier })) }}

{% endmacro %}

{% macro cratedb__make_intermediate_relation(base_relation, suffix) %}
{{ return(cratedb__make_relation_with_suffix(base_relation, suffix, dstring=False)) }}
{% endmacro %}

{% macro cratedb__make_temp_relation(base_relation, suffix) %}
{% set temp_relation = cratedb__make_relation_with_suffix(base_relation, suffix, dstring=True) %}
{{ return(temp_relation.incorporate(path={"schema": none,
"database": none})) }}
{% endmacro %}

{% macro cratedb__make_backup_relation(base_relation, backup_relation_type, suffix) %}
{% set backup_relation = cratedb__make_relation_with_suffix(base_relation, suffix, dstring=False) %}
{{ return(backup_relation.incorporate(type=backup_relation_type)) }}
{% endmacro %}

{#
By using dollar-quoting like this, users can embed anything they want into their comments
(including nested dollar-quoting), as long as they do not use this exact dollar-quoting
label. It would be nice to just pick a new one but eventually you do have to give up.
#}
{% macro cratedb_escape_comment(comment) -%}
{% if comment is not string %}
{% do exceptions.raise_compiler_error('cannot escape a non-string: ' ~ comment) %}
{% endif %}
{%- set magic = '$dbt_comment_literal_block$' -%}
{%- if magic in comment -%}
{%- do exceptions.raise_compiler_error('The string ' ~ magic ~ ' is not allowed in comments.') -%}
{%- endif -%}
{{ magic }}{{ comment }}{{ magic }}
{%- endmacro %}


{% macro cratedb__alter_relation_comment(relation, comment) %}
{% set escaped_comment = cratedb_escape_comment(comment) %}
{% if relation.type == 'materialized_view' -%}
{% set relation_type = "materialized view" %}
{%- else -%}
{%- set relation_type = relation.type -%}
{%- endif -%}
comment on {{ relation_type }} {{ relation }} is {{ escaped_comment }};
{% endmacro %}


{% macro cratedb__alter_column_comment(relation, column_dict) %}
{% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %}
{% for column_name in column_dict if (column_name in existing_columns) %}
{% set comment = column_dict[column_name]['description'] %}
{% set escaped_comment = cratedb_escape_comment(comment) %}
comment on column {{ relation }}.{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} is {{ escaped_comment }};
{% endfor %}
{% endmacro %}

{%- macro cratedb__get_show_grant_sql(relation) -%}
select grantee, privilege_type
from {{ relation.information_schema('role_table_grants') }}
where grantor = current_role
and grantee != current_role
and table_schema = '{{ relation.schema }}'
and table_name = '{{ relation.identifier }}'
{%- endmacro -%}

{% macro copy_grants() %}
{{ return(False) }}
{% endmacro %}


{% macro cratedb__get_show_indexes_sql(relation) %}
select
i.relname as name,
m.amname as method,
ix.indisunique as "unique",
array_to_string(array_agg(a.attname), ',') as column_names
from pg_index ix
join pg_class i
on i.oid = ix.indexrelid
join pg_am m
on m.oid=i.relam
join pg_class t
on t.oid = ix.indrelid
join pg_namespace n
on n.oid = t.relnamespace
join pg_attribute a
on a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
where t.relname = '{{ relation.identifier }}'
and n.nspname = '{{ relation.schema }}'
and t.relkind in ('r', 'm')
group by 1, 2, 3
order by 1, 2, 3
{% endmacro %}


{%- macro cratedb__get_drop_index_sql(relation, index_name) -%}
drop index if exists "{{ relation.schema }}"."{{ index_name }}"
{%- endmacro -%}

This file was deleted.

25 changes: 0 additions & 25 deletions dbt/include/cratedb/macros/materializations/snapshot_merge.sql

This file was deleted.

51 changes: 0 additions & 51 deletions dbt/include/cratedb/macros/relations/materialized_view/alter.sql

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions dbt/include/cratedb/macros/relations/table/drop.sql

This file was deleted.

3 changes: 0 additions & 3 deletions dbt/include/cratedb/macros/relations/table/rename.sql

This file was deleted.

17 changes: 0 additions & 17 deletions dbt/include/cratedb/macros/relations/table/replace.sql

This file was deleted.

3 changes: 0 additions & 3 deletions dbt/include/cratedb/macros/relations/view/drop.sql

This file was deleted.

3 changes: 0 additions & 3 deletions dbt/include/cratedb/macros/relations/view/rename.sql

This file was deleted.

Loading

0 comments on commit 926a551

Please sign in to comment.