Skip to content

Commit a9b150e

Browse files
authored
Merge pull request #1451 from ydb-platform/issue-1395
added test and example for explain query
2 parents 1ecfd60 + edd4163 commit a9b150e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

query/example_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ func Example_queryRow() {
124124
fmt.Printf("id=%v, myStr='%s'\n", id, myStr)
125125
}
126126

127+
func Example_explain() {
128+
ctx := context.TODO()
129+
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
130+
if err != nil {
131+
panic(err)
132+
}
133+
defer db.Close(ctx) // cleanup resources
134+
var (
135+
ast string
136+
plan string
137+
)
138+
err = db.Query().Exec(ctx,
139+
`SELECT CAST(42 AS Uint32);`,
140+
query.WithExecMode(query.ExecModeExplain),
141+
query.WithStatsMode(query.StatsModeNone, func(stats query.Stats) {
142+
ast = stats.QueryAST()
143+
plan = stats.QueryPlan()
144+
}),
145+
query.WithIdempotent(),
146+
)
147+
if err != nil {
148+
panic(err)
149+
}
150+
fmt.Println(plan)
151+
fmt.Println(ast)
152+
}
153+
127154
func Example_withoutRangeIterators() {
128155
ctx := context.TODO()
129156
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")

tests/integration/query_execute_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package integration
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"os"
1011
"testing"
@@ -92,6 +93,28 @@ func TestQueryExecute(t *testing.T) {
9293
require.True(t, ok)
9394
})
9495
})
96+
t.Run("Explain", func(t *testing.T) {
97+
var (
98+
ast string
99+
plan map[string]any
100+
)
101+
err := db.Query().Exec(ctx,
102+
`SELECT CAST(42 AS Uint32);`,
103+
query.WithExecMode(query.ExecModeExplain),
104+
query.WithStatsMode(query.StatsModeNone, func(stats query.Stats) {
105+
ast = stats.QueryAST()
106+
err := json.Unmarshal([]byte(stats.QueryPlan()), &plan)
107+
require.NoError(t, err)
108+
}),
109+
query.WithIdempotent(),
110+
)
111+
require.NoError(t, err)
112+
for _, key := range []string{"Plan", "tables", "meta"} {
113+
_, has := plan[key]
114+
require.True(t, has, key)
115+
}
116+
require.NotEmpty(t, ast)
117+
})
95118
t.Run("Scan", func(t *testing.T) {
96119
var (
97120
p1 string

0 commit comments

Comments
 (0)