apache / apache/datafusion

Scalar subquery (`Expr::ScalarSubquery`) does not fail when subquery is not scalar

Open
#16,134 3 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

When subquery is used in expression context, it must be a scalar subquery, i.e. it must produce a singe column and _at most_ one row.

### To Reproduce

```
SELECT
s.*,
-- not a scalar subquery
(SELECT * FROM (VALUES (1), (2)) t(x)) sub
FROM (VALUES ('a'), ('b')) s(a);
```

```
+---+-----+
| a | sub |
+---+-----+
| a | 1 |
| a | 2 |
| b | 1 |
| b | 2 |
+---+-----+
```

### Expected behavior

an error like the one produced by e.g. Snowflake "Single-row subquery returns more than one row."
or PostgreSQL - "more than one row returned by a subquery used as an expression"
or Trino - "Scalar sub-query has returned multiple rows"

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start from the Expr::ScalarSubquery implementation and reproduce the provided query to trace how multi-row results are handled in expression context. Add coverage for the two-row subquery and verify that execution returns a scalar-subquery error instead of expanding rows.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.