SUM over a union column should skip an undefined slot, not throw ArrayIndexOutOfBoundsException

Open Beginner friendly
#6,574 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java, sql
Domain
databases

Research direction

Start with DenseUnionVector.sumInto and compare it with the guarded per-leg logic in forEachLeaf; also review writeUndefined and the union copiers to understand how type id -1 is preserved. Add a focused unit test alongside the AggregateTest coverage for forEachLeaf, asserting that an undefined slot is treated as NULL by SUM. Done also includes checking whether other per-leg splits in DenseUnionVector have the same gap.

Written by the indexing model from the issue text.

Description

expression engine unshaved-yak

SUM over a union column holding an undefined slot throws ArrayIndexOutOfBoundsException, where it should skip the slot as NULL.

  • An undefined slot carries type id -1.
    DenseUnionVector.writeUndefined() writes it, and the union's copiers carry it across; isNull reads such a row as NULL (leg(idx)?.isNull(…) ?: true).
  • DenseUnionVector.sumInto splits the batch's mask per leg with legMasks[getTypeId(idx).toInt()].add(i), and doesn't check for -1.
    So an in-mask undefined slot indexes legMasks[-1].
  • #6565's forEachLeaf (landing with that PR), the same per-leg split for min/max and bool_and/bool_or, skips a negative type id, with a unit test in AggregateTest.
    sumInto is the remaining copy without the guard.

Symptoms

  • assumption: the exception is ArrayIndexOutOfBoundsException: Index -1 out of bounds for length N, thrown from DenseUnionVector.sumInto — read from the code, not reproduced.
  • assumption: reachable from SQL as SUM(x) over a union column with undefined slots among the rows that reach the aggregate; which query writes such a column is unknown, so done is a unit test on sumInto, as #6565's is on forEachLeaf.
  • COUNT is unaffected: DenseUnionVector doesn't override countInto, and the default goes through isNull, which handles -1.
  • check: whether DenseUnionVector's other per-leg splits share the gap.

Expected behaviour

  • An undefined slot contributes nothing to SUM, as a NULL row doesn't.
Dominant language
Clojure
Stars
3.1k
Forks
193
Avg merge
1d 3h
Merged PRs (30d)
84

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xtdb/xtdb

All issues in xtdb/xtdb

Similar issues

More Clojure issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.