Skip to content

[Bug]: MongoDB explicit pipeline rejects initial $sort and $unwind candidates #28337

Closed

Description

@Ariznawlll

Summary

The explicit MongoDB pipeline path rejects $sort and $unwind, although MatrixOne issue #27536 lists both stages as initial aggregation-stage candidates.

Environment

  • Host: TKE node 129
  • Namespace: mo-search-commit-4fdb9e916-20260907
  • MatrixOne image: commit-4fdb9e916
  • Topology: 3 CN / 1 DN / 3 Log / 2 Proxy
  • MongoDB: 8.0.12, 3-member rs0
  • Source fixture: mongodb_cov_4fdb.events

Reproduction

Both statements were executed three times in the target namespace:

SELECT COUNT(*)
FROM mongodb_cov_4fdb.events
WHERE __mo_query = '{"pipeline":[{"$sort":{"site_id":1}}]}';

SELECT COUNT(*)
FROM mongodb_cov_4fdb.events
WHERE __mo_query = '{"pipeline":[{"$unwind":"$site_id"}]}';

Actual result

Each run fails before the MongoDB operation with:

ERROR 20301 (HY000): invalid input: MongoDB pipeline stage is not allowed

Result: $sort 3/3 rejected; $unwind 3/3 rejected.

The current implementation's allowedUserPipelineStages map contains neither stage, while the validator has stage-specific handling for both. The positive controls $match, $project, $set, $addFields, $unset, $group, $limit, $skip, and $count execute successfully in the same environment.

Expected result

Please confirm and implement the intended contract for #27536. If $sort and $unwind remain in the initial candidate scope, valid read-only forms should be accepted and streamed through MongoDB aggregate; unsafe forms should continue to be rejected by the allowlist and resource checks. If they are intentionally out of scope, please update the issue/design documentation and acceptance matrix explicitly.

Safety checks

  • No MongoDB source data was modified.
  • The external-table baseline remained COUNT(*) = 5, SUM(measurement) = 74.
  • Target namespace pods remained Running with restart count 0.

Activity

  1. added
    kind/bug
    on Sep 7, 2026
  2. added theissue type on Sep 7, 2026
  3. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main(commit f72ca9e)本地验证:执行 mo-cgo-test ./test/mongodb -run TestMongoDBLocalE2E -count=1 -p=1,MongoDB E2E runner 的端口规划、契约用例及 explicit-sort-and-unwind-pipeline 测试均通过(4.165s);源码契约覆盖 $sort/$unwind 的合法管线和非法参数拒绝。当前本机没有可用 MongoDB ReplicaSet,且本地 Docker Mongo/MO 依赖未处于可运行状态,因此未执行真实外部 MongoDB 连接、cursor 和 __mo_query 数据校验。结论:#28344 已在当前 main,代码级回归通过;真实 MongoDB E2E 仍待可用服务后复测,暂不关闭。

  4. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    在最新官方 main f72ca9efbeb3a7c673701e4a135cc6670c33cfde 完成修复回归:$sort、$unwind 各连续 3/3 通过;同时验证 32 sort fields 成功/33 拒绝、199 unwind path segments 成功/200 拒绝、16 stages 成功/17 拒绝。该问题在此 SHA 上未再复现。证据见 Ariznawlll/mo-test-reports#26,commit c2152ec。

  5. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:585a38efd152fadf216c8675b7b997a35ca8deb1
    • 二进制 SHA-256:9e20c75f522037c7bc08ed0ff0d19ed8b449fdd44dfa553083e018df70ad6bee
    • 部署方式:macOS arm64 本地单进程 MatrixOne(1 CN / 1 TN)+ Docker MongoDB 8.0.12 单节点 ReplicaSet,SCRAM-SHA-256
    • 测试日期:2026-09-08

    原问题验证

    按 Issue 原始查询分别执行:

    SELECT COUNT(*) FROM mongodb_ci.events
    WHERE __mo_query = '{"pipeline":[{"$sort":{"site_id":1}}]}';
    
    SELECT COUNT(*) FROM mongodb_ci.events
    WHERE __mo_query = '{"pipeline":[{"$unwind":"$site_id"}]}';

    $sort 与 $unwind 各连续执行 3 次,共 6/6 返回 5;未再返回 pipeline stage is not allowed。

    Happy Path 覆盖

    1. 原始 $sort:{site_id:1}:3/3 返回 5。
    2. 原始 $unwind:"$site_id":3/3 返回 5。
    3. $sort:{_id:-1} + $limit:1 返回最大 _id=64b000000000000000000005。
    4. $set 构造双元素数组后 $unwind 返回 10 行。
    5. $unwind options object(path/includeArrayIndex/preserveNullAndEmptyArrays)返回 5 行。
    6. 正向上限:32 个 sort key 返回 5;199 段 unwind path 可执行;16 stages 返回 1。
    7. 官方 E2E explicit-sort-and-unwind-pipeline 通过;完整官方 MongoDB E2E 为 26/26 PASS。

    Unhappy Path 覆盖

    1. 33 个 sort key 按上限拒绝;$sort direction 为 0 按格式拒绝。
    2. 200 段 unwind path 按上限拒绝;不带 $ 的 unwind path 按格式拒绝。
    3. 17 stages、33 层嵌套、超过 64 KiB query 均按上限拒绝。
    4. 写阶段 $out、跨 collection $lookup、$function 和未知 stage 均 fail-closed 拒绝。
    5. 拒绝路径后普通查询基线仍为 COUNT=5, SUM(measurement)=74。
    6. 最终 cursor open=135, close=135,pool checked_out=0;Mongo 容器 RestartCount=0、OOMKilled=false;MatrixOne 日志未见 panic、fatal、OOM、越界或段错误。

    白盒验证

    执行:

    mo-cgo-test -race -count=10 -run '^(TestParseUserQueryAcceptsSortAndUnwind|TestParseUserQueryRejectsMalformedAndAmbiguousInput|TestParseUserQueryRejectsUnsafeStagesAndOperators|TestUserQueryPlanRevalidationFailsClosed)$' ./pkg/sql/mongodb
    

    结果:PASS,ok github.com/matrixorigin/matrixone/pkg/sql/mongodb。覆盖 sort/unwind 正常解析、非法格式、安全阶段拒绝及 plan revalidation fail-closed。

    回归测试

    • 修复提交:39225f5fbc63b44a190cdd43f954ee07d78e6cdc,PR fix(mongodb): allow sort and unwind pipeline stages #28344。
    • 已合入官方 E2E:explicit-sort-and-unwind-pipeline,包含 string/options unwind 形式和非法 sort/unwind。
    • 已合入 Go UT:TestParseUserQueryAcceptsSortAndUnwind 及对应拒绝/重校验测试。
    • 本轮未新增重复回归代码;直接验证 main 中已合入的回归资产。

    测试证据

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions