1- use std:: sync:: Arc ;
1+ use std:: { collections :: HashSet , sync:: Arc } ;
22
33use spacetimedb_lib:: { query:: Delta , AlgebraicType , AlgebraicValue } ;
4- use spacetimedb_primitives:: TableId ;
4+ use spacetimedb_primitives:: { TableId , ViewId } ;
55use spacetimedb_schema:: schema:: TableOrViewSchema ;
66use spacetimedb_sql_parser:: ast:: { BinOp , LogOp } ;
77
8+ pub trait CollectViews {
9+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) ;
10+ }
11+
12+ impl < T : CollectViews > CollectViews for Arc < T > {
13+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) {
14+ self . as_ref ( ) . collect_views ( views) ;
15+ }
16+ }
17+
18+ impl < T : CollectViews > CollectViews for Vec < T > {
19+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) {
20+ for item in self {
21+ item. collect_views ( views) ;
22+ }
23+ }
24+ }
25+
826/// A projection is the root of any relational expression.
927/// This type represents a projection that returns relvars.
1028///
@@ -25,6 +43,14 @@ pub enum ProjectName {
2543 Some ( RelExpr , Box < str > ) ,
2644}
2745
46+ impl CollectViews for ProjectName {
47+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) {
48+ match self {
49+ Self :: None ( expr) | Self :: Some ( expr, _) => expr. collect_views ( views) ,
50+ }
51+ }
52+ }
53+
2854impl ProjectName {
2955 /// Unwrap the outer projection, returning the inner expression
3056 pub fn unwrap ( self ) -> RelExpr {
@@ -146,6 +172,26 @@ pub enum AggType {
146172 Count ,
147173}
148174
175+ impl CollectViews for ProjectList {
176+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) {
177+ match self {
178+ Self :: Limit ( proj, _) => {
179+ proj. collect_views ( views) ;
180+ }
181+ Self :: Name ( exprs) => {
182+ for expr in exprs {
183+ expr. collect_views ( views) ;
184+ }
185+ }
186+ Self :: List ( exprs, _) | Self :: Agg ( exprs, ..) => {
187+ for expr in exprs {
188+ expr. collect_views ( views) ;
189+ }
190+ }
191+ }
192+ }
193+ }
194+
149195impl ProjectList {
150196 /// Does this expression project a single relvar?
151197 /// If so, we return it's [`TableOrViewSchema`].
@@ -212,6 +258,18 @@ pub struct Relvar {
212258 pub delta : Option < Delta > ,
213259}
214260
261+ impl CollectViews for RelExpr {
262+ fn collect_views ( & self , views : & mut HashSet < ViewId > ) {
263+ self . visit ( & mut |expr| {
264+ if let Self :: RelVar ( Relvar { schema, .. } ) = expr {
265+ if let Some ( info) = schema. view_info {
266+ views. insert ( info. view_id ) ;
267+ }
268+ }
269+ } ) ;
270+ }
271+ }
272+
215273impl RelExpr {
216274 /// Walk the expression tree and call `f` on each node
217275 pub fn visit ( & self , f : & mut impl FnMut ( & Self ) ) {
0 commit comments