be/src/exprs/lambda_function/lambda_execution_context.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <set> |
23 | | #include <string> |
24 | | #include <utility> |
25 | | #include <vector> |
26 | | |
27 | | namespace doris { |
28 | | |
29 | | class LambdaExecutionContext { |
30 | | public: |
31 | | struct Binding { |
32 | | std::string name; |
33 | | int column_position = -1; |
34 | | }; |
35 | | |
36 | | struct Frame { |
37 | | bool bind_by_name = true; |
38 | | bool parent_bindings_visible = true; |
39 | | std::vector<Binding> argument_bindings; |
40 | | }; |
41 | | |
42 | | struct ResolveResult { |
43 | | bool searched_named_scope = false; |
44 | | bool found = false; |
45 | | int column_position = -1; |
46 | | }; |
47 | | |
48 | | class FrameGuard { |
49 | | public: |
50 | 27 | FrameGuard(LambdaExecutionContext& context, Frame frame) : _context(&context) { |
51 | 27 | _context->push_frame(std::move(frame)); |
52 | 27 | } |
53 | | |
54 | | FrameGuard(FrameGuard&& other) = delete; |
55 | | FrameGuard& operator=(FrameGuard&& other) = delete; |
56 | | FrameGuard(const FrameGuard&) = delete; |
57 | | FrameGuard& operator=(const FrameGuard&) = delete; |
58 | | |
59 | 27 | ~FrameGuard() { release(); } |
60 | | |
61 | | private: |
62 | 27 | void release() { |
63 | 27 | if (_context != nullptr) { |
64 | 27 | _context->pop_frame(); |
65 | 27 | _context = nullptr; |
66 | 27 | } |
67 | 27 | } |
68 | | |
69 | | LambdaExecutionContext* _context; |
70 | | }; |
71 | | |
72 | 27 | void push_frame(Frame frame) { _frames.push_back(std::move(frame)); } |
73 | | |
74 | 27 | void pop_frame() { |
75 | 27 | DCHECK(!_frames.empty()); |
76 | 27 | _frames.pop_back(); |
77 | 27 | } |
78 | | |
79 | 455 | ResolveResult resolve_column_position(const std::string& name) const { |
80 | 455 | ResolveResult result; |
81 | 463 | for (auto frame_it = _frames.rbegin(); frame_it != _frames.rend(); ++frame_it) { |
82 | 459 | const auto& frame = *frame_it; |
83 | 459 | result.searched_named_scope |= frame.bind_by_name; |
84 | 459 | for (auto binding_it = frame.argument_bindings.rbegin(); |
85 | 467 | binding_it != frame.argument_bindings.rend(); ++binding_it) { |
86 | 455 | const auto& argument_binding = *binding_it; |
87 | 455 | if (argument_binding.name == name) { |
88 | 447 | result.found = true; |
89 | 447 | result.column_position = argument_binding.column_position; |
90 | 447 | return result; |
91 | 447 | } |
92 | 455 | } |
93 | 12 | if (!frame.parent_bindings_visible) { |
94 | 4 | break; |
95 | 4 | } |
96 | 12 | } |
97 | 8 | return result; |
98 | 455 | } |
99 | | |
100 | 26 | void collect_visible_binding_column_positions(std::set<int>& column_positions) const { |
101 | 34 | for (auto frame_it = _frames.rbegin(); frame_it != _frames.rend(); ++frame_it) { |
102 | 9 | for (const auto& binding : frame_it->argument_bindings) { |
103 | 9 | if (binding.column_position >= 0) { |
104 | 9 | column_positions.insert(binding.column_position); |
105 | 9 | } |
106 | 9 | } |
107 | 8 | if (!frame_it->parent_bindings_visible) { |
108 | 0 | break; |
109 | 0 | } |
110 | 8 | } |
111 | 26 | } |
112 | | |
113 | | private: |
114 | | std::vector<Frame> _frames; |
115 | | }; |
116 | | |
117 | | } // namespace doris |