Skip to content

Commit

Permalink
Runtime: Add missing dry run support in Exec method of clickhouse d…
Browse files Browse the repository at this point in the history
…river (#4559)

* dry run exec support clickhouse

* fix and unit test

* use explain for dry run
  • Loading branch information
k-anshul authored Apr 10, 2024
1 parent cc1dbba commit 41b7adc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
15 changes: 14 additions & 1 deletion runtime/drivers/clickhouse/information_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,21 @@ func prepareConn(t *testing.T, conn drivers.Handle) {
})
require.NoError(t, err)

// test dry run
err = olap.Exec(context.Background(), &drivers.Statement{
Query: "INSERT INTO other.bar VALUES ('a', 1), ('a', 2), ('b', 3), ('c', 4)",
DryRun: true,
Query: `WITH cte_numbers AS
(
SELECT num
FROM generateRandom('num UInt64', NULL)
LIMIT 10000000000
)
SELECT count()
FROM cte_numbers
WHERE num IN (
SELECT num
FROM cte_numbers
)`,
})
require.NoError(t, err)
}
23 changes: 13 additions & 10 deletions runtime/drivers/clickhouse/olap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/jmoiron/sqlx"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
Expand Down Expand Up @@ -60,6 +59,18 @@ func (c *connection) Exec(ctx context.Context, stmt *drivers.Statement) error {
c.logger.Info("clickhouse query", zap.String("sql", stmt.Query), zap.Any("args", stmt.Args))
}

// We use the meta conn for dry run queries
if stmt.DryRun {
conn, release, err := c.acquireMetaConn(ctx)
if err != nil {
return err
}
defer func() { _ = release() }()

_, err = conn.ExecContext(ctx, fmt.Sprintf("EXPLAIN %s", stmt.Query), stmt.Args...)
return err
}

conn, release, err := c.acquireOLAPConn(ctx, stmt.Priority)
if err != nil {
return err
Expand Down Expand Up @@ -94,15 +105,7 @@ func (c *connection) Execute(ctx context.Context, stmt *drivers.Statement) (res
}
defer func() { _ = release() }()

// TODO: Find way to validate with args

name := uuid.NewString()
_, err = conn.ExecContext(ctx, fmt.Sprintf("CREATE TEMPORARY VIEW %q AS %s", name, stmt.Query))
if err != nil {
return nil, err
}

_, err = conn.ExecContext(context.Background(), fmt.Sprintf("DROP VIEW %q", name))
_, err = conn.ExecContext(ctx, fmt.Sprintf("EXPLAIN %s", stmt.Query), stmt.Args...)
return nil, err
}

Expand Down

0 comments on commit 41b7adc

Please sign in to comment.