Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ndc-models and ndc-sdk to v0.1.4 #73

Merged
merged 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 7 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ resolver = "2"
# The tag or rev of ndc-models must match the locked tag or rev of the
# ndc-models dependency of ndc-sdk
[workspace.dependencies]
ndc-sdk = { git = "https://github.com/hasura/ndc-sdk-rs.git" }
ndc-models = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.2" }
ndc-sdk = { git = "https://github.com/hasura/ndc-sdk-rs.git", tag = "v0.1.4" }
ndc-models = { git = "http://github.com/hasura/ndc-spec.git", tag = "v0.1.4" }

indexmap = { version = "2", features = ["serde"] } # should match the version that ndc-models uses
itertools = "^0.12.1"
Expand Down
1 change: 0 additions & 1 deletion crates/cli/src/introspection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pub mod validation_schema;

pub use sampling::{sample_schema_from_db, type_from_bson};
pub use validation_schema::get_metadata_from_validation_schema;

36 changes: 27 additions & 9 deletions crates/cli/src/introspection/sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ pub async fn sample_schema_from_db(
while let Some(collection_spec) = collections_cursor.try_next().await? {
let collection_name = collection_spec.name;
if !existing_schemas.contains(&collection_name) || config_file_changed {
let collection_schema =
sample_schema_from_collection(&collection_name, sample_size, all_schema_nullalble, state).await?;
let collection_schema = sample_schema_from_collection(
&collection_name,
sample_size,
all_schema_nullalble,
state,
)
.await?;
schemas.insert(collection_name, collection_schema);
}
}
Expand Down Expand Up @@ -74,7 +79,11 @@ async fn sample_schema_from_collection(
})
}

fn make_object_type(object_type_name: &str, document: &Document, all_schema_nullalble: bool) -> Vec<ObjectType> {
fn make_object_type(
object_type_name: &str,
document: &Document,
all_schema_nullalble: bool,
) -> Vec<ObjectType> {
let (mut object_type_defs, object_fields) = {
let type_prefix = format!("{object_type_name}_");
let (object_type_defs, object_fields): (Vec<Vec<ObjectType>>, Vec<ObjectField>) = document
Expand Down Expand Up @@ -105,7 +114,8 @@ fn make_object_field(
all_schema_nullalble: bool,
) -> (Vec<ObjectType>, ObjectField) {
let object_type_name = format!("{type_prefix}{field_name}");
let (collected_otds, field_type) = make_field_type(&object_type_name, field_value, all_schema_nullalble);
let (collected_otds, field_type) =
make_field_type(&object_type_name, field_value, all_schema_nullalble);
let object_field_value = WithName::named(
field_name.to_owned(),
schema::ObjectField {
Expand All @@ -132,7 +142,11 @@ pub fn type_from_bson(
(WithName::into_map(object_types), t)
}

fn make_field_type(object_type_name: &str, field_value: &Bson, all_schema_nullalble: bool) -> (Vec<ObjectType>, Type) {
fn make_field_type(
object_type_name: &str,
field_value: &Bson,
all_schema_nullalble: bool,
) -> (Vec<ObjectType>, Type) {
fn scalar(t: BsonScalarType) -> (Vec<ObjectType>, Type) {
(vec![], Type::Scalar(t))
}
Expand All @@ -144,7 +158,8 @@ fn make_field_type(object_type_name: &str, field_value: &Bson, all_schema_nullal
let mut collected_otds = vec![];
let mut result_type = Type::Scalar(Undefined);
for elem in arr {
let (elem_collected_otds, elem_type) = make_field_type(object_type_name, elem, all_schema_nullalble);
let (elem_collected_otds, elem_type) =
make_field_type(object_type_name, elem, all_schema_nullalble);
collected_otds = if collected_otds.is_empty() {
elem_collected_otds
} else {
Expand Down Expand Up @@ -195,7 +210,8 @@ mod tests {
fn simple_doc() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_int": 1, "my_string": "two"};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([(
object_name.to_owned(),
Expand Down Expand Up @@ -229,7 +245,8 @@ mod tests {
fn array_of_objects() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_array": [{"foo": 42, "bar": ""}, {"bar": "wut", "baz": 3.77}]};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([
(
Expand Down Expand Up @@ -289,7 +306,8 @@ mod tests {
fn non_unifiable_array_of_objects() -> Result<(), anyhow::Error> {
let object_name = "foo";
let doc = doc! {"my_array": [{"foo": 42, "bar": ""}, {"bar": 17, "baz": 3.77}]};
let result = WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));
let result =
WithName::into_map::<BTreeMap<_, _>>(make_object_type(object_name, &doc, false));

let expected = BTreeMap::from([
(
Expand Down
17 changes: 13 additions & 4 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,28 @@ pub async fn run(command: Command, context: &Context) -> anyhow::Result<()> {

/// Update the configuration in the current directory by introspecting the database.
async fn update(context: &Context, args: &UpdateArgs) -> anyhow::Result<()> {
let configuration_options = configuration::parse_configuration_options_file(&context.path).await;
let configuration_options =
configuration::parse_configuration_options_file(&context.path).await;
// Prefer arguments passed to cli, and fallback to the configuration file
let sample_size = match args.sample_size {
Some(size) => size,
None => configuration_options.introspection_options.sample_size
None => configuration_options.introspection_options.sample_size,
};
let no_validator_schema = match args.no_validator_schema {
Some(validator) => validator,
None => configuration_options.introspection_options.no_validator_schema
None => {
configuration_options
.introspection_options
.no_validator_schema
}
};
let all_schema_nullable = match args.all_schema_nullable {
Some(validator) => validator,
None => configuration_options.introspection_options.all_schema_nullable
None => {
configuration_options
.introspection_options
.all_schema_nullable
}
};
let config_file_changed = configuration::get_config_file_changed(&context.path).await?;

Expand Down
1 change: 1 addition & 0 deletions crates/configuration/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl From<ObjectField> for ndc_models::ObjectField {
ndc_models::ObjectField {
description: field.description,
r#type: field.r#type.into(),
arguments: BTreeMap::new(),
}
}
}
8 changes: 2 additions & 6 deletions crates/mongodb-agent-common/src/mongodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ mod stage;
pub mod test_helpers;

pub use self::{
accumulator::Accumulator,
collection::CollectionTrait,
database::DatabaseTrait,
pipeline::Pipeline,
selection::Selection,
stage::Stage,
accumulator::Accumulator, collection::CollectionTrait, database::DatabaseTrait,
pipeline::Pipeline, selection::Selection, stage::Stage,
};

// MockCollectionTrait is generated by automock when the test flag is active.
Expand Down
10 changes: 8 additions & 2 deletions crates/mongodb-connector/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use ndc_sdk::models::{
Capabilities, CapabilitiesResponse, LeafCapability, QueryCapabilities, RelationshipCapabilities,
Capabilities, CapabilitiesResponse, LeafCapability, NestedFieldCapabilities, QueryCapabilities,
RelationshipCapabilities,
};

pub fn mongo_capabilities_response() -> CapabilitiesResponse {
ndc_sdk::models::CapabilitiesResponse {
version: "0.1.2".to_owned(),
version: "0.1.4".to_owned(),
capabilities: Capabilities {
query: QueryCapabilities {
aggregates: Some(LeafCapability {}),
variables: Some(LeafCapability {}),
explain: Some(LeafCapability {}),
nested_fields: NestedFieldCapabilities {
filter_by: Some(LeafCapability {}),
order_by: Some(LeafCapability {}),
aggregates: None,
},
},
mutation: ndc_sdk::models::MutationCapabilities {
transactional: None,
Expand Down
6 changes: 5 additions & 1 deletion crates/mongodb-connector/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ fn rewrite_doc(
.iter()
.map(|(name, field)| {
let field_value = match field {
ndc::Field::Column { column, fields } => {
ndc::Field::Column {
column,
fields,
arguments: _,
} => {
let orig_value = doc.remove(column).ok_or_else(|| {
MutationError::UnprocessableContent(format!(
"missing expected field from response: {name}"
Expand Down
8 changes: 7 additions & 1 deletion crates/mongodb-support/src/align.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use indexmap::IndexMap;
use std::hash::Hash;

pub fn align<K, T, U, V, FT, FU, FTU>(ts: IndexMap<K, T>, mut us: IndexMap<K, U>, ft: FT, fu: FU, ftu: FTU) -> IndexMap<K, V>
pub fn align<K, T, U, V, FT, FU, FTU>(
ts: IndexMap<K, T>,
mut us: IndexMap<K, U>,
ft: FT,
fu: FU,
ftu: FTU,
) -> IndexMap<K, V>
where
K: Hash + Eq,
FT: Fn(T) -> V,
Expand Down
35 changes: 25 additions & 10 deletions crates/ndc-query-plan/src/plan_for_query_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,16 @@ fn plan_for_aggregate<T: QueryContext>(
aggregate: ndc::Aggregate,
) -> Result<plan::Aggregate<T>> {
match aggregate {
ndc::Aggregate::ColumnCount { column, distinct } => {
Ok(plan::Aggregate::ColumnCount { column, distinct })
}
ndc::Aggregate::SingleColumn { column, function } => {
ndc::Aggregate::ColumnCount {
column,
distinct,
field_path: _,
} => Ok(plan::Aggregate::ColumnCount { column, distinct }),
ndc::Aggregate::SingleColumn {
column,
function,
field_path: _,
} => {
Comment on lines +140 to +149
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to forward field paths here. But we need to do a pass of testing and cleaning up aggregate support anyway so that can wait a bit.

let object_type_field_type =
find_object_field(collection_object_type, column.as_ref())?;
// let column_scalar_type_name = get_scalar_type_name(&object_type_field.r#type)?;
Expand Down Expand Up @@ -211,9 +217,13 @@ fn plan_for_order_by_element<T: QueryContext>(
element: ndc::OrderByElement,
) -> Result<plan::OrderByElement<T>> {
let target = match element.target {
ndc::OrderByTarget::Column { name, path } => plan::OrderByTarget::Column {
ndc::OrderByTarget::Column {
name,
field_path,
path,
} => plan::OrderByTarget::Column {
name: name.clone(),
field_path: Default::default(), // TODO: propagate this after ndc-spec update
field_path,
path: plan_for_relationship_path(
plan_state,
root_collection_object_type,
Expand All @@ -227,6 +237,7 @@ fn plan_for_order_by_element<T: QueryContext>(
column,
function,
path,
field_path: _,
} => {
let (plan_path, target_object_type) = plan_for_relationship_path(
plan_state,
Expand Down Expand Up @@ -495,7 +506,11 @@ fn plan_for_comparison_target<T: QueryContext>(
target: ndc::ComparisonTarget,
) -> Result<plan::ComparisonTarget<T>> {
match target {
ndc::ComparisonTarget::Column { name, path } => {
ndc::ComparisonTarget::Column {
name,
field_path,
path,
} => {
let requested_columns = vec![name.clone()];
let (path, target_object_type) = plan_for_relationship_path(
plan_state,
Expand All @@ -507,16 +522,16 @@ fn plan_for_comparison_target<T: QueryContext>(
let column_type = find_object_field(&target_object_type, &name)?.clone();
Ok(plan::ComparisonTarget::Column {
name,
field_path: Default::default(), // TODO: propagate this after ndc-spec update
field_path,
path,
column_type,
})
}
ndc::ComparisonTarget::RootCollectionColumn { name } => {
ndc::ComparisonTarget::RootCollectionColumn { name, field_path } => {
let column_type = find_object_field(root_collection_object_type, &name)?.clone();
Ok(plan::ComparisonTarget::ColumnInScope {
name,
field_path: Default::default(), // TODO: propagate this after ndc-spec update
field_path,
column_type,
scope: plan_state.scope.clone(),
})
Expand Down
3 changes: 3 additions & 0 deletions crates/ndc-query-plan/src/plan_for_query_request/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn translates_query_request_relationships() -> Result<(), anyhow::Error> {
order_direction: OrderDirection::Asc,
target: OrderByTarget::Column {
name: "advisor_name".to_owned(),
field_path: None,
path: vec![
path_element("school_classes")
.predicate(binop(
Expand Down Expand Up @@ -577,12 +578,14 @@ fn translates_relationships_in_fields_predicates_and_orderings() -> Result<(), a
column: "year".into(),
function: "Average".into(),
path: vec![path_element("author_articles").into()],
field_path: None,
},
},
ndc::OrderByElement {
order_direction: OrderDirection::Desc,
target: OrderByTarget::Column {
name: "id".into(),
field_path: None,
path: vec![],
},
},
Expand Down
Loading
Loading