-
Notifications
You must be signed in to change notification settings - Fork 3
support $project stage in native query pipeline type inference #126
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
support $project stage in native query pipeline type inference #126
Conversation
crates/cli/src/native_query/tests.rs
Outdated
let result_type_name = native_query.result_document_type; | ||
let result_type = &native_query.object_types[&result_type_name]; | ||
|
||
expect_false!(result_type.fields.contains_key("title")); | ||
|
||
let tomatoes_type_name = match result_type.fields.get("tomatoes") { | ||
Some(ObjectField { | ||
r#type: Type::Object(name), | ||
.. | ||
}) => ObjectTypeName::from(name.clone()), | ||
_ => panic!("tomatoes field does not have an object type"), | ||
}; | ||
let tomatoes_type = &native_query.object_types[&tomatoes_type_name]; | ||
expect_false!(tomatoes_type.fields.contains_key("title")); | ||
expect_that!( | ||
tomatoes_type.fields.keys().collect_vec(), | ||
unordered_elements_are![&&FieldName::from("viewer"), &&FieldName::from("critic")] | ||
); | ||
expect_eq!( | ||
tomatoes_type.fields["viewer"].r#type, | ||
Type::Object("TomatoesCriticViewer".into()), | ||
); | ||
|
||
let critic_type_name = match tomatoes_type.fields.get("critic") { | ||
Some(ObjectField { | ||
r#type: Type::Object(name), | ||
.. | ||
}) => ObjectTypeName::from(name.clone()), | ||
_ => panic!("tomatoes.critic field does not have an object type"), | ||
}; | ||
let critic_type = &native_query.object_types[&critic_type_name]; | ||
expect_eq!( | ||
critic_type.fields, | ||
object_fields([("numReviews", Type::Scalar(BsonScalarType::Int))]), | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[googletest::test] | ||
fn supports_project_stage_in_inclusion_mode() -> Result<()> { | ||
let config = mflix_config(); | ||
|
||
let pipeline = Pipeline::new(vec![Stage::Project(doc! { | ||
"title": 1, | ||
"tomatoes.critic.rating": true, | ||
"tomatoes.critic.meter": true, | ||
"tomatoes.lastUpdated": true, | ||
"releaseDate": "$released", | ||
})]); | ||
|
||
let native_query = | ||
native_query_from_pipeline(&config, "inclusion", Some("movies".into()), pipeline)?; | ||
|
||
expect_eq!(native_query.result_document_type, "inclusion_project".into()); | ||
|
||
expect_eq!( | ||
native_query.object_types, | ||
[ | ||
( | ||
"inclusion_project".into(), | ||
ObjectType { | ||
fields: object_fields([ | ||
("_id", Type::Scalar(BsonScalarType::ObjectId)), | ||
("title", Type::Scalar(BsonScalarType::String)), | ||
("tomatoes", Type::Object("inclusion_project_tomatoes".into())), | ||
("releaseDate", Type::Scalar(BsonScalarType::Date)), | ||
]), | ||
description: None | ||
} | ||
), | ||
( | ||
"inclusion_project_tomatoes".into(), | ||
ObjectType { | ||
fields: object_fields([ | ||
( | ||
"critic", | ||
Type::Object("inclusion_project_tomatoes_critic".into()) | ||
), | ||
("lastUpdated", Type::Scalar(BsonScalarType::Date)), | ||
]), | ||
description: None | ||
} | ||
), | ||
( | ||
"inclusion_project_tomatoes_critic".into(), | ||
ObjectType { | ||
fields: object_fields([ | ||
("rating", Type::Scalar(BsonScalarType::Double)), | ||
("meter", Type::Scalar(BsonScalarType::Int)), | ||
]), | ||
description: None | ||
} | ||
) | ||
] | ||
.into(), | ||
); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests demonstrates inference for $project
in its two possible modes.
e9ee52a
to
9ed82c6
Compare
16177c7
to
4952d73
Compare
…when-generating-native-query-configs
// | ||
// TODO: This implementation does not fully account for uses of $$REMOVE. It does correctly select | ||
// inclusion mode if $$REMOVE is used. A complete implementation would infer a nullable type for | ||
// a projection that conditionally resolves to $$REMOVE. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you filed a ticket for this so we can come back to it at some point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is work an an in-progress feature that is gated behind a feature flag,
native-query-subcommand
.This change allows the native query configuration generator to infer the output type of
$project
stages in pipelines. It also enables inference for parameters used in$limit
and$skip
stages.$project
is a complicated feature, but it's important to support it because it's widely used. It allows adding, removing, or modifying document fields in the document-processing pipeline. It has two modes: it can either remove fields, or it can select a subset of fields to keep while optionally adding more or changing values of kept fields. It also has dotted-path notation for easily manipulating nested structures.