Skip to content

AMBIGUOUS_COLUMN_NAME in union view #55803

Closed

Description

@cetra3

You have to provide the following information whenever possible.

The is an error when using a union and combining nullable and non-nullable fields.

A clear and concise description of what works not as it is supposed to.

Here's the distilled query that fails:

-- create the table
create database broken_view;

create table broken_view.table1
(
    start DateTime64(6),
    end DateTime64(6),
)
ENGINE = ReplacingMergeTree(start)
order by (start);

-- create the view

CREATE or replace VIEW broken_view.broken as 

select
  t.start as start,
  t.end as end,
  cast(datediff('second', t.start, t.end) as float) as total_sec
from broken_view.table1 t final

union all

select
  null as start,
  null as end,
  null as total_sec;

-- do a select

select
        v.start,
        v.total_sec
from
        broken_view.broken v final
where
        v.start is not null;

A link to reproducer in https://fiddle.clickhouse.com/.

https://fiddle.clickhouse.com/3a34b9c6-f36c-4ca2-956b-6de381799a00

Does it reproduce on recent release?

Yes

Enable crash reporting

No crash with ClickHouse

How to reproduce

  • Which ClickHouse server version to use - 23.9.x

See above for the playground & queries

Expected behavior

This should not return the error mentioned

Error message and/or stacktrace

Received exception from server (version 23.9.1):
Code: 352. DB::Exception: Received from localhost:9000. DB::Exception: Block structure mismatch in (columns with identical name must have identical structure) stream: different types:
start DateTime64(6) DateTime64(size = 0)
start Nullable(DateTime64(6)) Nullable(size = 0, DateTime64(size = 0), UInt8(size = 0)). (AMBIGUOUS_COLUMN_NAME)
(query: -- do a select

select
        v.start,
        v.total_sec
from
        broken_view.broken v final
where
        v.start is not null;)

Additional context

We have a view that uses unions over multiple tables. Some tables provide a value for the column, others do not

Activity

  1. added
    potential bug
    on Oct 18, 2023
  2. colmanhumphrey commented on Oct 19, 2023

    @colmanhumphrey

    Note that if you change the final WHERE clause to v.end is not null, it works fine: https://fiddle.clickhouse.com/eeda2c81-1f04-499d-9019-fb95e623a33b.

    You can also change it to v.total_sec is not null and it works (but if you replace v.start with v.end in the SELECT and also the WHERE, then it's back to broken).

    The working cases make sense according to the docs:

    Type casting is performed for unions. For example, if two queries being combined have the same field with non-Nullable and Nullable types from a compatible type, the resulting UNION has a Nullable type field.

    But yeah, not clear to us yet why the base case fails

  3. den-crane commented on Oct 19, 2023

    @den-crane
    Contributor

    It's known issue. You can name columns differently, not t.start as start but t.start as start_

    https://fiddle.clickhouse.com/a7d33161-b129-4bd4-8c73-49d80e517ac4

  4. added
    unexpected behaviour
    and removed
    potential bug
    on Oct 19, 2023
  5. Fallenyasha commented on Dec 8, 2023

    @Fallenyasha

    More easy way to reproduce:

    select 0, 0
    union all
    select cast(0 as Int16), cast(0 as Int32)

    Code: 352. DB::Exception: Block structure mismatch in (columns with identical name must have identical structure) stream: different types: 0 Int16 Int16(size = 0) 0 Int32 Int32(size = 0). (AMBIGUOUS_COLUMN_NAME) (version 23.11.1.2711 (official build))

    ClickHouse version 23.11.1.2711

  6. added a commit that references this issue on Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    unexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions