From 36d83ebccf867c380c47b8ad7dfd3e4ebe55072c Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Mon, 5 Aug 2024 19:06:54 +0200 Subject: [PATCH] Remove faulty flatten (#4463) --- core/src/sql/value/get.rs | 18 +++++++--- lib/tests/select.rs | 72 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/core/src/sql/value/get.rs b/core/src/sql/value/get.rs index 519bf94a..b57a2c2d 100644 --- a/core/src/sql/value/get.rs +++ b/core/src/sql/value/get.rs @@ -250,10 +250,20 @@ impl Value { .run(|stk| stm.compute(stk, ctx, opt, None)) .await? .all(); - stk.run(|stk| v.get(stk, ctx, opt, None, path.next())) - .await? - .flatten() - .ok() + let res = stk + .run(|stk| v.get(stk, ctx, opt, None, path.next())) + .await?; + // We only want to flatten the results if the next part + // is a graph part. Reason being that if we flatten fields, + // the results of those fields (which could be arrays) will + // be merged into each other. So [1, 2, 3], [4, 5, 6] would + // become [1, 2, 3, 4, 5, 6]. This slice access won't panic + // as we have already checked the length of the path. + Ok(if let Part::Graph(_) = path[1] { + res.flatten() + } else { + res + }) } } } diff --git a/lib/tests/select.rs b/lib/tests/select.rs index 3848c89c..395f0e6f 100644 --- a/lib/tests/select.rs +++ b/lib/tests/select.rs @@ -1224,3 +1224,75 @@ async fn select_destructure() -> Result<(), Error> { // Ok(()) } + +#[tokio::test] +async fn select_field_from_graph_no_flattening() -> Result<(), Error> { + let sql = " + CREATE a:1, a:2; + + RELATE a:1->b:1->a:2 SET list = [1, 2, 3]; + RELATE a:1->b:2->a:2 SET list = [4, 5, 6]; + + SELECT VALUE ->b.list FROM a:1; + "; + let dbs = new_ds().await?; + let ses = Session::owner().with_ns("test").with_db("test"); + let res = &mut dbs.execute(sql, &ses, None).await?; + assert_eq!(res.len(), 4); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + { id: a:1 }, + { id: a:2 } + ]", + ); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + { + id: b:1, + in: a:1, + out: a:2, + list: [1, 2, 3] + } + ]", + ); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + { + id: b:2, + in: a:1, + out: a:2, + list: [4, 5, 6] + } + ]", + ); + assert_eq!(tmp, val); + // + let tmp = res.remove(0).result?; + let val = Value::parse( + "[ + [ + [ + 1, + 2, + 3 + ], + [ + 4, + 5, + 6 + ] + ] + ]", + ); + assert_eq!(tmp, val); + // + Ok(()) +}