Skip to content

Commit

Permalink
Initial WIP on adding new Int8 scalar
Browse files Browse the repository at this point in the history
fixes and tests
  • Loading branch information
dotansimha committed Apr 17, 2023
1 parent e556be6 commit 865a9bc
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 14 deletions.
26 changes: 26 additions & 0 deletions graph/src/data/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub const ID: &str = "ID";
pub const BYTES_SCALAR: &str = "Bytes";
pub const BIG_INT_SCALAR: &str = "BigInt";
pub const BIG_DECIMAL_SCALAR: &str = "BigDecimal";
pub const INT8_SCALAR: &str = "Int8";

#[derive(Clone, Debug, PartialEq)]
pub enum ValueType {
Expand All @@ -138,6 +139,7 @@ pub enum ValueType {
Bytes,
BigDecimal,
Int,
Int8,
String,
}

Expand All @@ -151,6 +153,7 @@ impl FromStr for ValueType {
"Bytes" => Ok(ValueType::Bytes),
"BigDecimal" => Ok(ValueType::BigDecimal),
"Int" => Ok(ValueType::Int),
"Int8" => Ok(ValueType::Int8),
"String" | "ID" => Ok(ValueType::String),
s => Err(anyhow!("Type not available in this context: {}", s)),
}
Expand All @@ -172,6 +175,7 @@ impl ValueType {
pub enum Value {
String(String),
Int(i32),
Int8(i64),
BigDecimal(scalar::BigDecimal),
Bool(bool),
List(Vec<Value>),
Expand Down Expand Up @@ -207,6 +211,9 @@ impl stable_hash_legacy::StableHash for Value {
Int(inner) => {
stable_hash_legacy::StableHash::stable_hash(inner, sequence_number, state)
}
Int8(inner) => {
stable_hash_legacy::StableHash::stable_hash(inner, sequence_number, state)
}
BigDecimal(inner) => {
stable_hash_legacy::StableHash::stable_hash(inner, sequence_number, state)
}
Expand Down Expand Up @@ -265,6 +272,10 @@ impl StableHash for Value {
inner.stable_hash(field_address.child(0), state);
7
}
Int8(inner) => {
inner.stable_hash(field_address.child(0), state);
8
}
};

state.write(field_address, &[variant])
Expand Down Expand Up @@ -300,6 +311,9 @@ impl Value {
BYTES_SCALAR => Value::Bytes(scalar::Bytes::from_str(s)?),
BIG_INT_SCALAR => Value::BigInt(scalar::BigInt::from_str(s)?),
BIG_DECIMAL_SCALAR => Value::BigDecimal(scalar::BigDecimal::from_str(s)?),
INT8_SCALAR => Value::Int8(s.parse::<i64>().map_err(|_| {
QueryExecutionError::ValueParseError("Int8".to_string(), format!("{}", s))
})?),
_ => Value::String(s.clone()),
}
}
Expand Down Expand Up @@ -391,6 +405,7 @@ impl Value {
Value::Bool(_) => "Boolean".to_owned(),
Value::Bytes(_) => "Bytes".to_owned(),
Value::Int(_) => "Int".to_owned(),
Value::Int8(_) => "Int8".to_owned(),
Value::List(values) => {
if let Some(v) = values.first() {
format!("[{}]", v.type_name())
Expand All @@ -411,6 +426,7 @@ impl Value {
| (Value::Bool(_), ValueType::Boolean)
| (Value::Bytes(_), ValueType::Bytes)
| (Value::Int(_), ValueType::Int)
| (Value::Int8(_), ValueType::Int8)
| (Value::Null, _) => true,
(Value::List(values), _) if is_list => values
.iter()
Expand All @@ -428,6 +444,7 @@ impl fmt::Display for Value {
match self {
Value::String(s) => s.to_string(),
Value::Int(i) => i.to_string(),
Value::Int8(i) => i.to_string(),
Value::BigDecimal(d) => d.to_string(),
Value::Bool(b) => b.to_string(),
Value::Null => "null".to_string(),
Expand All @@ -445,6 +462,7 @@ impl fmt::Debug for Value {
match self {
Self::String(s) => f.debug_tuple("String").field(s).finish(),
Self::Int(i) => f.debug_tuple("Int").field(i).finish(),
Self::Int8(i) => f.debug_tuple("Int8").field(i).finish(),
Self::BigDecimal(d) => d.fmt(f),
Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
Self::List(arg0) => f.debug_tuple("List").field(arg0).finish(),
Expand All @@ -460,6 +478,7 @@ impl From<Value> for q::Value {
match value {
Value::String(s) => q::Value::String(s),
Value::Int(i) => q::Value::Int(q::Number::from(i)),
Value::Int8(i) => q::Value::String(i.to_string()),
Value::BigDecimal(d) => q::Value::String(d.to_string()),
Value::Bool(b) => q::Value::Boolean(b),
Value::Null => q::Value::Null,
Expand All @@ -477,6 +496,7 @@ impl From<Value> for r::Value {
match value {
Value::String(s) => r::Value::String(s),
Value::Int(i) => r::Value::Int(i as i64),
Value::Int8(i) => r::Value::String(i.to_string()),
Value::BigDecimal(d) => r::Value::String(d.to_string()),
Value::Bool(b) => r::Value::Boolean(b),
Value::Null => r::Value::Null,
Expand Down Expand Up @@ -543,6 +563,12 @@ impl From<u64> for Value {
}
}

impl From<i64> for Value {
fn from(value: i64) -> Value {
Value::Int8(value.into())
}
}

impl TryFrom<Value> for Option<scalar::BigInt> {
type Error = Error;

Expand Down
2 changes: 2 additions & 0 deletions graph/src/data/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ impl Value {
Err(Value::Int(num))
}
}
("Int8", Value::Int(num)) => Ok(Value::String(num.to_string())),
("Int8", Value::String(num)) => Ok(Value::String(num)),
("String", Value::String(s)) => Ok(Value::String(s)),
("ID", Value::String(s)) => Ok(Value::String(s)),
("ID", Value::Int(n)) => Ok(Value::String(n.to_string())),
Expand Down
1 change: 1 addition & 0 deletions graph/src/runtime/gas/size_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl GasSizeOf for Value {
Value::Null => Gas(1),
Value::List(list) => list.gas_size_of(),
Value::Int(int) => int.gas_size_of(),
Value::Int8(int) => int.gas_size_of(),
Value::Bytes(bytes) => bytes.gas_size_of(),
Value::Bool(bool) => bool.gas_size_of(),
Value::BigInt(big_int) => big_int.gas_size_of(),
Expand Down
2 changes: 1 addition & 1 deletion graph/src/util/cache_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl CacheWeight for Value {
Value::List(values) => values.indirect_weight(),
Value::Bytes(bytes) => bytes.indirect_weight(),
Value::BigInt(n) => n.indirect_weight(),
Value::Int(_) | Value::Bool(_) | Value::Null => 0,
Value::Int8(_) | Value::Int(_) | Value::Bool(_) | Value::Null => 0,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions graphql/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ fn field_scalar_filter_input_values(
"BigDecimal" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"ID" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"Int" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"Int8" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"String" => vec![
"",
"not",
Expand Down Expand Up @@ -894,6 +895,9 @@ mod tests {
schema
.get_named_type("String")
.expect("String type is missing in API schema");
schema
.get_named_type("Int8")
.expect("Int8 type is missing in API schema");
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions graphql/src/schema/meta.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# GraphQL core functionality
scalar Boolean
scalar ID
""" 4 bytes int """
scalar Int
scalar Float
scalar String
Expand All @@ -19,9 +20,12 @@ directive @subgraphId(id: String!) on OBJECT
"creates a virtual field on the entity that may be queried but cannot be set manually through the mappings API."
directive @derivedFrom(field: String!) on FIELD_DEFINITION

# Additional scalar types
scalar BigDecimal
scalar Bytes
scalar BigInt
""" 8 bytes signed integer """
scalar Int8

# The type names are purposely awkward to minimize the risk of them
# colliding with user-supplied types
Expand Down
19 changes: 19 additions & 0 deletions graphql/src/values/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl MaybeCoercible<ScalarType> for q::Value {
Err(q::Value::Int(num))
}
}
("Int8", q::Value::Int(num)) => {
let n = num.as_i64().ok_or_else(|| q::Value::Int(num.clone()))?;
Ok(r::Value::Int(n))
}
("String", q::Value::String(s)) => Ok(r::Value::String(s)),
("ID", q::Value::String(s)) => Ok(r::Value::String(s)),
("ID", q::Value::Int(n)) => Ok(r::Value::String(
Expand Down Expand Up @@ -395,6 +399,21 @@ mod tests {
);
}

#[test]
fn coerce_int8_scalar() {
let int8_type = TypeDefinition::Scalar(ScalarType::new("Int8".to_string()));
let resolver = |_: &str| Some(&int8_type);

assert_eq!(
coerce_to_definition(Value::Int(1234.into()), "", &resolver),
Ok(Value::String("1234".to_string()))
);
assert_eq!(
coerce_to_definition(Value::Int((-1234_i32).into()), "", &resolver,),
Ok(Value::String("-1234".to_string()))
);
}

#[test]
fn coerce_bytes_scalar() {
let bytes_type = TypeDefinition::Scalar(ScalarType::new("Bytes".to_string()));
Expand Down
6 changes: 6 additions & 0 deletions runtime/test/src/test/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ async fn test_abi_store_value(api_version: Version) {
let new_value_ptr = module.takes_val_returns_ptr("value_from_int", int);
let new_value: Value = module.asc_get(new_value_ptr).unwrap();
assert_eq!(new_value, Value::Int(int));

// Value::Int8
let int8 = i64::min_value();
let new_value_ptr = module.takes_val_returns_ptr("value_from_int8", int8);
let new_value: Value = module.asc_get(new_value_ptr).unwrap();
assert_eq!(new_value, Value::Int8(int8));

// Value::BigDecimal
let big_decimal = BigDecimal::from_str("3.14159001").unwrap();
Expand Down
8 changes: 8 additions & 0 deletions runtime/test/wasm_test/api_version_0_0_4/abi_store_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum ValueKind {
NULL = 5,
BYTES = 6,
BIG_INT = 7,
INT8 = 8,
}

// Big enough to fit any pointer or native `this.data`.
Expand Down Expand Up @@ -43,6 +44,13 @@ export function value_from_int(int: i32): Value {
return value
}

export function value_from_int8(int: i64): Value {
let value = new Value();
value.kind = ValueKind.INT8;
value.data = int as i64
return value
}

export function value_from_big_decimal(float: BigInt): Value {
let value = new Value();
value.kind = ValueKind.BIG_DECIMAL;
Expand Down
Binary file modified runtime/test/wasm_test/api_version_0_0_4/abi_store_value.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions runtime/test/wasm_test/api_version_0_0_5/abi_store_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export function value_from_int(int: i32): Value {
return value
}

export function value_from_int8(int: i64): Value {
let value = new Value();
value.kind = ValueKind.INT8;
value.data = int as i64
return value
}


export function value_from_big_decimal(float: BigInt): Value {
let value = new Value();
value.kind = ValueKind.BIG_DECIMAL;
Expand Down
Binary file modified runtime/test/wasm_test/api_version_0_0_5/abi_store_value.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions runtime/test/wasm_test/api_version_0_0_5/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum ValueKind {
NULL = 5,
BYTES = 6,
BIG_INT = 7,
INT8 = 8
}
// Big enough to fit any pointer or native `this.data`.
export type Payload = u64
Expand Down
8 changes: 8 additions & 0 deletions runtime/wasm/src/asc_abi/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ impl From<EnumPayload> for f64 {
}
}

impl From<EnumPayload> for i64 {
fn from(payload: EnumPayload) -> i64 {
payload.0 as i64
}
}

impl From<EnumPayload> for bool {
fn from(payload: EnumPayload) -> bool {
payload.0 != 0
Expand Down Expand Up @@ -546,6 +552,7 @@ pub enum StoreValueKind {
Null,
Bytes,
BigInt,
Int8,
}

impl StoreValueKind {
Expand All @@ -555,6 +562,7 @@ impl StoreValueKind {
match value {
Value::String(_) => StoreValueKind::String,
Value::Int(_) => StoreValueKind::Int,
Value::Int8(_) => StoreValueKind::Int8,
Value::BigDecimal(_) => StoreValueKind::BigDecimal,
Value::Bool(_) => StoreValueKind::Bool,
Value::List(_) => StoreValueKind::Array,
Expand Down
2 changes: 2 additions & 0 deletions runtime/wasm/src/to_from/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl FromAscObj<AscEnum<StoreValueKind>> for store::Value {
Value::String(asc_get(heap, ptr, gas)?)
}
StoreValueKind::Int => Value::Int(i32::from(payload)),
StoreValueKind::Int8 => Value::Int8(i64::from(payload)),
StoreValueKind::BigDecimal => {
let ptr: AscPtr<AscBigDecimal> = AscPtr::from(payload);
Value::BigDecimal(asc_get(heap, ptr, gas)?)
Expand Down Expand Up @@ -285,6 +286,7 @@ impl ToAscObj<AscEnum<StoreValueKind>> for store::Value {
let payload = match self {
Value::String(string) => asc_new(heap, string.as_str(), gas)?.into(),
Value::Int(n) => EnumPayload::from(*n),
Value::Int8(n) => EnumPayload::from(*n),
Value::BigDecimal(n) => asc_new(heap, n, gas)?.into(),
Value::Bool(b) => EnumPayload::from(*b),
Value::List(array) => asc_new(heap, array.as_slice(), gas)?.into(),
Expand Down
2 changes: 2 additions & 0 deletions store/postgres/examples/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn print_diesel_tables(layout: &Layout) {
ColumnType::BigDecimal | ColumnType::BigInt => "Numeric",
ColumnType::Bytes => "Binary",
ColumnType::Int => "Integer",
ColumnType::Int8 => "Int8",
ColumnType::String | ColumnType::Enum(_) | ColumnType::TSVector(_) => "Text",
}
.to_owned();
Expand All @@ -71,6 +72,7 @@ fn print_diesel_tables(layout: &Layout) {
ColumnType::BigDecimal | ColumnType::BigInt => "BigDecimal",
ColumnType::Bytes => "Vec<u8>",
ColumnType::Int => "i32",
ColumnType::Int8 => "i64",
ColumnType::String | ColumnType::Enum(_) | ColumnType::TSVector(_) => "String",
}
.to_owned();
Expand Down
3 changes: 3 additions & 0 deletions store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ pub enum ColumnType {
BigInt,
Bytes,
Int,
Int8,
String,
TSVector(FulltextConfig),
Enum(EnumType),
Expand Down Expand Up @@ -1067,6 +1068,7 @@ impl ColumnType {
ValueType::BigInt => Ok(ColumnType::BigInt),
ValueType::Bytes => Ok(ColumnType::Bytes),
ValueType::Int => Ok(ColumnType::Int),
ValueType::Int8 => Ok(ColumnType::Int8),
ValueType::String => Ok(ColumnType::String),
}
}
Expand All @@ -1078,6 +1080,7 @@ impl ColumnType {
ColumnType::BigInt => "numeric",
ColumnType::Bytes => "bytea",
ColumnType::Int => "integer",
ColumnType::Int8 => "int8",
ColumnType::String => "text",
ColumnType::TSVector(_) => "tsvector",
ColumnType::Enum(enum_type) => enum_type.name.as_str(),
Expand Down
Loading

0 comments on commit 865a9bc

Please sign in to comment.