forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhouse_decimal_test.go
97 lines (93 loc) · 2.4 KB
/
clickhouse_decimal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package clickhouse_test
import (
"database/sql"
"github.com/stretchr/testify/assert"
"testing"
)
func Test_Decimal(t *testing.T) {
const (
ddl = `
CREATE TABLE clickhouse_test_nullable (
decimal Decimal(18,5),
decimalNullable Nullable(Decimal(15,3))
) Engine=Memory;
`
dml = `
INSERT INTO clickhouse_test_nullable (
decimal,
decimalNullable
) VALUES (
?,
?
)
`
query = `
SELECT
decimal,
decimalNullable
FROM clickhouse_test_nullable
`
)
if connect, err := sql.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true"); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if _, err := connect.Exec("DROP TABLE IF EXISTS clickhouse_test_nullable"); assert.NoError(t, err) {
if _, err := tx.Exec(ddl); assert.NoError(t, err) {
if tx, err := connect.Begin(); assert.NoError(t, err) {
if stmt, err := tx.Prepare(dml); assert.NoError(t, err) {
for i := 0; i < 10; i++ {
if _, err := stmt.Exec(
16.55,
nil,
); !assert.NoError(t, err) {
t.Fatal(err)
}
}
}
if err := tx.Commit(); !assert.NoError(t, err) {
t.Fatal(err)
}
}
if rows, err := connect.Query(query); assert.NoError(t, err) {
columnTypes, err := rows.ColumnTypes()
assert.NoError(t, err)
for i, column := range columnTypes {
switch i {
case 0:
nullable, nullableOk := column.Nullable()
assert.False(t, nullable)
assert.True(t, nullableOk)
precision, scale, ok := column.DecimalSize()
assert.Equal(t, int64(5), scale)
assert.Equal(t, int64(18), precision)
assert.True(t, ok)
case 1:
nullable, nullableOk := column.Nullable()
assert.True(t, nullable)
assert.True(t, nullableOk)
precision, scale, ok := column.DecimalSize()
assert.Equal(t, int64(3), scale)
assert.Equal(t, int64(15), precision)
assert.True(t, ok)
}
}
for rows.Next() {
var (
decimal = new(int)
decimalNullable = new(int)
)
if err := rows.Scan(
&decimal,
&decimalNullable,
); assert.NoError(t, err) {
if assert.NotNil(t, decimal) {
assert.Equal(t, int(1655000), *decimal)
}
assert.Nil(t, decimalNullable)
}
}
}
}
}
}
}
}