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
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 61/100
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
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.
- CockroachDB: pkg/sql/sem/tree/datum.go#L6285 —
AdjustValueToType - CockroachDB: pkg/sql/colexec/colexecbase/cast.eg.go#L5119 —
castEnumStringOp.Next
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
cockroachdb/cockroach#176245 · 1 comment ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinPossibly taken @1AbdullahButt claimed this 14 days ago. OpenC-bug O-community X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cockroachdb/cockroach#176228 · 1 comment ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEMay be free again @darshpatel1052 claimed this 18 days ago, and no pull request is open. OpenC-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
cockroachdb/cockroach#176223 · 2 comments ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutPossibly taken @dajiaohuang claimed this 14 days ago. OpenC-bug O-community X-blathers-untriaged
Difficulty 1/5 Under an hour Newbie friendliness 84/100
cockroachdb/cockroach#176210 · 1 comment ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cockroachdb/cockroach#176150 · 1 comment ·
All issues in cockroachdb/cockroach
Similar issues
-
☢️ Bug v3
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area/import-export area/projects concern/ux
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
go-vikunja/vikunja#4455 · 1 comment ·
Maintainers usually reply within 2 days
-
Dependabot PRs no longer merge automaticallyPossibly taken @dwisiswant0 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
projectdiscovery/urlfinder#160 ·
Maintainers usually reply within 1 day
-
database
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
chnm/legal-modernism#450 ·
Maintainers usually reply within 1 day
-
tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day