diff --git a/rayforce/src/expr.rs b/rayforce/src/expr.rs index b041c09..799caa2 100644 --- a/rayforce/src/expr.rs +++ b/rayforce/src/expr.rs @@ -60,9 +60,9 @@ macro_rules! into_expr_lit { into_expr_lit!(bool, u8, i16, i32, i64, f32, f64); // In expressions, bare string literals become STRING atoms (matching the -// Python query compiler): the select/DAG path compares symbol columns against -// string scalars, whereas a symbol scalar raises a schema error. (This differs -// from the general `ToValue` path, where `&str` is a symbol.) +// Python query compiler). Explicit SYM literals are quoted during compilation +// below, so neither column nor global-env names can capture their value. +// This differs from the general `ToValue` path, where `&str` is a symbol. impl IntoExpr for &str { fn into_expr(self) -> Expr { Expr::Lit(Value::string(self)) @@ -195,6 +195,12 @@ impl Expr { pub fn compile(&self) -> Value { match self { Expr::Col(name) => Value::name_ref(name), + // A literal SYM must not be resolved as a column/global name. + // Use explicit quote: ATTR_QUOTED alone still permits query-column + // capture, and mutating a retained clone would alter the caller. + Expr::Lit(v) if v.type_code() == -(rayforce_sys::RAY_SYM as i8) => { + Value::list(&[Value::name_ref(Operation::Quote.as_str()), v.clone()]) + } Expr::Lit(v) => v.clone(), Expr::Op(op, operands) => { let mut items = Vec::with_capacity(operands.len() + 1); diff --git a/rayforce/tests/expr.rs b/rayforce/tests/expr.rs index 9daa0ed..102842b 100644 --- a/rayforce/tests/expr.rs +++ b/rayforce/tests/expr.rs @@ -139,3 +139,32 @@ fn sum_of_filter() { }) .unwrap(); } + +#[test] +fn symbol_literal_standalone_and_compile_shape() { + Runtime::scope(|_rt| { + let value = Value::sym("rs26_literal_a"); + let expression = lit(value.clone()); + let attrs = value.attrs(); + let compiled = expression.compile(); + assert!(compiled.is_list()); + assert_eq!(compiled.len(), 2); + assert_eq!(compiled.get(0).unwrap().as_sym().unwrap(), "quote"); + assert_eq!(compiled.get(1).unwrap().as_sym().unwrap(), "rs26_literal_a"); + assert_eq!(value.attrs(), attrs); + assert_eq!(expression.execute()?.as_sym().unwrap(), "rs26_literal_a"); + rayforce::set_global("rs26_literal_a", &Value::sym("rs26_literal_b"))?; + assert_eq!(expression.execute()?.as_sym().unwrap(), "rs26_literal_a"); + assert_eq!( + col("rs26_literal_a").execute()?.as_sym().unwrap(), + "rs26_literal_b" + ); + assert_eq!(value.attrs(), attrs); + let vector = lit(Value::sym_vec(&["A", "B"])).compile(); + assert!(vector.is_vec()); + assert!(!vector.is_list()); + assert_eq!(vector.get(0)?.as_sym().unwrap(), "A"); + Ok(()) + }) + .unwrap(); +} diff --git a/rayforce/tests/query.rs b/rayforce/tests/query.rs index 9ba13b4..ddf67df 100644 --- a/rayforce/tests/query.rs +++ b/rayforce/tests/query.rs @@ -251,3 +251,51 @@ fn head_tail_take() { }) .unwrap(); } + +#[test] +fn symbol_literal_filter_is_data() { + for scenario in 0..4 { + Runtime::scope(|_rt| { + let mut names = vec!["account"]; + let mut columns = vec![Value::sym_vec(&["A", "B"])]; + if scenario == 1 { + rayforce::set_global("A", &Value::sym("B"))?; + } + if scenario == 2 { + names.push("A"); + columns.push(Value::sym_vec(&["B", "B"])); + } + let table = Table::new(&names, &columns)?; + let select = table.select(); + let select = if scenario == 3 { + select.agg("A", col("account")) + } else { + select.col("account") + }; + let rows = select + .filter(col("account").eq(Value::sym("A"))) + .execute()?; + assert_eq!(rows.nrows(), 1, "scenario {scenario}"); + let output = if scenario == 3 { "A" } else { "account" }; + assert_eq!(rows.column(output)?.get(0)?.as_sym().unwrap(), "A"); + if scenario == 2 { + let explicit = table + .select() + .col("account") + .filter(col("account").eq(col("A"))) + .execute()?; + assert_eq!(explicit.nrows(), 1); + assert_eq!(explicit.column("account")?.get(0)?.as_sym().unwrap(), "B"); + } + let string = table + .select() + .col("account") + .filter(col("account").eq("A")) + .execute()?; + assert_eq!(string.nrows(), 1); + assert_eq!(string.column("account")?.get(0)?.as_sym().unwrap(), "A"); + Ok(()) + }) + .unwrap(); + } +}