Bugfix: Respect alias for dynamic field queries with type::field. (#3163)

This commit is contained in:
Mees Delzenne 2023-12-15 22:52:09 +01:00 committed by GitHub
parent 139320bdde
commit e5fb44fabd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View file

@ -204,8 +204,13 @@ impl Fields {
} }
v => v.to_owned(), v => v.to_owned(),
}; };
// This value is always a string, so we can convert it // find the name for the field, either from the argument or the
let name = syn::idiom(&name.to_raw_string())?; // alias.
let name = if let Some(x) = alias.as_ref().map(Cow::Borrowed) {
x
} else {
Cow::Owned(syn::idiom(&name.to_raw_string())?)
};
// Add the projected field to the output document // Add the projected field to the output document
out.set(ctx, opt, txn, name.as_ref(), expr).await? out.set(ctx, opt, txn, name.as_ref(), expr).await?
} }

View file

@ -314,11 +314,12 @@ async fn field_selection_variable_field_projection() -> Result<(), Error> {
SELECT type::field($param), type::field('name.last') FROM person; SELECT type::field($param), type::field('name.last') FROM person;
SELECT VALUE { 'firstname': type::field($param), lastname: type::field('name.last') } FROM person; SELECT VALUE { 'firstname': type::field($param), lastname: type::field('name.last') } FROM person;
SELECT VALUE [type::field($param), type::field('name.last')] FROM person; SELECT VALUE [type::field($param), type::field('name.last')] FROM person;
SELECT type::field($param) AS first_name FROM person;
"; ";
let dbs = new_ds().await?; let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test"); let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?; let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 5); assert_eq!(res.len(), 6);
// //
let tmp = res.remove(0).result?; let tmp = res.remove(0).result?;
let val = Value::parse( let val = Value::parse(
@ -369,6 +370,14 @@ async fn field_selection_variable_field_projection() -> Result<(), Error> {
]", ]",
); );
assert_eq!(tmp, val); assert_eq!(tmp, val);
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{ first_name: 'Tobie' }
]",
);
assert_eq!(tmp, val);
// //
Ok(()) Ok(())
} }