Skip to content

Commit

Permalink
added test and example for explain query
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Sep 9, 2024
1 parent 1ecfd60 commit 2b4e684
Show file tree
Hide file tree
Showing 2 changed files with 62 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
35 changes: 35 additions & 0 deletions tests/integration/query_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"os"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -92,6 +93,40 @@ func TestQueryExecute(t *testing.T) {
require.True(t, ok)
})
})
t.Run("Explain", func(t *testing.T) {
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(),
)
require.NoError(t, err)
require.EqualValues(t,
`{"Plan":{"Plans":[{"PlanNodeId":2,"Plans":[{"PlanNodeId":1,"Operators":[{"Inputs":[],"Iterator":"[{column0: 42}]","Name":"Iterator"}],"Node Type":"ConstantExpr"}],"Node Type":"ResultSet","PlanNodeType":"ResultSet"}],"Node Type":"Query","PlanNodeType":"Query"},"meta":{"version":"0.2","type":"query"},"tables":[],"SimplifiedPlan":{"PlanNodeId":0,"Plans":[{"PlanNodeId":1,"Node Type":"ResultSet","PlanNodeType":"ResultSet"}],"Node Type":"Query","PlanNodeType":"Query"}}`,
plan,
)
ast = regexp.MustCompile("\"_id\" '\"(\\w{8}-\\w{8}-\\w{8}-\\w{8})\"").ReplaceAllStringFunc(ast, func(string) string {
return `"_id" '"test-id"`
})
require.EqualValues(t,
`(
(let $1 (OptionalType (DataType 'Uint32)))
(let $2 '('('"_logical_id" '184) '('"_id" '"test-id") '('"_partition_mode" '"single")))
(let $3 (DqPhyStage '() (lambda '() (Iterator (AsList (AsStruct '('"column0" (SafeCast (Int32 '"42") $1)))))) $2))
(let $4 (DqCnResult (TDqOutput $3 '"0") '('"column0")))
(return (KqpPhysicalQuery '((KqpPhysicalTx '($3) '($4) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType (StructType '('"column0" $1))) '"0" '"0")) '('('"type" '"query"))))
)
`,
ast,
)
})
t.Run("Scan", func(t *testing.T) {
var (
p1 string
Expand Down

0 comments on commit 2b4e684

Please sign in to comment.