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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SPDX-License-Identifier: BUSL-1.1
//! Integration tests for UPDATE RETURNING and DELETE RETURNING via the pgwire
//! simple-query and extended-query protocols.
//!
//! Each test spins up a fresh single-core server, performs DML with a
//! RETURNING clause, and asserts that the correct columns and values come
//! back as a multi-column row result.
mod common;
use common::pgwire_harness::TestServer;
use tokio_postgres::types::Type;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/// Set up a schemaless collection with a single document {id, name, score}.
async fn seed_docs(server: &TestServer) {
server
.exec("CREATE COLLECTION items TYPE DOCUMENT (id STRING, name STRING, score INT)")
.await
.unwrap();
server
.exec("INSERT INTO items (id, name, score) VALUES ('a', 'alpha', 10)")
.await
.unwrap();
server
.exec("INSERT INTO items (id, name, score) VALUES ('b', 'beta', 20)")
.await
.unwrap();
server
.exec("INSERT INTO items (id, name, score) VALUES ('c', 'gamma', 30)")
.await
.unwrap();
}
// ---------------------------------------------------------------------------
// UPDATE RETURNING *
// ---------------------------------------------------------------------------
/// Point UPDATE with `RETURNING *` must return all fields of the post-update
/// document as a multi-column row.
#[tokio::test]
async fn point_update_returning_star() {
let server = TestServer::start().await;
seed_docs(&server).await;
let rows = server
.query_rows("UPDATE items SET score = 99 WHERE id = 'a' RETURNING *")
.await
.expect("UPDATE RETURNING * should succeed");
assert_eq!(rows.len(), 1, "expected exactly one returned row");
// The row must contain the updated score.
let row = &rows[0];
let joined = row.join(",");
assert!(
joined.contains("99"),
"returned row must reflect updated score=99, got: {joined}"
);
assert!(
joined.contains("alpha"),
"returned row must retain name=alpha, got: {joined}"
);
}
// ---------------------------------------------------------------------------
// UPDATE RETURNING named columns
// ---------------------------------------------------------------------------
/// Point UPDATE with named column list must return only the requested columns,
/// in spec order, using the alias where provided.
#[tokio::test]
async fn point_update_returning_named_columns() {
let server = TestServer::start().await;
seed_docs(&server).await;
let rows = server
.query_rows("UPDATE items SET score = 55 WHERE id = 'b' RETURNING id, score AS new_score")
.await
.expect("UPDATE RETURNING named should succeed");
assert_eq!(rows.len(), 1, "expected one row");
// Two columns: id, new_score.
let row = &rows[0];
assert_eq!(row.len(), 2, "expected 2 columns, got {}", row.len());
assert_eq!(row[0], "b", "first column (id) must be 'b'");
assert_eq!(row[1], "55", "second column (new_score) must be '55'");
}
// ---------------------------------------------------------------------------
// DELETE RETURNING *
// ---------------------------------------------------------------------------
/// Point DELETE with `RETURNING *` must return the pre-deletion document as a
/// multi-column row.
#[tokio::test]
async fn point_delete_returning_star() {
let server = TestServer::start().await;
seed_docs(&server).await;
let rows = server
.query_rows("DELETE FROM items WHERE id = 'c' RETURNING *")
.await
.expect("DELETE RETURNING * should succeed");
assert_eq!(rows.len(), 1, "expected one returned row for deleted doc");
let row = &rows[0];
let joined = row.join(",");
assert!(
joined.contains("gamma"),
"returned row must contain pre-deletion name=gamma, got: {joined}"
);
assert!(
joined.contains("30"),
"returned row must contain pre-deletion score=30, got: {joined}"
);
}
// ---------------------------------------------------------------------------
// DELETE RETURNING named columns
// ---------------------------------------------------------------------------
/// Point DELETE with named RETURNING columns must return only those columns.
#[tokio::test]
async fn point_delete_returning_named_columns() {
let server = TestServer::start().await;
seed_docs(&server).await;
let rows = server
.query_rows("DELETE FROM items WHERE id = 'a' RETURNING id, name")
.await
.expect("DELETE RETURNING named should succeed");
assert_eq!(rows.len(), 1, "expected one row");
let row = &rows[0];
assert_eq!(row.len(), 2, "expected 2 columns, got {}", row.len());
assert_eq!(row[0], "a");
assert_eq!(row[1], "alpha");
}
// ---------------------------------------------------------------------------
// Bulk UPDATE RETURNING
// ---------------------------------------------------------------------------
/// Bulk UPDATE (no WHERE clause) with RETURNING * must return one row per
/// matched document, each reflecting the post-update value.
#[tokio::test]
async fn bulk_update_returning() {
let server = TestServer::start().await;
seed_docs(&server).await;
let rows = server
.query_rows("UPDATE items SET score = 0 RETURNING id, score")
.await
.expect("bulk UPDATE RETURNING should succeed");
// All three documents must be returned.
assert_eq!(
rows.len(),
3,
"expected 3 returned rows, got {}",
rows.len()
);
// Every returned row must show score = 0.
for row in &rows {
assert_eq!(
row.len(),
2,
"each row must have 2 columns, got {}",
row.len()
);
assert_eq!(
row[1], "0",
"updated score must be 0 in every row, got {}",
row[1]
);
}
}
// ---------------------------------------------------------------------------
// Bulk DELETE RETURNING
// ---------------------------------------------------------------------------
/// Bulk DELETE with a WHERE clause that matches two rows must return two
/// pre-deletion documents.
#[tokio::test]
async fn bulk_delete_returning() {
let server = TestServer::start().await;
seed_docs(&server).await;
// Delete the two rows with score >= 20.
let rows = server
.query_rows("DELETE FROM items WHERE score >= 20 RETURNING id, score")
.await
.expect("bulk DELETE RETURNING should succeed");
assert_eq!(rows.len(), 2, "expected 2 deleted rows, got {}", rows.len());
// Collect returned ids.
let mut ids: Vec<&str> = rows.iter().map(|r| r[0].as_str()).collect();
ids.sort_unstable();
assert_eq!(ids, ["b", "c"], "returned ids must be b and c");
}
// ---------------------------------------------------------------------------
// Extended-query (prepared statement) RETURNING *
// ---------------------------------------------------------------------------
/// RETURNING * via the extended-query protocol (parameterised prepared
/// statement) must surface multi-column rows, not a JSON envelope.
#[tokio::test]
async fn extended_query_update_returning_star() {
let server = TestServer::start().await;
seed_docs(&server).await;
// Use the extended-query (prepared-statement) path via tokio-postgres
// `.query()` which drives Parse / Bind / Execute.
let stmt = server
.client
.prepare_typed(
"UPDATE items SET score = $1 WHERE id = $2 RETURNING *",
&[Type::UNKNOWN, Type::UNKNOWN],
)
.await
.expect("prepare should succeed");
let rows = server
.client
.query(&stmt, &[&"77", &"b"])
.await
.expect("prepared UPDATE RETURNING should succeed");
assert_eq!(rows.len(), 1, "expected one row");
// Must have at least 2 columns (id + score at minimum).
assert!(
rows[0].len() >= 2,
"row must expose multiple columns, got {}",
rows[0].len()
);
}
// ---------------------------------------------------------------------------
// Arithmetic expression in RETURNING — error path
// ---------------------------------------------------------------------------
/// A RETURNING clause containing an arithmetic expression must be rejected
/// with a typed error. NodeDB only supports column references and aliases
/// in RETURNING, not computed expressions.
#[tokio::test]
async fn returning_arithmetic_expression_rejected() {
let server = TestServer::start().await;
seed_docs(&server).await;
server
.expect_error(
"UPDATE items SET score = 1 WHERE id = 'a' RETURNING score + 1",
"not supported",
)
.await;
}