Skip to content

Commit

Permalink
Merge pull request #1451 from ydb-platform/issue-1395
Browse files Browse the repository at this point in the history
added test and example for explain query
  • Loading branch information
asmyasnikov authored Sep 9, 2024
2 parents 1ecfd60 + edd4163 commit a9b150e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions query/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ func Example_queryRow() {
fmt.Printf("id=%v, myStr='%s'\n", id, myStr)
}

func Example_explain() {
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
panic(err)
}
defer db.Close(ctx) // cleanup resources
var (
ast string
plan string
)
err = db.Query().Exec(ctx,
`SELECT CAST(42 AS Uint32);`,
query.WithExecMode(query.ExecModeExplain),
query.WithStatsMode(query.StatsModeNone, func(stats query.Stats) {
ast = stats.QueryAST()
plan = stats.QueryPlan()
}),
query.WithIdempotent(),
)
if err != nil {
panic(err)
}
fmt.Println(plan)
fmt.Println(ast)
}

func Example_withoutRangeIterators() {
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/query_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration

import (
"context"
"encoding/json"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -92,6 +93,28 @@ func TestQueryExecute(t *testing.T) {
require.True(t, ok)
})
})
t.Run("Explain", func(t *testing.T) {
var (
ast string
plan map[string]any
)
err := db.Query().Exec(ctx,
`SELECT CAST(42 AS Uint32);`,
query.WithExecMode(query.ExecModeExplain),
query.WithStatsMode(query.StatsModeNone, func(stats query.Stats) {
ast = stats.QueryAST()
err := json.Unmarshal([]byte(stats.QueryPlan()), &plan)
require.NoError(t, err)
}),
query.WithIdempotent(),
)
require.NoError(t, err)
for _, key := range []string{"Plan", "tables", "meta"} {
_, has := plan[key]
require.True(t, has, key)
}
require.NotEmpty(t, ast)
})
t.Run("Scan", func(t *testing.T) {
var (
p1 string
Expand Down

0 comments on commit a9b150e

Please sign in to comment.