File indexing completed on 2025-08-28 08:26:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #pragma once
0022
0023 #include <string>
0024 #include <utility>
0025 #include <vector>
0026
0027 #include "arrow/compute/kernel.h"
0028 #include "arrow/compute/type_fwd.h"
0029 #include "arrow/datum.h"
0030 #include "arrow/result.h"
0031 #include "arrow/status.h"
0032 #include "arrow/util/compare.h"
0033 #include "arrow/util/macros.h"
0034 #include "arrow/util/visibility.h"
0035
0036 namespace arrow {
0037 namespace compute {
0038
0039
0040
0041
0042
0043
0044
0045 struct ARROW_EXPORT Arity {
0046
0047 static Arity Nullary() { return Arity(0, false); }
0048
0049
0050 static Arity Unary() { return Arity(1, false); }
0051
0052
0053 static Arity Binary() { return Arity(2, false); }
0054
0055
0056 static Arity Ternary() { return Arity(3, false); }
0057
0058
0059
0060
0061
0062 static Arity VarArgs(int min_args = 0) { return Arity(min_args, true); }
0063
0064
0065 explicit Arity(int num_args = 0, bool is_varargs = false)
0066 : num_args(num_args), is_varargs(is_varargs) {}
0067
0068
0069
0070 int num_args;
0071
0072
0073 bool is_varargs = false;
0074 };
0075
0076 struct ARROW_EXPORT FunctionDoc {
0077
0078
0079
0080 std::string summary;
0081
0082
0083 std::string description;
0084
0085
0086
0087
0088 std::vector<std::string> arg_names;
0089
0090
0091
0092
0093 std::string options_class;
0094
0095
0096
0097
0098
0099 bool options_required;
0100
0101 FunctionDoc() = default;
0102
0103 FunctionDoc(std::string summary, std::string description,
0104 std::vector<std::string> arg_names, std::string options_class = "",
0105 bool options_required = false)
0106 : summary(std::move(summary)),
0107 description(std::move(description)),
0108 arg_names(std::move(arg_names)),
0109 options_class(std::move(options_class)),
0110 options_required(options_required) {}
0111
0112 static const FunctionDoc& Empty();
0113 };
0114
0115
0116 class ARROW_EXPORT FunctionExecutor {
0117 public:
0118 virtual ~FunctionExecutor() = default;
0119
0120
0121
0122
0123 virtual Status Init(const FunctionOptions* options = NULLPTR,
0124 ExecContext* exec_ctx = NULLPTR) = 0;
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 virtual Result<Datum> Execute(const std::vector<Datum>& args, int64_t length = -1) = 0;
0136 };
0137
0138
0139
0140
0141
0142 class ARROW_EXPORT Function {
0143 public:
0144
0145
0146 enum Kind {
0147
0148
0149
0150
0151 SCALAR,
0152
0153
0154
0155
0156 VECTOR,
0157
0158
0159 SCALAR_AGGREGATE,
0160
0161
0162
0163 HASH_AGGREGATE,
0164
0165
0166
0167 META
0168 };
0169
0170 virtual ~Function() = default;
0171
0172
0173 const std::string& name() const { return name_; }
0174
0175
0176
0177 Function::Kind kind() const { return kind_; }
0178
0179
0180
0181 const Arity& arity() const { return arity_; }
0182
0183
0184 const FunctionDoc& doc() const { return doc_; }
0185
0186
0187 virtual int num_kernels() const = 0;
0188
0189
0190
0191
0192
0193 virtual Result<const Kernel*> DispatchExact(const std::vector<TypeHolder>& types) const;
0194
0195
0196
0197
0198
0199
0200
0201
0202 virtual Result<const Kernel*> DispatchBest(std::vector<TypeHolder>* values) const;
0203
0204
0205
0206
0207
0208 virtual Result<std::shared_ptr<FunctionExecutor>> GetBestExecutor(
0209 std::vector<TypeHolder> inputs) const;
0210
0211
0212
0213
0214
0215
0216
0217
0218 virtual Result<Datum> Execute(const std::vector<Datum>& args,
0219 const FunctionOptions* options, ExecContext* ctx) const;
0220
0221 virtual Result<Datum> Execute(const ExecBatch& batch, const FunctionOptions* options,
0222 ExecContext* ctx) const;
0223
0224
0225
0226
0227
0228 const FunctionOptions* default_options() const { return default_options_; }
0229
0230 virtual Status Validate() const;
0231
0232
0233
0234
0235
0236
0237
0238 virtual bool is_pure() const { return true; }
0239
0240 protected:
0241 Function(std::string name, Function::Kind kind, const Arity& arity, FunctionDoc doc,
0242 const FunctionOptions* default_options)
0243 : name_(std::move(name)),
0244 kind_(kind),
0245 arity_(arity),
0246 doc_(std::move(doc)),
0247 default_options_(default_options) {}
0248
0249 Status CheckArity(size_t num_args) const;
0250
0251 std::string name_;
0252 Function::Kind kind_;
0253 Arity arity_;
0254 const FunctionDoc doc_;
0255 const FunctionOptions* default_options_ = NULLPTR;
0256 };
0257
0258 namespace detail {
0259
0260 template <typename KernelType>
0261 class FunctionImpl : public Function {
0262 public:
0263
0264 std::vector<const KernelType*> kernels() const {
0265 std::vector<const KernelType*> result;
0266 for (const auto& kernel : kernels_) {
0267 result.push_back(&kernel);
0268 }
0269 return result;
0270 }
0271
0272 int num_kernels() const override { return static_cast<int>(kernels_.size()); }
0273
0274 protected:
0275 FunctionImpl(std::string name, Function::Kind kind, const Arity& arity, FunctionDoc doc,
0276 const FunctionOptions* default_options)
0277 : Function(std::move(name), kind, arity, std::move(doc), default_options) {}
0278
0279 std::vector<KernelType> kernels_;
0280 };
0281
0282
0283 ARROW_EXPORT
0284 const Kernel* DispatchExactImpl(const Function* func, const std::vector<TypeHolder>&);
0285
0286
0287 ARROW_EXPORT
0288 Status NoMatchingKernel(const Function* func, const std::vector<TypeHolder>&);
0289
0290 }
0291
0292
0293
0294
0295
0296
0297 class ARROW_EXPORT ScalarFunction : public detail::FunctionImpl<ScalarKernel> {
0298 public:
0299 using KernelType = ScalarKernel;
0300
0301 ScalarFunction(std::string name, const Arity& arity, FunctionDoc doc,
0302 const FunctionOptions* default_options = NULLPTR, bool is_pure = true)
0303 : detail::FunctionImpl<ScalarKernel>(std::move(name), Function::SCALAR, arity,
0304 std::move(doc), default_options),
0305 is_pure_(is_pure) {}
0306
0307
0308
0309
0310 Status AddKernel(std::vector<InputType> in_types, OutputType out_type,
0311 ArrayKernelExec exec, KernelInit init = NULLPTR);
0312
0313
0314
0315 Status AddKernel(ScalarKernel kernel);
0316
0317
0318 bool is_pure() const override { return is_pure_; }
0319
0320 private:
0321 const bool is_pure_;
0322 };
0323
0324
0325
0326
0327
0328 class ARROW_EXPORT VectorFunction : public detail::FunctionImpl<VectorKernel> {
0329 public:
0330 using KernelType = VectorKernel;
0331
0332 VectorFunction(std::string name, const Arity& arity, FunctionDoc doc,
0333 const FunctionOptions* default_options = NULLPTR)
0334 : detail::FunctionImpl<VectorKernel>(std::move(name), Function::VECTOR, arity,
0335 std::move(doc), default_options) {}
0336
0337
0338
0339
0340 Status AddKernel(std::vector<InputType> in_types, OutputType out_type,
0341 ArrayKernelExec exec, KernelInit init = NULLPTR);
0342
0343
0344
0345 Status AddKernel(VectorKernel kernel);
0346 };
0347
0348 class ARROW_EXPORT ScalarAggregateFunction
0349 : public detail::FunctionImpl<ScalarAggregateKernel> {
0350 public:
0351 using KernelType = ScalarAggregateKernel;
0352
0353 ScalarAggregateFunction(std::string name, const Arity& arity, FunctionDoc doc,
0354 const FunctionOptions* default_options = NULLPTR)
0355 : detail::FunctionImpl<ScalarAggregateKernel>(std::move(name),
0356 Function::SCALAR_AGGREGATE, arity,
0357 std::move(doc), default_options) {}
0358
0359
0360
0361 Status AddKernel(ScalarAggregateKernel kernel);
0362 };
0363
0364 class ARROW_EXPORT HashAggregateFunction
0365 : public detail::FunctionImpl<HashAggregateKernel> {
0366 public:
0367 using KernelType = HashAggregateKernel;
0368
0369 HashAggregateFunction(std::string name, const Arity& arity, FunctionDoc doc,
0370 const FunctionOptions* default_options = NULLPTR)
0371 : detail::FunctionImpl<HashAggregateKernel>(std::move(name),
0372 Function::HASH_AGGREGATE, arity,
0373 std::move(doc), default_options) {}
0374
0375
0376
0377 Status AddKernel(HashAggregateKernel kernel);
0378 };
0379
0380
0381
0382
0383
0384
0385 class ARROW_EXPORT MetaFunction : public Function {
0386 public:
0387 int num_kernels() const override { return 0; }
0388
0389 Result<Datum> Execute(const std::vector<Datum>& args, const FunctionOptions* options,
0390 ExecContext* ctx) const override;
0391
0392 Result<Datum> Execute(const ExecBatch& batch, const FunctionOptions* options,
0393 ExecContext* ctx) const override;
0394
0395 protected:
0396 virtual Result<Datum> ExecuteImpl(const std::vector<Datum>& args,
0397 const FunctionOptions* options,
0398 ExecContext* ctx) const = 0;
0399
0400 MetaFunction(std::string name, const Arity& arity, FunctionDoc doc,
0401 const FunctionOptions* default_options = NULLPTR)
0402 : Function(std::move(name), Function::META, arity, std::move(doc),
0403 default_options) {}
0404 };
0405
0406
0407
0408 }
0409 }