1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Shared subquery evaluation logic
//!
//! This module contains core subquery evaluation functions that are shared between
//! `ExpressionEvaluator` and `CombinedExpressionEvaluator` to eliminate code duplication.
//!
//! ## Architecture
//!
//! The functions in this module implement the pure logic for subquery evaluation,
//! operating on already-executed query results (`Vec<Row>`). The evaluator-specific
//! code handles schema conversion and query execution, then delegates to these
//! shared functions for result processing.
//!
//! ## Refactoring Pattern
//!
//! This follows the DRY pattern established in #1427 and architectural plan from #1437:
//! - Extract common logic into static/standalone functions
//! - Both evaluators delegate to shared implementation
//! - Maintain identical SQL semantics across both evaluators
use Row;
use SqlValue;
use crateExecutorError;
/// Evaluate scalar subquery result - returns first row's single column value
///
/// ## SQL Semantics
///
/// Standard SQL (SQL:1999 Section 7.9) requires scalar subqueries to return exactly
/// one row and one column, erroring if multiple rows are returned.
///
/// However, SQLite takes a different approach: when a scalar subquery returns multiple
/// rows, SQLite uses the first row's value (officially "undefined" behavior per SQLite
/// docs, but consistently implemented). This is documented at:
/// https://www.sqlite.org/lang_expr.html#scalar_subqueries
///
/// VibeSQL follows SQLite's behavior for compatibility with SQLite test suites and
/// applications that rely on this behavior.
///
/// ## Behavior
/// - Zero rows: Returns NULL
/// - One row: Returns that row's single column value
/// - Multiple rows: Returns the first row's single column value (SQLite compatibility)
/// - Multiple columns: Returns error (both SQL standard and SQLite agree here)
///
/// ## Parameters
/// - `rows`: The result set from executing the subquery
///
/// ## Returns
/// - `Ok(SqlValue::Null)` if no rows returned
/// - `Ok(value)` the first row's single column value
/// - `Err(SubqueryColumnCountMismatch)` if not exactly one column
/// - `Err(ColumnIndexOutOfBounds)` if row doesn't have a value at index 0
/// Evaluate EXISTS predicate result
///
/// ## SQL Semantics (SQL:1999 Section 8.7)
/// - Returns TRUE if subquery returns at least one row
/// - Returns FALSE if subquery returns zero rows
/// - Never returns NULL (unlike most predicates)
/// - Negation applied after evaluation (NOT EXISTS)
///
/// ## Parameters
/// - `has_rows`: Whether the subquery returned any rows
/// - `negated`: Whether this is a NOT EXISTS predicate
///
/// ## Returns
/// - `SqlValue::Boolean(true)` if has_rows and not negated, or !has_rows and negated
/// - `SqlValue::Boolean(false)` otherwise
/// Evaluate quantified comparison result (ALL/ANY/SOME)
///
/// ## SQL Semantics (SQL:1999 Section 8.8)
///
/// ### ALL quantifier:
/// - Empty subquery: returns TRUE (vacuously true)
/// - Left value NULL: returns NULL
/// - Any comparison FALSE: returns FALSE immediately
/// - Any comparison NULL (and none FALSE): returns NULL
/// - All comparisons TRUE: returns TRUE
///
/// ### ANY/SOME quantifier:
/// - Empty subquery: returns FALSE (no rows to satisfy)
/// - Left value NULL: returns NULL
/// - Any comparison TRUE: returns TRUE immediately
/// - All comparisons FALSE: returns FALSE
/// - Any comparison NULL (and none TRUE): returns NULL
///
/// ## Parameters
/// - `left_val`: The left-hand value from the comparison expression
/// - `rows`: The result set from executing the subquery
/// - `op`: The comparison operator (=, <, >, etc.)
/// - `quantifier`: ALL, ANY, or SOME
/// - `eval_op`: Function to evaluate the binary operation (comparison)
///
/// ## Returns
/// - `Ok(SqlValue::Boolean)` with the quantified comparison result
/// - `Ok(SqlValue::Null)` if result is NULL per SQL three-valued logic
/// - `Err(SubqueryColumnCountMismatch)` if subquery doesn't return exactly one column
/// - `Err(...)` any error from evaluating the comparison operation