@@ -227,7 +227,7 @@ pub fn run(
227227 } ) ;
228228
229229 // Evaluate the query
230- let rows = execute_select_stmt ( stmt, & DeltaTx :: from ( & * tx) , & mut metrics, |plan| {
230+ let rows = execute_select_stmt ( & auth , stmt, & DeltaTx :: from ( & * tx) , & mut metrics, |plan| {
231231 check_row_limit (
232232 & [ & plan] ,
233233 db,
@@ -254,7 +254,7 @@ pub fn run(
254254 }
255255
256256 // Evaluate the mutation
257- let ( mut tx, _) = db. with_auto_rollback ( tx, |tx| execute_dml_stmt ( stmt, tx, & mut metrics) ) ?;
257+ let ( mut tx, _) = db. with_auto_rollback ( tx, |tx| execute_dml_stmt ( & auth , stmt, tx, & mut metrics) ) ?;
258258
259259 // Update transaction metrics
260260 tx. metrics . merge ( metrics) ;
@@ -332,7 +332,7 @@ pub(crate) mod tests {
332332 use std:: sync:: Arc ;
333333
334334 use super :: * ;
335- use crate :: db:: relational_db:: tests_utils:: { begin_tx, insert, with_auto_commit, TestDB } ;
335+ use crate :: db:: relational_db:: tests_utils:: { self , begin_tx, insert, with_auto_commit, TestDB } ;
336336 use crate :: vm:: tests:: create_table_with_rows;
337337 use itertools:: Itertools ;
338338 use pretty_assertions:: assert_eq;
@@ -904,6 +904,158 @@ pub(crate) mod tests {
904904 Ok ( ( ) )
905905 }
906906
907+ #[ test]
908+ fn test_view ( ) -> anyhow:: Result < ( ) > {
909+ let db = TestDB :: in_memory ( ) ?;
910+
911+ let schema = [ ( "a" , AlgebraicType :: U8 ) , ( "b" , AlgebraicType :: U8 ) ] ;
912+ let table_id = tests_utils:: create_view_for_test ( & db, "my_view" , & schema, false ) ?;
913+
914+ with_auto_commit ( & db, |tx| -> Result < _ , DBError > {
915+ tests_utils:: insert_into_view ( & db, tx, table_id, Some ( identity_from_u8 ( 1 ) ) , product ! [ 0u8 , 1u8 ] ) ?;
916+ tests_utils:: insert_into_view ( & db, tx, table_id, Some ( identity_from_u8 ( 2 ) ) , product ! [ 0u8 , 2u8 ] ) ?;
917+ Ok ( ( ) )
918+ } ) ?;
919+
920+ let id = identity_from_u8 ( 2 ) ;
921+ let auth = AuthCtx :: new ( Identity :: ZERO , id) ;
922+
923+ assert_query_results ( & db, "select * from my_view" , & auth, [ product ! [ 0u8 , 2u8 ] ] ) ;
924+
925+ Ok ( ( ) )
926+ }
927+
928+ #[ test]
929+ fn test_anonymous_view ( ) -> anyhow:: Result < ( ) > {
930+ let db = TestDB :: in_memory ( ) ?;
931+
932+ let schema = [ ( "a" , AlgebraicType :: U8 ) , ( "b" , AlgebraicType :: U8 ) ] ;
933+ let table_id = tests_utils:: create_view_for_test ( & db, "my_view" , & schema, true ) ?;
934+
935+ with_auto_commit ( & db, |tx| -> Result < _ , DBError > {
936+ tests_utils:: insert_into_view ( & db, tx, table_id, None , product ! [ 0u8 , 1u8 ] ) ?;
937+ tests_utils:: insert_into_view ( & db, tx, table_id, None , product ! [ 0u8 , 2u8 ] ) ?;
938+ Ok ( ( ) )
939+ } ) ?;
940+
941+ let id = identity_from_u8 ( 1 ) ;
942+ let auth = AuthCtx :: new ( Identity :: ZERO , id) ;
943+
944+ assert_query_results ( & db, "select b from my_view" , & auth, [ product ! [ 1u8 ] , product ! [ 2u8 ] ] ) ;
945+
946+ Ok ( ( ) )
947+ }
948+
949+ #[ test]
950+ fn test_view_join_table ( ) -> anyhow:: Result < ( ) > {
951+ let db = TestDB :: in_memory ( ) ?;
952+
953+ let schema = [ ( "a" , AlgebraicType :: U8 ) , ( "b" , AlgebraicType :: U8 ) ] ;
954+ let v_id = tests_utils:: create_view_for_test ( & db, "v" , & schema, false ) ?;
955+
956+ let schema = [ ( "c" , AlgebraicType :: U8 ) , ( "d" , AlgebraicType :: U8 ) ] ;
957+ let t_id = db. create_table_for_test ( "t" , & schema, & [ 0 . into ( ) ] ) ?;
958+
959+ with_auto_commit ( & db, |tx| -> Result < _ , DBError > {
960+ db. insert ( tx, t_id, & product ! [ 0u8 , 3u8 ] . to_bsatn_vec ( ) . unwrap ( ) ) ?;
961+ db. insert ( tx, t_id, & product ! [ 1u8 , 4u8 ] . to_bsatn_vec ( ) . unwrap ( ) ) ?;
962+ tests_utils:: insert_into_view ( & db, tx, v_id, Some ( identity_from_u8 ( 1 ) ) , product ! [ 0u8 , 1u8 ] ) ?;
963+ tests_utils:: insert_into_view ( & db, tx, v_id, Some ( identity_from_u8 ( 2 ) ) , product ! [ 1u8 , 2u8 ] ) ?;
964+ Ok ( ( ) )
965+ } ) ?;
966+
967+ let id = identity_from_u8 ( 2 ) ;
968+ let auth = AuthCtx :: new ( Identity :: ZERO , id) ;
969+
970+ assert_query_results (
971+ & db,
972+ "select t.* from v join t on v.a = t.c" ,
973+ & auth,
974+ [ product ! [ 1u8 , 4u8 ] ] ,
975+ ) ;
976+ assert_query_results (
977+ & db,
978+ "select v.* from v join t on v.a = t.c" ,
979+ & auth,
980+ [ product ! [ 1u8 , 2u8 ] ] ,
981+ ) ;
982+ assert_query_results (
983+ & db,
984+ "select v.* from v join t where v.a = t.c" ,
985+ & auth,
986+ [ product ! [ 1u8 , 2u8 ] ] ,
987+ ) ;
988+ assert_query_results (
989+ & db,
990+ "select v.b as b, t.d as d from v join t on v.a = t.c" ,
991+ & auth,
992+ [ product ! [ 2u8 , 4u8 ] ] ,
993+ ) ;
994+ assert_query_results (
995+ & db,
996+ "select v.b as b, t.d as d from v join t where v.a = t.c" ,
997+ & auth,
998+ [ product ! [ 2u8 , 4u8 ] ] ,
999+ ) ;
1000+
1001+ Ok ( ( ) )
1002+ }
1003+
1004+ #[ test]
1005+ fn test_view_join_view ( ) -> anyhow:: Result < ( ) > {
1006+ let db = TestDB :: in_memory ( ) ?;
1007+
1008+ let schema = [ ( "a" , AlgebraicType :: U8 ) , ( "b" , AlgebraicType :: U8 ) ] ;
1009+ let u_id = tests_utils:: create_view_for_test ( & db, "u" , & schema, false ) ?;
1010+
1011+ let schema = [ ( "c" , AlgebraicType :: U8 ) , ( "d" , AlgebraicType :: U8 ) ] ;
1012+ let v_id = tests_utils:: create_view_for_test ( & db, "v" , & schema, false ) ?;
1013+
1014+ with_auto_commit ( & db, |tx| -> Result < _ , DBError > {
1015+ tests_utils:: insert_into_view ( & db, tx, u_id, Some ( identity_from_u8 ( 1 ) ) , product ! [ 0u8 , 1u8 ] ) ?;
1016+ tests_utils:: insert_into_view ( & db, tx, u_id, Some ( identity_from_u8 ( 2 ) ) , product ! [ 1u8 , 2u8 ] ) ?;
1017+ tests_utils:: insert_into_view ( & db, tx, v_id, Some ( identity_from_u8 ( 1 ) ) , product ! [ 0u8 , 3u8 ] ) ?;
1018+ tests_utils:: insert_into_view ( & db, tx, v_id, Some ( identity_from_u8 ( 2 ) ) , product ! [ 1u8 , 4u8 ] ) ?;
1019+ Ok ( ( ) )
1020+ } ) ?;
1021+
1022+ let id = identity_from_u8 ( 2 ) ;
1023+ let auth = AuthCtx :: new ( Identity :: ZERO , id) ;
1024+
1025+ assert_query_results (
1026+ & db,
1027+ "select u.* from u join v on u.a = v.c" ,
1028+ & auth,
1029+ [ product ! [ 1u8 , 2u8 ] ] ,
1030+ ) ;
1031+ assert_query_results (
1032+ & db,
1033+ "select v.* from u join v on u.a = v.c" ,
1034+ & auth,
1035+ [ product ! [ 1u8 , 4u8 ] ] ,
1036+ ) ;
1037+ assert_query_results (
1038+ & db,
1039+ "select v.* from u join v where u.a = v.c" ,
1040+ & auth,
1041+ [ product ! [ 1u8 , 4u8 ] ] ,
1042+ ) ;
1043+ assert_query_results (
1044+ & db,
1045+ "select u.b as b, v.d as d from u join v on u.a = v.c" ,
1046+ & auth,
1047+ [ product ! [ 2u8 , 4u8 ] ] ,
1048+ ) ;
1049+ assert_query_results (
1050+ & db,
1051+ "select u.b as b, v.d as d from u join v where u.a = v.c" ,
1052+ & auth,
1053+ [ product ! [ 2u8 , 4u8 ] ] ,
1054+ ) ;
1055+
1056+ Ok ( ( ) )
1057+ }
1058+
9071059 #[ test]
9081060 fn test_select_star_table ( ) -> ResultTest < ( ) > {
9091061 let ( db, input) = create_data ( 1 ) ?;
0 commit comments