Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prepare batch does not break on values substring in table name #1290

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions conn_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)

var splitInsertRe = regexp.MustCompile(`(?i)\sVALUES\s*\(`)
var insertMatch = regexp.MustCompile(`(?i)(INSERT\s+INTO\s+\S+(?:\s*\([^()]*(?:\([^()]*\)[^()]*)*\))?)(?:\s*VALUES)?`)
var columnMatch = regexp.MustCompile(`INSERT INTO .+\s\((?P<Columns>.+)\)$`)

func (c *connect) prepareBatch(ctx context.Context, query string, opts driver.PrepareBatchOptions, release func(*connect, error), acquire func(context.Context) (*connect, error)) (driver.Batch, error) {
Expand All @@ -41,7 +41,13 @@ func (c *connect) prepareBatch(ctx context.Context, query string, opts driver.Pr
// fmt.Printf("panic occurred on %d:\n", c.num)
// }
//}()
query = splitInsertRe.Split(query, -1)[0]
subMatches := insertMatch.FindStringSubmatch(query)
if len(subMatches) > 1 {
query = subMatches[1]
} else {
return nil, errors.New("invalid query")
}

colMatch := columnMatch.FindStringSubmatch(query)
var columns []string
if len(colMatch) == 2 {
Expand All @@ -52,9 +58,8 @@ func (c *connect) prepareBatch(ctx context.Context, query string, opts driver.Pr
columns[i] = strings.Trim(strings.Trim(strings.TrimSpace(columns[i]), "\""), "`")
}
}
if !strings.HasSuffix(strings.TrimSpace(strings.ToUpper(query)), "VALUES") {
query += " VALUES"
}
query += " VALUES"

options := queryOptions(ctx)
if deadline, ok := ctx.Deadline(); ok {
c.conn.SetDeadline(deadline)
Expand Down
89 changes: 89 additions & 0 deletions tests/issues/1280_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package issues

import (
"context"
"testing"

"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
)

func Test1280(t *testing.T) {
var (
conn, err = clickhouse_tests.GetConnection(testSet, clickhouse.Settings{
"max_execution_time": 60,
"allow_experimental_object_type": true,
}, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
)
ctx := context.Background()
require.NoError(t, err)

ddl := "CREATE TABLE values (`id` Int32, `values` Int32) Engine = Memory"
require.NoError(t, conn.Exec(ctx, ddl))
defer conn.Exec(ctx, "DROP TABLE IF EXISTS values")

testCases1 := []struct {
input string
}{
{
input: "INSERT INTO values (values)",
},
{
input: "INSERT INTO values (values) values",
},
{
input: "INSERT INTO values (`values`) values",
},
}

for i, tc := range testCases1 {
batch, err := conn.PrepareBatch(context.Background(), tc.input)
require.NoError(t, err)
appendErr := batch.Append(i)
require.NoError(t, appendErr)
err = batch.Send()
require.NoError(t, err)
}

testCases2 := []struct {
input string
}{
{
input: `
INSERT
INTO
values
(
id,
values
)`,
},
{
input: `INSERT
INTO
values
(id,
values)
values`,
},
{
input: `
INSERT
INTO
values
(id,values) values (1,2)`,
},
}

for i, tc := range testCases2 {
batch, err := conn.PrepareBatch(context.Background(), tc.input)
require.NoError(t, err)
appendErr := batch.Append(i, i)
require.NoError(t, appendErr)
err = batch.Send()
require.NoError(t, err)
}
}
Loading