|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.optimizer.rules.logical.preoptimizer; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.support.CountDownActionListener; |
| 12 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 13 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 14 | +import org.elasticsearch.xpack.esql.expression.function.inference.InferenceFunction; |
| 15 | +import org.elasticsearch.xpack.esql.inference.InferenceRunner; |
| 16 | +import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * Pre-optimizer rule that evaluates inference functions (like TEXT_EMBEDDING) into constant values. |
| 25 | + * <p> |
| 26 | + * This rule identifies foldable inference functions in the logical plan, executes them using the |
| 27 | + * inference runner, and replaces them with their computed results. This enables downstream |
| 28 | + * optimizations to work with the actual embedding values rather than the function calls. |
| 29 | + * <p> |
| 30 | + * The rule processes inference functions recursively, handling newly revealed functions that might |
| 31 | + * appear after the first round of folding. |
| 32 | + */ |
| 33 | +public class InferenceFunctionConstantFolding implements PreOptimizerRule { |
| 34 | + private final InferenceRunner inferenceRunner; |
| 35 | + private final FoldContext foldContext; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new instance of the InferenceFunctionConstantFolding rule. |
| 39 | + * |
| 40 | + * @param inferenceRunner the inference runner to use for evaluating inference functions |
| 41 | + * @param foldContext the fold context to use for evaluating inference functions |
| 42 | + */ |
| 43 | + public InferenceFunctionConstantFolding(InferenceRunner inferenceRunner, FoldContext foldContext) { |
| 44 | + this.inferenceRunner = inferenceRunner; |
| 45 | + this.foldContext = foldContext; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Applies the InferenceFunctionConstantFolding rule to the given logical plan. |
| 50 | + * |
| 51 | + * @param plan the logical plan to apply the rule to |
| 52 | + * @param listener the listener to notify when the rule has been applied |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public void apply(LogicalPlan plan, ActionListener<LogicalPlan> listener) { |
| 56 | + foldInferenceFunctions(plan, listener); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Recursively folds inference functions in the logical plan. |
| 61 | + * <p> |
| 62 | + * This method collects all foldable inference functions, evaluates them in parallel, |
| 63 | + * and then replaces them with their results. If new inference functions are revealed |
| 64 | + * after the first round of folding, it recursively processes them as well. |
| 65 | + * |
| 66 | + * @param plan the logical plan to fold inference functions in |
| 67 | + * @param listener the listener to notify when the folding is complete |
| 68 | + */ |
| 69 | + private void foldInferenceFunctions(LogicalPlan plan, ActionListener<LogicalPlan> listener) { |
| 70 | + // First let's collect all the inference foldable inference functions |
| 71 | + List<InferenceFunction<?>> inferenceFunctions = collectFoldableInferenceFunctions(plan); |
| 72 | + |
| 73 | + if (inferenceFunctions.isEmpty()) { |
| 74 | + // No inference functions that can be evaluated at this time found. Return the original plan. |
| 75 | + listener.onResponse(plan); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // This is a map of inference functions to their results. |
| 80 | + // We will use this map to replace the inference functions in the plan. |
| 81 | + Map<InferenceFunction<?>, Expression> inferenceFunctionsToResults = new HashMap<>(); |
| 82 | + |
| 83 | + // Prepare a listener that will be called when all inference functions are done. |
| 84 | + // This listener will replace the inference functions in the plan with their results and then recursively fold the remaining |
| 85 | + // inference functions. |
| 86 | + CountDownActionListener completionListener = new CountDownActionListener( |
| 87 | + inferenceFunctions.size(), |
| 88 | + listener.delegateFailureIgnoreResponseAndWrap(l -> { |
| 89 | + // Replace the inference functions in the plan with their results |
| 90 | + LogicalPlan next = plan.transformExpressionsUp( |
| 91 | + InferenceFunction.class, |
| 92 | + f -> inferenceFunctionsToResults.getOrDefault(f, f) |
| 93 | + ); |
| 94 | + |
| 95 | + // Recursively fold the remaining inference functions |
| 96 | + foldInferenceFunctions(next, l); |
| 97 | + }) |
| 98 | + ); |
| 99 | + |
| 100 | + // Try to compute the result for each inference function. |
| 101 | + for (InferenceFunction<?> inferenceFunction : inferenceFunctions) { |
| 102 | + foldInferenceFunction(inferenceFunction, completionListener.map(e -> { |
| 103 | + inferenceFunctionsToResults.put(inferenceFunction, e); |
| 104 | + return null; |
| 105 | + })); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Collects all foldable inference functions from the logical plan. |
| 111 | + * <p> |
| 112 | + * A function is considered foldable if: |
| 113 | + * 1. It's an instance of InferenceFunction |
| 114 | + * 2. It's marked as foldable (all parameters are constants) |
| 115 | + * 3. It doesn't contain nested inference functions |
| 116 | + * |
| 117 | + * @param plan the logical plan to collect inference functions from |
| 118 | + * @return a list of foldable inference functions |
| 119 | + */ |
| 120 | + private List<InferenceFunction<?>> collectFoldableInferenceFunctions(LogicalPlan plan) { |
| 121 | + List<InferenceFunction<?>> inferenceFunctions = new ArrayList<>(); |
| 122 | + |
| 123 | + plan.forEachExpressionUp(InferenceFunction.class, f -> { |
| 124 | + if (f.foldable() && f.hasNestedInferenceFunction() == false) { |
| 125 | + inferenceFunctions.add(f); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + return inferenceFunctions; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Evaluates a single inference function asynchronously. |
| 134 | + * <p> |
| 135 | + * Uses the inference function's evaluator factory to create an evaluator |
| 136 | + * that can process the function with the given inference runner. |
| 137 | + * |
| 138 | + * @param inferenceFunction the inference function to evaluate |
| 139 | + * @param listener the listener to notify when the evaluation is complete |
| 140 | + */ |
| 141 | + private void foldInferenceFunction(InferenceFunction<?> inferenceFunction, ActionListener<Expression> listener) { |
| 142 | + inferenceFunction.inferenceEvaluatorFactory().get(inferenceRunner).eval(foldContext, listener); |
| 143 | + } |
| 144 | +} |
0 commit comments