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
// FILE: src/analysis/expr_ir.rs
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExprIr {
Literal(String),
ColumnRef(String),
FunctionCall {
name: String,
args: Vec<ExprIr>,
},
BinaryOp {
left: Box<ExprIr>,
op: String,
right: Box<ExprIr>,
},
Cast {
expr: Box<ExprIr>,
target_type: String,
},
Omitted, // Added to prevent positional shifting in incomplete expressions (e.g., arr[2:])
}
impl ExprIr {
pub fn is_volatile(&self) -> bool {
match self {
ExprIr::FunctionCall { name, args } => {
// Synthetic wrapper functions for nested expressions
// e.g. <case>, <array>, <between>, <slice>
if name.starts_with('<') && name.ends_with('>') {
return args.iter().any(|a| a.is_volatile());
}
const VOLATILE: &[&str] = &[
// Truly VOLATILE: return different values on every call
"clock_timestamp",
"timeofday",
"random",
"setseed",
"txid_current",
"txid_current_snapshot",
"txid_snapshot_xip",
"txid_snapshot_xmax",
"txid_snapshot_xmin",
"nextval",
"currval",
"lastval",
"setval",
"gen_random_uuid",
"uuid_generate_v1",
"uuid_generate_v1mc",
"uuid_generate_v4",
// Note: now(), current_timestamp, current_date, current_user,
// transaction_timestamp(), statement_timestamp() are all STABLE —
// they return the transaction start time and are constant within
// a statement. They do NOT require a table rewrite on PG11+.
];
VOLATILE.contains(&name.to_lowercase().as_str())
}
ExprIr::BinaryOp { left, right, .. } => left.is_volatile() || right.is_volatile(),
ExprIr::Cast { expr, .. } => expr.is_volatile(),
ExprIr::Literal(_) | ExprIr::ColumnRef(_) | ExprIr::Omitted => false,
}
}
}