Coverage Report

Created: 2026-11-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/extensions/select_optimization.h
Line
Count
Source
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_SELECT_OPTIMIZATION_H_
16
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_SELECT_OPTIMIZATION_H_
17
18
#include "absl/status/status.h"
19
#include "common/ast.h"
20
#include "eval/compiler/flat_expr_builder_extensions.h"
21
#include "runtime/runtime_builder.h"
22
23
namespace cel::extensions {
24
25
constexpr char kCelAttribute[] = "cel.@attribute";
26
constexpr char kCelHasField[] = "cel.@hasField";
27
28
// Configuration options for the select optimization.
29
struct SelectOptimizationOptions {
30
  // Force the program to use the fallback implementation for the select.
31
  // This implementation simply collapses the select operation into one program
32
  // step and calls the normal field accessors on the Struct value.
33
  //
34
  // Normally, the fallback implementation is used when the Qualify operation is
35
  // unimplemented for a given StructType. This option is exposed for testing or
36
  // to more closely match behavior of unoptimized expressions.
37
  bool force_fallback_implementation = false;
38
39
  // The minimum depth of a select chain to be considered for optimization.
40
  // For example, if this is set to 2, then `a.b.c` will be optimized, but
41
  // `a.b` will not be.
42
  //
43
  // This is applied in the pre-pass phase that rewrites the AST. If the
44
  // optimization is already applied by a compiler / static optimizer, this
45
  // option has no effect.
46
  int min_field_depth_for_optimization = 0;
47
48
  // Whether to try to cache field descriptor lookups for the select
49
  // optimization at plan time.
50
  //
51
  // This is temporary to make the change rollback safe for existing users.
52
  bool enable_field_descriptor_caching = true;
53
};
54
55
// Enable select optimization on the given RuntimeBuilder, replacing long
56
// select chains with a single operation. Also enables planning ASTs tagged with
57
// the `select_optimization` runtime extension.
58
//
59
// This assumes that the type information at check time agrees with the
60
// configured types at runtime.
61
//
62
// Important: The select optimization follows spec behavior for traversals.
63
//  - `enable_empty_wrapper_null_unboxing` is ignored and optimized traversals
64
//    always operates as though it is `true`.
65
//  - `enable_heterogeneous_equality` is ignored and optimized traversals
66
//    always operate as though it is `true`.
67
//
68
// This should only be called *once* on a given runtime builder.
69
//
70
// Assumes the default runtime implementation, an error with code
71
// InvalidArgument is returned if it is not.
72
//
73
// Note: implementation does not support optional field traversal, and will
74
// instead revert to the normal implementation instead of trying to optimize.
75
absl::Status EnableSelectOptimization(
76
    cel::RuntimeBuilder& builder,
77
    const SelectOptimizationOptions& options = {});
78
79
// ===============================================================
80
// Implementation details -- CEL users should not depend on these.
81
// Exposed here for enabling on Legacy APIs. They expose internal details
82
// which are not guaranteed to be stable.
83
// ===============================================================
84
85
// Scans ast for optimizable select branches.
86
//
87
// In general, this should be done by a type checker but may be deferred to
88
// runtime.
89
//
90
// This assumes the runtime type registry has the same definitions as the one
91
// used by the type checker.
92
class SelectOptimizationAstUpdater
93
    : public google::api::expr::runtime::AstTransform {
94
 public:
95
  explicit SelectOptimizationAstUpdater(
96
      const SelectOptimizationOptions& options = {})
97
0
      : options_(options) {}
98
99
  absl::Status UpdateAst(google::api::expr::runtime::PlannerContext& context,
100
                         cel::Ast& ast) const override;
101
102
 private:
103
  SelectOptimizationOptions options_;
104
};
105
106
google::api::expr::runtime::ProgramOptimizerFactory
107
CreateSelectOptimizationProgramOptimizer(
108
    const SelectOptimizationOptions& options = {});
109
110
}  // namespace cel::extensions
111
#endif  // THIRD_PARTY_CEL_CPP_EXTENSIONS_SELECT_OPTIMIZATION_H_