Skip to content

Commit ea3ad13

Browse files
committed
query.Executer.QueryResultSet now returns query.ResultSetWithClose
1 parent 8492468 commit ea3ad13

File tree

11 files changed

+46
-24
lines changed

11 files changed

+46
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Changed result type of method `query.Executor.QueryResultSet` from `query.ResultSet` to `query.ClosableResultSet`
12
* Added `table/types.DecimalValueFromString` decimal type constructor
23

34
## v3.77.1

examples/basic/native/query/series.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ func read(ctx context.Context, c query.Client, prefix string) error {
1717
return c.Do(ctx,
1818
func(ctx context.Context, s query.Session) (err error) {
1919
result, err := s.Query(ctx, fmt.Sprintf(`
20-
PRAGMA TablePathPrefix("%s");
21-
DECLARE $seriesID AS Uint64;
2220
SELECT
2321
series_id,
2422
title,

internal/query/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package query
22

33
import (
44
"context"
5+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
56
"time"
67

78
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
@@ -462,7 +463,7 @@ func (c *Client) Query(ctx context.Context, q string, opts ...options.Execute) (
462463

463464
func clientQueryResultSet(
464465
ctx context.Context, pool sessionPool, q string, settings executeSettings, resultOpts ...resultOption,
465-
) (rs query.ResultSet, finalErr error) {
466+
) (rs result.ClosableResultSet, finalErr error) {
466467
err := do(ctx, pool, func(ctx context.Context, s *Session) error {
467468
_, r, err := execute(ctx, s.id, s.queryServiceClient, q, settings, resultOpts...)
468469
if err != nil {
@@ -486,7 +487,7 @@ func clientQueryResultSet(
486487
// QueryResultSet is a helper which read all rows from first result set in result
487488
func (c *Client) QueryResultSet(
488489
ctx context.Context, q string, opts ...options.Execute,
489-
) (rs query.ResultSet, finalErr error) {
490+
) (rs result.ClosableResultSet, finalErr error) {
490491
ctx, cancel := xcontext.WithDone(ctx, c.done)
491492
defer cancel()
492493

internal/query/execute_query.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func readAll(ctx context.Context, r *streamResult) error {
143143
}
144144
}
145145

146-
func readResultSet(ctx context.Context, r *streamResult) (_ *resultSet, finalErr error) {
146+
func readResultSet(ctx context.Context, r *streamResult) (_ *resultSetWithClose, finalErr error) {
147147
defer func() {
148148
_ = r.Close(ctx)
149149
}()
@@ -161,7 +161,10 @@ func readResultSet(ctx context.Context, r *streamResult) (_ *resultSet, finalErr
161161
return nil, xerrors.WithStackTrace(err)
162162
}
163163

164-
return rs, nil
164+
return &resultSetWithClose{
165+
resultSet: rs,
166+
close: r.Close,
167+
}, nil
165168
}
166169

167170
func readMaterializedResultSet(ctx context.Context, r *streamResult) (_ *materializedResultSet, finalErr error) {

internal/query/range_experiment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"io"
66

7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
78
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xiter"
9-
"github.com/ydb-platform/ydb-go-sdk/v3/query"
1010
)
1111

12-
func rangeResultSets(ctx context.Context, r query.Result) xiter.Seq2[query.ResultSet, error] {
13-
return func(yield func(query.ResultSet, error) bool) {
12+
func rangeResultSets(ctx context.Context, r result.Result) xiter.Seq2[result.Set, error] {
13+
return func(yield func(result.Set, error) bool) {
1414
for {
1515
rs, err := r.NextResultSet(ctx)
1616
if err != nil {
@@ -26,8 +26,8 @@ func rangeResultSets(ctx context.Context, r query.Result) xiter.Seq2[query.Resul
2626
}
2727
}
2828

29-
func rangeRows(ctx context.Context, rs query.ResultSet) xiter.Seq2[query.Row, error] {
30-
return func(yield func(query.Row, error) bool) {
29+
func rangeRows(ctx context.Context, rs result.Set) xiter.Seq2[result.Set, error] {
30+
return func(yield func(result.Row, error) bool) {
3131
for {
3232
rs, err := rs.NextRow(ctx)
3333
if err != nil {

internal/query/result/result.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ type (
2929
// Rows is experimental API for range iterators available with Go version 1.23+
3030
Rows(ctx context.Context) xiter.Seq2[Row, error]
3131
}
32+
ClosableResultSet interface {
33+
Set
34+
closer.Closer
35+
}
3236
Row interface {
3337
Scan(dst ...interface{}) error
3438
ScanNamed(dst ...scanner.NamedDestination) error

internal/query/result_set.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,20 @@ type (
3535
rowIndex int
3636
done chan struct{}
3737
}
38+
resultSetWithClose struct {
39+
*resultSet
40+
close func(ctx context.Context) error
41+
}
3842
)
3943

44+
func (*materializedResultSet) Close(context.Context) error {
45+
return nil
46+
}
47+
48+
func (rs *resultSetWithClose) Close(ctx context.Context) error {
49+
return rs.close(ctx)
50+
}
51+
4052
func (rs *materializedResultSet) Rows(ctx context.Context) xiter.Seq2[query.Row, error] {
4153
return rangeRows(ctx, rs)
4254
}

internal/query/session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package query
22

33
import (
44
"context"
5+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
56
"sync/atomic"
67

78
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
@@ -32,7 +33,7 @@ type Session struct {
3233

3334
func (s *Session) QueryResultSet(
3435
ctx context.Context, q string, opts ...options.Execute,
35-
) (rs query.ResultSet, finalErr error) {
36+
) (rs result.ClosableResultSet, finalErr error) {
3637
onDone := trace.QueryOnSessionQueryResultSet(s.cfg.Trace(), &ctx,
3738
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Session).QueryResultSet"), s, q)
3839
defer func() {

internal/query/transaction.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package query
33
import (
44
"context"
55
"fmt"
6+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
67

78
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
89
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
@@ -77,7 +78,7 @@ func (tx *Transaction) UnLazy(ctx context.Context) (err error) {
7778

7879
func (tx *Transaction) QueryResultSet(
7980
ctx context.Context, q string, opts ...options.Execute,
80-
) (rs query.ResultSet, finalErr error) {
81+
) (rs result.ClosableResultSet, finalErr error) {
8182
onDone := trace.QueryOnTxQueryResultSet(tx.s.cfg.Trace(), &ctx,
8283
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.(*Transaction).QueryResultSet"), tx, q)
8384
defer func() {

query/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ type (
2525
//
2626
// Exec used by default:
2727
// - DefaultTxControl
28-
Query(ctx context.Context, query string, opts ...options.Execute) (r Result, err error)
28+
Query(ctx context.Context, query string, opts ...options.Execute) (Result, error)
2929

3030
// QueryResultSet execute query and take the exactly single materialized result set from result
3131
//
3232
// Exec used by default:
3333
// - DefaultTxControl
34-
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (rs ResultSet, err error)
34+
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (resultSetWithClose, error)
3535

3636
// QueryRow execute query and take the exactly single row from exactly single result set from result
3737
//
3838
// Exec used by default:
3939
// - DefaultTxControl
40-
QueryRow(ctx context.Context, query string, opts ...options.Execute) (row Row, err error)
40+
QueryRow(ctx context.Context, query string, opts ...options.Execute) (Row, error)
4141
}
4242
// Client defines API of query client
4343
//
@@ -80,14 +80,14 @@ type (
8080
//
8181
// Exec used by default:
8282
// - DefaultTxControl
83-
Query(ctx context.Context, query string, opts ...options.Execute) (r Result, err error)
83+
Query(ctx context.Context, query string, opts ...options.Execute) (Result, error)
8484

8585
// QueryResultSet is a helper which read all rows from first result set in result
8686
//
8787
// Warning: the large result set from query will be materialized and can happened to "OOM killed" problem
8888
//
8989
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
90-
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (ResultSet, error)
90+
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (resultSetWithClose, error)
9191

9292
// QueryRow is a helper which read only one row from first result set in result
9393
//

0 commit comments

Comments
 (0)