Skip to content

Commit

Permalink
feat: added support for case-when and if-else in composed metrics (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrmlvv authored Dec 16, 2024
1 parent 5cb41e6 commit cfd6f07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ trait FormulaParser extends JavaTokenParsers {
case class BoolBinaryFunc(t1: Tree, t2: Tree, f: (Boolean, Boolean) => Boolean) extends Tree
case class BoolCompareFunc(t1: Tree, t2: Tree, f: (Double, Double) => Boolean) extends Tree
case class Bool(t: Boolean) extends Tree
case class CaseWhen(cases: Seq[(Tree, Tree)], elseBranch: Tree) extends Tree
case class IfElse(condition: Tree, ifBranch: Tree, elseBranch: Tree) extends Tree

/**
* API to parse and evaluate arithmetic expression.
Expand Down Expand Up @@ -61,6 +63,10 @@ trait FormulaParser extends JavaTokenParsers {
case UnaryFunc(t1, f) => f(eval(t1))
case BinaryFunc(t1, t2, f) => f(eval(t1), eval(t2))
case Num(t) => t
case CaseWhen(cases, elseBranch) =>
cases.collectFirst { case (cond, branch) if boolEval(cond) => eval(branch) }.getOrElse(eval(elseBranch))
case IfElse(cond, ifBranch, elseBranch) =>
if (boolEval(cond)) eval(ifBranch) else eval(elseBranch)
case other => throw new MatchError(s"Illegal operation for arithmetic expression: ${other.getClass.getSimpleName}")
}

Expand Down Expand Up @@ -161,7 +167,7 @@ trait FormulaParser extends JavaTokenParsers {

private lazy val bool: Parser[Bool] = "true" ^^ (_ => Bool(true)) | "false" ^^ (_ => Bool(false))

private lazy val expr: Parser[Tree] = opt("[+-]".r) ~ term ~ rep("[+-]".r ~ term) ^^ {
private lazy val expr: Parser[Tree] = caseWhenExpr | ifElseExpr | opt("[+-]".r) ~ term ~ rep("[+-]".r ~ term) ^^ {
case s ~ t ~ ts =>
val signed = s match {
case None => t
Expand Down Expand Up @@ -208,4 +214,20 @@ trait FormulaParser extends JavaTokenParsers {
}

private lazy val num: Parser[Num] = floatingPointNumber ^^ (t => Num(t.toDouble))

private lazy val whenExpr: Parser[(Tree, Tree)] =
"(?i)when".r ~> boolExpr ~ ("(?i)then".r ~> expr) ^^ {
case condExpr ~ thenExpr => (condExpr, thenExpr)
}

private lazy val caseWhenExpr: Parser[Tree] =
"(?i)case".r ~> rep1(whenExpr) ~ opt("(?i)else".r ~> expr) <~ "(?i)end".r ^^ {
case conditions ~ Some(elseExpr) => CaseWhen(conditions, elseExpr)
case conditions ~ None => CaseWhen(conditions, Num(0.0))
}

private lazy val ifElseExpr: Parser[Tree] =
"(?i)if".r ~> boolExpr ~ expr ~ ("(?i)else".r ~> expr) ^^ {
case condition ~ thanBranch ~ elseBranch => IfElse(condition, thanBranch, elseBranch)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ class FormulaParserSpec extends AnyWordSpec with Matchers with FormulaParser {
"(1 < 0) || (3.14 * (2.77 - 1.31) ^ 0.5 > 3.5) && not(-3.14 * ln(max((2.77 - 1.31) ^ 0.5, - 5.55)) + 0.99 < 0)" -> true
).foreach{ case (f, r) => checkBoolean(f, r) }
}
"correctly parse case-when expressions" in {
Seq(
"CASE WHEN 1 > 0 THEN 777.7 ELSE 0 END" -> 777.7,
"case when 1 < 0 then 777.7 when 2 > 1 then 17 else 0 end" -> 17.0,
"case when 1 < 0 THEN 777.7 else 0 END" -> 0.0
).foreach { case (f, r) => checkArithmetic(f, r) }
}

"correctly parse if-else expressions" in {
Seq(
"IF 1 > 0 777.7 ELSE 0" -> 777.7,
"if 1 < 0 777.7 else 0" -> 0.0,
"if (3.14 > 2.0) (2.71 * 2) ELSE (1.41 + 0.5)" -> (2.71 * 2)
).foreach { case (f, r) => checkArithmetic(f, r) }
}
"throw error when arithmetic expression is invalid" in {
Seq(
"sin(3.14)",
Expand All @@ -43,5 +58,17 @@ class FormulaParserSpec extends AnyWordSpec with Matchers with FormulaParser {
"(1.41 > 0) == (-3.14 < 0)"
).foreach(f => an [RuntimeException] should be thrownBy evalBoolean(f))
}
"throw error when case-when expression is invalid" in {
Seq(
"case when true then 3.14 else true end"
).foreach(f => an[RuntimeException] should be thrownBy evalArithmetic(f))
}

"throw error when if-else expression is invalid" in {
Seq(
"IF 1 3.14 ELSE 0",
"if true 3.14 else true"
).foreach(f => an[RuntimeException] should be thrownBy evalArithmetic(f))
}
}
}

0 comments on commit cfd6f07

Please sign in to comment.