@@ -14,16 +14,20 @@ mod ddl_tests;
1414#[ cfg( test) ]
1515mod query_tests;
1616
17+ pub ( crate ) mod dsl;
1718pub ( crate ) mod index;
1819mod prune;
1920mod rollup;
21+ pub ( crate ) mod value;
2022
2123use diesel:: deserialize:: FromSql ;
2224use diesel:: pg:: Pg ;
2325use diesel:: serialize:: { Output , ToSql } ;
2426use diesel:: sql_types:: Text ;
2527use diesel:: { connection:: SimpleConnection , Connection } ;
26- use diesel:: { debug_query, sql_query, OptionalExtension , PgConnection , QueryResult , RunQueryDsl } ;
28+ use diesel:: {
29+ debug_query, sql_query, OptionalExtension , PgConnection , QueryDsl , QueryResult , RunQueryDsl ,
30+ } ;
2731use graph:: blockchain:: BlockTime ;
2832use graph:: cheap_clone:: CheapClone ;
2933use graph:: components:: store:: write:: { RowGroup , WriteChunk } ;
@@ -50,6 +54,7 @@ use std::str::FromStr;
5054use std:: sync:: { Arc , Mutex } ;
5155use std:: time:: { Duration , Instant } ;
5256
57+ use crate :: relational:: value:: { FromOidRow , OidRow } ;
5358use crate :: relational_queries:: {
5459 ConflictingEntitiesData , ConflictingEntitiesQuery , FindChangesQuery , FindDerivedQuery ,
5560 FindPossibleDeletionsQuery , ReturnedEntityData ,
@@ -58,10 +63,10 @@ use crate::{
5863 primary:: { Namespace , Site } ,
5964 relational_queries:: {
6065 ClampRangeQuery , EntityData , EntityDeletion , FilterCollection , FilterQuery , FindManyQuery ,
61- FindQuery , InsertQuery , RevertClampQuery , RevertRemoveQuery ,
66+ InsertQuery , RevertClampQuery , RevertRemoveQuery ,
6267 } ,
6368} ;
64- use graph:: components:: store:: DerivedEntityQuery ;
69+ use graph:: components:: store:: { AttributeNames , DerivedEntityQuery } ;
6570use graph:: data:: store:: { Id , IdList , IdType , BYTES_SCALAR } ;
6671use graph:: data:: subgraph:: schema:: POI_TABLE ;
6772use graph:: prelude:: {
@@ -172,6 +177,12 @@ impl From<String> for SqlName {
172177 }
173178}
174179
180+ impl From < SqlName > for Word {
181+ fn from ( name : SqlName ) -> Self {
182+ Word :: from ( name. 0 )
183+ }
184+ }
185+
175186impl fmt:: Display for SqlName {
176187 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
177188 self . 0 . fmt ( f)
@@ -184,6 +195,12 @@ impl Borrow<str> for &SqlName {
184195 }
185196}
186197
198+ impl PartialEq < str > for SqlName {
199+ fn eq ( & self , other : & str ) -> bool {
200+ self . 0 == other
201+ }
202+ }
203+
187204impl FromSql < Text , Pg > for SqlName {
188205 fn from_sql ( bytes : diesel:: pg:: PgValue ) -> diesel:: deserialize:: Result < Self > {
189206 <String as FromSql < Text , Pg > >:: from_sql ( bytes) . map ( |s| SqlName :: verbatim ( s) )
@@ -361,9 +378,11 @@ impl Layout {
361378 }
362379
363380 let table_name = SqlName :: verbatim ( POI_TABLE . to_owned ( ) ) ;
381+ let nsp = catalog. site . namespace . clone ( ) ;
364382 Table {
365383 object : poi_type. to_owned ( ) ,
366384 qualified_name : SqlName :: qualified_name ( & catalog. site . namespace , & table_name) ,
385+ nsp,
367386 name : table_name,
368387 columns,
369388 // The position of this table in all the tables for this layout; this
@@ -469,11 +488,19 @@ impl Layout {
469488 key : & EntityKey ,
470489 block : BlockNumber ,
471490 ) -> Result < Option < Entity > , StoreError > {
472- let table = self . table_for_entity ( & key. entity_type ) ?;
473- FindQuery :: new ( table. as_ref ( ) , key, block)
474- . get_result :: < EntityData > ( conn)
491+ let table = self . table_for_entity ( & key. entity_type ) ?. dsl_table ( ) ;
492+ let columns = table. selected_columns :: < Entity > ( & AttributeNames :: All , None ) ?;
493+
494+ let query = table
495+ . select_cols ( & columns)
496+ . filter ( table. id_eq ( & key. entity_id ) )
497+ . filter ( table. at_block ( block) )
498+ . filter ( table. belongs_to_causality_region ( key. causality_region ) ) ;
499+
500+ query
501+ . get_result :: < OidRow > ( conn)
475502 . optional ( ) ?
476- . map ( |entity_data| entity_data . deserialize_with_layout ( self , None ) )
503+ . map ( |row| Entity :: from_oid_row ( row , & self . input_schema , & columns ) )
477504 . transpose ( )
478505 }
479506
@@ -1348,6 +1375,21 @@ impl Column {
13481375 } )
13491376 }
13501377
1378+ pub fn pseudo_column ( name : & str , column_type : ColumnType ) -> Column {
1379+ let field_type = q:: Type :: NamedType ( column_type. to_string ( ) ) ;
1380+ let name = SqlName :: verbatim ( name. to_string ( ) ) ;
1381+ let field = Word :: from ( name. as_str ( ) ) ;
1382+ Column {
1383+ name,
1384+ field,
1385+ field_type,
1386+ column_type,
1387+ fulltext_fields : None ,
1388+ is_reference : false ,
1389+ use_prefix_comparison : false ,
1390+ }
1391+ }
1392+
13511393 fn new_fulltext ( def : & FulltextDefinition ) -> Result < Column , StoreError > {
13521394 SqlName :: check_valid_identifier ( & def. name , "attribute" ) ?;
13531395 let sql_name = SqlName :: from ( def. name . as_str ( ) ) ;
@@ -1440,6 +1482,9 @@ pub struct Table {
14401482 /// `Stats_hour`, not the overall aggregation type `Stats`.
14411483 pub object : EntityType ,
14421484
1485+ /// The namespace in which the table lives
1486+ nsp : Namespace ,
1487+
14431488 /// The name of the database table for this type ('thing'), snakecased
14441489 /// version of `object`
14451490 pub name : SqlName ,
@@ -1494,10 +1539,11 @@ impl Table {
14941539 . collect :: < Result < Vec < Column > , StoreError > > ( ) ?;
14951540 let qualified_name = SqlName :: qualified_name ( & catalog. site . namespace , & table_name) ;
14961541 let immutable = defn. is_immutable ( ) ;
1497-
1542+ let nsp = catalog . site . namespace . clone ( ) ;
14981543 let table = Table {
14991544 object : defn. cheap_clone ( ) ,
15001545 name : table_name,
1546+ nsp,
15011547 qualified_name,
15021548 // Default `is_account_like` to `false`; the caller should call
15031549 // `refresh` after constructing the layout, but that requires a
@@ -1516,6 +1562,7 @@ impl Table {
15161562 pub fn new_like ( & self , namespace : & Namespace , name : & SqlName ) -> Arc < Table > {
15171563 let other = Table {
15181564 object : self . object . clone ( ) ,
1565+ nsp : self . nsp . clone ( ) ,
15191566 name : name. clone ( ) ,
15201567 qualified_name : SqlName :: qualified_name ( namespace, name) ,
15211568 columns : self . columns . clone ( ) ,
@@ -1590,6 +1637,10 @@ impl Table {
15901637 & crate :: block_range:: BLOCK_RANGE_COLUMN_SQL
15911638 }
15921639 }
1640+
1641+ pub fn dsl_table ( & self ) -> dsl:: Table < ' _ > {
1642+ dsl:: Table :: new ( self )
1643+ }
15931644}
15941645
15951646#[ derive( Clone ) ]
0 commit comments