Skip to content

Query Parameters not working in INSERT statement #1703

@hermanschaaf

Description

@hermanschaaf

Observed

An INSERT INTO query using query parameters, like INSERT INTO test_table ({col:Identifier}) ..., fails with an error as if the placeholder is replaced with an empty string, rather than the given value:

panic: code: 62, message: Syntax error: failed at position 41 ()): ) SELECT {str:String} FROM system.numbers LIMIT 100.

Expected behaviour

It should replace the query parameter with an identifier specified by the provided parameters.

Code example

func InsertQueryWithParameters() {
	dsn := env.GetString("CLICKHOUSE_DSN", "clickhouse://user:pass@localhost:9000")
	copts, err := clickhouse.ParseDSN(dsn)
	if err != nil {
		panic(err)
	}
	conn, err := clickhouse.Open(copts)
	if err != nil {
		panic(err)
	}

	chCtx := clickhouse.Context(context.Background(), clickhouse.WithParameters(clickhouse.Parameters{
		"str": "hello",
		"col": "val",
	}))
	err = conn.Exec(chCtx, "CREATE TABLE IF NOT EXISTS test_table (val String) ENGINE = Memory")
	if err != nil {
		panic(err)
	}

	// Works:
	err = conn.Exec(chCtx, "INSERT INTO test_table (val) SELECT {str:String} FROM system.numbers LIMIT 100")
	if err != nil {
		panic(err)
	}

	// Doesn't work:
	err = conn.Exec(chCtx, "INSERT INTO test_table ({col:Identifier}) SELECT {str:String} FROM system.numbers LIMIT 100")
	if err != nil {
		panic(err)
	}
	return
}

func main() {
	InsertQueryWithParameters()
}

Error log

This is the output of the example program above:

panic: code: 62, message: Syntax error: failed at position 41 ()): ) SELECT {str:String} FROM system.numbers LIMIT 100. Expected one of: token sequence, Dot, token

Details

Environment

  • clickhouse-go version: v2.40.3
  • Interface: ClickHouse API / database/sql compatible driver
  • Go version: go version go1.25.3 darwin/arm64
  • Operating system: MacOS
  • ClickHouse version: 25.6.13
  • Is it a ClickHouse Cloud? No
  • ClickHouse Server non-default settings, if any: N/A
  • CREATE TABLE statements for tables involved: See example code
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions