ch-go icon indicating copy to clipboard operation
ch-go copied to clipboard

feat: improve decimal API

Open wangxuanyue opened this issue 3 years ago • 3 comments

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

wangxuanyue avatar Jun 16 '22 05:06 wangxuanyue

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!

ernado avatar Jun 16 '22 08:06 ernado

Hi, is there any progress on this or any plans to fix it in near future?

abhimanyusinghgaur avatar Feb 01 '24 14:02 abhimanyusinghgaur

Hi, I'm currently not working on this feature.

ernado avatar Feb 01 '24 16:02 ernado