Protryon / klickhouse

Rust crate for accessing Clickhouse

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default expression does not evaluate with native inserts

mnpw opened this issue · comments

Hey, thanks for working on this crate. I've run into an issue where DEFAULT columns are not getting set properly.

To reproduce the issue, you can follow these steps:

  1. Create test table
create table test (id UInt32, id_bump DEFAULT id+1) ENGINE = MergeTree() ORDER BY id;
  1. Insert values
// main.rs

#[derive(Row, Debug, Default)]
pub struct Test {
    id: u32,
}

// glue code

#[tokio::main]
async fn main() {
    // init client
    
    let row = Test {
        id: 1,
    };
    
    client
        .insert_native_block("INSERT INTO test FORMAT native", vec![row])
        .await
        .unwrap();
 }

On querying the table, I see that the default expression (id+1) set for column id_bump is not being honored. Any idea why this may be?

Observed:

SELECT *
FROM test

┌─id─┬─id_bump─┐
│  10 │
└────┴─────────┘

Expected:

SELECT *
FROM test

┌─id─┬─id_bump─┐
│  12 │
└────┴─────────┘

cc: @tekjar

This error isn't about klickhouse crate, but I believe you need to set the columns you want to insert explicitly for defaults to be honored.

INSERT INTO test (id) FORMAT native

Otherwise Clickhouse is going to assume you're inserting all columns, and maybe it's defaulting it out to zero during decoding since the column is missing.