Skip to content

Commit b7851fc

Browse files
committed
Revert "graph, store: Avoid using to_jsonb when looking up a single entity (#5372)"
This reverts commits: - 605c6d2. - 989c980. - 7848017. - 4f2b3f8. - 2f77063. - d3ff34d. - 8c8dbb4.
1 parent c020fb7 commit b7851fc

File tree

13 files changed

+871
-1741
lines changed

13 files changed

+871
-1741
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ axum = "0.7.5"
3333
chrono = "0.4.38"
3434
clap = { version = "4.5.4", features = ["derive", "env"] }
3535
derivative = "2.2.0"
36-
diesel = { version = "2.2.4", features = ["postgres", "serde_json", "numeric", "r2d2", "chrono", "uuid", "i-implement-a-third-party-backend-and-opt-into-breaking-changes"] }
36+
diesel = { version = "2.2.4", features = ["postgres", "serde_json", "numeric", "r2d2", "chrono", "uuid"] }
3737
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
38-
diesel-dynamic-schema = { version = "0.2.1", features = ["postgres"] }
38+
diesel-dynamic-schema = "0.2.1"
3939
diesel_derives = "2.1.4"
4040
diesel_migrations = "2.1.0"
4141
graph = { path = "./graph" }

graph/src/data/store/scalar/bigdecimal.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use diesel::deserialize::FromSqlRow;
22
use diesel::expression::AsExpression;
3-
use num_bigint::{self, ToBigInt};
3+
use num_bigint;
44
use num_traits::FromPrimitive;
55
use serde::{self, Deserialize, Serialize};
66
use stable_hash::{FieldAddress, StableHash};
@@ -10,8 +10,8 @@ use std::fmt::{self, Display, Formatter};
1010
use std::ops::{Add, Div, Mul, Sub};
1111
use std::str::FromStr;
1212

13-
use crate::anyhow::anyhow;
1413
use crate::runtime::gas::{Gas, GasSizeOf};
14+
1515
use old_bigdecimal::BigDecimal as OldBigDecimal;
1616
pub use old_bigdecimal::ToPrimitive;
1717

@@ -60,26 +60,6 @@ impl BigDecimal {
6060
self.0.as_bigint_and_exponent()
6161
}
6262

63-
pub fn is_integer(&self) -> bool {
64-
self.0.is_integer()
65-
}
66-
67-
/// Convert this `BigDecimal` to a `BigInt` if it is an integer, and
68-
/// return an error if it is not. Also return an error if the integer
69-
/// would use too many digits as definied by `BigInt::new`
70-
pub fn to_bigint(&self) -> Result<BigInt, anyhow::Error> {
71-
if !self.is_integer() {
72-
return Err(anyhow!(
73-
"Cannot convert non-integer `BigDecimal` to `BigInt`: {:?}",
74-
self
75-
));
76-
}
77-
let bi = self.0.to_bigint().ok_or_else(|| {
78-
anyhow!("The implementation of `to_bigint` for `OldBigDecimal` always returns `Some`")
79-
})?;
80-
BigInt::new(bi)
81-
}
82-
8363
pub fn digits(&self) -> u64 {
8464
self.0.digits()
8565
}

graph/src/data/store/scalar/bytes.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use diesel::deserialize::FromSql;
2-
use diesel::pg::PgValue;
31
use diesel::serialize::ToSql;
42
use hex;
53
use serde::{self, Deserialize, Serialize};
@@ -117,9 +115,3 @@ impl ToSql<diesel::sql_types::Binary, diesel::pg::Pg> for Bytes {
117115
<_ as ToSql<diesel::sql_types::Binary, _>>::to_sql(self.as_slice(), &mut out.reborrow())
118116
}
119117
}
120-
121-
impl FromSql<diesel::sql_types::Binary, diesel::pg::Pg> for Bytes {
122-
fn from_sql(value: PgValue) -> diesel::deserialize::Result<Self> {
123-
<Vec<u8> as FromSql<diesel::sql_types::Binary, _>>::from_sql(value).map(Bytes::from)
124-
}
125-
}

graph/src/data/store/scalar/timestamp.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use chrono::{DateTime, Utc};
2-
use diesel::deserialize::FromSql;
3-
use diesel::pg::PgValue;
42
use diesel::serialize::ToSql;
53
use diesel::sql_types::Timestamptz;
64
use serde::{self, Deserialize, Serialize};
@@ -110,10 +108,3 @@ impl GasSizeOf for Timestamp {
110108
Some(Gas::new(std::mem::size_of::<Timestamp>().saturating_into()))
111109
}
112110
}
113-
114-
impl FromSql<diesel::sql_types::Timestamptz, diesel::pg::Pg> for Timestamp {
115-
fn from_sql(value: PgValue) -> diesel::deserialize::Result<Self> {
116-
<DateTime<Utc> as FromSql<diesel::sql_types::Timestamptz, _>>::from_sql(value)
117-
.map(Timestamp)
118-
}
119-
}

graph/src/data/value.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,6 @@ impl PartialEq<&str> for Word {
115115
}
116116
}
117117

118-
impl PartialEq<str> for Word {
119-
fn eq(&self, other: &str) -> bool {
120-
self.as_str() == other
121-
}
122-
}
123-
124-
impl PartialEq<String> for Word {
125-
fn eq(&self, other: &String) -> bool {
126-
self.as_str() == other
127-
}
128-
}
129-
130-
impl PartialEq<Word> for String {
131-
fn eq(&self, other: &Word) -> bool {
132-
self.as_str() == other.as_str()
133-
}
134-
}
135-
136118
impl PartialEq<Word> for &str {
137119
fn eq(&self, other: &Word) -> bool {
138120
self == &other.as_str()

graph/src/data_source/causality_region.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use diesel::{
44
serialize::{Output, ToSql},
55
sql_types::Integer,
66
};
7-
use diesel_derives::AsExpression;
87
use std::fmt;
98

109
use crate::components::subgraph::Entity;
@@ -21,10 +20,7 @@ use crate::derive::CacheWeight;
2120
/// This necessary for determinism because offchain data sources don't have a deterministic order of
2221
/// execution, for example an IPFS file may become available at any point in time. The isolation
2322
/// rules make the indexing result reproducible, given a set of available files.
24-
#[derive(
25-
Debug, CacheWeight, Copy, Clone, PartialEq, Eq, FromSqlRow, Hash, PartialOrd, Ord, AsExpression,
26-
)]
27-
#[diesel(sql_type = Integer)]
23+
#[derive(Debug, CacheWeight, Copy, Clone, PartialEq, Eq, FromSqlRow, Hash, PartialOrd, Ord)]
2824
pub struct CausalityRegion(i32);
2925

3026
impl fmt::Display for CausalityRegion {

store/postgres/src/block_range.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ impl<'a> BlockRangeColumn<'a> {
165165
}
166166
}
167167
}
168+
169+
pub fn block(&self) -> BlockNumber {
170+
match self {
171+
BlockRangeColumn::Mutable { block, .. } => *block,
172+
BlockRangeColumn::Immutable { block, .. } => *block,
173+
}
174+
}
168175
}
169176

170177
impl<'a> BlockRangeColumn<'a> {
@@ -220,6 +227,13 @@ impl<'a> BlockRangeColumn<'a> {
220227
}
221228
}
222229

230+
pub fn column_name(&self) -> &str {
231+
match self {
232+
BlockRangeColumn::Mutable { .. } => BLOCK_RANGE_COLUMN,
233+
BlockRangeColumn::Immutable { .. } => BLOCK_COLUMN,
234+
}
235+
}
236+
223237
/// Output the qualified name of the block range column
224238
pub fn name(&self, out: &mut AstPass<Pg>) {
225239
match self {
@@ -266,6 +280,14 @@ impl<'a> BlockRangeColumn<'a> {
266280
}
267281
}
268282

283+
/// Output the name of the block range column without the table prefix
284+
pub(crate) fn bare_name(&self, out: &mut AstPass<Pg>) {
285+
match self {
286+
BlockRangeColumn::Mutable { .. } => out.push_sql(BLOCK_RANGE_COLUMN),
287+
BlockRangeColumn::Immutable { .. } => out.push_sql(BLOCK_COLUMN),
288+
}
289+
}
290+
269291
/// Output an expression that matches all rows that have been changed
270292
/// after `block` (inclusive)
271293
pub(crate) fn changed_since<'b>(&'b self, out: &mut AstPass<'_, 'b, Pg>) -> QueryResult<()> {

store/postgres/src/primary.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,6 @@ impl Borrow<str> for Namespace {
296296
}
297297
}
298298

299-
impl Borrow<str> for &Namespace {
300-
fn borrow(&self) -> &str {
301-
&self.0
302-
}
303-
}
304-
305299
/// A marker that an `i32` references a deployment. Values of this type hold
306300
/// the primary key from the `deployment_schemas` table
307301
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, AsExpression, FromSqlRow)]

store/postgres/src/relational.rs

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@ mod ddl_tests;
1414
#[cfg(test)]
1515
mod query_tests;
1616

17-
pub(crate) mod dsl;
1817
pub(crate) mod index;
1918
mod prune;
2019
mod rollup;
21-
pub(crate) mod value;
2220

2321
use diesel::deserialize::FromSql;
2422
use diesel::pg::Pg;
2523
use diesel::serialize::{Output, ToSql};
2624
use diesel::sql_types::Text;
2725
use diesel::{connection::SimpleConnection, Connection};
28-
use diesel::{
29-
debug_query, sql_query, OptionalExtension, PgConnection, QueryDsl, QueryResult, RunQueryDsl,
30-
};
26+
use diesel::{debug_query, sql_query, OptionalExtension, PgConnection, QueryResult, RunQueryDsl};
3127
use graph::blockchain::BlockTime;
3228
use graph::cheap_clone::CheapClone;
3329
use graph::components::store::write::{RowGroup, WriteChunk};
@@ -54,7 +50,6 @@ use std::str::FromStr;
5450
use std::sync::{Arc, Mutex};
5551
use std::time::{Duration, Instant};
5652

57-
use crate::relational::value::{FromOidRow, OidRow};
5853
use crate::relational_queries::{
5954
ConflictingEntitiesData, ConflictingEntitiesQuery, FindChangesQuery, FindDerivedQuery,
6055
FindPossibleDeletionsQuery, ReturnedEntityData,
@@ -63,10 +58,10 @@ use crate::{
6358
primary::{Namespace, Site},
6459
relational_queries::{
6560
ClampRangeQuery, EntityData, EntityDeletion, FilterCollection, FilterQuery, FindManyQuery,
66-
InsertQuery, RevertClampQuery, RevertRemoveQuery,
61+
FindQuery, InsertQuery, RevertClampQuery, RevertRemoveQuery,
6762
},
6863
};
69-
use graph::components::store::{AttributeNames, DerivedEntityQuery};
64+
use graph::components::store::DerivedEntityQuery;
7065
use graph::data::store::{Id, IdList, IdType, BYTES_SCALAR};
7166
use graph::data::subgraph::schema::POI_TABLE;
7267
use graph::prelude::{
@@ -177,12 +172,6 @@ impl From<String> for SqlName {
177172
}
178173
}
179174

180-
impl From<SqlName> for Word {
181-
fn from(name: SqlName) -> Self {
182-
Word::from(name.0)
183-
}
184-
}
185-
186175
impl fmt::Display for SqlName {
187176
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188177
self.0.fmt(f)
@@ -195,12 +184,6 @@ impl Borrow<str> for &SqlName {
195184
}
196185
}
197186

198-
impl PartialEq<str> for SqlName {
199-
fn eq(&self, other: &str) -> bool {
200-
self.0 == other
201-
}
202-
}
203-
204187
impl FromSql<Text, Pg> for SqlName {
205188
fn from_sql(bytes: diesel::pg::PgValue) -> diesel::deserialize::Result<Self> {
206189
<String as FromSql<Text, Pg>>::from_sql(bytes).map(|s| SqlName::verbatim(s))
@@ -378,11 +361,9 @@ impl Layout {
378361
}
379362

380363
let table_name = SqlName::verbatim(POI_TABLE.to_owned());
381-
let nsp = catalog.site.namespace.clone();
382364
Table {
383365
object: poi_type.to_owned(),
384366
qualified_name: SqlName::qualified_name(&catalog.site.namespace, &table_name),
385-
nsp,
386367
name: table_name,
387368
columns,
388369
// The position of this table in all the tables for this layout; this
@@ -488,19 +469,11 @@ impl Layout {
488469
key: &EntityKey,
489470
block: BlockNumber,
490471
) -> Result<Option<Entity>, StoreError> {
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)
472+
let table = self.table_for_entity(&key.entity_type)?;
473+
FindQuery::new(table.as_ref(), key, block)
474+
.get_result::<EntityData>(conn)
502475
.optional()?
503-
.map(|row| Entity::from_oid_row(row, &self.input_schema, &columns))
476+
.map(|entity_data| entity_data.deserialize_with_layout(self, None))
504477
.transpose()
505478
}
506479

@@ -1389,21 +1362,6 @@ impl Column {
13891362
})
13901363
}
13911364

1392-
pub fn pseudo_column(name: &str, column_type: ColumnType) -> Column {
1393-
let field_type = q::Type::NamedType(column_type.to_string());
1394-
let name = SqlName::verbatim(name.to_string());
1395-
let field = Word::from(name.as_str());
1396-
Column {
1397-
name,
1398-
field,
1399-
field_type,
1400-
column_type,
1401-
fulltext_fields: None,
1402-
is_reference: false,
1403-
use_prefix_comparison: false,
1404-
}
1405-
}
1406-
14071365
fn new_fulltext(def: &FulltextDefinition) -> Result<Column, StoreError> {
14081366
SqlName::check_valid_identifier(&def.name, "attribute")?;
14091367
let sql_name = SqlName::from(def.name.as_str());
@@ -1496,9 +1454,6 @@ pub struct Table {
14961454
/// `Stats_hour`, not the overall aggregation type `Stats`.
14971455
pub object: EntityType,
14981456

1499-
/// The namespace in which the table lives
1500-
nsp: Namespace,
1501-
15021457
/// The name of the database table for this type ('thing'), snakecased
15031458
/// version of `object`
15041459
pub name: SqlName,
@@ -1553,11 +1508,10 @@ impl Table {
15531508
.collect::<Result<Vec<Column>, StoreError>>()?;
15541509
let qualified_name = SqlName::qualified_name(&catalog.site.namespace, &table_name);
15551510
let immutable = defn.is_immutable();
1556-
let nsp = catalog.site.namespace.clone();
1511+
15571512
let table = Table {
15581513
object: defn.cheap_clone(),
15591514
name: table_name,
1560-
nsp,
15611515
qualified_name,
15621516
// Default `is_account_like` to `false`; the caller should call
15631517
// `refresh` after constructing the layout, but that requires a
@@ -1576,7 +1530,6 @@ impl Table {
15761530
pub fn new_like(&self, namespace: &Namespace, name: &SqlName) -> Arc<Table> {
15771531
let other = Table {
15781532
object: self.object.clone(),
1579-
nsp: self.nsp.clone(),
15801533
name: name.clone(),
15811534
qualified_name: SqlName::qualified_name(namespace, name),
15821535
columns: self.columns.clone(),
@@ -1651,10 +1604,6 @@ impl Table {
16511604
&crate::block_range::BLOCK_RANGE_COLUMN_SQL
16521605
}
16531606
}
1654-
1655-
pub fn dsl_table(&self) -> dsl::Table<'_> {
1656-
dsl::Table::new(self)
1657-
}
16581607
}
16591608

16601609
#[derive(Clone)]

0 commit comments

Comments
 (0)