Skip to content

Commit 8cbc6a2

Browse files
iyurochjkaflik
andauthored
Ignore materialized and alias cols infered during HTTP prepare batch (#1214)
* ignore materialized and alias cols during instantiation of the batch insert we construct all the columns, there is no need to do it for materialized and alias columns as they are non insertable * fix indent * add test case * Update and rename mat_cols_test.go to materialized_column_test.go --------- Co-authored-by: Kuba Kaflik <kuba.kaflik@clickhouse.com>
1 parent 7e2272c commit 8cbc6a2

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

conn_http_batch.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ func (h *httpConnect) prepareBatch(ctx context.Context, query string, opts drive
6363
var colNames []string
6464
for r.Next() {
6565
var (
66-
colName string
67-
colType string
68-
ignore string
66+
colName string
67+
colType string
68+
default_type string
69+
ignore string
6970
)
7071

71-
if err = r.Scan(&colName, &colType, &ignore, &ignore, &ignore, &ignore, &ignore); err != nil {
72+
if err = r.Scan(&colName, &colType, &default_type, &ignore, &ignore, &ignore, &ignore); err != nil {
7273
return nil, err
7374
}
75+
// these column types cannot be specified in INSERT queries
76+
if default_type == "MATERIALIZED" || default_type == "ALIAS" {
77+
continue
78+
}
7479
colNames = append(colNames, colName)
7580
columns[colName] = colType
7681
}

tests/std/materialized_column_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Licensed to ClickHouse, Inc. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. ClickHouse, Inc. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package std
19+
20+
import (
21+
"fmt"
22+
"math/big"
23+
"strconv"
24+
"testing"
25+
26+
"github.com/ClickHouse/clickhouse-go/v2"
27+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
28+
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func TestMaterializedColumnInsert(t *testing.T) {
33+
dsns := map[string]clickhouse.Protocol{"Native": clickhouse.Native, "Http": clickhouse.HTTP}
34+
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
35+
require.NoError(t, err)
36+
for name, protocol := range dsns {
37+
t.Run(fmt.Sprintf("%s Protocol", name), func(t *testing.T) {
38+
if conn, err := GetStdDSNConnection(protocol, useSSL, nil); assert.NoError(t, err) {
39+
if !CheckMinServerVersion(conn, 21, 12, 0) {
40+
t.Skip(fmt.Errorf("unsupported clickhouse version"))
41+
return
42+
}
43+
const ddl = `
44+
CREATE TABLE test_mat_cols (
45+
Col1 Int128
46+
, Col2 MATERIALIZED Col1 * 2
47+
) Engine MergeTree() ORDER BY tuple()
48+
`
49+
defer func() {
50+
conn.Exec("DROP TABLE test_mat_cols")
51+
}()
52+
_, err := conn.Exec(ddl)
53+
require.NoError(t, err)
54+
scope, err := conn.Begin()
55+
require.NoError(t, err)
56+
batch, err := scope.Prepare("INSERT INTO test_mat_cols")
57+
require.NoError(t, err)
58+
var (
59+
col1Data = big.NewInt(128)
60+
col2Data = big.NewInt(128 * 2)
61+
)
62+
_, err = batch.Exec(col1Data)
63+
require.NoError(t, err)
64+
require.NoError(t, scope.Commit())
65+
var (
66+
col1 big.Int
67+
col2 big.Int
68+
)
69+
require.NoError(t, conn.QueryRow("SELECT * FROM test_mat_cols").Scan(&col1))
70+
assert.Equal(t, *col1Data, col1)
71+
require.NoError(t, conn.QueryRow("SELECT Col1, Col2 from test_mat_cols").Scan(&col1, &col2))
72+
assert.Equal(t, *col1Data, col1)
73+
assert.Equal(t, *col2Data, col2)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)