Skip to content

World's first open-source enterprise world model.

Compile field conversions into the expression tree rules already execute (#1133)

Nash Aschenbath0e24b32f6197
diff --git a/Cargo.lock b/Cargo.lock
index c4b2fe60..51e19ff9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5482,6 +5482,7 @@ dependencies = [
name = "utopia-reason"
version = "0.1.0"
dependencies = [
+ "chrono",
"serde_json",
"uuid",
]
@@ -5539,6 +5540,7 @@ dependencies = [
"utopia-extract",
"utopia-ingest",
"utopia-llm",
+ "utopia-reason",
"utopia-search",
"utopia-store",
"uuid",
diff --git a/crates/utopia-reason/Cargo.toml b/crates/utopia-reason/Cargo.toml
index c5aa43fe..eaf71922 100644
--- a/crates/utopia-reason/Cargo.toml
+++ b/crates/utopia-reason/Cargo.toml
@@ -8,3 +8,4 @@ license.workspace = true
uuid.workspace = true
# 属性值是字面值(`facts.object_value`),比较要认它的形状
serde_json.workspace = true
+chrono.workspace = true
diff --git a/crates/utopia-reason/src/expressions.rs b/crates/utopia-reason/src/expressions.rs
new file mode 100644
index 00000000..3f08d4c2
--- /dev/null
+++ b/crates/utopia-reason/src/expressions.rs
@@ -0,0 +1,532 @@
+//! The expression tree shared by rules and column conversions (0032, 0036).
+//!
+//! CASE is a code-to-value map, not another predicate language. Its arms are
+//! literals, so every attribute the tree reads is a premise of every result.
+
+use crate::rules::Arith;
+use chrono::{Datelike, NaiveDate};
+use serde_json::{json, Value};
+use uuid::Uuid;
+
+/// The existing bound counts edges from the root, not nodes.
+pub const MAX_EXPR_DEPTH: usize = 4;
+pub const MAX_CASE_ARMS: usize = 32;
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Scalar {
+ Number(f64),
+ Text(String),
+}
+
+impl Scalar {
+ pub fn read(value: &Value) -> Option<Self> {
+ match value {
+ Value::Number(n) => n.as_f64().filter(|n| n.is_finite()).map(Self::Number),
+ Value::String(s) => Some(Self::Text(s.clone())),
+ _ => None,
+ }
+ }
+
+ /// Numeric strings remain valid readings, as they were before conversions.
+ pub fn number(&self) -> Option<f64> {
+ match self {
+ Self::Number(n) => Some(*n),
+ Self::Text(s) => s.trim().parse().ok(),
+ }
+ .filter(|n: &f64| n.is_finite())
+ }
+
+ pub fn to_json(&self) -> Value {
+ match self {
+ Self::Number(n) => json!(n),
+ Self::Text(s) => json!(s),
+ }
+ }
+
+ fn same_type(&self, other: &Self) -> bool {
+ std::mem::discriminant(self) == std::mem::discriminant(other)
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum DatePart {
+ Year,
+ Month,
+ Day,
+}
+
+impl DatePart {
+ pub fn parse(s: &str) -> Option<Self> {
+ Some(match s {
+ "year" => Self::Year,
+ "month" => Self::Month,
+ "day" => Self::Day,
+ _ => return None,
+ })
+ }
+
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::Year => "year",
+ Self::Month => "month",
+ Self::Day => "day",
+ }
+ }
+
+ fn truncate(self, value: Scalar) -> Option<Scalar> {
+ let Scalar::Text(text) = value else {
+ return None;
+ };
+ let date = if text.len() == 10 {
+ NaiveDate::parse_from_str(&text, "%Y-%m-%d").ok()?
+ } else {
+ // An offset is required: the host timezone must not change a fact.
+ chrono::DateTime::parse_from_rfc3339(&text)
+ .ok()?
+ .with_timezone(&chrono::Utc)
+ .date_naive()
+ };
+ let date = match self {
+ Self::Year => NaiveDate::from_ymd_opt(date.year(), 1, 1)?,
+ Self::Month => NaiveDate::from_ymd_opt(date.year(), date.month(), 1)?,
+ Self::Day => date,
+ };
+ Some(Scalar::Text(date.format("%Y-%m-%d").to_string()))
+ }
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Expr {
+ Attr(Uuid),
+ Const(f64),
+ Text(String),
+ Arith {
+ op: Arith,
+ l: Box<Expr>,
+ r: Box<Expr>,
+ },
+ Number(Box<Expr>),
+ Case {
+ expr: Box<Expr>,
+ arms: Vec<(Scalar, Scalar)>,
+ otherwise: Option<Scalar>,
+ },
+ DateTrunc {
+ part: DatePart,
+ expr: Box<Expr>,
+ },
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ExprError {
+ TooDeep,
+ Invalid(&'static str),
+}
+
+impl ExprError {
+ pub fn code(&self) -> &'static str {
+ match self {
+ Self::TooDeep => "expression_too_deep",
+ Self::Invalid(_) => "bad_expression",
+ }
+ }
+
+ pub fn message(&self) -> &'static str {
+ match self {
+ Self::TooDeep => "That expression nests too deeply; a rule computes with a few operations, not a program.",
+ Self::Invalid(message) => message,
+ }
+ }
+}
+
+impl Expr {
+ /// One decoder for write validation and materialization. Legacy numeric
+ /// strings keep their meaning; text labels have a separate node.
+ pub fn from_json(raw: &Value) -> Result<Self, ExprError> {
+ Self::decode(raw, 0)
+ }
+
+ fn decode(raw: &Value, depth: usize) -> Result<Self, ExprError> {
+ if depth > MAX_EXPR_DEPTH {
+ return Err(ExprError::TooDeep);
+ }
+ let bad = ExprError::Invalid("That expression contains an invalid or unsupported node.");
+ let obj = raw.as_object().ok_or_else(|| bad.clone())?;
+ // A mixed shape must not acquire meaning from the order of these checks.
+ // The page treats unknown fields as unknown too, rather than hiding them.
+ if let Some(a) = obj.get("attr") {
+ if obj.len() != 1 {
+ return Err(bad);
+ }
+ return Ok(Self::Attr(
+ a.as_str().and_then(|s| s.parse().ok()).ok_or(bad)?,
+ ));
+ }
+ if let Some(c) = obj.get("const") {
+ if obj.len() != 1 {
+ return Err(bad);
+ }
+ let n = Scalar::read(c).and_then(|s| s.number()).ok_or(bad)?;
+ return Ok(Self::Const(n));
+ }
+ if let Some(text) = obj.get("text").filter(|_| obj.len() == 1) {
+ return Ok(Self::Text(text.as_str().ok_or(bad)?.to_owned()));
+ }
+ if obj.get("cast").and_then(Value::as_str) == Some("number") && obj.len() == 2 {
+ return Ok(Self::Number(Box::new(Self::decode(
+ obj.get("expr").ok_or(bad)?,
+ depth + 1,
+ )?)));
+ }
+ if let Some(part) = obj.get("date_trunc").filter(|_| obj.len() == 2) {
+ return Ok(Self::DateTrunc {
+ part: part
+ .as_str()
+ .and_then(DatePart::parse)
+ .ok_or_else(|| bad.clone())?,
+ expr: Box::new(Self::decode(obj.get("expr").ok_or(bad)?, depth + 1)?),
+ });
+ }
+ if let Some(selector) = obj.get("case") {
+ if obj.len() != 2 + usize::from(obj.contains_key("else")) {
+ return Err(bad);
+ }
+ let raw_arms = obj
+ .get("when")
+ .and_then(Value::as_array)
+ .ok_or_else(|| bad.clone())?;
+ if raw_arms.is_empty() || raw_arms.len() > MAX_CASE_ARMS {
+ return Err(ExprError::Invalid(
+ "A case needs between one and 32 literal arms.",
+ ));
+ }
+ let mut arms = Vec::with_capacity(raw_arms.len());
+ for arm in raw_arms {
+ let arm = arm
+ .as_object()
+ .filter(|a| a.len() == 2)
+ .ok_or_else(|| bad.clone())?;
+ let key = arm
+ .get("is")
+ .and_then(Scalar::read)
+ .ok_or_else(|| bad.clone())?;
+ let value = arm
+ .get("then")
+ .and_then(Scalar::read)
+ .ok_or_else(|| bad.clone())?;
+ arms.push((key, value));
+ }
+ let otherwise = obj
+ .get("else")
+ .map(|v| Scalar::read(v).ok_or(bad))
+ .transpose()?;
+ let first = &arms[0].1;
+ if !arms.iter().all(|(_, v)| first.same_type(v))
+ || otherwise.as_ref().is_some_and(|v| !first.same_type(v))
+ {
+ return Err(ExprError::Invalid(
+ "A case's results must all be numbers or all be text.",
+ ));
+ }
+ return Ok(Self::Case {
+ expr: Box::new(Self::decode(selector, depth + 1)?),
+ arms,
+ otherwise,
+ });
+ }
+ if obj.len() != 3 {
+ return Err(bad);
+ }
+ let op = obj
+ .get("op")
+ .and_then(Value::as_str)
+ .and_then(Arith::parse)
+ .ok_or_else(|| bad.clone())?;
+ Ok(Self::Arith {
+ op,
+ l: Box::new(Self::decode(
+ obj.get("l").ok_or_else(|| bad.clone())?,
+ depth + 1,
+ )?),
+ r: Box::new(Self::decode(obj.get("r").ok_or(bad)?, depth + 1)?),
+ })
+ }
+
+ pub fn to_json(&self) -> Value {
+ match self {
+ Self::Attr(id) => json!({"attr": id}),
+ Self::Const(n) => json!({"const": n}),
+ Self::Text(s) => json!({"text": s}),
+ Self::Arith { op, l, r } => {
+ json!({"op": op.as_str(), "l": l.to_json(), "r": r.to_json()})
+ }
+ Self::Number(expr) => json!({"cast": "number", "expr": expr.to_json()}),
+ Self::DateTrunc { part, expr } => {
+ json!({"date_trunc": part.as_str(), "expr": expr.to_json()})
+ }
+ Self::Case {
+ expr,
+ arms,
+ otherwise,
+ } => {
+ let arms: Vec<_> = arms
+ .iter()
+ .map(|(key, value)| json!({"is": key.to_json(), "then": value.to_json()}))
+ .collect();
+ let mut out = json!({"case": expr.to_json(), "when": arms});
+ if let Some(value) = otherwise {
+ out["else"] = value.to_json();
+ }
+ out
+ }
+ }
+ }
+
+ /// Attribute IDs in encounter order, without duplicates.
+ pub fn predicates(&self, out: &mut Vec<Uuid>) {
+ match self {
+ Self::Attr(p) => {
+ if !out.contains(p) {
+ out.push(*p);
+ }
+ }
+ Self::Const(_) | Self::Text(_) => {}
+ Self::Arith { l, r, .. } => {
+ l.predicates(out);
+ r.predicates(out);
+ }
+ Self::Number(expr) | Self::Case { expr, .. } | Self::DateTrunc { expr, .. } => {
+ expr.predicates(out)
+ }
+ }
+ }
+
+ pub fn depth(&self) -> usize {
+ match self {
+ Self::Attr(_) | Self::Const(_) | Self::Text(_) => 1,
+ Self::Arith { l, r, .. } => 1 + l.depth().max(r.depth()),
+ Self::Number(expr) | Self::Case { expr, .. } | Self::DateTrunc { expr, .. } => {
+ 1 + expr.depth()
+ }
+ }
+ }
+
+ /// A failed conversion is absent, just like a missing reading or division
+ /// by zero. A caller must not turn it into a zero or a successful fact.
+ pub fn evaluate(&self, read: &impl Fn(Uuid) -> Option<Scalar>) -> Option<Scalar> {
+ match self {
+ Self::Attr(p) => read(*p),
+ Self::Const(n) => n.is_finite().then_some(Scalar::Number(*n)),
+ Self::Text(s) => Some(Scalar::Text(s.clone())),
+ Self::Number(expr) => expr.evaluate(read)?.number().map(Scalar::Number),
+ Self::Arith { op, l, r } => {
+ let (a, b) = (l.evaluate(read)?.number()?, r.evaluate(read)?.number()?);
+ let value = match op {
+ Arith::Add => a + b,
+ Arith::Sub => a - b,
+ Arith::Mul => a * b,
+ Arith::Div if b != 0.0 => a / b,
+ Arith::Div => return None,
+ };
+ value.is_finite().then_some(Scalar::Number(value))
+ }
+ Self::Case {
+ expr,
+ arms,
+ otherwise,
+ } => {
+ let key = expr.evaluate(read)?;
+ arms.iter()
+ .find(|(candidate, _)| *candidate == key)
+ .map(|(_, value)| value.clone())
+ .or_else(|| otherwise.clone())
+ }
+ Self::DateTrunc { part, expr } => part.truncate(expr.evaluate(read)?),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::rules::{
+ evaluate, AttrFact, BusinessRule, Conclusion, Condition, Op, Operand, Side,
+ };
+ use std::collections::HashMap;
+
+ fn id(n: u128) -> Uuid {
+ Uuid::from_u128(n)
+ }
+
+ #[test]
+ fn conversions_round_trip_and_keep_only_their_input_references() {
+ let raw = json!({"op":"div", "l":{"cast":"number","expr":{"attr":id(1)}},
+ "r":{"case":{"attr":id(2)},"when":[{"is":"cents","then":100}],"else":1}});
+ let expr = Expr::from_json(&raw).unwrap();
+ let mut reads = Vec::new();
+ expr.predicates(&mut reads);
+ assert_eq!(reads, [id(1), id(2)]);
+ let values = HashMap::from([
+ (id(1), Scalar::Text("12550".into())),
+ (id(2), Scalar::Text("cents".into())),
+ ]);
+ assert_eq!(
+ expr.evaluate(&|p| values.get(&p).cloned()),
+ Some(Scalar::Number(125.5))
+ );
+ assert_eq!(Expr::from_json(&expr.to_json()).unwrap(), expr);
+ assert_eq!(
+ Expr::from_json(&json!({"const":"2.50"})).unwrap(),
+ Expr::Const(2.5)
+ );
+ assert_eq!(
+ Expr::from_json(&json!({"text":"2.50"})).unwrap(),
+ Expr::Text("2.50".into())
+ );
+ }
+
+ #[test]
+ fn missing_or_invalid_values_never_become_a_default_or_zero() {
+ let cast = Expr::from_json(&json!({"cast":"number","expr":{"attr":id(1)}})).unwrap();
+ for value in [
+ Value::Null,
+ json!(false),
+ json!(""),
+ json!("NaN"),
+ json!("Infinity"),
+ json!("1e999"),
+ json!("12 CNY"),
+ ] {
+ assert_eq!(cast.evaluate(&|_| Scalar::read(&value)), None, "{value}");
+ }
+ let choice = Expr::from_json(
+ &json!({"case":{"attr":id(1)},"when":[{"is":1,"then":"app"}],"else":"other"}),
+ )
+ .unwrap();
+ assert_eq!(choice.evaluate(&|_| None), None);
+ assert_eq!(
+ choice.evaluate(&|_| Some(Scalar::Number(9.0))),
+ Some(Scalar::Text("other".into()))
+ );
+ let no_else =
+ Expr::from_json(&json!({"case":{"attr":id(1)},"when":[{"is":1,"then":"app"}]}))
+ .unwrap();
+ assert_eq!(no_else.evaluate(&|_| Some(Scalar::Number(9.0))), None);
+ // Text codes are not silently coerced to numeric codes by CASE.
+ assert_eq!(no_else.evaluate(&|_| Some(Scalar::Text("1".into()))), None);
+ }
+
+ #[test]
+ fn calendar_truncation_uses_utc_and_rejects_ambiguous_or_invalid_dates() {
+ for (part, input, expected) in [
+ ("month", "2024-03-01T00:30:00+08:00", "2024-02-01"),
+ ("day", "2024-02-29T23:30:00-02:00", "2024-03-01"),
+ ("year", "2024-01-01T00:30:00+01:00", "2023-01-01"),
+ ("day", "2024-02-29", "2024-02-29"),
+ ("month", "2024-02-29", "2024-02-01"),
+ ] {
+ let expr = Expr::from_json(&json!({"date_trunc":part,"expr":{"attr":id(1)}})).unwrap();
+ assert_eq!(
+ expr.evaluate(&|_| Some(Scalar::Text(input.into()))),
+ Some(Scalar::Text(expected.into()))
+ );
+ }
+ let expr = Expr::from_json(&json!({"date_trunc":"month","expr":{"attr":id(1)}})).unwrap();
+ for input in [
+ "2023-02-29",
+ "2024-02-30",
+ "2024-03-01T00:00:00",
+ "yesterday",
+ "",
+ ] {
+ assert_eq!(
+ expr.evaluate(&|_| Some(Scalar::Text(input.into()))),
+ None,
+ "{input}"
+ );
+ }
+ }
+
+ #[test]
+ fn every_new_node_obeys_the_depth_and_case_bounds() {
+ let mut raw = json!({"attr":id(1)});
+ for _ in 0..MAX_EXPR_DEPTH {
+ raw = json!({"cast":"number","expr":raw});
+ }
+ assert!(Expr::from_json(&raw).is_ok());
+ assert_eq!(
+ Expr::from_json(&json!({"date_trunc":"day","expr":raw})),
+ Err(ExprError::TooDeep)
+ );
+ for raw in [
+ json!({"attr":id(1),"cast":"number","expr":{"attr":id(2)}}),
+ json!({"const":2,"text":"app"}),
+ json!({"op":"add","l":{"attr":id(1)},"r":{"const":1},"future":true}),
+ json!({"cast":"integer","expr":{"attr":id(1)}}),
+ json!({"date_trunc":"hour","expr":{"attr":id(1)}}),
+ json!({"case":{"attr":id(1)},"when":[]}),
+ json!({"case":{"attr":id(1)},"when":[{"is":1,"then":"app"}],"else":0}),
+ json!({"case":{"attr":id(1)},"when":[{"is":1,"then":{"attr":id(2)}}]}),
+ json!({"case":{"attr":id(1)},"when":[{"is":null,"then":"app"}]}),
+ json!({"case":{"attr":id(1)},"when":(0..=MAX_CASE_ARMS).map(|n|json!({"is":n,"then":"app"})).collect::<Vec<_>>()}),
+ ] {
+ assert!(Expr::from_json(&raw).is_err(), "{raw}");
+ }
+ }
+
+ #[test]
+ fn text_results_keep_each_readings_interval_and_proof() {
+ let facts = [
+ AttrFact {
+ id: id(1),
+ subject: id(99),
+ predicate: id(10),
+ value: json!(5),
+ },
+ AttrFact {
+ id: id(2),
+ subject: id(99),
+ predicate: id(11),
+ value: json!(1),
+ },
+ AttrFact {
+ id: id(3),
+ subject: id(99),
+ predicate: id(11),
+ value: json!(2),
+ },
+ ];
+ let spans = HashMap::from([
+ (id(1), (Some(10), Some(30))),
+ (id(2), (Some(0), Some(20))),
+ (id(3), (Some(20), Some(40))),
+ ]);
+ let rule = BusinessRule {
+ id: id(50),
+ join_predicate: None,
+ conclusion: Conclusion::Computed {
+ predicate: id(12),
+ expr: Expr::from_json(&json!({
+ "case":{"attr":id(11)},"when":[{"is":1,"then":"app"},{"is":2,"then":"web"}]
+ }))
+ .unwrap(),
+ },
+ conditions: vec![Condition {
+ group: 0,
+ side: Side::X,
+ predicate: id(10),
+ op: Op::Present,
+ operand: Operand::None,
+ }],
+ };
+ let (mut hits, report) = evaluate(&[rule], &facts, &spans, &[]);
+ assert_eq!(report.hits, 2);
+ hits.sort_by_key(|h| h.from);
+ assert_eq!((hits[0].from, hits[0].to), (Some(10), Some(20)));
+ assert_eq!(hits[0].value, Some(Scalar::Text("app".into())));
+ assert_eq!(hits[0].premises, [id(1), id(2)]);
+ assert_eq!((hits[1].from, hits[1].to), (Some(20), Some(30)));
+ assert_eq!(hits[1].value, Some(Scalar::Text("web".into())));
+ assert_eq!(hits[1].premises, [id(1), id(3)]);
+ }
+}
diff --git a/crates/utopia-reason/src/lib.rs b/crates/utopia-reason/src/lib.rs
index a5a9c274..d3eedc45 100644
--- a/crates/utopia-reason/src/lib.rs
+++ b/crates/utopia-reason/src/lib.rs
@@ -13,6 +13,7 @@
//! 是零,那是实情不是故障。
pub mod derive;
+pub mod expressions;
pub mod ontology;
pub mod rules;
diff --git a/crates/utopia-reason/src/rules.rs b/crates/utopia-reason/src/rules.rs
index 198ed9d4..93d70163 100644
--- a/crates/utopia-reason/src/rules.rs
+++ b/crates/utopia-reason/src/rules.rs
@@ -10,6 +10,7 @@
//! 这一层同样不碰数据库。取规则、取属性事实、落库都在 `utopia-store`。
use crate::derive::validity;
+pub use crate::expressions::{Expr, Scalar, MAX_EXPR_DEPTH};
use std::collections::HashMap;
use uuid::Uuid;
@@ -145,76 +146,22 @@ impl Arith {
}
}
-/// 一个数是怎么算出来的(0032)。叶子要么是这个实体的一条属性读数,要么是
-/// 写死的数;中间是四则。
-///
-/// **树,不是字符串。** 存下来的就是这棵树,界面照着它渲染,求值照着它算——
-/// 两者不会漂移,而这正是 0002 拒掉「用户自定义规则语言」时真正在守的东西。
-#[derive(Debug, Clone, PartialEq)]
-pub enum Expr {
- /// 这个实体在这个谓词上的读数
- Attr(Uuid),
- Const(f64),
- Arith {
- op: Arith,
- l: Box<Expr>,
- r: Box<Expr>,
- },
-}
-
-/// 一棵算式最深几层。太深的树是「有人在这里写程序」的信号,编译时就该拦下
-pub const MAX_EXPR_DEPTH: usize = 4;
-
impl Expr {
- /// 这棵树读了哪些谓词,按出现顺序、去重。求值前要为它们各留一个槽位
- pub fn predicates(&self, out: &mut Vec<Uuid>) {
- match self {
- Expr::Attr(p) => {
- if !out.contains(p) {
- out.push(*p);
- }
- }
- Expr::Const(_) => {}
- Expr::Arith { l, r, .. } => {
- l.predicates(out);
- r.predicates(out);
- }
- }
+ /// Comparisons still need a number, including the legacy numeric-string readings.
+ pub fn eval(&self, bound: &HashMap<Uuid, &AttrFact>) -> Option<f64> {
+ self.compute(bound)?.number()
}
- pub fn depth(&self) -> usize {
- match self {
- Expr::Attr(_) | Expr::Const(_) => 1,
- Expr::Arith { l, r, .. } => 1 + l.depth().max(r.depth()),
+ fn compute(&self, bound: &HashMap<Uuid, &AttrFact>) -> Option<Scalar> {
+ let value = self.evaluate(&|id| Scalar::read(&bound.get(&id)?.value))?;
+ // An old bare-attribute expression meant a numeric reading. CASE and
+ // date truncation introduce text results explicitly, without changing it.
+ if matches!(self, Self::Attr(_)) {
+ value.number().map(Scalar::Number)
+ } else {
+ Some(value)
}
}
-
- /// 按这一轮选中的读数算一个数。
- ///
- /// **算不出来就是 None,不是 0**:读数不在(没记 ≠ 零)、值不是数、除零,
- /// 三种情况一律没有值,于是这条规则在这个组合上不出结论(0032)。把它们
- /// 当零,等于在无知的地方填一个确定的答案。
- pub fn eval(&self, bound: &HashMap<Uuid, &AttrFact>) -> Option<f64> {
- Some(match self {
- Expr::Attr(p) => num(&bound.get(p)?.value)?,
- Expr::Const(n) => *n,
- Expr::Arith { op, l, r } => {
- let (a, b) = (l.eval(bound)?, r.eval(bound)?);
- match op {
- Arith::Add => a + b,
- Arith::Sub => a - b,
- Arith::Mul => a * b,
- Arith::Div => {
- if b == 0.0 {
- return None;
- }
- a / b
- }
- }
- }
- })
- .filter(|n: &f64| n.is_finite())
- }
}
#[derive(Debug, Clone, PartialEq)]
@@ -267,9 +214,9 @@ pub struct RuleHit {
pub premises: Vec<Uuid>,
pub from: Option<i64>,
pub to: Option<i64>,
- /// 算出来的结论值(0032)。**每个组合各算各的**——两条 revenue 三条 cost
- /// 就是六个数、六段区间、六行结论;常量结论这里是 None
- pub value: Option<f64>,
+ /// Each combination has its own scalar result and interval. A conversion
+ /// can also produce text or an ISO calendar date; constant conclusions use None.
+ pub value: Option<Scalar>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
@@ -360,7 +307,7 @@ pub fn evaluate_with_pool(
// 同一实体上,一条规则的多个组可能推出同一段区间。去重跨组,
// 因为它们落成的是同一行派生事实
// 去重的键带上算出来的值(0032):同一区间上两个不同的值是两行
- let mut seen: Vec<(Option<i64>, Option<i64>, Option<u64>)> = Vec::new();
+ let mut seen: Vec<(Option<i64>, Option<i64>, Option<Scalar>)> = Vec::new();
// 这条规则在这个实体上有没有哪一组因组合太多没展开完。**按 (规则, 实体)
// 记一次**:报出来的是「这里少推了东西」,不是「少推了几组」
let mut capped_here = false;
@@ -435,10 +382,10 @@ pub fn evaluate_with_pool(
if !holds {
continue;
}
- // 算出来的结论:这个组合上算不出数就不出结论(缺读数、
- // 不是数、除零),而不是落一行没有值的派生
+ // Missing readings and failed conversions do not manufacture
+ // a value. The same premises and interval cover text results.
let value = match &rule.conclusion {
- Conclusion::Computed { expr, .. } => match expr.eval(&bound) {
+ Conclusion::Computed { expr, .. } => match expr.compute(&bound) {
Some(v) => Some(v),
None => continue,
},
@@ -449,8 +396,18 @@ pub fn evaluate_with_pool(
};
// 去重按 (区间, 值):算出来的结论同一区间可以有不同的值,
// 那是两行,不是一行
- let key = (from, to, value.map(f64::to_bits));
- if seen.contains(&key) {
+ let key = (from, to, value.clone());
+ // Keep the old numeric bit key while admitting text results.
+ if seen.iter().any(|(old_from, old_to, old_value)| {
+ *old_from == from
+ && *old_to == to
+ && match (old_value, &value) {
+ (Some(Scalar::Number(a)), Some(Scalar::Number(b))) => {
+ a.to_bits() == b.to_bits()
+ }
+ _ => old_value == &value,
+ }
+ }) {
continue;
}
seen.push(key);
@@ -639,17 +596,12 @@ fn joined_evaluate(
}
fn expr_predicates(expr: &Expr, side: Side, out: &mut Vec<(Side, Uuid)>) {
- match expr {
- Expr::Attr(predicate) => {
- let slot = (side, *predicate);
- if !out.contains(&slot) {
- out.push(slot);
- }
- }
- Expr::Const(_) => {}
- Expr::Arith { l, r, .. } => {
- expr_predicates(l, side, out);
- expr_predicates(r, side, out);
+ let mut predicates = Vec::new();
+ expr.predicates(&mut predicates);
+ for predicate in predicates {
+ let slot = (side, predicate);
+ if !out.contains(&slot) {
+ out.push(slot);
}
}
}
@@ -1291,7 +1243,7 @@ mod tests {
let spans = HashMap::from([(id(1), (Some(100), None)), (id(2), (Some(100), None))]);
let (hits, _) = evaluate(&[rule], &facts, &spans, &[]);
assert_eq!(hits.len(), 1);
- assert_eq!(hits[0].value, Some(180.0));
+ assert_eq!(hits[0].value, Some(Scalar::Number(180.0)));
// 条件只提到 revenue,可 cost 也读了——它照样是前提
assert_eq!(hits[0].premises.len(), 2);
assert!(hits[0].premises.contains(&id(1)) && hits[0].premises.contains(&id(2)));
@@ -1328,7 +1280,10 @@ mod tests {
]);
let (hits, _) = evaluate(&[rule], &facts, &spans, &[]);
assert_eq!(hits.len(), 2, "两条 revenue 各算一个 margin");
- let mut values: Vec<f64> = hits.iter().filter_map(|h| h.value).collect();
+ let mut values: Vec<f64> = hits
+ .iter()
+ .filter_map(|h| h.value.as_ref()?.number())
+ .collect();
values.sort_by(f64::total_cmp);
assert_eq!(values, vec![180.0, 280.0]);
}
diff --git a/crates/utopia-server/Cargo.toml b/crates/utopia-server/Cargo.toml
index f5863311..6cf9f297 100644
--- a/crates/utopia-server/Cargo.toml
+++ b/crates/utopia-server/Cargo.toml
@@ -11,6 +11,7 @@ path = "src/main.rs"
[dependencies]
utopia-core.workspace = true
utopia-store.workspace = true
+utopia-reason.workspace = true
utopia-ingest.workspace = true
utopia-search.workspace = true
utopia-llm.workspace = true
diff --git a/crates/utopia-server/src/api/mod.rs b/crates/utopia-server/src/api/mod.rs
index 119831f9..ba7cd2d2 100644
--- a/crates/utopia-server/src/api/mod.rs
+++ b/crates/utopia-server/src/api/mod.rs
@@ -19,6 +19,7 @@ pub(crate) mod ontology_routes;
mod question_routes;
mod review_routes;
mod rig_model;
+mod rule_expression_input;
pub(crate) mod rule_routes;
mod search_routes;
mod settings_routes;
diff --git a/crates/utopia-server/src/api/rule_expression_input.rs b/crates/utopia-server/src/api/rule_expression_input.rs
new file mode 100644
index 00000000..4c5dafe0
--- /dev/null
+++ b/crates/utopia-server/src/api/rule_expression_input.rs
@@ -0,0 +1,350 @@
+//! Text is a write-time convenience, never a second stored rule language (0036).
+//! Resolve keys, compile an allowlisted scalar expression, then let the ordinary
+//! rule writer check the UUID references in the current base again.
+
+use serde_json::Value;
+use sqlparser::ast::{
+ BinaryOperator, CastKind, Expr as SqlExpr, FunctionArg, FunctionArgExpr, FunctionArguments,
+ UnaryOperator, Value as SqlValue,
+};
+use sqlparser::{dialect::GenericDialect, parser::Parser, tokenizer::Token};
+use std::collections::HashMap;
+use utopia_core::error::{AppError, AppResult};
+use utopia_reason::expressions::{DatePart, Scalar};
+use utopia_reason::rules::{Arith, Expr, MAX_EXPR_DEPTH};
+use utopia_store::business_rules::ConditionInput;
+use uuid::Uuid;
+
+const MAX_INPUT_BYTES: usize = 8192;
+
+pub(super) async fn compile_inputs(
+ pool: &sqlx::PgPool,
+ kb_id: Uuid,
+ conclusion: Option<&mut Value>,
+ conditions: &mut [ConditionInput],
+) -> AppResult<()> {
+ let mut inputs: Vec<&mut Value> = conclusion
+ .into_iter()
+ .chain(
+ conditions
+ .iter_mut()
+ .filter(|c| matches!(c.op.as_str(), "gt" | "gte" | "lt" | "lte"))
+ .filter_map(|c| c.operand.as_mut()),
+ )
+ .filter(|value| value.get("expression").is_some())
+ .collect();
+ if inputs.is_empty() {
+ // Metadata-only PATCH must not acquire new semantic requirements.
+ return Ok(());
+ }
+ let rows: Vec<(String, Uuid)> =
+ sqlx::query_as("SELECT key, id FROM relation_types WHERE kb_id=$1 AND kind='attribute'")
+ .bind(kb_id)
+ .fetch_all(pool)
+ .await?;
+ let names = rows.into_iter().collect();
+ for value in &mut inputs {
+ let text = value
+ .as_object()
+ .filter(|o| o.len() == 1)
+ .and_then(|o| o.get("expression"))
+ .and_then(Value::as_str)
+ .ok_or_else(|| {
+ invalid("Text input is an object containing only an expression string.")
+ })?;
+ **value = compile(text, &names)?;
+ }
+ Ok(())
+}
+
+fn invalid(message: impl Into<String>) -> AppError {
+ AppError::invalid("bad_expression", message)
+}
+
+fn unsupported() -> AppError {
+ invalid("Use attributes, literals, arithmetic, CAST AS DOUBLE PRECISION, a simple literal CASE, or date_trunc with year, month or day.")
+}
+
+fn compile(text: &str, names: &HashMap<String, Uuid>) -> AppResult<Value> {
+ if text.trim().is_empty() || text.len() > MAX_INPUT_BYTES {
+ return Err(invalid(
+ "An expression must contain between one and 8192 bytes.",
+ ));
+ }
+ // Bound parser recursion too: parentheses do not count as expression nodes,
+ // but accepting arbitrarily many of them would still consume a parser stack.
+ let dialect = GenericDialect {};
+ let mut parser = Parser::new(&dialect)
+ .with_recursion_limit(32)
+ .try_with_sql(text)
+ .map_err(|e| invalid(e.to_string()))?;
+ let ast = parser.parse_expr().map_err(|e| invalid(e.to_string()))?;
+ if parser.peek_token().token != Token::EOF {
+ return Err(invalid(
+ "Supply one scalar expression, without a statement, alias or trailing clause.",
+ ));
+ }
+ let expr = compile_ast(&ast, names, 0)?;
+ let tree = expr.to_json();
+ // The JSON and text entry points must accept the same case shapes and depth.
+ Expr::from_json(&tree).map_err(|e| AppError::invalid(e.code(), e.message()))?;
+ Ok(tree)
+}
+
+fn literal(ast: &SqlExpr) -> AppResult<Scalar> {
+ match ast {
+ SqlExpr::Nested(inner) => literal(inner),
+ SqlExpr::Value(v) => match &v.value {
+ SqlValue::Number(text, false) => text
+ .parse::<f64>()
+ .ok()
+ .filter(|n| n.is_finite())
+ .map(Scalar::Number)
+ .ok_or_else(unsupported),
+ SqlValue::SingleQuotedString(text) => Ok(Scalar::Text(text.clone())),
+ _ => Err(unsupported()),
+ },
+ SqlExpr::UnaryOp { op, expr }
+ if matches!(op, UnaryOperator::Plus | UnaryOperator::Minus) =>
+ {
+ let Scalar::Number(n) = literal(expr)? else {
+ return Err(unsupported());
+ };
+ Ok(Scalar::Number(if *op == UnaryOperator::Minus {
+ -n
+ } else {
+ n
+ }))
+ }
+ _ => Err(invalid(
+ "CASE keys and results must be literal numbers or text.",
+ )),
+ }
+}
+
+fn compile_ast(ast: &SqlExpr, names: &HashMap<String, Uuid>, depth: usize) -> AppResult<Expr> {
+ if depth > MAX_EXPR_DEPTH {
+ let error = utopia_reason::expressions::ExprError::TooDeep;
+ return Err(AppError::invalid(error.code(), error.message()));
+ }
+ match ast {
+ SqlExpr::Nested(inner) => compile_ast(inner, names, depth),
+ SqlExpr::Identifier(name) => {
+ names
+ .get(&name.value)
+ .copied()
+ .map(Expr::Attr)
+ .ok_or_else(|| {
+ AppError::invalid(
+ "unknown_expression_attribute",
+ format!("There is no attribute key {:?} in this base.", name.value),
+ )
+ })
+ }
+ SqlExpr::Value(_) => match literal(ast)? {
+ Scalar::Number(n) => Ok(Expr::Const(n)),
+ Scalar::Text(s) => Ok(Expr::Text(s)),
+ },
+ SqlExpr::BinaryOp { left, op, right } => {
+ let op = match op {
+ BinaryOperator::Plus => Arith::Add,
+ BinaryOperator::Minus => Arith::Sub,
+ BinaryOperator::Multiply => Arith::Mul,
+ BinaryOperator::Divide => Arith::Div,
+ _ => return Err(unsupported()),
+ };
+ Ok(Expr::Arith {
+ op,
+ l: Box::new(compile_ast(left, names, depth + 1)?),
+ r: Box::new(compile_ast(right, names, depth + 1)?),
+ })
+ }
+ SqlExpr::UnaryOp { op, expr }
+ if matches!(op, UnaryOperator::Plus | UnaryOperator::Minus) =>
+ {
+ if let Ok(Scalar::Number(n)) = literal(ast) {
+ return Ok(Expr::Const(n));
+ }
+ if *op == UnaryOperator::Plus {
+ return compile_ast(expr, names, depth);
+ }
+ Ok(Expr::Arith {
+ op: Arith::Sub,
+ l: Box::new(Expr::Const(0.0)),
+ r: Box::new(compile_ast(expr, names, depth + 1)?),
+ })
+ }
+ SqlExpr::Cast {
+ kind,
+ expr,
+ data_type,
+ array,
+ format,
+ } if matches!(kind, CastKind::Cast | CastKind::DoubleColon)
+ && !array
+ && format.is_none()
+ && matches!(
+ data_type.to_string().as_str(),
+ "DOUBLE" | "DOUBLE PRECISION"
+ ) =>
+ {
+ Ok(Expr::Number(Box::new(compile_ast(expr, names, depth + 1)?)))
+ }
+ SqlExpr::Case {
+ operand: Some(operand),
+ conditions,
+ else_result,
+ ..
+ } => {
+ let arms = conditions
+ .iter()
+ .map(|arm| Ok((literal(&arm.condition)?, literal(&arm.result)?)))
+ .collect::<AppResult<_>>()?;
+ Ok(Expr::Case {
+ expr: Box::new(compile_ast(operand, names, depth + 1)?),
+ arms,
+ otherwise: else_result.as_ref().map(|v| literal(v)).transpose()?,
+ })
+ }
+ SqlExpr::Function(function)
+ if function.name.to_string().eq_ignore_ascii_case("date_trunc") =>
+ {
+ if function.uses_odbc_syntax
+ || !matches!(function.parameters, FunctionArguments::None)
+ || function.filter.is_some()
+ || function.null_treatment.is_some()
+ || function.over.is_some()
+ || !function.within_group.is_empty()
+ {
+ return Err(unsupported());
+ }
+ let FunctionArguments::List(args) = &function.args else {
+ return Err(unsupported());
+ };
+ if args.duplicate_treatment.is_some() || !args.clauses.is_empty() {
+ return Err(unsupported());
+ }
+ let [FunctionArg::Unnamed(FunctionArgExpr::Expr(part)), FunctionArg::Unnamed(FunctionArgExpr::Expr(expr))] =
+ args.args.as_slice()
+ else {
+ return Err(unsupported());
+ };
+ let Scalar::Text(part) = literal(part)? else {
+ return Err(unsupported());
+ };
+ let part = DatePart::parse(&part.to_ascii_lowercase()).ok_or_else(unsupported)?;
+ Ok(Expr::DateTrunc {
+ part,
+ expr: Box::new(compile_ast(expr, names, depth + 1)?),
+ })
+ }
+ _ => Err(unsupported()),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ fn names() -> HashMap<String, Uuid> {
+ ["amt_pay", "chnl", "dt_crt", "净额"]
+ .into_iter()
+ .enumerate()
+ .map(|(i, name)| (name.to_owned(), Uuid::from_u128(i as u128 + 1)))
+ .collect()
+ }
+
+ #[test]
+ fn the_wide_corpus_conversions_compile_to_ids_and_run() {
+ let names = names();
+ let inputs = HashMap::from([
+ (names["amt_pay"], Scalar::Text("12345".into())),
+ (names["chnl"], Scalar::Number(2.0)),
+ (
+ names["dt_crt"],
+ Scalar::Text("2024-03-01T00:30:00+08:00".into()),
+ ),
+ (names["净额"], Scalar::Number(250.0)),
+ ]);
+ for (text, expected) in [
+ ("CAST(amt_pay AS DOUBLE PRECISION) / 100", json!(123.45)),
+ (
+ "CASE chnl WHEN 1 THEN 'app' WHEN 2 THEN 'web' ELSE 'other' END",
+ json!("web"),
+ ),
+ ("date_trunc('month', dt_crt)", json!("2024-02-01")),
+ ("(\"净额\" - 50) / (10 - 2)", json!(25.0)),
+ ("-amt_pay / -1e2", json!(123.45)),
+ (
+ "CASE chnl WHEN 2 THEN 'customer''s app' END",
+ json!("customer's app"),
+ ),
+ ] {
+ let tree = compile(text, &names).unwrap_or_else(|e| panic!("{text}: {e}"));
+ let expr = Expr::from_json(&tree).unwrap();
+ assert_eq!(
+ expr.evaluate(&|p| inputs.get(&p).cloned())
+ .unwrap()
+ .to_json(),
+ expected,
+ "{text}"
+ );
+ assert_eq!(Expr::from_json(&expr.to_json()).unwrap(), expr);
+ }
+ assert!(compile("CASE chnl WHEN 3 THEN 'web' END", &names).is_ok());
+ }
+
+ #[test]
+ fn unsupported_sql_cannot_be_silently_shortened_into_a_conversion() {
+ for text in [
+ "unknown / 100",
+ "other.amt_pay",
+ "amt_pay; DELETE FROM jobs",
+ "amt_pay AS amount",
+ "amt_pay FROM jobs",
+ "(SELECT 1)",
+ "SUM(amt_pay)",
+ "amt_pay % 100",
+ "amt_pay > 0",
+ "CAST(amt_pay AS DECIMAL(10, 2))",
+ "CAST(amt_pay AS INTEGER)",
+ "TRY_CAST(amt_pay AS DOUBLE)",
+ "CASE WHEN chnl = 1 THEN 'app' END",
+ "CASE chnl WHEN 1 THEN amt_pay END",
+ "CASE chnl WHEN 1 THEN 'app' ELSE 0 END",
+ "CASE chnl WHEN NULL THEN 'app' END",
+ "date_trunc('hour', dt_crt)",
+ "date_trunc('month', dt_crt, 'UTC')",
+ "date_trunc(DISTINCT 'month', dt_crt)",
+ "date_trunc('month', dt_crt) OVER ()",
+ "date_trunc('month', dt_crt) FILTER (WHERE chnl = 1)",
+ "date_trunc('month', dt_crt ORDER BY chnl)",
+ "pg_catalog.date_trunc('month', dt_crt)",
+ "date_trunc(unit => 'month', value => dt_crt)",
+ "1e999",
+ "true",
+ "NULL",
+ "",
+ ":parameter",
+ ] {
+ assert!(compile(text, &names()).is_err(), "accepted {text}");
+ }
+ }
+
+ #[test]
+ fn text_and_json_share_the_existing_depth_bound() {
+ let mut text = "amt_pay".to_owned();
+ for _ in 0..MAX_EXPR_DEPTH {
+ text = format!("({text} + 1)");
+ }
+ assert!(compile(&text, &names()).is_ok());
+ assert!(compile(&format!("{text} + 1"), &names()).is_err());
+ assert!(compile(
+ &format!("{}amt_pay{}", "(".repeat(1000), ")".repeat(1000)),
+ &names()
+ )
+ .is_err());
+ assert!(compile(&" ".repeat(MAX_INPUT_BYTES + 1), &names()).is_err());
+ }
+}
diff --git a/crates/utopia-server/src/api/rule_expression_tests.rs b/crates/utopia-server/src/api/rule_expression_tests.rs
index efa511f7..7956e4a0 100644
--- a/crates/utopia-server/src/api/rule_expression_tests.rs
+++ b/crates/utopia-server/src/api/rule_expression_tests.rs
@@ -1,4 +1,4 @@
-//! Exercise expression thresholds through authenticated writes, then the real materializer.
+//! Exercise expression writes and conversions through authentication and the real materializer.
use crate::state::AppState;
use axum::{
body::Body,
@@ -132,7 +132,109 @@ async fn expression_operands_round_trip_and_execute_through_authenticated_routes
let c=json!([{"predicate_id":input,"op":"gt","operand":deep}]);
let (status,error)=call("PATCH",path.clone(),json!({"conditions":c})).await?;
anyhow::ensure!(status == StatusCode::UNPROCESSABLE_ENTITY && error["code"] == "expression_too_deep");
+
+ // The same authenticated writer accepts conversion text, but neither
+ // the live definition nor its history may retain executable text.
+ let fields: std::collections::HashMap<_,_> = ["amount", "channel", "recorded_on", "paid_amount", "channel_name", "recorded_month", "eligible"]
+ .into_iter().map(|key| (key,Uuid::now_v7())).collect();
+ for (key, datatype) in [("amount","text"),("channel","number"),("recorded_on","date"),("paid_amount","number"),("channel_name","text"),("recorded_month","date"),("eligible","text")] {
+ sqlx::query("INSERT INTO relation_types(id,kb_id,key,label,kind,datatype) VALUES($1,$2,$3,$3,'attribute',$4)")
+ .bind(fields[key]).bind(kb).bind(key).bind(datatype).execute(&pool).await?;
+ }
+ let mut conversion_facts = std::collections::HashMap::new();
+ for (key,value) in [("amount",json!("12550")),("channel",json!(1)),("recorded_on",json!("2024-03-01T00:30:00+08:00"))] {
+ let fact = Uuid::now_v7();
+ sqlx::query("INSERT INTO facts(id,kb_id,subject_id,predicate_id,object_value,valid_from,valid_from_precision,valid_to,valid_to_precision,confidence) VALUES($1,$2,$3,$4,$5,'2023-06-01','day','2024-06-01','day',0.9)")
+ .bind(fact).bind(kb).bind(entity).bind(fields[key]).bind(json!({"value":value})).execute(&pool).await?;
+ conversion_facts.insert(key,fact);
+ }
+ let mut conversion_rules = std::collections::HashMap::new();
+ for (target,text) in [
+ ("paid_amount","CAST(amount AS DOUBLE PRECISION) / 100"),
+ ("channel_name","CASE channel WHEN 1 THEN 'app' WHEN 2 THEN 'web' ELSE 'other' END"),
+ ("recorded_month","date_trunc('month', recorded_on)"),
+ ] {
+ let request = json!({"name":target,"subject_type_id":class,"conclusion":"computed","conclude_predicate_id":fields[target],
+ "conclude_expr":{"expression":text},"conditions":[{"predicate_id":input,"op":"present"}]});
+ let (status,body) = call("POST",base.clone(),request).await?;
+ anyhow::ensure!(status.is_success(), "{target}: {status} {body}");
+ conversion_rules.insert(target,body["id"].as_str().unwrap().parse::<Uuid>()?);
+ }
+ let downstream = json!({"name":"App customer","subject_type_id":class,"conclusion":"attribute","conclude_predicate_id":fields["eligible"],
+ "conclude_value":"yes","conditions":[{"predicate_id":fields["channel_name"],"op":"in","operand":["app"]}]});
+ anyhow::ensure!(call("POST",base.clone(),downstream).await?.0.is_success());
+ let (_,read) = call("GET",base.clone(),json!(null)).await?;
+ for id in conversion_rules.values() {
+ let row = read["rules"].as_array().unwrap().iter().find(|r|r["id"]==id.to_string()).unwrap();
+ anyhow::ensure!(row["conclude_expr"].is_object() && row["conclude_expr"].get("expression").is_none());
+ }
+ let targets = vec![fields["paid_amount"],fields["channel_name"],fields["recorded_month"],fields["eligible"]];
+ utopia_store::reasoning::materialize(&pool,kb).await?;
+ let results: Vec<(Uuid,Value,bool)> = sqlx::query_as("SELECT predicate_id,object_value->'value',valid_from='2023-06-01'::timestamptz AND valid_to='2024-06-01'::timestamptz FROM derived_facts WHERE kb_id=$1 AND subject_id=$2 AND predicate_id=ANY($3) AND invalidated_at IS NULL")
+ .bind(kb).bind(entity).bind(&targets).fetch_all(&pool).await?;
+ anyhow::ensure!(results.len()==4,"conversion results: {results:?}");
+ for (target,expected) in [("paid_amount",json!(125.5)),("channel_name",json!("app")),("recorded_month",json!("2024-02-01")),("eligible",json!("yes"))] {
+ let result = results.iter().find(|r|r.0==fields[target]).unwrap();
+ anyhow::ensure!(result.1==expected && result.2,"{target}: {result:?}");
+ }
+ let premises: Vec<Uuid> = sqlx::query_scalar("SELECT fd.premise_fact_id FROM fact_derivations fd JOIN derived_facts d ON d.id=fd.derived_fact_id WHERE d.kb_id=$1 AND d.predicate_id=$2 AND d.invalidated_at IS NULL AND fd.premise_fact_id IS NOT NULL")
+ .bind(kb).bind(fields["channel_name"]).fetch_all(&pool).await?;
+ anyhow::ensure!(premises.len()==2 && premises.contains(&facts[0]) && premises.contains(&conversion_facts["channel"]));
+
+ // A later definition retires the old label and its downstream conclusion.
+ let channel_path = format!("{base}/{}",conversion_rules["channel_name"]);
+ let patch = json!({"conclusion":"computed","conclude_predicate_id":fields["channel_name"],
+ "conclude_expr":{"expression":"CASE channel WHEN 1 THEN 'mobile' END"}});
+ anyhow::ensure!(call("PATCH",channel_path.clone(),patch).await?.0.is_success());
+ utopia_store::reasoning::materialize(&pool,kb).await?;
+ let labels: Vec<Value> = sqlx::query_scalar("SELECT object_value->'value' FROM derived_facts WHERE kb_id=$1 AND predicate_id=$2 AND invalidated_at IS NULL")
+ .bind(kb).bind(fields["channel_name"]).fetch_all(&pool).await?;
+ anyhow::ensure!(labels==vec![json!("mobile")]);
+ let retired: i64 = sqlx::query_scalar("SELECT count(*) FROM derived_facts WHERE kb_id=$1 AND predicate_id=ANY($2) AND invalidated_at IS NOT NULL")
+ .bind(kb).bind(vec![fields["channel_name"],fields["eligible"]]).fetch_one(&pool).await?;
+ anyhow::ensure!(retired==2);
+ let versions: Vec<Value> = sqlx::query_scalar("SELECT definition->'conclude_expr' FROM attribute_rule_versions WHERE rule_id=$1 ORDER BY seq")
+ .bind(conversion_rules["channel_name"]).fetch_all(&pool).await?;
+ anyhow::ensure!(versions.len()==2 && versions.iter().all(|v|v.get("expression").is_none()));
+ anyhow::ensure!(versions[0]["when"][0]["then"]=="app" && versions[1]["when"][0]["then"]=="mobile");
+
+ // Neither a missing name nor a foreign UUID can partly rename a rule.
+ let (_,before) = call("GET",base.clone(),json!(null)).await?;
+ for (bad,code) in [
+ (json!({"expression":"missing_amount / 100"}),"unknown_expression_attribute"),
+ (json!({"expression":"amount; DELETE FROM jobs"}),"bad_expression"),
+ (json!({"expression":"amount / 100","future":true}),"bad_expression"),
+ (json!({"case":{"attr":foreign},"when":[{"is":1,"then":"app"}]}),"unknown_predicate"),
+ (json!({"date_trunc":"month","expr":{"attr":relation}}),"not_an_attribute"),
+ ] {
+ let patch=json!({"name":"Must not commit","conclusion":"computed","conclude_predicate_id":fields["channel_name"],"conclude_expr":bad});
+ let (status,error)=call("PATCH",channel_path.clone(),patch).await?;
+ anyhow::ensure!(status==StatusCode::UNPROCESSABLE_ENTITY && error["code"]==code,"rejected conversion: {status} {error}");
+ anyhow::ensure!(call("GET",base.clone(),json!(null)).await?.1==before);
+ }
+ // Condition text uses the same compiler; scalar "5" above remains scalar.
+ let conditions=json!([{"predicate_id":input,"op":"gt","operand":{"expression":"CAST(amount AS DOUBLE PRECISION) / 100"}}]);
+ anyhow::ensure!(call("PATCH",path.clone(),json!({"conditions":conditions})).await?.0.is_success());
+ let stored: Value = sqlx::query_scalar("SELECT operand FROM attribute_rule_conditions WHERE rule_id=$1")
+ .bind(id.parse::<Uuid>()?).fetch_one(&pool).await?;
+ anyhow::ensure!(stored["l"]["cast"]=="number" && stored.get("expression").is_none());
+
+ // Invalid observations and an unmatched code remove the old results;
+ // no zero, default date or stale downstream label survives recomputation.
+ for (key,value) in [("amount",json!("not a number")),("channel",json!(9)),("recorded_on",json!("2024-02-30"))] {
+ sqlx::query("UPDATE facts SET object_value=$2 WHERE id=$1").bind(conversion_facts[key])
+ .bind(json!({"value":value})).execute(&pool).await?;
+ }
+ utopia_store::reasoning::materialize(&pool,kb).await?;
+ let active: i64=sqlx::query_scalar("SELECT count(*) FROM derived_facts WHERE kb_id=$1 AND predicate_id=ANY($2) AND invalidated_at IS NULL")
+ .bind(kb).bind(&targets).fetch_one(&pool).await?;
+ anyhow::ensure!(active==0);
+ let foreign_request=json!({"name":"Foreign","subject_type_id":class,"conclusion":"computed","conclude_predicate_id":fields["paid_amount"],
+ "conclude_expr":{"expression":"CAST(amount AS DOUBLE PRECISION) / 100"},"conditions":[{"predicate_id":input,"op":"present"}]});
+ // An inaccessible base stays indistinguishable from a missing one.
+ anyhow::ensure!(call("POST",format!("/api/v1/kbs/{foreign_kb}/rules"),foreign_request.clone()).await?.0==StatusCode::NOT_FOUND);
sqlx::query("UPDATE kb_members SET role='viewer' WHERE user_id=$1").bind(user).execute(&pool).await?;
+ anyhow::ensure!(call("POST",base.clone(),foreign_request).await?.0==StatusCode::FORBIDDEN);
anyhow::ensure!(call("POST",base.clone(),definition).await?.0 == StatusCode::FORBIDDEN);
anyhow::Ok(())
}.await;
diff --git a/crates/utopia-server/src/api/rule_routes.rs b/crates/utopia-server/src/api/rule_routes.rs
index 2cfb7aa9..9503642b 100644
--- a/crates/utopia-server/src/api/rule_routes.rs
+++ b/crates/utopia-server/src/api/rule_routes.rs
@@ -78,9 +78,16 @@ pub async fn create(
State(state): State<AppState>,
AuthUser(user): AuthUser,
Path(kb_id): Path<Uuid>,
- Json(req): Json<RuleReq>,
+ Json(mut req): Json<RuleReq>,
) -> ApiResult<Json<serde_json::Value>> {
require_kb(&state, &user, kb_id, Role::Editor).await?;
+ super::rule_expression_input::compile_inputs(
+ &state.pool,
+ kb_id,
+ req.conclude_expr.as_mut(),
+ &mut req.conditions,
+ )
+ .await?;
let id = utopia_store::business_rules::create(
&state.pool,
kb_id,
@@ -113,9 +120,20 @@ pub async fn update(
State(state): State<AppState>,
AuthUser(user): AuthUser,
Path((kb_id, rule_id)): Path<(Uuid, Uuid)>,
- Json(req): Json<RulePatch>,
+ Json(mut req): Json<RulePatch>,
) -> ApiResult<Json<serde_json::Value>> {
require_kb(&state, &user, kb_id, Role::Editor).await?;
+ super::rule_expression_input::compile_inputs(
+ &state.pool,
+ kb_id,
+ if req.conclusion.is_some() {
+ req.conclude_expr.as_mut()
+ } else {
+ None
+ },
+ req.conditions.as_deref_mut().unwrap_or_default(),
+ )
+ .await?;
let conclusion = req.conclusion.as_ref().map(|kind| ConclusionInput {
kind: kind.clone(),
type_id: req.conclude_type_id,
diff --git a/crates/utopia-store/src/business_rules.rs b/crates/utopia-store/src/business_rules.rs
index 160af072..9e09eed5 100644
--- a/crates/utopia-store/src/business_rules.rs
+++ b/crates/utopia-store/src/business_rules.rs
@@ -594,10 +594,7 @@ pub async fn describe_expressions(
kb_id: Uuid,
expressions: &[&serde_json::Value],
) -> AppResult<Vec<Option<String>>> {
- let reads: Vec<_> = expressions
- .iter()
- .map(|e| validate_expr(e, 0).ok())
- .collect();
+ let reads: Vec<_> = expressions.iter().map(|e| validate_expr(e).ok()).collect();
let mut ids: Vec<Uuid> = reads.iter().flatten().flatten().copied().collect();
ids.sort();
ids.dedup();
@@ -851,7 +848,7 @@ async fn validate_conclusion(pool: &PgPool, kb_id: Uuid, c: &ConclusionInput) ->
// **这几条挡在入口而不是留给求值器。** 读不懂的树在求值时只会
// 「这条规则什么都不推」,而写它的人看不见任何理由(`not_in`
// 那次就是这么丢的,见 #494)
- let reads = validate_expr(expr, 0)?;
+ let reads = validate_expr(expr)?;
if reads.is_empty() {
return Err(AppError::invalid(
"constant_expression",
@@ -887,40 +884,11 @@ async fn validate_conclusion(pool: &PgPool, kb_id: Uuid, c: &ConclusionInput) ->
/// **形状不对就说清哪里不对。** 库里的 CHECK 只管「computed 得有一棵树」,
/// 树自己长得对不对得在这里判——否则一棵写坏的树要等到下一次物化才表现为
/// 「这条规则不推东西」,而那时候没有任何地方说得出为什么。
-fn validate_expr(raw: &serde_json::Value, depth: usize) -> AppResult<Vec<Uuid>> {
- use utopia_reason::rules::{Arith, MAX_EXPR_DEPTH};
- if depth > MAX_EXPR_DEPTH {
- return Err(AppError::invalid(
- "expression_too_deep",
- "That expression nests too deeply; a rule computes with a few operations, not a program.",
- ));
- }
- let bad = || {
- AppError::invalid(
- "bad_expression",
- "An expression is an attribute, a number, or two of those combined with + − × ÷.",
- )
- };
- let obj = raw.as_object().ok_or_else(bad)?;
- if let Some(a) = obj.get("attr") {
- let id: Uuid = a.as_str().ok_or_else(bad)?.parse().map_err(|_| bad())?;
- return Ok(vec![id]);
- }
- if let Some(c) = obj.get("const") {
- let n = c
- .as_f64()
- .or_else(|| c.as_str()?.trim().parse().ok())
- .ok_or_else(bad)?;
- if !n.is_finite() {
- return Err(bad());
- }
- return Ok(Vec::new());
- }
- Arith::parse(obj.get("op").and_then(|v| v.as_str()).ok_or_else(bad)?).ok_or_else(bad)?;
- let mut reads = validate_expr(obj.get("l").ok_or_else(bad)?, depth + 1)?;
- reads.extend(validate_expr(obj.get("r").ok_or_else(bad)?, depth + 1)?);
- reads.sort();
- reads.dedup();
+fn validate_expr(raw: &serde_json::Value) -> AppResult<Vec<Uuid>> {
+ let expr = utopia_reason::rules::Expr::from_json(raw)
+ .map_err(|e| AppError::invalid(e.code(), e.message()))?;
+ let mut reads = Vec::new();
+ expr.predicates(&mut reads);
Ok(reads)
}
@@ -962,7 +930,7 @@ async fn validate_conditions(
| utopia_reason::rules::Op::Lte
) {
if let Some(expr) = c.operand.as_ref().filter(|v| v.is_object()) {
- for predicate in validate_expr(expr, 0)? {
+ for predicate in validate_expr(expr)? {
attribute_predicate(pool, kb_id, predicate).await?;
}
continue;
diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rs
index 6ec9b141..de49b18d 100644
--- a/crates/utopia-store/src/reasoning.rs
+++ b/crates/utopia-store/src/reasoning.rs
@@ -1694,7 +1694,7 @@ async fn attribute_rules(conn: &mut PgConnection, kb_id: Uuid) -> AppResult<Load
let (Some(p), Some(e)) = (conclude_pred, conclude_expr) else {
continue;
};
- let Some(expr) = parse_expr(&e, 0) else {
+ let Ok(expr) = utopia_reason::rules::Expr::from_json(&e) else {
continue;
};
(Conclusion::Computed { predicate: p, expr }, p)
@@ -1746,34 +1746,6 @@ async fn attribute_rules(conn: &mut PgConnection, kb_id: Uuid) -> AppResult<Load
})
}
-/// 算式的 JSON 形状 → 树(0032)。
-///
-/// `{"attr": "<uuid>"} | {"const": 12.5} | {"op": "sub", "l": {…}, "r": {…}}`
-///
-/// **认不出来返回 None**,调用方整条规则跳过——一棵读不懂的算式算不出数,
-/// 而算不出数的规则不该带着半棵树去求值。深度也在这里拦:太深的树是
-/// 「有人在这里写程序」的信号(0032)。
-fn parse_expr(raw: &serde_json::Value, depth: usize) -> Option<utopia_reason::rules::Expr> {
- use utopia_reason::rules::{Arith, Expr, MAX_EXPR_DEPTH};
- if depth > MAX_EXPR_DEPTH {
- return None;
- }
- let obj = raw.as_object()?;
- if let Some(a) = obj.get("attr") {
- return Some(Expr::Attr(a.as_str()?.parse().ok()?));
- }
- if let Some(c) = obj.get("const") {
- let n = c.as_f64().or_else(|| c.as_str()?.trim().parse().ok())?;
- return n.is_finite().then_some(Expr::Const(n));
- }
- let op = Arith::parse(obj.get("op")?.as_str()?)?;
- Some(Expr::Arith {
- op,
- l: Box::new(parse_expr(obj.get("l")?, depth + 1)?),
- r: Box::new(parse_expr(obj.get("r")?, depth + 1)?),
- })
-}
-
/// 操作数按 op 解析。形状不对返回 None,调用方整条规则跳过。
fn parse_operand(
op: utopia_reason::rules::Op,
@@ -1807,7 +1779,9 @@ fn parse_operand(
_ => {
let raw = raw?;
if raw.is_object() {
- return parse_expr(raw, 0).map(Operand::Calc);
+ return utopia_reason::rules::Expr::from_json(raw)
+ .ok()
+ .map(Operand::Calc);
}
Some(Operand::Num(raw.as_f64().or_else(|| {
raw.as_str().and_then(|s| s.trim().parse().ok())
@@ -2339,14 +2313,11 @@ async fn resolve(conn: &mut PgConnection, kb_id: Uuid) -> AppResult<Resolved> {
utopia_reason::rules::Conclusion::Attribute { value, .. } => {
(serde_json::json!({ "value": value }), value.clone())
}
- // 算出来的结论:值在命中里,**每个组合各一个**(0032)。
- // 求值器算不出数的组合根本不会产出命中,所以这里不会没有值
+ // The evaluator has already rejected failed conversions.
+ // Preserve its scalar type when this result feeds another rule.
utopia_reason::rules::Conclusion::Computed { .. } => {
- let Some(n) = h.value else { continue };
- let Some(v) = serde_json::Number::from_f64(n) else {
- continue;
- };
- let v = serde_json::Value::Number(v);
+ let Some(ref result) = h.value else { continue };
+ let v = result.to_json();
(serde_json::json!({ "value": v }), v)
}
// The relation arm above stores an entity edge and returns
diff --git a/docs/decisions/0036-exploration-aligns-a-schema-to-the-ontology.md b/docs/decisions/0036-exploration-aligns-a-schema-to-the-ontology.md
index 07df2f23..d5cb6a63 100644
--- a/docs/decisions/0036-exploration-aligns-a-schema-to-the-ontology.md
+++ b/docs/decisions/0036-exploration-aligns-a-schema-to-the-ontology.md
@@ -1,6 +1,6 @@
# 0036 · Exploration aligns a schema to the ontology
-- **Status**: In progress · decision 7 built (#561, #563) · open: #554, #555, #556
+- **Status**: In progress · conversion expressions (decision 3) and decision 7 built · open: #554, #556
- **Written**: 2026-09-09 (conventions in the [README](README.md))
- **Related**: [0011](0011-a-mapping-is-not-a-fact.md) moved a mapping out of the ledger and
is right that it is configuration; this record moves the *concept* out of the entity table.
@@ -148,6 +148,51 @@ The natural-language column stays, as explanation: `summary` is what a reviewer
what the chat prompt quotes. The tree is the definition, the sentence is the description,
and neither replaces the other.
+#### Conversion expressions (#555)
+
+The first conversion nodes are deliberately small: a cast to the existing finite
+floating-point number type, a simple `CASE` mapping scalar codes to scalar values,
+and truncation to a UTC calendar year, month or day. `CASE chnl WHEN 1 THEN 'app'
+ELSE 'other' END` translates a representation and belongs on the alignment. A
+named business grouping still belongs in a rule; using the same expression there
+does not make exploration its author. Case arms are literal numbers or text, not
+predicates or subqueries. This keeps each conversion's input readings and temporal
+premises explicit. A missing input, an unmatched case without `ELSE`, an invalid
+date or a failed numeric conversion produces no value, never a substitute zero.
+
+Numeric constants retain `const`, including the previously accepted numeric
+strings. Text literals use `text`, so admitting labels does not turn a misspelled
+numeric constant into a valid number. The additional shapes are:
+
+```json
+{"cast":"number","expr":{"attr":"<attribute UUID>"}}
+{"case":{"attr":"<attribute UUID>"},"when":[{"is":1,"then":"app"},{"is":2,"then":"web"}],"else":"other"}
+{"date_trunc":"month","expr":{"attr":"<attribute UUID>"}}
+```
+
+Case results have one scalar type, and at most 32 arms. The existing four-edge
+`MAX_EXPR_DEPTH` bound applies to every expression. Dates accept an ISO calendar
+date or an RFC 3339 timestamp with an offset; timestamp offsets are converted to
+UTC before truncation. The result is an ISO calendar date, such as `2024-02-01`.
+Naive timestamps, arbitrary cast types, decimal precision/scale, searched cases,
+aggregates and other SQL functions are outside this cut.
+
+Text is an input format only. The rule API can accept an expression as
+`{"expression":"CAST(amount AS DOUBLE PRECISION) / 100"}` wherever it accepts
+an expression tree. It resolves attribute **keys** in the current base and stores
+the resulting UUID tree; unresolved names and unsupported syntax fail before the
+rule is written. Ordinary scalar threshold strings keep their existing meaning.
+This gives the conversion tree an executable consumer before table alignments
+exist. Reading, validating and evaluating JSON share the same node definitions;
+rule results and their existing premise/interval machinery can carry text and
+calendar dates as well as numbers. A bare attribute expression retains its
+existing numeric interpretation. The read-only rule display follows the tree;
+the experimental picker is not enabled by this change.
+
+Table/column binding and adoption remain #554. Rendering the tree into each
+source dialect and retiring the old mapping classes remain #556. This cut does
+not reinterpret existing SQL mappings or decide the unit policy proposed in 0049.
+
### 4. A definition is a rule over aligned attributes, and a person writes it
GMV is `sum(Order.paid_amount) where Order.is_valid`. It is not an entity and not a row in
@@ -205,9 +250,8 @@ the one decision here that is a bug fix and can land on its own.
- **Matching a column to an existing attribute.** By name? By sampled values (#502)? By
asking the model to say, with the attribute list in the prompt? The wide corpus, where
`amt_pay` must land on `Order.paid_amount` and `buyer_lvl` on `Customer.tier`, is the test.
-- **What the tree needs beyond four operators**, and whether `case` over a code column is
- a conversion (alignment) or a dimension (a rule that names groups). The wide corpus has
- both readings of `chnl`.
+- **What another corpus needs beyond the conversion subset above.** A code-to-label
+ case is a conversion; a named business grouping is a rule that may reuse it.
- **The page.** How a person reviews a table's alignment, edits one column's expression,
and sees which rules a change reaches. Capability first, interface after, as usual.
- **`derived`** on `concept_mappings` (0011 §1) was for "conversion rate = orders / visits".
diff --git a/docs/design/rules.md b/docs/design/rules.md
index 146e11d4..987cd47f 100644
--- a/docs/design/rules.md
+++ b/docs/design/rules.md
@@ -34,8 +34,41 @@ attribute value, or a computed expression) and conditions `(attribute, op, opera
`> >= < <= in not_in present`, grouped: conditions in a group join with and, groups with or, one
level [0021, 0029, 0032]. A concluded typing is a derived attribute fact and never writes
`entities.type_id` [0021 d2]. An operand or a conclusion may be an expression tree over attributes
-(`attr | const | add sub mul div`, depth capped), stored as JSONB, reachable through the API, the
-picker not built; a missing reading or a division by zero concludes nothing and is reported [0032].
+(`attr | const | text | add sub mul div`, numeric cast, scalar case and date truncation), stored as
+JSONB and reachable through the API. The depth cap is four edges from the root. A missing reading,
+division by zero or failed conversion concludes nothing [0032, 0036]; the experimental picker
+does not enable semantic edits of conversion expressions.
+
+**A conversion has a stored tree.** `{"cast":"number","expr":node}` converts to the existing
+finite floating-point number type. `{"case":node,"when":[{"is":1,"then":"app"}],"else":"other"}`
+maps codes to values; its one to 32 arms are scalar literals, its results all have the same type,
+and an unmatched code without `else` has no value. A missing selector has no value even with
+`else`. Case compares text and numeric codes separately; use an explicit cast to interpret a
+text code as a number. `{"date_trunc":"month","expr":node}` returns an ISO calendar date;
+`year`, `month` and `day` are supported. An ISO date is read as a calendar date; an RFC 3339
+timestamp is converted to UTC first, so `2024-03-01T00:30:00+08:00` truncates to `2024-02-01`.
+Naive timestamps and invalid dates have no value. These results use the same premise intersection,
+history, retirement and chaining as numeric computations. Numeric arithmetic remains floating
+point, not fixed-scale decimal accounting, and this does not introduce a unit-conversion policy.
+
+**Text is parsed before a write.** On `POST /api/v1/kbs/{id}/rules` and the corresponding rule
+`PATCH`, `conclude_expr` or a comparison condition's `operand` may instead contain
+`{"expression":"CAST(amount AS DOUBLE PRECISION) / 100"}`. Attribute **keys** resolve exactly
+within that base; quoted keys support spaces or non-Latin text. Only the resulting UUID tree is
+stored, including in definition versions. Ordinary numeric threshold strings remain scalar
+thresholds. Input is limited to 8192 bytes and the same tree depth bound. Supported text includes
+arithmetic, `CAST(... AS DOUBLE PRECISION)` (also `DOUBLE` or `::`), simple literal
+`CASE ... WHEN ... THEN ... [ELSE ...] END`, and `date_trunc('year'|'month'|'day', ...)`.
+Mixed or unknown JSON fields, unknown names, qualified paths, arbitrary functions, aggregates, subqueries, additional statements,
+cast precision/scale and function modifiers are refused. A rename or metadata-only patch keeps the
+existing definition. The rule page renders conversions read-only and includes their input
+attributes in the dependency view.
+
+The conversion regressions exercise these writes through authenticated routes and the real
+materializer: text and date results, chained conclusions, premise intervals, version changes,
+failed conversions and cross-base/viewer rejection. Table adoption and SQL rendering per source
+dialect remain [#554](https://github.com/deeplethe/utopia/issues/554) and
+[#556](https://github.com/deeplethe/utopia/issues/556); existing SQL mappings are unchanged.
**Rules chain inside one run.** A round's conclusions rejoin the pool as readings and as derived
class membership (entering as a premise so the interval narrows to when the entity was that class);
@@ -71,8 +104,8 @@ read it, so an invalidated conclusion still says what the rule said when it was
open-world [0029].
- **A fixed point, not an ordering**, or whether `A → B → C` fires depends on load order; the DDL's
objection protects "a run reads only asserted facts", which the in-memory fixed point keeps [0030].
-- **Picked, not typed**, because a picker cannot name an attribute that does not exist and a text
- box grows a grammar; storing a string evaluated at run time is what is forbidden [0032].
+- **One stored expression tree.** A picker or the bounded text compiler resolves attribute IDs
+ before saving; storing a string evaluated at run time is forbidden [0032, 0036].
- **Aggregation is refused** because a sum asserts a completeness the base cannot hold, its proof
cannot explain its retirement, and it forecloses incremental maintenance [0032].
- **Repairs, not rulings**: a contradiction is stale knowledge, a misread, a wrong merge or an
diff --git a/web/src/pages/ExpressionDraftLab.tsx b/web/src/pages/ExpressionDraftLab.tsx
index 425863da..76e619b1 100644
--- a/web/src/pages/ExpressionDraftLab.tsx
+++ b/web/src/pages/ExpressionDraftLab.tsx
@@ -41,8 +41,9 @@ function Lab({ kbId }: { kbId: string }) {
<SearchSelect value={source} options={candidates} placeholder={S.expressionDraft.choose} onChange={(id) => {
const raw = candidates.find((c) => c.value === id)?.raw;
const expr = readExpression(raw);
- setSource(id); setUnsupported(!expr);
- if (expr) setDraft(draftFromExpression(expr));
+ const nextDraft = expr ? draftFromExpression(expr) : null;
+ setSource(id); setUnsupported(!nextDraft);
+ if (nextDraft) setDraft(nextDraft);
}} />
</Field>
{unsupported ? <p role="alert" className="text-small text-warn">{S.expressionDraft.unsupported}</p> : <>
diff --git a/web/src/pages/expressionDraft.test.ts b/web/src/pages/expressionDraft.test.ts
index 129d99b6..b8ebae75 100644
--- a/web/src/pages/expressionDraft.test.ts
+++ b/web/src/pages/expressionDraft.test.ts
@@ -12,7 +12,7 @@ describe("expression draft fidelity", () => {
const expr = { op, l: { attr: a }, r: { op, l: { attr: b }, r: { attr: c } } };
const draft = draftFromExpression(expr);
expect(previewExpression(draft, ids)).toEqual(expr);
- if (!("op" in draft) || !("op" in draft.r)) throw new Error("lost tree");
+ if (!draft || !("op" in draft) || !("op" in draft.r)) throw new Error("lost tree");
const changed = { ...draft, r: { ...draft.r, r: { const: "2.5" } } };
expect(previewExpression(changed, ids)).toEqual({ ...expr, r: { ...expr.r, r: { const: "2.5" } } });
expect(draft).toEqual(expr);
@@ -33,4 +33,9 @@ describe("expression draft fidelity", () => {
expect(previewExpression({ op: "add", l: draft, r: { attr: a } }, ids)).toBeNull();
expect(readExpression({ attr: a, future: true })).toBeNull();
});
+ it("leaves conversion expressions out of the arithmetic-only draft editor", () => {
+ const conversion = { cast: "number" as const, expr: { attr: a } };
+ expect(draftFromExpression(conversion)).toBeNull();
+ expect(draftFromExpression({ op: "div", l: conversion, r: { const: 100 } })).toBeNull();
+ });
});
diff --git a/web/src/pages/expressionDraft.ts b/web/src/pages/expressionDraft.ts
index f6a77c4d..b203e3ca 100644
--- a/web/src/pages/expressionDraft.ts
+++ b/web/src/pages/expressionDraft.ts
@@ -6,17 +6,20 @@ export type ExpressionDraft =
| { const: string }
| { op: "add" | "sub" | "mul" | "div"; l: ExpressionDraft; r: ExpressionDraft };
-export function draftFromExpression(expr: RuleExpression): ExpressionDraft {
+export function draftFromExpression(expr: RuleExpression): ExpressionDraft | null {
if ("const" in expr) return { const: String(expr.const) };
if ("attr" in expr) return { attr: expr.attr };
- return { op: expr.op, l: draftFromExpression(expr.l), r: draftFromExpression(expr.r) };
+ // The experimental arithmetic picker must not flatten a conversion it cannot edit.
+ if (!("op" in expr)) return null;
+ const l = draftFromExpression(expr.l), r = draftFromExpression(expr.r);
+ return l && r ? { op: expr.op, l, r } : null;
}
/** Structural preview only. Declaration policy and server write validation are separate. */
-export function previewExpression(draft: ExpressionDraft, attributeIds: ReadonlySet<string>): RuleExpression | null {
+export function previewExpression(draft: ExpressionDraft | null, attributeIds: ReadonlySet<string>): RuleExpression | null {
const parsed = readExpression(draft);
if (!parsed) return null;
const referencesExist = (node: RuleExpression): boolean =>
- "attr" in node ? attributeIds.has(node.attr) : "const" in node || (referencesExist(node.l) && referencesExist(node.r));
+ "attr" in node ? attributeIds.has(node.attr) : "const" in node || ("op" in node && referencesExist(node.l) && referencesExist(node.r));
return referencesExist(parsed) ? parsed : null;
}
diff --git a/web/src/pages/ruleDependencies.test.ts b/web/src/pages/ruleDependencies.test.ts
index c103f47b..7db13460 100644
--- a/web/src/pages/ruleDependencies.test.ts
+++ b/web/src/pages/ruleDependencies.test.ts
@@ -29,6 +29,17 @@ describe("potential definition dependencies", () => {
expect([...links.get("computed")!.producers]).toEqual(["p", "q"]);
expect([...links.get("operand")!.producers]).toEqual(["p", "q"]);
});
+ it("follows conversion inputs without treating case labels as attribute references", () => {
+ const expr = { op: "div", l: { cast: "number", expr: { attr: "amount" } }, r: { case: { attr: "unit" }, when: [{ is: "cents", then: "label-only" }] } };
+ const converted = r("converted", { conclusion: "computed", conclude_expr: expr });
+ const dated = r("dated", { conclusion: "computed", conclude_expr: { date_trunc: "month", expr: { attr: "date" } } });
+ const producers = ["amount", "unit", "date", "label-only"].map((p) => r(p, { conclude_predicate_id: p }));
+ const { links, incomplete } = ruleDependencies([...producers, converted, dated], classes);
+ expect([...links.get("converted")!.producers]).toEqual(["amount", "unit"]);
+ expect([...links.get("dated")!.producers]).toEqual(["date"]);
+ expect(links.get("label-only")!.consumers.size).toBe(0);
+ expect(incomplete).toBe(false);
+ });
it("matches concluded subclasses to ancestor scopes, not the reverse", () => {
const { links } = ruleDependencies([r("child-output", { conclusion: "typing", conclude_type_id: "child" }), r("root-output", { conclusion: "typing", conclude_type_id: "root" }), r("child-scope", { subject_type_id: "child" }), r("leaf-scope", { subject_type_id: "leaf" })], classes);
expect(links.get("child-output")!.consumers.has("child-scope")).toBe(true);
diff --git a/web/src/pages/ruleDependencies.ts b/web/src/pages/ruleDependencies.ts
index b163e4bd..5ab31306 100644
--- a/web/src/pages/ruleDependencies.ts
+++ b/web/src/pages/ruleDependencies.ts
@@ -21,6 +21,14 @@ export function ruleDependencies(rules: Definition[], classes: Pick<EntityTypeVi
const node = raw as Record<string, unknown>;
if (typeof node.attr === "string") { index(readers, node.attr, id); return; }
if (typeof node.const === "number" || typeof node.const === "string") return;
+ if (typeof node.text === "string") return;
+ if (node.cast === "number" || ["year", "month", "day"].includes(String(node.date_trunc))) {
+ readExpr(node.expr, id, depth + 1); return;
+ }
+ if ("case" in node && Array.isArray(node.when)) {
+ // Case arms are literals. Only the selector can read another rule's result.
+ readExpr(node.case, id, depth + 1); return;
+ }
if (["add", "sub", "mul", "div"].includes(String(node.op))) {
readExpr(node.l, id, depth + 1); readExpr(node.r, id, depth + 1);
} else incomplete = true;
diff --git a/web/src/pages/ruleExpressions.test.ts b/web/src/pages/ruleExpressions.test.ts
index 2d6cef8b..0a741e83 100644
--- a/web/src/pages/ruleExpressions.test.ts
+++ b/web/src/pages/ruleExpressions.test.ts
@@ -34,6 +34,28 @@ describe("reading existing rule expressions", () => {
expect(readExpression(tree)).not.toBeNull();
expect(readExpression({ op: "add", l: tree, r: { const: 1 } })).toBeNull();
});
+ it("renders conversions and quoted labels without losing their structure", () => {
+ const cast = { op: "div", l: { cast: "number", expr: { attr: a } }, r: { const: 100 } };
+ const choice = { case: { attr: b }, when: [{ is: 1, then: "用户's app" }, { is: 2, then: "web" }], else: "other" };
+ const date = { date_trunc: "month", expr: { attr: a } };
+ for (const raw of [cast, choice, date, { text: "NaN is a label" }]) expect(readExpression(raw)).toEqual(raw);
+ expect(expressionText(cast, attrs, "unknown")).toBe("(CAST(收入 AS DOUBLE PRECISION) ÷ 100)");
+ expect(expressionText(choice, attrs, "unknown")).toBe("CASE 成本 WHEN 1 THEN '用户''s app' WHEN 2 THEN 'web' ELSE 'other' END");
+ expect(expressionText(date, attrs.map((attr) => ({ ...attr, label: attr.key })), "unknown")).toBe("DATE_TRUNC('month', revenue)");
+ });
+ it("keeps malformed and future conversion shapes unknown", () => {
+ for (const raw of [
+ { cast: "integer", expr: { attr: a } }, { cast: "number", expr: { attr: a }, future: true },
+ { date_trunc: "hour", expr: { attr: a } }, { case: { attr: a }, when: [] },
+ { case: { attr: a }, when: [{ is: 1, then: "app" }], else: 0 },
+ { case: { attr: a }, when: [{ is: 1, then: { attr: b } }] },
+ { case: { attr: a }, when: Array.from({ length: 33 }, (_, i) => ({ is: i, then: "app" })) },
+ ]) expect(readExpression(raw)).toBeNull();
+ let deep: unknown = { attr: a };
+ for (let i = 0; i < 4; i++) deep = { cast: "number", expr: deep };
+ expect(readExpression(deep)).not.toBeNull();
+ expect(readExpression({ date_trunc: "month", expr: deep })).toBeNull();
+ });
it("guards definitions the constant form cannot round-trip", () => {
expect(metadataOnly(rule)).toBe(true);
expect(metadataOnly({ ...rule, conclusion: "typing", conditions: [{ predicate_id: a, op: "gt", operand: expr }] })).toBe(true);
diff --git a/web/src/pages/ruleExpressions.ts b/web/src/pages/ruleExpressions.ts
index 96f60606..762f8eb8 100644
--- a/web/src/pages/ruleExpressions.ts
+++ b/web/src/pages/ruleExpressions.ts
@@ -3,7 +3,14 @@ import type { BusinessRule, RelationTypeView } from "../api";
export type RuleExpression =
| { attr: string }
| { const: number | string }
- | { op: "add" | "sub" | "mul" | "div"; l: RuleExpression; r: RuleExpression };
+ | { text: string }
+ | { op: "add" | "sub" | "mul" | "div"; l: RuleExpression; r: RuleExpression }
+ | { cast: "number"; expr: RuleExpression }
+ | { case: RuleExpression; when: { is: number | string; then: number | string }[]; else?: number | string }
+ | { date_trunc: "year" | "month" | "day"; expr: RuleExpression };
+
+const scalar = (value: unknown): value is number | string =>
+ typeof value === "string" || (typeof value === "number" && Number.isFinite(value));
// Match the store's root-at-zero validation bound. Unknown nodes stay unknown;
// a reader must never replace them with a constant or simplify their structure.
@@ -19,6 +26,31 @@ export function readExpression(raw: unknown, depth = 0): RuleExpression | null {
/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/.test(String(node.const).trim()) && Number.isFinite(Number(node.const))) {
return { const: node.const };
}
+ if (Object.keys(node).length === 1 && typeof node.text === "string") return { text: node.text };
+ if (Object.keys(node).length === 2 && node.cast === "number") {
+ const expr = readExpression(node.expr, depth + 1);
+ return expr ? { cast: "number", expr } : null;
+ }
+ if (Object.keys(node).length === 2 && ["year", "month", "day"].includes(String(node.date_trunc))) {
+ const expr = readExpression(node.expr, depth + 1);
+ return expr ? { date_trunc: node.date_trunc as "year" | "month" | "day", expr } : null;
+ }
+ if ("case" in node) {
+ const hasElse = Object.hasOwn(node, "else");
+ if (Object.keys(node).length !== 2 + Number(hasElse) || !Array.isArray(node.when) ||
+ !node.when.length || node.when.length > 32 || (hasElse && !scalar(node.else))) return null;
+ const expr = readExpression(node.case, depth + 1);
+ if (!expr) return null;
+ const arms: { is: number | string; then: number | string }[] = [];
+ for (const raw of node.when) {
+ if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).length !== 2 ||
+ !scalar(raw.is) || !scalar(raw.then)) return null;
+ arms.push({ is: raw.is, then: raw.then });
+ }
+ if (arms.some((a) => typeof a.then !== typeof arms[0].then) ||
+ (hasElse && typeof node.else !== typeof arms[0].then)) return null;
+ return { case: expr, when: arms, ...(hasElse ? { else: node.else as number | string } : {}) };
+ }
if (Object.keys(node).length !== 3 || !["add", "sub", "mul", "div"].includes(String(node.op))) return null;
const l = readExpression(node.l, depth + 1), r = readExpression(node.r, depth + 1);
return l && r ? { op: node.op as "add" | "sub" | "mul" | "div", l, r } : null;
@@ -30,12 +62,19 @@ export function expressionText(raw: unknown, attributes: Pick<RelationTypeView,
const labels = new Map(attributes.map((a) => [a.id, a]));
const counts = new Map<string, number>();
for (const a of attributes) counts.set(a.label, (counts.get(a.label) ?? 0) + 1);
+ // SQL keywords are expression notation, not UI copy. Quoting keeps a literal
+ // label distinguishable from an attribute or an operator in either language.
+ const literal = (value: number | string) => typeof value === "number" ? String(value) : `'${value.replaceAll("'", "''")}'`;
const render = (node: RuleExpression): string => {
if ("attr" in node) {
const attr = labels.get(node.attr);
return attr ? (counts.get(attr.label)! > 1 ? `${attr.label} (${attr.key})` : attr.label) : node.attr;
}
if ("const" in node) return String(node.const);
+ if ("text" in node) return literal(node.text);
+ if ("cast" in node) return `CAST(${render(node.expr)} AS DOUBLE PRECISION)`;
+ if ("date_trunc" in node) return `DATE_TRUNC('${node.date_trunc}', ${render(node.expr)})`;
+ if ("case" in node) return `CASE ${render(node.case)} ${node.when.map((arm) => `WHEN ${literal(arm.is)} THEN ${literal(arm.then)}`).join(" ")}${node.else === undefined ? "" : ` ELSE ${literal(node.else)}`} END`;
return `(${render(node.l)} ${{ add: "+", sub: "−", mul: "×", div: "÷" }[node.op]} ${render(node.r)})`;
};
return render(expr);