Skip to content

[Bug]: MongoDB external table explicit filter with residual predicate panics #28333

Closed

Description

@Ariznawlll

问题概述

MongoDB external table 使用显式 __mo_query filter 时,如果同时叠加普通 MatrixOne residual predicate,查询会触发 CN runtime panic,而不是返回正确结果或稳定 SQL 错误。

环境

  • MatrixOne TKE namespace:mo-search-commit-4fdb9e916-20260907
  • MatrixOne 构建:commit-4fdb9e916
  • 拓扑:3 CN / 1 DN / 3 Log / 2 Proxy
  • MongoDB:8.0.12,3-member rs0
  • 外表:mongodb_cov_4fdb.events,fixture 基线 5 行,SUM(measurement)=74
  • 说明:当前 TKE 镜像不是官方最新 main;提报时保留该构建差异,需在最新 main 复核。

复现步骤

SELECT COUNT(*)
FROM mongodb_cov_4fdb.events
WHERE __mo_query = '{"filter":{"site_id":"site-west"}}'
  AND measurement > 0;

也可以使用以下 residual 条件复现:

SELECT COUNT(*)
FROM mongodb_cov_4fdb.events
WHERE __mo_query = '{"filter":{"site_id":"site-west"}}'
  AND site_id = 'site-west';

期望结果

显式 MongoDB filter 先筛选 site-west,MatrixOne 再执行 residual predicate,返回 COUNT(*) = 1;查询不应 panic。

实际结果

连续 3/3 执行均返回:

ERROR 20101 (HY000): internal error: panic runtime error: index out of range [1] with length 1

投影变体连续 3/3 也复现,越界索引随投影列数变化,例如 [2] with length 2。

CN 日志调用栈关键位置:

pkg/sql/colexec.(*ColumnExpressionExecutor).Eval
pkg/sql/colexec/evalExpression.go:1685
pkg/sql/colexec.(*FunctionExpressionExecutor).Eval
pkg/sql/colexec/filter.(*Filter).Call

对照结果

  • 仅显式 filter:连续 3/3 返回 COUNT(*) = 1
  • 不使用 __mo_query 的普通查询:连续 3/3 返回 COUNT(*) = 1
  • 显式 pipeline + event_count >= 1 residual:连续 3/3 返回正确结果
  • EXPLAIN 显示该失败 SQL 为 pushed=0、residual,不是错误的 Mongo pushdown
  • 失败后 CR 仍为 Ready,CN/DN/Mongo 均无重启;基线仍为 COUNT=5, SUM(measurement)=74

影响

#27536 新增的显式 filter 查询在与普通 SQL 条件组合时会导致客户端收到内部 panic,阻断合法查询,并可能暴露内部运行时错误栈。

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    补充复现证据(2026-09-07)

    继续覆盖 #27536 允许的 pipeline stage 和外层查询组合后,确认同一类 panic 还可由外层排序触发:

    SELECT device_id
    FROM mongodb_cov_4fdb.events
    WHERE __mo_query = '{"filter":{"site_id":"site-east"}}'
    ORDER BY device_id;

    该语句连续 3/3 返回:

    ERROR 20101 (HY000): internal error: panic runtime error: index out of range [1] with length 1
    

    对照与补充结果:

    • 显式 filter + AND site_id='site-west':3/3 panic
    • 显式 filter + AND measurement > 0:3/3 panic
    • 显式 filter + 外层 ORDER BY:3/3 panic
    • 显式 filter 单独、普通查询、AND 1=1:3/3 正常
    • $match/$project/$set/$addFields/$unset/$limit/$skip/$count 正向 pipeline 组合:各 3/3 正常
    • 失败后 CR 仍 Ready,CN/DN/Mongo 无重启,外表基线仍为 5/74

    EXPLAIN 对 residual 组合显示 pushed=0、residual;CN 栈仍落在 ColumnExpressionExecutor.Eval evalExpression.go:1685,随后为 FunctionExpressionExecutor/Filter。

  4. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一组更小的复现:不叠加普通 residual,仅对显式 filter 做普通行投影也会触发同一类 CN 越界 panic。

    SELECT device_id
    FROM mongodb_cov_4fdb.events
    WHERE __mo_query = '{"filter":{"site_id":"site-west"}}';

    在 mo-search-commit-4fdb9e916-20260907 连续执行 3/3 均返回:

    ERROR 20101 (HY000): internal error: panic runtime error: index out of range [1] with length 1
    

    SELECT measurement, device_id, site_id ... 的 3/3 结果为 index out of range [3] with length 3;显式 filter + 外层 LIMIT 1 也是 3/3 panic。相同 fixture 的普通无 __mo_query 行投影、显式 filter 的 COUNT(*) 和 AND 1=1 对照正常。堆栈仍落在 pkg/sql/colexec.(*ColumnExpressionExecutor).Eval (evalExpression.go:1685)。目标 namespace 健康,基线仍为 COUNT=5, SUM(measurement)=74。

  5. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新官方 main f72ca9efbeb3a7c673701e4a135cc6670c33cfde 本地单进程 MO + MongoDB 8.0.12 ReplicaSet 复核:正文涉及的 4 种显式 filter + residual/projection/order-limit 形态均稳定复现 3/3(共 12/12),仍报 ColumnExpressionExecutor.Eval pkg/sql/colexec/evalExpression.go:1685 index out of range panic。失败后基线查询可继续使用,最终 cursor open=134/close=134、pool checked_out=0。逐项证据见 Ariznawlll/mo-test-reports#26,commit c2152ec。

  6. added
    ai-easy
    on Sep 8, 2026
  7. 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 原始合法查询执行显式 filter + MO residual:

    SELECT COUNT(*) FROM mongodb_ci.events
    WHERE __mo_query = '{"filter":{"site_id":"site-west"}}'
      AND measurement > 0;

    连续 3/3 返回 1,未再出现 ColumnExpressionExecutor.Eval / evalExpression.go:1685 越界 panic。

    Happy Path 覆盖

    1. 显式 filter + measurement > 0 residual:3/3 返回 1。
    2. 显式 filter + site_id='site-west' residual:3/3 返回 1。
    3. 显式 filter + 行投影:3/3 返回 device-001。
    4. 显式 filter + 外层 ORDER BY/LIMIT:3/3 返回 device-001。
    5. 官方 E2E explicit-filter-residual 通过;完整官方 MongoDB E2E 为 26/26 PASS。
    6. 目标查询后普通基线仍为 COUNT=5, SUM(measurement)=74,10 路并发读取结果一致。

    Unhappy Path 覆盖

    1. 非法 uppercase envelope 稳定拒绝,没有进入残余表达式执行。
    2. $out、$lookup、$function、未知 stage 均 fail-closed 拒绝,无 panic。
    3. 33 层嵌套、超过 64 KiB、17 stages 均按上限拒绝。
    4. 上述失败路径执行后普通查询仍为 5|74;最终 cursor open=135, close=135,pool checked_out=0。
    5. Mongo 容器 RestartCount=0、OOMKilled=false;MatrixOne 日志未发现 panic、fatal、OOM、越界或段错误。

    白盒验证

    执行:

    mo-cgo-test -race -count=10 -run '^(TestCompilePreparedMongoScanDoesNotMutateCachedPlan|TestCompilePlanScopeMongoScanUsesConfiguredResidualFilter|TestConfigureMongoUserQuerySeparatesSelectorAndResidual|TestConfigureMongoUserQueryReconstructsSourceForExplicitProjection|TestConfigureMongoUserQueryPipelineKeepsHiddenCarrierForResidualFilter)$' ./pkg/sql/compile
    

    结果:PASS,ok github.com/matrixorigin/matrixone/pkg/sql/compile。修复后 scan config 与 residual filter 使用同一 compile-owned Mongo node,相关 plan ownership/投影场景均通过 race 重复验证。

    回归测试

    • 修复提交:34aff8c5e2c8388514a194d5f5d3a5e5110cfe65,PR fix(mongodb): preserve residual filters for explicit queries (#28333) #28366。
    • 已合入官方 E2E:explicit-filter-residual。
    • 已合入 Go UT:TestCompilePlanScopeMongoScanUsesConfiguredResidualFilter、TestCompilePreparedMongoScanDoesNotMutateCachedPlan 等。
    • 本轮未新增重复回归代码;直接验证 main 中已合入的回归资产。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions