dbt-utils icon indicating copy to clipboard operation
dbt-utils copied to clipboard

union_relations macro unable to union relations with ARRAY on Postgres

Open pcreux opened this issue 1 year ago • 5 comments

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 a table with an array of integers or strings:
CREATE TABLE my_table(my_array int[]);
  • Perform an union_relations on that table:
    {% 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?

  • [x] postgres

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.

pcreux avatar Dec 06 '23 10:12 pcreux

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

rubenvereecken avatar Mar 26 '24 14:03 rubenvereecken

I also had the same problem

shayansm2 avatar Mar 31 '24 21:03 shayansm2

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

saad-saras avatar Apr 04 '24 17:04 saad-saras

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

  • #202

rubenvereecken avatar Apr 09 '24 19:04 rubenvereecken

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

nstringham avatar May 30 '24 14:05 nstringham