Skip to content

Commit

Permalink
assemble: add numeric information to inference
Browse files Browse the repository at this point in the history
Includes the "numeric" information in projection inference if it is available.
  • Loading branch information
williamhbaker committed Aug 16, 2024
1 parent 7d68a3b commit 409571a
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 68 deletions.
21 changes: 18 additions & 3 deletions crates/assemble/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ pub fn inference(shape: &Shape, exists: Exists) -> flow::Inference {
} else {
None
},
numeric: None,
numeric: if shape.type_.overlaps(types::INT_OR_FRAC) {
Some(flow::inference::Numeric {
has_minimum: shape.numeric.minimum.is_some(),
minimum: shape.numeric.minimum.map(Into::into).unwrap_or_default(),
has_maximum: shape.numeric.maximum.is_some(),
maximum: shape.numeric.maximum.map(Into::into).unwrap_or_default(),
})
} else {
None
},
}
}

Expand Down Expand Up @@ -551,7 +560,7 @@ pub fn pb_datetime(t: &time::OffsetDateTime) -> pbjson_types::Timestamp {
#[cfg(test)]
mod test {
use super::*;
use doc::shape::StringShape;
use doc::shape::{NumericShape, StringShape};
use serde_json::{json, Value};
use std::collections::BTreeMap;

Expand All @@ -570,14 +579,20 @@ mod test {
min_length: 10,
max_length: Some(123),
},
numeric: NumericShape {
minimum: None,
maximum: Some(json::Number::Unsigned(1000)),
},
..Shape::anything()
};

let out1 = inference(&shape, Exists::Must);
shape.type_ = types::BOOLEAN;
let out2 = inference(&shape, Exists::May);
shape.type_ = types::INTEGER | types::STRING;
let out3 = inference(&shape, Exists::May);

insta::assert_debug_snapshot!(&[out1, out2]);
insta::assert_debug_snapshot!(&[out1, out2, out3]);
}

#[test]
Expand Down
29 changes: 28 additions & 1 deletion crates/assemble/src/snapshots/assemble__test__inference.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/assemble/src/lib.rs
expression: "&[out1, out2]"
expression: "&[out1, out2, out3]"
---
[
Inference {
Expand Down Expand Up @@ -35,4 +35,31 @@ expression: "&[out1, out2]"
exists: May,
numeric: None,
},
Inference {
types: [
"integer",
"string",
],
string: Some(
String {
content_type: "a/type",
format: "date-time",
content_encoding: "BaSE64",
max_length: 123,
},
),
title: "the title",
description: "the description",
default_json: "{\"hello\":\"world\"}",
secret: true,
exists: May,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: true,
maximum: 1000.0,
},
),
},
]
10 changes: 10 additions & 0 deletions crates/json/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ impl TryFrom<Number> for serde_json::Value {
}
}

impl Into<f64> for Number {
fn into(self) -> f64 {
match self {
Unsigned(n) => n as f64,
Signed(n) => n as f64,
Float(n) => n,
}
}
}

impl Ord for Number {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down Expand Up @@ -450,7 +457,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down Expand Up @@ -705,7 +719,14 @@ Outcome {
default_json: "",
secret: false,
exists: Must,
numeric: None,
numeric: Some(
Numeric {
has_minimum: false,
minimum: 0.0,
has_maximum: false,
maximum: 0.0,
},
),
},
),
},
Expand Down
Loading

0 comments on commit 409571a

Please sign in to comment.