From 75b64a2ce8edeea8fd226964a64986f6e1837d2d Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Wed, 4 Sep 2024 14:19:04 +0300 Subject: [PATCH] set public interface query.ClosableResultSet --- internal/query/client.go | 2 +- ...lt_range_test.go => result_go1.23_test.go} | 2 +- ...ange_test.go => result_set_go1.23_test.go} | 2 +- internal/query/result_test.go | 148 ++++++++++++++++++ ...r_experiment_go1.23.go => xiter_go1.23.go} | 0 query/client.go | 4 +- query/result.go | 14 +- 7 files changed, 160 insertions(+), 12 deletions(-) rename internal/query/{result_range_test.go => result_go1.23_test.go} (99%) rename internal/query/{result_set_range_test.go => result_set_go1.23_test.go} (99%) rename internal/xiter/{xiter_experiment_go1.23.go => xiter_go1.23.go} (100%) diff --git a/internal/query/client.go b/internal/query/client.go index 99b9055d9..556aea07d 100644 --- a/internal/query/client.go +++ b/internal/query/client.go @@ -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 } diff --git a/internal/query/result_range_test.go b/internal/query/result_go1.23_test.go similarity index 99% rename from internal/query/result_range_test.go rename to internal/query/result_go1.23_test.go index 34024a5e9..945c7f349 100644 --- a/internal/query/result_range_test.go +++ b/internal/query/result_go1.23_test.go @@ -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) diff --git a/internal/query/result_set_range_test.go b/internal/query/result_set_go1.23_test.go similarity index 99% rename from internal/query/result_set_range_test.go rename to internal/query/result_set_go1.23_test.go index a1c349235..a96d035f5 100644 --- a/internal/query/result_set_range_test.go +++ b/internal/query/result_set_go1.23_test.go @@ -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) { diff --git a/internal/query/result_test.go b/internal/query/result_test.go index eab0712a0..4be2ac98e 100644 --- a/internal/query/result_test.go +++ b/internal/query/result_test.go @@ -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) { @@ -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) { diff --git a/internal/xiter/xiter_experiment_go1.23.go b/internal/xiter/xiter_go1.23.go similarity index 100% rename from internal/xiter/xiter_experiment_go1.23.go rename to internal/xiter/xiter_go1.23.go diff --git a/query/client.go b/query/client.go index 3b2549eaa..c5366ebad 100644 --- a/query/client.go +++ b/query/client.go @@ -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 // @@ -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 // diff --git a/query/result.go b/query/result.go index 8a4869ff4..9990fa8f0 100644 --- a/query/result.go +++ b/query/result.go @@ -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) {