Skip to content

Commit

Permalink
fix panic in accessing settings in clickhouse driver (#6290)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anshul authored Dec 17, 2024
1 parent a0c96fd commit 8e7bca1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion runtime/drivers/clickhouse/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func connFromContext(ctx context.Context) *sqlx.Conn {
// This is used to use certain session aware features like temporary tables.
func (c *connection) sessionAwareContext(ctx context.Context) context.Context {
if c.opts.Protocol == clickhouse.HTTP {
settings := maps.Clone(c.opts.Settings)
var settings map[string]any
if len(c.opts.Settings) == 0 {
settings = make(map[string]any)
} else {
settings = maps.Clone(c.opts.Settings)
}
settings["session_id"] = uuid.New().String()
return clickhouse.Context(ctx, clickhouse.WithSettings(settings))
}
Expand Down
30 changes: 30 additions & 0 deletions runtime/drivers/clickhouse/olap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse_test

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

Expand All @@ -25,6 +26,7 @@ func TestClickhouseSingle(t *testing.T) {

olap, ok := conn.AsOLAP("default")
require.True(t, ok)
t.Run("WithConnection", func(t *testing.T) { testWithConnection(t, olap) })
t.Run("RenameView", func(t *testing.T) { testRenameView(t, olap) })
t.Run("RenameTable", func(t *testing.T) { testRenameTable(t, olap) })
t.Run("CreateTableAsSelect", func(t *testing.T) { testCreateTableAsSelect(t, olap) })
Expand Down Expand Up @@ -52,6 +54,7 @@ func TestClickhouseCluster(t *testing.T) {

prepareClusterConn(t, olap, cluster)

t.Run("WithConnection", func(t *testing.T) { testWithConnection(t, olap) })
t.Run("RenameView", func(t *testing.T) { testRenameView(t, olap) })
t.Run("RenameTable", func(t *testing.T) { testRenameTable(t, olap) })
t.Run("CreateTableAsSelect", func(t *testing.T) { testCreateTableAsSelect(t, olap) })
Expand All @@ -62,6 +65,33 @@ func TestClickhouseCluster(t *testing.T) {
t.Run("TestDictionary", func(t *testing.T) { testDictionary(t, olap) })
}

func testWithConnection(t *testing.T, olap drivers.OLAPStore) {
err := olap.WithConnection(context.Background(), 1, false, func(ctx, ensuredCtx context.Context, conn *sql.Conn) error {
err := olap.Exec(ctx, &drivers.Statement{
Query: "CREATE table tbl engine=Memory AS SELECT 1 AS id, 'Earth' AS planet",
})
require.NoError(t, err)

res, err := olap.Execute(ctx, &drivers.Statement{
Query: "SELECT id, planet FROM tbl",
})
require.NoError(t, err)
var (
id int
planet string
)
for res.Next() {
err = res.Scan(&id, &planet)
require.NoError(t, err)
require.Equal(t, 1, id)
}
require.NoError(t, res.Err())
require.NoError(t, res.Close())
return nil
})
require.NoError(t, err)
}

func testRenameView(t *testing.T, olap drivers.OLAPStore) {
ctx := context.Background()
opts := &drivers.CreateTableOptions{
Expand Down

0 comments on commit 8e7bca1

Please sign in to comment.