-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathexecute.rs
More file actions
1285 lines (1094 loc) · 42.6 KB
/
execute.rs
File metadata and controls
1285 lines (1094 loc) · 42.6 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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::time::Duration;
use super::ast::SchemaViewer;
use crate::db::datastore::locking_tx_datastore::state_view::StateView;
use crate::db::datastore::traits::IsolationLevel;
use crate::db::relational_db::{RelationalDB, Tx};
use crate::energy::EnergyQuanta;
use crate::error::DBError;
use crate::estimation::estimate_rows_scanned;
use crate::execution_context::Workload;
use crate::host::module_host::{DatabaseTableUpdate, DatabaseUpdate, EventStatus, ModuleEvent, ModuleFunctionCall};
use crate::host::ArgsTuple;
use crate::subscription::module_subscription_actor::{ModuleSubscriptions, WriteConflict};
use crate::subscription::tx::DeltaTx;
use crate::util::slow::SlowQueryLogger;
use crate::vm::{check_row_limit, DbProgram, TxMode};
use anyhow::anyhow;
use spacetimedb_expr::statement::Statement;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::metrics::ExecutionMetrics;
use spacetimedb_lib::relation::FieldName;
use spacetimedb_lib::Timestamp;
use spacetimedb_lib::{AlgebraicType, ProductType, ProductValue};
use spacetimedb_query::{compile_sql_stmt, execute_dml_stmt, execute_select_stmt};
use spacetimedb_vm::eval::run_ast;
use spacetimedb_vm::expr::{CodeResult, CrudExpr, Expr};
use spacetimedb_vm::relation::MemTable;
pub struct StmtResult {
pub schema: ProductType,
pub rows: Vec<ProductValue>,
}
// TODO(cloutiertyler): we could do this the swift parsing way in which
// we always generate a plan, but it may contain errors
pub(crate) fn collect_result(
result: &mut Vec<MemTable>,
updates: &mut Vec<DatabaseTableUpdate>,
r: CodeResult,
) -> Result<(), DBError> {
match r {
CodeResult::Value(_) => {}
CodeResult::Table(x) => result.push(x),
CodeResult::Block(lines) => {
for x in lines {
collect_result(result, updates, x)?;
}
}
CodeResult::Halt(err) => return Err(DBError::VmUser(err)),
CodeResult::Pass(x) => match x {
None => {}
Some(update) => {
updates.push(DatabaseTableUpdate {
table_name: update.table_name,
table_id: update.table_id,
inserts: update.inserts.into(),
deletes: update.deletes.into(),
});
}
},
}
Ok(())
}
fn execute(
p: &mut DbProgram<'_, '_>,
ast: Vec<CrudExpr>,
sql: &str,
updates: &mut Vec<DatabaseTableUpdate>,
) -> Result<Vec<MemTable>, DBError> {
let slow_query_threshold = if let TxMode::Tx(tx) = p.tx {
p.db.query_limit(tx)?.map(Duration::from_millis)
} else {
None
};
let _slow_query_logger = SlowQueryLogger::new(sql, slow_query_threshold, p.tx.ctx().workload()).log_guard();
let mut result = Vec::with_capacity(ast.len());
let query = Expr::Block(ast.into_iter().map(|x| Expr::Crud(Box::new(x))).collect());
// SQL queries can never reference `MemTable`s, so pass an empty `SourceSet`.
collect_result(&mut result, updates, run_ast(p, query, [].into()).into())?;
Ok(result)
}
/// Run the compiled `SQL` expression inside the `vm` created by [DbProgram]
///
/// Evaluates `ast` and accordingly triggers mutable or read tx to execute
///
/// Also, in case the execution takes more than x, log it as `slow query`
pub fn execute_sql(
db: &RelationalDB,
sql: &str,
ast: Vec<CrudExpr>,
auth: AuthCtx,
subs: Option<&ModuleSubscriptions>,
) -> Result<Vec<MemTable>, DBError> {
if CrudExpr::is_reads(&ast) {
let mut updates = Vec::new();
db.with_read_only(Workload::Sql, |tx| {
execute(
&mut DbProgram::new(db, &mut TxMode::Tx(tx), auth),
ast,
sql,
&mut updates,
)
})
} else if subs.is_none() {
let mut updates = Vec::new();
db.with_auto_commit(Workload::Sql, |mut_tx| {
execute(
&mut DbProgram::new(db, &mut mut_tx.into(), auth),
ast,
sql,
&mut updates,
)
})
} else {
let mut tx = db.begin_mut_tx(IsolationLevel::Serializable, Workload::Sql);
let mut updates = Vec::with_capacity(ast.len());
let res = execute(
&mut DbProgram::new(db, &mut (&mut tx).into(), auth),
ast,
sql,
&mut updates,
);
if res.is_ok() && !updates.is_empty() {
let event = ModuleEvent {
timestamp: Timestamp::now(),
caller_identity: auth.caller,
caller_connection_id: None,
function_call: ModuleFunctionCall {
reducer: String::new(),
reducer_id: u32::MAX.into(),
args: ArgsTuple::default(),
},
status: EventStatus::Committed(DatabaseUpdate { tables: updates }),
energy_quanta_used: EnergyQuanta::ZERO,
host_execution_duration: Duration::ZERO,
request_id: None,
timer: None,
};
match subs.unwrap().commit_and_broadcast_event(None, event, tx).unwrap() {
Ok(_) => res,
Err(WriteConflict) => todo!("See module_host_actor::call_reducer_with_tx"),
}
} else {
db.finish_tx(tx, res)
}
}
}
/// Like [`execute_sql`], but for providing your own `tx`.
///
/// Returns None if you pass a mutable query with an immutable tx.
pub fn execute_sql_tx<'a>(
db: &RelationalDB,
tx: impl Into<TxMode<'a>>,
sql: &str,
ast: Vec<CrudExpr>,
auth: AuthCtx,
) -> Result<Option<Vec<MemTable>>, DBError> {
let mut tx = tx.into();
if matches!(tx, TxMode::Tx(_)) && !CrudExpr::is_reads(&ast) {
return Ok(None);
}
let mut updates = Vec::new(); // No subscription updates in this path, because it requires owning the tx.
execute(&mut DbProgram::new(db, &mut tx, auth), ast, sql, &mut updates).map(Some)
}
pub struct SqlResult {
pub rows: Vec<ProductValue>,
/// These metrics will be reported via `report_tx_metrics`.
/// They should not be reported separately to avoid double counting.
pub metrics: ExecutionMetrics,
}
/// Run the `SQL` string using the `auth` credentials
pub fn run(
db: &RelationalDB,
sql_text: &str,
auth: AuthCtx,
subs: Option<&ModuleSubscriptions>,
head: &mut Vec<(Box<str>, AlgebraicType)>,
) -> Result<SqlResult, DBError> {
// We parse the sql statement in a mutable transation.
// If it turns out to be a query, we downgrade the tx.
let (tx, stmt) = db.with_auto_rollback(db.begin_mut_tx(IsolationLevel::Serializable, Workload::Sql), |tx| {
compile_sql_stmt(sql_text, &SchemaViewer::new(tx, &auth), &auth)
})?;
let mut metrics = ExecutionMetrics::default();
match stmt {
Statement::Select(stmt) => {
// Up to this point, the tx has been read-only,
// and hence there are no deltas to process.
let (tx_data, tx_metrics_mut, tx) = tx.commit_downgrade(Workload::Sql);
// Release the tx on drop, so that we record metrics.
let mut tx = scopeguard::guard(tx, |tx| {
let (tx_metrics_downgrade, reducer) = db.release_tx(tx);
db.report_tx_metricses(&reducer, Some(&tx_data), Some(&tx_metrics_mut), &tx_metrics_downgrade);
});
// Compute the header for the result set
stmt.for_each_return_field(|col_name, col_type| {
head.push((col_name.into(), col_type.clone()));
});
// Evaluate the query
let rows = execute_select_stmt(stmt, &DeltaTx::from(&*tx), &mut metrics, |plan| {
check_row_limit(
&[&plan],
db,
&tx,
|plan, tx| plan.plan_iter().map(|plan| estimate_rows_scanned(tx, plan)).sum(),
&auth,
)?;
Ok(plan)
})?;
// Update transaction metrics
tx.metrics.merge(metrics);
Ok(SqlResult {
rows,
metrics: tx.metrics,
})
}
Statement::DML(stmt) => {
// An extra layer of auth is required for DML
if auth.caller != auth.owner {
return Err(anyhow!("Only owners are authorized to run SQL DML statements").into());
}
// Evaluate the mutation
let (mut tx, _) = db.with_auto_rollback(tx, |tx| execute_dml_stmt(stmt, tx, &mut metrics))?;
// Update transaction metrics
tx.metrics.merge(metrics);
// Commit the tx if there are no deltas to process
if subs.is_none() {
let metrics = tx.metrics;
return db.commit_tx(tx).map(|tx_opt| {
if let Some((tx_data, tx_metrics, reducer)) = tx_opt {
db.report(&reducer, &tx_metrics, Some(&tx_data));
}
SqlResult { rows: vec![], metrics }
});
}
// Otherwise downgrade the tx and process the deltas.
// Note, we get the delta by downgrading the tx.
// Hence we just pass a default `DatabaseUpdate` here.
// It will ultimately be replaced with the correct one.
match subs
.unwrap()
.commit_and_broadcast_event(
None,
ModuleEvent {
timestamp: Timestamp::now(),
caller_identity: auth.caller,
caller_connection_id: None,
function_call: ModuleFunctionCall {
reducer: String::new(),
reducer_id: u32::MAX.into(),
args: ArgsTuple::default(),
},
status: EventStatus::Committed(DatabaseUpdate::default()),
energy_quanta_used: EnergyQuanta::ZERO,
host_execution_duration: Duration::ZERO,
request_id: None,
timer: None,
},
tx,
)
.unwrap()
{
Err(WriteConflict) => {
todo!("See module_host_actor::call_reducer_with_tx")
}
Ok(_) => Ok(SqlResult { rows: vec![], metrics }),
}
}
}
}
/// Translates a `FieldName` to the field's name.
pub fn translate_col(tx: &Tx, field: FieldName) -> Option<Box<str>> {
Some(
tx.get_schema(field.table)?
.get_column(field.col.idx())?
.col_name
.clone(),
)
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::db::datastore::system_tables::{
StRowLevelSecurityRow, StTableFields, ST_ROW_LEVEL_SECURITY_ID, ST_TABLE_ID, ST_TABLE_NAME,
};
use crate::db::relational_db::tests_utils::{begin_tx, insert, with_auto_commit, TestDB};
use crate::vm::tests::create_table_with_rows;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use spacetimedb_lib::bsatn::ToBsatn;
use spacetimedb_lib::db::auth::{StAccess, StTableType};
use spacetimedb_lib::error::{ResultTest, TestError};
use spacetimedb_lib::relation::Header;
use spacetimedb_lib::{AlgebraicValue, Identity};
use spacetimedb_primitives::{col_list, ColId, TableId};
use spacetimedb_sats::{product, AlgebraicType, ArrayValue, ProductType};
use spacetimedb_vm::eval::test_helpers::create_game_data;
use std::sync::Arc;
pub(crate) fn execute_for_testing(
db: &RelationalDB,
sql_text: &str,
q: Vec<CrudExpr>,
) -> Result<Vec<MemTable>, DBError> {
let subs = ModuleSubscriptions::new(Arc::new(db.clone()), <_>::default(), Identity::ZERO);
execute_sql(db, sql_text, q, AuthCtx::for_testing(), Some(&subs))
}
/// Short-cut for simplify test execution
pub(crate) fn run_for_testing(db: &RelationalDB, sql_text: &str) -> Result<Vec<ProductValue>, DBError> {
let subs = ModuleSubscriptions::new(Arc::new(db.clone()), <_>::default(), Identity::ZERO);
run(db, sql_text, AuthCtx::for_testing(), Some(&subs), &mut vec![]).map(|x| x.rows)
}
fn create_data(total_rows: u64) -> ResultTest<(TestDB, MemTable)> {
let stdb = TestDB::durable()?;
let rows: Vec<_> = (1..=total_rows)
.map(|i| product!(i, format!("health{i}").into_boxed_str()))
.collect();
let head = ProductType::from([("inventory_id", AlgebraicType::U64), ("name", AlgebraicType::String)]);
let schema = with_auto_commit(&stdb, |tx| {
create_table_with_rows(&stdb, tx, "inventory", head.clone(), &rows, StAccess::Public)
})?;
let header = Header::from(&*schema).into();
Ok((stdb, MemTable::new(header, schema.table_access, rows)))
}
fn create_identity_table(table_name: &str) -> ResultTest<(TestDB, MemTable)> {
let stdb = TestDB::durable()?;
let head = ProductType::from([("identity", AlgebraicType::identity())]);
let rows = vec![product!(Identity::ZERO), product!(Identity::ONE)];
let schema = with_auto_commit(&stdb, |tx| {
create_table_with_rows(&stdb, tx, table_name, head.clone(), &rows, StAccess::Public)
})?;
let header = Header::from(&*schema).into();
Ok((stdb, MemTable::new(header, schema.table_access, rows)))
}
#[test]
fn test_select_star() -> ResultTest<()> {
let (db, input) = create_data(1)?;
let result = run_for_testing(&db, "SELECT * FROM inventory")?;
assert_eq!(result, input.data, "Inventory");
Ok(())
}
#[test]
fn test_limit() -> ResultTest<()> {
let (db, _) = create_data(5)?;
let result = run_for_testing(&db, "SELECT * FROM inventory limit 2")?;
let (_, input) = create_data(2)?;
assert_eq!(result, input.data, "Inventory");
Ok(())
}
#[test]
fn test_count() -> ResultTest<()> {
let (db, _) = create_data(5)?;
let sql = "SELECT count(*) as n FROM inventory";
let result = run_for_testing(&db, sql)?;
assert_eq!(result, vec![product![5u64]], "Inventory");
let sql = "SELECT count(*) as n FROM inventory limit 2";
let result = run_for_testing(&db, sql)?;
assert_eq!(result, vec![product![5u64]], "Inventory");
let sql = "SELECT count(*) as n FROM inventory WHERE inventory_id = 4 or inventory_id = 5";
let result = run_for_testing(&db, sql)?;
assert_eq!(result, vec![product![2u64]], "Inventory");
Ok(())
}
/// Test the evaluation of SELECT, UPDATE, and DELETE parameterized with `:sender`
#[test]
fn test_sender_param() -> ResultTest<()> {
let (db, _) = create_identity_table("user")?;
const SELECT_ALL: &str = "SELECT * FROM user";
let sql = "SELECT * FROM user WHERE identity = :sender";
let result = run_for_testing(&db, sql)?;
assert_eq!(result, vec![product![Identity::ZERO]]);
let sql = "DELETE FROM user WHERE identity = :sender";
run_for_testing(&db, sql)?;
let result = run_for_testing(&db, SELECT_ALL)?;
assert_eq!(result, vec![product![Identity::ONE]]);
let zero = "0".repeat(64);
let one = "0".repeat(63) + "1";
let sql = format!("UPDATE user SET identity = 0x{zero}");
run_for_testing(&db, &sql)?;
let sql = format!("UPDATE user SET identity = 0x{one} WHERE identity = :sender");
run_for_testing(&db, &sql)?;
let result = run_for_testing(&db, SELECT_ALL)?;
assert_eq!(result, vec![product![Identity::ONE]]);
Ok(())
}
/// Create an [Identity] from a [u8]
fn identity_from_u8(v: u8) -> Identity {
Identity::from_byte_array([v; 32])
}
/// Insert rules into the RLS system table
fn insert_rls_rules(
db: &RelationalDB,
table_ids: impl IntoIterator<Item = TableId>,
rules: impl IntoIterator<Item = &'static str>,
) -> anyhow::Result<()> {
with_auto_commit(db, |tx| {
for (table_id, sql) in table_ids.into_iter().zip(rules) {
db.insert(
tx,
ST_ROW_LEVEL_SECURITY_ID,
&ProductValue::from(StRowLevelSecurityRow {
table_id,
sql: sql.into(),
})
.to_bsatn_vec()?,
)?;
}
Ok(())
})
}
/// Insert product values into a table
fn insert_rows(
db: &RelationalDB,
table_id: TableId,
rows: impl IntoIterator<Item = ProductValue>,
) -> anyhow::Result<()> {
with_auto_commit(db, |tx| {
for row in rows.into_iter() {
db.insert(tx, table_id, &row.to_bsatn_vec()?)?;
}
Ok(())
})
}
/// Assert this query returns the expected rows for this user
fn assert_query_results(
db: &RelationalDB,
sql: &str,
auth: &AuthCtx,
expected: impl IntoIterator<Item = ProductValue>,
) {
assert_eq!(
run(db, sql, *auth, None, &mut vec![])
.unwrap()
.rows
.into_iter()
.sorted()
.dedup()
.collect::<Vec<_>>(),
expected.into_iter().sorted().dedup().collect::<Vec<_>>()
);
}
/// Test a query that uses a multi-column index
#[test]
fn test_multi_column_index() -> anyhow::Result<()> {
let db = TestDB::in_memory()?;
let schema = [
("a", AlgebraicType::U64),
("b", AlgebraicType::U64),
("c", AlgebraicType::U64),
];
let table_id = db.create_table_for_test_multi_column("t", &schema, [1, 2].into())?;
insert_rows(
&db,
table_id,
vec![
product![0_u64, 1_u64, 2_u64],
product![1_u64, 2_u64, 1_u64],
product![2_u64, 2_u64, 2_u64],
],
)?;
assert_query_results(
&db,
"select * from t where c = 1 and b = 2",
&AuthCtx::for_testing(),
[product![1_u64, 2_u64, 1_u64]],
);
Ok(())
}
/// Test querying a table with RLS rules
#[test]
fn test_rls_rules() -> anyhow::Result<()> {
let db = TestDB::in_memory()?;
let id_for_a = identity_from_u8(1);
let id_for_b = identity_from_u8(2);
let users_schema = [("identity", AlgebraicType::identity())];
let sales_schema = [
("order_id", AlgebraicType::U64),
("customer", AlgebraicType::identity()),
];
let users_table_id = db.create_table_for_test("users", &users_schema, &[])?;
let sales_table_id = db.create_table_for_test("sales", &sales_schema, &[])?;
insert_rows(&db, users_table_id, vec![product![id_for_a], product![id_for_b]])?;
insert_rows(
&db,
sales_table_id,
vec![
product![1u64, id_for_a],
product![2u64, id_for_b],
product![3u64, id_for_a],
product![4u64, id_for_b],
],
)?;
insert_rls_rules(
&db,
[users_table_id, sales_table_id],
[
"select * from users where identity = :sender",
"select s.* from users u join sales s on u.identity = s.customer",
],
)?;
let auth_for_a = AuthCtx::new(Identity::ZERO, id_for_a);
let auth_for_b = AuthCtx::new(Identity::ZERO, id_for_b);
assert_query_results(
&db,
// Should only return the identity for sender "a"
"select * from users",
&auth_for_a,
[product![id_for_a]],
);
assert_query_results(
&db,
// Should only return the identity for sender "b"
"select * from users",
&auth_for_b,
[product![id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
"select * from users where identity = :sender",
&auth_for_a,
[product![id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
"select * from users where identity = :sender",
&auth_for_b,
[product![id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
&format!("select * from users where identity = 0x{}", id_for_a.to_hex()),
&auth_for_a,
[product![id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
&format!("select * from users where identity = 0x{}", id_for_b.to_hex()),
&auth_for_b,
[product![id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
&format!(
"select * from users where identity = :sender and identity = 0x{}",
id_for_a.to_hex()
),
&auth_for_a,
[product![id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
&format!(
"select * from users where identity = :sender and identity = 0x{}",
id_for_b.to_hex()
),
&auth_for_b,
[product![id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
&format!(
"select * from users where identity = :sender or identity = 0x{}",
id_for_b.to_hex()
),
&auth_for_a,
[product![id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
&format!(
"select * from users where identity = :sender or identity = 0x{}",
id_for_a.to_hex()
),
&auth_for_b,
[product![id_for_b]],
);
assert_query_results(
&db,
// Should not return any rows.
// Querying as sender "a", but filtering on sender "b".
&format!("select * from users where identity = 0x{}", id_for_b.to_hex()),
&auth_for_a,
[],
);
assert_query_results(
&db,
// Should not return any rows.
// Querying as sender "b", but filtering on sender "a".
&format!("select * from users where identity = 0x{}", id_for_a.to_hex()),
&auth_for_b,
[],
);
assert_query_results(
&db,
// Should not return any rows.
// Querying as sender "a", but filtering on sender "b".
&format!(
"select * from users where identity = :sender and identity = 0x{}",
id_for_b.to_hex()
),
&auth_for_a,
[],
);
assert_query_results(
&db,
// Should not return any rows.
// Querying as sender "b", but filtering on sender "a".
&format!(
"select * from users where identity = :sender and identity = 0x{}",
id_for_a.to_hex()
),
&auth_for_b,
[],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
"select * from sales",
&auth_for_a,
[product![1u64, id_for_a], product![3u64, id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
"select * from sales",
&auth_for_b,
[product![2u64, id_for_b], product![4u64, id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
"select s.* from users u join sales s on u.identity = s.customer",
&auth_for_a,
[product![1u64, id_for_a], product![3u64, id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
"select s.* from users u join sales s on u.identity = s.customer",
&auth_for_b,
[product![2u64, id_for_b], product![4u64, id_for_b]],
);
assert_query_results(
&db,
// Should only return the orders for sender "a"
"select s.* from users u join sales s on u.identity = s.customer where u.identity = :sender",
&auth_for_a,
[product![1u64, id_for_a], product![3u64, id_for_a]],
);
assert_query_results(
&db,
// Should only return the orders for sender "b"
"select s.* from users u join sales s on u.identity = s.customer where u.identity = :sender",
&auth_for_b,
[product![2u64, id_for_b], product![4u64, id_for_b]],
);
Ok(())
}
/// Test querying tables with multiple levels of RLS rules
#[test]
fn test_nested_rls_rules() -> anyhow::Result<()> {
let db = TestDB::in_memory()?;
let id_for_a = identity_from_u8(1);
let id_for_b = identity_from_u8(2);
let id_for_c = identity_from_u8(3);
let users_schema = [("identity", AlgebraicType::identity())];
let sales_schema = [
("order_id", AlgebraicType::U64),
("product_id", AlgebraicType::U64),
("customer", AlgebraicType::identity()),
];
let users_table_id = db.create_table_for_test("users", &users_schema, &[0.into()])?;
let admin_table_id = db.create_table_for_test("admins", &users_schema, &[0.into()])?;
let sales_table_id = db.create_table_for_test("sales", &sales_schema, &[0.into()])?;
insert_rows(&db, admin_table_id, [product![id_for_c]])?;
insert_rows(
&db,
users_table_id,
[product![id_for_a], product![id_for_b], product![id_for_c]],
)?;
insert_rows(
&db,
sales_table_id,
[product![1u64, 1u64, id_for_a], product![2u64, 2u64, id_for_b]],
)?;
insert_rls_rules(
&db,
[admin_table_id, users_table_id, users_table_id, sales_table_id],
[
"select * from admins where identity = :sender",
"select * from users where identity = :sender",
"select users.* from admins join users",
"select s.* from users u join sales s on u.identity = s.customer",
],
)?;
let auth_for_a = AuthCtx::new(Identity::ZERO, id_for_a);
let auth_for_b = AuthCtx::new(Identity::ZERO, id_for_b);
let auth_for_c = AuthCtx::new(Identity::ZERO, id_for_c);
assert_query_results(
&db,
"select * from admins",
&auth_for_a,
// Identity "a" is not an admin
[],
);
assert_query_results(
&db,
"select * from admins",
&auth_for_b,
// Identity "b" is not an admin
[],
);
assert_query_results(
&db,
"select * from admins",
&auth_for_c,
// Identity "c" is an admin
[product![id_for_c]],
);
assert_query_results(
&db,
"select * from users",
&auth_for_a,
// Identity "a" can only see its own user
vec![product![id_for_a]],
);
assert_query_results(
&db,
"select * from users",
&auth_for_b,
// Identity "b" can only see its own user
vec![product![id_for_b]],
);
assert_query_results(
&db,
"select * from users",
&auth_for_c,
// Identity "c" is an admin so it can see everyone's users
[product![id_for_a], product![id_for_b], product![id_for_c]],
);
assert_query_results(
&db,
"select * from sales",
&auth_for_a,
// Identity "a" can only see its own orders
[product![1u64, 1u64, id_for_a]],
);
assert_query_results(
&db,
"select * from sales",
&auth_for_b,
// Identity "b" can only see its own orders
[product![2u64, 2u64, id_for_b]],
);
assert_query_results(
&db,
"select * from sales",
&auth_for_c,
// Identity "c" is an admin so it can see everyone's orders
[product![1u64, 1u64, id_for_a], product![2u64, 2u64, id_for_b]],
);
Ok(())
}
/// Test projecting columns from both tables in join
#[test]
fn test_project_join() -> anyhow::Result<()> {
let db = TestDB::in_memory()?;
let t_schema = [("id", AlgebraicType::U8), ("x", AlgebraicType::U8)];
let s_schema = [("id", AlgebraicType::U8), ("y", AlgebraicType::U8)];
let t_id = db.create_table_for_test("t", &t_schema, &[0.into()])?;
let s_id = db.create_table_for_test("s", &s_schema, &[0.into()])?;
insert_rows(&db, t_id, [product![1_u8, 2_u8]])?;
insert_rows(&db, s_id, [product![1_u8, 3_u8]])?;
let id = identity_from_u8(1);
let auth = AuthCtx::new(Identity::ZERO, id);
assert_query_results(
&db,
"select t.x, s.y from t join s on t.id = s.id",
&auth,
[product![2_u8, 3_u8]],
);
Ok(())
}
#[test]
fn test_select_star_table() -> ResultTest<()> {
let (db, input) = create_data(1)?;
let result = run_for_testing(&db, "SELECT inventory.* FROM inventory")?;
assert_eq!(result, input.data, "Inventory");
let result = run_for_testing(
&db,
"SELECT inventory.inventory_id FROM inventory WHERE inventory.inventory_id = 1",
)?;
assert_eq!(result, vec![product!(1u64)], "Inventory");
Ok(())
}
#[test]
fn test_select_catalog() -> ResultTest<()> {
let (db, _) = create_data(1)?;
let tx = begin_tx(&db);
let _ = db.release_tx(tx);
let result = run_for_testing(
&db,
&format!("SELECT * FROM {} WHERE table_id = {}", ST_TABLE_NAME, ST_TABLE_ID),
)?;
let pk_col_id: ColId = StTableFields::TableId.into();
let row = product![
ST_TABLE_ID,
ST_TABLE_NAME,
StTableType::System.as_str(),
StAccess::Public.as_str(),
Some(AlgebraicValue::Array(ArrayValue::U16(vec![pk_col_id.0].into()))),
];
assert_eq!(result, vec![row], "st_table");
Ok(())
}
#[test]
fn test_select_column() -> ResultTest<()> {
let (db, _) = create_data(1)?;
let result = run_for_testing(&db, "SELECT inventory_id FROM inventory")?;
let row = product![1u64];
assert_eq!(result, vec![row], "Inventory");
Ok(())
}
#[test]
fn test_where() -> ResultTest<()> {
let (db, _) = create_data(1)?;
let result = run_for_testing(&db, "SELECT inventory_id FROM inventory WHERE inventory_id = 1")?;
let row = product![1u64];
assert_eq!(result, vec![row], "Inventory");
Ok(())
}
#[test]
fn test_or() -> ResultTest<()> {
let (db, _) = create_data(2)?;
let mut result = run_for_testing(
&db,
"SELECT inventory_id FROM inventory WHERE inventory_id = 1 OR inventory_id = 2",
)?;
result.sort();
assert_eq!(result, vec![product![1u64], product![2u64]], "Inventory");
Ok(())
}
#[test]
fn test_nested() -> ResultTest<()> {
let (db, _) = create_data(2)?;
let mut result = run_for_testing(
&db,
"SELECT inventory_id FROM inventory WHERE (inventory_id = 1 OR inventory_id = 2 AND (true))",
)?;
result.sort();
assert_eq!(result, vec![product![1u64], product![2u64]], "Inventory");
Ok(())
}
#[test]
fn test_inner_join() -> ResultTest<()> {
let data = create_game_data();
let db = TestDB::durable()?;
with_auto_commit::<_, TestError>(&db, |tx| {
let i = create_table_with_rows(&db, tx, "Inventory", data.inv_ty, &data.inv.data, StAccess::Public)?;
let p = create_table_with_rows(&db, tx, "Player", data.player_ty, &data.player.data, StAccess::Public)?;
create_table_with_rows(
&db,
tx,
"Location",
data.location_ty,
&data.location.data,
StAccess::Public,
)?;
Ok((p, i))
})?;
let result = run_for_testing(
&db,
"SELECT
Player.*
FROM
Player