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
// SPDX-License-Identifier: BUSL-1.1
//! Integration coverage for integer overflow handling in SQL expressions.
//!
//! The const-folder and procedural executor must detect integer overflow
//! and return an error rather than panicking (debug) or silently wrapping
//! (release). Float divide-by-zero must return an error, not ±Inf.
mod common;
use common::pgwire_harness::TestServer;
// ---------------------------------------------------------------------------
// Const-folder overflow (nodedb-sql planner)
// ---------------------------------------------------------------------------
/// `i64::MAX + 1` in a constant expression must not panic (debug) or wrap
/// to a negative number (release). An error or null are both acceptable.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn const_fold_addition_overflow_does_not_wrap() {
let server = TestServer::start().await;
let result = server.query_text("SELECT 9223372036854775807 + 1").await;
match result {
Err(_) => { /* error is acceptable */ }
Ok(rows) => {
if let Some(val) = rows.first() {
assert!(
!val.contains("-9223372036854775808"),
"i64::MAX + 1 must not silently wrap to i64::MIN: got {val}"
);
}
}
}
}
/// `i64::MAX * 2` must not panic or wrap.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn const_fold_multiplication_overflow_does_not_wrap() {
let server = TestServer::start().await;
let result = server.query_text("SELECT 9223372036854775807 * 2").await;
match result {
Err(_) => { /* error is acceptable */ }
Ok(rows) => {
if let Some(val) = rows.first() {
// Wrapped value would be -2.
assert!(
!val.contains("\"-2\""),
"i64::MAX * 2 must not silently wrap: got {val}"
);
}
}
}
}
/// `i64::MIN - 1` must not panic or wrap.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn const_fold_subtraction_overflow_does_not_wrap() {
let server = TestServer::start().await;
let result = server.query_text("SELECT -9223372036854775808 - 1").await;
match result {
Err(_) => { /* error is acceptable */ }
Ok(rows) => {
if let Some(val) = rows.first() {
// Wrapped value would be i64::MAX = 9223372036854775807.
assert!(
!val.contains("9223372036854775807"),
"i64::MIN - 1 must not silently wrap to i64::MAX: got {val}"
);
}
}
}
}
// ---------------------------------------------------------------------------
// Const-folder in INSERT context
// ---------------------------------------------------------------------------
/// Overflow in an INSERT VALUES expression should be caught before storage.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn const_fold_overflow_in_insert_values() {
let server = TestServer::start().await;
server
.exec(
"CREATE COLLECTION overflow_tbl (\
id TEXT PRIMARY KEY, \
v BIGINT) WITH (engine='document_strict')",
)
.await
.unwrap();
let result = server
.exec("INSERT INTO overflow_tbl (id, v) VALUES ('k', 9223372036854775807 * 2)")
.await;
assert!(
result.is_err(),
"INSERT with overflowing constant should fail, not store wrapped value"
);
}
// ---------------------------------------------------------------------------
// Procedural executor overflow (triggers / DO blocks)
// ---------------------------------------------------------------------------
/// Integer overflow in a DO block's variable arithmetic should error, not
/// panic or silently wrap.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn procedural_eval_integer_overflow_returns_error() {
let server = TestServer::start().await;
server
.exec("CREATE COLLECTION proc_ov (id TEXT PRIMARY KEY, v BIGINT) WITH (engine='document_strict')")
.await
.unwrap();
// A DO block (or procedure) that overflows during evaluation.
let result = server
.exec(
"DO $$ \
DECLARE x BIGINT := 9223372036854775807; \
BEGIN \
x := x + 1; \
INSERT INTO proc_ov (id, v) VALUES ('k', x); \
END $$",
)
.await;
assert!(
result.is_err(),
"integer overflow in procedural block should error, not wrap"
);
}
/// `i64::MIN / -1` is undefined behavior in two's complement and must error.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn procedural_eval_min_div_neg1_returns_error() {
let server = TestServer::start().await;
let result = server
.exec(
"DO $$ \
DECLARE x BIGINT := -9223372036854775808; \
DECLARE y BIGINT; \
BEGIN \
y := x / -1; \
END $$",
)
.await;
assert!(
result.is_err(),
"i64::MIN / -1 in procedural block should error, not panic"
);
}
/// Float divide by negative zero should return an error or NULL, not ±Inf.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn procedural_eval_float_div_neg_zero() {
let server = TestServer::start().await;
server
.exec(
"CREATE COLLECTION fdiv (id TEXT PRIMARY KEY, v FLOAT) WITH (engine='document_strict')",
)
.await
.unwrap();
// -0.0 passes the `!= 0.0` guard in f64 comparison but produces -inf.
let result = server
.exec(
"DO $$ \
DECLARE a FLOAT := 1.0; \
DECLARE b FLOAT := -0.0; \
DECLARE c FLOAT; \
BEGIN \
c := a / b; \
INSERT INTO fdiv (id, v) VALUES ('k', c); \
END $$",
)
.await;
// Either error, or if it succeeds, the stored value must not be infinity.
if result.is_ok() {
let rows = server
.query_text("SELECT v FROM fdiv WHERE id = 'k'")
.await
.unwrap();
if let Some(val) = rows.first() {
assert!(
!val.to_lowercase().contains("inf"),
"float / -0.0 should not produce infinity: got {val}"
);
}
}
}