ch-go
ch-go copied to clipboard
feat: improve decimal API
I found a problem when I tested the code. Is there something wrong?
CREATE TABLE default.dev
(
test Decimal32(2)
)
ENGINE = MergeTree()
ORDER BY tuple();
var data proto.ColDecimal32
data.Append(1)
if err := c.Do(ctx, ch.Query{
Body: "INSERT INTO default.dev VALUES",
Input: []proto.InputColumn{
{
Name: "test",
Data: data,
},
},
}); err != nil {
panic(err)
}
run error:
panic: handle packet: NUMBER_OF_ARGUMENTS_DOESNT_MATCH (42): DB::Exception: Decimal data type family must have exactly two arguments: precision and scale
ColDecimal32/ColDecimal64/ColDecimal128 the same
I've managed to workaround this with proto.Alias and help of documentation:
func Test(t *testing.T) {
conn := Conn(t)
createTable := Query{
Body: "CREATE TABLE test_table (v Decimal32(2)) ENGINE = Memory",
}
require.NoError(t, conn.Do(ctx, createTable), "create table")
var data proto.ColDecimal32
data.Append(1234)
data.Append(5678)
insertQuery := Query{
Body: "INSERT INTO test_table VALUES",
Input: []proto.InputColumn{
{Name: "v", Data: proto.Alias(&data, "Decimal(9, 2)")},
},
}
require.NoError(t, conn.Do(ctx, insertQuery), "insert")
var gotData proto.ColDecimal32
selectData := Query{
Body: "SELECT * FROM test_table",
Result: proto.Results{
{Name: "v", Data: proto.Alias(&gotData, "Decimal(9, 2)")},
},
}
require.NoError(t, conn.Do(ctx, selectData), "select")
require.Equal(t, data.Rows(), gotData.Rows())
for i := 0; i < data.Rows(); i++ {
require.Equal(t, data.Row(i), gotData.Row(i))
}
}
Looks like ClickHouse always uses the Decimal(P, S) form of column type.
Nothing wrong with your code, decimals API is currently incomplete and should improve soon. Sorry for inconvenience and thank you for your report!
Hi, is there any progress on this or any plans to fix it in near future?
Hi, I'm currently not working on this feature.