Skip to content

[Bug]: Reversed Prepared BETWEEN bounds panic in persisted secondary-index prefix filtering #29644

Closed

Description

@LeftHandCold

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Related: #29606 tracks Prepared range performance, index eligibility and pruning. Its acceptance criteria mention reversed bounds, but it does not record this negative-length allocation panic. Searches of open and closed issues for the helper name, makeslice with between, and reversed-range panics did not find an exact duplicate. Historical #15196 / #15425 concern different prefix-comparison correctness problems.

Branch Name

main and 4.2-dev

Commit ID

  • Main Daily, also reproduced independently: ce2a22aa5a521d59ff7bb60e7e84d34cb2c93f81.
  • 4.2 Daily: 5c4590a55d77c93cde687ab824e9238ddce4d9c1.
  • Source reviewed on newer main: 614ab46453638324a7997ac3ecc6813e5c810309.
  • Source reviewed on newer 4.2-dev: e93a5d20524d17e2a89100ce609cf67a45d13aa6.

The newer heads still contain the affected guard. They were source-reviewed, not separately rebuilt and SQL-tested for this report.

Other Environment Information

  • Main MOTR Job.
  • 4.2 MOTR Job.
  • MOTR revision: 3d19e35ef32c3bf360dc55325cf701c4a7ceea93; the prepared_pruning probe uses a finite-domain oracle, not a stored result golden.
  • Independent reproduction on an isolated Linux x86_64 single-instance container: 4 CPUs, 8 GiB memory limit, GOMEMLIMIT=4500MiB; loopback-only SQL endpoint. Existing environments were not modified.
  • Exact Main image: matrixone:commit-ce2a22aa5a521d59ff7bb60e7e84d34cb2c93f81; digest sha256:a0fe26e06fe812c594b00940c34004771018941c14661f5259c2ac7b6060ca90, matching the Daily image.
  • Fixture: 32,768 rows, INT k, ordinary secondary index on k, both base and hidden index tables flushed to persisted storage.
  • Binary protocol reproduction uses go-sql-driver/mysql v1.8.1, int64 parameters and interpolateParams=false. SQL PREPARE / EXECUTE also reproduces.

Actual Behavior

Executing this Prepared predicate against the persisted secondary index returns an internal panic error rather than an empty result:

k BETWEEN 16002 AND 16000
OR k BETWEEN 16006 AND 16005

The independent reproduction returned:

Error 20101 (HY000): internal error: panic runtime error: makeslice: len out of range
runtime.panicmakeslicelen
runtime.makeslice
pkg/objectio.(*readFilterSearchTerm).search.CollectOffsetsByPrefixBetweenFactory.func8
    pkg/container/vector/search.go:636
pkg/objectio.(*readFilterSearchTerm).search
    pkg/objectio/constructors.go:638
pkg/objectio.(*ReadFilterSearch).search
    pkg/objectio/constructors.go:519
pkg/objectio.SearchCachedVector
    pkg/objectio/constructors.go:495
pkg/objectio/ioutil.LoadColumnDataBySearch

Both Daily branches report the same CollectOffsetsByPrefixBetweenFactory allocation failure. Main reports indexed-ranges first: binary/reversed; 4.2 preserves the same reversed-bound error among additional, separate numeric-boundary failures. The 4.2 constructors line numbers differ, but the helper and allocation are identical.

Independent controls on the same Main image and dataset:

Execution Actual Expected
Binary Prepared, forward bounds [16000,16002,16005,16006] 5 correct rows 5 rows
Same Prepared, outside-INT32 first interval 2 correct rows from the second interval 2 rows
Same Prepared, NULL first lower bound 2 correct rows from the second interval 2 rows
Same Prepared, different forward values 5 correct rows 5 rows
Same Prepared, both intervals reversed Error 20101 / makeslice panic Empty result
Same Prepared, forward values after the error 5 correct rows 5 rows
Fresh SQL PREPARE, first execution with reversed values Same panic error Empty result
Literal SQL with the same reversed bounds Empty result Empty result
Binary Prepared on a copy without the secondary index Empty result Empty result
SELECT 1 after the failing queries 1 1

Impact is a valid SELECT failing on the indexed Prepared path. The panic is recovered by the SQL execution pipeline. The independent container remained running and was not OOM-killed before deliberate cleanup. These observations do not establish a CN process crash, persistent data corruption, or cluster-wide unavailability.

Expected Behavior

Each reversed closed interval denotes an empty set. Their OR must therefore return zero rows without an internal error, regardless of index access or parameter protocol. Normal executions of the same Prepared statement must continue to return correct results.

Steps to Reproduce

Use a disposable database on the affected Main image.

CREATE DATABASE prefix_between_probe;
USE prefix_between_probe;
CREATE TABLE scan_rows (
  i INT PRIMARY KEY,
  b BIGINT,
  u BIGINT UNSIGNED,
  d DECIMAL(18,2),
  k INT,
  KEY numeric_k(k)
);
INSERT INTO scan_rows
SELECT result, result, result, result, ((result - 1) * 17) % 32768
FROM generate_series(1,32768) g;
SELECT COUNT(*) FROM scan_rows; -- 32768
SELECT mo_ctl('dn','flush','prefix_between_probe.scan_rows');

SELECT DISTINCT index_table_name
FROM mo_catalog.mo_indexes
WHERE name = 'numeric_k'
  AND table_id = (
    SELECT rel_id FROM mo_catalog.mo_tables
    WHERE reldatabase = 'prefix_between_probe' AND relname = 'scan_rows'
  );

Flush the returned hidden index table as well, substituting its actual name:

SELECT mo_ctl('dn','flush','prefix_between_probe.<returned_index_table_name>');

PREPARE range_p FROM
  'SELECT k FROM scan_rows
   WHERE k BETWEEN ? AND ? OR k BETWEEN ? AND ?
   ORDER BY k';
SET @a=16002, @b=16000, @c=16006, @d=16005;
EXECUTE range_p USING @a,@b,@c,@d;
-- Actual: Error 20101 with makeslice panic; expected: zero rows.

SELECT k FROM scan_rows
WHERE k BETWEEN 16002 AND 16000 OR k BETWEEN 16006 AND 16005
ORDER BY k;
-- Literal control: zero rows.

The binary protocol probe prepares the identical query and executes the parameter sequence shown in the control table above with int64 bindings. Its independent oracle enumerates k in [0,32768) and applies the two closed-interval predicates directly. Multiplication by 17 modulo 32768 is a permutation, so every k in this domain exists exactly once.

Additional information

Root cause confirmed by execution and exact-version source

  1. Persisted sorted secondary-index filtering reaches readFilterSearchPrefixBetween with closed bounds (hint == 0). Main dispatch.
  2. CollectOffsetsByPrefixBetweenFactory independently binary-searches the first key at/above the lower bound and the first key beyond the upper prefix. For reversed bounds with matching persisted keys, the searches can produce end < start.
  3. Its guard only handles start == end; execution then calls make([]int64, end-start) with a negative length. The observed stack identifies exactly that allocation. Affected helper.
  4. The neighboring CollectOffsetsByPrefixInRangeFactory already rejects start >= end. The closed-prefix helper has not applied the same empty-range invariant. Existing neighboring guard.
  5. Literal and no-secondary-index counterfactuals return the expected empty set. This is not a result golden error, a connection timeout, or evidence of an OOM.

Regression classification

This is newly exposed by the expanded boundary probe, not proven to be introduced by the immediately preceding Daily commits. The same missing empty-range guard and negative-length allocation are present in the helper added by 57a9f7a20d86a275730838dd003bfca7ab48e170 on March 13, 2024 (PR #14893). That proves a longstanding component boundary defect, not the earliest release in which this exact SQL plan was reachable. No SQL introducing-commit bisection is claimed.

Focused validation for a repair

  • Direct sorted-prefix helper checks for reversed, equal, forward, out-of-block and empty ranges, compared with the existing linear path where applicable.
  • SQL PREPARE and real binary int64 executions against a persisted secondary index, with a no-index control and an independent expected-result oracle.
  • Forward -> reversed -> forward reuse of the same handle, plus a fresh handle first executed with reversed bounds.
  • Preserve valid equal-bound and forward-range results, prefix semantics and existing pruning behavior.
  • Keep this panic separate from [Bug]: Prepared random_ranges regression disables pruning and loses index access with BIGINT bindings #29606's performance/type-normalization mechanisms and from unrelated numeric diagnostic failures in the same MOTR job.

No production fix was applied for this report.

Activity

  1. added
    kind/bug
    on Oct 6, 2026
  2. self-assigned this
    on Oct 6, 2026
  3. LeftHandCold commented on Oct 6, 2026

    @LeftHandCold
    CollaboratorAuthor

    Root cause, regression classification and verified Main repair

    Main-targeted repair: #29645, head cef0b7215fd10bd31ec73c19c1ddf9f7889cf39a.

    Root cause is confirmed. The sorted closed-prefix helper independently
    searches its lower and upper positions. Reversed Prepared bounds can produce
    end < start; the old equality-only guard then permits a negative-length
    selection allocation. The repair changes that guard to start >= end, without
    changing the search comparisons or adding a fallback scan.

    This is not established as a recently introduced MO regression.

    • The exact missing guard exists in the helper's introducing commit
      57a9f7a20d86a275730838dd003bfca7ab48e170, #14893,
      dated March 13, 2024. This establishes the component defect's origin, not
      the first release whose planner exposes the precise SQL shape.
    • I also independently reproduced the public binary/SQL PREPARE panic on the
      September 30 4.2 image 28f1f02a89339def1b2572dbe07e98144a203167.
    • MOTR #215, merged October 5,
      added the reversed OR/BETWEEN boundary probe. Previous helper tests only
      covered forward ranges. Literal reversed SQL and a no-secondary-index
      control return empty results and do not expose this failing indexed path.

    Before/after validation on an isolated Linux instance on 50:

    Check Before repair Fixed Main
    Extended helper UT Reversed case panics All 13 subtests pass
    256-row persisted-index BVT, normal result comparison Three reversed indexed queries panic on the incident Main image 37/37 statements pass, twice on the same instance
    Original real binary-int64 / SQL PREPARE probe, 32,768 rows Matching reversed-bound panic on incident Main and historical 4.2 All nine independent oracle checks pass
    Forward/rebound/NULL and literal/no-index controls Correct on incident Main Still correct

    The fixed binary was built from fresh Main
    c65991043b10fb39988d45b72af1485560ac5433 plus the exact delivered source
    changes. The complete vector suite passes on Linux amd64 and macOS arm64;
    the objectio consumer suite and vector/objectio vet also pass locally.
    Both normal BVT runs have zero ignored/abnormal statements and zero BVT
    database residue. The owned test container had no OOM and exited 0 after
    deliberate shutdown. Existing environments were not modified.

    Risk/scope: one production condition, plus UT/BVT. Normal nonempty ranges
    retain identical searches, allocation and offsets. A shorter upper prefix can
    still denote a valid range; that boundary is explicitly tested and preserved.
    There is no new per-row scan, copy, synchronization, I/O, cache, index-format or
    parameter-conversion change. Complete self-review found no unresolved blockers.
    Unrelated numeric-normalization/performance problems in #29606 are not claimed
    to be resolved.

    The PR is ready for review, not yet merged. GitHub CI has started and is still
    pending; the results above are completed local/isolated-host validation, not
    a claim that all CI checks or an external reviewer have approved the PR.

  4. LeftHandCold commented on Oct 6, 2026

    @LeftHandCold
    CollaboratorAuthor

    4.2-dev backport submitted and independently validated

    Backport PR: #29647 (base 4.2-dev), cherry-picked with -x from Main #29645.
    There are no conflicts/adaptations or additional Main changes.

    • Fresh 4.2 base: e93a5d20524d17e2a89100ce609cf67a45d13aa6.
    • Exact tested/published head: 1d14bc3476ec3d491cb71c765e0e2258c02f92c5.
    • Same four files and stable patch-id as the Main repair; the only production
      change is the start >= end empty-selection guard before allocation.

    Independent validation on an owned Linux amd64 instance on 50:

    1. Branch-matching native build and full make build passed.
    2. All 13 helper boundary subtests, complete vector/objectio suites and vet passed.
    3. Real binary-int64 and SQL PREPARE reversed ranges now return empty results
      without the panic. All eight in-scope independent oracle checks passed;
      normal/rebound/NULL and literal/no-index controls stayed correct.
    4. The seven non-reversed probe records match the historical 4.2 run exactly,
      including its separate outside-INT32 Error 1690. That behavior is not
      silently reclassified as fixed or changed by this backport.
    5. Normal BVT comparison passed 37/37 twice on the same instance, zero
      ignored/abnormal statements, zero BVT database residue and successful
      SELECT 1 after both runs. No generated failing result was accepted.
    6. Full backport self-review and diff-check passed with no unresolved blockers.
      Valid ranges retain their searches/allocation/offsets; no added scan,
      copying, synchronization, cache, I/O or index-format change.

    The PR is ready for review, not merged. CI preflight has passed; the remaining
    GitHub checks are still running. The completed validation above belongs to
    the actual 4.2 backport, not just reused Main test results.

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions