这是indexloc提供的服务,不要输入任何密码
Skip to content

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

Conversation

hallettj
Copy link
Collaborator

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.

Comment on lines 327 to 439
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(())
}
Copy link
Collaborator Author

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.

@hallettj hallettj force-pushed the jessehallett/eng-1248-type-inference-for-more-match-operators branch from e9ee52a to 9ed82c6 Compare November 19, 2024 19:58
Base automatically changed from jessehallett/eng-1248-type-inference-for-more-match-operators to main November 19, 2024 20:10
@hallettj hallettj force-pushed the jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs branch from 16177c7 to 4952d73 Compare November 19, 2024 20:41
//
// 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.
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@hallettj hallettj merged commit b5e3cf6 into main Nov 22, 2024
1 check passed
@hallettj hallettj deleted the jessehallett/eng-1106-support-project-stage-when-generating-native-query-configs branch November 22, 2024 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants