Skip to content

coldata: use Equivalent() instead of Identical() in ReplaceCol - #172874

Open
shivamshaw23 wants to merge 1 commit into
cockroachdb:masterfrom
shivamshaw23:fix-172870-varbit-vectorized-replace-col
Open

coldata: use Equivalent() instead of Identical() in ReplaceCol#172874
shivamshaw23 wants to merge 1 commit into
cockroachdb:masterfrom
shivamshaw23:fix-172870-varbit-vectorized-replace-col

Conversation

@shivamshaw23

Copy link
Copy Markdown

Summary

Fixed a vectorized execution internal error for valid queries involving
BIT/VARBIT types (with width constraints) combined with GROUP BY and
UNION ALL.

Error: unexpected replacement: original vector is varbit[] whereas the replacement is varbit[]

Root Cause

ReplaceCol in MemBatch used Type().Identical() to validate column
vector replacements. Identical() requires exact match of all type
metadata including Width, Precision, and Oid. When the
SerialUnorderedSynchronizer (used for UNION ALL) swapped column vectors
from the second input into the output batch, two VARBIT vectors with
different widths (e.g., VARBIT(1) with Width=1 vs VARBIT with
Width=0) caused a panic — despite having the same physical
representation (both backed by DatumVec).

Fix

Changed ReplaceCol and Reset in MemBatch to use
Type().Equivalent() instead of Type().Identical().
Equivalent() checks type family compatibility without requiring
exact width/precision/OID match, which is the correct semantic for
physical-level column vector operations. This also resolves a
long-standing TODO in the code that noted Identical() was likely
overkill.

Changes

  • pkg/col/coldata/batch.go: Changed ReplaceCol and Reset
    to use Equivalent() instead of Identical(). Removed stale TODO
    comment that explicitly called out this issue.
  • pkg/sql/logictest/testdata/logic_test/union: Added 4
    regression test cases covering all reproducing variants (VARBIT[],
    BIT[], VARBIT scalar, BIT scalar — all with width constraints).

Fixes: #172870

Release note (bug fix): Fixed an internal error in the vectorized
execution engine that occurred when running queries with BIT or VARBIT
types (with width constraints) combined with GROUP BY and UNION ALL.
The error message was: unexpected replacement: original vector is varbit[] whereas the replacement is varbit[].

@shivamshaw23
shivamshaw23 requested a review from a team as a code owner July 28, 2026 07:10
@shivamshaw23
shivamshaw23 requested review from DrewKimball and removed request for a team July 28, 2026 07:10
@blathers-crl

blathers-crl Bot commented Jul 28, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jul 28, 2026
@cockroachlabs-cla-agent

cockroachlabs-cla-agent Bot commented Jul 28, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Previously, ReplaceCol and Reset in MemBatch used Type().Identical()
to validate column vector replacements. Identical() requires exact
match of all type metadata including Width, Precision, and Oid. This
caused a panic when two vectors had the same physical representation
but different type metadata - for example, VARBIT(1) (Width=1) vs
VARBIT (Width=0), which are both backed by DatumVec.

This manifested as a vectorized runtime crash for valid queries
involving BIT/VARBIT expressions with width constraints followed by
GROUP BY and UNION ALL:

  SELECT IF(false, ARRAY[]::VARBIT(1)[], NULL)
    GROUP BY 1 UNION ALL SELECT NULL;

  ERROR: unexpected replacement: original vector is varbit[]
         whereas the replacement is varbit[]

The fix changes both ReplaceCol and Reset to use Type().Equivalent()
instead. Equivalent() checks type family compatibility without
requiring exact width/precision/OID match, which is the correct
semantic for physical-level column vector operations. This also
resolves a long-standing TODO noting that Identical() was overkill.

Fixes: cockroachdb#172870

Release note (bug fix): Fixed an internal error in the vectorized
execution engine that occurred when running queries with BIT or VARBIT
types (with width constraints) combined with GROUP BY and UNION ALL.
The error message was: 'unexpected replacement: original vector is
varbit[] whereas the replacement is varbit[]'.
@shivamshaw23
shivamshaw23 force-pushed the fix-172870-varbit-vectorized-replace-col branch from dc192c0 to 827669f Compare July 28, 2026 07:29
@blathers-crl

blathers-crl Bot commented Jul 28, 2026

Copy link
Copy Markdown

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@shivamshaw23

Copy link
Copy Markdown
Author

@cla-bot check

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

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vectorized execution internal error for BIT/VARBIT expressions with GROUP BY and UNION ALL

1 participant