Skip to content

Commit 07b8bd2

Browse files
feat(spanner): add OrderBy feature (#10289)
* feat(spanner): add OrderBy feature * fix comment --------- Co-authored-by: Sri Harsha CH <[email protected]>
1 parent 385b6ee commit 07b8bd2

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

spanner/client_test.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,9 @@ func checkReqsForReadOptions(t *testing.T, server InMemSpannerServer, ro ReadOpt
874874
if got, want := sqlReq.DirectedReadOptions, ro.DirectedReadOptions; got.String() != want.String() {
875875
t.Fatalf("Directed Read Options mismatch, got %v, want %v", got, want)
876876
}
877+
if got, want := sqlReq.OrderBy, ro.OrderBy; got != want {
878+
t.Fatalf("OrderBy mismatch, got %v, want %v", got, want)
879+
}
877880
}
878881

879882
func checkReqsForTransactionOptions(t *testing.T, server InMemSpannerServer, txo TransactionOptions) {
@@ -4436,20 +4439,26 @@ func readOptionsTestCases() []ReadOptionsTestCase {
44364439
return []ReadOptionsTestCase{
44374440
{
44384441
name: "Client level",
4439-
client: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag"},
4440-
want: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag"},
4442+
client: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
4443+
want: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
4444+
},
4445+
{
4446+
name: "Client level has precendence when ORDER_BY_UNSPECIFIED at read level",
4447+
client: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
4448+
read: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag"},
4449+
want: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
44414450
},
44424451
{
44434452
name: "Read level",
44444453
client: &ReadOptions{},
4445-
read: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag"},
4446-
want: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag"},
4454+
read: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
4455+
want: &ReadOptions{Index: "testIndex", Limit: 100, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "testRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
44474456
},
44484457
{
44494458
name: "Read level has precedence than client level",
4450-
client: &ReadOptions{Index: "clientIndex", Limit: 10, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "clientRequestTag"},
4451-
read: &ReadOptions{Index: "readIndex", Limit: 20, Priority: sppb.RequestOptions_PRIORITY_MEDIUM, RequestTag: "readRequestTag"},
4452-
want: &ReadOptions{Index: "readIndex", Limit: 20, Priority: sppb.RequestOptions_PRIORITY_MEDIUM, RequestTag: "readRequestTag"},
4459+
client: &ReadOptions{Index: "clientIndex", Limit: 10, Priority: sppb.RequestOptions_PRIORITY_LOW, RequestTag: "clientRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_NO_ORDER},
4460+
read: &ReadOptions{Index: "readIndex", Limit: 20, Priority: sppb.RequestOptions_PRIORITY_MEDIUM, RequestTag: "readRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_PRIMARY_KEY},
4461+
want: &ReadOptions{Index: "readIndex", Limit: 20, Priority: sppb.RequestOptions_PRIORITY_MEDIUM, RequestTag: "readRequestTag", OrderBy: sppb.ReadRequest_ORDER_BY_PRIMARY_KEY},
44534462
},
44544463
}
44554464
}

spanner/transaction.go

+12
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ type ReadOptions struct {
182182
// ReadOptions option used to set the DirectedReadOptions for all ReadRequests which indicate
183183
// which replicas or regions should be used for running read operations.
184184
DirectedReadOptions *sppb.DirectedReadOptions
185+
186+
// An option to control the order in which rows are returned from a read.
187+
OrderBy sppb.ReadRequest_OrderBy
185188
}
186189

187190
// merge combines two ReadOptions that the input parameter will have higher
@@ -194,6 +197,7 @@ func (ro ReadOptions) merge(opts ReadOptions) ReadOptions {
194197
RequestTag: ro.RequestTag,
195198
DataBoostEnabled: ro.DataBoostEnabled,
196199
DirectedReadOptions: ro.DirectedReadOptions,
200+
OrderBy: ro.OrderBy,
197201
}
198202
if opts.Index != "" {
199203
merged.Index = opts.Index
@@ -213,6 +217,9 @@ func (ro ReadOptions) merge(opts ReadOptions) ReadOptions {
213217
if opts.DirectedReadOptions != nil {
214218
merged.DirectedReadOptions = opts.DirectedReadOptions
215219
}
220+
if opts.OrderBy != sppb.ReadRequest_ORDER_BY_UNSPECIFIED {
221+
merged.OrderBy = opts.OrderBy
222+
}
216223
return merged
217224
}
218225

@@ -245,6 +252,7 @@ func (t *txReadOnly) ReadWithOptions(ctx context.Context, table string, keys Key
245252
requestTag := t.ro.RequestTag
246253
dataBoostEnabled := t.ro.DataBoostEnabled
247254
directedReadOptions := t.ro.DirectedReadOptions
255+
orderBy := t.ro.OrderBy
248256
if opts != nil {
249257
index = opts.Index
250258
if opts.Limit > 0 {
@@ -258,6 +266,9 @@ func (t *txReadOnly) ReadWithOptions(ctx context.Context, table string, keys Key
258266
if opts.DirectedReadOptions != nil {
259267
directedReadOptions = opts.DirectedReadOptions
260268
}
269+
if opts.OrderBy != sppb.ReadRequest_ORDER_BY_UNSPECIFIED {
270+
orderBy = opts.OrderBy
271+
}
261272
}
262273
var setTransactionID func(transactionID)
263274
if _, ok := ts.Selector.(*sppb.TransactionSelector_Begin); ok {
@@ -285,6 +296,7 @@ func (t *txReadOnly) ReadWithOptions(ctx context.Context, table string, keys Key
285296
RequestOptions: createRequestOptions(prio, requestTag, t.txOpts.TransactionTag),
286297
DataBoostEnabled: dataBoostEnabled,
287298
DirectedReadOptions: directedReadOptions,
299+
OrderBy: orderBy,
288300
})
289301
if err != nil {
290302
if _, ok := t.getTransactionSelector().GetSelector().(*sppb.TransactionSelector_Begin); ok {

0 commit comments

Comments
 (0)