nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Main SQL execution entry point with transaction handling and DDL dispatch.

use std::sync::Arc;

use pgwire::api::results::{DataRowEncoder, QueryResponse, Response, Tag};
use pgwire::error::{ErrorInfo, PgWireError, PgWireResult};

use crate::control::security::identity::AuthenticatedIdentity;

use super::super::session::TransactionState;
use super::super::types::text_field;
use super::core::NodeDbPgHandler;

impl NodeDbPgHandler {
    /// Execute a SQL query: session state → identity → DDL check → quota → plan → perms → dispatch.
    pub(super) async fn execute_sql(
        &self,
        identity: &AuthenticatedIdentity,
        addr: &std::net::SocketAddr,
        sql: &str,
    ) -> PgWireResult<Vec<Response>> {
        use super::super::types::error_to_sqlstate;

        let sql_trimmed = sql.trim();
        let upper = sql_trimmed.to_uppercase();

        self.sessions.ensure_session(*addr);

        if sql_trimmed.is_empty() || sql_trimmed == ";" {
            return Ok(vec![Response::EmptyQuery]);
        }

        if upper == "BEGIN" || upper == "BEGIN TRANSACTION" || upper == "START TRANSACTION" {
            // Capture current WAL LSN as the snapshot point for this transaction.
            let snapshot_lsn = {
                let next = self.state.wal.next_lsn();
                crate::types::Lsn::new(next.as_u64().saturating_sub(1))
            };
            self.sessions.begin(addr, snapshot_lsn).map_err(|msg| {
                PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "25P02".to_owned(),
                    msg.to_owned(),
                )))
            })?;
            return Ok(vec![Response::Execution(Tag::new("BEGIN"))]);
        }

        if upper == "COMMIT" || upper == "END" || upper == "END TRANSACTION" {
            // Snapshot isolation: check for write conflicts before committing.
            // If any collection read during this transaction has been modified
            // since the snapshot LSN, reject with serialization failure.
            let read_set = self.sessions.take_read_set(addr);
            if let Some(snapshot_lsn) = self.sessions.snapshot_lsn(addr) {
                let current_lsn = self.state.wal.next_lsn();
                let current = crate::types::Lsn::new(current_lsn.as_u64().saturating_sub(1));
                for (_collection, _doc_id, read_lsn) in &read_set {
                    if current > *read_lsn && current > snapshot_lsn {
                        // WAL advanced past what we read — concurrent write detected.
                        self.sessions.rollback(addr).ok();
                        return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                            "ERROR".to_owned(),
                            "40001".to_owned(),
                            "could not serialize access due to concurrent update".to_owned(),
                        ))));
                    }
                }
            }

            let buffered = self.sessions.commit(addr).map_err(|msg| {
                PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "25000".to_owned(),
                    msg.to_owned(),
                )))
            })?;

            if !buffered.is_empty() {
                let tenant_id = identity.tenant_id;
                let vshard_id = buffered[0].vshard_id;

                let mut sub_records: Vec<(u16, Vec<u8>)> = Vec::with_capacity(buffered.len());
                for task in &buffered {
                    if let Some(entry) = crate::control::wal_replication::to_replicated_entry(
                        task.tenant_id,
                        task.vshard_id,
                        &task.plan,
                    ) {
                        let bytes = entry.to_bytes();
                        sub_records.push((nodedb_wal::record::RecordType::Put as u16, bytes));
                    }
                }

                if !sub_records.is_empty() {
                    let tx_payload = rmp_serde::to_vec_named(&sub_records).map_err(|e| {
                        PgWireError::UserError(Box::new(ErrorInfo::new(
                            "ERROR".to_owned(),
                            "XX000".to_owned(),
                            format!("transaction WAL serialization failed: {e}"),
                        )))
                    })?;
                    self.state
                        .wal
                        .append_transaction(tenant_id, vshard_id, &tx_payload)
                        .map_err(|e| {
                            PgWireError::UserError(Box::new(ErrorInfo::new(
                                "ERROR".to_owned(),
                                "XX000".to_owned(),
                                format!("transaction WAL append failed: {e}"),
                            )))
                        })?;
                }

                // Dispatch all writes as a single TransactionBatch for
                // atomic execution on the Data Plane.
                let plans: Vec<crate::bridge::envelope::PhysicalPlan> =
                    buffered.iter().map(|t| t.plan.clone()).collect();
                let batch_task = crate::control::planner::physical::PhysicalTask {
                    tenant_id,
                    vshard_id,
                    plan: crate::bridge::envelope::PhysicalPlan::Meta(
                        crate::bridge::physical_plan::MetaOp::TransactionBatch { plans },
                    ),
                };
                if let Err(e) = self.dispatch_task_no_wal(batch_task).await {
                    tracing::warn!(error = %e, "transaction batch dispatch failed");
                    return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                        "ERROR".to_owned(),
                        "40001".to_owned(),
                        format!("transaction commit failed: {e}"),
                    ))));
                }
            }

            return Ok(vec![Response::Execution(Tag::new("COMMIT"))]);
        }

        if upper == "ROLLBACK" || upper == "ABORT" {
            let _ = self.sessions.rollback(addr);
            return Ok(vec![Response::Execution(Tag::new("ROLLBACK"))]);
        }

        if upper.starts_with("SAVEPOINT ") {
            let sp_name = sql_trimmed
                .split_whitespace()
                .nth(1)
                .unwrap_or("sp")
                .to_string();
            self.sessions.create_savepoint(addr, sp_name);
            return Ok(vec![Response::Execution(Tag::new("SAVEPOINT"))]);
        }

        if upper.starts_with("RELEASE SAVEPOINT ") || upper.starts_with("RELEASE ") {
            let sp_name = sql_trimmed
                .split_whitespace()
                .last()
                .unwrap_or("sp")
                .to_string();
            self.sessions.release_savepoint(addr, &sp_name);
            return Ok(vec![Response::Execution(Tag::new("RELEASE"))]);
        }

        // DECLARE cursor_name CURSOR FOR SELECT ...
        if upper.starts_with("DECLARE ") && upper.contains(" CURSOR ") {
            let parts: Vec<&str> = sql_trimmed.split_whitespace().collect();
            let cursor_name = parts.get(1).unwrap_or(&"default").to_string();
            // Extract the SQL after "CURSOR FOR"
            if let Some(for_pos) = upper.find(" FOR ") {
                let inner_sql = sql_trimmed[for_pos + 5..].trim();
                // Execute the inner query to get results.
                match self
                    .execute_query_for_cursor(addr, inner_sql, identity)
                    .await
                {
                    Ok(rows) => {
                        self.sessions.declare_cursor(addr, cursor_name, rows);
                        return Ok(vec![Response::Execution(Tag::new("DECLARE CURSOR"))]);
                    }
                    Err(e) => return Err(e),
                }
            }
            return Ok(vec![Response::Execution(Tag::new("DECLARE CURSOR"))]);
        }

        // FETCH [count] FROM cursor_name
        if upper.starts_with("FETCH ") {
            let parts: Vec<&str> = sql_trimmed.split_whitespace().collect();
            let (count, cursor_name) = if parts.len() >= 4 && parts[2].eq_ignore_ascii_case("FROM")
            {
                let n = parts[1].parse::<usize>().unwrap_or(1);
                (n, parts[3].to_string())
            } else if parts.len() >= 3 && parts[1].eq_ignore_ascii_case("FROM") {
                (1, parts[2].to_string())
            } else {
                (1, parts.get(1).unwrap_or(&"default").to_string())
            };

            match self.sessions.fetch_cursor(addr, &cursor_name, count) {
                Ok((rows, _exhausted)) => {
                    let schema = std::sync::Arc::new(vec![text_field("result")]);
                    let mut encoded_rows = Vec::with_capacity(rows.len());
                    for row_json in &rows {
                        let mut encoder = DataRowEncoder::new(schema.clone());
                        let _ = encoder.encode_field(row_json);
                        encoded_rows.push(Ok(encoder.take_row()));
                    }
                    return Ok(vec![Response::Query(QueryResponse::new(
                        schema,
                        futures::stream::iter(encoded_rows),
                    ))]);
                }
                Err(msg) => {
                    return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                        "ERROR".to_owned(),
                        "34000".to_owned(),
                        msg.to_string(),
                    ))));
                }
            }
        }

        // CLOSE cursor_name
        if upper.starts_with("CLOSE ") {
            let cursor_name = sql_trimmed
                .split_whitespace()
                .nth(1)
                .unwrap_or("default")
                .to_string();
            self.sessions.close_cursor(addr, &cursor_name);
            return Ok(vec![Response::Execution(Tag::new("CLOSE CURSOR"))]);
        }

        if upper.starts_with("ROLLBACK TO ") {
            let sp_name = sql_trimmed
                .split_whitespace()
                .last()
                .unwrap_or("sp")
                .to_string();
            if let Err(msg) = self.sessions.rollback_to_savepoint(addr, &sp_name) {
                return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "3B001".to_owned(),
                    msg.to_string(),
                ))));
            }
            return Ok(vec![Response::Execution(Tag::new("ROLLBACK"))]);
        }

        if self.sessions.transaction_state(addr) == TransactionState::Failed {
            return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                "ERROR".to_owned(),
                "25P02".to_owned(),
                "current transaction is aborted, commands ignored until end of transaction block"
                    .to_owned(),
            ))));
        }

        if upper.starts_with("SET ") {
            return self.handle_set(addr, sql_trimmed);
        }

        if upper == "SHOW CONNECTIONS" {
            let schema = Arc::new(vec![
                text_field("peer_address"),
                text_field("transaction_state"),
            ]);
            let sessions = self.sessions.all_sessions();
            let mut rows = Vec::with_capacity(sessions.len());
            let mut encoder = DataRowEncoder::new(schema.clone());
            for (addr_str, tx_state) in &sessions {
                encoder.encode_field(addr_str)?;
                encoder.encode_field(tx_state)?;
                rows.push(Ok(encoder.take_row()));
            }
            return Ok(vec![Response::Query(QueryResponse::new(
                schema,
                futures::stream::iter(rows),
            ))]);
        }

        if upper.starts_with("KILL CONNECTION ") {
            if !identity.is_superuser {
                return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "42501".to_owned(),
                    "permission denied: only superuser can kill connections".to_owned(),
                ))));
            }
            let target = sql_trimmed[16..]
                .trim()
                .trim_matches('\'')
                .trim_matches('"');
            if let Ok(target_addr) = target.parse::<std::net::SocketAddr>() {
                self.sessions.remove(&target_addr);
                return Ok(vec![Response::Execution(Tag::new("KILL"))]);
            }
            return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
                "ERROR".to_owned(),
                "42601".to_owned(),
                format!("invalid connection address: '{target}'. Use SHOW CONNECTIONS to list."),
            ))));
        }

        if upper.starts_with("SHOW ")
            && !upper.starts_with("SHOW USERS")
            && !upper.starts_with("SHOW TENANTS")
            && !upper.starts_with("SHOW SESSION")
            && !upper.starts_with("SHOW CLUSTER")
            && !upper.starts_with("SHOW RAFT")
            && !upper.starts_with("SHOW MIGRATIONS")
            && !upper.starts_with("SHOW PEER")
            && !upper.starts_with("SHOW NODES")
            && !upper.starts_with("SHOW NODE ")
            && !upper.starts_with("SHOW COLLECTIONS")
            && !upper.starts_with("SHOW AUDIT")
            && !upper.starts_with("SHOW PERMISSIONS")
            && !upper.starts_with("SHOW GRANTS")
            && upper != "SHOW CONNECTIONS"
            && !upper.starts_with("SHOW INDEXES")
        {
            return self.handle_show(addr, sql_trimmed);
        }

        if upper.starts_with("RESET ") {
            let param = sql_trimmed[6..].trim().to_lowercase();
            self.sessions.set_parameter(addr, param, String::new());
            return Ok(vec![Response::Execution(Tag::new("RESET"))]);
        }

        if upper == "DISCARD ALL" {
            self.sessions.remove(addr);
            self.sessions.ensure_session(*addr);
            return Ok(vec![Response::Execution(Tag::new("DISCARD ALL"))]);
        }

        if upper.starts_with("EXPLAIN ") {
            return self.handle_explain(identity, sql_trimmed).await;
        }

        if let Some(result) = super::super::ddl::dispatch(&self.state, identity, sql_trimmed).await
        {
            return result;
        }

        let tenant_id = identity.tenant_id;

        self.state.check_tenant_quota(tenant_id).map_err(|e| {
            let (severity, code, message) = error_to_sqlstate(&e);
            PgWireError::UserError(Box::new(ErrorInfo::new(
                severity.to_owned(),
                code.to_owned(),
                message,
            )))
        })?;

        self.state.tenant_request_start(tenant_id);
        let result = self
            .execute_planned_sql(identity, sql_trimmed, tenant_id, addr)
            .await;
        self.state.tenant_request_end(tenant_id);

        if result.is_err() {
            self.sessions.fail_transaction(addr);
        }

        result
    }

    /// Execute a SELECT query and return results as JSON strings for cursor storage.
    async fn execute_query_for_cursor(
        &self,
        _addr: &std::net::SocketAddr,
        sql: &str,
        identity: &AuthenticatedIdentity,
    ) -> PgWireResult<Vec<String>> {
        let tenant_id = identity.tenant_id;
        let query_ctx = crate::control::planner::context::QueryContext::with_catalog(
            std::sync::Arc::clone(&self.state.credentials),
            tenant_id.as_u32(),
        );

        let auth_ctx = crate::control::server::session_auth::build_auth_context(identity);
        let tasks = query_ctx
            .plan_sql_with_rls(sql, tenant_id, &auth_ctx, &self.state.rls)
            .await
            .map_err(|e| {
                PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "42000".to_owned(),
                    e.to_string(),
                )))
            })?;

        let mut rows = Vec::new();
        for task in tasks {
            let resp = crate::control::server::dispatch_utils::dispatch_to_data_plane(
                &self.state,
                task.tenant_id,
                task.vshard_id,
                task.plan,
                0,
            )
            .await
            .map_err(|e| {
                PgWireError::UserError(Box::new(ErrorInfo::new(
                    "ERROR".to_owned(),
                    "XX000".to_owned(),
                    e.to_string(),
                )))
            })?;

            if !resp.payload.is_empty() {
                let json =
                    crate::data::executor::response_codec::decode_payload_to_json(&resp.payload);
                rows.push(json);
            }
        }
        Ok(rows)
    }
}