dbt-labs / dbt-utils

Utility functions for dbt projects.
https://hub.getdbt.com/dbt-labs/dbt_utils/latest/
Apache License 2.0
1.39k stars 499 forks source link

union_relations macro unable to union relations with ARRAY on Postgres #857

Open pcreux opened 11 months ago

pcreux commented 11 months ago

Describe the bug

Calling union_relations on relations with an array column generates invalid SQL:

  syntax error at or near "ARRAY"
  cast("my_array" as ARRAY) as "my_array"

Steps to reproduce

CREATE TABLE my_table(my_array int[]);
    {% set relations = [
      api.Relation.create(schema='public', identifier='my_table')
      ]
    %}
   {{ dbt_utils.union_relations(relations) }

Expected results

Valid SQL:

--- ...
cast(my_array as int[]) as my_array,
--- ...

Actual results

Invalid SQL:

--- ...
cast(my_array as ARRAY) as my_array,
--- ...

System information

The contents of your packages.yml file:

packages:
  - package: dbt-labs/dbt_utils
    version: 1.1.1

Which database are you using dbt with?

The output of dbt --version:

Core:
  - installed: 1.7.2
  - latest:    1.7.3 - Update available!

  Your version of dbt-core is out of date!
  You can find instructions for upgrading here:
  https://docs.getdbt.com/docs/installation

Plugins:
  - bigquery: 1.7.2 - Up to date!
  - postgres: 1.7.2 - Update available!

Are you interested in contributing the fix?

The code casts using dbt.type_string() or col.data_type. I don't know if there's another attribute that would return the_type[] instead of ARRAY.

rubenvereecken commented 8 months ago

Just ran into the same for a jsonb[] column.

shayansm2 commented 7 months ago

I also had the same problem

saad-saras commented 7 months ago

Has anyone figured out a solution for this? When I'm trying to union two tables with nested arrays, I'm facing the same issue

rubenvereecken commented 7 months ago

Turns out this issue has been around for at least 4 years

nstringham commented 5 months ago

I tried using column_override but I'm still getting the error.