Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
kernel.h
Go to the documentation of this file.
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 #ifndef ARROW_COMPUTE_KERNEL_H
19 #define ARROW_COMPUTE_KERNEL_H
20 
21 #include <memory>
22 #include <vector>
23 
24 #include "arrow/array.h"
25 #include "arrow/record_batch.h"
26 #include "arrow/table.h"
27 #include "arrow/util/macros.h"
28 #include "arrow/util/variant.h"
29 #include "arrow/util/visibility.h"
30 
31 namespace arrow {
32 namespace compute {
33 
34 class FunctionContext;
35 
38 class ARROW_EXPORT OpKernel {
39  public:
40  virtual ~OpKernel() = default;
41 };
42 
44 struct ARROW_EXPORT Scalar {
45  ~Scalar() {}
46 
48 };
49 
52 struct ARROW_EXPORT Datum {
53  enum type { NONE, SCALAR, ARRAY, CHUNKED_ARRAY, RECORD_BATCH, TABLE, COLLECTION };
54 
56  std::shared_ptr<ChunkedArray>, std::shared_ptr<RecordBatch>,
57  std::shared_ptr<Table>, std::vector<Datum>>
59 
61  Datum() : value(nullptr) {}
62 
63  explicit Datum(const std::shared_ptr<Scalar>& value) : value(value) {}
64 
65  explicit Datum(const std::shared_ptr<ArrayData>& value) : value(value) {}
66 
67  explicit Datum(const std::shared_ptr<Array>& value) : Datum(value->data()) {}
68 
69  explicit Datum(const std::shared_ptr<ChunkedArray>& value) : value(value) {}
70 
71  explicit Datum(const std::shared_ptr<RecordBatch>& value) : value(value) {}
72 
73  explicit Datum(const std::shared_ptr<Table>& value) : value(value) {}
74 
75  explicit Datum(const std::vector<Datum>& value) : value(value) {}
76 
77  ~Datum() {}
78 
79  Datum(const Datum& other) noexcept { this->value = other.value; }
80 
81  Datum::type kind() const {
82  switch (this->value.which()) {
83  case 0:
84  return Datum::NONE;
85  case 1:
86  return Datum::SCALAR;
87  case 2:
88  return Datum::ARRAY;
89  case 3:
90  return Datum::CHUNKED_ARRAY;
91  case 4:
92  return Datum::RECORD_BATCH;
93  case 5:
94  return Datum::TABLE;
95  case 6:
96  return Datum::COLLECTION;
97  default:
98  return Datum::NONE;
99  }
100  }
101 
102  std::shared_ptr<ArrayData> array() const {
103  return util::get<std::shared_ptr<ArrayData>>(this->value);
104  }
105 
106  std::shared_ptr<ChunkedArray> chunked_array() const {
107  return util::get<std::shared_ptr<ChunkedArray>>(this->value);
108  }
109 
110  const std::vector<Datum> collection() const {
111  return util::get<std::vector<Datum>>(this->value);
112  }
113 
114  bool is_arraylike() const {
115  return this->kind() == Datum::ARRAY || this->kind() == Datum::CHUNKED_ARRAY;
116  }
117 
121  std::shared_ptr<DataType> type() const {
122  if (this->kind() == Datum::ARRAY) {
123  return util::get<std::shared_ptr<ArrayData>>(this->value)->type;
124  } else if (this->kind() == Datum::CHUNKED_ARRAY) {
125  return util::get<std::shared_ptr<ChunkedArray>>(this->value)->type();
126  }
127  return nullptr;
128  }
129 };
130 
133 class ARROW_EXPORT UnaryKernel : public OpKernel {
134  public:
135  virtual Status Call(FunctionContext* ctx, const Datum& input, Datum* out) = 0;
136 };
137 
138 } // namespace compute
139 } // namespace arrow
140 
141 #endif // ARROW_COMPUTE_KERNEL_H
const std::vector< Datum > collection() const
Definition: kernel.h:110
std::shared_ptr< ArrayData > array() const
Definition: kernel.h:102
Placeholder for Scalar values until we implement these.
Definition: kernel.h:44
Container for variables and options used by function evaluation.
Definition: context.h:39
Datum(const Datum &other) noexcept
Definition: kernel.h:79
Datum(const std::shared_ptr< Array > &value)
Definition: kernel.h:67
Datum(const std::vector< Datum > &value)
Definition: kernel.h:75
Definition: status.h:93
~Datum()
Definition: kernel.h:77
~Scalar()
Definition: kernel.h:45
util::variant< decltype(NULLPTR), std::shared_ptr< Scalar >, std::shared_ptr< ArrayData >, std::shared_ptr< ChunkedArray >, std::shared_ptr< RecordBatch >, std::shared_ptr< Table >, std::vector< Datum > > value
Definition: kernel.h:58
Variant type for various Arrow C++ data structures.
Definition: kernel.h:52
std::shared_ptr< DataType > type() const
The value type of the variant, if any.
Definition: kernel.h:121
Definition: kernel.h:53
Datum()
Empty datum, to be populated elsewhere.
Definition: kernel.h:61
bool is_arraylike() const
Definition: kernel.h:114
Definition: kernel.h:53
Definition: kernel.h:53
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
std::shared_ptr< ChunkedArray > chunked_array() const
Definition: kernel.h:106
Definition: kernel.h:53
Datum(const std::shared_ptr< RecordBatch > &value)
Definition: kernel.h:71
Datum(const std::shared_ptr< Scalar > &value)
Definition: kernel.h:63
Definition: variant.h:581
Datum::type kind() const
Definition: kernel.h:81
Datum(const std::shared_ptr< Table > &value)
Definition: kernel.h:73
Definition: kernel.h:53
An array-valued function of a single input argument.
Definition: kernel.h:133
type
Definition: kernel.h:53
Datum(const std::shared_ptr< ArrayData > &value)
Definition: kernel.h:65
Datum(const std::shared_ptr< ChunkedArray > &value)
Definition: kernel.h:69
#define ARROW_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:23
Base class for operator kernels.
Definition: kernel.h:38