summaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/arrow_next/cpp/src/arrow/compute/registry.cc
blob: 08ca05d2f714b6f4a32d52e48e1dd5d71d046b17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/registry.h"

#include <algorithm>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <utility>

#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/function.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/function_internal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/compute/registry_internal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/status.h"
#include "contrib/libs/apache/arrow_next/src/arrow/util/config.h"  // For ARROW_COMPUTE
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/logging.h"

namespace arrow20 {
namespace compute {

class FunctionRegistry::FunctionRegistryImpl {
 public:
  explicit FunctionRegistryImpl(FunctionRegistryImpl* parent = NULLPTR)
      : parent_(parent) {}
  ~FunctionRegistryImpl() {}

  Status CanAddFunction(std::shared_ptr<Function> function, bool allow_overwrite) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunction(function, allow_overwrite));
    }
    return DoAddFunction(function, allow_overwrite, /*add=*/false);
  }

  Status AddFunction(std::shared_ptr<Function> function, bool allow_overwrite) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunction(function, allow_overwrite));
    }
    return DoAddFunction(function, allow_overwrite, /*add=*/true);
  }

  Status CanAddAlias(const std::string& target_name, const std::string& source_name) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunctionName(target_name,
                                                /*allow_overwrite=*/false));
    }
    return DoAddAlias(target_name, source_name, /*add=*/false);
  }

  Status AddAlias(const std::string& target_name, const std::string& source_name) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunctionName(target_name,
                                                /*allow_overwrite=*/false));
    }
    return DoAddAlias(target_name, source_name, /*add=*/true);
  }

  Status CanAddFunctionOptionsType(const FunctionOptionsType* options_type,
                                   bool allow_overwrite = false) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunctionOptionsType(options_type, allow_overwrite));
    }
    return DoAddFunctionOptionsType(options_type, allow_overwrite, /*add=*/false);
  }

  Status AddFunctionOptionsType(const FunctionOptionsType* options_type,
                                bool allow_overwrite = false) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunctionOptionsType(options_type, allow_overwrite));
    }
    return DoAddFunctionOptionsType(options_type, allow_overwrite, /*add=*/true);
  }

  Result<std::shared_ptr<Function>> GetFunction(const std::string& name) const {
    auto it = name_to_function_.find(name);
    if (it == name_to_function_.end()) {
      if (parent_ != NULLPTR) {
        return parent_->GetFunction(name);
      }
      return Status::KeyError("No function registered with name: ", name);
    }
    return it->second;
  }

  std::vector<std::string> GetFunctionNames() const {
    std::vector<std::string> results;
    if (parent_ != NULLPTR) {
      results = parent_->GetFunctionNames();
    }
    for (auto it : name_to_function_) {
      results.push_back(it.first);
    }
    std::sort(results.begin(), results.end());
    return results;
  }

  Result<const FunctionOptionsType*> GetFunctionOptionsType(
      const std::string& name) const {
    auto it = name_to_options_type_.find(name);
    if (it == name_to_options_type_.end()) {
      if (parent_ != NULLPTR) {
        return parent_->GetFunctionOptionsType(name);
      }
      return Status::KeyError("No function options type registered with name: ", name);
    }
    return it->second;
  }

  int num_functions() const {
    return (parent_ == NULLPTR ? 0 : parent_->num_functions()) +
           static_cast<int>(name_to_function_.size());
  }

  const Function* cast_function() { return cast_function_; }

 private:
  // must not acquire mutex
  Status CanAddFunctionName(const std::string& name, bool allow_overwrite) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddFunctionName(name, allow_overwrite));
    }
    if (!allow_overwrite) {
      auto it = name_to_function_.find(name);
      if (it != name_to_function_.end()) {
        return Status::KeyError("Already have a function registered with name: ", name);
      }
    }
    return Status::OK();
  }

  // must not acquire mutex
  Status CanAddOptionsTypeName(const std::string& name, bool allow_overwrite) {
    if (parent_ != NULLPTR) {
      RETURN_NOT_OK(parent_->CanAddOptionsTypeName(name, allow_overwrite));
    }
    if (!allow_overwrite) {
      auto it = name_to_options_type_.find(name);
      if (it != name_to_options_type_.end()) {
        return Status::KeyError(
            "Already have a function options type registered with name: ", name);
      }
    }
    return Status::OK();
  }

  Status DoAddFunction(std::shared_ptr<Function> function, bool allow_overwrite,
                       bool add) {
#ifndef NDEBUG
    // This validates docstrings extensively, so don't waste time on it
    // in release builds.
    RETURN_NOT_OK(function->Validate());
#endif

    std::lock_guard<std::mutex> mutation_guard(lock_);

    const std::string& name = function->name();
    RETURN_NOT_OK(CanAddFunctionName(name, allow_overwrite));
    if (add) {
      name_to_function_[name] = std::move(function);
      if (name == "cast") {
        cast_function_ = name_to_function_[name].get();
      }
    }
    return Status::OK();
  }

  Status DoAddAlias(const std::string& target_name, const std::string& source_name,
                    bool add) {
    // source name must exist in this registry or the parent
    // check outside mutex, in case GetFunction leads to mutex acquisition
    ARROW_ASSIGN_OR_RAISE(auto func, GetFunction(source_name));

    std::lock_guard<std::mutex> mutation_guard(lock_);

    // target name must be available in this registry and the parent
    RETURN_NOT_OK(CanAddFunctionName(target_name, /*allow_overwrite=*/false));
    if (add) {
      name_to_function_[target_name] = func;
    }
    return Status::OK();
  }

  Status DoAddFunctionOptionsType(const FunctionOptionsType* options_type,
                                  bool allow_overwrite, bool add) {
    std::lock_guard<std::mutex> mutation_guard(lock_);

    const std::string name = options_type->type_name();
    RETURN_NOT_OK(CanAddOptionsTypeName(name, /*allow_overwrite=*/false));
    if (add) {
      name_to_options_type_[options_type->type_name()] = options_type;
    }
    return Status::OK();
  }

  FunctionRegistryImpl* parent_;
  std::mutex lock_;
  std::unordered_map<std::string, std::shared_ptr<Function>> name_to_function_;
  std::unordered_map<std::string, const FunctionOptionsType*> name_to_options_type_;

  const Function* cast_function_;
};

std::unique_ptr<FunctionRegistry> FunctionRegistry::Make() {
  return std::unique_ptr<FunctionRegistry>(new FunctionRegistry());
}

std::unique_ptr<FunctionRegistry> FunctionRegistry::Make(FunctionRegistry* parent) {
  return std::unique_ptr<FunctionRegistry>(new FunctionRegistry(
      new FunctionRegistry::FunctionRegistryImpl(parent->impl_.get())));
}

FunctionRegistry::FunctionRegistry() : FunctionRegistry(new FunctionRegistryImpl()) {}

FunctionRegistry::FunctionRegistry(FunctionRegistryImpl* impl) { impl_.reset(impl); }

FunctionRegistry::~FunctionRegistry() {}

Status FunctionRegistry::CanAddFunction(std::shared_ptr<Function> function,
                                        bool allow_overwrite) {
  return impl_->CanAddFunction(std::move(function), allow_overwrite);
}

Status FunctionRegistry::AddFunction(std::shared_ptr<Function> function,
                                     bool allow_overwrite) {
  return impl_->AddFunction(std::move(function), allow_overwrite);
}

Status FunctionRegistry::CanAddAlias(const std::string& target_name,
                                     const std::string& source_name) {
  return impl_->CanAddAlias(target_name, source_name);
}

Status FunctionRegistry::AddAlias(const std::string& target_name,
                                  const std::string& source_name) {
  return impl_->AddAlias(target_name, source_name);
}

Status FunctionRegistry::CanAddFunctionOptionsType(
    const FunctionOptionsType* options_type, bool allow_overwrite) {
  return impl_->CanAddFunctionOptionsType(options_type, allow_overwrite);
}

Status FunctionRegistry::AddFunctionOptionsType(const FunctionOptionsType* options_type,
                                                bool allow_overwrite) {
  return impl_->AddFunctionOptionsType(options_type, allow_overwrite);
}

Result<std::shared_ptr<Function>> FunctionRegistry::GetFunction(
    const std::string& name) const {
  return impl_->GetFunction(name);
}

std::vector<std::string> FunctionRegistry::GetFunctionNames() const {
  return impl_->GetFunctionNames();
}

Result<const FunctionOptionsType*> FunctionRegistry::GetFunctionOptionsType(
    const std::string& name) const {
  return impl_->GetFunctionOptionsType(name);
}

int FunctionRegistry::num_functions() const { return impl_->num_functions(); }

const Function* FunctionRegistry::cast_function() const { return impl_->cast_function(); }

namespace internal {

static std::unique_ptr<FunctionRegistry> CreateBuiltInRegistry() {
  auto registry = FunctionRegistry::Make();

  // Register core kernels
  RegisterScalarCast(registry.get());
  RegisterDictionaryDecode(registry.get());
  RegisterVectorHash(registry.get());
  RegisterVectorSelection(registry.get());

  RegisterScalarOptions(registry.get());
  RegisterVectorOptions(registry.get());
  RegisterAggregateOptions(registry.get());

#ifdef ARROW_COMPUTE
  // Register additional kernels

  // Scalar functions
  RegisterScalarArithmetic(registry.get());
  RegisterScalarBoolean(registry.get());
  RegisterScalarComparison(registry.get());
  RegisterScalarIfElse(registry.get());
  RegisterScalarNested(registry.get());
  RegisterScalarRandom(registry.get());  // Nullary
  RegisterScalarRoundArithmetic(registry.get());
  RegisterScalarSetLookup(registry.get());
  RegisterScalarStringAscii(registry.get());
  RegisterScalarStringUtf8(registry.get());
  RegisterScalarTemporalBinary(registry.get());
  RegisterScalarTemporalUnary(registry.get());
  RegisterScalarValidity(registry.get());

  // Vector functions
  RegisterVectorArraySort(registry.get());
  RegisterVectorCumulativeSum(registry.get());
  RegisterVectorNested(registry.get());
  RegisterVectorRank(registry.get());
  RegisterVectorReplace(registry.get());
  RegisterVectorSelectK(registry.get());
  RegisterVectorSort(registry.get());
  RegisterVectorRunEndEncode(registry.get());
  RegisterVectorRunEndDecode(registry.get());
  RegisterVectorPairwise(registry.get());
  RegisterVectorStatistics(registry.get());
  RegisterVectorSwizzle(registry.get());

  // Aggregate functions
  RegisterHashAggregateBasic(registry.get());
  RegisterHashAggregateNumeric(registry.get());
  RegisterHashAggregatePivot(registry.get());
  RegisterScalarAggregateBasic(registry.get());
  RegisterScalarAggregateMode(registry.get());
  RegisterScalarAggregatePivot(registry.get());
  RegisterScalarAggregateQuantile(registry.get());
  RegisterScalarAggregateTDigest(registry.get());
  RegisterScalarAggregateVariance(registry.get());
#endif

  return registry;
}

}  // namespace internal

FunctionRegistry* GetFunctionRegistry() {
  static auto g_registry = internal::CreateBuiltInRegistry();
  return g_registry.get();
}

}  // namespace compute
}  // namespace arrow20