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 #pragma once
0021
0022 #include <memory>
0023 #include <string>
0024 #include <utility>
0025 #include <variant>
0026 #include <vector>
0027
0028 #include "arrow/compute/type_fwd.h"
0029 #include "arrow/datum.h"
0030 #include "arrow/type_fwd.h"
0031 #include "arrow/util/small_vector.h"
0032
0033 namespace arrow {
0034 namespace compute {
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 class ARROW_EXPORT Expression {
0046 public:
0047 struct Call {
0048 std::string function_name;
0049 std::vector<Expression> arguments;
0050 std::shared_ptr<FunctionOptions> options;
0051
0052 size_t hash;
0053
0054
0055 std::shared_ptr<Function> function;
0056 const Kernel* kernel = NULLPTR;
0057 std::shared_ptr<KernelState> kernel_state;
0058 TypeHolder type;
0059
0060 void ComputeHash();
0061 };
0062
0063 std::string ToString() const;
0064 bool Equals(const Expression& other) const;
0065 size_t hash() const;
0066 struct Hash {
0067 size_t operator()(const Expression& expr) const { return expr.hash(); }
0068 };
0069
0070
0071
0072
0073 Result<Expression> Bind(const TypeHolder& in, ExecContext* = NULLPTR) const;
0074 Result<Expression> Bind(const Schema& in_schema, ExecContext* = NULLPTR) const;
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 bool IsBound() const;
0088
0089
0090
0091 bool IsScalarExpression() const;
0092
0093
0094 bool IsNullLiteral() const;
0095
0096
0097
0098
0099
0100
0101
0102 bool IsSatisfiable() const;
0103
0104
0105
0106
0107 bool is_valid() const { return impl_ != NULLPTR; }
0108
0109
0110 const Call* call() const;
0111
0112 const Datum* literal() const;
0113
0114 const FieldRef* field_ref() const;
0115
0116
0117 const DataType* type() const;
0118
0119
0120
0121 struct Parameter {
0122 FieldRef ref;
0123
0124
0125 TypeHolder type;
0126 ::arrow::internal::SmallVector<int, 2> indices;
0127 };
0128 const Parameter* parameter() const;
0129
0130 Expression() = default;
0131 explicit Expression(Call call);
0132 explicit Expression(Datum literal);
0133 explicit Expression(Parameter parameter);
0134
0135 private:
0136 using Impl = std::variant<Datum, Parameter, Call>;
0137 std::shared_ptr<Impl> impl_;
0138
0139 ARROW_FRIEND_EXPORT friend bool Identical(const Expression& l, const Expression& r);
0140 };
0141
0142 inline bool operator==(const Expression& l, const Expression& r) { return l.Equals(r); }
0143 inline bool operator!=(const Expression& l, const Expression& r) { return !l.Equals(r); }
0144
0145 ARROW_EXPORT void PrintTo(const Expression&, std::ostream*);
0146
0147
0148
0149 ARROW_EXPORT
0150 Expression literal(Datum lit);
0151
0152 template <typename Arg>
0153 Expression literal(Arg&& arg) {
0154 return literal(Datum(std::forward<Arg>(arg)));
0155 }
0156
0157 ARROW_EXPORT
0158 Expression field_ref(FieldRef ref);
0159
0160 ARROW_EXPORT
0161 Expression call(std::string function, std::vector<Expression> arguments,
0162 std::shared_ptr<FunctionOptions> options = NULLPTR);
0163
0164 template <typename Options, typename = typename std::enable_if<
0165 std::is_base_of<FunctionOptions, Options>::value>::type>
0166 Expression call(std::string function, std::vector<Expression> arguments,
0167 Options options) {
0168 return call(std::move(function), std::move(arguments),
0169 std::make_shared<Options>(std::move(options)));
0170 }
0171
0172
0173 ARROW_EXPORT
0174 std::vector<FieldRef> FieldsInExpression(const Expression&);
0175
0176
0177 ARROW_EXPORT
0178 bool ExpressionHasFieldRefs(const Expression&);
0179
0180 struct ARROW_EXPORT KnownFieldValues;
0181
0182
0183
0184 ARROW_EXPORT
0185 Result<KnownFieldValues> ExtractKnownFieldValues(
0186 const Expression& guaranteed_true_predicate);
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 ARROW_EXPORT
0206 Result<Expression> Canonicalize(Expression, ExecContext* = NULLPTR);
0207
0208
0209
0210
0211 ARROW_EXPORT
0212 Result<Expression> FoldConstants(Expression);
0213
0214
0215 ARROW_EXPORT
0216 Result<Expression> ReplaceFieldsWithKnownValues(const KnownFieldValues& known_values,
0217 Expression);
0218
0219
0220
0221
0222
0223 ARROW_EXPORT
0224 Result<Expression> SimplifyWithGuarantee(Expression,
0225 const Expression& guaranteed_true_predicate);
0226
0227
0228
0229
0230
0231 ARROW_EXPORT Result<Expression> RemoveNamedRefs(Expression expression);
0232
0233
0234
0235
0236
0237
0238
0239
0240 ARROW_EXPORT Result<ExecBatch> MakeExecBatch(const Schema& full_schema,
0241 const Datum& partial,
0242 Expression guarantee = literal(true));
0243
0244
0245
0246 ARROW_EXPORT
0247 Result<Datum> ExecuteScalarExpression(const Expression&, const ExecBatch& input,
0248 ExecContext* = NULLPTR);
0249
0250
0251 ARROW_EXPORT
0252 Result<Datum> ExecuteScalarExpression(const Expression&, const Schema& full_schema,
0253 const Datum& partial_input, ExecContext* = NULLPTR);
0254
0255
0256
0257 ARROW_EXPORT
0258 Result<std::shared_ptr<Buffer>> Serialize(const Expression&);
0259
0260 ARROW_EXPORT
0261 Result<Expression> Deserialize(std::shared_ptr<Buffer>);
0262
0263
0264
0265
0266
0267 ARROW_EXPORT Expression project(std::vector<Expression> values,
0268 std::vector<std::string> names);
0269
0270 ARROW_EXPORT Expression equal(Expression lhs, Expression rhs);
0271
0272 ARROW_EXPORT Expression not_equal(Expression lhs, Expression rhs);
0273
0274 ARROW_EXPORT Expression less(Expression lhs, Expression rhs);
0275
0276 ARROW_EXPORT Expression less_equal(Expression lhs, Expression rhs);
0277
0278 ARROW_EXPORT Expression greater(Expression lhs, Expression rhs);
0279
0280 ARROW_EXPORT Expression greater_equal(Expression lhs, Expression rhs);
0281
0282 ARROW_EXPORT Expression is_null(Expression lhs, bool nan_is_null = false);
0283
0284 ARROW_EXPORT Expression is_valid(Expression lhs);
0285
0286 ARROW_EXPORT Expression and_(Expression lhs, Expression rhs);
0287 ARROW_EXPORT Expression and_(const std::vector<Expression>&);
0288 ARROW_EXPORT Expression or_(Expression lhs, Expression rhs);
0289 ARROW_EXPORT Expression or_(const std::vector<Expression>&);
0290 ARROW_EXPORT Expression not_(Expression operand);
0291
0292
0293
0294 }
0295 }