Skip to content

Exception: Number of columns doesn't match while inserting with UNION ALL into AggregatingMergeTree table #8030

Closed

Description

@odaykhovskaya

I get an exception when trying to insert data into table.
It looks like floating bug, or I just cannot understand why this is happening.

Code: 20. DB::Exception: Received from host:port. DB::Exception: Number of columns doesn't match.

Table schema:

create table test_table on cluster test_cluster
(
  category          Enum8('ONE' = 1, 'TWO' = 2)        Codec(T64, LZ4),
  direction         Enum8('OUT' = 1, 'IN' = 2)             Codec(T64, LZ4),
  field1            FixedString(50)                               Codec(LZ4),
  field2            FixedString(50)                               Codec(LZ4),
  field3            FixedString(50)                               Codec(LZ4),
  field4            FixedString(50)                               Codec(LZ4),
  cnt               AggregateFunction(sum, UInt32)   Codec(LZ4),
  min_ts            AggregateFunction(min, UInt64)     Codec(LZ4),
  max_ts            AggregateFunction(max, UInt64)    Codec(LZ4)
)
engine = AggregatingMergeTree()
primary key (category, direction, field1, field2, field3, field4)
order by (category, direction, field1, field2, field3, field4)
partition by bitAnd(sipHash64(field1), 100);

Insert request which causes the bug:

INSERT INTO test_table SELECT * FROM (
(SELECT 'ONE','OUT','79853108939','35338606180662','250016171930158','79853108939',sumState(toUInt32(1)),minState(toUInt64(1519851774000)),maxState(toUInt64(1519851774000)))
UNION ALL
(SELECT 'ONE','IN','79155050277','35915207655548','250016113029618','123456',sumState(toUInt32(1)),minState(toUInt64(1519851669000)),maxState(toUInt64(1519851669000))))

Though this works perfectly (the same request, but with different data - changed one field in the first row to SOME_OTHER_VALUE)

INSERT INTO test_table SELECT * FROM (
(SELECT 'ONE','OUT','79853108939','35338606180662','250016171930158','SOME_OTHER_VALUE',sumState(toUInt32(1)),minState(toUInt64(1519851774000)),maxState(toUInt64(1519851774000)))
UNION ALL
(SELECT 'ONE','IN','79155050277','35915207655548','250016113029618','123456',sumState(toUInt32(1)),minState(toUInt64(1519851669000)),maxState(toUInt64(1519851669000))))

ClickHouse server version 19.17.2
It happens in CLI and with Flink Connector

Activity

  1. added
    bug
    on Dec 4, 2019
  2. den-crane commented on Dec 6, 2019

    @den-crane
    Contributor

    Workarounds (copy from Telegram):

    1. Insert through Null table https://gist.github.com/den-crane/f7382cd4f1f859ff6ac46afe7dc9925a

    2. SimpleAggregateFunction

    create table test_table 
    (  x Int64,
      cnt   UInt32,
      min_ts SimpleAggregateFunction(min, UInt64)     Codec(LZ4),
      max_ts SimpleAggregateFunction(max, UInt64)    Codec(LZ4))
    engine = AggregatingMergeTree() order by x;
    
    insert into test_table values (1,1,1,1);
    insert into test_table values (1,1,2,2);
    
    optimize table test_table final;
    select * from test_table;
    ┌─x─┬─cnt─┬─min_ts─┬─max_ts─┐
    │ 1 │   1 │      1 │      2 │
    └───┴─────┴────────┴────────┘
    
    1. input function
    create table mv_z_store(a date, max_b AggregateFunction(MAX,Int64)) 
    ENGINE = AggregatingMergeTree Partition by toYYYYMM(a) order by a;
    
    INSERT INTO mv_z_store SELECT a, maxState(b) FROM input('a Date, b Int64') group by a format CSV "2019-12-05",1
    INSERT INTO mv_z_store SELECT a, maxState(b) FROM input('a Date, b Int64') group by a format Values ('2019-12-05',1);
    
  3. alexey-milovidov commented on Nov 2, 2020

    @alexey-milovidov
    Member
    milovidov-desktop :) DESCRIBE (SELECT 1, 1 UNION ALL SELECT 1, 2)
    
    DESCRIBE TABLE 
    (
        SELECT
            1,
            1
        UNION ALL
        SELECT
            1,
            2
    )
    
    Query id: ce6051b7-9a51-4d3d-a4fd-39db75047958
    
    
    Received exception from server (version 20.11.1):
    Code: 44. DB::Exception: Received from localhost:9000. DB::Exception: Cannot add column 1: column with this name already exists.
    
  4. added
    unexpected behaviour
    and removed
    bug
    on Nov 2, 2020
  5. added a commit that references this issue on Apr 10, 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

    sql-compatibilityunexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions