|
| 1 | +/* |
| 2 | + * Override for starrocks__olap_table to guard against empty PROPERTIES dicts. |
| 3 | + * |
| 4 | + * The upstream adapter checks `properties is not none`, but an empty dict {} |
| 5 | + * (set via +properties: {} in dbt_project.yml to clear inherited Iceberg/Trino |
| 6 | + * properties) is not None and causes StarRocks to reject the DDL with |
| 7 | + * "No viable statement for input 'PROPERTIES ( )'". |
| 8 | + */ |
| 9 | + |
| 10 | +{% macro starrocks__olap_table(is_create_table_as) -%} |
| 11 | + |
| 12 | + {%- set is_create_table = is_create_table_as is none or not is_create_table_as -%} |
| 13 | + |
| 14 | + {%- set table_type = config.get('table_type', 'DUPLICATE') -%} |
| 15 | + {%- set keys = config.get('keys') -%} |
| 16 | + {%- set partition_by = config.get('partition_by') -%} |
| 17 | + {%- set partition_by_init = config.get('partition_by_init') -%} |
| 18 | + {%- set order_by = config.get('order_by') -%} |
| 19 | + {%- set partition_type = config.get('partition_type', 'RANGE') -%} |
| 20 | + {%- set buckets = config.get('buckets') -%} |
| 21 | + {%- set distributed_by = config.get('distributed_by') -%} |
| 22 | + {%- set properties = config.get('properties') -%} |
| 23 | + {%- set materialized = config.get('materialized', none) -%} |
| 24 | + {%- set unique_key = config.get('unique_key', none) -%} |
| 25 | + |
| 26 | + {%- if materialized == 'incremental' and unique_key is not none -%} |
| 27 | + {%- set table_type = 'PRIMARY' -%} |
| 28 | + {%- set keys = unique_key if unique_key is sequence and unique_key is not mapping and unique_key is not string else [unique_key] -%} |
| 29 | + {%- endif -%} |
| 30 | + |
| 31 | + {# 1. SET ENGINE #} |
| 32 | + {%- if is_create_table %} ENGINE = OLAP {% endif -%} |
| 33 | + |
| 34 | + {# 2. SET KEYS #} |
| 35 | + {%- if keys is not none -%} |
| 36 | + {%- if table_type == "DUPLICATE" %} |
| 37 | + DUPLICATE KEY ( |
| 38 | + {%- for item in keys -%} |
| 39 | + {{ item }} {%- if not loop.last -%}, {%- endif -%} |
| 40 | + {%- endfor -%} |
| 41 | + ) |
| 42 | + {%- elif table_type == "PRIMARY" %} |
| 43 | + PRIMARY KEY ( |
| 44 | + {%- for item in keys -%} |
| 45 | + {{ item }} {%- if not loop.last -%}, {%- endif -%} |
| 46 | + {%- endfor -%} |
| 47 | + ) |
| 48 | + {%- elif table_type == "UNIQUE" %} |
| 49 | + UNIQUE KEY ( |
| 50 | + {%- for item in keys -%} |
| 51 | + {{ item }} {%- if not loop.last -%}, {%- endif -%} |
| 52 | + {%- endfor -%} |
| 53 | + ) |
| 54 | + {%- else -%} |
| 55 | + {%- set msg -%} |
| 56 | + "{{ table_type }}" is not support |
| 57 | + {%- endset -%} |
| 58 | + {{ exceptions.raise_compiler_error(msg) }} |
| 59 | + {%- endif -%} |
| 60 | + {%- else -%} |
| 61 | + {%- if table_type != "DUPLICATE" -%} |
| 62 | + {%- set msg -%} |
| 63 | + "{{ table_type }}" is must set "keys" |
| 64 | + {%- endset -%} |
| 65 | + {{ exceptions.raise_compiler_error(msg) }} |
| 66 | + {%- endif -%} |
| 67 | + {% endif -%} |
| 68 | + |
| 69 | + {# 3. SET PARTITION #} |
| 70 | + {% if partition_by is not none -%} |
| 71 | + {{ starrocks__partition_by(partition_type, partition_by, partition_by_init) }} |
| 72 | + {%- endif %} |
| 73 | + |
| 74 | + {# 4. SET DISTRIBUTED #} |
| 75 | + {%- if distributed_by is not none %} |
| 76 | + DISTRIBUTED BY HASH ( |
| 77 | + {%- for item in distributed_by -%} |
| 78 | + {{ item }} {%- if not loop.last -%}, {%- endif -%} |
| 79 | + {%- endfor -%} |
| 80 | + ) |
| 81 | + {%- if buckets is not none -%} |
| 82 | + BUCKETS {{ buckets }} |
| 83 | + {%- elif adapter.is_before_version("2.5.7") -%} |
| 84 | + {%- set msg -%} |
| 85 | + [buckets] must set before version 2.5.7, current version is {{ adapter.current_version() }} |
| 86 | + {%- endset -%} |
| 87 | + {{ exceptions.raise_compiler_error(msg) }} |
| 88 | + {%- endif -%} |
| 89 | + {%- elif adapter.is_before_version("3.1.0") -%} |
| 90 | + {%- set msg -%} |
| 91 | + [distributed_by] must set before version 3.1, current version is {{ adapter.current_version() }} |
| 92 | + {%- endset -%} |
| 93 | + {{ exceptions.raise_compiler_error(msg) }} |
| 94 | + {% endif -%} |
| 95 | + |
| 96 | + {# 5. SET ORDER BY #} |
| 97 | + {% if order_by is not none %} |
| 98 | + ORDER BY ( |
| 99 | + {%- for item in order_by -%} |
| 100 | + {{ item }} {%- if not loop.last -%}, {%- endif -%} |
| 101 | + {%- endfor -%} |
| 102 | + ) |
| 103 | + {% endif %} |
| 104 | + |
| 105 | + {# 6. SET PROPERTIES — skip if None or empty dict #} |
| 106 | + {% if properties is not none and properties | length > 0 %} |
| 107 | + PROPERTIES ( |
| 108 | + {% for key, value in properties.items() -%} |
| 109 | + "{{ key }}" = "{{ value }}" |
| 110 | + {%- if not loop.last -%}, |
| 111 | + {% endif -%} |
| 112 | + {%- endfor %} |
| 113 | + ) |
| 114 | + {% endif %} |
| 115 | +{%- endmacro %} |
0 commit comments