-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecution_paths.rs
More file actions
595 lines (553 loc) · 22.8 KB
/
execution_paths.rs
File metadata and controls
595 lines (553 loc) · 22.8 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use actix_web::{HttpRequest, HttpResponse};
use bytes::Bytes;
use kalamdb_commons::models::{NamespaceId, Username};
use kalamdb_commons::schemas::TableType;
use kalamdb_core::app_context::AppContext;
use kalamdb_core::schema_registry::SchemaRegistry;
use kalamdb_core::sql::context::ExecutionContext;
use kalamdb_core::sql::executor::request_transaction_state::RequestTransactionState;
use kalamdb_core::sql::executor::{PreparedExecutionStatement, ScalarValue, SqlExecutor};
use kalamdb_core::sql::SqlImpersonationService;
use kalamdb_sql::classifier::SqlStatementKind;
use kalamdb_system::FileSubfolderState;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use super::file_utils::{stage_and_finalize_files, substitute_file_placeholders};
use super::forward::handle_not_leader_error;
use super::helpers::{
cleanup_files, execute_single_statement, execute_single_statement_raw,
execution_result_to_query_result, stream_sql_rows_response,
};
use super::models::{ErrorCode, QueryRequest, QueryResult, SqlResponse};
use super::request::took_ms;
use super::statements::{
classify_sql, resolve_execute_as_user, resolve_result_username, PreparedApiExecutionStatement,
};
#[allow(clippy::too_many_arguments)]
pub(super) async fn execute_file_upload_path(
is_multipart: bool,
mut files: Option<HashMap<String, (String, Bytes, Option<String>)>>,
required_files: &[String],
prepared_statements: &[PreparedApiExecutionStatement],
app_context: &Arc<AppContext>,
sql_executor: &Arc<SqlExecutor>,
exec_ctx: &ExecutionContext,
impersonation_service: &SqlImpersonationService,
authorized_username: &Username,
default_namespace: &NamespaceId,
params: Vec<ScalarValue>,
schema_registry: &SchemaRegistry,
start_time: Instant,
) -> HttpResponse {
if !is_multipart {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidInput,
"FILE placeholders require multipart/form-data",
took_ms(start_time),
));
}
if prepared_statements.len() != 1 {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidInput,
"File uploads require a single SQL statement",
took_ms(start_time),
));
}
let stmt = &prepared_statements[0];
let execute_as_user = match resolve_execute_as_user(stmt, impersonation_service, exec_ctx).await
{
Ok(uid) => uid,
Err(err) => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
&err,
took_ms(start_time),
));
},
};
let mut files_map = files.take().unwrap_or_default();
if !required_files.is_empty() {
files_map = files_map.into_iter().filter(|(key, _)| required_files.contains(key)).collect();
}
let table_id = match stmt.prepared_statement.table_id.clone() {
Some(tid) => tid,
None => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidInput,
"Could not determine target table from SQL. Use fully qualified table name (namespace.table).",
took_ms(start_time),
));
},
};
let table_entry = match schema_registry.get(&table_id) {
Some(cached) => cached.table_entry(),
None => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::TableNotFound,
&format!("Table '{}' not found", table_id),
took_ms(start_time),
));
},
};
let storage_id = table_entry.storage_id.clone();
let table_type = table_entry.table_type;
if execute_as_user.is_some() && table_type == TableType::Shared {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
&format!(
"EXECUTE AS USER is not allowed on SHARED tables (table '{}'). \
AS USER impersonation is only supported for USER tables.",
table_id
),
took_ms(start_time),
));
}
let user_id = match table_type {
TableType::User => execute_as_user.clone().or_else(|| Some(exec_ctx.user_id().clone())),
TableType::Shared => None,
TableType::Stream | TableType::System => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidInput,
"File uploads are not supported for stream or system tables",
took_ms(start_time),
));
},
};
let manifest_service = app_context.manifest_service();
let mut subfolder_state = match manifest_service.get_file_subfolder_state(&table_id) {
Ok(Some(state)) => state,
Ok(None) => FileSubfolderState::new(),
Err(e) => {
log::warn!("Failed to get subfolder state for {}: {}", table_id, e);
FileSubfolderState::new()
},
};
let file_service = app_context.file_storage_service();
let file_refs = if files_map.is_empty() {
HashMap::new()
} else {
match stage_and_finalize_files(
file_service.as_ref(),
&files_map,
&storage_id,
table_type,
&table_id,
user_id.as_ref(),
&mut subfolder_state,
None,
)
.await
{
Ok(refs) => refs,
Err(e) => {
return HttpResponse::InternalServerError().json(SqlResponse::error(
e.code,
&e.message,
took_ms(start_time),
));
},
}
};
let modified_sql = substitute_file_placeholders(&stmt.prepared_statement.sql, &file_refs);
match kalamdb_sql::parse_single_statement(&modified_sql) {
Ok(Some(_)) => {},
Ok(None) => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidSql,
"Expected exactly one SQL statement after FILE() substitution",
took_ms(start_time),
));
},
Err(err) => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::InvalidSql,
&format!("Failed to parse SQL statement after FILE() substitution: {}", err),
took_ms(start_time),
));
},
};
let modified_classified =
match classify_sql(&modified_sql, default_namespace, exec_ctx, start_time) {
Ok(c) => c,
Err(resp) => return resp,
};
let modified_metadata = PreparedExecutionStatement::new(
modified_sql.clone(),
Some(table_id.clone()),
Some(table_type),
Some(modified_classified),
);
let effective_username =
resolve_result_username(authorized_username, stmt.execute_as_username.as_ref());
match execute_single_statement(
&modified_metadata,
app_context,
sql_executor,
exec_ctx,
execute_as_user,
params,
)
.await
{
Ok(result) => {
let result = result.with_as_user(effective_username);
if let Err(e) = manifest_service.update_file_subfolder_state(&table_id, subfolder_state)
{
log::warn!("Failed to update subfolder state for {}: {}", table_id, e);
}
HttpResponse::Ok().json(SqlResponse::success(vec![result], took_ms(start_time)))
},
Err(err) => {
cleanup_files(
&file_refs,
&storage_id,
table_type,
&table_id,
user_id.as_ref(),
app_context,
)
.await;
HttpResponse::BadRequest().json(SqlResponse::error_with_details(
ErrorCode::SqlExecutionError,
&format!("Statement 1 failed: {}", err),
&modified_sql,
took_ms(start_time),
))
},
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn execute_batch_path(
prepared_statements: &[PreparedApiExecutionStatement],
app_context: &Arc<AppContext>,
sql_executor: &Arc<SqlExecutor>,
exec_ctx: &ExecutionContext,
impersonation_service: &SqlImpersonationService,
authorized_username: &Username,
params: Vec<ScalarValue>,
http_req: &HttpRequest,
req_for_forward: &QueryRequest,
start_time: Instant,
) -> HttpResponse {
let is_batch = prepared_statements.len() > 1;
let stmt_count = prepared_statements.len();
let mut results = Vec::with_capacity(stmt_count);
let mut total_inserted = 0usize;
let mut total_updated = 0usize;
let mut total_deleted = 0usize;
let mut params_remaining = Some(params);
let mut request_transaction_state =
match RequestTransactionState::from_execution_context(exec_ctx) {
Ok(state) => state,
Err(err) => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
&err.to_string(),
took_ms(start_time),
));
},
};
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
let mut idx = 0;
while idx < stmt_count {
let stmt = &prepared_statements[idx];
// ── Transaction batch INSERT path ───────────────────────────────
// When an explicit transaction is active and we see consecutive INSERT
// statements targeting the same table (no EXECUTE AS USER, no params),
// collect them and process through the transaction batch insert path.
if let Some(state) = request_transaction_state.as_ref() {
if let Some(transaction_id) = state.active_transaction_id() {
if is_batchable_insert(stmt) {
let batch_table_id = stmt.prepared_statement.table_id.as_ref();
let mut batch_end = idx + 1;
while batch_end < stmt_count
&& is_batchable_insert(&prepared_statements[batch_end])
&& prepared_statements[batch_end].prepared_statement.table_id.as_ref()
== batch_table_id
{
batch_end += 1;
}
let batch_len = batch_end - idx;
if batch_len > 1 {
let batch_stmts: Vec<&PreparedExecutionStatement> = prepared_statements
[idx..batch_end]
.iter()
.map(|s| &s.prepared_statement)
.collect();
let batch_start = Instant::now();
match sql_executor.try_batch_insert_in_transaction(
&batch_stmts,
exec_ctx,
transaction_id,
) {
Ok(Some(results)) => {
let batch_rows: usize =
results.iter().map(|r| r.affected_rows()).sum();
let batch_ms = batch_start.elapsed().as_secs_f64() * 1000.0;
log::debug!(
target: "sql::exec",
"✅ Batch INSERT ({} stmts, {} rows) | took={:.3}ms",
batch_len,
batch_rows,
batch_ms,
);
total_inserted += batch_rows;
idx = batch_end;
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
continue;
},
Ok(None) => { /* fast path not applicable, fall through */ },
Err(err) => {
if let Some(state) = request_transaction_state.as_mut() {
let _ = state.rollback_if_active(app_context);
}
return HttpResponse::BadRequest().json(
SqlResponse::error_with_details(
ErrorCode::SqlExecutionError,
&format!("Statement {} failed: {}", idx + 1, err),
&prepared_statements[idx].prepared_statement.sql,
took_ms(start_time),
),
);
},
}
}
}
}
}
// ── Per-statement execution (original path) ────────────────────
let execute_as_user =
match resolve_execute_as_user(stmt, impersonation_service, exec_ctx).await {
Ok(uid) => uid,
Err(err) => {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
&err,
took_ms(start_time),
));
},
};
if execute_as_user.is_some()
&& stmt.prepared_statement.table_type == Some(TableType::Shared)
{
if let Some(table_id) = stmt.prepared_statement.table_id.as_ref() {
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
&format!(
"EXECUTE AS USER is not allowed on SHARED tables (table '{}'). \
AS USER impersonation is only supported for USER tables.",
table_id
),
took_ms(start_time),
));
}
}
let stmt_start = Instant::now();
let effective_username =
resolve_result_username(authorized_username, stmt.execute_as_username.as_ref());
let is_last = idx + 1 == stmt_count;
let stmt_params = if is_last {
params_remaining.take().unwrap_or_default()
} else {
params_remaining.as_ref().cloned().unwrap_or_default()
};
match execute_single_statement_raw(
&stmt.prepared_statement,
sql_executor,
exec_ctx,
execute_as_user.clone(),
stmt_params,
)
.await
{
Ok(exec_result) => {
let stmt_duration_secs = stmt_start.elapsed().as_secs_f64();
let stmt_duration_ms = stmt_duration_secs * 1000.0;
let row_count = exec_result.affected_rows();
let safe_sql = kalamdb_commons::helpers::security::redact_sensitive_sql(
&stmt.prepared_statement.sql,
);
if log::log_enabled!(log::Level::Debug) {
log::debug!(
target: "sql::exec",
"✅ SQL executed | sql='{}' | user='{}' | role='{:?}' | rows={} | took={:.3}ms",
safe_sql,
exec_ctx.user_id().as_str(),
exec_ctx.user_role(),
row_count,
stmt_duration_ms
);
}
app_context.slow_query_logger().log_if_slow(
safe_sql,
stmt_duration_secs,
row_count,
exec_ctx.user_id().clone(),
kalamdb_core::schema_registry::TableType::User,
None,
);
if !is_batch {
if let kalamdb_core::sql::ExecutionResult::Rows {
batches,
row_count,
schema,
} = exec_result
{
let effective_role = if execute_as_user.is_some() {
Some(kalamdb_commons::Role::User)
} else {
Some(exec_ctx.user_role())
};
return match stream_sql_rows_response(
batches,
schema,
effective_role,
effective_username,
row_count,
took_ms(start_time),
) {
Ok(response) => response,
Err(err) => {
HttpResponse::InternalServerError().json(SqlResponse::error(
ErrorCode::InternalError,
&format!("Failed to stream SQL response: {}", err),
took_ms(start_time),
))
},
};
}
}
let effective_role = if execute_as_user.is_some() {
Some(kalamdb_commons::Role::User)
} else {
Some(exec_ctx.user_role())
};
let result = match execution_result_to_query_result(exec_result, effective_role) {
Ok(result) => result.with_as_user(effective_username),
Err(err) => {
return HttpResponse::InternalServerError().json(SqlResponse::error(
ErrorCode::InternalError,
&format!("Failed to serialize SQL result: {}", err),
took_ms(start_time),
));
},
};
if is_batch {
if let Some(ref msg) = result.message {
if msg.contains("Inserted") {
total_inserted += result.row_count;
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
idx += 1;
continue;
} else if msg.contains("Updated") {
total_updated += result.row_count;
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
idx += 1;
continue;
} else if msg.contains("Deleted") {
total_deleted += result.row_count;
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
idx += 1;
continue;
}
}
}
results.push(result);
},
Err(err) => {
if let Some(state) = request_transaction_state.as_mut() {
let _ = state.rollback_if_active(app_context);
}
if let Some(kalamdb_err) = err.downcast_ref::<kalamdb_core::error::KalamDbError>() {
if let Some(response) = handle_not_leader_error(
kalamdb_err,
http_req,
req_for_forward,
app_context,
exec_ctx.request_id(),
start_time,
)
.await
{
return response;
}
}
return HttpResponse::BadRequest().json(SqlResponse::error_with_details(
ErrorCode::SqlExecutionError,
&format!("Statement {} failed: {}", idx + 1, err),
&stmt.prepared_statement.sql,
took_ms(start_time),
));
},
}
if let Some(state) = request_transaction_state.as_mut() {
state.sync_from_coordinator(app_context);
}
idx += 1;
}
if let Some(state) = request_transaction_state.as_mut() {
if state.is_active() {
let _ = state.rollback_if_active(app_context);
return HttpResponse::BadRequest().json(SqlResponse::error(
ErrorCode::SqlExecutionError,
"Request completed with an open explicit transaction; rolled back automatically",
took_ms(start_time),
));
}
}
if is_batch {
if total_inserted > 0 {
results.push(
QueryResult::with_affected_rows(
total_inserted,
Some(format!("Inserted {} row(s)", total_inserted)),
)
.with_as_user(authorized_username.clone()),
);
}
if total_updated > 0 {
results.push(
QueryResult::with_affected_rows(
total_updated,
Some(format!("Updated {} row(s)", total_updated)),
)
.with_as_user(authorized_username.clone()),
);
}
if total_deleted > 0 {
results.push(
QueryResult::with_affected_rows(
total_deleted,
Some(format!("Deleted {} row(s)", total_deleted)),
)
.with_as_user(authorized_username.clone()),
);
}
}
HttpResponse::Ok().json(SqlResponse::success(results, took_ms(start_time)))
}
/// Check if a prepared statement is a simple INSERT eligible for batching:
/// no EXECUTE AS USER, has a table_id and table_type, and is classified as INSERT.
fn is_batchable_insert(stmt: &PreparedApiExecutionStatement) -> bool {
if stmt.execute_as_username.is_some() {
return false;
}
if stmt.prepared_statement.table_id.is_none() || stmt.prepared_statement.table_type.is_none() {
return false;
}
matches!(
stmt.prepared_statement.classified_statement.as_ref().map(|c| c.kind()),
Some(SqlStatementKind::Insert(_))
)
}