Skip to content

Commit 40eda3f

Browse files
authored
Fix column with double quotes PrepareBatch failed.(#1216) (#1217)
1 parent e92e46a commit 40eda3f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

conn_batch.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func (c *connect) prepareBatch(ctx context.Context, query string, opts driver.Pr
4747
if len(colMatch) == 2 {
4848
columns = strings.Split(colMatch[1], ",")
4949
for i := range columns {
50-
columns[i] = strings.Trim(strings.TrimSpace(columns[i]), "`\"")
50+
// refers to https://clickhouse.com/docs/en/sql-reference/syntax#identifiers
51+
// we can use identifiers with double quotes or backticks, for example: "id", `id`, but not both, like `"id"`.
52+
columns[i] = strings.Trim(strings.Trim(strings.TrimSpace(columns[i]), "\""), "`")
5153
}
5254
}
5355
if !strings.HasSuffix(strings.TrimSpace(strings.ToUpper(query)), "VALUES") {

tests/issues/1216_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package issues
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/ClickHouse/clickhouse-go/v2"
8+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test1216(t *testing.T) {
13+
var (
14+
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
15+
"max_execution_time": 60,
16+
"allow_experimental_object_type": true,
17+
}, nil, &clickhouse.Compression{
18+
Method: clickhouse.CompressionLZ4,
19+
})
20+
)
21+
ctx := context.Background()
22+
require.NoError(t, err)
23+
const ddl = "CREATE TABLE IF NOT EXISTS test_1216 (`@id` String,`\"@id_with_quotes\"` String) Engine = Memory"
24+
require.NoError(t, conn.Exec(ctx, ddl))
25+
defer func() {
26+
conn.Exec(ctx, "DROP TABLE IF EXISTS test_1216")
27+
}()
28+
29+
_, err = conn.PrepareBatch(context.Background(), "INSERT INTO test_1216 (`@id`, `\"@id_with_quotes\"`)")
30+
require.NoError(t, err)
31+
}

0 commit comments

Comments
 (0)