From 91d4ac3ac097ea89bf275120cfa4ca37d2d6190d Mon Sep 17 00:00:00 2001 From: Kacper Madej Date: Fri, 10 Jan 2025 12:14:57 +0700 Subject: [PATCH] Fix expression chaining --- core/json/mod.rs | 4 ++++ core/translate/expr.rs | 5 +++-- testing/json.test | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/json/mod.rs b/core/json/mod.rs index 59adc4bb2..f0a362ab6 100644 --- a/core/json/mod.rs +++ b/core/json/mod.rs @@ -290,6 +290,10 @@ pub fn json_type(value: &OwnedValue, path: Option<&OwnedValue>) -> crate::Result /// Returns the value at the given JSON path. If the path does not exist, it returns None. /// If the path is an invalid path, returns an error. +/// +/// *strict* - if false, we will try to resolve the path even if it does not start with "$" +/// in a way that's compatible with the `->` and `->>` operators. See examples in the docs: +/// https://sqlite.org/json1.html#the_and_operators fn json_extract_single<'a>( json: &'a Val, path: &OwnedValue, diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 6897308c0..f3834601e 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -457,9 +457,10 @@ pub fn translate_expr( match expr { ast::Expr::Between { .. } => todo!(), ast::Expr::Binary(e1, op, e2) => { - let e1_reg = program.alloc_register(); + let e1_reg = program.alloc_registers(2); + let e2_reg = e1_reg + 1; + translate_expr(program, referenced_tables, e1, e1_reg, resolver)?; - let e2_reg = program.alloc_register(); translate_expr(program, referenced_tables, e2, e2_reg, resolver)?; match op { diff --git a/testing/json.test b/testing/json.test index 7d0d93203..f8472db96 100755 --- a/testing/json.test +++ b/testing/json.test @@ -331,6 +331,10 @@ do_execsql_test json_arrow_shift_implicit_real_cast { # SELECT '[1,2,3]' ->> false #} {{1}} +do_execsql_test json_arrow_chained { + select '{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 ->> 'f' +} {{7}} + # TODO: fix me - this passes on SQLite and needs to be fixed in Limbo. do_execsql_test json_extract_multiple_null_paths { SELECT json_extract(1, null, null, null)