Skip to content

Commit

Permalink
set public interface query.ClosableResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Sep 4, 2024
1 parent 903a6df commit 75b64a2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/query/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (c *Client) ExecuteScript(
return op, nil
}

func (p *poolStub) Close(ctx context.Context) error {
func (p *poolStub) Close(context.Context) error {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
)

func TestResultRange(t *testing.T) {
func TestResultRangeResultSets(t *testing.T) {
ctx, cancel := context.WithCancel(xtest.Context(t))
defer cancel()
ctrl := gomock.NewController(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
)

func TestResultSetRange(t *testing.T) {
func TestResultSetRangeRows(t *testing.T) {
ctx := xtest.Context(t)
ctrl := gomock.NewController(t)
t.Run("EmptyResultSet", func(t *testing.T) {
Expand Down
148 changes: 148 additions & 0 deletions internal/query/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

func TestResultNextResultSet(t *testing.T) {
Expand Down Expand Up @@ -1435,6 +1436,153 @@ func TestExactlyOneResultSetFromResult(t *testing.T) {
})
}

func TestCloseResultOnCloseClosableResultSet(t *testing.T) {
ctx := xtest.Context(t)
ctrl := gomock.NewController(t)
stream := NewMockQueryService_ExecuteQueryClient(ctrl)
stream.EXPECT().Recv().Return(&Ydb_Query.ExecuteQueryResponsePart{
Status: Ydb.StatusIds_SUCCESS,
TxMeta: &Ydb_Query.TransactionMeta{
Id: "456",
},
ResultSetIndex: 0,
ResultSet: &Ydb.ResultSet{
Columns: []*Ydb.Column{
{
Name: "a",
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UINT64,
},
},
},
{
Name: "b",
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UTF8,
},
},
},
},
Rows: []*Ydb.Value{
{
Items: []*Ydb.Value{{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 1,
},
}, {
Value: &Ydb.Value_TextValue{
TextValue: "1",
},
}},
},
{
Items: []*Ydb.Value{{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 2,
},
}, {
Value: &Ydb.Value_TextValue{
TextValue: "2",
},
}},
},
},
},
}, nil)
stream.EXPECT().Recv().Return(&Ydb_Query.ExecuteQueryResponsePart{
Status: Ydb.StatusIds_SUCCESS,
TxMeta: &Ydb_Query.TransactionMeta{
Id: "456",
},
ResultSetIndex: 0,
ResultSet: &Ydb.ResultSet{
Columns: []*Ydb.Column{
{
Name: "a",
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UINT64,
},
},
},
{
Name: "b",
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UTF8,
},
},
},
},
Rows: []*Ydb.Value{
{
Items: []*Ydb.Value{{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 1,
},
}, {
Value: &Ydb.Value_TextValue{
TextValue: "1",
},
}},
},
{
Items: []*Ydb.Value{{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 2,
},
}, {
Value: &Ydb.Value_TextValue{
TextValue: "2",
},
}},
},
},
},
}, nil)
stream.EXPECT().Recv().Return(nil, io.EOF)
var closed bool
r, _, err := newResult(ctx, stream, withTrace(&trace.Query{
OnResultClose: func(info trace.QueryResultCloseStartInfo) func(info trace.QueryResultCloseDoneInfo) {
require.False(t, closed)
closed = true

return nil
},
}))

require.NoError(t, err)

rs, err := readResultSet(ctx, r)
require.NoError(t, err)
var (
a uint64
b string
)
r1, err1 := rs.NextRow(ctx)
require.NoError(t, err1)
require.NotNil(t, r1)
scanErr1 := r1.Scan(&a, &b)
require.NoError(t, scanErr1)
require.EqualValues(t, 1, a)
require.EqualValues(t, "1", b)
r2, err2 := rs.NextRow(ctx)
require.NoError(t, err2)
require.NotNil(t, r2)
scanErr2 := r2.Scan(&a, &b)
require.NoError(t, scanErr2)
require.EqualValues(t, 2, a)
require.EqualValues(t, "2", b)
r3, err3 := rs.NextRow(ctx)
require.ErrorIs(t, err3, io.EOF)
require.Nil(t, r3)
err = rs.Close(ctx)
require.NoError(t, err)
require.True(t, closed)
}

func TestResultStats(t *testing.T) {
t.Run("Stats", func(t *testing.T) {
t.Run("Never", func(t *testing.T) {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions query/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
//
// Exec used by default:
// - DefaultTxControl
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (resultSetWithClose, error)
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (ClosableResultSet, error)

// QueryRow execute query and take the exactly single row from exactly single result set from result
//
Expand Down Expand Up @@ -87,7 +87,7 @@ type (
// Warning: the large result set from query will be materialized and can happened to "OOM killed" problem
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (resultSetWithClose, error)
QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (ClosableResultSet, error)

// QueryRow is a helper which read only one row from first result set in result
//
Expand Down
14 changes: 7 additions & 7 deletions query/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type (
Result = result.Result
ResultSet = result.Set
resultSetWithClose = result.ClosableResultSet
Row = result.Row
Type = types.Type
NamedDestination = scanner.NamedDestination
ScanStructOption = scanner.ScanStructOption
Result = result.Result
ResultSet = result.Set
ClosableResultSet = result.ClosableResultSet
Row = result.Row
Type = types.Type
NamedDestination = scanner.NamedDestination
ScanStructOption = scanner.ScanStructOption
)

func Named(columnName string, destinationValueReference interface{}) (dst NamedDestination) {
Expand Down

0 comments on commit 75b64a2

Please sign in to comment.