bkietz commented on a change in pull request #8894:
URL: https://github.com/apache/arrow/pull/8894#discussion_r545384788



##########
File path: cpp/src/arrow/dataset/expression_internal.h
##########
@@ -0,0 +1,465 @@
+// 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 "arrow/dataset/expression.h"
+
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/registry.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+#include "arrow/util/logging.h"
+
+namespace arrow {
+
+using internal::checked_cast;
+
+namespace dataset {
+
+bool Identical(const Expression& l, const Expression& r) { return l.impl_ == 
r.impl_; }
+
+const Expression::Call* CallNotNull(const Expression& expr) {
+  auto call = expr.call();
+  DCHECK_NE(call, nullptr);
+  return call;
+}
+
+inline void GetAllFieldRefs(const Expression& expr,
+                            std::unordered_set<FieldRef, FieldRef::Hash>* 
refs) {
+  if (auto lit = expr.literal()) return;
+
+  if (auto ref = expr.field_ref()) {
+    refs->emplace(*ref);
+    return;
+  }
+
+  for (const Expression& arg : CallNotNull(expr)->arguments) {
+    GetAllFieldRefs(arg, refs);
+  }
+}
+
+inline std::vector<ValueDescr> GetDescriptors(const std::vector<Expression>& 
exprs) {
+  std::vector<ValueDescr> descrs(exprs.size());
+  for (size_t i = 0; i < exprs.size(); ++i) {
+    DCHECK(exprs[i].IsBound());
+    descrs[i] = exprs[i].descr();
+  }
+  return descrs;
+}
+
+inline std::vector<ValueDescr> GetDescriptors(const std::vector<Datum>& 
values) {
+  std::vector<ValueDescr> descrs(values.size());
+  for (size_t i = 0; i < values.size(); ++i) {
+    descrs[i] = values[i].descr();
+  }
+  return descrs;
+}
+
+struct FieldPathGetDatumImpl {
+  template <typename T, typename = decltype(FieldPath{}.Get(std::declval<const 
T&>()))>
+  Result<Datum> operator()(const std::shared_ptr<T>& ptr) {
+    return path_.Get(*ptr).template As<Datum>();
+  }
+
+  template <typename T>
+  Result<Datum> operator()(const T&) {
+    return Status::NotImplemented("FieldPath::Get() into Datum ", 
datum_.ToString());
+  }
+
+  const Datum& datum_;
+  const FieldPath& path_;
+};
+
+inline Result<Datum> GetDatumField(const FieldRef& ref, const Datum& input) {
+  Datum field;
+
+  FieldPath path;
+  if (auto type = input.type()) {
+    ARROW_ASSIGN_OR_RAISE(path, ref.FindOneOrNone(*input.type()));
+  } else if (input.kind() == Datum::RECORD_BATCH) {
+    ARROW_ASSIGN_OR_RAISE(path, 
ref.FindOneOrNone(*input.record_batch()->schema()));
+  } else if (input.kind() == Datum::TABLE) {
+    ARROW_ASSIGN_OR_RAISE(path, ref.FindOneOrNone(*input.table()->schema()));
+  }
+
+  if (path) {
+    ARROW_ASSIGN_OR_RAISE(field,
+                          util::visit(FieldPathGetDatumImpl{input, path}, 
input.value));
+  }
+
+  if (field == Datum{}) {
+    field = Datum(std::make_shared<NullScalar>());
+  }
+
+  return field;
+}
+
+struct Comparison {
+  enum type {
+    NA = 0,
+    EQUAL = 1,
+    LESS = 2,
+    GREATER = 4,
+    NOT_EQUAL = LESS | GREATER,
+    LESS_EQUAL = LESS | EQUAL,
+    GREATER_EQUAL = GREATER | EQUAL,
+  };
+
+  static const type* Get(const std::string& function) {
+    static std::unordered_map<std::string, type> flipped_comparisons{
+        {"equal", EQUAL},     {"not_equal", NOT_EQUAL},
+        {"less", LESS},       {"less_equal", LESS_EQUAL},
+        {"greater", GREATER}, {"greater_equal", GREATER_EQUAL},
+    };
+
+    auto it = flipped_comparisons.find(function);
+    return it != flipped_comparisons.end() ? &it->second : nullptr;
+  }
+
+  static const type* Get(const Expression& expr) {
+    if (auto call = expr.call()) {
+      return Comparison::Get(call->function_name);
+    }
+    return nullptr;
+  }
+
+  // Execute a simple Comparison between scalars, casting the RHS if types 
disagree
+  static Result<type> Execute(Datum l, Datum r) {
+    if (!l.is_scalar() || !r.is_scalar()) {
+      return Status::Invalid("Cannot Execute Comparison on non-scalars");
+    }
+
+    if (!l.type()->Equals(r.type())) {
+      ARROW_ASSIGN_OR_RAISE(r, compute::Cast(r, l.type()));
+    }
+
+    std::vector<Datum> arguments{std::move(l), std::move(r)};
+
+    ARROW_ASSIGN_OR_RAISE(auto equal, compute::CallFunction("equal", 
arguments));
+
+    if (!equal.scalar()->is_valid) return NA;
+    if (equal.scalar_as<BooleanScalar>().value) return EQUAL;
+
+    ARROW_ASSIGN_OR_RAISE(auto less, compute::CallFunction("less", arguments));
+
+    if (!less.scalar()->is_valid) return NA;
+    return less.scalar_as<BooleanScalar>().value ? LESS : GREATER;
+  }
+
+  static type GetFlipped(type op) {
+    switch (op) {
+      case NA:
+        return NA;
+      case EQUAL:
+        return EQUAL;
+      case LESS:
+        return GREATER;
+      case GREATER:
+        return LESS;
+      case NOT_EQUAL:
+        return NOT_EQUAL;
+      case LESS_EQUAL:
+        return GREATER_EQUAL;
+      case GREATER_EQUAL:
+        return LESS_EQUAL;
+    }
+    DCHECK(false);
+    return NA;
+  }
+
+  static std::string GetName(type op) {
+    switch (op) {
+      case NA:
+        DCHECK(false) << "unreachable";
+        break;
+      case EQUAL:
+        return "equal";
+      case LESS:
+        return "less";
+      case GREATER:
+        return "greater";
+      case NOT_EQUAL:
+        return "not_equal";
+      case LESS_EQUAL:
+        return "less_equal";
+      case GREATER_EQUAL:
+        return "greater_equal";
+    }
+    DCHECK(false);
+    return "na";
+  }
+
+  static std::string GetOp(type op) {
+    switch (op) {
+      case NA:
+        DCHECK(false) << "unreachable";
+        break;
+      case EQUAL:
+        return "==";
+      case LESS:
+        return "<";
+      case GREATER:
+        return ">";
+      case NOT_EQUAL:
+        return "!=";
+      case LESS_EQUAL:
+        return "<=";
+      case GREATER_EQUAL:
+        return ">=";
+    }
+    DCHECK(false);
+    return "";
+  }
+};
+
+inline const compute::CastOptions* GetCastOptions(const Expression::Call& 
call) {
+  if (call.function_name != "cast") return nullptr;
+  return checked_cast<const compute::CastOptions*>(call.options.get());
+}
+
+inline bool IsSetLookup(const std::string& function) {
+  return function == "is_in" || function == "index_in";
+}
+
+inline bool IsSameTypesBinary(const std::string& function) {
+  if (Comparison::Get(function)) return true;
+
+  static std::unordered_set<std::string> set{"add", "subtract", "multiply", 
"divide"};
+
+  return set.find(function) != set.end();
+}
+
+inline const compute::SetLookupOptions* GetSetLookupOptions(
+    const Expression::Call& call) {
+  if (!IsSetLookup(call.function_name)) return nullptr;
+  return checked_cast<const compute::SetLookupOptions*>(call.options.get());
+}
+
+inline const compute::StructOptions* GetStructOptions(const Expression::Call& 
call) {
+  if (call.function_name != "struct") return nullptr;
+  return checked_cast<const compute::StructOptions*>(call.options.get());
+}
+
+inline const compute::StrptimeOptions* GetStrptimeOptions(const 
Expression::Call& call) {
+  if (call.function_name != "strptime") return nullptr;
+  return checked_cast<const compute::StrptimeOptions*>(call.options.get());
+}
+
+inline const std::shared_ptr<DataType>& GetDictionaryValueType(
+    const std::shared_ptr<DataType>& type) {
+  if (type && type->id() == Type::DICTIONARY) {
+    return checked_cast<const DictionaryType&>(*type).value_type();
+  }
+  static std::shared_ptr<DataType> null;
+  return null;
+}
+
+inline Status EnsureNotDictionary(ValueDescr* descr) {
+  if (auto value_type = GetDictionaryValueType(descr->type)) {
+    descr->type = std::move(value_type);
+  }
+  return Status::OK();
+}
+
+inline Status EnsureNotDictionary(Datum* datum) {
+  if (datum->type()->id() == Type::DICTIONARY) {
+    const auto& type = checked_cast<const 
DictionaryType&>(*datum->type()).value_type();
+    ARROW_ASSIGN_OR_RAISE(*datum, compute::Cast(*datum, type));
+  }
+  return Status::OK();
+}
+
+inline Status EnsureNotDictionary(Expression::Call* call) {
+  if (auto options = GetSetLookupOptions(*call)) {
+    auto new_options = *options;
+    RETURN_NOT_OK(EnsureNotDictionary(&new_options.value_set));
+    call->options.reset(new compute::SetLookupOptions(std::move(new_options)));
+  }
+  return Status::OK();
+}
+
+inline Result<std::shared_ptr<StructScalar>> FunctionOptionsToStructScalar(
+    const Expression::Call& call) {
+  if (call.options == nullptr) {
+    return nullptr;
+  }
+
+  auto Finish = [](ScalarVector values, std::vector<std::string> names) {

Review comment:
       Alright, I'll add a constructor




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to