Skip to content

Instantly share code, notes, and snippets.

@den-crane
Last active June 29, 2022 17:31
Show Gist options
  • Save den-crane/b8ba8a7d06bf3392c60e604e9a0c33d0 to your computer and use it in GitHub Desktop.
Save den-crane/b8ba8a7d06bf3392c60e604e9a0c33d0 to your computer and use it in GitHub Desktop.
go driver / test very long SQL

CH settings

cat /etc/clickhouse-server/users.d/max_query_size.xml
<?xml version="1.0" ?>
<yandex>
    <profiles>
        <default>
            <max_query_size>5242880</max_query_size>
        </default>
    </profiles>
</yandex>

code

package main

import (
	"context"
	"fmt"
	"log"
	"time"
  "strings"

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

func example() error {
	conn, err := clickhouse.Open(&clickhouse.Options{
		Addr: []string{"127.0.0.1:9000"},
		Auth: clickhouse.Auth{
			Database: "default",
			Username: "default",
			Password: "",
		},
		//Debug:           true,
		DialTimeout:     time.Second,
		MaxOpenConns:    10,
		MaxIdleConns:    5,
		ConnMaxLifetime: time.Hour,
		Compression: &clickhouse.Compression{
			Method: clickhouse.CompressionLZ4,
		},
	})
	if err != nil {
		return err
	}
	ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{
		"max_block_size": 10,
	}), clickhouse.WithProgress(func(p *clickhouse.Progress) {
		fmt.Println("progress: ", p)
	}), clickhouse.WithProfileInfo(func(p *clickhouse.ProfileInfo) {
		fmt.Println("profile info: ", p)
	}))
	if err := conn.Ping(ctx); err != nil {
		if exception, ok := err.(*clickhouse.Exception); ok {
			fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
		}
		return err
	}


	var qi []string

	for i := 1; i <= 150000; i++ {
	  qi = append(qi, fmt.Sprintf("( %d, '%s' )", i, "1234567890"))
	}
	q := strings.Join(qi, "," )

	sql := fmt.Sprintf("SELECT count() col1 from (select number x FROM numbers(200000)) WHERE (x, '1234567890') in ( %s )", q)

	fmt.Println("SQL length: ", len(sql))

	rows, err := conn.Query(ctx, sql)
	if err != nil {
		return err
	}
	for rows.Next() {
		var (
			col1 uint64
		)
		if err := rows.Scan(&col1); err != nil {
			return err
		}
		fmt.Printf("row: col1=%d\n", col1)
	}
	rows.Close()
	return rows.Err()
}

func main() {
	if err := example(); err != nil {
		log.Fatal(err)
	}
}

output

go run main.go
SQL length:  3638989
...
row: col1=150000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment