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

Incorrect column list parsing for multiline INSERT statements #1375

Closed
6 tasks done
Fiery-Fenix opened this issue Aug 16, 2024 · 1 comment
Closed
6 tasks done

Incorrect column list parsing for multiline INSERT statements #1375

Fiery-Fenix opened this issue Aug 16, 2024 · 1 comment

Comments

@Fiery-Fenix
Copy link
Contributor

Observed

If in Batch operation INSERT statement defined as multiline go string - insert will failed with error clickhouse [Append]: clickhouse: expected N arguments, got M
This happened because of incorrect Regex used in extractNormalizedInsertQueryAndColumns which not honor newlines in statement. Because of that Batch operation uses list of columns from ClickHouse table instead of list of columns in INSERT statement and batch fails with error
Fix: #1373

Expected behavior

Correct insert without errors

Code example

package code

package main

import (
	"context"
	"database/sql"
	"fmt"

	_ "github.com/ClickHouse/clickhouse-go/v2"
)

func main() {
	dsn := "http://127.0.0.1:8123?dial_timeout=10s"

	conn, err := sql.Open("clickhouse", dsn)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer conn.Close()

	ctx := context.Background()
	if _, err := conn.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS test (col1 String, col2 String, col3 String) ENGINE = MergeTree() ORDER BY tuple()"); err != nil {
		fmt.Println(err)
		return
	}

	tx, err := conn.BeginTx(ctx, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer tx.Rollback()

	sql := `INSERT INTO test (
		col1,
		col2
	) VALUES (
	 	?,
		?
	)`
	stmt, err := tx.PrepareContext(ctx, sql)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer stmt.Close()

	_, err = stmt.ExecContext(ctx, "test", "test")
	if err != nil {
		fmt.Println(err)
		return
	}

	err = tx.Commit()
	if err != nil {
		fmt.Println(err)
		return
	}
}

Error log

clickhouse [Append]:  clickhouse: expected 3 arguments, got 2

Details

Environment

  • clickhouse-go version: v2.27.1
  • Interface: database/sql compatible driver
  • Go version: 1.22.6
  • Operating system: Ubuntu 22.04
  • ClickHouse version: 24.6.2.17
  • Is it a ClickHouse Cloud? No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants