Coverage Report

Created: 2026-05-27 07:00

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
40
// Enable select optimization on the given RuntimeBuilder, replacing long
41
// select chains with a single operation.
42
//
43
// This assumes that the type information at check time agrees with the
44
// configured types at runtime.
45
//
46
// Important: The select optimization follows spec behavior for traversals.
47
//  - `enable_empty_wrapper_null_unboxing` is ignored and optimized traversals
48
//    always operates as though it is `true`.
49
//  - `enable_heterogeneous_equality` is ignored and optimized traversals
50
//    always operate as though it is `true`.
51
//
52
// This should only be called *once* on a given runtime builder.
53
//
54
// Assumes the default runtime implementation, an error with code
55
// InvalidArgument is returned if it is not.
56
//
57
// Note: implementation does not support optional field traversal, and will
58
// instead revert to the normal implementation instead of trying to optimize.
59
absl::Status EnableSelectOptimization(
60
    cel::RuntimeBuilder& builder,
61
    const SelectOptimizationOptions& options = {});
62
63
// ===============================================================
64
// Implementation details -- CEL users should not depend on these.
65
// Exposed here for enabling on Legacy APIs. They expose internal details
66
// which are not guaranteed to be stable.
67
// ===============================================================
68
69
// Scans ast for optimizable select branches.
70
//
71
// In general, this should be done by a type checker but may be deferred to
72
// runtime.
73
//
74
// This assumes the runtime type registry has the same definitions as the one
75
// used by the type checker.
76
class SelectOptimizationAstUpdater
77
    : public google::api::expr::runtime::AstTransform {
78
 public:
79
0
  SelectOptimizationAstUpdater() = default;
80
81
  absl::Status UpdateAst(google::api::expr::runtime::PlannerContext& context,
82
                         cel::Ast& ast) const override;
83
};
84
85
google::api::expr::runtime::ProgramOptimizerFactory
86
CreateSelectOptimizationProgramOptimizer(
87
    const SelectOptimizationOptions& options = {});
88
89
}  // namespace cel::extensions
90
#endif  // THIRD_PARTY_CEL_CPP_EXTENSIONS_SELECT_OPTIMIZATION_H_