bkietz commented on a change in pull request #8894: URL: https://github.com/apache/arrow/pull/8894#discussion_r545410376
########## 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())); Review comment: Proper argument promotion will be handled in: https://issues.apache.org/jira/browse/ARROW-8919 For now, I'm maintaining the old behavior ---------------------------------------------------------------- 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