MD5 correlated scalar subquery duplicates hash results

Open
#176,221 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, sql
Domain
databases

Research direction

Reproduce the wrong result on a local single-node cluster with cockroach sql, using the md5_source and md5_guard tables and the two queries in the issue. Run EXPLAIN on both queries to see where the correlated scalar subquery is planned differently from the plain join. Done means the correlated MD5 query returns the same two distinct hashes as the control query, with a regression test covering this GROUP BY shape.

Written by the indexing model from the issue text.

Description

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

Describe the problem

A correlated scalar subquery containing MD5 duplicates hash results under GROUP BY. The direct query produces one hash for alpha and one for beta. The correlated version returns each hash twice.

To Reproduce

The issue reproduces with the following sequence:

  1. Start a CockroachDB v26.3.0 cluster and connect with cockroach sql.
  2. Create the source and guard tables and insert the values shown in Additional data and screenshots.
  3. Run the control MD5 query and observe two distinct hashes.
  4. Run the correlated MD5 query and observe that each hash appears twice.

Expected behavior

Both queries should return exactly two hashes. The correlated predicate uses the primary-key column input_value, so the scalar subquery returns exactly one MD5 value for each outer row.

Additional data and screenshots

The complete schema, test data, control query, affected query, and observed output are included below. No screenshot is required because the behavior is visible in the cockroach sql output.

Schema and test data
DROP TABLE IF EXISTS md5_guard;
DROP TABLE IF EXISTS md5_source;

CREATE TABLE md5_guard (
    guard_value STRING
);

CREATE TABLE md5_source (
    input_value STRING PRIMARY KEY
);

INSERT INTO md5_guard VALUES ('not-a-hash');
INSERT INTO md5_source VALUES ('alpha'), ('beta');
Control query
SELECT NULLIF(
           MD5(s.input_value),
           g.guard_value
       ) AS result
FROM md5_source AS s,
     md5_guard AS g
GROUP BY s.input_value, g.guard_value
ORDER BY result;
Control output
              result
----------------------------------
2c1743a391305fbf367df8e4f069f9f
987bcab01b929eb2c07877b224215c92
(2 rows)
Affected query
SELECT NULLIF(
           (
               SELECT MD5(x.input_value)
               FROM md5_source AS x
               WHERE x.input_value = s.input_value
           ),
           g.guard_value
       ) AS result
FROM md5_source AS s,
     md5_guard AS g
GROUP BY s.input_value, g.guard_value
ORDER BY result;
Actual output
              result
----------------------------------
2c1743a391305fbf367df8e4f069f9f
2c1743a391305fbf367df8e4f069f9f
987bcab01b929eb2c07877b224215c92
987bcab01b929eb2c07877b224215c92
(4 rows)

Environment:

  • CockroachDB version: CockroachDB CCL v26.3.0, built 2026-07-28
  • Server OS: macOS on Apple Silicon
  • CockroachDB build target: aarch64-apple-darwin21.2
  • Client application: cockroach sql
  • Connection: local single-node test cluster at 127.0.0.1:26257

Additional context

This is a silent wrong-result issue. The duplicated hashes can affect deduplication, grouping, and application logic that assumes one result for each primary-key row.

Jira issue: CRDB-71128

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.