forked from darshanDevrai/brahmand
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrite_helpers.rs
More file actions
684 lines (595 loc) · 22.3 KB
/
write_helpers.rs
File metadata and controls
684 lines (595 loc) · 22.3 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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Helpers for write operations: property validation, column mapping, INSERT/DELETE SQL generation.
use std::collections::HashMap;
use clickgraph::graph_catalog::expression_parser::PropertyValue;
use super::error::EmbeddedError;
use super::value::Value;
/// Validate that all property keys in `properties` are known to the schema.
///
/// Accepts Cypher property names (keys of `property_mappings`) and ID column names.
/// Rejects unknown keys with `EmbeddedError::Validation`.
pub fn validate_properties(
properties: &HashMap<String, Value>,
property_mappings: &HashMap<&str, &str>,
id_columns: &[&str],
) -> Result<(), EmbeddedError> {
let mut unknown_keys = Vec::new();
for key in properties.keys() {
if !property_mappings.contains_key(key.as_str()) && !id_columns.contains(&key.as_str()) {
unknown_keys.push(key.clone());
}
}
if !unknown_keys.is_empty() {
unknown_keys.sort();
let mut valid_keys: Vec<&str> = property_mappings.keys().copied().collect();
valid_keys.extend(id_columns.iter().copied());
valid_keys.sort();
valid_keys.dedup();
return Err(EmbeddedError::Validation(format!(
"Unknown property key(s): {:?}. Valid keys: {:?}",
unknown_keys, valid_keys
)));
}
Ok(())
}
/// Extract a simple `&str -> &str` mapping from the schema's `HashMap<String, PropertyValue>`.
///
/// Only `PropertyValue::Column` entries produce mappings. Expression entries are skipped
/// since they cannot be written to.
pub fn extract_property_mappings(
schema_mappings: &HashMap<String, PropertyValue>,
) -> HashMap<&str, &str> {
let mut result = HashMap::new();
for (cypher_name, prop_val) in schema_mappings {
match prop_val {
PropertyValue::Column(col) => {
result.insert(cypher_name.as_str(), col.as_str());
}
PropertyValue::Expression(_) => {
// Expression properties are computed, not writable
}
}
}
result
}
/// Check that a table is writable (has no `source:` field set).
///
/// Tables with a `source:` field are backed by external data (Parquet, S3, etc.)
/// and cannot be modified via INSERT, DELETE, or UPDATE operations.
pub fn check_writable(source: &Option<String>, label: &str) -> Result<(), EmbeddedError> {
if source.is_some() {
return Err(EmbeddedError::Validation(format!(
"Cannot modify source-backed table for '{}'. \
Only writable tables (without a 'source:' field) support write operations.",
label
)));
}
Ok(())
}
/// Build an INSERT SQL statement for one or more rows.
///
/// Generates:
/// - Single row: `INSERT INTO {db}.{table} ({cols}) VALUES ({vals})`
/// - Multiple rows: `INSERT INTO {db}.{table} ({cols}) VALUES ({vals1}), ({vals2}), ...`
pub fn build_insert_sql(
db: &str,
table: &str,
columns: &[String],
values_rows: &[Vec<String>],
) -> String {
let cols = columns
.iter()
.map(|c| format!("`{}`", c))
.collect::<Vec<_>>()
.join(", ");
let rows: Vec<String> = values_rows
.iter()
.map(|row| format!("({})", row.join(", ")))
.collect();
format!(
"INSERT INTO `{}`.`{}` ({}) VALUES {}",
db,
table,
cols,
rows.join(", ")
)
}
/// Build a `DELETE FROM ... WHERE ...` SQL statement (lightweight delete).
///
/// Maps filter keys from Cypher property names to ClickHouse column names using
/// `property_mappings`, and renders filter values via `Value::to_sql_literal()`.
///
/// The filter map must not be empty — at least one condition is required to prevent
/// accidental deletion of all rows.
pub fn build_delete_sql(
db: &str,
table: &str,
filters: &HashMap<String, Value>,
property_mappings: &HashMap<&str, &str>,
id_columns: &[&str],
) -> Result<String, EmbeddedError> {
if filters.is_empty() {
return Err(EmbeddedError::Validation(
"DELETE requires at least one filter condition to prevent accidental deletion of all rows."
.to_string(),
));
}
// Build WHERE clauses in sorted key order for deterministic output
let mut conditions: Vec<String> = Vec::new();
let mut sorted_keys: Vec<&String> = filters.keys().collect();
sorted_keys.sort();
for key in sorted_keys {
let value = &filters[key];
// Resolve column name: check property_mappings first, then id_columns, then use as-is
let col_name = if let Some(mapped) = property_mappings.get(key.as_str()) {
mapped.to_string()
} else if id_columns.contains(&key.as_str()) {
key.clone()
} else {
// Should not reach here if validate_properties was called first
key.clone()
};
let literal = value.to_sql_literal().map_err(EmbeddedError::Validation)?;
conditions.push(format!("`{}` = {}", col_name, literal));
}
Ok(format!(
"DELETE FROM `{}`.`{}` WHERE {}",
db,
table,
conditions.join(" AND ")
))
}
/// Transform JSON keys from Cypher property names to ClickHouse column names.
///
/// Parses each line of newline-delimited JSON, remaps keys using `property_mappings`,
/// and re-serializes with ClickHouse column names. Unknown keys are skipped with a
/// warning log.
///
/// Returns the transformed JSON lines and the count of lines processed.
pub fn transform_json_keys(
json_lines: &str,
property_mappings: &HashMap<&str, &str>,
id_columns: &[&str],
) -> Result<(String, usize), EmbeddedError> {
let mut output_lines = Vec::new();
let mut line_count = 0;
for (line_idx, line) in json_lines.lines().enumerate() {
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
let parsed: serde_json::Value = serde_json::from_str(trimmed).map_err(|e| {
EmbeddedError::Validation(format!("Invalid JSON on line {}: {}", line_idx + 1, e))
})?;
let obj = parsed.as_object().ok_or_else(|| {
EmbeddedError::Validation(format!(
"Expected JSON object on line {}, got: {}",
line_idx + 1,
trimmed
))
})?;
let mut mapped_obj = serde_json::Map::new();
for (key, val) in obj {
if let Some(mapped_col) = property_mappings.get(key.as_str()) {
mapped_obj.insert(mapped_col.to_string(), val.clone());
} else if id_columns.contains(&key.as_str()) {
mapped_obj.insert(key.clone(), val.clone());
} else {
log::warn!(
"import_json: skipping unknown key '{}' on line {}",
key,
line_idx + 1
);
}
}
let mapped_line = serde_json::to_string(&serde_json::Value::Object(mapped_obj))
.map_err(|e| EmbeddedError::Validation(format!("JSON serialization error: {}", e)))?;
output_lines.push(mapped_line);
line_count += 1;
}
Ok((output_lines.join("\n"), line_count))
}
/// Validate a file path for safety (no SQL injection via metacharacters).
pub fn validate_file_path(path: &str) -> Result<(), EmbeddedError> {
// Reject paths containing SQL metacharacters that could enable injection
let forbidden = [';', '\'', '"', '\\', '\0', '`', '\n', '\r', '$'];
for ch in &forbidden {
if path.contains(*ch) {
return Err(EmbeddedError::Validation(format!(
"File path contains forbidden character '{}': {}",
ch, path
)));
}
}
Ok(())
}
/// Build an `INSERT INTO ... SELECT ... FROM file(...)` SQL statement for file-based import.
///
/// If all Cypher property names match ClickHouse column names (identity mapping),
/// generates `SELECT * FROM file()`. Otherwise generates explicit column aliases.
///
/// `format` is the ClickHouse format name (e.g., `"JSONEachRow"`, `"CSVWithNames"`,
/// `"Parquet"`).
pub fn build_import_file_sql(
db: &str,
table: &str,
file_path: &str,
format: &str,
property_mappings: &HashMap<&str, &str>,
id_columns: &[&str],
) -> String {
// Check if mapping is identity (Cypher names == CH column names)
let needs_mapping = property_mappings
.iter()
.any(|(cypher, ch_col)| cypher != ch_col);
if needs_mapping {
// Build explicit SELECT with aliases: SELECT cypher_col AS ch_col
let mut select_parts: Vec<String> = Vec::new();
// Add ID columns as-is (they pass through without mapping)
for id_col in id_columns {
select_parts.push(format!("`{}`", id_col));
}
// Add mapped property columns
let mut sorted_mappings: Vec<(&&str, &&str)> = property_mappings.iter().collect();
sorted_mappings.sort_by_key(|(k, _)| *k);
for (cypher_name, ch_col) in sorted_mappings {
if cypher_name == ch_col {
select_parts.push(format!("`{}`", ch_col));
} else {
select_parts.push(format!("`{}` AS `{}`", cypher_name, ch_col));
}
}
format!(
"INSERT INTO `{}`.`{}` SELECT {} FROM file('{}', '{}')",
db,
table,
select_parts.join(", "),
file_path,
format
)
} else {
format!(
"INSERT INTO `{}`.`{}` SELECT * FROM file('{}', '{}')",
db, table, file_path, format
)
}
}
/// Detect the ClickHouse import format name from a file extension.
///
/// Returns `None` for unrecognized extensions.
pub fn import_format_from_extension(path: &str) -> Option<&'static str> {
let ext = path.rsplit('.').next()?.to_lowercase();
match ext.as_str() {
"parquet" | "pq" => Some("Parquet"),
"csv" => Some("CSVWithNames"),
"tsv" | "tab" => Some("TabSeparatedWithNames"),
"json" | "ndjson" | "jsonl" => Some("JSONEachRow"),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_properties_accepts_known_keys() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
mappings.insert("age", "age");
let mut props = HashMap::new();
props.insert("name".to_string(), Value::String("Alice".to_string()));
let result = validate_properties(&props, &mappings, &["user_id"]);
assert!(result.is_ok());
}
#[test]
fn test_validate_properties_accepts_id_columns() {
let mappings = HashMap::new();
let mut props = HashMap::new();
props.insert("user_id".to_string(), Value::String("u1".to_string()));
let result = validate_properties(&props, &mappings, &["user_id"]);
assert!(result.is_ok());
}
#[test]
fn test_validate_properties_rejects_unknown_keys() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let mut props = HashMap::new();
props.insert("bad_key".to_string(), Value::String("val".to_string()));
let result = validate_properties(&props, &mappings, &["user_id"]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("bad_key"));
}
#[test]
fn test_build_insert_sql_single_row() {
let cols = vec!["id".to_string(), "name".to_string()];
let rows = vec![vec!["'abc'".to_string(), "'Alice'".to_string()]];
let sql = build_insert_sql("mydb", "mytable", &cols, &rows);
assert_eq!(
sql,
"INSERT INTO `mydb`.`mytable` (`id`, `name`) VALUES ('abc', 'Alice')"
);
}
#[test]
fn test_build_insert_sql_batch() {
let cols = vec!["id".to_string(), "name".to_string()];
let rows = vec![
vec!["'a'".to_string(), "'Alice'".to_string()],
vec!["'b'".to_string(), "'Bob'".to_string()],
];
let sql = build_insert_sql("mydb", "mytable", &cols, &rows);
assert_eq!(
sql,
"INSERT INTO `mydb`.`mytable` (`id`, `name`) VALUES ('a', 'Alice'), ('b', 'Bob')"
);
}
#[test]
fn test_extract_property_mappings() {
let mut schema_map = HashMap::new();
schema_map.insert(
"name".to_string(),
PropertyValue::Column("full_name".to_string()),
);
schema_map.insert(
"computed".to_string(),
PropertyValue::Expression("col1 + col2".to_string()),
);
let result = extract_property_mappings(&schema_map);
assert_eq!(result.get("name"), Some(&"full_name"));
assert!(!result.contains_key("computed"));
}
// --- DELETE SQL generation tests ---
#[test]
fn test_build_delete_sql_with_property_mapping() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
mappings.insert("age", "user_age");
let mut filters = HashMap::new();
filters.insert("name".to_string(), Value::String("Alice".to_string()));
let sql = build_delete_sql("mydb", "users", &filters, &mappings, &["user_id"]).unwrap();
assert_eq!(
sql,
"DELETE FROM `mydb`.`users` WHERE `full_name` = 'Alice'"
);
}
#[test]
fn test_build_delete_sql_with_id_column() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let mut filters = HashMap::new();
filters.insert("user_id".to_string(), Value::String("u123".to_string()));
let sql = build_delete_sql("mydb", "users", &filters, &mappings, &["user_id"]).unwrap();
assert_eq!(sql, "DELETE FROM `mydb`.`users` WHERE `user_id` = 'u123'");
}
#[test]
fn test_build_delete_sql_multiple_filters() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
mappings.insert("age", "user_age");
let mut filters = HashMap::new();
filters.insert("name".to_string(), Value::String("Bob".to_string()));
filters.insert("age".to_string(), Value::Int64(30));
let sql = build_delete_sql("mydb", "users", &filters, &mappings, &["user_id"]).unwrap();
// Keys are sorted for deterministic output
assert_eq!(
sql,
"DELETE FROM `mydb`.`users` WHERE `user_age` = 30 AND `full_name` = 'Bob'"
);
}
#[test]
fn test_build_delete_sql_empty_filters_rejected() {
let mappings = HashMap::new();
let filters = HashMap::new();
let result = build_delete_sql("mydb", "users", &filters, &mappings, &[]);
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("at least one filter"));
}
#[test]
fn test_build_delete_sql_filter_keys_validated() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let mut filters = HashMap::new();
filters.insert("unknown_key".to_string(), Value::String("val".to_string()));
// validate_properties should catch this before build_delete_sql is called
let result = validate_properties(&filters, &mappings, &["user_id"]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("unknown_key"));
}
// --- Writable guard tests ---
#[test]
fn test_check_writable_allows_no_source() {
assert!(check_writable(&None, "User").is_ok());
}
#[test]
fn test_check_writable_rejects_source_backed() {
let source = Some("s3://bucket/data.parquet".to_string());
let result = check_writable(&source, "User");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("source-backed"));
}
// --- JSON key transformation tests ---
#[test]
fn test_transform_json_keys_maps_cypher_to_ch_columns() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
mappings.insert("email", "email_address");
let input = r#"{"name": "Alice", "email": "alice@test.com"}"#;
let (output, count) = transform_json_keys(input, &mappings, &[]).unwrap();
assert_eq!(count, 1);
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
let obj = parsed.as_object().unwrap();
assert!(obj.contains_key("full_name"));
assert!(obj.contains_key("email_address"));
assert!(!obj.contains_key("name"));
assert!(!obj.contains_key("email"));
}
#[test]
fn test_transform_json_keys_skips_unknown_keys() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let input = r#"{"name": "Alice", "unknown_field": "ignored"}"#;
let (output, count) = transform_json_keys(input, &mappings, &[]).unwrap();
assert_eq!(count, 1);
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
let obj = parsed.as_object().unwrap();
assert!(obj.contains_key("full_name"));
assert!(!obj.contains_key("unknown_field"));
}
#[test]
fn test_transform_json_keys_preserves_id_columns() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let input = r#"{"user_id": "u1", "name": "Alice"}"#;
let (output, count) = transform_json_keys(input, &mappings, &["user_id"]).unwrap();
assert_eq!(count, 1);
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
let obj = parsed.as_object().unwrap();
assert!(obj.contains_key("user_id"));
assert!(obj.contains_key("full_name"));
}
#[test]
fn test_transform_json_keys_multiple_lines() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let input = r#"{"name": "Alice"}
{"name": "Bob"}"#;
let (output, count) = transform_json_keys(input, &mappings, &[]).unwrap();
assert_eq!(count, 2);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 2);
}
#[test]
fn test_transform_json_keys_invalid_json() {
let mappings = HashMap::new();
let input = "not valid json";
let result = transform_json_keys(input, &mappings, &[]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Invalid JSON"));
}
// --- File path validation tests ---
#[test]
fn test_validate_file_path_accepts_normal_paths() {
assert!(validate_file_path("/tmp/data.json").is_ok());
assert!(validate_file_path("data/import.jsonl").is_ok());
}
#[test]
fn test_validate_file_path_rejects_semicolons() {
let result = validate_file_path("/tmp/data.json; DROP TABLE users");
assert!(result.is_err());
}
// --- File import SQL tests ---
#[test]
fn test_build_import_file_sql_no_mapping() {
let mut mappings = HashMap::new();
mappings.insert("name", "name");
mappings.insert("age", "age");
let sql = build_import_file_sql(
"mydb",
"users",
"/tmp/data.json",
"JSONEachRow",
&mappings,
&["id"],
);
assert_eq!(
sql,
"INSERT INTO `mydb`.`users` SELECT * FROM file('/tmp/data.json', 'JSONEachRow')"
);
}
#[test]
fn test_build_import_file_sql_with_mapping() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
mappings.insert("email", "email_address");
let sql = build_import_file_sql(
"mydb",
"users",
"/tmp/data.json",
"JSONEachRow",
&mappings,
&["id"],
);
assert!(sql.contains("INSERT INTO `mydb`.`users`"));
assert!(sql.contains("`email` AS `email_address`"));
assert!(sql.contains("`name` AS `full_name`"));
assert!(sql.contains("FROM file('/tmp/data.json', 'JSONEachRow')"));
}
#[test]
fn test_build_import_file_sql_with_id_columns() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let sql = build_import_file_sql(
"mydb",
"users",
"/tmp/data.json",
"JSONEachRow",
&mappings,
&["user_id"],
);
assert!(sql.contains("`user_id`"));
assert!(sql.contains("`name` AS `full_name`"));
}
#[test]
fn test_build_import_file_sql_parquet() {
let mut mappings = HashMap::new();
mappings.insert("name", "name");
let sql = build_import_file_sql(
"mydb",
"users",
"/tmp/data.parquet",
"Parquet",
&mappings,
&["id"],
);
assert_eq!(
sql,
"INSERT INTO `mydb`.`users` SELECT * FROM file('/tmp/data.parquet', 'Parquet')"
);
}
#[test]
fn test_build_import_file_sql_csv() {
let mut mappings = HashMap::new();
mappings.insert("name", "full_name");
let sql = build_import_file_sql(
"mydb",
"users",
"/tmp/data.csv",
"CSVWithNames",
&mappings,
&["id"],
);
assert!(sql.contains("FROM file('/tmp/data.csv', 'CSVWithNames')"));
assert!(sql.contains("`name` AS `full_name`"));
}
// --- import format detection tests ---
#[test]
fn test_import_format_from_extension() {
assert_eq!(
import_format_from_extension("data.parquet"),
Some("Parquet")
);
assert_eq!(import_format_from_extension("data.pq"), Some("Parquet"));
assert_eq!(
import_format_from_extension("data.csv"),
Some("CSVWithNames")
);
assert_eq!(
import_format_from_extension("data.tsv"),
Some("TabSeparatedWithNames")
);
assert_eq!(
import_format_from_extension("data.json"),
Some("JSONEachRow")
);
assert_eq!(
import_format_from_extension("data.ndjson"),
Some("JSONEachRow")
);
assert_eq!(
import_format_from_extension("data.jsonl"),
Some("JSONEachRow")
);
assert_eq!(import_format_from_extension("data.unknown"), None);
}
}