Skip to content

Commit 1734076

Browse files
committed
splitted operation.List to five list operations operation.List{BuildIndex,ImportFromS3,ExportToS3,ExportToYT,ExecuteQuery}
1 parent c25594d commit 1734076

File tree

8 files changed

+356
-170
lines changed

8 files changed

+356
-170
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
* Added log topic writer ack
2+
* Replaced `operation.Client.List` to five methods for listing operations `operation.List{BuildIndex,ImportFromS3,ExportToS3,ExportToYT,ExecuteQuery}`
23

34
## v3.77.0
45
* Changed log message about send topic message
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package metadata
2+
3+
import (
4+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Export"
5+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Import"
6+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
7+
"google.golang.org/protobuf/types/known/anypb"
8+
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
10+
)
11+
12+
type (
13+
TypesConstraint interface {
14+
BuildIndex | ImportFromS3 | ExportToS3 | ExportToYT | ExecuteQuery
15+
}
16+
Constraint[T TypesConstraint] interface {
17+
fromProto(metadata *anypb.Any) *T
18+
}
19+
BuildIndex struct {
20+
Description string
21+
State string
22+
Progress float32
23+
}
24+
ImportFromS3 struct {
25+
Settings string
26+
Status string
27+
Items []string
28+
}
29+
ExportToS3 struct {
30+
Settings string
31+
Status string
32+
Items []string
33+
}
34+
ExportToYT struct {
35+
Settings string
36+
Status string
37+
Items []string
38+
}
39+
ExecuteQuery options.MetadataExecuteQuery
40+
)
41+
42+
func (*ImportFromS3) fromProto(metadata *anypb.Any) *ImportFromS3 { //nolint:unused
43+
var pb Ydb_Import.ImportFromS3Metadata
44+
if err := metadata.UnmarshalTo(&pb); err != nil {
45+
panic(err)
46+
}
47+
48+
return &ImportFromS3{
49+
Settings: pb.GetSettings().String(),
50+
Status: pb.GetProgress().String(),
51+
Items: func() (items []string) {
52+
for _, item := range pb.GetItemsProgress() {
53+
items = append(items, item.String())
54+
}
55+
56+
return items
57+
}(),
58+
}
59+
}
60+
61+
func (*ExportToS3) fromProto(metadata *anypb.Any) *ExportToS3 { //nolint:unused
62+
var pb Ydb_Export.ExportToS3Metadata
63+
if err := metadata.UnmarshalTo(&pb); err != nil {
64+
panic(err)
65+
}
66+
67+
return &ExportToS3{
68+
Settings: pb.GetSettings().String(),
69+
Status: pb.GetProgress().String(),
70+
Items: func() (items []string) {
71+
for _, item := range pb.GetItemsProgress() {
72+
items = append(items, item.String())
73+
}
74+
75+
return items
76+
}(),
77+
}
78+
}
79+
80+
func (*ExportToYT) fromProto(metadata *anypb.Any) *ExportToYT { //nolint:unused
81+
var pb Ydb_Export.ExportToYtMetadata
82+
if err := metadata.UnmarshalTo(&pb); err != nil {
83+
panic(err)
84+
}
85+
86+
return &ExportToYT{
87+
Settings: pb.GetSettings().String(),
88+
Status: pb.GetProgress().String(),
89+
Items: func() (items []string) {
90+
for _, item := range pb.GetItemsProgress() {
91+
items = append(items, item.String())
92+
}
93+
94+
return items
95+
}(),
96+
}
97+
}
98+
99+
func (*ExecuteQuery) fromProto(metadata *anypb.Any) *ExecuteQuery { //nolint:unused
100+
return (*ExecuteQuery)(options.ToMetadataExecuteQuery(metadata))
101+
}
102+
103+
func (*BuildIndex) fromProto(metadata *anypb.Any) *BuildIndex { //nolint:unused
104+
var pb Ydb_Table.IndexBuildMetadata
105+
if err := metadata.UnmarshalTo(&pb); err != nil {
106+
panic(err)
107+
}
108+
109+
return &BuildIndex{
110+
Description: pb.GetDescription().String(),
111+
State: pb.GetState().String(),
112+
Progress: pb.GetProgress(),
113+
}
114+
}
115+
116+
func FromProto[PT Constraint[T], T TypesConstraint](metadata *anypb.Any) *T {
117+
var pt PT
118+
119+
return pt.fromProto(metadata)
120+
}

internal/query/client.go

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/config"
1717
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
1818
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
19-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stats"
2019
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
2120
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
2221
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -135,7 +134,7 @@ func (s *executeScriptSettings) ResultsTTL() time.Duration {
135134
return s.ttl
136135
}
137136

138-
func executeScript(ctx context.Context, //nolint:funlen
137+
func executeScript(ctx context.Context,
139138
client Ydb_Query_V1.QueryServiceClient, request *Ydb_Query.ExecuteScriptRequest, grpcOpts ...grpc.CallOption,
140139
) (*options.ExecuteScriptOperation, error) {
141140
op, err := retry.RetryWithResult(ctx, func(ctx context.Context) (*options.ExecuteScriptOperation, error) {
@@ -144,79 +143,10 @@ func executeScript(ctx context.Context, //nolint:funlen
144143
return nil, xerrors.WithStackTrace(err)
145144
}
146145

147-
var md Ydb_Query.ExecuteScriptMetadata
148-
err = response.GetMetadata().UnmarshalTo(&md)
149-
if err != nil {
150-
return nil, xerrors.WithStackTrace(err)
151-
}
152-
153146
return &options.ExecuteScriptOperation{
154147
ID: response.GetId(),
155148
ConsumedUnits: response.GetCostInfo().GetConsumedUnits(),
156-
Metadata: struct {
157-
ID string
158-
Script struct {
159-
Syntax options.Syntax
160-
Query string
161-
}
162-
Mode options.ExecMode
163-
Stats stats.QueryStats
164-
ResultSetsMeta []struct {
165-
Columns []struct {
166-
Name string
167-
Type query.Type
168-
}
169-
}
170-
}{
171-
ID: md.GetExecutionId(),
172-
Script: struct {
173-
Syntax options.Syntax
174-
Query string
175-
}{
176-
Syntax: options.Syntax(md.GetScriptContent().GetSyntax()),
177-
Query: md.GetScriptContent().GetText(),
178-
},
179-
Mode: options.ExecMode(md.GetExecMode()),
180-
Stats: stats.FromQueryStats(md.GetExecStats()),
181-
ResultSetsMeta: func() (
182-
resultSetsMeta []struct {
183-
Columns []struct {
184-
Name string
185-
Type query.Type
186-
}
187-
},
188-
) {
189-
for _, rs := range md.GetResultSetsMeta() {
190-
resultSetsMeta = append(resultSetsMeta, struct {
191-
Columns []struct {
192-
Name string
193-
Type query.Type
194-
}
195-
}{
196-
Columns: func() (
197-
columns []struct {
198-
Name string
199-
Type types.Type
200-
},
201-
) {
202-
for _, c := range rs.GetColumns() {
203-
columns = append(columns, struct {
204-
Name string
205-
Type types.Type
206-
}{
207-
Name: c.GetName(),
208-
Type: types.TypeFromYDB(c.GetType()),
209-
})
210-
}
211-
212-
return columns
213-
}(),
214-
})
215-
}
216-
217-
return resultSetsMeta
218-
}(),
219-
},
149+
Metadata: options.ToMetadataExecuteQuery(response.GetMetadata()),
220150
}, nil
221151
}, retry.WithIdempotent(true))
222152
if err != nil {

internal/query/options/execute_script.go

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package options
22

33
import (
44
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
5+
"google.golang.org/protobuf/types/known/anypb"
56

67
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
78
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stats"
8-
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
910
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1011
)
1112

@@ -15,8 +16,18 @@ type (
1516

1617
Trace *trace.Query
1718
}
18-
FetchScriptOption func(request *FetchScriptResultsRequest)
19-
ExecuteScriptOperationMetadata struct {
19+
FetchScriptOption func(request *FetchScriptResultsRequest)
20+
ExecuteScriptOperation struct {
21+
ID string
22+
ConsumedUnits float64
23+
Metadata *MetadataExecuteQuery
24+
}
25+
FetchScriptResult struct {
26+
ResultSetIndex int64
27+
ResultSet result.Set
28+
NextToken string
29+
}
30+
MetadataExecuteQuery struct {
2031
ID string
2132
Script struct {
2233
Syntax Syntax
@@ -31,16 +42,6 @@ type (
3142
}
3243
}
3344
}
34-
ExecuteScriptOperation struct {
35-
ID string
36-
ConsumedUnits float64
37-
Metadata ExecuteScriptOperationMetadata
38-
}
39-
FetchScriptResult struct {
40-
ResultSetIndex int64
41-
ResultSet result.Set
42-
NextToken string
43-
}
4445
)
4546

4647
func WithFetchToken(fetchToken string) FetchScriptOption {
@@ -60,3 +61,61 @@ func WithRowsLimit(rowsLimit int64) FetchScriptOption {
6061
request.RowsLimit = rowsLimit
6162
}
6263
}
64+
65+
func ToMetadataExecuteQuery(metadata *anypb.Any) *MetadataExecuteQuery {
66+
var pb Ydb_Query.ExecuteScriptMetadata
67+
if err := metadata.UnmarshalTo(&pb); err != nil {
68+
panic(err)
69+
}
70+
71+
return &MetadataExecuteQuery{
72+
ID: pb.GetExecutionId(),
73+
Script: struct {
74+
Syntax Syntax
75+
Query string
76+
}{
77+
Syntax: Syntax(pb.GetScriptContent().GetSyntax()),
78+
Query: pb.GetScriptContent().GetText(),
79+
},
80+
Mode: ExecMode(pb.GetExecMode()),
81+
Stats: stats.FromQueryStats(pb.GetExecStats()),
82+
ResultSetsMeta: func() (
83+
resultSetsMeta []struct {
84+
Columns []struct {
85+
Name string
86+
Type types.Type
87+
}
88+
},
89+
) {
90+
for _, rs := range pb.GetResultSetsMeta() {
91+
resultSetsMeta = append(resultSetsMeta, struct {
92+
Columns []struct {
93+
Name string
94+
Type types.Type
95+
}
96+
}{
97+
Columns: func() (
98+
columns []struct {
99+
Name string
100+
Type types.Type
101+
},
102+
) {
103+
for _, c := range rs.GetColumns() {
104+
columns = append(columns, struct {
105+
Name string
106+
Type types.Type
107+
}{
108+
Name: c.GetName(),
109+
Type: types.TypeFromYDB(c.GetType()),
110+
})
111+
}
112+
113+
return columns
114+
}(),
115+
})
116+
}
117+
118+
return resultSetsMeta
119+
}(),
120+
}
121+
}

0 commit comments

Comments
 (0)