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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: Apache-2.0
use sqlparser::ast;
use crate::error::{Result, SqlError};
use crate::parser::normalize::{normalize_ident, normalize_object_name_checked};
use crate::resolver::expr::convert_value;
use crate::types::*;
pub(super) fn convert_value_rows(
columns: &[String],
rows: &[Vec<ast::Expr>],
) -> Result<Vec<Vec<(String, SqlValue)>>> {
rows.iter()
.map(|row| {
row.iter()
.enumerate()
.map(|(i, expr)| {
let col = columns.get(i).cloned().unwrap_or_else(|| format!("col{i}"));
let val = expr_to_sql_value(expr)?;
Ok((col, val))
})
.collect::<Result<Vec<_>>>()
})
.collect()
}
pub(super) fn expr_to_sql_value(expr: &ast::Expr) -> Result<SqlValue> {
match expr {
ast::Expr::Value(v) => convert_value(&v.value),
// Array literals lower element-wise into `SqlValue::Array`; there is
// no array-literal `SqlValue` the constant folder could produce.
ast::Expr::Array(ast::Array { elem, .. }) => {
let vals = elem.iter().map(expr_to_sql_value).collect::<Result<_>>()?;
Ok(SqlValue::Array(vals))
}
// `ST_Point(...)` / `ST_GeomFromGeoJSON(...)` synthesise a GeoJSON
// string in place rather than resolving as registered scalar
// functions, so they keep their bespoke handling.
ast::Expr::Function(func) => match super::spatial_ctor::try_spatial_constructor(func) {
Some(result) => result,
// Non-spatial functions (`now()`, `date_add(...)`, registered
// scalars) fold through the shared pipeline below.
None => fold_constant_value(expr),
},
// Everything else — `::TYPE` / `CAST(... AS TYPE)` casts, arithmetic,
// string concatenation, parenthesised literals — goes through the
// same resolver and constant folder the `SELECT` projection path
// uses, so the two surfaces never drift. Only genuinely row- or
// runtime-dependent expressions (column refs, subqueries, unknown
// functions) fail here.
_ => fold_constant_value(expr),
}
}
fn fold_constant_value(expr: &ast::Expr) -> Result<SqlValue> {
let sql_expr = crate::resolver::expr::convert_expr(expr)?;
super::const_fold::fold_constant_default(&sql_expr).ok_or_else(|| SqlError::Unsupported {
detail: format!("value expression: {expr}"),
})
}
pub(super) fn extract_table_name_from_table_with_joins(
table: &ast::TableWithJoins,
) -> Result<String> {
match &table.relation {
ast::TableFactor::Table { name, .. } => Ok(normalize_object_name_checked(name)?),
_ => Err(SqlError::Unsupported {
detail: "non-table target in DML".into(),
}),
}
}
/// Extract point-operation keys from WHERE clause (WHERE pk = literal OR pk IN (...)).
pub fn extract_point_keys(selection: Option<&ast::Expr>, info: &CollectionInfo) -> Vec<SqlValue> {
let pk = match &info.primary_key {
Some(pk) => pk.clone(),
None => return Vec::new(),
};
let expr = match selection {
Some(e) => e,
None => return Vec::new(),
};
let mut keys = Vec::new();
collect_pk_equalities(expr, &pk, &mut keys);
keys
}
fn collect_pk_equalities(expr: &ast::Expr, pk: &str, keys: &mut Vec<SqlValue>) {
match expr {
ast::Expr::BinaryOp {
left,
op: ast::BinaryOperator::Eq,
right,
} => {
if is_column(left, pk)
&& let Ok(v) = expr_to_sql_value(right)
{
keys.push(v);
} else if is_column(right, pk)
&& let Ok(v) = expr_to_sql_value(left)
{
keys.push(v);
}
}
ast::Expr::BinaryOp {
left,
op: ast::BinaryOperator::Or,
right,
} => {
collect_pk_equalities(left, pk, keys);
collect_pk_equalities(right, pk, keys);
}
ast::Expr::InList {
expr: inner,
list,
negated: false,
} if is_column(inner, pk) => {
for item in list {
if let Ok(v) = expr_to_sql_value(item) {
keys.push(v);
}
}
}
_ => {}
}
}
fn is_column(expr: &ast::Expr, name: &str) -> bool {
match expr {
ast::Expr::Identifier(ident) => normalize_ident(ident) == name,
// Three or more parts: schema.table.col — never matches a plain pk name.
ast::Expr::CompoundIdentifier(parts) if parts.len() >= 3 => false,
ast::Expr::CompoundIdentifier(parts) if parts.len() == 2 => {
normalize_ident(&parts[1]) == name
}
_ => false,
}
}
/// Build a `SqlPlan::VectorPrimaryInsert` from parsed rows.
///
/// Extracts the vector-field column into `vector: Vec<f32>` and collects
/// all remaining columns into `payload_fields`. Rows missing the vector
/// column are rejected.
pub(super) fn build_vector_primary_insert_plan(
collection: &str,
vpc: &nodedb_types::VectorPrimaryConfig,
_columns: &[String],
rows: Vec<Vec<(String, SqlValue)>>,
) -> Result<Vec<SqlPlan>> {
let mut result_rows = Vec::with_capacity(rows.len());
for row in rows {
let mut vector: Option<Vec<f32>> = None;
let mut payload_fields = std::collections::HashMap::new();
for (col, val) in row {
if col == vpc.vector_field {
match val {
SqlValue::Array(items) => {
let floats: Result<Vec<f32>> = items
.iter()
.map(|v| match v {
SqlValue::Float(f) => Ok(*f as f32),
SqlValue::Int(i) => Ok(*i as f32),
SqlValue::Decimal(d) => {
use rust_decimal::prelude::ToPrimitive;
d.to_f32().ok_or_else(|| SqlError::Parse {
detail: format!(
"vector element decimal '{d}' is out of f32 range"
),
})
}
other => Err(SqlError::Parse {
detail: format!(
"vector field must contain numbers, got {other:?}"
),
}),
})
.collect();
vector = Some(floats?);
}
other => {
return Err(SqlError::Parse {
detail: format!(
"vector field '{}' must be an array literal, got {other:?}",
vpc.vector_field
),
});
}
}
} else {
payload_fields.insert(col, val);
}
}
let vector = vector.ok_or_else(|| SqlError::Parse {
detail: format!(
"vector-primary INSERT missing required vector field '{}'",
vpc.vector_field
),
})?;
result_rows.push(VectorPrimaryRow {
surrogate: nodedb_types::Surrogate::ZERO,
vector,
payload_fields,
});
}
Ok(vec![SqlPlan::VectorPrimaryInsert {
collection: collection.to_string(),
field: vpc.vector_field.clone(),
quantization: vpc.quantization,
storage_dtype: vpc.storage_dtype,
payload_indexes: vpc.payload_indexes.clone(),
rows: result_rows,
}])
}
/// Build a `SqlPlan::KvInsert` from a VALUES clause. Shared by plain INSERT,
/// UPSERT, and `INSERT ... ON CONFLICT (key) DO UPDATE` — the three paths
/// differ only in `intent` and `on_conflict_updates`, never in how entries
/// are extracted from the row exprs.
///
/// `pk_col` is the schema-defined primary-key column name from
/// `CollectionInfo::primary_key`. When supplied, that column is used as
/// the KV key regardless of whether it is named `"key"`. Falls back to
/// the literal name `"key"` when `pk_col` is `None` (legacy / generic
/// KV collections that use the built-in key/value column convention).
pub(super) fn build_kv_insert_plan(
table_name: String,
columns: &[String],
rows_ast: &[Vec<ast::Expr>],
intent: KvInsertIntent,
on_conflict_updates: Vec<(String, SqlExpr)>,
pk_col: Option<&str>,
) -> Result<Vec<SqlPlan>> {
let key_col_name = pk_col.unwrap_or("key");
let key_idx = columns.iter().position(|c| c == key_col_name);
let ttl_idx = columns.iter().position(|c| c == "ttl");
// When using a named primary-key column (e.g. `k STRING PRIMARY KEY`), we
// store the key bytes in the KV key slot AND also keep the column in the
// value map. This allows scan filters on the primary-key column (e.g.
// `WHERE k = 'x'`) and projection (e.g. `SELECT k FROM ...`) to work
// without teaching the KV scan handler to inspect the raw key bytes.
// The only column we exclude from the value map is the built-in `"key"`
// sentinel (used by raw key/value KV collections) and `"ttl"`.
let exclude_from_value: std::collections::HashSet<usize> = {
let mut s = std::collections::HashSet::new();
// Exclude the raw "key" sentinel column (not a named PK column).
if key_col_name == "key"
&& let Some(idx) = key_idx
{
s.insert(idx);
}
if let Some(idx) = ttl_idx {
s.insert(idx);
}
s
};
let mut entries = Vec::with_capacity(rows_ast.len());
let mut ttl_secs: u64 = 0;
for row_exprs in rows_ast {
let key_val = match key_idx {
Some(idx) => expr_to_sql_value(&row_exprs[idx])?,
None => SqlValue::String(String::new()),
};
if let Some(idx) = ttl_idx {
match expr_to_sql_value(&row_exprs[idx]) {
Ok(SqlValue::Int(n)) => ttl_secs = n.max(0) as u64,
Ok(SqlValue::Float(f)) => ttl_secs = f.max(0.0) as u64,
_ => {}
}
}
let value_cols: Vec<(String, SqlValue)> = columns
.iter()
.enumerate()
.filter(|(i, _)| !exclude_from_value.contains(i))
.map(|(i, col)| {
let val = expr_to_sql_value(&row_exprs[i])?;
Ok((col.clone(), val))
})
.collect::<Result<Vec<_>>>()?;
entries.push((key_val, value_cols));
}
Ok(vec![SqlPlan::KvInsert {
collection: table_name,
entries,
ttl_secs,
intent,
on_conflict_updates,
}])
}