Skip to content

Commit

Permalink
added iterator API over query phases
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Jan 16, 2025
1 parent 0d3f84a commit 7347733
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
15 changes: 15 additions & 0 deletions internal/stats/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"time"

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/xiter"
)

type (
Expand Down Expand Up @@ -104,6 +106,19 @@ func (s *QueryStats) NextPhase() (p QueryPhase, ok bool) {
}, true
}

func (s *QueryStats) QueryPhases() xiter.Seq[QueryPhase] {
return func(yield func(p QueryPhase) bool) {
for _, pb := range s.pb.GetQueryPhases() {
cont := yield(QueryPhase{
pb: pb,
})
if !cont {
return
}
}
}
}

// NextTableAccess returns next accessed table within query execution phase.
//
// If ok flag is false, then there are no more accessed tables and t is
Expand Down
55 changes: 55 additions & 0 deletions internal/stats/query_go1.23_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build go1.23

package stats

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"
)

func TestIterateOverQueryPhases(t *testing.T) {
s := &QueryStats{
pb: &Ydb_TableStats.QueryStats{
QueryPhases: []*Ydb_TableStats.QueryPhaseStats{
{
DurationUs: 1,
},
{
DurationUs: 2,
},
{
DurationUs: 3,
},
},
},
}
t.Run("ImmutableIteration", func(t *testing.T) {
for i := range make([]struct{}, 3) {
t.Run(fmt.Sprintf("Pass#%d", i), func(t *testing.T) {
durations := make([]uint64, 0, 3)
for phase := range s.QueryPhases() {
durations = append(durations, phase.pb.GetDurationUs())
}
require.Equal(t, []uint64{1, 2, 3}, durations)
})
}
})
t.Run("MutableIteration", func(t *testing.T) {
durations := make([]uint64, 0, 3)
for {
phase, ok := s.NextPhase()
if !ok {
break
}
durations = append(durations, phase.pb.GetDurationUs())
}
require.Equal(t, []uint64{1, 2, 3}, durations)
require.Equal(t, 3, s.pos)

_, ok := s.NextPhase()
require.False(t, ok)
})
}
5 changes: 4 additions & 1 deletion internal/xiter/xiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

package xiter

type Seq2[K, V any] func(yield func(K, V) bool)
type (
Seq[T any] func(yield func(T) bool)
Seq2[K, V any] func(yield func(K, V) bool)
)
5 changes: 4 additions & 1 deletion internal/xiter/xiter_go1.23.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import (
"iter"
)

type Seq2[K, V any] iter.Seq2[K, V]
type (
Seq[T any] iter.Seq[T]
Seq2[K, V any] iter.Seq2[K, V]
)

0 comments on commit 7347733

Please sign in to comment.