这是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

Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 48 additions & 11 deletions crates/cli/src/native_query/pipeline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod match_stage;
mod project_stage;

use std::{collections::BTreeMap, iter::once};

Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn infer_pipeline_types(
if let TypeConstraint::Object(stage_type_name) = last_stage_type {
if let Some(object_type) = context.get_object_type(&stage_type_name) {
context.insert_object_type(object_type_name.clone(), object_type.into_owned());
context.set_stage_doc_type(TypeConstraint::Object(object_type_name))
context.set_stage_doc_type(TypeConstraint::Object(object_type_name));
}
}

Expand Down Expand Up @@ -93,9 +94,25 @@ fn infer_stage_output_type(
None
}
Stage::Sort(_) => None,
Stage::Limit(_) => None,
Stage::Skip(expression) => {
infer_type_from_aggregation_expression(
context,
desired_object_type_name,
Some(&TypeConstraint::Scalar(BsonScalarType::Int)),
expression.clone(),
)?;
None
}
Stage::Limit(expression) => {
infer_type_from_aggregation_expression(
context,
desired_object_type_name,
Some(&TypeConstraint::Scalar(BsonScalarType::Int)),
expression.clone(),
)?;
None
}
Stage::Lookup { .. } => todo!("lookup stage"),
Stage::Skip(_) => None,
Stage::Group {
key_expression,
accumulators,
Expand All @@ -110,7 +127,18 @@ fn infer_stage_output_type(
}
Stage::Facet(_) => todo!("facet stage"),
Stage::Count(_) => todo!("count stage"),
Stage::ReplaceWith(selection) => {
Stage::Project(doc) => {
let augmented_type = project_stage::infer_type_from_project_stage(
context,
&format!("{desired_object_type_name}_project"),
doc,
)?;
Some(augmented_type)
}
Stage::ReplaceRoot {
new_root: selection,
}
| Stage::ReplaceWith(selection) => {
let selection: &Document = selection.into();
Some(
aggregation_expression::infer_type_from_aggregation_expression(
Expand Down Expand Up @@ -291,7 +319,11 @@ fn infer_type_from_unwind_stage(
Ok(TypeConstraint::WithFieldOverrides {
augmented_object_type_name: format!("{desired_object_type_name}_unwind").into(),
target_type: Box::new(context.get_input_document_type()?.clone()),
fields: unwind_stage_object_type.fields,
fields: unwind_stage_object_type
.fields
.into_iter()
.map(|(k, t)| (k, Some(t)))
.collect(),
})
}

Expand Down Expand Up @@ -360,7 +392,7 @@ mod tests {
}))]);
let config = mflix_config();
let pipeline_types =
infer_pipeline_types(&config, "movies", Some(&("movies".into())), &pipeline).unwrap();
infer_pipeline_types(&config, "movies", Some(&("movies".into())), &pipeline)?;
let expected = [(
"movies_replaceWith".into(),
ObjectType {
Expand Down Expand Up @@ -415,13 +447,18 @@ mod tests {
augmented_object_type_name: "unwind_stage_unwind".into(),
target_type: Box::new(TypeConstraint::Variable(input_doc_variable)),
fields: [
("idx".into(), TypeConstraint::Scalar(BsonScalarType::Long)),
(
"idx".into(),
Some(TypeConstraint::Scalar(BsonScalarType::Long))
),
(
"words".into(),
TypeConstraint::ElementOf(Box::new(TypeConstraint::FieldOf {
target_type: Box::new(TypeConstraint::Variable(input_doc_variable)),
path: nonempty!["words".into()],
}))
Some(TypeConstraint::ElementOf(Box::new(
TypeConstraint::FieldOf {
target_type: Box::new(TypeConstraint::Variable(input_doc_variable)),
path: nonempty!["words".into()],
}
)))
)
]
.into(),
Expand Down
Loading
Loading