|
| 1 | +package dev.cel.optimizer.optimizers; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | + |
| 6 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 7 | +import dev.cel.bundle.Cel; |
| 8 | +import dev.cel.bundle.CelFactory; |
| 9 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 10 | +import dev.cel.common.CelOptions; |
| 11 | +import dev.cel.common.CelSource; |
| 12 | +import dev.cel.common.ast.CelExpr; |
| 13 | +import dev.cel.common.types.SimpleType; |
| 14 | +import dev.cel.extensions.CelExtensions; |
| 15 | +import dev.cel.optimizer.CelOptimizationException; |
| 16 | +import dev.cel.optimizer.CelOptimizer; |
| 17 | +import dev.cel.optimizer.CelOptimizerFactory; |
| 18 | +import dev.cel.optimizer.optimizers.InliningOptimizer.InlineVariable; |
| 19 | +import dev.cel.optimizer.optimizers.InliningOptimizer.InliningOptions; |
| 20 | +import dev.cel.parser.CelStandardMacro; |
| 21 | +import dev.cel.parser.CelUnparserFactory; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +@RunWith(TestParameterInjector.class) |
| 26 | +public class InliningOptimizerTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void inlineConstant() throws Exception { |
| 30 | + Cel cel = CelFactory.standardCelBuilder().addVar("ident_to_inline", SimpleType.INT).build(); |
| 31 | + String expression = "ident_to_inline + 2 + ident_to_inline"; |
| 32 | + CelAbstractSyntaxTree astToInline = cel.compile(expression).getAst(); |
| 33 | + CelOptimizer optimizer = CelOptimizerFactory.standardCelOptimizerBuilder(cel) |
| 34 | + .addAstOptimizers(InliningOptimizer.newInstance( |
| 35 | + InlineVariable.of("ident_to_inline", cel.compile("1").getAst()) |
| 36 | + )) |
| 37 | + .build(); |
| 38 | + |
| 39 | + CelAbstractSyntaxTree optimized = optimizer.optimize(astToInline); |
| 40 | + |
| 41 | + String unparsed = CelUnparserFactory.newUnparser().unparse(optimized); |
| 42 | + assertThat(unparsed).isEqualTo("1 + 2 + 1"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void inline_doesNotInlineIterVar() throws Exception { |
| 47 | + Cel cel = CelFactory.standardCelBuilder() |
| 48 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 49 | + .setOptions(CelOptions.current().populateMacroCalls(true).build()) |
| 50 | + .addVar("shadowed_ident", SimpleType.INT) |
| 51 | + .build(); |
| 52 | + String expression = "[0].exists(shadowed_ident, shadowed_ident == 0)"; |
| 53 | + CelAbstractSyntaxTree astToInline = cel.compile(expression).getAst(); |
| 54 | + CelOptimizer optimizer = CelOptimizerFactory.standardCelOptimizerBuilder(cel) |
| 55 | + .addAstOptimizers(InliningOptimizer.newInstance( |
| 56 | + InlineVariable.of("shadowed_ident", cel.compile("1").getAst()) |
| 57 | + )) |
| 58 | + .build(); |
| 59 | + |
| 60 | + CelAbstractSyntaxTree optimized = optimizer.optimize(astToInline); |
| 61 | + |
| 62 | + String unparsed = CelUnparserFactory.newUnparser().unparse(optimized); |
| 63 | + assertThat(unparsed).isEqualTo("[0].exists(shadowed_ident, shadowed_ident == 0)"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void inline_bindMacro_doesNotInlineVarName() throws Exception { |
| 68 | + Cel cel = CelFactory.standardCelBuilder() |
| 69 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 70 | + .addCompilerLibraries(CelExtensions.bindings()) |
| 71 | + .setOptions(CelOptions.current().populateMacroCalls(true).build()) |
| 72 | + .addVar("shadowed_ident", SimpleType.INT) |
| 73 | + .build(); |
| 74 | + String expression = "cel.bind(shadowed_ident, 2, shadowed_ident + 1)"; |
| 75 | + CelAbstractSyntaxTree astToInline = cel.compile(expression).getAst(); |
| 76 | + CelOptimizer optimizer = CelOptimizerFactory.standardCelOptimizerBuilder(cel) |
| 77 | + .addAstOptimizers(InliningOptimizer.newInstance( |
| 78 | + InlineVariable.of("shadowed_ident", cel.compile("1").getAst()) |
| 79 | + )) |
| 80 | + .build(); |
| 81 | + |
| 82 | + CelAbstractSyntaxTree optimized = optimizer.optimize(astToInline); |
| 83 | + |
| 84 | + String unparsed = CelUnparserFactory.newUnparser().unparse(optimized); |
| 85 | + assertThat(unparsed).isEqualTo("cel.bind(shadowed_ident, 2, shadowed_ident + 1)"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void inline_exceededIterationLimit_throws() throws Exception { |
| 90 | + Cel cel = CelFactory.standardCelBuilder() |
| 91 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 92 | + .addCompilerLibraries(CelExtensions.bindings()) |
| 93 | + .addVar("ident_to_replace", SimpleType.INT) |
| 94 | + .build(); |
| 95 | + String expression = "ident_to_replace + ident_to_replace + ident_to_replace"; |
| 96 | + CelAbstractSyntaxTree astToInline = cel.compile(expression).getAst(); |
| 97 | + CelOptimizer optimizer = CelOptimizerFactory.standardCelOptimizerBuilder(cel) |
| 98 | + .addAstOptimizers(InliningOptimizer.newInstance( |
| 99 | + InliningOptions.newBuilder().maxIterationLimit(2).build(), |
| 100 | + InlineVariable.of("ident_to_replace", cel.compile("1").getAst()) |
| 101 | + )) |
| 102 | + .build(); |
| 103 | + |
| 104 | + CelOptimizationException e = assertThrows(CelOptimizationException.class, () -> optimizer.optimize(astToInline)); |
| 105 | + assertThat(e).hasMessageThat().contains("Max iteration count reached."); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void inlineVariableDecl_internalVar_throws() throws Exception { |
| 110 | + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> |
| 111 | + InlineVariable.of("@internal_var", CelAbstractSyntaxTree.newParsedAst(CelExpr.ofNotSet(0L), CelSource.newBuilder().build()))); |
| 112 | + assertThat(e).hasMessageThat().contains("Internal variables cannot be inlined: @internal_var" ); |
| 113 | + } |
| 114 | +} |
0 commit comments