sql: casting an ENUM value to CHAR(n) keeps a trailing space after truncation in the vectorized engine but not in the row-by-row engine

Open
#176,194 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
61/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, postgresql, sql
Domain
backend, databases

Research direction

Start with the vectorized toString template in pkg/sql/colexec/execgen/cmd/execgen/cast_gen_util.go and compare its trimming and truncation steps with performCastWithoutPrecisionTruncation in pkg/sql/sem/eval/cast.go and the bpchar branch of AdjustValueToType in pkg/sql/sem/tree/datum.go. Reproduce with the SQL in the issue, toggling vectorize off and on. Done when both modes return abX for the CHAR(3) cast concatenation.

Written by the indexing model from the issue text.

Description

C-bug O-community T-sql-queries X-blathers-triaged X-move-internal

Describe the problem

When an ENUM (or string) value is cast to CHAR(n) and the truncation to n characters leaves a space as the last character (for example SELECT (v::CHAR(3)) || 'X' FROM t with the enum value 'ab c'), the row-by-row engine strips that trailing space and the concatenation gives abX. The vectorized engine keeps the space and gives ab X. Trailing spaces of CHAR(n) values are semantically insignificant and are removed when converting to another string type, so the vectorized engine should also return abX.

To Reproduce

Deployment: a single-node CockroachDB cluster started with cockroach start-single-node --insecure (tested: v26.2.7, 1 node, default settings except sql.stats.automatic_collection.enabled = false). Run the script in an empty database. The script runs the query twice in one session: first with vectorize = off (row-by-row engine), then with vectorize = on (vectorized engine).

SET TIME ZONE 'UTC';
CREATE TYPE e_char AS ENUM ('ab c');
CREATE TABLE t (v e_char);
INSERT INTO t VALUES ('ab c');

-- row-by-row engine
SET vectorize = off;
EXPLAIN (VERBOSE) SELECT (v::CHAR(3)) || 'X' FROM t;
SELECT (v::CHAR(3)) || 'X' FROM t;
-- vectorized engine
SET vectorize = on;
EXPLAIN (VERBOSE) SELECT (v::CHAR(3)) || 'X' FROM t;
SELECT (v::CHAR(3)) || 'X' FROM t;

Expected behavior

abX, as in the row-by-row engine. The PostgreSQL manual (datatype.sgml, character types) says: "Trailing spaces are removed when converting a character value to one of the other string types.", and, in the same section, "trailing spaces are treated as semantically insignificant". The CockroachDB docs (string.md) only say that a CHAR(n) value shorter than the limit is padded with spaces; they do not say whether trailing spaces are removed when converting to another string type. For reference, PostgreSQL 18.6 returns abX for the same query.

PostgreSQL 18.6 (Debian 18.6-1.pgdg12+2):

SELECT (v::CHAR(3)) || 'X' FROM t;
 ?column? 
----------
 abX
(1 row)

Additional data / screenshots

With SET vectorize = off (row-by-row engine) the query returns one text row, abX. With SET vectorize = on (vectorized engine) it returns one text row, ab X.

-- row-by-row engine
EXPLAIN (VERBOSE) SELECT (v::CHAR(3)) || 'X' FROM t;
                       info                       
--------------------------------------------------
 distribution: local
 vectorized: false
 
 • render
 │ columns: ("?column?")
 │ render ?column?: v::CHAR(3) || 'X'
 │
 └── • scan
       columns: (v)
       estimated row count: 1,000 (missing stats)
       table: t@t_pkey
       spans: FULL SCAN
(12 rows)

SELECT (v::CHAR(3)) || 'X' FROM t;
 ?column? 
----------
 abX
(1 row)

-- vectorized engine
EXPLAIN (VERBOSE) SELECT (v::CHAR(3)) || 'X' FROM t;
                       info                       
--------------------------------------------------
 distribution: local
 vectorized: true
 
 • render
 │ columns: ("?column?")
 │ render ?column?: v::CHAR(3) || 'X'
 │
 └── • scan
       columns: (v)
       estimated row count: 1,000 (missing stats)
       table: t@t_pkey
       spans: FULL SCAN
(12 rows)

SELECT (v::CHAR(3)) || 'X' FROM t;
 ?column? 
----------
 ab X
(1 row)

Environment:

  • CockroachDB version:
CockroachDB CCL v26.2.7-dev-8652ac22d9f90501e48ae4f24efd7faab33cf7c5 (x86_64-pc-linux-gnu, built 2026/10/23 21:35:41, go1.25.13)
  • Server OS: Linux 6.8.0-138-generic x86_64, AMD EPYC 7J13 64-Core Processor
  • Client app: psql

Additional context

In the row-by-row engine, performCastWithoutPrecisionTruncation (pkg/sql/sem/eval/cast.go:522-533) strips trailing spaces for bpchar before truncating to the width, and AdjustValueToType (pkg/sql/sem/tree/datum.go:6266-6319, the bpchar branch at lines 6282-6285) strips them again after the truncation. The vectorized toString template (pkg/sql/colexec/execgen/cmd/execgen/cast_gen_util.go:526-552) is documented as "a combination of the relevant pieces from eval.performCastWithoutPrecisionTruncation as well as from tree.AdjustValueToType", but it applies bytes.TrimRight(..., " ") only before the truncation and nothing afterwards, so a space that the truncation leaves at the end stays in the result.

Found with AI assistance.

Jira issue: CRDB-70789

Dominant language
Go
Stars
32.6k
Forks
4.2k
PR merge metrics
No merged PRs in 30d

Getting set up

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 cockroachdb/cockroach

All issues in cockroachdb/cockroach

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.