Skip to content
Draft
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
269 changes: 108 additions & 161 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ members = [
"cala-ledger",
"cala-ledger-core-types",
"cala-tracing",
"cala-cel-parser",
"cala-cel-interpreter",
"cala-perf",
]

[workspace.dependencies]
cel-parser = { path = "cala-cel-parser", package = "cala-cel-parser", version = "0.15.9-dev" }
cel-interpreter = { path = "cala-cel-interpreter", package = "cala-cel-interpreter", version = "0.15.9-dev" }
cala-types = { path = "cala-ledger-core-types", package = "cala-ledger-core-types", version = "0.15.9-dev" }
cala-tracing = { path = "cala-tracing", version = "0.15.9-dev" }
cala-ledger = { path = "cala-ledger", version = "0.15.9-dev" }

cel = "0.13.0"
es-entity = "0.10.34"
job = { version = "0.6.22", features = ["es-entity"] }
obix = { version = "0.2.26", default-features = false }
Expand Down Expand Up @@ -47,8 +46,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = { version = "0.28", features = ["derive"] }
futures = "0.3.29"
lalrpop-util = { version = "0.23", features = ["lexer"] }
lalrpop = { version = "0.23", features = ["lexer"] }
rust_decimal_macros = "1.39"
rust_decimal = "1.41"
rusty-money = { version = "0.5", features = ["iso", "crypto"] }
Expand Down
2 changes: 1 addition & 1 deletion cala-cel-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fail-on-warnings = []
json-schema = ["dep:schemars"]

[dependencies]
cel-parser = { workspace = true }
cel = { workspace = true }
es-entity = { workspace = true }

chrono = { workspace = true }
Expand Down
26 changes: 0 additions & 26 deletions cala-cel-interpreter/src/builtins/decimal.rs

This file was deleted.

134 changes: 106 additions & 28 deletions cala-cel-interpreter/src/builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,118 @@
pub(crate) mod decimal;
pub(crate) mod timestamp;
use std::sync::Arc;

use chrono::NaiveDate;
use tracing::instrument;
use cel::{
extractors::{Arguments, This},
objects::Value,
ExecutionError,
};
use chrono::{FixedOffset, NaiveDate, TimeZone, Utc};
use es_entity::clock::ClockHandle;

use std::sync::Arc;
use crate::value::{CelDecimal, CelUuid};

use super::value::*;
use crate::context::CelContext;
use crate::error::*;
type Result<T> = std::result::Result<T, ExecutionError>;

#[instrument(name = "cel.builtin.date", skip_all, level = "debug", err(level = tracing::Level::WARN))]
pub(crate) fn date(ctx: &CelContext, args: Vec<CelValue>) -> Result<CelValue, CelError> {
if args.is_empty() {
return Ok(CelValue::Date(ctx.clock().now().date_naive()));
pub(crate) fn date(clock: ClockHandle, Arguments(args): Arguments) -> Result<Value> {
let date = match args.as_slice() {
[] => clock.now().date_naive(),
[Value::String(s)] => NaiveDate::parse_from_str(s, "%Y-%m-%d")
.map_err(|e| ExecutionError::function_error("date", e))?,
[Value::Timestamp(ts)] => ts.date_naive(),
[v] => {
return Err(ExecutionError::function_error(
"date",
format!("cannot convert {v:?} to date"),
))
}
values => {
return Err(ExecutionError::invalid_argument_count(1, values.len()));
}
};

let dt = date.and_hms_opt(0, 0, 0).expect("midnight is valid");
Ok(Value::Timestamp(
FixedOffset::east_opt(0)
.expect("UTC offset is valid")
.from_utc_datetime(&dt),
))
}

pub(crate) fn uuid(Arguments(args): Arguments) -> Result<Value> {
match args.as_slice() {
[Value::String(s)] => {
let id = s
.parse()
.map_err(|e| ExecutionError::function_error("uuid", format!("{e:?}")))?;
Ok(Value::Opaque(Arc::new(CelUuid(id))))
}
[v] => Err(ExecutionError::function_error(
"uuid",
format!("cannot convert {v:?} to uuid"),
)),
values => Err(ExecutionError::invalid_argument_count(1, values.len())),
}
}

let s: Arc<String> = assert_arg(args.first())?;
Ok(CelValue::Date(NaiveDate::parse_from_str(&s, "%Y-%m-%d")?))
pub(crate) fn decimal(Arguments(args): Arguments) -> Result<Value> {
match args.as_slice() {
[Value::Opaque(o)] if o.runtime_type_name() == "cala.Decimal" => {
Ok(Value::Opaque(o.clone()))
}
[Value::String(s)] => {
let decimal = s
.parse()
.map_err(|e| ExecutionError::function_error("decimal", format!("{e:?}")))?;
Ok(Value::Opaque(Arc::new(CelDecimal(decimal))))
}
[Value::Int(i)] => Ok(Value::Opaque(Arc::new(CelDecimal((*i).into())))),
[Value::UInt(u)] => Ok(Value::Opaque(Arc::new(CelDecimal((*u).into())))),
[v] => Err(ExecutionError::function_error(
"decimal",
format!("cannot convert {v:?} to decimal"),
)),
values => Err(ExecutionError::invalid_argument_count(1, values.len())),
}
}

#[instrument(name = "cel.builtin.uuid", skip_all, level = "debug", err(level = tracing::Level::WARN))]
pub(crate) fn uuid(args: Vec<CelValue>) -> Result<CelValue, CelError> {
let s: Arc<String> = assert_arg(args.first())?;
Ok(CelValue::Uuid(
s.parse()
.map_err(|e| CelError::UuidError(format!("{e:?}")))?,
))
pub(crate) fn decimal_add(Arguments(args): Arguments) -> Result<Value> {
match args.as_slice() {
[left, right] => {
let left = decimal_from_value(left)?;
let right = decimal_from_value(right)?;
Ok(Value::Opaque(Arc::new(CelDecimal(left + right))))
}
values => Err(ExecutionError::invalid_argument_count(2, values.len())),
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimal arithmetic and ordering operators lost with opaque wrapping

Medium Severity

Wrapping Decimal as Value::Opaque removes support for native +, -, * operators and <, <=, >, >= comparisons that the old evaluator explicitly provided. Only decimal.Add is re-implemented as a named function. There are no equivalents for subtraction or multiplication, and cel-rust cannot apply arithmetic or ordering operators to opaque types. In a financial ledger system, this regression in decimal capabilities is concerning even if current expressions happen to use decimal.Add() rather than the + operator.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d9dd2cd. Configure here.


pub(crate) fn timestamp_format(This(this): This<Value>, format: Arc<String>) -> Result<Value> {
match this {
Value::Timestamp(ts) => Ok(Value::String(
ts.with_timezone(&Utc).format(&format).to_string().into(),
)),
v => Err(ExecutionError::function_error(
"format",
format!("cannot format {v:?} as timestamp"),
)),
}
}

fn assert_arg<'a, T: TryFrom<&'a CelValue, Error = CelError>>(
arg: Option<&'a CelValue>,
) -> Result<T, CelError> {
if let Some(v) = arg {
T::try_from(v)
} else {
Err(CelError::MissingArgument)
fn decimal_from_value(value: &Value) -> Result<rust_decimal::Decimal> {
match value {
Value::Opaque(o) if o.runtime_type_name() == "cala.Decimal" => {
let decimal = o.downcast_ref::<CelDecimal>().ok_or_else(|| {
ExecutionError::function_error("decimal", "failed to downcast decimal")
})?;
Ok(decimal.0)
}
Value::String(s) => s
.parse()
.map_err(|e| ExecutionError::function_error("decimal", format!("{e:?}"))),
Value::Int(i) => Ok((*i).into()),
Value::UInt(u) => Ok((*u).into()),
v => Err(ExecutionError::function_error(
"decimal",
format!("cannot convert {v:?} to decimal"),
)),
}
}
27 changes: 0 additions & 27 deletions cala-cel-interpreter/src/builtins/timestamp.rs

This file was deleted.

20 changes: 0 additions & 20 deletions cala-cel-interpreter/src/cel_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,3 @@ pub enum CelType {
Uuid,
Decimal,
}

impl CelType {
pub(crate) fn package_name(&self) -> &'static str {
match self {
CelType::Map => "map",
CelType::List => "list",
CelType::Int => "int",
CelType::UInt => "uint",
CelType::Double => "double",
CelType::String => "string",
CelType::Bytes => "bytes",
CelType::Bool => "bool",
CelType::Null => "null",
CelType::Date => "date",
CelType::Timestamp => "timestamp",
CelType::Uuid => "uuid",
CelType::Decimal => "decimal",
}
}
}
25 changes: 0 additions & 25 deletions cala-cel-interpreter/src/context/decimal.rs

This file was deleted.

Loading
Loading