44 "time"
55
66 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"
7+
8+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xiter"
79)
810
911type (
@@ -19,12 +21,17 @@ type (
1921 // NextPhase returns next execution phase within query.
2022 // If ok flag is false, then there are no more phases and p is invalid.
2123 NextPhase () (p QueryPhase , ok bool )
24+
25+ // QueryPhases is a range iterator over query phases.
26+ QueryPhases () xiter.Seq [QueryPhase ]
2227 }
2328 // QueryPhase holds query execution phase statistics.
2429 QueryPhase interface {
2530 // NextTableAccess returns next accessed table within query execution phase.
2631 // If ok flag is false, then there are no more accessed tables and t is invalid.
2732 NextTableAccess () (t * TableAccess , ok bool )
33+ // TableAccess is a range iterator over query execution phase's accessed tables.
34+ TableAccess () xiter.Seq [* TableAccess ]
2835 Duration () time.Duration
2936 CPUTime () time.Duration
3037 AffectedShards () uint64
@@ -86,57 +93,70 @@ func fromOperationStats(pb *Ydb_TableStats.OperationStats) OperationStats {
8693 }
8794}
8895
89- func (s * queryStats ) ProcessCPUTime () time.Duration {
90- return fromUs (s .pb .GetProcessCpuTimeUs ())
96+ func (stats * queryStats ) ProcessCPUTime () time.Duration {
97+ return fromUs (stats .pb .GetProcessCpuTimeUs ())
9198}
9299
93- func (s * queryStats ) Compilation () (c * CompilationStats ) {
94- return fromCompilationStats (s .pb .GetCompilation ())
100+ func (stats * queryStats ) Compilation () (c * CompilationStats ) {
101+ return fromCompilationStats (stats .pb .GetCompilation ())
95102}
96103
97- func (s * queryStats ) QueryPlan () string {
98- return s .pb .GetQueryPlan ()
104+ func (stats * queryStats ) QueryPlan () string {
105+ return stats .pb .GetQueryPlan ()
99106}
100107
101- func (s * queryStats ) QueryAST () string {
102- return s .pb .GetQueryAst ()
108+ func (stats * queryStats ) QueryAST () string {
109+ return stats .pb .GetQueryAst ()
103110}
104111
105- func (s * queryStats ) TotalCPUTime () time.Duration {
106- return fromUs (s .pb .GetTotalCpuTimeUs ())
112+ func (stats * queryStats ) TotalCPUTime () time.Duration {
113+ return fromUs (stats .pb .GetTotalCpuTimeUs ())
107114}
108115
109- func (s * queryStats ) TotalDuration () time.Duration {
110- return fromUs (s .pb .GetTotalDurationUs ())
116+ func (stats * queryStats ) TotalDuration () time.Duration {
117+ return fromUs (stats .pb .GetTotalDurationUs ())
111118}
112119
113120// NextPhase returns next execution phase within query.
114121// If ok flag is false, then there are no more phases and p is invalid.
115- func (s * queryStats ) NextPhase () (p QueryPhase , ok bool ) {
116- if s .pos >= len (s .pb .GetQueryPhases ()) {
122+ func (stats * queryStats ) NextPhase () (p QueryPhase , ok bool ) {
123+ if stats .pos >= len (stats .pb .GetQueryPhases ()) {
117124 return
118125 }
119- pb := s .pb .GetQueryPhases ()[s .pos ]
126+ pb := stats .pb .GetQueryPhases ()[stats .pos ]
120127 if pb == nil {
121128 return
122129 }
123- s .pos ++
130+ stats .pos ++
124131
125132 return & queryPhase {
126133 pb : pb ,
127134 }, true
128135}
129136
137+ func (stats * queryStats ) QueryPhases () xiter.Seq [QueryPhase ] {
138+ return func (yield func (p QueryPhase ) bool ) {
139+ for _ , pb := range stats .pb .GetQueryPhases () {
140+ cont := yield (& queryPhase {
141+ pb : pb ,
142+ })
143+ if ! cont {
144+ return
145+ }
146+ }
147+ }
148+ }
149+
130150// NextTableAccess returns next accessed table within query execution phase.
131151//
132152// If ok flag is false, then there are no more accessed tables and t is
133153// invalid.
134- func (queryPhase * queryPhase ) NextTableAccess () (t * TableAccess , ok bool ) {
135- if queryPhase .pos >= len (queryPhase .pb .GetTableAccess ()) {
154+ func (phase * queryPhase ) NextTableAccess () (t * TableAccess , ok bool ) {
155+ if phase .pos >= len (phase .pb .GetTableAccess ()) {
136156 return
137157 }
138- pb := queryPhase .pb .GetTableAccess ()[queryPhase .pos ]
139- queryPhase .pos ++
158+ pb := phase .pb .GetTableAccess ()[phase .pos ]
159+ phase .pos ++
140160
141161 return & TableAccess {
142162 Name : pb .GetName (),
@@ -147,20 +167,37 @@ func (queryPhase *queryPhase) NextTableAccess() (t *TableAccess, ok bool) {
147167 }, true
148168}
149169
150- func (queryPhase * queryPhase ) Duration () time.Duration {
151- return fromUs (queryPhase .pb .GetDurationUs ())
170+ func (phase * queryPhase ) TableAccess () xiter.Seq [* TableAccess ] {
171+ return func (yield func (access * TableAccess ) bool ) {
172+ for _ , pb := range phase .pb .GetTableAccess () {
173+ cont := yield (& TableAccess {
174+ Name : pb .GetName (),
175+ Reads : fromOperationStats (pb .GetReads ()),
176+ Updates : fromOperationStats (pb .GetUpdates ()),
177+ Deletes : fromOperationStats (pb .GetDeletes ()),
178+ PartitionsCount : pb .GetPartitionsCount (),
179+ })
180+ if ! cont {
181+ return
182+ }
183+ }
184+ }
185+ }
186+
187+ func (phase * queryPhase ) Duration () time.Duration {
188+ return fromUs (phase .pb .GetDurationUs ())
152189}
153190
154- func (queryPhase * queryPhase ) CPUTime () time.Duration {
155- return fromUs (queryPhase .pb .GetCpuTimeUs ())
191+ func (phase * queryPhase ) CPUTime () time.Duration {
192+ return fromUs (phase .pb .GetCpuTimeUs ())
156193}
157194
158- func (queryPhase * queryPhase ) AffectedShards () uint64 {
159- return queryPhase .pb .GetAffectedShards ()
195+ func (phase * queryPhase ) AffectedShards () uint64 {
196+ return phase .pb .GetAffectedShards ()
160197}
161198
162- func (queryPhase * queryPhase ) IsLiteralPhase () bool {
163- return queryPhase .pb .GetLiteralPhase ()
199+ func (phase * queryPhase ) IsLiteralPhase () bool {
200+ return phase .pb .GetLiteralPhase ()
164201}
165202
166203func FromQueryStats (pb * Ydb_TableStats.QueryStats ) QueryStats {
0 commit comments