Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(cosmosdb): drop the azcosmos fork for upstream v1.6.0-beta.4 (#21886)

Danny Xubfd35d9c6401
diff --git a/backend/plugin/db/cosmosdb/cosmosdb.go b/backend/plugin/db/cosmosdb/cosmosdb.go
index c5ef0a7648..d0903d710d 100644
--- a/backend/plugin/db/cosmosdb/cosmosdb.go
+++ b/backend/plugin/db/cosmosdb/cosmosdb.go
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
+ "strings"
"time"
"connectrpc.com/connect"
@@ -18,6 +19,8 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pkg/errors"
+ "github.com/bytebase/bytebase/backend/plugin/db/cosmosdb/queryengine/gonative"
+
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -170,24 +173,9 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
}
}
- pager := container.NewCrossPartitionQueryItemsPager(statement, queryOption)
- var items [][]byte
- for pager.More() {
- response, err := pager.NextPage(ctx)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to read more items"))
- }
- var stop bool
- for _, item := range response.Items {
- items = append(items, item)
- if queryContext.Limit > 0 && len(items) >= queryContext.Limit {
- stop = true
- break
- }
- }
- if stop {
- break
- }
+ items, err := QueryEveryPartition(ctx, container, statement, queryOption, queryContext.Limit)
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to read more items"))
}
result := &v1pb.QueryResult{
@@ -208,3 +196,61 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
return []*v1pb.QueryResult{result}, nil
}
+
+// QueryEveryPartition runs statement across every partition of the container.
+// The gateway serves what it can on its own; an aggregate, DISTINCT, ORDER BY,
+// TOP, OFFSET/LIMIT or GROUP BY that spans partitions it refuses with a
+// BadRequest instead, and the query then runs again through the pure-Go engine,
+// which merges the per-partition streams itself.
+//
+// Only a query that has returned nothing yet can switch: once the gateway has
+// handed back a continuation token, the engine cannot resume from it.
+func QueryEveryPartition(ctx context.Context, container *azcosmos.ContainerClient, statement string, o *azcosmos.QueryOptions, limit int) ([][]byte, error) {
+ items, pages, err := drainQuery(ctx, container, statement, o, limit)
+ if err == nil || pages > 0 || !isCrossPartitionUnsupported(err) {
+ return items, err
+ }
+
+ withEngine := azcosmos.QueryOptions{}
+ if o != nil {
+ withEngine = *o
+ }
+ withEngine.QueryEngine = gonative.Default()
+ items, _, err = drainQuery(ctx, container, statement, &withEngine, limit)
+ return items, err
+}
+
+// drainQuery pages a query to exhaustion or to limit, reporting how many pages
+// it read so the caller can tell a query that failed outright from one that
+// failed partway through.
+func drainQuery(ctx context.Context, container *azcosmos.ContainerClient, statement string, o *azcosmos.QueryOptions, limit int) ([][]byte, int, error) {
+ pager := container.NewQueryItemsPager(statement, azcosmos.NewPartitionKey(), o)
+ var items [][]byte
+ pages := 0
+ for pager.More() {
+ response, err := pager.NextPage(ctx)
+ if err != nil {
+ return nil, pages, err
+ }
+ pages++
+ for _, item := range response.Items {
+ items = append(items, item)
+ if limit > 0 && len(items) >= limit {
+ return items, pages, nil
+ }
+ }
+ }
+ return items, pages, nil
+}
+
+// isCrossPartitionUnsupported reports whether err is the gateway saying it
+// cannot serve this query across partitions by itself.
+func isCrossPartitionUnsupported(err error) bool {
+ var responseErr *azcore.ResponseError
+ if !errors.As(err, &responseErr) || responseErr.StatusCode != http.StatusBadRequest {
+ return false
+ }
+ msg := err.Error()
+ return strings.Contains(msg, "cross partition query can not be directly served by the gateway") ||
+ strings.Contains(msg, "Cross partition query only supports")
+}
diff --git a/backend/plugin/db/cosmosdb/cosmosdb_test.go b/backend/plugin/db/cosmosdb/cosmosdb_test.go
index d9edc8f189..07dfeaf4f2 100644
--- a/backend/plugin/db/cosmosdb/cosmosdb_test.go
+++ b/backend/plugin/db/cosmosdb/cosmosdb_test.go
@@ -1,11 +1,10 @@
// Copyright (c) Bytebase (Hong Kong) Limited.
package cosmosdb
-// Two suites cover the cross-partition query path, the one thing the forked
-// azcosmos SDK adds over upstream: the gateway cannot serve an aggregate,
-// DISTINCT, ORDER BY, TOP, OFFSET/LIMIT or GROUP BY that spans partitions, so
-// NewCrossPartitionQueryItemsPager pivots to a query engine that merges the
-// per-partition streams itself.
+// Two suites cover the cross-partition query path: the gateway cannot serve an
+// aggregate, DISTINCT, ORDER BY, TOP, OFFSET/LIMIT or GROUP BY that spans
+// partitions, so QueryEveryPartition pivots to the pure-Go query engine in
+// queryengine/gonative, which merges the per-partition streams itself.
//
// TestCrossPartition* run on the emulator SharedCosmosContainer starts, against
// a fixture whose contents are known, and so assert exact answers. Both suites
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/agg.go b/backend/plugin/db/cosmosdb/queryengine/gonative/agg.go
new file mode 100644
index 0000000000..2eb8e0ea63
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/agg.go
@@ -0,0 +1,218 @@
+package gonative
+
+import (
+ "encoding/json"
+ "strconv"
+
+ "github.com/pkg/errors"
+)
+
+// aggregator is the interface the pipeline uses to accumulate per-partition
+// partial values and produce a single finalized scalar.
+//
+// feedPartial receives a JSON-encoded partial whose shape depends on the
+// aggregate kind:
+//
+// Count, Sum — bare number, e.g. 18 or 20442258
+// Avg — {"sum": <number>, "count": <int>}
+// Min — {"min": <value>, "count": <int>}
+// Max — {"max": <value>, "count": <int>}
+//
+// finalize returns the final scalar as a json.RawMessage so the pipeline can
+// emit it without re-marshaling. Returns an empty value (JSON null) when no
+// contributing rows were observed (e.g. Avg over an empty set).
+type aggregator interface {
+ feedPartial(raw json.RawMessage) error
+ finalize() (json.RawMessage, error)
+}
+
+// newAggregator constructs an aggregator for the named kind. Returns nil for
+// an unrecognized kind so the caller can surface an explicit error.
+//
+// Gateway plans emit either "Average" or "Avg" for the mean aggregate.
+// Accepting both avoids surprises if a future gateway version shifts.
+func newAggregator(kind string) aggregator {
+ switch kind {
+ case "Count":
+ return &countAgg{}
+ case "Sum":
+ return &sumAgg{}
+ case "Min":
+ return &minAgg{}
+ case "Max":
+ return &maxAgg{}
+ case "Average", "Avg":
+ return &avgAgg{}
+ }
+ return nil
+}
+
+// -----------------------------------------------------------------------------
+// Count
+
+type countAgg struct {
+ total float64
+}
+
+func (a *countAgg) feedPartial(raw json.RawMessage) error {
+ var n float64
+ if err := json.Unmarshal(raw, &n); err != nil {
+ return errors.Wrap(err, "gonative/agg.count")
+ }
+ a.total += n
+ return nil
+}
+
+func (a *countAgg) finalize() (json.RawMessage, error) {
+ return numberRawMessage(a.total), nil
+}
+
+// -----------------------------------------------------------------------------
+// Sum
+
+type sumAgg struct {
+ total float64
+ seen bool
+}
+
+func (a *sumAgg) feedPartial(raw json.RawMessage) error {
+ var n float64
+ if err := json.Unmarshal(raw, &n); err != nil {
+ return errors.Wrap(err, "gonative/agg.sum")
+ }
+ a.total += n
+ a.seen = true
+ return nil
+}
+
+func (a *sumAgg) finalize() (json.RawMessage, error) {
+ if !a.seen {
+ return json.RawMessage(`null`), nil
+ }
+ return numberRawMessage(a.total), nil
+}
+
+// -----------------------------------------------------------------------------
+// Min
+
+type minAgg struct {
+ best json.RawMessage
+}
+
+type minPartial struct {
+ Min json.RawMessage `json:"min"`
+ Count int `json:"count"`
+}
+
+func (a *minAgg) feedPartial(raw json.RawMessage) error {
+ var p minPartial
+ if err := json.Unmarshal(raw, &p); err != nil {
+ return errors.Wrap(err, "gonative/agg.min")
+ }
+ if p.Count == 0 {
+ // Partition contributed no values — ignore.
+ return nil
+ }
+ if a.best == nil {
+ a.best = append([]byte(nil), p.Min...)
+ return nil
+ }
+ cmp, err := compareItems(p.Min, a.best)
+ if err != nil {
+ return errors.Wrap(err, "gonative/agg.min")
+ }
+ if cmp < 0 {
+ a.best = append([]byte(nil), p.Min...)
+ }
+ return nil
+}
+
+func (a *minAgg) finalize() (json.RawMessage, error) {
+ if a.best == nil {
+ return json.RawMessage(`null`), nil
+ }
+ return a.best, nil
+}
+
+// -----------------------------------------------------------------------------
+// Max
+
+type maxAgg struct {
+ best json.RawMessage
+}
+
+type maxPartial struct {
+ Max json.RawMessage `json:"max"`
+ Count int `json:"count"`
+}
+
+func (a *maxAgg) feedPartial(raw json.RawMessage) error {
+ var p maxPartial
+ if err := json.Unmarshal(raw, &p); err != nil {
+ return errors.Wrap(err, "gonative/agg.max")
+ }
+ if p.Count == 0 {
+ return nil
+ }
+ if a.best == nil {
+ a.best = append([]byte(nil), p.Max...)
+ return nil
+ }
+ cmp, err := compareItems(p.Max, a.best)
+ if err != nil {
+ return errors.Wrap(err, "gonative/agg.max")
+ }
+ if cmp > 0 {
+ a.best = append([]byte(nil), p.Max...)
+ }
+ return nil
+}
+
+func (a *maxAgg) finalize() (json.RawMessage, error) {
+ if a.best == nil {
+ return json.RawMessage(`null`), nil
+ }
+ return a.best, nil
+}
+
+// -----------------------------------------------------------------------------
+// Avg
+
+type avgAgg struct {
+ sum float64
+ count int64
+}
+
+type avgPartial struct {
+ Sum float64 `json:"sum"`
+ Count int64 `json:"count"`
+}
+
+func (a *avgAgg) feedPartial(raw json.RawMessage) error {
+ var p avgPartial
+ if err := json.Unmarshal(raw, &p); err != nil {
+ return errors.Wrap(err, "gonative/agg.avg")
+ }
+ a.sum += p.Sum
+ a.count += p.Count
+ return nil
+}
+
+func (a *avgAgg) finalize() (json.RawMessage, error) {
+ if a.count == 0 {
+ // Matches Cosmos semantics: AVG over an empty set is undefined;
+ // gateway serializes this as JSON null for cross-partition results.
+ return json.RawMessage(`null`), nil
+ }
+ return numberRawMessage(a.sum / float64(a.count)), nil
+}
+
+// numberRawMessage encodes a float64 as a compact JSON number. Prefers integer
+// formatting when the value is an exact integer — matches the .NET SDK's
+// observed behavior of emitting `18` rather than `18.0`.
+func numberRawMessage(v float64) json.RawMessage {
+ if v == float64(int64(v)) {
+ return json.RawMessage(strconv.FormatInt(int64(v), 10))
+ }
+ return json.RawMessage(strconv.FormatFloat(v, 'g', -1, 64))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/agg_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/agg_test.go
new file mode 100644
index 0000000000..526f2691b6
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/agg_test.go
@@ -0,0 +1,172 @@
+package gonative
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNewAggregator_KnownKinds(t *testing.T) {
+ for _, kind := range []string{"Count", "Sum", "Min", "Max", "Average", "Avg"} {
+ t.Run(kind, func(t *testing.T) {
+ require.NotNil(t, newAggregator(kind))
+ })
+ }
+}
+
+func TestNewAggregator_UnknownKind(t *testing.T) {
+ assert.Nil(t, newAggregator("Median"))
+ assert.Nil(t, newAggregator(""))
+}
+
+// feedFinal is a small helper — feed a sequence of partials then finalize.
+func feedFinal(t *testing.T, a aggregator, partials ...json.RawMessage) json.RawMessage {
+ t.Helper()
+ for _, p := range partials {
+ require.NoError(t, a.feedPartial(p))
+ }
+ out, err := a.finalize()
+ require.NoError(t, err)
+ return out
+}
+
+// ---------------------------------------------------------- Count
+
+func TestCountAgg_EmptyFinalizesToZero(t *testing.T) {
+ out := feedFinal(t, newAggregator("Count"))
+ assert.Equal(t, json.RawMessage(`0`), out)
+}
+
+func TestCountAgg_SinglePartition(t *testing.T) {
+ out := feedFinal(t, newAggregator("Count"), []byte(`5`))
+ assert.Equal(t, json.RawMessage(`5`), out)
+}
+
+func TestCountAgg_SumsAcrossPartitions(t *testing.T) {
+ out := feedFinal(t, newAggregator("Count"), []byte(`5`), []byte(`0`), []byte(`13`))
+ assert.Equal(t, json.RawMessage(`18`), out)
+}
+
+// ---------------------------------------------------------- Sum
+
+func TestSumAgg_EmptyFinalizesToNull(t *testing.T) {
+ out := feedFinal(t, newAggregator("Sum"))
+ assert.Equal(t, json.RawMessage(`null`), out)
+}
+
+func TestSumAgg_SumsAcrossPartitions(t *testing.T) {
+ out := feedFinal(t, newAggregator("Sum"), []byte(`10`), []byte(`20.5`), []byte(`-5`))
+ // 25.5 encodes as the short form.
+ assert.Equal(t, json.RawMessage(`25.5`), out)
+}
+
+// ---------------------------------------------------------- Avg
+
+func TestAvgAgg_EmptyFinalizesToNull(t *testing.T) {
+ out := feedFinal(t, newAggregator("Avg"))
+ assert.Equal(t, json.RawMessage(`null`), out)
+}
+
+func TestAvgAgg_SinglePartition(t *testing.T) {
+ out := feedFinal(t, newAggregator("Avg"), []byte(`{"sum":100,"count":4}`))
+ assert.Equal(t, json.RawMessage(`25`), out)
+}
+
+func TestAvgAgg_MergesAcrossPartitions(t *testing.T) {
+ // (5+3)/(2+2) = 2.
+ out := feedFinal(t, newAggregator("Avg"), []byte(`{"sum":5,"count":2}`), []byte(`{"sum":3,"count":2}`))
+ assert.Equal(t, json.RawMessage(`2`), out)
+}
+
+func TestAvgAgg_ZeroCountPartialContributesNothing(t *testing.T) {
+ out := feedFinal(t, newAggregator("Avg"),
+ []byte(`{"sum":0,"count":0}`),
+ []byte(`{"sum":10,"count":5}`),
+ )
+ assert.Equal(t, json.RawMessage(`2`), out)
+}
+
+// ---------------------------------------------------------- Min
+
+func TestMinAgg_EmptyFinalizesToNull(t *testing.T) {
+ out := feedFinal(t, newAggregator("Min"))
+ assert.Equal(t, json.RawMessage(`null`), out)
+}
+
+func TestMinAgg_SinglePartitionNumeric(t *testing.T) {
+ out := feedFinal(t, newAggregator("Min"), []byte(`{"min":42,"count":5}`))
+ assert.Equal(t, json.RawMessage(`42`), out)
+}
+
+func TestMinAgg_AcrossPartitions_TakesSmallest(t *testing.T) {
+ out := feedFinal(t, newAggregator("Min"),
+ []byte(`{"min":100,"count":5}`),
+ []byte(`{"min":7,"count":3}`),
+ []byte(`{"min":50,"count":2}`),
+ )
+ assert.Equal(t, json.RawMessage(`7`), out)
+}
+
+func TestMinAgg_IgnoresZeroCountPartitions(t *testing.T) {
+ out := feedFinal(t, newAggregator("Min"),
+ []byte(`{"count":0}`), // empty partition — "min" missing, count=0 → skip
+ []byte(`{"min":99,"count":1}`),
+ )
+ assert.Equal(t, json.RawMessage(`99`), out)
+}
+
+func TestMinAgg_MixedTypesUsesCosmosOrdering(t *testing.T) {
+ // null < bool < number < string; Min should pick the smallest type-class.
+ out := feedFinal(t, newAggregator("Min"),
+ []byte(`{"min":"apple","count":1}`),
+ []byte(`{"min":42,"count":1}`),
+ )
+ assert.Equal(t, json.RawMessage(`42`), out)
+}
+
+// ---------------------------------------------------------- Max
+
+func TestMaxAgg_EmptyFinalizesToNull(t *testing.T) {
+ out := feedFinal(t, newAggregator("Max"))
+ assert.Equal(t, json.RawMessage(`null`), out)
+}
+
+func TestMaxAgg_AcrossPartitions_TakesLargest(t *testing.T) {
+ out := feedFinal(t, newAggregator("Max"),
+ []byte(`{"max":100,"count":5}`),
+ []byte(`{"max":8982000,"count":3}`),
+ []byte(`{"max":50,"count":2}`),
+ )
+ assert.Equal(t, json.RawMessage(`8982000`), out)
+}
+
+func TestMaxAgg_IgnoresZeroCountPartitions(t *testing.T) {
+ out := feedFinal(t, newAggregator("Max"),
+ []byte(`{"count":0}`),
+ []byte(`{"max":7,"count":1}`),
+ )
+ assert.Equal(t, json.RawMessage(`7`), out)
+}
+
+// ---------------------------------------------------------- Error paths
+
+func TestAggregators_RejectBadPartials(t *testing.T) {
+ for _, kind := range []string{"Count", "Sum", "Avg", "Min", "Max"} {
+ t.Run(kind, func(t *testing.T) {
+ a := newAggregator(kind)
+ err := a.feedPartial([]byte(`not json`))
+ require.Error(t, err)
+ })
+ }
+}
+
+// ---------------------------------------------------------- Format regression
+
+func TestNumberRawMessage_IntegerFormatting(t *testing.T) {
+ assert.Equal(t, json.RawMessage(`18`), numberRawMessage(18))
+ assert.Equal(t, json.RawMessage(`-5`), numberRawMessage(-5))
+ assert.Equal(t, json.RawMessage(`0`), numberRawMessage(0))
+ assert.Equal(t, json.RawMessage(`1.5`), numberRawMessage(1.5))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/cmp.go b/backend/plugin/db/cosmosdb/queryengine/gonative/cmp.go
new file mode 100644
index 0000000000..1cf3cadcdb
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/cmp.go
@@ -0,0 +1,115 @@
+package gonative
+
+import (
+ "bytes"
+ "encoding/json"
+ "strings"
+
+ "github.com/pkg/errors"
+)
+
+// jsonType enumerates the Cosmos item-ordering classes in ascending order:
+//
+// undefined < null < bool < number < string
+//
+// Arrays and objects are not orderable in the Cosmos item-comparison rules;
+// callers that encounter them should treat the result as an error.
+type jsonType int
+
+const (
+ jsonUndefined jsonType = iota
+ jsonNull
+ jsonBool
+ jsonNumber
+ jsonString
+ jsonUnorderable // arrays, objects — reject rather than compare
+)
+
+// detectJSONType inspects raw JSON (possibly leading whitespace) and returns
+// its Cosmos ordering class.
+func detectJSONType(raw json.RawMessage) jsonType {
+ trimmed := bytes.TrimLeft(raw, " \t\n\r")
+ if len(trimmed) == 0 {
+ return jsonUndefined
+ }
+ switch trimmed[0] {
+ case 'n':
+ return jsonNull
+ case 't', 'f':
+ return jsonBool
+ case '"':
+ return jsonString
+ case '[', '{':
+ return jsonUnorderable
+ }
+ // digit, minus, plus, or dot → number
+ if (trimmed[0] >= '0' && trimmed[0] <= '9') || trimmed[0] == '-' || trimmed[0] == '+' || trimmed[0] == '.' {
+ return jsonNumber
+ }
+ return jsonUndefined
+}
+
+// compareItems compares two JSON-encoded scalars using Cosmos item ordering.
+// Return values follow the usual convention: -1, 0, +1.
+// Returns an error when either side is unorderable (array/object).
+func compareItems(a, b json.RawMessage) (int, error) {
+ ta, tb := detectJSONType(a), detectJSONType(b)
+ if ta == jsonUnorderable || tb == jsonUnorderable {
+ return 0, errors.New("gonative: cannot compare unorderable values (arrays/objects)")
+ }
+ if ta != tb {
+ if ta < tb {
+ return -1, nil
+ }
+ return 1, nil
+ }
+ // Same type — compare within the type.
+ switch ta {
+ case jsonUndefined, jsonNull:
+ // All undefined == undefined; all null == null.
+ return 0, nil
+ case jsonBool:
+ var av, bv bool
+ if err := json.Unmarshal(a, &av); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp bool a")
+ }
+ if err := json.Unmarshal(b, &bv); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp bool b")
+ }
+ switch {
+ case av == bv:
+ return 0, nil
+ case !av && bv:
+ return -1, nil
+ default:
+ return 1, nil
+ }
+ case jsonNumber:
+ var av, bv float64
+ if err := json.Unmarshal(a, &av); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp number a")
+ }
+ if err := json.Unmarshal(b, &bv); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp number b")
+ }
+ switch {
+ case av == bv:
+ return 0, nil
+ case av < bv:
+ return -1, nil
+ default:
+ return 1, nil
+ }
+ case jsonString:
+ var av, bv string
+ if err := json.Unmarshal(a, &av); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp string a")
+ }
+ if err := json.Unmarshal(b, &bv); err != nil {
+ return 0, errors.Wrap(err, "gonative: cmp string b")
+ }
+ return strings.Compare(av, bv), nil
+ default:
+ return 0, errors.Errorf("gonative: unknown jsonType %d", ta)
+ }
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/cmp_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/cmp_test.go
new file mode 100644
index 0000000000..36248c3fc2
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/cmp_test.go
@@ -0,0 +1,93 @@
+package gonative
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestCompareItems_TypeOrder(t *testing.T) {
+ // Cosmos order: undefined < null < bool < number < string.
+ // We emulate "undefined" with an empty RawMessage.
+ cases := []struct {
+ name string
+ a, b json.RawMessage
+ wantCmp int
+ }{
+ {"undefined<null", json.RawMessage(``), json.RawMessage(`null`), -1},
+ {"null<bool", json.RawMessage(`null`), json.RawMessage(`false`), -1},
+ {"bool<number", json.RawMessage(`true`), json.RawMessage(`0`), -1},
+ {"number<string", json.RawMessage(`42`), json.RawMessage(`"a"`), -1},
+ {"string>bool", json.RawMessage(`"a"`), json.RawMessage(`true`), 1},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := compareItems(tc.a, tc.b)
+ require.NoError(t, err)
+ assert.Equal(t, tc.wantCmp, got)
+ })
+ }
+}
+
+func TestCompareItems_WithinType(t *testing.T) {
+ cases := []struct {
+ name string
+ a, b json.RawMessage
+ wantCmp int
+ }{
+ {"bool false<true", json.RawMessage(`false`), json.RawMessage(`true`), -1},
+ {"bool equal true", json.RawMessage(`true`), json.RawMessage(`true`), 0},
+ {"number ascending", json.RawMessage(`1.5`), json.RawMessage(`2`), -1},
+ {"number equal", json.RawMessage(`42`), json.RawMessage(`42`), 0},
+ {"number descending", json.RawMessage(`10`), json.RawMessage(`-3`), 1},
+ {"string lex", json.RawMessage(`"AD"`), json.RawMessage(`"AE"`), -1},
+ {"string equal", json.RawMessage(`"AE"`), json.RawMessage(`"AE"`), 0},
+ {"null equal", json.RawMessage(`null`), json.RawMessage(`null`), 0},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := compareItems(tc.a, tc.b)
+ require.NoError(t, err)
+ assert.Equal(t, tc.wantCmp, got)
+ })
+ }
+}
+
+func TestCompareItems_UnorderableFails(t *testing.T) {
+ _, err := compareItems(json.RawMessage(`[1,2]`), json.RawMessage(`3`))
+ require.Error(t, err)
+ _, err = compareItems(json.RawMessage(`{}`), json.RawMessage(`null`))
+ require.Error(t, err)
+}
+
+func TestCompareItems_HandlesLeadingWhitespace(t *testing.T) {
+ got, err := compareItems(json.RawMessage(` 1 `), json.RawMessage(`2`))
+ require.NoError(t, err)
+ assert.Equal(t, -1, got)
+}
+
+func TestDetectJSONType(t *testing.T) {
+ cases := []struct {
+ raw string
+ want jsonType
+ }{
+ {``, jsonUndefined},
+ {`null`, jsonNull},
+ {`true`, jsonBool},
+ {`false`, jsonBool},
+ {`0`, jsonNumber},
+ {`42`, jsonNumber},
+ {`-1.5`, jsonNumber},
+ {`"x"`, jsonString},
+ {`[1,2]`, jsonUnorderable},
+ {`{"a":1}`, jsonUnorderable},
+ {` "pad" `, jsonString},
+ }
+ for _, tc := range cases {
+ t.Run(tc.raw, func(t *testing.T) {
+ assert.Equal(t, tc.want, detectJSONType(json.RawMessage(tc.raw)))
+ })
+ }
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/distinct.go b/backend/plugin/db/cosmosdb/queryengine/gonative/distinct.go
new file mode 100644
index 0000000000..cd2ebb6ca5
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/distinct.go
@@ -0,0 +1,175 @@
+package gonative
+
+import (
+ "encoding/json"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/pkg/errors"
+)
+
+// distinctPartitionResponse decodes the per-partition response body for a
+// DISTINCT query. Unlike aggregate pipelines, the per-partition Documents is
+// an array of raw JSON values: one projected object per object-form DISTINCT,
+// or one scalar per DISTINCT VALUE. We preserve them as json.RawMessage so the
+// de-duplication stage can hash the canonical form without type-specific code.
+type distinctPartitionResponse struct {
+ Documents []json.RawMessage `json:"Documents"`
+}
+
+// parseDistinctPartitionResponse decodes a DISTINCT per-partition body.
+func parseDistinctPartitionResponse(raw []byte) (*distinctPartitionResponse, error) {
+ var r distinctPartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse distinct partition response")
+ }
+ return &r, nil
+}
+
+// canonicalJSON re-marshals the given JSON value so that semantically equal
+// values produce byte-equal output (sorted object keys, no extraneous whitespace).
+// Returns the canonical bytes so the caller can use them as a map key.
+//
+// The approach: unmarshal into `any`, which turns objects into map[string]any,
+// then re-marshal — Go's encoding/json sorts map keys alphabetically in that
+// path, which gives us the canonical ordering for free.
+func canonicalJSON(raw json.RawMessage) ([]byte, error) {
+ var v any
+ if err := json.Unmarshal(raw, &v); err != nil {
+ return nil, errors.Wrap(err, "gonative: canonicalize")
+ }
+ return json.Marshal(v)
+}
+
+// distinctPipeline implements queryengine.QueryPipeline for unordered DISTINCT
+// queries. It streams raw documents in the order partitions deliver them and
+// emits each unique value the first time it is seen. The pipeline works
+// uniformly for object DISTINCT (SELECT DISTINCT c.field FROM c) and
+// DISTINCT VALUE (SELECT DISTINCT VALUE c.field FROM c) because both forms
+// land in the Documents array as raw JSON values.
+//
+// The seen-set grows unbounded — same as the .NET SDK's
+// Microsoft.Azure.Cosmos.UnorderedDistinctMap (see
+// https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/Distinct/DistinctMap.UnorderedDistinctMap.cs).
+// The OS is the memory ceiling. Queries whose DISTINCT cardinality could
+// exhaust memory should scope themselves with a WHERE clause.
+type distinctPipeline struct {
+ query string
+ pkRangeIDs []string
+ partitionsDone map[string]bool
+ issued bool
+ closed bool
+
+ // Pending items not yet returned to the caller. Populated during
+ // ProvideData as each partition's response is de-duplicated against the
+ // seen set.
+ pending [][]byte
+
+ // seen is the global hash-set of canonicalized documents. Keys are the
+ // canonical JSON bytes rendered as a string — Go strings are immutable
+ // and comparable, so they make lightweight map keys even for long JSON.
+ seen map[string]struct{}
+}
+
+// newDistinctPipeline builds a pipeline for a plan whose distinctType is
+// "Unordered". Returns an error if the plan's distinctType is "Ordered" —
+// that form composes with Stage 3's ORDER BY merge and is not handled here.
+func newDistinctPipeline(plan *planDoc, pkRangeIDs []string) (*distinctPipeline, error) {
+ if plan.QueryInfo.DistinctType != "Unordered" {
+ // "Ordered" is Stage 3's responsibility; anything else is unexpected.
+ return nil, ErrUnsupportedPlanFeature
+ }
+ // DISTINCT plans carry an empty rewrittenQuery — run the original query
+ // against each partition. The pipeline needs the original query text to
+ // populate QueryRequest.Query; the caller passes it via CreateQueryPipeline.
+ return &distinctPipeline{
+ pkRangeIDs: append([]string(nil), pkRangeIDs...),
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ seen: make(map[string]struct{}),
+ }, nil
+}
+
+// Query implements queryengine.QueryPipeline.
+func (p *distinctPipeline) Query() string { return p.query }
+
+// IsComplete implements queryengine.QueryPipeline. A DISTINCT pipeline is
+// complete once every partition has delivered and every deduped item has been
+// flushed to the caller.
+func (p *distinctPipeline) IsComplete() bool {
+ if !p.issued {
+ return false
+ }
+ if len(p.pending) > 0 {
+ return false
+ }
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ return true
+}
+
+// Close implements queryengine.QueryPipeline.
+func (p *distinctPipeline) Close() { p.closed = true }
+
+// Run drives one pipeline turn:
+// 1. First call — emit one QueryRequest per pk-range carrying the original query.
+// 2. Subsequent calls — flush any pending deduplicated items; otherwise wait
+// for the SDK to deliver more partition responses via ProvideData.
+func (p *distinctPipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ reqs := make([]queryengine.QueryRequest, len(p.pkRangeIDs))
+ for i, id := range p.pkRangeIDs {
+ reqs[i] = queryengine.QueryRequest{
+ PartitionKeyRangeID: id,
+ Id: uint64(i),
+ Query: p.query,
+ IncludeParameters: true,
+ }
+ }
+ p.issued = true
+ return &queryengine.PipelineResult{Requests: reqs}, nil
+ }
+ if len(p.pending) > 0 {
+ batch := p.pending
+ p.pending = nil
+ return &queryengine.PipelineResult{
+ Items: batch,
+ IsCompleted: p.IsComplete(),
+ }, nil
+ }
+ return &queryengine.PipelineResult{IsCompleted: p.IsComplete()}, nil
+}
+
+// ProvideData implements queryengine.QueryPipeline. Each incoming partition
+// response is parsed and its documents are fed through the global seen set.
+// First-sightings become pending items; duplicates are silently dropped.
+func (p *distinctPipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ resp, err := parseDistinctPartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for _, doc := range resp.Documents {
+ canonical, err := canonicalJSON(doc)
+ if err != nil {
+ return err
+ }
+ key := string(canonical)
+ if _, ok := p.seen[key]; ok {
+ continue
+ }
+ p.seen[key] = struct{}{}
+ // Preserve the partition's original document bytes in the output —
+ // callers receive what the gateway returned, not our canonicalized
+ // form. Copy defensively: json.RawMessage is aliased to the input
+ // buffer, which the SDK may reuse for later pages.
+ p.pending = append(p.pending, append([]byte(nil), doc...))
+ }
+ if r.NextContinuation != "" {
+ return errors.New("gonative: distinct partition returned a continuation token (multi-page not supported in Stage 2)")
+ }
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/distinct_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/distinct_test.go
new file mode 100644
index 0000000000..332bf9b50f
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/distinct_test.go
@@ -0,0 +1,208 @@
+package gonative
+
+import (
+ "encoding/json"
+ "fmt"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// ------------------------------------------------------------ canonicalJSON
+
+func TestCanonicalJSON_SortsObjectKeys(t *testing.T) {
+ a, err := canonicalJSON([]byte(`{"a": 1, "b": 2}`))
+ require.NoError(t, err)
+ b, err := canonicalJSON([]byte(`{"b": 2, "a": 1}`))
+ require.NoError(t, err)
+ assert.Equal(t, string(a), string(b),
+ "objects with same members but different key order should canonicalize equal")
+}
+
+func TestCanonicalJSON_DropsExtraneousWhitespace(t *testing.T) {
+ a, err := canonicalJSON([]byte(` { "k" : "v" } `))
+ require.NoError(t, err)
+ b, err := canonicalJSON([]byte(`{"k":"v"}`))
+ require.NoError(t, err)
+ assert.Equal(t, string(a), string(b))
+}
+
+func TestCanonicalJSON_DistinctScalars(t *testing.T) {
+ a, err := canonicalJSON([]byte(`"AE"`))
+ require.NoError(t, err)
+ b, err := canonicalJSON([]byte(`"AD"`))
+ require.NoError(t, err)
+ assert.NotEqual(t, string(a), string(b))
+}
+
+func TestCanonicalJSON_NumericEquivalence(t *testing.T) {
+ // JSON 1 and 1.0 are both valid numerics; Go unmarshal treats both as float64,
+ // then re-marshal emits the shortest representation. Both → "1" (same canonical bytes).
+ a, err := canonicalJSON([]byte(`1`))
+ require.NoError(t, err)
+ b, err := canonicalJSON([]byte(`1.0`))
+ require.NoError(t, err)
+ assert.Equal(t, string(a), string(b))
+}
+
+// ------------------------------------------------------------ distinct pipeline
+
+// feedSingle exercises the pipeline through a full cycle with one partition's
+// worth of documents and returns the emitted rows.
+func feedSingle(t *testing.T, p *distinctPipeline, docs []json.RawMessage) [][]byte {
+ t.Helper()
+
+ // First Run() requests partitions.
+ res, err := p.Run()
+ require.NoError(t, err)
+ require.NotEmpty(t, res.Requests, "expected initial partition requests")
+
+ // Simulate the SDK delivering the partition body.
+ body := map[string]any{"Documents": docs, "_count": len(docs)}
+ raw, err := json.Marshal(body)
+ require.NoError(t, err)
+ require.NoError(t, p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: raw},
+ }))
+
+ // Flush pending items.
+ var out [][]byte
+ for {
+ r, err := p.Run()
+ require.NoError(t, err)
+ out = append(out, r.Items...)
+ if r.IsCompleted || (len(r.Items) == 0 && len(r.Requests) == 0 && p.IsComplete()) {
+ break
+ }
+ }
+ return out
+}
+
+func rawMsgs(ss ...string) []json.RawMessage {
+ out := make([]json.RawMessage, len(ss))
+ for i, s := range ss {
+ out[i] = json.RawMessage(s)
+ }
+ return out
+}
+
+func TestDistinctPipeline_SinglePartitionEmitsAll(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ p.query = `SELECT DISTINCT c.country FROM c`
+
+ items := feedSingle(t, p, rawMsgs(`{"country":"AD"}`, `{"country":"AE"}`, `{"country":"DE"}`))
+ assert.Len(t, items, 3)
+ assert.True(t, p.IsComplete())
+}
+
+func TestDistinctPipeline_DedupesWithinPartition(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ items := feedSingle(t, p, rawMsgs(`{"country":"AE"}`, `{"country":"AE"}`, `{"country":"AD"}`))
+ assert.Len(t, items, 2)
+}
+
+func TestDistinctPipeline_DedupesAcrossPartitions(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0", "1", "2"})
+ require.NoError(t, err)
+
+ // Kick off the request fan-out.
+ res, err := p.Run()
+ require.NoError(t, err)
+ require.Len(t, res.Requests, 3)
+
+ mkBody := func(docs ...string) []byte {
+ b, _ := json.Marshal(map[string]any{"Documents": rawMsgs(docs...)})
+ return b
+ }
+ require.NoError(t, p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: mkBody(`{"c":"AE"}`, `{"c":"US"}`)},
+ {PartitionKeyRangeID: "1", Data: mkBody(`{"c":"US"}`, `{"c":"GB"}`)},
+ {PartitionKeyRangeID: "2", Data: mkBody(`{"c":"AE"}`, `{"c":"DE"}`)},
+ }))
+
+ var out [][]byte
+ for i := 0; i < 5 && !p.IsComplete(); i++ {
+ r, err := p.Run()
+ require.NoError(t, err)
+ out = append(out, r.Items...)
+ }
+ // Four unique countries: AE, US, GB, DE.
+ assert.Len(t, out, 4)
+}
+
+func TestDistinctPipeline_TreatsKeyReorderedObjectsAsDuplicates(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ items := feedSingle(t, p, rawMsgs(
+ `{"a": 1, "b": 2}`,
+ `{"b": 2, "a": 1}`, // same value, different key order
+ ))
+ assert.Len(t, items, 1, "canonicalization must treat these as the same")
+}
+
+func TestDistinctPipeline_DistinctValueScalars(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ plan.QueryInfo.HasSelectValue = true
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ // DISTINCT VALUE emits bare scalars in Documents[].
+ items := feedSingle(t, p, rawMsgs(`"1"`, `"2"`, `"1"`, `"3"`))
+ assert.Len(t, items, 3)
+}
+
+func TestDistinctPipeline_RejectsOrderedDistinctType(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Ordered"
+ _, err := newDistinctPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestDistinctPipeline_RejectsContinuationToken(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ _, _ = p.Run()
+
+ body, _ := json.Marshal(map[string]any{"Documents": rawMsgs(`"AE"`)})
+ err = p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body, NextContinuation: "deadbeef"},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "continuation token")
+}
+
+// Verifies the pipeline accepts high-cardinality inputs unchanged — the seen-set
+// grows unbounded, matching the .NET SDK's UnorderedDistinctMap behavior.
+func TestDistinctPipeline_HighCardinality(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.DistinctType = "Unordered"
+ p, err := newDistinctPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ _, _ = p.Run()
+ docs := make([]json.RawMessage, 100)
+ for i := range docs {
+ docs[i] = json.RawMessage(fmt.Sprintf(`%d`, i))
+ }
+ body, _ := json.Marshal(map[string]any{"Documents": docs})
+ require.NoError(t, p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body},
+ }))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/doc.go b/backend/plugin/db/cosmosdb/queryengine/gonative/doc.go
new file mode 100644
index 0000000000..5cef1119ab
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/doc.go
@@ -0,0 +1,9 @@
+// Package gonative implements queryengine.QueryEngine entirely in Go
+// (no cgo, no native plugins, no external toolchain).
+//
+// The CosmosDB driver engages it per query: the gateway runs what it can on
+// its own, and only a cross-partition query it refuses is replayed through
+// this engine. SupportedFeatures lists the operators the engine implements;
+// a plan that needs anything else fails with ErrUnsupportedPlanFeature, and
+// the caller then surfaces the gateway's original error.
+package gonative
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/engine.go b/backend/plugin/db/cosmosdb/queryengine/gonative/engine.go
new file mode 100644
index 0000000000..8dd9f06aa7
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/engine.go
@@ -0,0 +1,125 @@
+package gonative
+
+import (
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+)
+
+// Engine is the pure-Go implementation of queryengine.QueryEngine.
+// It is constructed by Default(); callers should not instantiate Engine
+// directly because future stages may add required initialization.
+type Engine struct {
+ // (future stages add fields: supportedFeatures set, cardinality caps, etc.)
+}
+
+// Compile-time check that *Engine satisfies the interface.
+var _ queryengine.QueryEngine = (*Engine)(nil)
+
+// Default returns a new Engine with Stage-0 capabilities (none).
+// Callers should treat the returned value as opaque.
+func Default() *Engine {
+ return &Engine{}
+}
+
+// supportedFeatures enumerates the gateway-known feature names the engine
+// can serve. The string is comma-separated per the gateway's query-plan
+// protocol; new feature names are appended as each stage lands.
+//
+// Aggregate — VALUE-form aggregate queries (Stage 1)
+// NonValueAggregate — aliased-form aggregates (Stage 1)
+// MultipleAggregates — multi-aggregate in a single SELECT (Stage 1)
+// DistinctValue — SELECT DISTINCT VALUE c.field FROM c (Stage 2)
+// Distinct — SELECT DISTINCT c.field FROM c — object form (Stage 2)
+// OrderBy — single-key ORDER BY (Stage 3)
+// Top — SELECT TOP N FROM c (Stage 4)
+// OffsetAndLimit — SELECT … ORDER BY … OFFSET N LIMIT M (Stage 5)
+// GroupBy — SELECT … FROM c GROUP BY c.field (Stage 6)
+const supportedFeatures = "Aggregate,NonValueAggregate,MultipleAggregates,Distinct,DistinctValue,OrderBy,Top,OffsetAndLimit,GroupBy"
+
+// SupportedFeatures implements queryengine.QueryEngine.
+func (*Engine) SupportedFeatures() string {
+ return supportedFeatures
+}
+
+// CreateQueryPipeline implements queryengine.QueryEngine. It parses the plan,
+// rejects any feature the engine does not yet support, then dispatches to the
+// appropriate pipeline implementation.
+func (*Engine) CreateQueryPipeline(query string, plan string, pkranges string) (queryengine.QueryPipeline, error) {
+ p, err := parsePlan([]byte(plan))
+ if err != nil {
+ return nil, err
+ }
+ if err := rejectUnsupportedPlan(p); err != nil {
+ return nil, err
+ }
+ rangeIDs, err := parsePKRangeIDs([]byte(pkranges))
+ if err != nil {
+ return nil, err
+ }
+ switch {
+ case len(p.QueryInfo.OrderBy) > 0:
+ return newOrderByPipeline(p, rangeIDs)
+ case p.QueryInfo.DistinctType == "Unordered":
+ pipe, err := newDistinctPipeline(p, rangeIDs)
+ if err != nil {
+ return nil, err
+ }
+ pipe.query = query
+ return pipe, nil
+ case len(p.QueryInfo.Aggregates) > 0:
+ pipe, err := newValuePipeline(p, rangeIDs)
+ if err != nil {
+ return nil, err
+ }
+ return pipe, nil
+ case len(p.QueryInfo.GroupByExpressions) > 0:
+ // GROUP BY plans carry groupByExpressions; the aliased-aggregate
+ // (single-group) path used by Stage 1 has groupByAliasToAggregateType
+ // populated but groupByExpressions empty. The check order matters.
+ return newGroupPipeline(p, rangeIDs)
+ case len(p.QueryInfo.GroupByAliasToAggregateType) > 0:
+ pipe, err := newAliasedPipeline(p, rangeIDs)
+ if err != nil {
+ return nil, err
+ }
+ return pipe, nil
+ case p.QueryInfo.Top != nil:
+ pipe, err := newTopPipeline(p, rangeIDs)
+ if err != nil {
+ return nil, err
+ }
+ pipe.query = query
+ return pipe, nil
+ default:
+ return nil, ErrUnsupportedPlanFeature
+ }
+}
+
+// rejectUnsupportedPlan returns ErrUnsupportedPlanFeature when the plan uses
+// any feature beyond the engine's current scope. Later stages will prune
+// entries from this guard as their operators come online.
+func rejectUnsupportedPlan(p *planDoc) error {
+ qi := p.QueryInfo
+ // "Ordered" DISTINCT composes with ORDER BY merge; reject until Stage 3
+ // wires composition through the merge heap. Stage 3 itself handles plain
+ // ORDER BY — see the dispatch above.
+ if qi.DistinctType == "Ordered" {
+ return ErrUnsupportedPlanFeature
+ }
+ if qi.DistinctType != "" && qi.DistinctType != "None" && qi.DistinctType != "Unordered" {
+ return ErrUnsupportedPlanFeature
+ }
+ // OFFSET/LIMIT always composes with ORDER BY per Cosmos syntax; when that
+ // composition lands the plan has both qi.OrderBy and qi.Offset/qi.Limit,
+ // and the dispatch routes to newOrderByPipeline which handles them.
+ // Plans that set offset/limit without an ORDER BY fall through to the
+ // default case in CreateQueryPipeline and are rejected there.
+ if qi.HasNonStreamingOrderBy {
+ return ErrUnsupportedPlanFeature
+ }
+ return nil
+}
+
+// CreateReadManyPipeline implements queryengine.QueryEngine.
+func (*Engine) CreateReadManyPipeline(_ []queryengine.ItemIdentity, _ string, _ string, _ uint8, _ []string) (queryengine.QueryPipeline, error) {
+ return nil, ErrUnsupportedPlanFeature
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/engine_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/engine_test.go
new file mode 100644
index 0000000000..8bf2d215f9
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/engine_test.go
@@ -0,0 +1,53 @@
+package gonative_test
+
+import (
+ "errors"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/db/cosmosdb/queryengine/gonative"
+)
+
+func TestDefaultReturnsNonNilEngine(t *testing.T) {
+ e := gonative.Default()
+ require.NotNil(t, e)
+}
+
+func TestDefaultAdvertisesStage1Features(t *testing.T) {
+ // Stage 1 advertises aggregate-family features so the gateway emits plans
+ // for VALUE + aliased + multi-aggregate queries.
+ feats := gonative.Default().SupportedFeatures()
+ assert.Contains(t, feats, "Aggregate")
+ assert.Contains(t, feats, "NonValueAggregate")
+ assert.Contains(t, feats, "MultipleAggregates")
+}
+
+func TestDefaultCreateQueryPipelineRejectsUnparseablePlan(t *testing.T) {
+ _, err := gonative.Default().CreateQueryPipeline("SELECT * FROM c", "not json", "{}")
+ require.Error(t, err)
+}
+
+func TestDefaultCreateQueryPipelineRejectsUnsupportedPlan(t *testing.T) {
+ // A plan carrying hasNonStreamingOrderBy (vector/hybrid-search scoring)
+ // is out of the engine's scope — the design spec explicitly excludes it.
+ plan := `{"partitionedQueryExecutionInfoVersion":2,"queryInfo":{"hasNonStreamingOrderBy":true}}`
+ pkranges := `{"PartitionKeyRanges":[{"id":"0"}]}`
+ _, err := gonative.Default().CreateQueryPipeline("SELECT ...", plan, pkranges)
+ require.Error(t, err)
+ assert.True(t, errors.Is(err, gonative.ErrUnsupportedPlanFeature))
+}
+
+func TestDefaultCreateReadManyPipelineReturnsUnsupported(t *testing.T) {
+ _, err := gonative.Default().CreateReadManyPipeline(nil, "{}", "", 0, nil)
+ require.Error(t, err)
+ assert.True(t, errors.Is(err, gonative.ErrUnsupportedPlanFeature))
+}
+
+func TestDefaultReturnsDistinctEngineInstances(t *testing.T) {
+ // Each caller gets its own instance — enables per-engine caches in later stages.
+ a := gonative.Default()
+ b := gonative.Default()
+ assert.NotSame(t, a, b)
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/group.go b/backend/plugin/db/cosmosdb/queryengine/gonative/group.go
new file mode 100644
index 0000000000..fd67741acd
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/group.go
@@ -0,0 +1,378 @@
+package gonative
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/pkg/errors"
+)
+
+// groupByItem is one entry in the per-document groupByItems array. For a
+// single-key GROUP BY the array has one element; multi-key GROUP BY (not in
+// Stage 6's target inventory but straightforward to support since we hash the
+// whole array) would have more.
+type groupByItem struct {
+ Item json.RawMessage `json:"item"`
+}
+
+// groupDoc is one Documents[i] entry in a GROUP BY per-partition response.
+type groupDoc struct {
+ GroupByItems []groupByItem `json:"groupByItems"`
+ Payload map[string]json.RawMessage `json:"payload"`
+}
+
+// groupPartitionResponse decodes a GROUP BY per-partition body.
+type groupPartitionResponse struct {
+ Documents []groupDoc `json:"Documents"`
+}
+
+func parseGroupPartitionResponse(raw []byte) (*groupPartitionResponse, error) {
+ var r groupPartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse group partition response")
+ }
+ return &r, nil
+}
+
+// groupState accumulates one group's worth of aggregate partials across every
+// partition that contributed to that group. Group-key columns are captured
+// once (they're identical for every partition that reports this key).
+type groupState struct {
+ // keyValues holds the raw payload value for each group-key column, in the
+ // order those aliases appear in groupByAliases. The first partition to
+ // report a given group populates these; subsequent partitions overwrite
+ // with the same value (Cosmos guarantees equality).
+ keyValues map[string]json.RawMessage
+ // aggregators are ordered per groupByAliases; entries whose alias is a
+ // group-key column are nil.
+ aggregators []aggregator
+}
+
+// groupPipeline implements queryengine.QueryPipeline for plans with GROUP BY.
+// It collects per-partition group contributions into a hash map keyed on the
+// canonical JSON of groupByItems, then emits one row per group once every
+// partition has delivered.
+//
+// GROUP BY is non-streaming — all partitions must arrive before any row is
+// emitted. Memory is bounded by the cardinality of distinct group keys, not
+// by the total row count across partitions.
+type groupPipeline struct {
+ query string
+ pkRangeIDs []string
+ partitionsDone map[string]bool
+ issued bool
+ closed bool
+
+ // aliases preserves the order of groupByAliases; kinds[i] is the
+ // aggregate kind for aliases[i] ("" when the alias is a group-key column).
+ aliases []string
+ kinds []string
+
+ // groups is the per-group state, keyed on the canonical JSON of
+ // groupByItems. Grows to the cardinality of distinct group keys.
+ groups map[string]*groupState
+
+ // pending holds fully finalized rows awaiting the caller's next Run().
+ pending [][]byte
+ // finalized is true after the first post-drain Run() has built the pending
+ // set; subsequent Runs just flush.
+ finalized bool
+}
+
+// newGroupPipeline builds a pipeline for a GROUP BY plan. It extracts the
+// alias order and per-alias aggregate kinds from the plan's groupByAliases
+// and groupByAliasToAggregateType fields; any alias missing from the map is
+// treated as a group-key column.
+//
+// Returns ErrUnsupportedPlanFeature when the plan composes GROUP BY with
+// operators this stage does not handle (ORDER BY, DISTINCT, TOP, OFFSET/LIMIT).
+// Those compositions are valid SQL but require wrapping groupPipeline in
+// additional operators — deferred.
+func newGroupPipeline(plan *planDoc, pkRangeIDs []string) (*groupPipeline, error) {
+ qi := plan.QueryInfo
+ if len(qi.GroupByExpressions) == 0 {
+ return nil, errors.New("gonative: groupPipeline requires groupByExpressions")
+ }
+ if len(qi.GroupByAliases) == 0 {
+ return nil, errors.New("gonative: groupPipeline requires groupByAliases")
+ }
+ // Stage 6 does not compose GROUP BY with other operators. Reject anything
+ // that would require wrapping in ORDER BY / TOP / OFFSET-LIMIT / DISTINCT.
+ if len(qi.OrderBy) > 0 || qi.Top != nil || qi.Offset != nil || qi.Limit != nil ||
+ qi.DistinctType == "Unordered" || qi.DistinctType == "Ordered" {
+ return nil, ErrUnsupportedPlanFeature
+ }
+
+ aliases := append([]string(nil), qi.GroupByAliases...)
+ kinds := make([]string, len(aliases))
+ for i, alias := range aliases {
+ kind, ok := qi.GroupByAliasToAggregateType[alias]
+ if !ok {
+ // Alias exists in the output but has no map entry. Treat as a
+ // group-key column.
+ kinds[i] = ""
+ continue
+ }
+ kinds[i] = kind
+ if kind != "" {
+ if a := newAggregator(kind); a == nil {
+ return nil, errors.Errorf("gonative: unsupported aggregate kind %q for alias %q", kind, alias)
+ }
+ }
+ }
+
+ return &groupPipeline{
+ // GROUP BY plans always carry a non-empty rewrittenQuery that wraps the
+ // user's projection into {groupByItems, payload}; running the original
+ // query instead would return raw rows with no groupByItems field.
+ query: qi.RewrittenQuery,
+ pkRangeIDs: append([]string(nil), pkRangeIDs...),
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ aliases: aliases,
+ kinds: kinds,
+ groups: make(map[string]*groupState),
+ }, nil
+}
+
+// Query implements queryengine.QueryPipeline.
+func (p *groupPipeline) Query() string { return p.query }
+
+// IsComplete returns true once every partition has delivered and every
+// finalized row has been flushed to the caller.
+func (p *groupPipeline) IsComplete() bool {
+ if !p.issued {
+ return false
+ }
+ if len(p.pending) > 0 {
+ return false
+ }
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ return p.finalized
+}
+
+// Close implements queryengine.QueryPipeline.
+func (p *groupPipeline) Close() { p.closed = true }
+
+// Run drives one pipeline turn:
+// 1. First call — fan out one request per pk-range.
+// 2. After every partition has provided data, build the output rows by
+// finalizing each group's aggregators and emitting in alias order.
+// 3. Flush pending rows on subsequent calls until drained.
+func (p *groupPipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ reqs := make([]queryengine.QueryRequest, len(p.pkRangeIDs))
+ for i, id := range p.pkRangeIDs {
+ reqs[i] = queryengine.QueryRequest{
+ PartitionKeyRangeID: id,
+ Id: uint64(i),
+ Query: p.query,
+ IncludeParameters: true,
+ }
+ }
+ p.issued = true
+ return &queryengine.PipelineResult{Requests: reqs}, nil
+ }
+
+ if !p.finalized && p.allPartitionsDone() {
+ if err := p.finalize(); err != nil {
+ return nil, err
+ }
+ }
+
+ if len(p.pending) > 0 {
+ batch := p.pending
+ p.pending = nil
+ return &queryengine.PipelineResult{Items: batch, IsCompleted: p.IsComplete()}, nil
+ }
+ return &queryengine.PipelineResult{IsCompleted: p.IsComplete()}, nil
+}
+
+// ProvideData consumes each partition's response and folds every row into the
+// appropriate group's state.
+func (p *groupPipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ resp, err := parseGroupPartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for docIdx, doc := range resp.Documents {
+ if len(doc.GroupByItems) == 0 {
+ return errors.Errorf("gonative: group doc %d missing groupByItems", docIdx)
+ }
+ key, err := groupKey(doc.GroupByItems)
+ if err != nil {
+ return err
+ }
+ state, ok := p.groups[key]
+ if !ok {
+ state = p.initGroupState(doc)
+ p.groups[key] = state
+ }
+ // Merge this partition's aggregate contributions into the group's state.
+ for i, alias := range p.aliases {
+ if p.kinds[i] == "" {
+ // Group-key column: set once; subsequent partitions carry
+ // the same value so no work needed.
+ continue
+ }
+ rawItem, ok := doc.Payload[alias]
+ if !ok {
+ return errors.Errorf("gonative: group doc %d missing payload alias %q", docIdx, alias)
+ }
+ // The payload value is shaped {"item": <partial> [, "item2": ...]}.
+ partial, err := extractAggregatePartial(p.kinds[i], rawItem)
+ if err != nil {
+ return err
+ }
+ if err := state.aggregators[i].feedPartial(partial); err != nil {
+ return err
+ }
+ }
+ }
+ if r.NextContinuation != "" {
+ return errors.New("gonative: group partition returned a continuation token (multi-page not supported in Stage 6)")
+ }
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
+
+// initGroupState creates a fresh state for a newly-seen group key. Captures
+// the group-key column values from the first document reporting this key
+// (they're identical for every subsequent partition reporting the same key)
+// and allocates aggregators for the aggregate columns.
+func (p *groupPipeline) initGroupState(doc groupDoc) *groupState {
+ s := &groupState{
+ keyValues: make(map[string]json.RawMessage, len(p.aliases)),
+ aggregators: make([]aggregator, len(p.aliases)),
+ }
+ for i, alias := range p.aliases {
+ if p.kinds[i] == "" {
+ // Group-key column: store raw value.
+ if v, ok := doc.Payload[alias]; ok {
+ s.keyValues[alias] = append([]byte(nil), v...)
+ }
+ } else {
+ s.aggregators[i] = newAggregator(p.kinds[i])
+ }
+ }
+ return s
+}
+
+// allPartitionsDone returns true when every pk-range has reported.
+func (p *groupPipeline) allPartitionsDone() bool {
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ return true
+}
+
+// finalize walks the group map and produces one output row per group.
+// Emission order is unspecified (follows Go's map iteration randomization);
+// callers that need deterministic order should add an ORDER BY clause, which
+// a future composition stage will wrap around this pipeline.
+func (p *groupPipeline) finalize() error {
+ for _, state := range p.groups {
+ row, err := p.buildRow(state)
+ if err != nil {
+ return err
+ }
+ p.pending = append(p.pending, row)
+ }
+ p.finalized = true
+ return nil
+}
+
+// buildRow assembles the final JSON object for a single group, preserving
+// the alias order from groupByAliases. JSON object key order in Go's encoder
+// depends on sort for map marshaling, so we write the object manually.
+func (p *groupPipeline) buildRow(s *groupState) ([]byte, error) {
+ var buf []byte
+ buf = append(buf, '{')
+ for i, alias := range p.aliases {
+ if i > 0 {
+ buf = append(buf, ',')
+ }
+ aliasJSON, err := json.Marshal(alias)
+ if err != nil {
+ return nil, err
+ }
+ buf = append(buf, aliasJSON...)
+ buf = append(buf, ':')
+ if p.kinds[i] == "" {
+ v, ok := s.keyValues[alias]
+ if !ok {
+ buf = append(buf, []byte("null")...)
+ continue
+ }
+ buf = append(buf, v...)
+ } else {
+ final, err := s.aggregators[i].finalize()
+ if err != nil {
+ return nil, err
+ }
+ buf = append(buf, final...)
+ }
+ }
+ buf = append(buf, '}')
+ return buf, nil
+}
+
+// groupKey returns a canonical hash key for a groupByItems array. The key is
+// the canonical JSON of the array — equal keys produce equal bytes.
+func groupKey(items []groupByItem) (string, error) {
+ // Build a slice of the inner "item" values, canonicalize each, then join.
+ // Using a combined key of canonicalized element JSON keeps equality
+ // deterministic even for complex multi-key GROUP BY.
+ parts := make([]json.RawMessage, len(items))
+ for i, it := range items {
+ c, err := canonicalJSON(it.Item)
+ if err != nil {
+ return "", errors.Wrapf(err, "gonative: canonicalize group key part %d", i)
+ }
+ parts[i] = c
+ }
+ b, err := json.Marshal(parts)
+ if err != nil {
+ return "", errors.Wrap(err, "gonative: marshal group key")
+ }
+ return string(b), nil
+}
+
+// extractAggregatePartial decodes the payload value for an aggregate alias
+// into the shape the corresponding aggregator's feedPartial expects.
+//
+// The wire format is {"item": <direct value> [, "item2": {"min"|"max":...,
+// "count":...}]}. For Count/Sum/Avg the aggregator consumes "item" directly.
+// For Min/Max: prefer "item2" (the reliable cross-partition partial carrying
+// count); fall back to synthesizing a single-count partial from "item" when
+// item2 is absent. Mirrors the feedAliasedPartial logic from Stage 1's
+// aliased-aggregate pipeline.
+func extractAggregatePartial(kind string, raw json.RawMessage) (json.RawMessage, error) {
+ var wrap aliasedItem
+ if err := json.Unmarshal(raw, &wrap); err != nil {
+ return nil, errors.Wrap(err, "gonative: extract aggregate partial")
+ }
+ switch kind {
+ case "Min":
+ if len(wrap.Item2) > 0 {
+ return wrap.Item2, nil
+ }
+ return fmt.Appendf(nil, `{"min":%s,"count":1}`, wrap.Item), nil
+ case "Max":
+ if len(wrap.Item2) > 0 {
+ return wrap.Item2, nil
+ }
+ return fmt.Appendf(nil, `{"max":%s,"count":1}`, wrap.Item), nil
+ default:
+ // Count, Sum, Avg — Item alone carries the partial.
+ return wrap.Item, nil
+ }
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/group_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/group_test.go
new file mode 100644
index 0000000000..340910c24c
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/group_test.go
@@ -0,0 +1,343 @@
+package gonative
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// newGroupPlan builds the minimum plan fields a groupPipeline needs for a
+// single-expression GROUP BY with the given alias + aggregate-kind mapping.
+// aliases order defines output column order.
+func newGroupPlan(groupByExpr string, aliases []string, aliasKinds map[string]string) *planDoc {
+ plan := &planDoc{}
+ plan.QueryInfo.GroupByExpressions = []string{groupByExpr}
+ plan.QueryInfo.GroupByAliases = aliases
+ plan.QueryInfo.GroupByAliasToAggregateType = aliasKinds
+ return plan
+}
+
+// mkGroupBody synthesizes a partition response body from a list of per-group
+// documents. Each entry is (groupByItemRaw, payloadRaw) — the full JSON of
+// groupByItems[0].item and of the payload object respectively.
+func mkGroupBody(t *testing.T, entries ...struct {
+ key, payload string
+}) []byte {
+ t.Helper()
+ docs := make([]map[string]any, 0, len(entries))
+ for _, e := range entries {
+ var payloadObj map[string]json.RawMessage
+ require.NoError(t, json.Unmarshal([]byte(e.payload), &payloadObj), "invalid payload fixture: %s", e.payload)
+ docs = append(docs, map[string]any{
+ "groupByItems": []map[string]any{{"item": json.RawMessage(e.key)}},
+ "payload": payloadObj,
+ })
+ }
+ b, err := json.Marshal(map[string]any{"Documents": docs})
+ require.NoError(t, err)
+ return b
+}
+
+type groupEntry = struct {
+ key, payload string
+}
+
+// driveGroup feeds the given partition bodies through a group pipeline and
+// returns every emitted row.
+func driveGroup(t *testing.T, p *groupPipeline, feed map[string][]byte) [][]byte {
+ t.Helper()
+ res, err := p.Run()
+ require.NoError(t, err)
+ require.NotEmpty(t, res.Requests)
+ results := make([]queryengine.QueryResult, 0, len(res.Requests))
+ for _, req := range res.Requests {
+ body, ok := feed[req.PartitionKeyRangeID]
+ require.True(t, ok, "missing fixture for %s", req.PartitionKeyRangeID)
+ results = append(results, queryengine.NewQueryResult(req.PartitionKeyRangeID, body, ""))
+ }
+ require.NoError(t, p.ProvideData(results))
+
+ var out [][]byte
+ for !p.IsComplete() {
+ r, err := p.Run()
+ require.NoError(t, err)
+ out = append(out, r.Items...)
+ }
+ return out
+}
+
+// rowsByKey indexes GROUP BY output rows by a key extracted via extractor.
+// Order of emission isn't deterministic in Go's map iteration, so callers
+// should assert on this map rather than on the raw slice order.
+func rowsByKey(t *testing.T, rows [][]byte, keyAlias string) map[string]map[string]any {
+ t.Helper()
+ out := map[string]map[string]any{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ v, ok := obj[keyAlias].(string)
+ require.True(t, ok, "row missing %s key: %s", keyAlias, r)
+ out[v] = obj
+ }
+ return out
+}
+
+// ---------------------------------------------------------- validation
+
+func TestNewGroupPipeline_RequiresGroupByExpressions(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.GroupByAliases = []string{"a"}
+ _, err := newGroupPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "groupByExpressions")
+}
+
+func TestNewGroupPipeline_RequiresGroupByAliases(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.GroupByExpressions = []string{"c.a"}
+ _, err := newGroupPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "groupByAliases")
+}
+
+func TestNewGroupPipeline_RejectsCompositionWithOrderBy(t *testing.T) {
+ plan := newGroupPlan("c.a", []string{"a"}, map[string]string{"a": ""})
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ _, err := newGroupPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestNewGroupPipeline_RejectsCompositionWithTop(t *testing.T) {
+ plan := newGroupPlan("c.a", []string{"a"}, map[string]string{"a": ""})
+ n := 5
+ plan.QueryInfo.Top = &n
+ _, err := newGroupPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestNewGroupPipeline_RejectsCompositionWithDistinct(t *testing.T) {
+ plan := newGroupPlan("c.a", []string{"a"}, map[string]string{"a": ""})
+ plan.QueryInfo.DistinctType = "Unordered"
+ _, err := newGroupPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+// ---------------------------------------------------------- semantics
+
+func TestGroupPipeline_SinglePartition_GroupKeyPassthrough(t *testing.T) {
+ // SELECT c.country, COUNT(1) AS cityCount FROM c GROUP BY c.country
+ plan := newGroupPlan("c.country",
+ []string{"country", "cityCount"},
+ map[string]string{"country": "", "cityCount": "Count"})
+ p, err := newGroupPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ rows := driveGroup(t, p, map[string][]byte{
+ "0": mkGroupBody(t,
+ groupEntry{key: `"AD"`, payload: `{"country":"AD","cityCount":{"item":3}}`},
+ groupEntry{key: `"AE"`, payload: `{"country":"AE","cityCount":{"item":5}}`},
+ ),
+ })
+ byKey := rowsByKey(t, rows, "country")
+ require.Len(t, byKey, 2)
+ assert.EqualValues(t, 3, byKey["AD"]["cityCount"])
+ assert.EqualValues(t, 5, byKey["AE"]["cityCount"])
+}
+
+func TestGroupPipeline_MultiPartition_MergesSameKey(t *testing.T) {
+ plan := newGroupPlan("c.country",
+ []string{"country", "cityCount"},
+ map[string]string{"country": "", "cityCount": "Count"})
+ p, err := newGroupPipeline(plan, []string{"A", "B", "C"})
+ require.NoError(t, err)
+
+ // AE appears in all three partitions with counts 2, 3, 1 → expect 6.
+ // US only in A (count 4) and C (count 2) → expect 6.
+ // GB only in B (count 7) → expect 7.
+ rows := driveGroup(t, p, map[string][]byte{
+ "A": mkGroupBody(t,
+ groupEntry{key: `"AE"`, payload: `{"country":"AE","cityCount":{"item":2}}`},
+ groupEntry{key: `"US"`, payload: `{"country":"US","cityCount":{"item":4}}`},
+ ),
+ "B": mkGroupBody(t,
+ groupEntry{key: `"AE"`, payload: `{"country":"AE","cityCount":{"item":3}}`},
+ groupEntry{key: `"GB"`, payload: `{"country":"GB","cityCount":{"item":7}}`},
+ ),
+ "C": mkGroupBody(t,
+ groupEntry{key: `"AE"`, payload: `{"country":"AE","cityCount":{"item":1}}`},
+ groupEntry{key: `"US"`, payload: `{"country":"US","cityCount":{"item":2}}`},
+ ),
+ })
+ byKey := rowsByKey(t, rows, "country")
+ require.Len(t, byKey, 3)
+ assert.EqualValues(t, 6, byKey["AE"]["cityCount"])
+ assert.EqualValues(t, 6, byKey["US"]["cityCount"])
+ assert.EqualValues(t, 7, byKey["GB"]["cityCount"])
+}
+
+func TestGroupPipeline_MultipleAggregates(t *testing.T) {
+ // SELECT c.cr, COUNT(1) AS count, SUM(...) AS totalPop FROM c GROUP BY c.cr
+ plan := newGroupPlan("c.cr",
+ []string{"cr", "count", "totalPop"},
+ map[string]string{"cr": "", "count": "Count", "totalPop": "Sum"})
+ p, err := newGroupPipeline(plan, []string{"A", "B"})
+ require.NoError(t, err)
+
+ rows := driveGroup(t, p, map[string][]byte{
+ "A": mkGroupBody(t,
+ groupEntry{key: `"r1"`, payload: `{"cr":"r1","count":{"item":2},"totalPop":{"item":1000}}`},
+ groupEntry{key: `"r2"`, payload: `{"cr":"r2","count":{"item":1},"totalPop":{"item":500}}`},
+ ),
+ "B": mkGroupBody(t,
+ groupEntry{key: `"r1"`, payload: `{"cr":"r1","count":{"item":3},"totalPop":{"item":2500}}`},
+ ),
+ })
+ byKey := rowsByKey(t, rows, "cr")
+ require.Len(t, byKey, 2)
+ assert.EqualValues(t, 5, byKey["r1"]["count"])
+ assert.EqualValues(t, 3500, byKey["r1"]["totalPop"])
+ assert.EqualValues(t, 1, byKey["r2"]["count"])
+ assert.EqualValues(t, 500, byKey["r2"]["totalPop"])
+}
+
+func TestGroupPipeline_PreservesAliasOrder(t *testing.T) {
+ // Rely on the raw emitted bytes to check alias order — map iteration is
+ // non-deterministic, but the output JSON is built manually to honor
+ // groupByAliases order.
+ plan := newGroupPlan("c.country",
+ []string{"country", "cityCount"},
+ map[string]string{"country": "", "cityCount": "Count"})
+ p, err := newGroupPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ rows := driveGroup(t, p, map[string][]byte{
+ "0": mkGroupBody(t,
+ groupEntry{key: `"AD"`, payload: `{"country":"AD","cityCount":{"item":3}}`},
+ ),
+ })
+ require.Len(t, rows, 1)
+ assert.Equal(t, `{"country":"AD","cityCount":3}`, string(rows[0]))
+}
+
+func TestGroupPipeline_EmptyPartitions(t *testing.T) {
+ plan := newGroupPlan("c.a",
+ []string{"a", "n"},
+ map[string]string{"a": "", "n": "Count"})
+ p, err := newGroupPipeline(plan, []string{"A", "B"})
+ require.NoError(t, err)
+ rows := driveGroup(t, p, map[string][]byte{
+ "A": mkGroupBody(t),
+ "B": mkGroupBody(t),
+ })
+ assert.Empty(t, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestGroupPipeline_GroupKeysBeforeAggregatesInAliasOrder(t *testing.T) {
+ // Aggregate listed first, group key second — output should still honor that order.
+ plan := newGroupPlan("c.country",
+ []string{"cityCount", "country"},
+ map[string]string{"country": "", "cityCount": "Count"})
+ p, err := newGroupPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ rows := driveGroup(t, p, map[string][]byte{
+ "0": mkGroupBody(t,
+ groupEntry{key: `"AE"`, payload: `{"country":"AE","cityCount":{"item":5}}`},
+ ),
+ })
+ require.Len(t, rows, 1)
+ assert.Equal(t, `{"cityCount":5,"country":"AE"}`, string(rows[0]))
+}
+
+func TestGroupPipeline_RejectsContinuationToken(t *testing.T) {
+ plan := newGroupPlan("c.a",
+ []string{"a", "n"},
+ map[string]string{"a": "", "n": "Count"})
+ p, err := newGroupPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ _, _ = p.Run()
+
+ body := mkGroupBody(t, groupEntry{key: `"x"`, payload: `{"a":"x","n":{"item":1}}`})
+ err = p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body, NextContinuation: "cont"},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "continuation token")
+}
+
+func TestGroupPipeline_RejectsMissingPayloadAlias(t *testing.T) {
+ plan := newGroupPlan("c.a",
+ []string{"a", "n"},
+ map[string]string{"a": "", "n": "Count"})
+ p, err := newGroupPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ _, _ = p.Run()
+
+ body := mkGroupBody(t, groupEntry{key: `"x"`, payload: `{"a":"x"}`}) // missing n
+ err = p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), `alias "n"`)
+}
+
+// ------------------------------------------------------- group-key canonicalization
+
+func TestGroupKey_TreatsKeyReorderedObjectsAsSameGroup(t *testing.T) {
+ // Two docs whose groupByItems differ only by object key ordering must hash
+ // to the same group key.
+ a, err := groupKey([]groupByItem{{Item: json.RawMessage(`{"x":1,"y":2}`)}})
+ require.NoError(t, err)
+ b, err := groupKey([]groupByItem{{Item: json.RawMessage(`{"y":2,"x":1}`)}})
+ require.NoError(t, err)
+ assert.Equal(t, a, b)
+}
+
+func TestGroupKey_DifferentTypesHashDifferently(t *testing.T) {
+ a, err := groupKey([]groupByItem{{Item: json.RawMessage(`"1"`)}})
+ require.NoError(t, err)
+ b, err := groupKey([]groupByItem{{Item: json.RawMessage(`1`)}})
+ require.NoError(t, err)
+ assert.NotEqual(t, a, b)
+}
+
+// ------------------------------------------------------- extractAggregatePartial
+
+func TestExtractAggregatePartial_CountUsesItem(t *testing.T) {
+ got, err := extractAggregatePartial("Count", json.RawMessage(`{"item":42}`))
+ require.NoError(t, err)
+ assert.Equal(t, "42", string(got))
+}
+
+func TestExtractAggregatePartial_SumUsesItem(t *testing.T) {
+ got, err := extractAggregatePartial("Sum", json.RawMessage(`{"item":3500}`))
+ require.NoError(t, err)
+ assert.Equal(t, "3500", string(got))
+}
+
+func TestExtractAggregatePartial_AvgUsesItem(t *testing.T) {
+ got, err := extractAggregatePartial("Average", json.RawMessage(`{"item":{"sum":100,"count":4}}`))
+ require.NoError(t, err)
+ assert.JSONEq(t, `{"sum":100,"count":4}`, string(got))
+}
+
+func TestExtractAggregatePartial_MinPrefersItem2WhenPresent(t *testing.T) {
+ got, err := extractAggregatePartial("Min", json.RawMessage(`{"item":10,"item2":{"min":10,"count":3}}`))
+ require.NoError(t, err)
+ assert.JSONEq(t, `{"min":10,"count":3}`, string(got))
+}
+
+func TestExtractAggregatePartial_MinSynthesizesFromItemWhenItem2Absent(t *testing.T) {
+ got, err := extractAggregatePartial("Min", json.RawMessage(`{"item":42}`))
+ require.NoError(t, err)
+ assert.JSONEq(t, `{"min":42,"count":1}`, string(got))
+}
+
+func TestExtractAggregatePartial_MaxSynthesizesFromItemWhenItem2Absent(t *testing.T) {
+ got, err := extractAggregatePartial("Max", json.RawMessage(`{"item":99}`))
+ require.NoError(t, err)
+ assert.JSONEq(t, `{"max":99,"count":1}`, string(got))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage2_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage2_test.go
new file mode 100644
index 0000000000..1d6fa1e8b3
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage2_test.go
@@ -0,0 +1,108 @@
+package gonative_test
+
+// Stage 2 live-Azure integration tests for BYT-9239. Guarded by AZURE_COSMOS_KEY;
+// skips automatically when unset so the default unit-test run stays hermetic.
+//
+// export AZURE_COSMOS_ENDPOINT="https://...documents.azure.com:443/"
+// export AZURE_COSMOS_KEY="$(az cosmosdb keys list --name ... -o tsv)"
+// go test -count=1 -run "^TestIntegration_BYT9239_Stage2" \
+// ./sdk/data/azcosmos/queryengine/gonative/
+
+import (
+ "context"
+ "encoding/json"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/db/cosmosdb/queryengine/gonative"
+)
+
+func skipIfNoAzureStage2(t *testing.T) (endpoint, key, dbName, collName string) {
+ t.Helper()
+ endpoint = os.Getenv("AZURE_COSMOS_ENDPOINT")
+ key = os.Getenv("AZURE_COSMOS_KEY")
+ if endpoint == "" || key == "" {
+ t.Skip("set AZURE_COSMOS_ENDPOINT + AZURE_COSMOS_KEY to run live integration tests")
+ }
+ dbName = envOrDefaultS2("AZURE_COSMOS_DB", "testdb")
+ collName = envOrDefaultS2("AZURE_COSMOS_CONTAINER", "WorldCities")
+ return endpoint, key, dbName, collName
+}
+
+func envOrDefaultS2(k, def string) string {
+ if v := os.Getenv(k); v != "" {
+ return v
+ }
+ return def
+}
+
+// drainAzure runs a cross-partition query end-to-end against a live Cosmos
+// account and returns every row it produces. Engages the default engine
+// (engage-on-error path), so this covers the real "gateway fails → engine
+// takes over" flow.
+func drainAzure(t *testing.T, container *azcosmos.ContainerClient, sql string) [][]byte {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+
+ pager := container.NewQueryItemsPager(sql, azcosmos.NewPartitionKey(), &azcosmos.QueryOptions{QueryEngine: gonative.Default()})
+ var items [][]byte
+ for pager.More() {
+ page, err := pager.NextPage(ctx)
+ require.NoError(t, err, "NextPage for %q", sql)
+ items = append(items, page.Items...)
+ }
+ return items
+}
+
+func TestIntegration_BYT9239_Stage2_10_1_ObjectDistinct(t *testing.T) {
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t)
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+
+ rows := drainAzure(t, container, `SELECT DISTINCT c.country FROM c`)
+ require.NotEmpty(t, rows, "expected at least one distinct country")
+
+ // Each row is {"country": "AE"} etc — no duplicates.
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ country, ok := obj["country"].(string)
+ require.True(t, ok, "row missing country field: %s", r)
+ assert.False(t, seen[country], "duplicate country emitted: %s", country)
+ seen[country] = true
+ }
+ t.Logf("distinct countries: %d", len(seen))
+}
+
+func TestIntegration_BYT9239_Stage2_10_2_DistinctValue(t *testing.T) {
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t)
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+
+ rows := drainAzure(t, container, `SELECT DISTINCT VALUE c.countryRegion FROM c`)
+ require.NotEmpty(t, rows, "expected at least one distinct countryRegion")
+
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var v string
+ require.NoError(t, json.Unmarshal(r, &v))
+ assert.False(t, seen[v], "duplicate region emitted: %s", v)
+ seen[v] = true
+ }
+ t.Logf("distinct countryRegions: %d", len(seen))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage3_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage3_test.go
new file mode 100644
index 0000000000..314c2f6143
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage3_test.go
@@ -0,0 +1,115 @@
+package gonative_test
+
+// Stage 3 live-Azure integration tests for BYT-9239. Guarded by AZURE_COSMOS_KEY.
+//
+// export AZURE_COSMOS_ENDPOINT="https://...documents.azure.com:443/"
+// export AZURE_COSMOS_KEY="$(az cosmosdb keys list --name ... -o tsv)"
+// go test -count=1 -run "^TestIntegration_BYT9239_Stage3" \
+// ./sdk/data/azcosmos/queryengine/gonative/
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func newContainerS3(t *testing.T) *azcosmos.ContainerClient {
+ t.Helper()
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t) // same env guard as Stage 2
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+ return container
+}
+
+// sortKey extracts a comparable representation of a row's `population` field
+// that honors Cosmos item ordering: numbers sort before strings, so we tag
+// the value with the type class first. Returned tuple is (typeRank, raw).
+func sortKey(t *testing.T, row []byte) (int, any) {
+ t.Helper()
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ v, ok := obj["population"]
+ require.True(t, ok, "row missing population: %s", row)
+ switch x := v.(type) {
+ case float64:
+ return 3, x // numbers
+ case string:
+ return 4, x // strings
+ case bool:
+ return 2, x
+ case nil:
+ return 1, x
+ default:
+ return 0, x
+ }
+}
+
+func TestIntegration_BYT9239_Stage3_4_1_Ascending(t *testing.T) {
+ container := newContainerS3(t)
+ rows := drainAzure(t, container, `SELECT c.name, c.population FROM c ORDER BY c.population ASC`)
+ require.NotEmpty(t, rows, "expected rows back")
+
+ // Verify the result is in ascending order under Cosmos item ordering.
+ var prevRank int
+ var prevVal any
+ for i, r := range rows {
+ rank, val := sortKey(t, r)
+ if i == 0 {
+ prevRank, prevVal = rank, val
+ continue
+ }
+ if rank != prevRank {
+ assert.GreaterOrEqual(t, rank, prevRank, "type-rank must not regress at row %d: %s", i, r)
+ prevRank, prevVal = rank, val
+ continue
+ }
+ switch pv := prevVal.(type) {
+ case float64:
+ assert.LessOrEqual(t, pv, val.(float64), "ascending number order broken at row %d", i)
+ case string:
+ assert.LessOrEqual(t, pv, val.(string), "ascending string order broken at row %d", i)
+ default:
+ t.Fatalf("unexpected sort key type %T at row %d", pv, i)
+ }
+ prevVal = val
+ }
+ t.Logf("ASC verified across %d rows", len(rows))
+}
+
+func TestIntegration_BYT9239_Stage3_4_2_Descending(t *testing.T) {
+ container := newContainerS3(t)
+ rows := drainAzure(t, container, `SELECT c.name, c.population FROM c ORDER BY c.population DESC`)
+ require.NotEmpty(t, rows)
+
+ var prevRank int
+ var prevVal any
+ for i, r := range rows {
+ rank, val := sortKey(t, r)
+ if i == 0 {
+ prevRank, prevVal = rank, val
+ continue
+ }
+ if rank != prevRank {
+ assert.LessOrEqual(t, rank, prevRank, "type-rank must not ascend under DESC at row %d: %s", i, r)
+ prevRank, prevVal = rank, val
+ continue
+ }
+ switch pv := prevVal.(type) {
+ case float64:
+ assert.GreaterOrEqual(t, pv, val.(float64), "descending number order broken at row %d", i)
+ case string:
+ assert.GreaterOrEqual(t, pv, val.(string), "descending string order broken at row %d", i)
+ default:
+ t.Fatalf("unexpected sort key type %T at row %d", pv, i)
+ }
+ prevVal = val
+ }
+ t.Logf("DESC verified across %d rows", len(rows))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage4_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage4_test.go
new file mode 100644
index 0000000000..a27a26bd53
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage4_test.go
@@ -0,0 +1,37 @@
+package gonative_test
+
+// Stage 4 live-Azure integration test for BYT-9239. Guarded by AZURE_COSMOS_KEY.
+//
+// export AZURE_COSMOS_ENDPOINT="https://...documents.azure.com:443/"
+// export AZURE_COSMOS_KEY="$(az cosmosdb keys list --name ... -o tsv)"
+// go test -count=1 -run "^TestIntegration_BYT9239_Stage4" \
+// ./sdk/data/azcosmos/queryengine/gonative/
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestIntegration_BYT9239_Stage4_1_3_TopTen(t *testing.T) {
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t)
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+
+ rows := drainAzure(t, container, `SELECT TOP 10 * FROM c`)
+ assert.Len(t, rows, 10, "TOP 10 across partitions must emit exactly 10 rows end-to-end")
+ for i, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj), "row %d failed to parse: %s", i, r)
+ _, hasCountry := obj["country"]
+ assert.True(t, hasCountry, "row %d should be a full document with a country field: %s", i, r)
+ }
+ t.Logf("emitted %d rows", len(rows))
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage5_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage5_test.go
new file mode 100644
index 0000000000..31320fc254
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage5_test.go
@@ -0,0 +1,73 @@
+package gonative_test
+
+// Stage 5 live-Azure integration tests. Guarded by AZURE_COSMOS_KEY.
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func newContainerS5(t *testing.T) *azcosmos.ContainerClient {
+ t.Helper()
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t)
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+ return container
+}
+
+// assertAscendingByName walks `rows` and fails if `rows[i]["name"]` is not in
+// nondecreasing string order. Empty input is treated as trivially ordered.
+func assertAscendingByName(t *testing.T, rows [][]byte) []string {
+ t.Helper()
+ names := make([]string, 0, len(rows))
+ var prev string
+ for i, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj), "row %d failed to parse: %s", i, r)
+ name, ok := obj["name"].(string)
+ require.True(t, ok, "row %d missing name: %s", i, r)
+ names = append(names, name)
+ if i > 0 {
+ assert.LessOrEqual(t, prev, name, "ascending broken at row %d: prev=%q cur=%q", i, prev, name)
+ }
+ prev = name
+ }
+ return names
+}
+
+func TestIntegration_BYT9239_Stage5_12_1_OffsetZeroLimitTen(t *testing.T) {
+ container := newContainerS5(t)
+ rows := drainAzure(t, container, `SELECT * FROM c ORDER BY c.name OFFSET 0 LIMIT 10`)
+ require.Len(t, rows, 10, "OFFSET 0 LIMIT 10 must return exactly 10 rows")
+ names := assertAscendingByName(t, rows)
+ t.Logf("first page: %v", names)
+}
+
+func TestIntegration_BYT9239_Stage5_12_2_OffsetTenLimitTen(t *testing.T) {
+ container := newContainerS5(t)
+ // First grab the full ordered list so we know what rows 11 onward should be.
+ allRows := drainAzure(t, container, `SELECT * FROM c ORDER BY c.name`)
+ require.NotEmpty(t, allRows, "expected baseline rows")
+ allNames := assertAscendingByName(t, allRows)
+
+ rows := drainAzure(t, container, `SELECT * FROM c ORDER BY c.name OFFSET 10 LIMIT 10`)
+ names := assertAscendingByName(t, rows)
+
+ // The OFFSET 10 LIMIT 10 result must be a contiguous slice of the sorted
+ // name list starting at index 10. If the container has < 20 rows the
+ // returned set is allNames[10:]; otherwise allNames[10:20].
+ start := 10
+ end := start + len(rows)
+ require.LessOrEqual(t, end, len(allNames), "pagination window extends past available rows")
+ assert.Equal(t, allNames[start:end], names,
+ "OFFSET 10 LIMIT 10 must equal the [10:%d] slice of the globally sorted list", end)
+ t.Logf("second page (%d rows): %v", len(names), names)
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage6_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage6_test.go
new file mode 100644
index 0000000000..b024e063d4
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/integration_stage6_test.go
@@ -0,0 +1,94 @@
+package gonative_test
+
+// Stage 6 live-Azure integration tests. Guarded by AZURE_COSMOS_KEY.
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func newContainerS6(t *testing.T) *azcosmos.ContainerClient {
+ t.Helper()
+ endpoint, key, dbName, collName := skipIfNoAzureStage2(t)
+ cred, err := azcosmos.NewKeyCredential(key)
+ require.NoError(t, err)
+ client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
+ require.NoError(t, err)
+ container, err := client.NewContainer(dbName, collName)
+ require.NoError(t, err)
+ return container
+}
+
+// TestIntegration_BYT9239_Stage6_6_1_CountByCountry verifies cross-partition
+// GROUP BY with COUNT against the live Cosmos account. The assertion is
+// independent of the particular container contents: the grand total of all
+// groups' cityCount must equal the total row count of the container (obtained
+// via a separate VALUE COUNT(1) query).
+func TestIntegration_BYT9239_Stage6_6_1_CountByCountry(t *testing.T) {
+ container := newContainerS6(t)
+
+ // Baseline: total row count.
+ totalRows := drainAzure(t, container, `SELECT VALUE COUNT(1) FROM c`)
+ require.Len(t, totalRows, 1)
+ var total float64
+ require.NoError(t, json.Unmarshal(totalRows[0], &total))
+
+ // Grouped query.
+ rows := drainAzure(t, container, `SELECT c.country, COUNT(1) AS cityCount FROM c GROUP BY c.country`)
+ require.NotEmpty(t, rows)
+
+ var summed float64
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ country, ok := obj["country"].(string)
+ require.True(t, ok, "country must be a string: %s", r)
+ assert.False(t, seen[country], "duplicate country group: %s", country)
+ seen[country] = true
+ cnt, ok := obj["cityCount"].(float64)
+ require.True(t, ok, "cityCount must be numeric: %s", r)
+ summed += cnt
+ }
+ assert.Equal(t, total, summed, "sum of per-country counts must equal total row count")
+ t.Logf("%d country groups covering %.0f rows", len(rows), total)
+}
+
+// TestIntegration_BYT9239_Stage6_6_2_MultipleAggregates verifies multi-aggregate
+// GROUP BY (COUNT + SUM) cross-partition against the live account. Asserts the
+// summed counts match the container total and that totalPop per group is
+// non-negative and finite.
+func TestIntegration_BYT9239_Stage6_6_2_MultipleAggregates(t *testing.T) {
+ container := newContainerS6(t)
+
+ totalRows := drainAzure(t, container, `SELECT VALUE COUNT(1) FROM c`)
+ require.Len(t, totalRows, 1)
+ var total float64
+ require.NoError(t, json.Unmarshal(totalRows[0], &total))
+
+ rows := drainAzure(t, container, `SELECT c.countryRegion, COUNT(1) AS count, SUM(StringToNumber(c.population)) AS totalPop FROM c GROUP BY c.countryRegion`)
+ require.NotEmpty(t, rows)
+
+ var sumCounts float64
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ region, ok := obj["countryRegion"].(string)
+ require.True(t, ok, "countryRegion must be a string: %s", r)
+ assert.False(t, seen[region], "duplicate region group: %s", region)
+ seen[region] = true
+ cnt, ok := obj["count"].(float64)
+ require.True(t, ok)
+ sumCounts += cnt
+ pop, ok := obj["totalPop"].(float64)
+ require.True(t, ok)
+ assert.GreaterOrEqual(t, pop, float64(0), "totalPop must be non-negative for region %s", region)
+ }
+ assert.Equal(t, total, sumCounts, "sum of per-region counts must equal total row count")
+ t.Logf("%d region groups covering %.0f rows", len(rows), total)
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/merge.go b/backend/plugin/db/cosmosdb/queryengine/gonative/merge.go
new file mode 100644
index 0000000000..b692450ddb
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/merge.go
@@ -0,0 +1,354 @@
+package gonative
+
+import (
+ "container/heap"
+ "encoding/json"
+ "strings"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/pkg/errors"
+)
+
+// orderByQueryFilterPlaceholder is the token the gateway embeds into ORDER BY
+// rewritten queries. The client-side engine must substitute it before firing
+// per-partition requests — with "true" on the initial page, and with a
+// continuation filter comparing the last emitted order-by key on subsequent
+// pages. Stage 3 handles the single-page case only; continuation is deferred.
+const orderByQueryFilterPlaceholder = "{documentdb-formattableorderbyquery-filter}"
+
+// orderByItem is each element of the per-document orderByItems array. The
+// gateway emits one per ORDER BY expression; Stage 3 only supports single-key
+// ORDER BY so we read orderByItems[0].
+type orderByItem struct {
+ Item json.RawMessage `json:"item"`
+}
+
+// orderByDoc is one Documents[i] entry in an ORDER BY partition response.
+// Payload is the user's projected row — what we eventually emit.
+type orderByDoc struct {
+ OrderByItems []orderByItem `json:"orderByItems"`
+ Payload json.RawMessage `json:"payload"`
+}
+
+// orderByPartitionResponse decodes an ORDER BY per-partition response body.
+type orderByPartitionResponse struct {
+ Documents []orderByDoc `json:"Documents"`
+}
+
+func parseOrderByPartitionResponse(raw []byte) (*orderByPartitionResponse, error) {
+ var r orderByPartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse order-by partition response")
+ }
+ return &r, nil
+}
+
+// partitionStream holds one partition's ORDER BY documents, in gateway-sorted
+// order. The pipeline pops from the head as the heap selects from this
+// partition; emptying the slice means the partition is drained.
+type partitionStream struct {
+ docs []orderByDoc
+ head int // index of next doc to emit; len(docs)-head is the remaining count
+}
+
+func (s *partitionStream) peek() (*orderByDoc, bool) {
+ if s.head >= len(s.docs) {
+ return nil, false
+ }
+ return &s.docs[s.head], true
+}
+
+func (s *partitionStream) pop() *orderByDoc {
+ d := &s.docs[s.head]
+ s.head++
+ return d
+}
+
+// mergeHeap implements heap.Interface over partition indices. The comparator
+// uses compareItems on each partition's current head's orderByItems[0], with
+// the sort direction flipped for descending ORDER BY.
+type mergeHeap struct {
+ partitions []*partitionStream
+ idx []int // heap of partition indices
+ descending bool
+}
+
+func (h *mergeHeap) Len() int { return len(h.idx) }
+
+func (h *mergeHeap) Less(i, j int) bool {
+ // We never call Less when a partition is empty — the pipeline drops empty
+ // partitions from the heap before they can be compared.
+ a, _ := h.partitions[h.idx[i]].peek()
+ b, _ := h.partitions[h.idx[j]].peek()
+ cmp, err := compareItems(a.OrderByItems[0].Item, b.OrderByItems[0].Item)
+ if err != nil {
+ // compareItems rejects unorderable shapes (arrays, objects). For
+ // ORDER BY scalars this should never happen; if it does, fall back to
+ // stable-by-partition-index order so the heap remains well-defined.
+ return h.idx[i] < h.idx[j]
+ }
+ if h.descending {
+ return cmp > 0
+ }
+ return cmp < 0
+}
+
+func (h *mergeHeap) Swap(i, j int) { h.idx[i], h.idx[j] = h.idx[j], h.idx[i] }
+
+func (h *mergeHeap) Push(x any) { h.idx = append(h.idx, x.(int)) }
+
+func (h *mergeHeap) Pop() any {
+ n := len(h.idx)
+ out := h.idx[n-1]
+ h.idx = h.idx[:n-1]
+ return out
+}
+
+// orderByPipeline implements queryengine.QueryPipeline for plans with a single
+// ORDER BY expression. It emits rows in the requested order by k-way-merging
+// each partition's gateway-sorted stream.
+//
+// Stage 3 scope is single-key ORDER BY only. Multi-key ORDER BY requires a
+// Cosmos composite index on the container — if the container lacks one the
+// gateway rejects the query outright; if it has one the gateway emits a plan
+// with multiple entries in orderByExpressions, and the comparator would need
+// to walk all of them. Both cases are deferred.
+//
+// Stage 5 extends the pipeline with OFFSET/LIMIT awareness. Per Cosmos
+// semantics, OFFSET/LIMIT always composes with ORDER BY; the gateway rewrites
+// the per-partition query to `OFFSET 0 LIMIT (offset+limit)` so every
+// partition delivers enough rows to satisfy any interleaving, and this
+// pipeline handles the global skip + take after merging.
+type orderByPipeline struct {
+ rewrittenQuery string
+ pkRangeIDs []string
+ descending bool
+
+ partitionsDone map[string]bool
+ partitionByID map[string]int
+ partitions []*partitionStream
+
+ heap *mergeHeap
+ issued bool
+ closed bool
+
+ // offset and limit implement plan.QueryInfo.Offset/Limit. A negative limit
+ // (the sentinel returned by newOrderByPipeline when the plan has no limit)
+ // means "unlimited". skipped counts rows dropped by offset; emitted counts
+ // rows forwarded to the caller — both drive the skip+take logic in drain.
+ offset int
+ limit int
+ skipped int
+ emitted int
+
+ // pending emits produced by k-way merging when all partitions have data.
+ pending [][]byte
+}
+
+// newOrderByPipeline constructs a pipeline for a plan with exactly one ORDER BY
+// expression. Returns ErrUnsupportedPlanFeature when the plan names multiple
+// ORDER BY keys — Stage 3 doesn't yet support that composition.
+func newOrderByPipeline(plan *planDoc, pkRangeIDs []string) (*orderByPipeline, error) {
+ if len(plan.QueryInfo.OrderBy) != 1 {
+ return nil, ErrUnsupportedPlanFeature
+ }
+ if plan.QueryInfo.OrderBy[0] != "Ascending" && plan.QueryInfo.OrderBy[0] != "Descending" {
+ return nil, ErrUnsupportedPlanFeature
+ }
+ // Substitute the formattable-order-by-query-filter placeholder with "true"
+ // for the initial request per partition. Multi-page continuation (which
+ // would substitute with a tail-comparison filter) is out of scope here.
+ query := strings.ReplaceAll(plan.QueryInfo.RewrittenQuery, orderByQueryFilterPlaceholder, "true")
+
+ // OFFSET/LIMIT always composes with ORDER BY per Cosmos syntax; defaults
+ // mean "no skip, unlimited take".
+ offset := 0
+ if plan.QueryInfo.Offset != nil {
+ if *plan.QueryInfo.Offset < 0 {
+ return nil, errors.Errorf("gonative: negative OFFSET=%d", *plan.QueryInfo.Offset)
+ }
+ offset = *plan.QueryInfo.Offset
+ }
+ limit := -1
+ if plan.QueryInfo.Limit != nil {
+ if *plan.QueryInfo.Limit < 0 {
+ return nil, errors.Errorf("gonative: negative LIMIT=%d", *plan.QueryInfo.Limit)
+ }
+ limit = *plan.QueryInfo.Limit
+ }
+
+ partByID := make(map[string]int, len(pkRangeIDs))
+ partitions := make([]*partitionStream, len(pkRangeIDs))
+ for i, id := range pkRangeIDs {
+ partByID[id] = i
+ partitions[i] = &partitionStream{}
+ }
+ return &orderByPipeline{
+ rewrittenQuery: query,
+ pkRangeIDs: append([]string(nil), pkRangeIDs...),
+ descending: plan.QueryInfo.OrderBy[0] == "Descending",
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ partitionByID: partByID,
+ partitions: partitions,
+ offset: offset,
+ limit: limit,
+ }, nil
+}
+
+func (p *orderByPipeline) Query() string { return p.rewrittenQuery }
+func (p *orderByPipeline) Close() { p.closed = true }
+
+// IsComplete returns true once every partition's gateway response has been
+// merged and the last pending row has been handed to the caller, or once a
+// LIMIT has been satisfied (in which case unprocessed partition rows are
+// intentionally discarded).
+func (p *orderByPipeline) IsComplete() bool {
+ if !p.issued {
+ return false
+ }
+ if len(p.pending) > 0 {
+ return false
+ }
+ // LIMIT reached — the caller has everything it asked for. Any rows still
+ // sitting in partition streams were produced speculatively by the gateway
+ // (it fetched OFFSET+LIMIT rows per partition to cover any interleaving)
+ // and are safe to drop.
+ if p.limit >= 0 && p.emitted >= p.limit {
+ return true
+ }
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ if p.heap != nil && p.heap.Len() > 0 {
+ return false
+ }
+ for _, s := range p.partitions {
+ if s.head < len(s.docs) {
+ return false
+ }
+ }
+ return true
+}
+
+// Run drives one pipeline turn.
+// 1. First call: fan out one request per pk-range carrying the (substituted)
+// rewritten query.
+// 2. After every partition has provided data, drain the heap into `pending`,
+// then emit.
+func (p *orderByPipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ reqs := make([]queryengine.QueryRequest, len(p.pkRangeIDs))
+ for i, id := range p.pkRangeIDs {
+ reqs[i] = queryengine.QueryRequest{
+ PartitionKeyRangeID: id,
+ Id: uint64(i),
+ Query: p.rewrittenQuery,
+ IncludeParameters: true,
+ }
+ }
+ p.issued = true
+ return &queryengine.PipelineResult{Requests: reqs}, nil
+ }
+
+ if p.heap == nil && p.allPartitionsReady() {
+ p.initHeap()
+ p.drain()
+ }
+
+ if len(p.pending) > 0 {
+ batch := p.pending
+ p.pending = nil
+ return &queryengine.PipelineResult{Items: batch, IsCompleted: p.IsComplete()}, nil
+ }
+ return &queryengine.PipelineResult{IsCompleted: p.IsComplete()}, nil
+}
+
+// ProvideData parses and stores each partition's response. Validates that
+// every document has at least one orderByItems entry and a payload.
+func (p *orderByPipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ resp, err := parseOrderByPartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for i, doc := range resp.Documents {
+ if len(doc.OrderByItems) == 0 {
+ return errors.Errorf("gonative: order-by doc %d missing orderByItems", i)
+ }
+ if len(doc.Payload) == 0 {
+ return errors.Errorf("gonative: order-by doc %d missing payload", i)
+ }
+ }
+ if r.NextContinuation != "" {
+ return errors.New("gonative: order-by partition returned a continuation token (multi-page not supported in Stage 3)")
+ }
+ idx, ok := p.partitionByID[r.PartitionKeyRangeID]
+ if !ok {
+ return errors.Errorf("gonative: unexpected partition id %q", r.PartitionKeyRangeID)
+ }
+ p.partitions[idx].docs = append(p.partitions[idx].docs, resp.Documents...)
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
+
+func (p *orderByPipeline) allPartitionsReady() bool {
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ return true
+}
+
+// initHeap seeds the merge heap with every non-empty partition's head.
+func (p *orderByPipeline) initHeap() {
+ h := &mergeHeap{partitions: p.partitions, descending: p.descending}
+ for i, s := range p.partitions {
+ if _, ok := s.peek(); ok {
+ h.idx = append(h.idx, i)
+ }
+ }
+ heap.Init(h)
+ p.heap = h
+}
+
+// drain flushes the k-way merge into p.pending. After the single-page
+// guarantee the heap holds at most one row per partition; popping the top,
+// advancing that partition, and re-pushing (if not drained) emits the
+// globally-next row. Runs to completion in one Run() turn.
+//
+// OFFSET/LIMIT (Stage 5) apply here: the first `offset` rows are discarded,
+// and emission stops once `limit` rows have been collected (when limit >= 0).
+// Both are tracked on the pipeline across calls so a partial drain would
+// resume correctly — though in practice drain runs to completion.
+func (p *orderByPipeline) drain() {
+ for p.heap.Len() > 0 {
+ if p.limit >= 0 && p.emitted >= p.limit {
+ // LIMIT reached — stop popping and drop the remaining heap state
+ // so IsComplete trips on the next turn.
+ p.heap.idx = p.heap.idx[:0]
+ return
+ }
+ top, ok := heap.Pop(p.heap).(int)
+ if !ok {
+ // Unreachable: the heap is seeded with partition indexes only.
+ p.heap.idx = p.heap.idx[:0]
+ return
+ }
+ s := p.partitions[top]
+ doc := s.pop()
+ if p.skipped < p.offset {
+ p.skipped++
+ } else {
+ // Defensive copy — json.RawMessage aliases the underlying buffer.
+ p.pending = append(p.pending, append([]byte(nil), doc.Payload...))
+ p.emitted++
+ }
+ if _, ok := s.peek(); ok {
+ heap.Push(p.heap, top)
+ }
+ }
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/merge_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/merge_test.go
new file mode 100644
index 0000000000..4217df6a40
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/merge_test.go
@@ -0,0 +1,390 @@
+package gonative
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// mkOrderByBody constructs a partition response body carrying an arbitrary
+// sequence of (orderByValue, payload) pairs. The sequence is NOT sorted here —
+// callers pass a pre-sorted list because the gateway would have sorted within
+// the partition.
+func mkOrderByBody(t *testing.T, entries ...struct {
+ ob, payload string
+}) []byte {
+ t.Helper()
+ docs := make([]orderByDoc, 0, len(entries))
+ for _, e := range entries {
+ docs = append(docs, orderByDoc{
+ OrderByItems: []orderByItem{{Item: json.RawMessage(e.ob)}},
+ Payload: json.RawMessage(e.payload),
+ })
+ }
+ body, err := json.Marshal(struct {
+ Documents []orderByDoc `json:"Documents"`
+ }{docs})
+ require.NoError(t, err)
+ return body
+}
+
+// ordered is a shorthand for the (orderByItems[0].item, payload) tuple used
+// in fixture construction.
+type ordered = struct {
+ ob, payload string
+}
+
+func newAscPipeline(t *testing.T, pkRangeIDs ...string) *orderByPipeline {
+ t.Helper()
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ plan.QueryInfo.RewrittenQuery = `SELECT c._rid, [{"item":c.x}] AS orderByItems, c AS payload FROM c WHERE ({documentdb-formattableorderbyquery-filter}) ORDER BY c.x ASC`
+ p, err := newOrderByPipeline(plan, pkRangeIDs)
+ require.NoError(t, err)
+ return p
+}
+
+func newDescPipeline(t *testing.T, pkRangeIDs ...string) *orderByPipeline {
+ t.Helper()
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Descending"}
+ plan.QueryInfo.RewrittenQuery = `SELECT c._rid, [{"item":c.x}] AS orderByItems, c AS payload FROM c WHERE ({documentdb-formattableorderbyquery-filter}) ORDER BY c.x DESC`
+ p, err := newOrderByPipeline(plan, pkRangeIDs)
+ require.NoError(t, err)
+ return p
+}
+
+// runOnePass drives the pipeline through: initial Run() → feed → flush.
+// Returns every emitted row as a JSON string.
+func runOnePass(t *testing.T, p *orderByPipeline, feed map[string][]byte) []string {
+ t.Helper()
+ res, err := p.Run()
+ require.NoError(t, err)
+ require.NotEmpty(t, res.Requests, "expected initial fan-out")
+
+ results := make([]queryengine.QueryResult, 0, len(res.Requests))
+ for _, req := range res.Requests {
+ body, ok := feed[req.PartitionKeyRangeID]
+ require.True(t, ok, "missing fixture for %s", req.PartitionKeyRangeID)
+ results = append(results, queryengine.NewQueryResult(req.PartitionKeyRangeID, body, ""))
+ }
+ require.NoError(t, p.ProvideData(results))
+
+ var out []string
+ for !p.IsComplete() {
+ r, err := p.Run()
+ require.NoError(t, err)
+ for _, it := range r.Items {
+ out = append(out, string(it))
+ }
+ }
+ return out
+}
+
+// --------------------------------------------------------------- placeholder
+
+func TestNewOrderByPipeline_SubstitutesFilterPlaceholder(t *testing.T) {
+ p := newAscPipeline(t, "0")
+ assert.NotContains(t, p.Query(), orderByQueryFilterPlaceholder)
+ assert.Contains(t, p.Query(), "WHERE (true)",
+ "initial per-partition request must substitute the placeholder with true")
+}
+
+// ----------------------------------------------------------- single-partition
+
+func TestOrderByPipeline_SinglePartition_Ascending(t *testing.T) {
+ p := newAscPipeline(t, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ordered{ob: `5`, payload: `{"x":5}`},
+ ordered{ob: `9`, payload: `{"x":9}`},
+ ),
+ })
+ assert.Equal(t, []string{`{"x":1}`, `{"x":5}`, `{"x":9}`}, rows)
+}
+
+func TestOrderByPipeline_SinglePartition_Descending(t *testing.T) {
+ p := newDescPipeline(t, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `9`, payload: `{"x":9}`},
+ ordered{ob: `5`, payload: `{"x":5}`},
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ),
+ })
+ assert.Equal(t, []string{`{"x":9}`, `{"x":5}`, `{"x":1}`}, rows)
+}
+
+// ----------------------------------------------------------- multi-partition
+
+func TestOrderByPipeline_MultiPartitionKWayMerge_Ascending(t *testing.T) {
+ // Three pre-sorted streams; expect interleaved global order.
+ p := newAscPipeline(t, "A", "B", "C")
+ rows := runOnePass(t, p, map[string][]byte{
+ "A": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"id":"a1"}`},
+ ordered{ob: `4`, payload: `{"id":"a4"}`},
+ ordered{ob: `7`, payload: `{"id":"a7"}`},
+ ),
+ "B": mkOrderByBody(t,
+ ordered{ob: `2`, payload: `{"id":"b2"}`},
+ ordered{ob: `5`, payload: `{"id":"b5"}`},
+ ordered{ob: `8`, payload: `{"id":"b8"}`},
+ ),
+ "C": mkOrderByBody(t,
+ ordered{ob: `3`, payload: `{"id":"c3"}`},
+ ordered{ob: `6`, payload: `{"id":"c6"}`},
+ ordered{ob: `9`, payload: `{"id":"c9"}`},
+ ),
+ })
+ assert.Equal(t, []string{
+ `{"id":"a1"}`, `{"id":"b2"}`, `{"id":"c3"}`,
+ `{"id":"a4"}`, `{"id":"b5"}`, `{"id":"c6"}`,
+ `{"id":"a7"}`, `{"id":"b8"}`, `{"id":"c9"}`,
+ }, rows)
+}
+
+func TestOrderByPipeline_MultiPartitionKWayMerge_Descending(t *testing.T) {
+ p := newDescPipeline(t, "A", "B")
+ rows := runOnePass(t, p, map[string][]byte{
+ "A": mkOrderByBody(t,
+ ordered{ob: `9`, payload: `{"id":"a9"}`},
+ ordered{ob: `5`, payload: `{"id":"a5"}`},
+ ordered{ob: `1`, payload: `{"id":"a1"}`},
+ ),
+ "B": mkOrderByBody(t,
+ ordered{ob: `10`, payload: `{"id":"b10"}`},
+ ordered{ob: `7`, payload: `{"id":"b7"}`},
+ ordered{ob: `3`, payload: `{"id":"b3"}`},
+ ),
+ })
+ assert.Equal(t, []string{
+ `{"id":"b10"}`, `{"id":"a9"}`, `{"id":"b7"}`, `{"id":"a5"}`, `{"id":"b3"}`, `{"id":"a1"}`,
+ }, rows)
+}
+
+// --------------------------------------- type-aware ordering (Cosmos rules)
+
+func TestOrderByPipeline_MixedTypes_Ascending(t *testing.T) {
+ // Cosmos order: undefined < null < bool < number < string.
+ // One partition emits numbers, another emits strings. ASC => numbers first.
+ p := newAscPipeline(t, "nums", "strs")
+ rows := runOnePass(t, p, map[string][]byte{
+ "nums": mkOrderByBody(t,
+ ordered{ob: `3`, payload: `{"kind":"num","v":3}`},
+ ordered{ob: `7`, payload: `{"kind":"num","v":7}`},
+ ),
+ "strs": mkOrderByBody(t,
+ ordered{ob: `"AD"`, payload: `{"kind":"str","v":"AD"}`},
+ ordered{ob: `"AE"`, payload: `{"kind":"str","v":"AE"}`},
+ ),
+ })
+ assert.Equal(t, []string{
+ `{"kind":"num","v":3}`,
+ `{"kind":"num","v":7}`,
+ `{"kind":"str","v":"AD"}`,
+ `{"kind":"str","v":"AE"}`,
+ }, rows)
+}
+
+func TestOrderByPipeline_MixedTypes_Descending(t *testing.T) {
+ p := newDescPipeline(t, "nums", "strs")
+ rows := runOnePass(t, p, map[string][]byte{
+ "nums": mkOrderByBody(t,
+ ordered{ob: `7`, payload: `{"v":7}`},
+ ordered{ob: `3`, payload: `{"v":3}`},
+ ),
+ "strs": mkOrderByBody(t,
+ ordered{ob: `"AE"`, payload: `{"v":"AE"}`},
+ ordered{ob: `"AD"`, payload: `{"v":"AD"}`},
+ ),
+ })
+ // DESC: strings first (largest), then numbers.
+ assert.Equal(t, []string{
+ `{"v":"AE"}`, `{"v":"AD"}`, `{"v":7}`, `{"v":3}`,
+ }, rows)
+}
+
+// --------------------------------------- empty and malformed partitions
+
+func TestOrderByPipeline_EmptyPartition(t *testing.T) {
+ p := newAscPipeline(t, "A", "B")
+ rows := runOnePass(t, p, map[string][]byte{
+ "A": mkOrderByBody(t), // empty
+ "B": mkOrderByBody(t, ordered{ob: `5`, payload: `{"x":5}`}),
+ })
+ assert.Equal(t, []string{`{"x":5}`}, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestOrderByPipeline_AllPartitionsEmpty(t *testing.T) {
+ p := newAscPipeline(t, "A", "B")
+ rows := runOnePass(t, p, map[string][]byte{
+ "A": mkOrderByBody(t),
+ "B": mkOrderByBody(t),
+ })
+ assert.Empty(t, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestOrderByPipeline_RejectsMultiKeyPlan(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Ascending", "Descending"}
+ plan.QueryInfo.RewrittenQuery = "SELECT ... ORDER BY c.a, c.b"
+ _, err := newOrderByPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestOrderByPipeline_RejectsMissingPayload(t *testing.T) {
+ p := newAscPipeline(t, "0")
+ _, _ = p.Run()
+ // Document with orderByItems but no payload.
+ body := []byte(`{"Documents":[{"orderByItems":[{"item":1}]}]}`)
+ err := p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "missing payload")
+}
+
+func TestOrderByPipeline_RejectsMissingOrderByItems(t *testing.T) {
+ p := newAscPipeline(t, "0")
+ _, _ = p.Run()
+ body := []byte(`{"Documents":[{"payload":{"x":1}}]}`)
+ err := p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "missing orderByItems")
+}
+
+// ------------------------------------------------------------- OFFSET/LIMIT
+
+func newAscOffsetLimit(t *testing.T, offset, limit int, pkRangeIDs ...string) *orderByPipeline {
+ t.Helper()
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ plan.QueryInfo.RewrittenQuery = `SELECT c._rid, [{"item":c.x}] AS orderByItems, c AS payload FROM c WHERE ({documentdb-formattableorderbyquery-filter}) ORDER BY c.x ASC`
+ off := offset
+ lim := limit
+ plan.QueryInfo.Offset = &off
+ plan.QueryInfo.Limit = &lim
+ p, err := newOrderByPipeline(plan, pkRangeIDs)
+ require.NoError(t, err)
+ return p
+}
+
+func TestOrderByPipeline_Offset_SkipsFirstN(t *testing.T) {
+ p := newAscOffsetLimit(t, 3, 10, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ordered{ob: `2`, payload: `{"x":2}`},
+ ordered{ob: `3`, payload: `{"x":3}`},
+ ordered{ob: `4`, payload: `{"x":4}`},
+ ordered{ob: `5`, payload: `{"x":5}`},
+ ),
+ })
+ assert.Equal(t, []string{`{"x":4}`, `{"x":5}`}, rows)
+}
+
+func TestOrderByPipeline_Limit_CapsAtN(t *testing.T) {
+ p := newAscOffsetLimit(t, 0, 3, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ordered{ob: `2`, payload: `{"x":2}`},
+ ordered{ob: `3`, payload: `{"x":3}`},
+ ordered{ob: `4`, payload: `{"x":4}`},
+ ordered{ob: `5`, payload: `{"x":5}`},
+ ),
+ })
+ assert.Equal(t, []string{`{"x":1}`, `{"x":2}`, `{"x":3}`}, rows)
+}
+
+func TestOrderByPipeline_OffsetAndLimit_MultiPartition(t *testing.T) {
+ // Global merged order: 1,2,3,4,5,6,7,8,9.
+ // OFFSET 2 LIMIT 4 → 3,4,5,6.
+ p := newAscOffsetLimit(t, 2, 4, "A", "B", "C")
+ rows := runOnePass(t, p, map[string][]byte{
+ "A": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"id":"a1"}`},
+ ordered{ob: `4`, payload: `{"id":"a4"}`},
+ ordered{ob: `7`, payload: `{"id":"a7"}`},
+ ),
+ "B": mkOrderByBody(t,
+ ordered{ob: `2`, payload: `{"id":"b2"}`},
+ ordered{ob: `5`, payload: `{"id":"b5"}`},
+ ordered{ob: `8`, payload: `{"id":"b8"}`},
+ ),
+ "C": mkOrderByBody(t,
+ ordered{ob: `3`, payload: `{"id":"c3"}`},
+ ordered{ob: `6`, payload: `{"id":"c6"}`},
+ ordered{ob: `9`, payload: `{"id":"c9"}`},
+ ),
+ })
+ assert.Equal(t, []string{
+ `{"id":"c3"}`, `{"id":"a4"}`, `{"id":"b5"}`, `{"id":"c6"}`,
+ }, rows)
+}
+
+func TestOrderByPipeline_OffsetBeyondAvailableRows(t *testing.T) {
+ p := newAscOffsetLimit(t, 100, 10, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ordered{ob: `2`, payload: `{"x":2}`},
+ ),
+ })
+ assert.Empty(t, rows, "OFFSET beyond the total must emit nothing")
+ assert.True(t, p.IsComplete())
+}
+
+func TestOrderByPipeline_LimitZero_EmitsNothing(t *testing.T) {
+ p := newAscOffsetLimit(t, 0, 0, "0")
+ rows := runOnePass(t, p, map[string][]byte{
+ "0": mkOrderByBody(t,
+ ordered{ob: `1`, payload: `{"x":1}`},
+ ordered{ob: `2`, payload: `{"x":2}`},
+ ),
+ })
+ assert.Empty(t, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestNewOrderByPipeline_RejectsNegativeOffset(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ plan.QueryInfo.RewrittenQuery = `SELECT ... ORDER BY c.x`
+ neg := -1
+ plan.QueryInfo.Offset = &neg
+ _, err := newOrderByPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "negative OFFSET")
+}
+
+func TestNewOrderByPipeline_RejectsNegativeLimit(t *testing.T) {
+ plan := &planDoc{}
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ plan.QueryInfo.RewrittenQuery = `SELECT ... ORDER BY c.x`
+ neg := -1
+ plan.QueryInfo.Limit = &neg
+ _, err := newOrderByPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "negative LIMIT")
+}
+
+func TestOrderByPipeline_RejectsContinuationToken(t *testing.T) {
+ p := newAscPipeline(t, "0")
+ _, _ = p.Run()
+ body := mkOrderByBody(t, ordered{ob: `1`, payload: `{}`})
+ err := p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: body, NextContinuation: "cont"},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "continuation token")
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/partition.go b/backend/plugin/db/cosmosdb/queryengine/gonative/partition.go
new file mode 100644
index 0000000000..6dee2a3904
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/partition.go
@@ -0,0 +1,71 @@
+package gonative
+
+import (
+ "encoding/json"
+
+ "github.com/pkg/errors"
+)
+
+// Per-partition response shapes observed in
+// queryengine/gonative/testdata after the gateway has rewritten an
+// aggregate query. Two shapes:
+//
+// VALUE form — plan.QueryInfo.Aggregates non-empty:
+// {"Documents":[[{"item":18}]]}
+// Each Documents[i] is an array of one item-object per aggregate in
+// plan.QueryInfo.Aggregates (positional match).
+//
+// Aliased form — plan.QueryInfo.GroupByAliasToAggregateType non-empty:
+// {"Documents":[{"payload":{"alias":{"item":...,"item2":{...}}}}]}
+// Each Documents[i] is a single object keyed by "payload"; payload is
+// keyed by alias name. Item2 is present for MIN/MAX (reliable partial
+// {min|max, count}); for COUNT/SUM/AVG only Item is populated.
+
+// valueItem is each element inside a VALUE-form partition document.
+type valueItem struct {
+ Item json.RawMessage `json:"item"`
+}
+
+// valuePartitionResponse decodes VALUE-form per-partition bodies.
+// Documents[i] is an array whose length equals the number of aggregates.
+type valuePartitionResponse struct {
+ Documents [][]valueItem `json:"Documents"`
+}
+
+// parseValuePartitionResponse decodes a per-partition response body for a
+// VALUE-form aggregate query.
+func parseValuePartitionResponse(raw []byte) (*valuePartitionResponse, error) {
+ var r valuePartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse value partition response")
+ }
+ return &r, nil
+}
+
+// aliasedItem is the per-alias partial inside an aliased-form partition
+// document's payload. Item is always present; Item2 is populated only for
+// MIN/MAX and carries the reliable cross-partition partial.
+type aliasedItem struct {
+ Item json.RawMessage `json:"item"`
+ Item2 json.RawMessage `json:"item2,omitempty"`
+}
+
+// aliasedDoc is one entry of Documents for an aliased aggregate query.
+type aliasedDoc struct {
+ Payload map[string]aliasedItem `json:"payload"`
+}
+
+// aliasedPartitionResponse decodes aliased-form per-partition bodies.
+type aliasedPartitionResponse struct {
+ Documents []aliasedDoc `json:"Documents"`
+}
+
+// parseAliasedPartitionResponse decodes a per-partition response body for an
+// aliased-form aggregate query (the single-group GROUP BY protocol).
+func parseAliasedPartitionResponse(raw []byte) (*aliasedPartitionResponse, error) {
+ var r aliasedPartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse aliased partition response")
+ }
+ return &r, nil
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/partition_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/partition_test.go
new file mode 100644
index 0000000000..b689baf2f8
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/partition_test.go
@@ -0,0 +1,121 @@
+package gonative
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestParseValuePartition_CountIsNumeric(t *testing.T) {
+ // 5.1b: SELECT VALUE COUNT(1) FROM c — partition doc is [ {"item": <number>} ].
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_1b_partitions", "range_0.json"))
+ require.NoError(t, err)
+ p, err := parseValuePartitionResponse(raw)
+ require.NoError(t, err)
+ require.NotEmpty(t, p.Documents)
+ require.Len(t, p.Documents[0], 1, "single-aggregate partition has one item per document")
+ var n float64
+ require.NoError(t, json.Unmarshal(p.Documents[0][0].Item, &n))
+ assert.Positive(t, n)
+}
+
+func TestParseValuePartition_SumIsNumeric(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_3b_partitions", "range_0.json"))
+ require.NoError(t, err)
+ p, err := parseValuePartitionResponse(raw)
+ require.NoError(t, err)
+ require.NotEmpty(t, p.Documents)
+ require.Len(t, p.Documents[0], 1)
+ var n float64
+ require.NoError(t, json.Unmarshal(p.Documents[0][0].Item, &n))
+ assert.Positive(t, n)
+}
+
+func TestParseValuePartition_AvgHasSumAndCount(t *testing.T) {
+ // 5.4b: SELECT VALUE AVG(...) FROM c — item is {"sum": s, "count": n}.
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_4b_partitions", "range_0.json"))
+ require.NoError(t, err)
+ p, err := parseValuePartitionResponse(raw)
+ require.NoError(t, err)
+ require.NotEmpty(t, p.Documents)
+ require.Len(t, p.Documents[0], 1)
+ var partial map[string]json.RawMessage
+ require.NoError(t, json.Unmarshal(p.Documents[0][0].Item, &partial))
+ assert.Contains(t, partial, "sum")
+ assert.Contains(t, partial, "count")
+}
+
+func TestParseAliasedPartition_Count(t *testing.T) {
+ // 5.1: SELECT COUNT(1) AS totalRecords FROM c — payload.totalRecords.item.
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_1_partitions", "range_0.json"))
+ require.NoError(t, err)
+ p, err := parseAliasedPartitionResponse(raw)
+ require.NoError(t, err)
+ require.NotEmpty(t, p.Documents)
+ alias, ok := p.Documents[0].Payload["totalRecords"]
+ require.True(t, ok, "expected totalRecords alias in payload")
+ var n float64
+ require.NoError(t, json.Unmarshal(alias.Item, &n))
+ assert.Positive(t, n)
+ assert.Empty(t, alias.Item2, "Count aliased carries only item, no item2")
+}
+
+func TestParseAliasedPartition_SumAndAvg(t *testing.T) {
+ // 5.3: SELECT SUM(...) AS totalPop FROM c
+ sumRaw, err := os.ReadFile(filepath.Join("testdata", "query_5_3_partitions", "range_0.json"))
+ require.NoError(t, err)
+ sumP, err := parseAliasedPartitionResponse(sumRaw)
+ require.NoError(t, err)
+ require.NotEmpty(t, sumP.Documents)
+ _, ok := sumP.Documents[0].Payload["totalPop"]
+ require.True(t, ok)
+
+ // 5.4: SELECT AVG(...) AS avgPop FROM c — item contains {"sum": s, "count": n}.
+ avgRaw, err := os.ReadFile(filepath.Join("testdata", "query_5_4_partitions", "range_0.json"))
+ require.NoError(t, err)
+ avgP, err := parseAliasedPartitionResponse(avgRaw)
+ require.NoError(t, err)
+ avgAlias, ok := avgP.Documents[0].Payload["avgPop"]
+ require.True(t, ok)
+ var partial map[string]json.RawMessage
+ require.NoError(t, json.Unmarshal(avgAlias.Item, &partial))
+ assert.Contains(t, partial, "sum")
+ assert.Contains(t, partial, "count")
+}
+
+func TestParseAliasedPartition_MinMaxHasItem2(t *testing.T) {
+ // 5.5: multi-agg MIN/MAX aliased — item2 carries the reliable cross-partition partial.
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_5_partitions", "range_0.json"))
+ require.NoError(t, err)
+ p, err := parseAliasedPartitionResponse(raw)
+ require.NoError(t, err)
+ require.NotEmpty(t, p.Documents)
+
+ minPop, ok := p.Documents[0].Payload["minPop"]
+ require.True(t, ok)
+ require.NotEmpty(t, minPop.Item2, "Min aliased MUST carry item2 for cross-partition correctness")
+ var reliable struct {
+ Min json.RawMessage `json:"min"`
+ Count int `json:"count"`
+ }
+ require.NoError(t, json.Unmarshal(minPop.Item2, &reliable))
+ assert.NotZero(t, reliable.Count)
+
+ maxPop, ok := p.Documents[0].Payload["maxPop"]
+ require.True(t, ok)
+ require.NotEmpty(t, maxPop.Item2, "Max aliased MUST carry item2")
+}
+
+func TestParseValuePartition_RejectsInvalidJSON(t *testing.T) {
+ _, err := parseValuePartitionResponse([]byte(`not json`))
+ require.Error(t, err)
+}
+
+func TestParseAliasedPartition_RejectsInvalidJSON(t *testing.T) {
+ _, err := parseAliasedPartitionResponse([]byte(`not json`))
+ require.Error(t, err)
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline.go b/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline.go
new file mode 100644
index 0000000000..c0607b373e
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline.go
@@ -0,0 +1,303 @@
+package gonative
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/pkg/errors"
+)
+
+// aggPipelineBase holds state common to both the VALUE-form and aliased-form
+// aggregate pipelines. Aggregators execute in a fixed positional order — the
+// pipeline knows how many slots it needs before the first partition response
+// arrives, and which slot each partial lands in.
+type aggPipelineBase struct {
+ rewrittenQuery string
+ pkRangeIDs []string
+ aggregators []aggregator // len == number of aggregates to emit
+ partitionsDone map[string]bool
+ issued bool
+ emitted bool
+ closed bool
+}
+
+// issueRequests constructs the initial fan-out: one QueryRequest per
+// partition-key-range, all carrying the rewritten query the gateway produced.
+func (b *aggPipelineBase) issueRequests() []queryengine.QueryRequest {
+ reqs := make([]queryengine.QueryRequest, len(b.pkRangeIDs))
+ for i, id := range b.pkRangeIDs {
+ reqs[i] = queryengine.QueryRequest{
+ PartitionKeyRangeID: id,
+ Id: uint64(i),
+ Query: b.rewrittenQuery,
+ IncludeParameters: true,
+ }
+ }
+ b.issued = true
+ return reqs
+}
+
+// allPartitionsDone reports whether every pk-range has finished streaming.
+func (b *aggPipelineBase) allPartitionsDone() bool {
+ if len(b.pkRangeIDs) == 0 {
+ return true
+ }
+ for _, id := range b.pkRangeIDs {
+ if !b.partitionsDone[id] {
+ return false
+ }
+ }
+ return true
+}
+
+// -----------------------------------------------------------------------------
+// VALUE-form pipeline — plan.QueryInfo.Aggregates non-empty, hasSelectValue true.
+// Emits one final scalar (single aggregate) or one JSON array (multi-aggregate).
+
+type valuePipeline struct {
+ aggPipelineBase
+}
+
+// newValuePipeline builds a VALUE-form pipeline for the given plan + pk-ranges.
+// Returns an error if the plan references an aggregate kind the engine cannot
+// handle, so the caller can surface ErrUnsupportedPlanFeature.
+func newValuePipeline(plan *planDoc, pkRangeIDs []string) (*valuePipeline, error) {
+ aggs := make([]aggregator, len(plan.QueryInfo.Aggregates))
+ for i, kind := range plan.QueryInfo.Aggregates {
+ a := newAggregator(kind)
+ if a == nil {
+ return nil, errors.Errorf("gonative: unsupported aggregate %q", kind)
+ }
+ aggs[i] = a
+ }
+ return &valuePipeline{
+ aggPipelineBase: aggPipelineBase{
+ rewrittenQuery: plan.QueryInfo.RewrittenQuery,
+ pkRangeIDs: pkRangeIDs,
+ aggregators: aggs,
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ },
+ }, nil
+}
+
+// Query returns the gateway-rewritten per-partition query.
+func (p *valuePipeline) Query() string { return p.rewrittenQuery }
+
+// IsComplete returns true after the final row has been emitted.
+func (p *valuePipeline) IsComplete() bool { return p.emitted }
+
+// Close releases pipeline state. Idempotent and a no-op for pure-Go pipelines.
+func (p *valuePipeline) Close() { p.closed = true }
+
+// Run drives one pipeline turn. Must be called in the order:
+// 1. Run() — returns per-partition Requests on first call.
+// 2. Caller invokes ProvideData for each Request's partition response.
+// 3. Run() — finalizes aggregators and returns the single result row.
+func (p *valuePipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ return &queryengine.PipelineResult{Requests: p.issueRequests()}, nil
+ }
+ if p.emitted {
+ return &queryengine.PipelineResult{IsCompleted: true}, nil
+ }
+ if !p.allPartitionsDone() {
+ // Caller has not yet fed every partition — no new work to surface.
+ return &queryengine.PipelineResult{}, nil
+ }
+ item, err := p.finalRow()
+ if err != nil {
+ return nil, err
+ }
+ p.emitted = true
+ return &queryengine.PipelineResult{Items: [][]byte{item}, IsCompleted: true}, nil
+}
+
+// ProvideData feeds per-partition response bodies into the pipeline.
+func (p *valuePipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ resp, err := parseValuePartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for _, doc := range resp.Documents {
+ if len(doc) != len(p.aggregators) {
+ return errors.Errorf("gonative: value partition document has %d items, expected %d", len(doc), len(p.aggregators))
+ }
+ for i, item := range doc {
+ if err := p.aggregators[i].feedPartial(item.Item); err != nil {
+ return err
+ }
+ }
+ }
+ // Stage 1 assumes one page per partition for aggregates; NextContinuation
+ // should be empty. Flag explicitly if the assumption breaks.
+ if r.NextContinuation != "" {
+ return errors.New("gonative: aggregate partition returned a continuation token (multi-page not supported in Stage 1)")
+ }
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
+
+// finalRow produces the final emitted row for a VALUE-form aggregate query.
+// For single-aggregate queries it is the finalized scalar itself; for
+// multi-aggregate queries (plan.QueryInfo.Aggregates with len > 1) it is a
+// JSON array of finalized scalars in positional order.
+func (p *valuePipeline) finalRow() ([]byte, error) {
+ finals := make([]json.RawMessage, len(p.aggregators))
+ for i, a := range p.aggregators {
+ v, err := a.finalize()
+ if err != nil {
+ return nil, err
+ }
+ finals[i] = v
+ }
+ if len(finals) == 1 {
+ return finals[0], nil
+ }
+ return json.Marshal(finals)
+}
+
+// -----------------------------------------------------------------------------
+// Aliased-form pipeline — plan has groupByAliases + groupByAliasToAggregateType.
+// Emits one object row: {"<alias1>": <v1>, "<alias2>": <v2>, ...}.
+
+type aliasedPipeline struct {
+ aggPipelineBase
+ aliases []string // preserved order from plan.QueryInfo.GroupByAliases
+ kinds []string // positional: aggregate kind per alias
+}
+
+func newAliasedPipeline(plan *planDoc, pkRangeIDs []string) (*aliasedPipeline, error) {
+ if len(plan.QueryInfo.GroupByAliases) == 0 {
+ return nil, errors.New("gonative: aliased plan has no aliases")
+ }
+ aliases := append([]string(nil), plan.QueryInfo.GroupByAliases...)
+ kinds := make([]string, len(aliases))
+ aggs := make([]aggregator, len(aliases))
+ for i, alias := range aliases {
+ kind, ok := plan.QueryInfo.GroupByAliasToAggregateType[alias]
+ if !ok {
+ return nil, errors.Errorf("gonative: alias %q missing from groupByAliasToAggregateType", alias)
+ }
+ a := newAggregator(kind)
+ if a == nil {
+ return nil, errors.Errorf("gonative: unsupported aggregate %q for alias %q", kind, alias)
+ }
+ kinds[i] = kind
+ aggs[i] = a
+ }
+ return &aliasedPipeline{
+ aggPipelineBase: aggPipelineBase{
+ rewrittenQuery: plan.QueryInfo.RewrittenQuery,
+ pkRangeIDs: pkRangeIDs,
+ aggregators: aggs,
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ },
+ aliases: aliases,
+ kinds: kinds,
+ }, nil
+}
+
+func (p *aliasedPipeline) Query() string { return p.rewrittenQuery }
+func (p *aliasedPipeline) IsComplete() bool { return p.emitted }
+func (p *aliasedPipeline) Close() { p.closed = true }
+
+func (p *aliasedPipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ return &queryengine.PipelineResult{Requests: p.issueRequests()}, nil
+ }
+ if p.emitted {
+ return &queryengine.PipelineResult{IsCompleted: true}, nil
+ }
+ if !p.allPartitionsDone() {
+ return &queryengine.PipelineResult{}, nil
+ }
+ item, err := p.finalRow()
+ if err != nil {
+ return nil, err
+ }
+ p.emitted = true
+ return &queryengine.PipelineResult{Items: [][]byte{item}, IsCompleted: true}, nil
+}
+
+func (p *aliasedPipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ resp, err := parseAliasedPartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for _, doc := range resp.Documents {
+ for slot, alias := range p.aliases {
+ partial, ok := doc.Payload[alias]
+ if !ok {
+ return errors.Errorf("gonative: aliased partition missing alias %q in payload", alias)
+ }
+ if err := p.feedAliasedPartial(slot, p.kinds[slot], partial); err != nil {
+ return err
+ }
+ }
+ }
+ if r.NextContinuation != "" {
+ return errors.New("gonative: aggregate partition returned a continuation token (multi-page not supported in Stage 1)")
+ }
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
+
+// feedAliasedPartial routes an aliasedItem to the right aggregator. For
+// Min/Max it prefers Item2 (the reliable `{min|max, count}` partial) when
+// present, falling back to wrapping Item when the gateway short-circuited to
+// a single-partition shape (Item2 absent).
+func (p *aliasedPipeline) feedAliasedPartial(slot int, kind string, partial aliasedItem) error {
+ switch kind {
+ case "Min":
+ if len(partial.Item2) > 0 {
+ return p.aggregators[slot].feedPartial(partial.Item2)
+ }
+ synth := fmt.Appendf(nil, `{"min":%s,"count":1}`, partial.Item)
+ return p.aggregators[slot].feedPartial(synth)
+ case "Max":
+ if len(partial.Item2) > 0 {
+ return p.aggregators[slot].feedPartial(partial.Item2)
+ }
+ synth := fmt.Appendf(nil, `{"max":%s,"count":1}`, partial.Item)
+ return p.aggregators[slot].feedPartial(synth)
+ default:
+ // Count, Sum, Avg — the Item itself carries the partial in the right shape.
+ return p.aggregators[slot].feedPartial(partial.Item)
+ }
+}
+
+// finalRow builds `{"<alias1>": <v1>, ...}` preserving the original alias order
+// from the plan (matters for deterministic output — map iteration is random).
+func (p *aliasedPipeline) finalRow() ([]byte, error) {
+ finals := make([]json.RawMessage, len(p.aggregators))
+ for i, a := range p.aggregators {
+ v, err := a.finalize()
+ if err != nil {
+ return nil, err
+ }
+ finals[i] = v
+ }
+ // Build the JSON manually to preserve alias order; json.Marshal on a map
+ // would alphabetize.
+ var buf []byte
+ buf = append(buf, '{')
+ for i, alias := range p.aliases {
+ if i > 0 {
+ buf = append(buf, ',')
+ }
+ keyRaw, err := json.Marshal(alias)
+ if err != nil {
+ return nil, err
+ }
+ buf = append(buf, keyRaw...)
+ buf = append(buf, ':')
+ buf = append(buf, finals[i]...)
+ }
+ buf = append(buf, '}')
+ return buf, nil
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline_test.go
new file mode 100644
index 0000000000..fff9433b88
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/pipeline_test.go
@@ -0,0 +1,351 @@
+package gonative_test
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/db/cosmosdb/queryengine/gonative"
+)
+
+// mustReadFile loads a fixture path and fails the test on error.
+func mustReadFile(t *testing.T, path string) []byte {
+ t.Helper()
+ b, err := os.ReadFile(path)
+ require.NoError(t, err, "read %s", path)
+ return b
+}
+
+// partitionBodies returns a map of pk-range-id -> response body for each file
+// found under `dir`. Expects filenames of the form `range_<id>.json`.
+func partitionBodies(t *testing.T, dir string) map[string][]byte {
+ t.Helper()
+ entries, err := os.ReadDir(dir)
+ require.NoError(t, err, "read dir %s", dir)
+ out := make(map[string][]byte, len(entries))
+ for _, e := range entries {
+ name := e.Name()
+ // file is `range_<id>.json` — strip prefix/suffix
+ id := name[len("range_") : len(name)-len(".json")]
+ out[id] = mustReadFile(t, filepath.Join(dir, name))
+ }
+ return out
+}
+
+// driveAggPipeline runs a pipeline end-to-end against captured fixtures and
+// returns the single emitted row.
+func driveAggPipeline(t *testing.T, queryID, originalQuery string) json.RawMessage {
+ t.Helper()
+ base := filepath.Join("testdata", "query_"+queryID)
+ plan := mustReadFile(t, base+"_plan.json")
+ pkranges := mustReadFile(t, base+"_pkranges.json")
+ bodies := partitionBodies(t, base+"_partitions")
+
+ engine := gonative.Default()
+ pipe, err := engine.CreateQueryPipeline(originalQuery, string(plan), string(pkranges))
+ require.NoError(t, err, "CreateQueryPipeline")
+ defer pipe.Close()
+
+ var emitted json.RawMessage
+ for i := 0; i < 10 && !pipe.IsComplete(); i++ { // bounded loop — 10 turns is a safe upper bound for single-partition aggregates
+ res, err := pipe.Run()
+ require.NoError(t, err, "Run turn %d", i)
+ if len(res.Requests) > 0 {
+ results := make([]queryengine.QueryResult, 0, len(res.Requests))
+ for _, req := range res.Requests {
+ body, ok := bodies[req.PartitionKeyRangeID]
+ require.True(t, ok, "missing fixture for pk-range %s", req.PartitionKeyRangeID)
+ results = append(results, queryengine.NewQueryResult(req.PartitionKeyRangeID, body, ""))
+ }
+ require.NoError(t, pipe.ProvideData(results))
+ }
+ if len(res.Items) > 0 {
+ require.Len(t, res.Items, 1, "aggregate query emits exactly one row")
+ emitted = append(json.RawMessage(nil), res.Items[0]...)
+ }
+ }
+ require.True(t, pipe.IsComplete(), "pipeline did not converge")
+ require.NotNil(t, emitted, "no row emitted")
+ return emitted
+}
+
+func TestAggPipeline_5_1_AliasedCount(t *testing.T) {
+ row := driveAggPipeline(t, "5_1", `SELECT COUNT(1) AS totalRecords FROM c`)
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ assert.EqualValues(t, 18, obj["totalRecords"])
+}
+
+func TestAggPipeline_5_1b_ValueCount(t *testing.T) {
+ row := driveAggPipeline(t, "5_1b", `SELECT VALUE COUNT(1) FROM c`)
+ // VALUE form emits the scalar directly.
+ var n float64
+ require.NoError(t, json.Unmarshal(row, &n))
+ assert.EqualValues(t, 18, n)
+}
+
+func TestAggPipeline_5_2_AliasedCountWithFilter(t *testing.T) {
+ row := driveAggPipeline(t, "5_2", `SELECT COUNT(1) AS aeCount FROM c WHERE c.country = "AE"`)
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ assert.EqualValues(t, 5, obj["aeCount"])
+}
+
+func TestAggPipeline_5_2b_ValueCountWithFilter(t *testing.T) {
+ row := driveAggPipeline(t, "5_2b", `SELECT VALUE COUNT(1) FROM c WHERE c.country = "AE"`)
+ var n float64
+ require.NoError(t, json.Unmarshal(row, &n))
+ assert.EqualValues(t, 5, n)
+}
+
+func TestAggPipeline_5_3_AliasedSum(t *testing.T) {
+ row := driveAggPipeline(t, "5_3", `SELECT SUM(StringToNumber(c.population)) AS totalPop FROM c`)
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ assert.Positive(t, obj["totalPop"])
+}
+
+func TestAggPipeline_5_3b_ValueSum(t *testing.T) {
+ row := driveAggPipeline(t, "5_3b", `SELECT VALUE SUM(StringToNumber(c.population)) FROM c`)
+ var n float64
+ require.NoError(t, json.Unmarshal(row, &n))
+ assert.Positive(t, n)
+}
+
+func TestAggPipeline_5_4_AliasedAvg(t *testing.T) {
+ row := driveAggPipeline(t, "5_4", `SELECT AVG(StringToNumber(c.population)) AS avgPop FROM c`)
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ assert.Positive(t, obj["avgPop"])
+}
+
+func TestAggPipeline_5_4b_ValueAvg(t *testing.T) {
+ row := driveAggPipeline(t, "5_4b", `SELECT VALUE AVG(StringToNumber(c.population)) FROM c`)
+ var n float64
+ require.NoError(t, json.Unmarshal(row, &n))
+ assert.Positive(t, n)
+}
+
+func TestAggPipeline_5_5_MultiAggMinMax(t *testing.T) {
+ row := driveAggPipeline(t, "5_5", `SELECT MIN(StringToNumber(c.population)) AS minPop, MAX(StringToNumber(c.population)) AS maxPop FROM c WHERE StringToNumber(c.population) > 0`)
+ // Aliased multi-agg preserves alias order from the plan.
+ assert.Contains(t, string(row), `"minPop"`)
+ assert.Contains(t, string(row), `"maxPop"`)
+ // Order assertion: minPop appears before maxPop because the plan lists it first.
+ idxMin := indexOf(row, []byte(`"minPop"`))
+ idxMax := indexOf(row, []byte(`"maxPop"`))
+ assert.Less(t, idxMin, idxMax, "aliases emitted in plan order")
+
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(row, &obj))
+ minV, ok := obj["minPop"].(float64)
+ require.True(t, ok)
+ maxV, ok := obj["maxPop"].(float64)
+ require.True(t, ok)
+ assert.LessOrEqual(t, minV, maxV)
+}
+
+// drivePipelineAll runs a pipeline end-to-end against captured fixtures and
+// returns every emitted row. Used for operators that emit more than one row
+// (DISTINCT, later Stage 3+ ORDER BY/TOP/LIMIT/GROUP BY).
+func drivePipelineAll(t *testing.T, queryID, originalQuery string) []json.RawMessage {
+ t.Helper()
+ base := filepath.Join("testdata", "query_"+queryID)
+ plan := mustReadFile(t, base+"_plan.json")
+ pkranges := mustReadFile(t, base+"_pkranges.json")
+ bodies := partitionBodies(t, base+"_partitions")
+
+ engine := gonative.Default()
+ pipe, err := engine.CreateQueryPipeline(originalQuery, string(plan), string(pkranges))
+ require.NoError(t, err, "CreateQueryPipeline")
+ defer pipe.Close()
+
+ var rows []json.RawMessage
+ for i := 0; i < 50 && !pipe.IsComplete(); i++ {
+ res, err := pipe.Run()
+ require.NoError(t, err, "Run turn %d", i)
+ if len(res.Requests) > 0 {
+ results := make([]queryengine.QueryResult, 0, len(res.Requests))
+ for _, req := range res.Requests {
+ body, ok := bodies[req.PartitionKeyRangeID]
+ require.True(t, ok, "missing fixture for pk-range %s", req.PartitionKeyRangeID)
+ results = append(results, queryengine.NewQueryResult(req.PartitionKeyRangeID, body, ""))
+ }
+ require.NoError(t, pipe.ProvideData(results))
+ }
+ for _, it := range res.Items {
+ rows = append(rows, append(json.RawMessage(nil), it...))
+ }
+ }
+ require.True(t, pipe.IsComplete(), "pipeline did not converge")
+ return rows
+}
+
+func TestDistinctPipeline_10_1_ObjectDistinct(t *testing.T) {
+ rows := drivePipelineAll(t, "10_1", `SELECT DISTINCT c.country FROM c`)
+ // Captured fixture has 6 unique countries in a single partition.
+ assert.Len(t, rows, 6)
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ country, ok := obj["country"].(string)
+ require.True(t, ok, "each row must carry a country field")
+ assert.False(t, seen[country], "duplicate country emitted: %s", country)
+ seen[country] = true
+ }
+}
+
+func TestGroupPipeline_6_1_CountByCountry(t *testing.T) {
+ rows := drivePipelineAll(t, "6_1", `SELECT c.country, COUNT(1) AS cityCount FROM c GROUP BY c.country`)
+ // Captured fixture covers 6 countries.
+ assert.Len(t, rows, 6)
+ byCountry := map[string]float64{}
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ country, ok := obj["country"].(string)
+ require.True(t, ok, "row missing country: %s", r)
+ cnt, ok := obj["cityCount"].(float64)
+ require.True(t, ok, "row missing cityCount: %s", r)
+ byCountry[country] = cnt
+ }
+ // AE has 5 cities in the test container (from Stage 1 captures).
+ assert.EqualValues(t, 5, byCountry["AE"])
+ // Verify sum of group counts equals the total row count (18).
+ var total float64
+ for _, n := range byCountry {
+ total += n
+ }
+ assert.Equal(t, float64(18), total)
+}
+
+func TestGroupPipeline_6_2_MultipleAggregates(t *testing.T) {
+ rows := drivePipelineAll(t, "6_2", `SELECT c.countryRegion, COUNT(1) AS count, SUM(StringToNumber(c.population)) AS totalPop FROM c GROUP BY c.countryRegion`)
+ assert.NotEmpty(t, rows)
+ // Each row must carry all three aliases and their values must be numeric
+ // (count) / numeric (totalPop) / string (countryRegion).
+ var totalCount float64
+ for _, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ _, ok := obj["countryRegion"].(string)
+ assert.True(t, ok, "countryRegion should be a string: %s", r)
+ cnt, ok := obj["count"].(float64)
+ require.True(t, ok, "count should be numeric: %s", r)
+ _, ok = obj["totalPop"].(float64)
+ assert.True(t, ok, "totalPop should be numeric: %s", r)
+ totalCount += cnt
+ }
+ assert.Equal(t, float64(18), totalCount, "sum of per-region counts must equal total row count")
+}
+
+func TestOrderByPipeline_12_1_OffsetZeroLimitTen(t *testing.T) {
+ rows := drivePipelineAll(t, "12_1", `SELECT * FROM c ORDER BY c.name OFFSET 0 LIMIT 10`)
+ assert.Len(t, rows, 10, "OFFSET 0 LIMIT 10 must emit 10 rows")
+ // Rows should be in ascending order by c.name.
+ var prevName string
+ for i, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj), "row %d: %s", i, r)
+ name, ok := obj["name"].(string)
+ require.True(t, ok, "name must be a string: %s", r)
+ if i > 0 {
+ assert.LessOrEqual(t, prevName, name, "ascending order broken at row %d", i)
+ }
+ prevName = name
+ }
+}
+
+func TestOrderByPipeline_12_2_OffsetTenLimitTen(t *testing.T) {
+ rows := drivePipelineAll(t, "12_2", `SELECT * FROM c ORDER BY c.name OFFSET 10 LIMIT 10`)
+ // WorldCities has 18 rows total; OFFSET 10 LIMIT 10 returns rows 11–18 (8 rows).
+ assert.Len(t, rows, 8)
+ var prevName string
+ for i, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj))
+ name, ok := obj["name"].(string)
+ require.True(t, ok, "name must be a string: %s", r)
+ if i > 0 {
+ assert.LessOrEqual(t, prevName, name)
+ }
+ prevName = name
+ }
+}
+
+func TestTopPipeline_1_3_TopTen(t *testing.T) {
+ rows := drivePipelineAll(t, "1_3", `SELECT TOP 10 * FROM c`)
+ assert.Len(t, rows, 10, "TOP 10 must emit exactly 10 rows")
+ // Every row should be a valid object (not the payload-wrapped form).
+ for i, r := range rows {
+ var obj map[string]any
+ require.NoError(t, json.Unmarshal(r, &obj), "row %d: %s", i, r)
+ assert.NotEmpty(t, obj, "row %d should be a non-empty object", i)
+ }
+}
+
+func TestOrderByPipeline_4_1_Ascending(t *testing.T) {
+ rows := drivePipelineAll(t, "4_1", `SELECT c.name, c.population FROM c ORDER BY c.population ASC`)
+ // Captured fixture: 18 cities in the WorldCities test container, mixed
+ // numeric + string populations (the gateway delivered them pre-sorted).
+ assert.Len(t, rows, 18, "expected one row per city")
+ // First row should be the smallest population (number type comes before
+ // string under Cosmos ordering, so this is a numeric).
+ var first map[string]any
+ require.NoError(t, json.Unmarshal(rows[0], &first))
+ _, ok := first["population"].(float64)
+ assert.True(t, ok, "first row under ASC ordering must be numeric population (numbers sort before strings)")
+}
+
+func TestOrderByPipeline_4_2_Descending(t *testing.T) {
+ rows := drivePipelineAll(t, "4_2", `SELECT c.name, c.population FROM c ORDER BY c.population DESC`)
+ assert.Len(t, rows, 18)
+ // DESC: strings come first (strings > numbers under Cosmos ordering).
+ var first map[string]any
+ require.NoError(t, json.Unmarshal(rows[0], &first))
+ _, ok := first["population"].(string)
+ assert.True(t, ok, "first row under DESC must be a string-valued population (strings sort after numbers)")
+}
+
+func TestDistinctPipeline_10_2_DistinctValue(t *testing.T) {
+ rows := drivePipelineAll(t, "10_2", `SELECT DISTINCT VALUE c.countryRegion FROM c`)
+ // Captured fixture has 6 unique countryRegion scalars.
+ assert.Len(t, rows, 6)
+ seen := map[string]bool{}
+ for _, r := range rows {
+ var v string
+ require.NoError(t, json.Unmarshal(r, &v))
+ assert.False(t, seen[v], "duplicate scalar emitted: %s", v)
+ seen[v] = true
+ }
+}
+
+func TestAggPipeline_11_2_ValueCount(t *testing.T) {
+ row := driveAggPipeline(t, "11_2", `SELECT VALUE COUNT(1) FROM c`)
+ var n float64
+ require.NoError(t, json.Unmarshal(row, &n))
+ assert.EqualValues(t, 18, n)
+}
+
+// indexOf is a tiny helper around bytes.Index to keep the test file imports minimal.
+func indexOf(haystack, needle []byte) int {
+ for i := 0; i+len(needle) <= len(haystack); i++ {
+ match := true
+ for j := range needle {
+ if haystack[i+j] != needle[j] {
+ match = false
+ break
+ }
+ }
+ if match {
+ return i
+ }
+ }
+ return -1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/plan.go b/backend/plugin/db/cosmosdb/queryengine/gonative/plan.go
new file mode 100644
index 0000000000..9f544312e3
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/plan.go
@@ -0,0 +1,88 @@
+package gonative
+
+import (
+ "encoding/json"
+
+ "github.com/pkg/errors"
+)
+
+// planDoc is the subset of the Cosmos query-plan document that Stage 1 uses.
+// Later stages (DISTINCT, ORDER BY, TOP, OFFSET/LIMIT, GROUP BY) will read
+// additional fields; declaring them defensively here keeps Stage 1 parsing
+// tolerant of plans that carry extra information.
+//
+// Observed shapes (see queryengine/gonative/testdata):
+//
+// SELECT VALUE agg(x) FROM c — "VALUE form"
+// aggregates: ["Count"]
+// hasSelectValue: true
+// rewrittenQuery: SELECT VALUE [{"item": agg(...)}] FROM c
+//
+// SELECT agg(x) AS alias FROM c — "aliased form" (single-group GROUP BY)
+// aggregates: []
+// groupByAliases: ["alias"]
+// groupByAliasToAggregateType: {"alias": "Count"|"Sum"|"Min"|"Max"|"Average"}
+// rewrittenQuery: SELECT {"alias": {"item": ..., "item2": {min|max, count}}} AS payload FROM c
+type planDoc struct {
+ PartitionedQueryExecutionInfoVersion int `json:"partitionedQueryExecutionInfoVersion"`
+ QueryInfo planQueryInfo `json:"queryInfo"`
+ QueryRanges []queryRange `json:"queryRanges"`
+}
+
+// planQueryInfo holds the instruction set the engine uses to build a pipeline.
+type planQueryInfo struct {
+ RewrittenQuery string `json:"rewrittenQuery"`
+ HasSelectValue bool `json:"hasSelectValue"`
+ Aggregates []string `json:"aggregates"`
+ GroupByAliases []string `json:"groupByAliases"`
+ GroupByAliasToAggregateType map[string]string `json:"groupByAliasToAggregateType"`
+ // Fields that Stage 1 does not act on but decodes defensively so the
+ // dispatch logic can reject unsupported plans cleanly.
+ DistinctType string `json:"distinctType,omitempty"`
+ Top *int `json:"top,omitempty"`
+ Offset *int `json:"offset,omitempty"`
+ Limit *int `json:"limit,omitempty"`
+ OrderBy []string `json:"orderBy,omitempty"`
+ OrderByExpressions []string `json:"orderByExpressions,omitempty"`
+ GroupByExpressions []string `json:"groupByExpressions,omitempty"`
+ HasNonStreamingOrderBy bool `json:"hasNonStreamingOrderBy,omitempty"`
+}
+
+// queryRange mirrors the gateway's pk-range description inside the plan.
+type queryRange struct {
+ Min string `json:"min"`
+ Max string `json:"max"`
+ IsMinInclusive bool `json:"isMinInclusive"`
+ IsMaxInclusive bool `json:"isMaxInclusive"`
+}
+
+// parsePlan decodes the gateway's query-plan JSON payload.
+func parsePlan(raw []byte) (*planDoc, error) {
+ var p planDoc
+ if err := json.Unmarshal(raw, &p); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse query plan")
+ }
+ return &p, nil
+}
+
+// pkRangesResponse decodes the gateway's pk-ranges response just enough to
+// extract the ordered partition-key-range IDs the pipeline needs to fan out.
+type pkRangesResponse struct {
+ PartitionKeyRanges []struct {
+ ID string `json:"id"`
+ } `json:"PartitionKeyRanges"`
+}
+
+// parsePKRangeIDs returns the partition-key-range IDs from the pk-ranges JSON
+// body the SDK hands to CreateQueryPipeline.
+func parsePKRangeIDs(raw []byte) ([]string, error) {
+ var r pkRangesResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse pk ranges")
+ }
+ ids := make([]string, len(r.PartitionKeyRanges))
+ for i, rng := range r.PartitionKeyRanges {
+ ids[i] = rng.ID
+ }
+ return ids, nil
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/plan_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/plan_test.go
new file mode 100644
index 0000000000..c00bb137e5
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/plan_test.go
@@ -0,0 +1,71 @@
+package gonative
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestParsePlan_5_1_AliasedIsGroupByForm(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_1_plan.json"))
+ require.NoError(t, err)
+ p, err := parsePlan(raw)
+ require.NoError(t, err)
+ // 5.1 is `SELECT COUNT(1) AS totalRecords FROM c`.
+ // IMPORTANT: aliased aggregates use the groupBy-alias wire format (single-group),
+ // NOT the aggregates array. See testdata/query_5_1_plan.json.
+ assert.Empty(t, p.QueryInfo.Aggregates, "aliased form leaves aggregates[] empty")
+ assert.Equal(t, []string{"totalRecords"}, p.QueryInfo.GroupByAliases)
+ assert.Equal(t, map[string]string{"totalRecords": "Count"}, p.QueryInfo.GroupByAliasToAggregateType)
+ assert.False(t, p.QueryInfo.HasSelectValue)
+ assert.Contains(t, p.QueryInfo.RewrittenQuery, "AS payload",
+ "aliased form rewrites to {...} AS payload")
+}
+
+func TestParsePlan_5_1b_ValueForm(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_1b_plan.json"))
+ require.NoError(t, err)
+ p, err := parsePlan(raw)
+ require.NoError(t, err)
+ // 5.1b is `SELECT VALUE COUNT(1) FROM c` — one aggregate, VALUE form.
+ assert.Equal(t, []string{"Count"}, p.QueryInfo.Aggregates)
+ assert.True(t, p.QueryInfo.HasSelectValue)
+ assert.Empty(t, p.QueryInfo.GroupByAliasToAggregateType, "VALUE form has no aliases")
+ assert.Contains(t, p.QueryInfo.RewrittenQuery, `SELECT VALUE [{"item":`,
+ "VALUE form rewrites to an array of {item: ...} objects")
+}
+
+func TestParsePlan_5_5_MultiAgg_Aliased(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_5_plan.json"))
+ require.NoError(t, err)
+ p, err := parsePlan(raw)
+ require.NoError(t, err)
+ // 5.5 has two aliased aggregates — same groupBy form, just two aliases.
+ assert.Empty(t, p.QueryInfo.Aggregates)
+ assert.ElementsMatch(t, []string{"minPop", "maxPop"}, p.QueryInfo.GroupByAliases)
+ assert.Equal(t, map[string]string{"minPop": "Min", "maxPop": "Max"}, p.QueryInfo.GroupByAliasToAggregateType)
+ // MIN/MAX aliased include "item2" partial for cross-partition correctness.
+ assert.Contains(t, p.QueryInfo.RewrittenQuery, `"item2"`,
+ "MIN/MAX aliased include item2: {min|max, count} for cross-partition correctness")
+}
+
+func TestParsePKRangeIDs(t *testing.T) {
+ raw, err := os.ReadFile(filepath.Join("testdata", "query_5_1_pkranges.json"))
+ require.NoError(t, err)
+ ids, err := parsePKRangeIDs(raw)
+ require.NoError(t, err)
+ assert.Equal(t, []string{"0"}, ids, "test container has a single partition with id=0")
+}
+
+func TestParsePlan_RejectsInvalidJSON(t *testing.T) {
+ _, err := parsePlan([]byte(`not json`))
+ require.Error(t, err)
+}
+
+func TestParsePKRangeIDs_RejectsInvalidJSON(t *testing.T) {
+ _, err := parsePKRangeIDs([]byte(`not json`))
+ require.Error(t, err)
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/sentinel.go b/backend/plugin/db/cosmosdb/queryengine/gonative/sentinel.go
new file mode 100644
index 0000000000..ff2cb2d33b
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/sentinel.go
@@ -0,0 +1,9 @@
+package gonative
+
+import "errors"
+
+// ErrUnsupportedPlanFeature reports that the gateway's query plan needs a
+// feature this engine does not implement, so the caller should surface the
+// gateway's own error rather than the engine's. Upstream's queryengine package
+// defines no such sentinel, so it lives with the engine that returns it.
+var ErrUnsupportedPlanFeature = errors.New("cosmosdb/queryengine: unsupported plan feature")
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_partitions/range_0.json
new file mode 100644
index 0000000000..ed96a756af
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_partitions/range_0.json
@@ -0,0 +1,24 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "country": "AD"
+ },
+ {
+ "country": "AE"
+ },
+ {
+ "country": "DE"
+ },
+ {
+ "country": "GB"
+ },
+ {
+ "country": "JP"
+ },
+ {
+ "country": "US"
+ }
+ ],
+ "_count": 6
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_plan.json
new file mode 100644
index 0000000000..b9d1bcf81e
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_1_plan.json
@@ -0,0 +1,28 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "Unordered",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_partitions/range_0.json
new file mode 100644
index 0000000000..80f87b9123
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_partitions/range_0.json
@@ -0,0 +1,12 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ "1",
+ "2",
+ "3",
+ "4",
+ "5",
+ "7"
+ ],
+ "_count": 6
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_plan.json
new file mode 100644
index 0000000000..205451f7ab
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_10_2_plan.json
@@ -0,0 +1,28 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "Unordered",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_partitions/range_0.json
new file mode 100644
index 0000000000..056b81da9c
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_partitions/range_0.json
@@ -0,0 +1,11 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ [
+ {
+ "item": 18
+ }
+ ]
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_plan.json
new file mode 100644
index 0000000000..a881047c1d
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_11_2_plan.json
@@ -0,0 +1,30 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [
+ "Count"
+ ],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT VALUE [{\"item\": COUNT(1)}]\nFROM c",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_partitions/range_0.json
new file mode 100644
index 0000000000..d374b1feba
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_partitions/range_0.json
@@ -0,0 +1,266 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Al Fujayrah"
+ }
+ ],
+ "payload": {
+ "id": "1",
+ "code": "292878",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.11641",
+ "longitude": "56.34141",
+ "name": "Al Fujayrah",
+ "population": "62415",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsBAAAAAAAAAA==/",
+ "_etag": "\"350115c8-0000-0100-0000-69df41520000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238930
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Andorra la Vella"
+ }
+ ],
+ "payload": {
+ "code": "290030",
+ "country": "AD",
+ "countryRegion": "4",
+ "date": "6/12/18",
+ "id": "6",
+ "latitude": 42.5078,
+ "longitude": 1.5211,
+ "name": "Andorra la Vella",
+ "population": 22256,
+ "region": "Europe/Andorra",
+ "regionCode": 16,
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsGAAAAAAAAAA==/",
+ "_etag": "\"e4008ff4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Andorra la Vella"
+ }
+ ],
+ "payload": {
+ "id": "3",
+ "code": "100001",
+ "country": "AD",
+ "countryRegion": "7",
+ "date": "1/18/12",
+ "latitude": "42.5",
+ "longitude": "1.5",
+ "name": "Andorra la Vella",
+ "population": "22256",
+ "region": "Europe/Andorra",
+ "regionCode": "7",
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsQAAAAAAAAAA==/",
+ "_etag": "\"3501dcc8-0000-0100-0000-69df41540000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238932
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Ar Ruways"
+ }
+ ],
+ "payload": {
+ "code": "291630",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "4/22/16",
+ "id": "4",
+ "latitude": 25.3573,
+ "longitude": 55.4033,
+ "name": "Ar Ruways",
+ "population": 15000,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsEAAAAAAAAAA==/",
+ "_etag": "\"e4007cf4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Berlin"
+ }
+ ],
+ "payload": {
+ "code": "600100",
+ "country": "DE",
+ "countryRegion": "2",
+ "date": "3/21/20",
+ "id": "15",
+ "latitude": 52.52,
+ "longitude": 13.405,
+ "name": "Berlin",
+ "population": 3645000,
+ "region": "Europe/Berlin",
+ "regionCode": 4,
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsPAAAAAAAAAA==/",
+ "_etag": "\"e400e3f4-0000-0100-0000-69de02d50000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157397
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Chicago"
+ }
+ ],
+ "payload": {
+ "code": "300102",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "10/01/22",
+ "id": "10",
+ "latitude": 41.8781,
+ "longitude": -87.6298,
+ "name": "Chicago",
+ "population": 2693976,
+ "region": "America/Chicago",
+ "regionCode": 2,
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsKAAAAAAAAAA==/",
+ "_etag": "\"e400b2f4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Dubai"
+ }
+ ],
+ "payload": {
+ "id": "2",
+ "code": "292879",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.27",
+ "longitude": "55.31",
+ "name": "Dubai",
+ "population": "2956587",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsCAAAAAAAAAA==/",
+ "_etag": "\"350184c8-0000-0100-0000-69df41530000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238931
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Dubai"
+ }
+ ],
+ "payload": {
+ "code": "292968",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "3/10/15",
+ "id": "3",
+ "latitude": 25.2048,
+ "longitude": 55.2708,
+ "name": "Dubai",
+ "population": 3400800,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsDAAAAAAAAAA==/",
+ "_etag": "\"e40075f4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "London"
+ }
+ ],
+ "payload": {
+ "code": "400100",
+ "country": "GB",
+ "countryRegion": "2",
+ "date": "11/11/19",
+ "id": "11",
+ "latitude": 51.5074,
+ "longitude": -0.1278,
+ "name": "London",
+ "population": 8982000,
+ "region": "Europe/London",
+ "regionCode": 3,
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsLAAAAAAAAAA==/",
+ "_etag": "\"e400c0f4-0000-0100-0000-69de02d40000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157396
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "London"
+ }
+ ],
+ "payload": {
+ "id": "5",
+ "code": "100003",
+ "country": "GB",
+ "countryRegion": "1",
+ "date": "1/18/12",
+ "latitude": "51.50",
+ "longitude": "-0.12",
+ "name": "London",
+ "population": "8982000",
+ "region": "Europe/London",
+ "regionCode": "LND",
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsSAAAAAAAAAA==/",
+ "_etag": "\"3501edc9-0000-0100-0000-69df41570000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238935
+ }
+ }
+ ],
+ "_count": 10
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_plan.json
new file mode 100644
index 0000000000..9cbebe26e6
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_1_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": 0,
+ "limit": 10,
+ "orderBy": [
+ "Ascending"
+ ],
+ "orderByExpressions": [
+ "c.name"
+ ],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT c._rid, [{\"item\": c.name}] AS orderByItems, c AS payload\nFROM c\nWHERE ({documentdb-formattableorderbyquery-filter})\nORDER BY c.name\nOFFSET 0 LIMIT 10",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_partitions/range_0.json
new file mode 100644
index 0000000000..5a8d4cdb9e
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_partitions/range_0.json
@@ -0,0 +1,474 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Al Fujayrah"
+ }
+ ],
+ "payload": {
+ "id": "1",
+ "code": "292878",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.11641",
+ "longitude": "56.34141",
+ "name": "Al Fujayrah",
+ "population": "62415",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsBAAAAAAAAAA==/",
+ "_etag": "\"350115c8-0000-0100-0000-69df41520000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238930
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Andorra la Vella"
+ }
+ ],
+ "payload": {
+ "code": "290030",
+ "country": "AD",
+ "countryRegion": "4",
+ "date": "6/12/18",
+ "id": "6",
+ "latitude": 42.5078,
+ "longitude": 1.5211,
+ "name": "Andorra la Vella",
+ "population": 22256,
+ "region": "Europe/Andorra",
+ "regionCode": 16,
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsGAAAAAAAAAA==/",
+ "_etag": "\"e4008ff4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Andorra la Vella"
+ }
+ ],
+ "payload": {
+ "id": "3",
+ "code": "100001",
+ "country": "AD",
+ "countryRegion": "7",
+ "date": "1/18/12",
+ "latitude": "42.5",
+ "longitude": "1.5",
+ "name": "Andorra la Vella",
+ "population": "22256",
+ "region": "Europe/Andorra",
+ "regionCode": "7",
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsQAAAAAAAAAA==/",
+ "_etag": "\"3501dcc8-0000-0100-0000-69df41540000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238932
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Ar Ruways"
+ }
+ ],
+ "payload": {
+ "code": "291630",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "4/22/16",
+ "id": "4",
+ "latitude": 25.3573,
+ "longitude": 55.4033,
+ "name": "Ar Ruways",
+ "population": 15000,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsEAAAAAAAAAA==/",
+ "_etag": "\"e4007cf4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Berlin"
+ }
+ ],
+ "payload": {
+ "code": "600100",
+ "country": "DE",
+ "countryRegion": "2",
+ "date": "3/21/20",
+ "id": "15",
+ "latitude": 52.52,
+ "longitude": 13.405,
+ "name": "Berlin",
+ "population": 3645000,
+ "region": "Europe/Berlin",
+ "regionCode": 4,
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsPAAAAAAAAAA==/",
+ "_etag": "\"e400e3f4-0000-0100-0000-69de02d50000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157397
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Chicago"
+ }
+ ],
+ "payload": {
+ "code": "300102",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "10/01/22",
+ "id": "10",
+ "latitude": 41.8781,
+ "longitude": -87.6298,
+ "name": "Chicago",
+ "population": 2693976,
+ "region": "America/Chicago",
+ "regionCode": 2,
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsKAAAAAAAAAA==/",
+ "_etag": "\"e400b2f4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Dubai"
+ }
+ ],
+ "payload": {
+ "id": "2",
+ "code": "292879",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.27",
+ "longitude": "55.31",
+ "name": "Dubai",
+ "population": "2956587",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsCAAAAAAAAAA==/",
+ "_etag": "\"350184c8-0000-0100-0000-69df41530000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238931
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Dubai"
+ }
+ ],
+ "payload": {
+ "code": "292968",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "3/10/15",
+ "id": "3",
+ "latitude": 25.2048,
+ "longitude": 55.2708,
+ "name": "Dubai",
+ "population": 3400800,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsDAAAAAAAAAA==/",
+ "_etag": "\"e40075f4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "London"
+ }
+ ],
+ "payload": {
+ "code": "400100",
+ "country": "GB",
+ "countryRegion": "2",
+ "date": "11/11/19",
+ "id": "11",
+ "latitude": 51.5074,
+ "longitude": -0.1278,
+ "name": "London",
+ "population": 8982000,
+ "region": "Europe/London",
+ "regionCode": 3,
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsLAAAAAAAAAA==/",
+ "_etag": "\"e400c0f4-0000-0100-0000-69de02d40000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157396
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "London"
+ }
+ ],
+ "payload": {
+ "id": "5",
+ "code": "100003",
+ "country": "GB",
+ "countryRegion": "1",
+ "date": "1/18/12",
+ "latitude": "51.50",
+ "longitude": "-0.12",
+ "name": "London",
+ "population": "8982000",
+ "region": "Europe/London",
+ "regionCode": "LND",
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsSAAAAAAAAAA==/",
+ "_etag": "\"3501edc9-0000-0100-0000-69df41570000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238935
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsJAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Los Angeles"
+ }
+ ],
+ "payload": {
+ "code": "300101",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "9/15/21",
+ "id": "9",
+ "latitude": 34.0522,
+ "longitude": -118.2437,
+ "name": "Los Angeles",
+ "population": 3979576,
+ "region": "America/LosAngeles",
+ "regionCode": 1,
+ "_rid": "S9w7AIGecFsJAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsJAAAAAAAAAA==/",
+ "_etag": "\"e400abf4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsMAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Manchester"
+ }
+ ],
+ "payload": {
+ "code": "400101",
+ "country": "GB",
+ "countryRegion": "2",
+ "date": "12/25/20",
+ "id": "12",
+ "latitude": 53.4808,
+ "longitude": -2.2426,
+ "name": "Manchester",
+ "population": 553230,
+ "region": "Europe/London",
+ "regionCode": 3,
+ "_rid": "S9w7AIGecFsMAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsMAAAAAAAAAA==/",
+ "_etag": "\"e400c8f4-0000-0100-0000-69de02d40000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157396
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsIAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "New York"
+ }
+ ],
+ "payload": {
+ "code": "300100",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "8/05/20",
+ "id": "8",
+ "latitude": 40.7128,
+ "longitude": -74.006,
+ "name": "New York",
+ "population": 8336817,
+ "region": "America/NewYork",
+ "regionCode": 1,
+ "_rid": "S9w7AIGecFsIAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsIAAAAAAAAAA==/",
+ "_etag": "\"e400a3f4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsRAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "New York"
+ }
+ ],
+ "payload": {
+ "id": "4",
+ "code": "100002",
+ "country": "US",
+ "countryRegion": "5",
+ "date": "1/18/12",
+ "latitude": "40.71",
+ "longitude": "-74.00",
+ "name": "New York",
+ "population": "8419000",
+ "region": "America/New_York",
+ "regionCode": "NY",
+ "_rid": "S9w7AIGecFsRAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsRAAAAAAAAAA==/",
+ "_etag": "\"35014ec9-0000-0100-0000-69df41560000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238934
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsOAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Osaka"
+ }
+ ],
+ "payload": {
+ "code": "500101",
+ "country": "JP",
+ "countryRegion": "3",
+ "date": "2/14/22",
+ "id": "14",
+ "latitude": 34.6937,
+ "longitude": 135.5023,
+ "name": "Osaka",
+ "population": 2753000,
+ "region": "Asia/Tokyo",
+ "regionCode": 5,
+ "_rid": "S9w7AIGecFsOAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsOAAAAAAAAAA==/",
+ "_etag": "\"e400dcf4-0000-0100-0000-69de02d50000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157397
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsFAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Sharjah"
+ }
+ ],
+ "payload": {
+ "code": "292672",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "5/01/17",
+ "id": "5",
+ "latitude": 25.3375,
+ "longitude": 55.5127,
+ "name": "Sharjah",
+ "population": 1274749,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsFAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsFAAAAAAAAAA==/",
+ "_etag": "\"e40080f4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsNAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Tokyo"
+ }
+ ],
+ "payload": {
+ "code": "500100",
+ "country": "JP",
+ "countryRegion": "3",
+ "date": "1/30/21",
+ "id": "13",
+ "latitude": 35.6762,
+ "longitude": 139.6503,
+ "name": "Tokyo",
+ "population": 13960000,
+ "region": "Asia/Tokyo",
+ "regionCode": 5,
+ "_rid": "S9w7AIGecFsNAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsNAAAAAAAAAA==/",
+ "_etag": "\"e400d0f4-0000-0100-0000-69de02d50000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157397
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsHAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "Umm Al Qaywayn"
+ }
+ ],
+ "payload": {
+ "code": "290031",
+ "country": "AD",
+ "countryRegion": "4",
+ "date": "7/20/19",
+ "id": "7",
+ "latitude": 42.5,
+ "longitude": 1.5167,
+ "name": "Umm Al Qaywayn",
+ "population": 62000,
+ "region": "Europe/Andorra",
+ "regionCode": 16,
+ "_rid": "S9w7AIGecFsHAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsHAAAAAAAAAA==/",
+ "_etag": "\"e4009af4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ }
+ }
+ ],
+ "_count": 18
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_plan.json
new file mode 100644
index 0000000000..837927a2c1
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_12_2_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": 10,
+ "limit": 10,
+ "orderBy": [
+ "Ascending"
+ ],
+ "orderByExpressions": [
+ "c.name"
+ ],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT c._rid, [{\"item\": c.name}] AS orderByItems, c AS payload\nFROM c\nWHERE ({documentdb-formattableorderbyquery-filter})\nORDER BY c.name\nOFFSET 0 LIMIT 20",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_partitions/range_0.json
new file mode 100644
index 0000000000..554c3d063a
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_partitions/range_0.json
@@ -0,0 +1,186 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "id": "1",
+ "code": "292878",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.11641",
+ "longitude": "56.34141",
+ "name": "Al Fujayrah",
+ "population": "62415",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsBAAAAAAAAAA==/",
+ "_etag": "\"350115c8-0000-0100-0000-69df41520000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238930
+ },
+ {
+ "id": "2",
+ "code": "292879",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "1/18/12",
+ "latitude": "25.27",
+ "longitude": "55.31",
+ "name": "Dubai",
+ "population": "2956587",
+ "region": "Asia/Dubai",
+ "regionCode": "15",
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsCAAAAAAAAAA==/",
+ "_etag": "\"350184c8-0000-0100-0000-69df41530000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776238931
+ },
+ {
+ "code": "292968",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "3/10/15",
+ "id": "3",
+ "latitude": 25.2048,
+ "longitude": 55.2708,
+ "name": "Dubai",
+ "population": 3400800,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsDAAAAAAAAAA==/",
+ "_etag": "\"e40075f4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ },
+ {
+ "code": "291630",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "4/22/16",
+ "id": "4",
+ "latitude": 25.3573,
+ "longitude": 55.4033,
+ "name": "Ar Ruways",
+ "population": 15000,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsEAAAAAAAAAA==/",
+ "_etag": "\"e4007cf4-0000-0100-0000-69de02d10000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157393
+ },
+ {
+ "code": "292672",
+ "country": "AE",
+ "countryRegion": "4",
+ "date": "5/01/17",
+ "id": "5",
+ "latitude": 25.3375,
+ "longitude": 55.5127,
+ "name": "Sharjah",
+ "population": 1274749,
+ "region": "Asia/Dubai",
+ "regionCode": 15,
+ "_rid": "S9w7AIGecFsFAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsFAAAAAAAAAA==/",
+ "_etag": "\"e40080f4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ },
+ {
+ "code": "290030",
+ "country": "AD",
+ "countryRegion": "4",
+ "date": "6/12/18",
+ "id": "6",
+ "latitude": 42.5078,
+ "longitude": 1.5211,
+ "name": "Andorra la Vella",
+ "population": 22256,
+ "region": "Europe/Andorra",
+ "regionCode": 16,
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsGAAAAAAAAAA==/",
+ "_etag": "\"e4008ff4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ },
+ {
+ "code": "290031",
+ "country": "AD",
+ "countryRegion": "4",
+ "date": "7/20/19",
+ "id": "7",
+ "latitude": 42.5,
+ "longitude": 1.5167,
+ "name": "Umm Al Qaywayn",
+ "population": 62000,
+ "region": "Europe/Andorra",
+ "regionCode": 16,
+ "_rid": "S9w7AIGecFsHAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsHAAAAAAAAAA==/",
+ "_etag": "\"e4009af4-0000-0100-0000-69de02d20000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157394
+ },
+ {
+ "code": "300100",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "8/05/20",
+ "id": "8",
+ "latitude": 40.7128,
+ "longitude": -74.006,
+ "name": "New York",
+ "population": 8336817,
+ "region": "America/NewYork",
+ "regionCode": 1,
+ "_rid": "S9w7AIGecFsIAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsIAAAAAAAAAA==/",
+ "_etag": "\"e400a3f4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ },
+ {
+ "code": "300101",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "9/15/21",
+ "id": "9",
+ "latitude": 34.0522,
+ "longitude": -118.2437,
+ "name": "Los Angeles",
+ "population": 3979576,
+ "region": "America/LosAngeles",
+ "regionCode": 1,
+ "_rid": "S9w7AIGecFsJAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsJAAAAAAAAAA==/",
+ "_etag": "\"e400abf4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ },
+ {
+ "code": "300102",
+ "country": "US",
+ "countryRegion": "1",
+ "date": "10/01/22",
+ "id": "10",
+ "latitude": 41.8781,
+ "longitude": -87.6298,
+ "name": "Chicago",
+ "population": 2693976,
+ "region": "America/Chicago",
+ "regionCode": 2,
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/docs/S9w7AIGecFsKAAAAAAAAAA==/",
+ "_etag": "\"e400b2f4-0000-0100-0000-69de02d30000\"",
+ "_attachments": "attachments/",
+ "_ts": 1776157395
+ }
+ ],
+ "_count": 10
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_plan.json
new file mode 100644
index 0000000000..123129091c
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_1_3_plan.json
@@ -0,0 +1,28 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": 10,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_partitions/range_0.json
new file mode 100644
index 0000000000..d8cba4d8e7
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_partitions/range_0.json
@@ -0,0 +1,222 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 15000
+ }
+ ],
+ "payload": {
+ "name": "Ar Ruways",
+ "population": 15000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 22256
+ }
+ ],
+ "payload": {
+ "name": "Andorra la Vella",
+ "population": 22256
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsHAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 62000
+ }
+ ],
+ "payload": {
+ "name": "Umm Al Qaywayn",
+ "population": 62000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsMAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 553230
+ }
+ ],
+ "payload": {
+ "name": "Manchester",
+ "population": 553230
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsFAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 1274749
+ }
+ ],
+ "payload": {
+ "name": "Sharjah",
+ "population": 1274749
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 2693976
+ }
+ ],
+ "payload": {
+ "name": "Chicago",
+ "population": 2693976
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsOAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 2753000
+ }
+ ],
+ "payload": {
+ "name": "Osaka",
+ "population": 2753000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3400800
+ }
+ ],
+ "payload": {
+ "name": "Dubai",
+ "population": 3400800
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3645000
+ }
+ ],
+ "payload": {
+ "name": "Berlin",
+ "population": 3645000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsJAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3979576
+ }
+ ],
+ "payload": {
+ "name": "Los Angeles",
+ "population": 3979576
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsIAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 8336817
+ }
+ ],
+ "payload": {
+ "name": "New York",
+ "population": 8336817
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 8982000
+ }
+ ],
+ "payload": {
+ "name": "London",
+ "population": 8982000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsNAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 13960000
+ }
+ ],
+ "payload": {
+ "name": "Tokyo",
+ "population": 13960000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "22256"
+ }
+ ],
+ "payload": {
+ "name": "Andorra la Vella",
+ "population": "22256"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "2956587"
+ }
+ ],
+ "payload": {
+ "name": "Dubai",
+ "population": "2956587"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "62415"
+ }
+ ],
+ "payload": {
+ "name": "Al Fujayrah",
+ "population": "62415"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsRAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "8419000"
+ }
+ ],
+ "payload": {
+ "name": "New York",
+ "population": "8419000"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "8982000"
+ }
+ ],
+ "payload": {
+ "name": "London",
+ "population": "8982000"
+ }
+ }
+ ],
+ "_count": 18
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_plan.json
new file mode 100644
index 0000000000..1e645f96a5
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_1_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [
+ "Ascending"
+ ],
+ "orderByExpressions": [
+ "c.population"
+ ],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT c._rid, [{\"item\": c.population}] AS orderByItems, {\"name\": c.name, \"population\": c.population} AS payload\nFROM c\nWHERE ({documentdb-formattableorderbyquery-filter})\nORDER BY c.population ASC",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_partitions/range_0.json
new file mode 100644
index 0000000000..25a0d46216
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_partitions/range_0.json
@@ -0,0 +1,222 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "_rid": "S9w7AIGecFsSAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "8982000"
+ }
+ ],
+ "payload": {
+ "name": "London",
+ "population": "8982000"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsRAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "8419000"
+ }
+ ],
+ "payload": {
+ "name": "New York",
+ "population": "8419000"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsBAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "62415"
+ }
+ ],
+ "payload": {
+ "name": "Al Fujayrah",
+ "population": "62415"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "2956587"
+ }
+ ],
+ "payload": {
+ "name": "Dubai",
+ "population": "2956587"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsQAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": "22256"
+ }
+ ],
+ "payload": {
+ "name": "Andorra la Vella",
+ "population": "22256"
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsNAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 13960000
+ }
+ ],
+ "payload": {
+ "name": "Tokyo",
+ "population": 13960000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsLAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 8982000
+ }
+ ],
+ "payload": {
+ "name": "London",
+ "population": 8982000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsIAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 8336817
+ }
+ ],
+ "payload": {
+ "name": "New York",
+ "population": 8336817
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsJAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3979576
+ }
+ ],
+ "payload": {
+ "name": "Los Angeles",
+ "population": 3979576
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsPAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3645000
+ }
+ ],
+ "payload": {
+ "name": "Berlin",
+ "population": 3645000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsDAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 3400800
+ }
+ ],
+ "payload": {
+ "name": "Dubai",
+ "population": 3400800
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsOAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 2753000
+ }
+ ],
+ "payload": {
+ "name": "Osaka",
+ "population": 2753000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsKAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 2693976
+ }
+ ],
+ "payload": {
+ "name": "Chicago",
+ "population": 2693976
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsFAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 1274749
+ }
+ ],
+ "payload": {
+ "name": "Sharjah",
+ "population": 1274749
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsMAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 553230
+ }
+ ],
+ "payload": {
+ "name": "Manchester",
+ "population": 553230
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsHAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 62000
+ }
+ ],
+ "payload": {
+ "name": "Umm Al Qaywayn",
+ "population": 62000
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsGAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 22256
+ }
+ ],
+ "payload": {
+ "name": "Andorra la Vella",
+ "population": 22256
+ }
+ },
+ {
+ "_rid": "S9w7AIGecFsEAAAAAAAAAA==",
+ "orderByItems": [
+ {
+ "item": 15000
+ }
+ ],
+ "payload": {
+ "name": "Ar Ruways",
+ "population": 15000
+ }
+ }
+ ],
+ "_count": 18
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_plan.json
new file mode 100644
index 0000000000..483a584b2d
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_4_2_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [
+ "Descending"
+ ],
+ "orderByExpressions": [
+ "c.population"
+ ],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT c._rid, [{\"item\": c.population}] AS orderByItems, {\"name\": c.name, \"population\": c.population} AS payload\nFROM c\nWHERE ({documentdb-formattableorderbyquery-filter})\nORDER BY c.population DESC",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_partitions/range_0.json
new file mode 100644
index 0000000000..27ab9495a7
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_partitions/range_0.json
@@ -0,0 +1,13 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "payload": {
+ "totalRecords": {
+ "item": 18
+ }
+ }
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_plan.json
new file mode 100644
index 0000000000..31d45659ff
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [
+ "totalRecords"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "totalRecords": "Count"
+ },
+ "rewrittenQuery": "SELECT {\"totalRecords\": {\"item\": COUNT(1)}} AS payload\nFROM c",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_partitions/range_0.json
new file mode 100644
index 0000000000..056b81da9c
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_partitions/range_0.json
@@ -0,0 +1,11 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ [
+ {
+ "item": 18
+ }
+ ]
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_plan.json
new file mode 100644
index 0000000000..a881047c1d
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_1b_plan.json
@@ -0,0 +1,30 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [
+ "Count"
+ ],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT VALUE [{\"item\": COUNT(1)}]\nFROM c",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_partitions/range_0.json
new file mode 100644
index 0000000000..41abfa890d
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_partitions/range_0.json
@@ -0,0 +1,13 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "payload": {
+ "aeCount": {
+ "item": 5
+ }
+ }
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_plan.json
new file mode 100644
index 0000000000..ea0fb38a53
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [
+ "aeCount"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "aeCount": "Count"
+ },
+ "rewrittenQuery": "SELECT {\"aeCount\": {\"item\": COUNT(1)}} AS payload\nFROM c\nWHERE (c.country = \"AE\")",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "05C1E327DDF5CC08424600",
+ "max": "05C1E327DDF5CC08424600",
+ "isMinInclusive": true,
+ "isMaxInclusive": true
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_partitions/range_0.json
new file mode 100644
index 0000000000..939e4be96c
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_partitions/range_0.json
@@ -0,0 +1,11 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ [
+ {
+ "item": 5
+ }
+ ]
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_plan.json
new file mode 100644
index 0000000000..6a29cd98f2
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_2b_plan.json
@@ -0,0 +1,30 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [
+ "Count"
+ ],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT VALUE [{\"item\": COUNT(1)}]\nFROM c\nWHERE (c.country = \"AE\")",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "05C1E327DDF5CC08424600",
+ "max": "05C1E327DDF5CC08424600",
+ "isMinInclusive": true,
+ "isMaxInclusive": true
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_partitions/range_0.json
new file mode 100644
index 0000000000..b42744e3cd
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_partitions/range_0.json
@@ -0,0 +1,13 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "payload": {
+ "totalPop": {
+ "item": 20442258
+ }
+ }
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_plan.json
new file mode 100644
index 0000000000..15f554584a
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [
+ "totalPop"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "totalPop": "Sum"
+ },
+ "rewrittenQuery": "SELECT {\"totalPop\": {\"item\": SUM(StringToNumber(c.population))}} AS payload\nFROM c",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_partitions/range_0.json
new file mode 100644
index 0000000000..f2165c6ebf
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_partitions/range_0.json
@@ -0,0 +1,11 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ [
+ {
+ "item": 20442258
+ }
+ ]
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_plan.json
new file mode 100644
index 0000000000..71b7e2cd85
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_3b_plan.json
@@ -0,0 +1,30 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [
+ "Sum"
+ ],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT VALUE [{\"item\": SUM(StringToNumber(c.population))}]\nFROM c",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_partitions/range_0.json
new file mode 100644
index 0000000000..395df1810e
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_partitions/range_0.json
@@ -0,0 +1,16 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "payload": {
+ "avgPop": {
+ "item": {
+ "sum": 20442258,
+ "count": 5
+ }
+ }
+ }
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_plan.json
new file mode 100644
index 0000000000..bc105c92a2
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4_plan.json
@@ -0,0 +1,32 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [
+ "avgPop"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "avgPop": "Average"
+ },
+ "rewrittenQuery": "SELECT {\"avgPop\": {\"item\": {\"sum\": SUM(StringToNumber(c.population)), \"count\": COUNT(StringToNumber(c.population))}}} AS payload\nFROM c",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_partitions/range_0.json
new file mode 100644
index 0000000000..f538f67210
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_partitions/range_0.json
@@ -0,0 +1,14 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ [
+ {
+ "item": {
+ "sum": 20442258,
+ "count": 5
+ }
+ }
+ ]
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_plan.json
new file mode 100644
index 0000000000..4788b90097
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_4b_plan.json
@@ -0,0 +1,30 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [],
+ "aggregates": [
+ "Average"
+ ],
+ "groupByAliasToAggregateType": {},
+ "rewrittenQuery": "SELECT VALUE [{\"item\": {\"sum\": SUM(StringToNumber(c.population)), \"count\": COUNT(StringToNumber(c.population))}}]\nFROM c",
+ "hasSelectValue": true,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_partitions/range_0.json
new file mode 100644
index 0000000000..c4963db8b0
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_partitions/range_0.json
@@ -0,0 +1,24 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "payload": {
+ "minPop": {
+ "item": 22256,
+ "item2": {
+ "min": 22256,
+ "count": 5
+ }
+ },
+ "maxPop": {
+ "item": 8982000,
+ "item2": {
+ "max": 8982000,
+ "count": 5
+ }
+ }
+ }
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_plan.json
new file mode 100644
index 0000000000..3e695987a8
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_5_5_plan.json
@@ -0,0 +1,34 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [],
+ "groupByAliases": [
+ "minPop",
+ "maxPop"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "maxPop": "Max",
+ "minPop": "Min"
+ },
+ "rewrittenQuery": "SELECT {\"minPop\": {\"item\": MIN(StringToNumber(c.population)), \"item2\": {\"min\": MIN(StringToNumber(c.population)), \"count\": COUNT(StringToNumber(c.population))}}, \"maxPop\": {\"item\": MAX(StringToNumber(c.population)), \"item2\": {\"max\": MAX(StringToNumber(c.population)), \"count\": COUNT(StringToNumber(c.population))}}} AS payload\nFROM c\nWHERE (StringToNumber(c.population) > 0)",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_partitions/range_0.json
new file mode 100644
index 0000000000..fe07d969f4
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_partitions/range_0.json
@@ -0,0 +1,84 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "groupByItems": [
+ {
+ "item": "AD"
+ }
+ ],
+ "payload": {
+ "country": "AD",
+ "cityCount": {
+ "item": 3
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "AE"
+ }
+ ],
+ "payload": {
+ "country": "AE",
+ "cityCount": {
+ "item": 5
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "DE"
+ }
+ ],
+ "payload": {
+ "country": "DE",
+ "cityCount": {
+ "item": 1
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "GB"
+ }
+ ],
+ "payload": {
+ "country": "GB",
+ "cityCount": {
+ "item": 3
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "JP"
+ }
+ ],
+ "payload": {
+ "country": "JP",
+ "cityCount": {
+ "item": 2
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "US"
+ }
+ ],
+ "payload": {
+ "country": "US",
+ "cityCount": {
+ "item": 4
+ }
+ }
+ }
+ ],
+ "_count": 6
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_plan.json
new file mode 100644
index 0000000000..f27d53cca2
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_1_plan.json
@@ -0,0 +1,36 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [
+ "c.country"
+ ],
+ "groupByAliases": [
+ "country",
+ "cityCount"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "cityCount": "Count",
+ "country": null
+ },
+ "rewrittenQuery": "SELECT [{\"item\": c.country}] AS groupByItems, {\"country\": c.country, \"cityCount\": {\"item\": COUNT(1)}} AS payload\nFROM c\nGROUP BY c.country",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_partitions/range_0.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_partitions/range_0.json
new file mode 100644
index 0000000000..ec6d8074ec
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_partitions/range_0.json
@@ -0,0 +1,102 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "Documents": [
+ {
+ "groupByItems": [
+ {
+ "item": "4"
+ }
+ ],
+ "payload": {
+ "countryRegion": "4",
+ "count": {
+ "item": 7
+ },
+ "totalPop": {
+ "item": 3019002
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "1"
+ }
+ ],
+ "payload": {
+ "countryRegion": "1",
+ "count": {
+ "item": 4
+ },
+ "totalPop": {
+ "item": 8982000
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "2"
+ }
+ ],
+ "payload": {
+ "countryRegion": "2",
+ "count": {
+ "item": 3
+ },
+ "totalPop": {
+ "item": 0
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "3"
+ }
+ ],
+ "payload": {
+ "countryRegion": "3",
+ "count": {
+ "item": 2
+ },
+ "totalPop": {
+ "item": 0
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "7"
+ }
+ ],
+ "payload": {
+ "countryRegion": "7",
+ "count": {
+ "item": 1
+ },
+ "totalPop": {
+ "item": 22256
+ }
+ }
+ },
+ {
+ "groupByItems": [
+ {
+ "item": "5"
+ }
+ ],
+ "payload": {
+ "countryRegion": "5",
+ "count": {
+ "item": 1
+ },
+ "totalPop": {
+ "item": 8419000
+ }
+ }
+ }
+ ],
+ "_count": 6
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_pkranges.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_pkranges.json
new file mode 100644
index 0000000000..c15c6ef797
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_pkranges.json
@@ -0,0 +1,21 @@
+{
+ "_rid": "S9w7AIGecFs=",
+ "PartitionKeyRanges": [
+ {
+ "_rid": "S9w7AIGecFsCAAAAAAAAUA==",
+ "id": "0",
+ "_etag": "\"0000d642-0000-0100-0000-69de026a0000\"",
+ "minInclusive": "",
+ "maxExclusive": "FF",
+ "ridPrefix": 0,
+ "_self": "dbs/S9w7AA==/colls/S9w7AIGecFs=/pkranges/S9w7AIGecFsCAAAAAAAAUA==/",
+ "throughputFraction": 1,
+ "status": "online",
+ "parents": [],
+ "ownedArchivalPKRangeIds": [],
+ "_ts": 1776157290,
+ "lsn": 15
+ }
+ ],
+ "_count": 1
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_plan.json b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_plan.json
new file mode 100644
index 0000000000..93a31ca086
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/testdata/query_6_2_plan.json
@@ -0,0 +1,38 @@
+{
+ "partitionedQueryExecutionInfoVersion": 2,
+ "queryInfo": {
+ "distinctType": "None",
+ "top": null,
+ "offset": null,
+ "limit": null,
+ "orderBy": [],
+ "orderByExpressions": [],
+ "groupByExpressions": [
+ "c.countryRegion"
+ ],
+ "groupByAliases": [
+ "countryRegion",
+ "count",
+ "totalPop"
+ ],
+ "aggregates": [],
+ "groupByAliasToAggregateType": {
+ "count": "Count",
+ "countryRegion": null,
+ "totalPop": "Sum"
+ },
+ "rewrittenQuery": "SELECT [{\"item\": c.countryRegion}] AS groupByItems, {\"countryRegion\": c.countryRegion, \"count\": {\"item\": COUNT(1)}, \"totalPop\": {\"item\": SUM(StringToNumber(c.population))}} AS payload\nFROM c\nGROUP BY c.countryRegion",
+ "hasSelectValue": false,
+ "dCountInfo": null,
+ "hasNonStreamingOrderBy": false
+ },
+ "queryRanges": [
+ {
+ "min": "",
+ "max": "FF",
+ "isMinInclusive": true,
+ "isMaxInclusive": false
+ }
+ ],
+ "hybridSearchQueryInfo": null
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake.go b/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake.go
new file mode 100644
index 0000000000..c7dcf44eca
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake.go
@@ -0,0 +1,163 @@
+package gonative
+
+import (
+ "encoding/json"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/pkg/errors"
+)
+
+// topPartitionResponse decodes a "no rewrite" per-partition response — the
+// Documents array carries raw user-shaped rows directly (no orderByItems /
+// payload wrapping). Used for standalone TOP, where the gateway runs the
+// original query per partition and returns whatever matches.
+type topPartitionResponse struct {
+ Documents []json.RawMessage `json:"Documents"`
+}
+
+func parseTopPartitionResponse(raw []byte) (*topPartitionResponse, error) {
+ var r topPartitionResponse
+ if err := json.Unmarshal(raw, &r); err != nil {
+ return nil, errors.Wrap(err, "gonative: parse top partition response")
+ }
+ return &r, nil
+}
+
+// topPipeline implements queryengine.QueryPipeline for standalone TOP queries
+// — plans with `top: N` and none of ORDER BY, DISTINCT, aggregates, or GROUP BY.
+// Rows from each partition are concatenated and the first N are emitted.
+//
+// TOP composed with ORDER BY/DISTINCT/aggregates/GROUP BY is out of Stage 4's
+// scope; the dispatch rejects those plans with ErrUnsupportedPlanFeature. The
+// gateway itself already caps each partition's response size when TOP is
+// requested, so concatenation + cap at N is always bounded.
+type topPipeline struct {
+ query string
+ n int
+ pkRangeIDs []string
+ partitionsDone map[string]bool
+ issued bool
+ closed bool
+
+ // pending items ready to flush to the caller, already capped at N total.
+ pending [][]byte
+ // emitted tracks how many rows have been drained via Run(); we stop
+ // accepting/emitting once we hit N.
+ emitted int
+ // accumulated counts pending + emitted. Keeps ProvideData from growing
+ // `pending` past N so downstream flushes never over-emit.
+ accumulated int
+}
+
+// newTopPipeline builds a pipeline for a plan whose only operator is TOP.
+// Returns ErrUnsupportedPlanFeature when the plan carries any other operator
+// that would require composition (ORDER BY, DISTINCT, aggregate, GROUP BY).
+func newTopPipeline(plan *planDoc, pkRangeIDs []string) (*topPipeline, error) {
+ if plan.QueryInfo.Top == nil {
+ return nil, errors.New("gonative: topPipeline requires a non-nil Top")
+ }
+ // Composition with other operators is a follow-up. Stage 4 only handles
+ // standalone TOP — the dispatcher routes composed plans elsewhere or
+ // rejects them.
+ if len(plan.QueryInfo.OrderBy) > 0 ||
+ plan.QueryInfo.DistinctType == "Unordered" || plan.QueryInfo.DistinctType == "Ordered" ||
+ len(plan.QueryInfo.Aggregates) > 0 ||
+ len(plan.QueryInfo.GroupByAliasToAggregateType) > 0 ||
+ len(plan.QueryInfo.GroupByExpressions) > 0 {
+ return nil, ErrUnsupportedPlanFeature
+ }
+ n := *plan.QueryInfo.Top
+ if n < 0 {
+ return nil, errors.Errorf("gonative: negative TOP N=%d", n)
+ }
+ return &topPipeline{
+ n: n,
+ pkRangeIDs: append([]string(nil), pkRangeIDs...),
+ partitionsDone: make(map[string]bool, len(pkRangeIDs)),
+ }, nil
+}
+
+// Query implements queryengine.QueryPipeline.
+func (p *topPipeline) Query() string { return p.query }
+
+// IsComplete returns true once either (a) we've emitted N rows or (b) every
+// partition has delivered and all pending rows have been flushed.
+func (p *topPipeline) IsComplete() bool {
+ if p.emitted >= p.n {
+ return true
+ }
+ if !p.issued {
+ return false
+ }
+ if len(p.pending) > 0 {
+ return false
+ }
+ for _, id := range p.pkRangeIDs {
+ if !p.partitionsDone[id] {
+ return false
+ }
+ }
+ return true
+}
+
+// Close implements queryengine.QueryPipeline.
+func (p *topPipeline) Close() { p.closed = true }
+
+// Run drives one pipeline turn.
+func (p *topPipeline) Run() (*queryengine.PipelineResult, error) {
+ if !p.issued {
+ // Edge case: TOP 0 — emit zero rows without firing any partition work.
+ if p.n == 0 {
+ p.issued = true
+ return &queryengine.PipelineResult{IsCompleted: true}, nil
+ }
+ reqs := make([]queryengine.QueryRequest, len(p.pkRangeIDs))
+ for i, id := range p.pkRangeIDs {
+ reqs[i] = queryengine.QueryRequest{
+ PartitionKeyRangeID: id,
+ Id: uint64(i),
+ Query: p.query,
+ IncludeParameters: true,
+ }
+ }
+ p.issued = true
+ return &queryengine.PipelineResult{Requests: reqs}, nil
+ }
+ if len(p.pending) > 0 {
+ batch := p.pending
+ p.pending = nil
+ p.emitted += len(batch)
+ return &queryengine.PipelineResult{Items: batch, IsCompleted: p.IsComplete()}, nil
+ }
+ return &queryengine.PipelineResult{IsCompleted: p.IsComplete()}, nil
+}
+
+// ProvideData consumes each partition's response and appends rows to `pending`
+// up to the overall TOP N limit. Anything beyond N is dropped.
+func (p *topPipeline) ProvideData(results []queryengine.QueryResult) error {
+ for _, r := range results {
+ if p.accumulated >= p.n {
+ // Already have enough rows; just mark remaining partitions done
+ // so IsComplete can trip.
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ continue
+ }
+ resp, err := parseTopPartitionResponse(r.Data)
+ if err != nil {
+ return err
+ }
+ for _, doc := range resp.Documents {
+ if p.accumulated >= p.n {
+ break
+ }
+ // Defensive copy — json.RawMessage aliases the input buffer.
+ p.pending = append(p.pending, append([]byte(nil), doc...))
+ p.accumulated++
+ }
+ if r.NextContinuation != "" {
+ return errors.New("gonative: top partition returned a continuation token (multi-page not supported in Stage 4)")
+ }
+ p.partitionsDone[r.PartitionKeyRangeID] = true
+ }
+ return nil
+}
diff --git a/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake_test.go b/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake_test.go
new file mode 100644
index 0000000000..8348ef3aca
--- /dev/null
+++ b/backend/plugin/db/cosmosdb/queryengine/gonative/topskiptake_test.go
@@ -0,0 +1,174 @@
+package gonative
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos/queryengine"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// mkTopBody wraps an arbitrary set of JSON documents into the partition
+// response shape the gateway produces for a standalone TOP query.
+func mkTopBody(t *testing.T, docs ...string) []byte {
+ t.Helper()
+ raw := make([]json.RawMessage, len(docs))
+ for i, d := range docs {
+ raw[i] = json.RawMessage(d)
+ }
+ body, err := json.Marshal(map[string]any{"Documents": raw})
+ require.NoError(t, err)
+ return body
+}
+
+func intPtr(v int) *int { return &v }
+
+func newTopPlan(n int) *planDoc {
+ p := &planDoc{}
+ p.QueryInfo.Top = intPtr(n)
+ return p
+}
+
+// driveTop feeds the given per-partition bodies through a TOP pipeline and
+// returns the emitted rows.
+func driveTop(t *testing.T, p *topPipeline, feed map[string][]byte) [][]byte {
+ t.Helper()
+ res, err := p.Run()
+ require.NoError(t, err)
+ if p.n == 0 {
+ // Edge case: TOP 0 completes immediately without any partition work.
+ assert.Empty(t, res.Requests)
+ return nil
+ }
+ require.NotEmpty(t, res.Requests, "expected fan-out for non-zero TOP")
+
+ results := make([]queryengine.QueryResult, 0, len(res.Requests))
+ for _, req := range res.Requests {
+ body, ok := feed[req.PartitionKeyRangeID]
+ require.True(t, ok, "missing fixture for %s", req.PartitionKeyRangeID)
+ results = append(results, queryengine.NewQueryResult(req.PartitionKeyRangeID, body, ""))
+ }
+ require.NoError(t, p.ProvideData(results))
+
+ var out [][]byte
+ for !p.IsComplete() {
+ r, err := p.Run()
+ require.NoError(t, err)
+ out = append(out, r.Items...)
+ }
+ return out
+}
+
+// ---------------------------------------------------------------- validation
+
+func TestNewTopPipeline_RequiresNonNilTop(t *testing.T) {
+ plan := &planDoc{}
+ _, err := newTopPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "non-nil Top")
+}
+
+func TestNewTopPipeline_RejectsNegativeN(t *testing.T) {
+ plan := newTopPlan(-1)
+ _, err := newTopPipeline(plan, []string{"0"})
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "negative TOP")
+}
+
+func TestNewTopPipeline_RejectsCompositionWithOrderBy(t *testing.T) {
+ plan := newTopPlan(5)
+ plan.QueryInfo.OrderBy = []string{"Ascending"}
+ _, err := newTopPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestNewTopPipeline_RejectsCompositionWithDistinct(t *testing.T) {
+ plan := newTopPlan(5)
+ plan.QueryInfo.DistinctType = "Unordered"
+ _, err := newTopPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+func TestNewTopPipeline_RejectsCompositionWithAggregate(t *testing.T) {
+ plan := newTopPlan(5)
+ plan.QueryInfo.Aggregates = []string{"Count"}
+ _, err := newTopPipeline(plan, []string{"0"})
+ require.ErrorIs(t, err, ErrUnsupportedPlanFeature)
+}
+
+// ---------------------------------------------------------------- semantics
+
+func TestTopPipeline_SinglePartition_EmitsFirstN(t *testing.T) {
+ plan := newTopPlan(3)
+ p, err := newTopPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ rows := driveTop(t, p, map[string][]byte{
+ "0": mkTopBody(t, `{"id":"a"}`, `{"id":"b"}`, `{"id":"c"}`, `{"id":"d"}`, `{"id":"e"}`),
+ })
+ assert.Equal(t, [][]byte{
+ []byte(`{"id":"a"}`), []byte(`{"id":"b"}`), []byte(`{"id":"c"}`),
+ }, rows)
+}
+
+func TestTopPipeline_FewerRowsThanN_EmitsAll(t *testing.T) {
+ plan := newTopPlan(10)
+ p, err := newTopPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ rows := driveTop(t, p, map[string][]byte{
+ "0": mkTopBody(t, `{"id":1}`, `{"id":2}`),
+ })
+ assert.Len(t, rows, 2)
+ assert.True(t, p.IsComplete())
+}
+
+func TestTopPipeline_MultiPartition_CapsAtN(t *testing.T) {
+ plan := newTopPlan(4)
+ p, err := newTopPipeline(plan, []string{"A", "B", "C"})
+ require.NoError(t, err)
+
+ rows := driveTop(t, p, map[string][]byte{
+ "A": mkTopBody(t, `{"p":"A","i":1}`, `{"p":"A","i":2}`, `{"p":"A","i":3}`),
+ "B": mkTopBody(t, `{"p":"B","i":1}`, `{"p":"B","i":2}`),
+ "C": mkTopBody(t, `{"p":"C","i":1}`, `{"p":"C","i":2}`),
+ })
+ assert.Len(t, rows, 4, "total emission must be capped at N=4 despite 7 inputs")
+}
+
+func TestTopPipeline_ZeroN_EmitsNothing(t *testing.T) {
+ plan := newTopPlan(0)
+ p, err := newTopPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+
+ rows := driveTop(t, p, nil)
+ assert.Empty(t, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestTopPipeline_EmptyPartitions(t *testing.T) {
+ plan := newTopPlan(5)
+ p, err := newTopPipeline(plan, []string{"A", "B"})
+ require.NoError(t, err)
+
+ rows := driveTop(t, p, map[string][]byte{
+ "A": mkTopBody(t),
+ "B": mkTopBody(t),
+ })
+ assert.Empty(t, rows)
+ assert.True(t, p.IsComplete())
+}
+
+func TestTopPipeline_RejectsContinuationToken(t *testing.T) {
+ plan := newTopPlan(5)
+ p, err := newTopPipeline(plan, []string{"0"})
+ require.NoError(t, err)
+ _, _ = p.Run()
+
+ err = p.ProvideData([]queryengine.QueryResult{
+ {PartitionKeyRangeID: "0", Data: mkTopBody(t, `{"x":1}`), NextContinuation: "cont"},
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "continuation token")
+}
diff --git a/go.mod b/go.mod
index d42bf76b23..24ade7d186 100644
--- a/go.mod
+++ b/go.mod
@@ -19,7 +19,7 @@ require (
connectrpc.com/validate v0.9.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.23.3
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.15.0
- github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.5.0
+ github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.6.0-beta.4
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.5.1
github.com/ClickHouse/clickhouse-go/v2 v2.50.0
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0
@@ -367,8 +367,6 @@ require (
)
replace (
- github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos => github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20260424030223-eac0db373bca
-
github.com/github/gh-ost => github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5
github.com/jackc/pgx/v5 => github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4
diff --git a/go.sum b/go.sum
index 058d4b9ef7..76a00cfa00 100644
--- a/go.sum
+++ b/go.sum
@@ -66,6 +66,8 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.15.0 h1:w3gDUJlJ0BfBsHH02DCB
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.15.0/go.mod h1:7scl8mG/IFLVjJ9ef/ltdND2T1z0CkgVSaQoUxLvmeU=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.4.0 h1:xFaZZ+IubdftrDHnGGwZ6QvQ3KHTtWl2MCK+GMt2vxs=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.4.0/go.mod h1:mCBhUhlMjLLJKr5aqw2TNS/VqJOie8MzWq3DAMJeKso=
+github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.6.0-beta.4 h1:/GpP9p+5+8qotgGFTXkDV4grC7aoZZfEvOeouSm9P3U=
+github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.6.0-beta.4/go.mod h1:OxZ2e11jJKw8M4k/0AI1ROgqGP8IdEkUbcWP9nFfbPc=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.13.0 h1:Terj1CMW4+NY5CpWjSRkqLoxh2DkYDdv/FH2z0897Uc=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.13.0/go.mod h1:7Ut2Qw9vcwpLW5Q4MBy43uSxXS7tK2yjs94OJ6j5thE=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.0 h1:LR0kAX9ykz8G4YgLCaRDVJ3+n43R8MneB5dTy2konZo=
@@ -251,8 +253,6 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJk=
github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
-github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20260424030223-eac0db373bca h1:bDgHzFb+pn23vrCdQ8+VYTu8dHKwV9KWgcAUvL6CT+s=
-github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20260424030223-eac0db373bca/go.mod h1:OwIkGuhFwOV7zicy7lmJl3MRqhkf2OmCSX8NMFEPm4A=
github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5 h1:mL04uKgVhYjXsZTvVTk1GbDz0qzzn8Jv+t24fT7/ydM=
github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5/go.mod h1:FwG3wgXv1mXHLEXEcgvgM7KIrefWrwYHI44+BLOVO2c=
github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735 h1:v8y6mXeHmYVsdM8cHul7kXSbOuDPL7yN1XSRMu1zo48=
diff --git a/scripts/cosmos-repro-go/README.md b/scripts/cosmos-repro-go/README.md
index 43d7eb5366..f51cf11af2 100644
--- a/scripts/cosmos-repro-go/README.md
+++ b/scripts/cosmos-repro-go/README.md
@@ -1,14 +1,14 @@
# cosmos-repro-go — BYT-9239 reproduction harness (Go)
Go twin of `scripts/cosmos-repro-dotnet`. Runs the full 40-query target
-inventory from BYT-9239 against a live Azure Cosmos account through
-Bytebase's forked `azcosmos` SDK (which embeds the pure-Go cross-partition
-query engine), and prints a markdown PASS/FAIL table in the same shape as
-the .NET harness. Pairing both harnesses on the same container is the
-regression guard for the BYT-9239 feature set: as long as the Go matrix
-matches the .NET matrix (39/40 PASS; the single FAIL is the multi-key
-ORDER BY query that needs a composite index — a server-side constraint
-that affects every SDK), the Go engine is at .NET parity for this inventory.
+inventory from BYT-9239 against a live Azure Cosmos account through the same
+cross-partition path the Bytebase driver uses — the gateway first, the pure-Go
+query engine when the gateway refuses — and prints a markdown PASS/FAIL table
+in the same shape as the .NET harness. Pairing both harnesses on the same
+container is the regression guard for the BYT-9239 feature set: as long as the
+Go matrix matches the .NET matrix (39/40 PASS; the single FAIL is the multi-key
+ORDER BY query that needs a composite index — a server-side constraint that
+affects every SDK), the Go engine is at .NET parity for this inventory.
## Prerequisites
@@ -30,11 +30,11 @@ cd scripts/cosmos-repro-go
go run . --verbose | tee out-go.md
```
-The harness uses `NewCrossPartitionQueryItemsPager`, which engages the
-pure-Go query engine automatically when the gateway returns its
-cross-partition sentinel error. Queries the gateway can still serve
-(e.g. single-partition WHERE filters, projections, built-ins) incur no
-extra round-trip — they never leave the gateway path.
+The harness calls the driver's own `QueryEveryPartition`, which engages the
+pure-Go query engine when the gateway returns its cross-partition sentinel
+error. Queries the gateway can still serve (e.g. single-partition WHERE
+filters, projections, built-ins) incur no extra round-trip — they never leave
+the gateway path.
## Scope
diff --git a/scripts/cosmos-repro-go/main.go b/scripts/cosmos-repro-go/main.go
index 77491bf889..8085cffac3 100644
--- a/scripts/cosmos-repro-go/main.go
+++ b/scripts/cosmos-repro-go/main.go
@@ -3,10 +3,11 @@
// Go reproduction harness for BYT-9239 — the twin of scripts/cosmos-repro-dotnet.
// Runs the full 40-query target inventory from
// https://linear.app/bytebase/issue/BYT-9239 against a live Azure Cosmos
-// account through Bytebase's forked azcosmos SDK (which includes the pure-Go
-// query engine), and prints a markdown PASS/FAIL table matching the shape
-// the .NET harness produces. Pairing both harnesses on the same container
-// is the regression guard for the BYT-9239 feature set.
+// account through the same cross-partition path the Bytebase driver uses
+// (gateway first, pure-Go query engine on refusal), and prints a markdown
+// PASS/FAIL table matching the shape the .NET harness produces. Pairing both
+// harnesses on the same container is the regression guard for the BYT-9239
+// feature set.
//
// Required environment variables:
// COSMOS_ENDPOINT e.g. https://bytebase-cosmostest.documents.azure.com:443/
@@ -37,6 +38,8 @@ import (
"time"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
+
+ "github.com/bytebase/bytebase/backend/plugin/db/cosmosdb"
)
type testCase struct {
@@ -156,7 +159,7 @@ func main() {
}
fmt.Printf("# Go SDK reproduction — db=%s coll=%s\n", db, coll)
- fmt.Println("SDK: github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos (pure-Go query engine)")
+ fmt.Println("SDK: github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos + backend/plugin/db/cosmosdb/queryengine/gonative")
fmt.Println()
rows := make([]result, 0, len(cases))
@@ -209,19 +212,11 @@ func runOne(c *azcosmos.ContainerClient, tc testCase) result {
// containers. Matches the .NET harness's ceiling.
const rowCap = 10_000
- pager := c.NewCrossPartitionQueryItemsPager(tc.sql, nil)
- n := 0
- for pager.More() {
- page, err := pager.NextPage(ctx)
- if err != nil {
- return result{id: tc.id, sql: tc.sql, status: "FAIL", rows: n, elapsedMs: time.Since(start).Milliseconds(), err: err.Error()}
- }
- n += len(page.Items)
- if n >= rowCap {
- break
- }
+ items, err := cosmosdb.QueryEveryPartition(ctx, c, tc.sql, nil, rowCap)
+ if err != nil {
+ return result{id: tc.id, sql: tc.sql, status: "FAIL", rows: len(items), elapsedMs: time.Since(start).Milliseconds(), err: err.Error()}
}
- return result{id: tc.id, sql: tc.sql, status: "PASS", rows: n, elapsedMs: time.Since(start).Milliseconds()}
+ return result{id: tc.id, sql: tc.sql, status: "PASS", rows: len(items), elapsedMs: time.Since(start).Milliseconds()}
}
func envOr(key, def string) string {