Bug #120004 IndexSkipScanIterator Assertion Failure

Submitted: 7 Mar 19:38 Modified: 9 Mar 9:48
Reporter: Chunling Qin Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0.31 OS:Any
Assigned to: CPU Architecture:Any

[7 Mar 19:38] Chunling Qin
Description:
When using a LATERAL subquery that references an outer query block and employs Index Skip Scan optimization, the server crashes with an assertion failure in `handler::ha_index_init()`:

```
mysqld: /data/mysql-server/sql/handler.cc:2792: int handler::ha_index_init(uint, bool): Assertion `inited == NONE' failed.
```

## Root Cause

In `IndexSkipScanIterator::Init()` (line 152), the code calls `ha_index_init()` without first checking if the handler is already initialized. When a LATERAL subquery is re-materialized for each outer row, `IndexSkipScanIterator::Init()` is called multiple times, but the handler's `inited` flag is still set from the previous iteration.

**Problematic Code (`sql/range_optimizer/index_skip_scan.cc:152`):**
```cpp
bool IndexSkipScanIterator::Init(void) {
  // ... initialization code ...

  // BUG: No check for table()->file->inited before calling ha_index_init()
  if ((result = table()->file->ha_index_init(index, true))) {
    table()->file->print_error(result, MYF(0));
    return true;
  }
  // ...
}
```
## Stack Trace

```
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  __GI_abort () at abort.c:79
#2  __assert_fail_base (...) at assert.c:92
#3  __GI___assert_fail (...) at assert.c:101
#4  handler::ha_index_init(unsigned int, bool) at sql/handler.cc:2792
#5  IndexSkipScanIterator::Init() at sql/range_optimizer/index_skip_scan.cc:152
#6  FilterIterator::Init() at sql/iterators/composite_iterators.cc:...
#7  LimitOffsetIterator::Init() at sql/iterators/composite_iterators.cc:...
#8  MaterializeIterator::MaterializeQueryBlock(...) at sql/iterators/composite_iterators.cc:846
#9  MaterializeIterator::Init() at sql/iterators/composite_iterators.cc:557
#10 NestedLoopIterator::Read() at sql/iterators/composite_iterators.cc:...
...
```

How to repeat:
  CREATE TABLE t_outer (id INT PRIMARY KEY);
  INSERT INTO t_outer VALUES (1), (2);

  -- trigger Bug
  SET SESSION optimizer_switch = 'hash_join=off';

  SELECT subq_0.id as c0
  FROM (SELECT id FROM t_outer LIMIT 110) as subq_0,
  LATERAL (
      SELECT subq_0.id as c1
      FROM information_schema.ST_SPATIAL_REFERENCE_SYSTEMS as ref_8
      WHERE ref_8.ORGANIZATION IS NOT NULL
      LIMIT 64
  ) as subq_1;
[9 Mar 9:48] Roy Lyseng
Thank you for the bug report.
Verified as described.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.038 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.