diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-09-29 13:18:05 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-09-29 13:18:05 +0200 |
commit | 067c45bdff39f88b7c7ce586c03f217532272ba5 (patch) | |
tree | 920c87daca3cd8e981e0a3cc40c1a8c0fd60cca2 /config-model/src/main | |
parent | 62928f4d8b7571c4b10fedffc56b762f57b6b2ca (diff) |
Only optimize boolean expressions on primitives
Only transform a && b to if(a, b, false) etc.0
if both a and b are primitives, not tensors,
as if requires both branches to return the same type.
Diffstat (limited to 'config-model/src/main')
-rw-r--r-- | config-model/src/main/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformer.java | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/config-model/src/main/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformer.java b/config-model/src/main/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformer.java index ad050d4ca63..25cd7fe7bd2 100644 --- a/config-model/src/main/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformer.java +++ b/config-model/src/main/java/com/yahoo/schema/expressiontransforms/BooleanExpressionTransformer.java @@ -36,12 +36,12 @@ public class BooleanExpressionTransformer extends ExpressionTransformer<Transfor node = transformChildren(composite, context); if (node instanceof OperationNode arithmetic) - node = transformBooleanArithmetics(arithmetic); + node = transformBooleanArithmetics(arithmetic, context); return node; } - private ExpressionNode transformBooleanArithmetics(OperationNode node) { + private ExpressionNode transformBooleanArithmetics(OperationNode node, TransformContext context) { Iterator<ExpressionNode> child = node.children().iterator(); // Transform in precedence order: @@ -51,24 +51,25 @@ public class BooleanExpressionTransformer extends ExpressionTransformer<Transfor Operator op = it.next(); if ( ! stack.isEmpty()) { while (stack.size() > 1 && ! op.hasPrecedenceOver(stack.peek().op)) { - popStack(stack); + popStack(stack, context); } } stack.push(new ChildNode(op, child.next())); } while (stack.size() > 1) - popStack(stack); + popStack(stack, context); return stack.getFirst().child; } - private void popStack(Deque<ChildNode> stack) { + private void popStack(Deque<ChildNode> stack, TransformContext context) { ChildNode rhs = stack.pop(); ChildNode lhs = stack.peek(); + boolean primitives = isPrimitive(lhs.child, context) && isPrimitive(rhs.child, context); ExpressionNode combination; - if (rhs.op == Operator.and) + if (primitives && rhs.op == Operator.and) combination = andByIfNode(lhs.child, rhs.child); - else if (rhs.op == Operator.or) + else if (primitives && rhs.op == Operator.or) combination = orByIfNode(lhs.child, rhs.child); else { combination = resolve(lhs, rhs); @@ -77,6 +78,10 @@ public class BooleanExpressionTransformer extends ExpressionTransformer<Transfor lhs.child = combination; } + private boolean isPrimitive(ExpressionNode node, TransformContext context) { + return node.type(context.types()).rank() == 0; + } + private static OperationNode resolve(ChildNode left, ChildNode right) { if (! (left.child instanceof OperationNode) && ! (right.child instanceof OperationNode)) return new OperationNode(left.child, right.op, right.child); |