bkietz commented on a change in pull request #8894: URL: https://github.com/apache/arrow/pull/8894#discussion_r545406868
########## File path: cpp/src/arrow/dataset/expression.cc ########## @@ -0,0 +1,1177 @@ +// 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 "arrow/chunked_array.h" +#include "arrow/compute/exec_internal.h" +#include "arrow/dataset/expression_internal.h" +#include "arrow/io/memory.h" +#include "arrow/ipc/reader.h" +#include "arrow/ipc/writer.h" +#include "arrow/util/key_value_metadata.h" +#include "arrow/util/logging.h" +#include "arrow/util/optional.h" +#include "arrow/util/string.h" +#include "arrow/util/value_parsing.h" + +namespace arrow { + +using internal::checked_cast; +using internal::checked_pointer_cast; + +namespace dataset { + +Expression::Expression(Call call) : impl_(std::make_shared<Impl>(std::move(call))) {} + +Expression::Expression(Datum literal) + : impl_(std::make_shared<Impl>(std::move(literal))) {} + +Expression::Expression(Parameter parameter) + : impl_(std::make_shared<Impl>(std::move(parameter))) {} + +Expression literal(Datum lit) { return Expression(std::move(lit)); } + +Expression field_ref(FieldRef ref) { + return Expression(Expression::Parameter{std::move(ref), {}}); +} + +Expression call(std::string function, std::vector<Expression> arguments, + std::shared_ptr<compute::FunctionOptions> options) { + Expression::Call call; + call.function_name = std::move(function); + call.arguments = std::move(arguments); + call.options = std::move(options); + return Expression(std::move(call)); +} + +const Datum* Expression::literal() const { return util::get_if<Datum>(impl_.get()); } + +const FieldRef* Expression::field_ref() const { + if (auto parameter = util::get_if<Parameter>(impl_.get())) { + return ¶meter->ref; + } + return nullptr; +} + +const Expression::Call* Expression::call() const { + return util::get_if<Call>(impl_.get()); +} + +ValueDescr Expression::descr() const { + if (impl_ == nullptr) return {}; + + if (auto lit = literal()) { + return lit->descr(); + } + + if (auto parameter = util::get_if<Parameter>(impl_.get())) { + return parameter->descr; + } + + return CallNotNull(*this)->descr; +} + +std::string Expression::ToString() const { + if (auto lit = literal()) { + if (lit->is_scalar()) { + switch (lit->type()->id()) { + case Type::STRING: + case Type::LARGE_STRING: + return '"' + + Escape(util::string_view(*lit->scalar_as<BaseBinaryScalar>().value)) + + '"'; + + case Type::BINARY: + case Type::FIXED_SIZE_BINARY: + case Type::LARGE_BINARY: + return '"' + lit->scalar_as<BaseBinaryScalar>().value->ToHexString() + '"'; + + default: + break; + } + return lit->scalar()->ToString(); + } + return lit->ToString(); + } + + if (auto ref = field_ref()) { + if (auto name = ref->name()) { + return *name; + } + if (auto path = ref->field_path()) { + return path->ToString(); + } + return ref->ToString(); + } + + auto call = CallNotNull(*this); + auto binary = [&](std::string op) { + return "(" + call->arguments[0].ToString() + " " + op + " " + + call->arguments[1].ToString() + ")"; + }; + + if (auto cmp = Comparison::Get(call->function_name)) { + return binary(Comparison::GetOp(*cmp)); + } + + constexpr util::string_view kleene = "_kleene"; + if (util::string_view{call->function_name}.ends_with(kleene)) { + auto op = call->function_name.substr(0, call->function_name.size() - kleene.size()); + return binary(std::move(op)); + } + + if (auto options = GetStructOptions(*call)) { + std::string out = "{"; + auto argument = call->arguments.begin(); + for (const auto& field_name : options->field_names) { + out += field_name + "=" + argument++->ToString() + ", "; + } + out.resize(out.size() - 1); + out.back() = '}'; + return out; + } + + std::string out = call->function_name + "("; + for (const auto& arg : call->arguments) { + out += arg.ToString() + ", "; + } + + if (call->options == nullptr) { + out.resize(out.size() - 1); + out.back() = ')'; + return out; + } + + if (auto options = GetSetLookupOptions(*call)) { + DCHECK_EQ(options->value_set.kind(), Datum::ARRAY); + out += "value_set=" + options->value_set.make_array()->ToString(); + if (options->skip_nulls) { + out += ", skip_nulls"; + } + return out + ")"; + } + + if (auto options = GetCastOptions(*call)) { + if (options->to_type == nullptr) { + return out + "to_type=<INVALID NOT PROVIDED>)"; + } + out += "to_type=" + options->to_type->ToString(); + if (options->allow_int_overflow) out += ", allow_int_overflow"; + if (options->allow_time_truncate) out += ", allow_time_truncate"; + if (options->allow_time_overflow) out += ", allow_time_overflow"; + if (options->allow_decimal_truncate) out += ", allow_decimal_truncate"; + if (options->allow_float_truncate) out += ", allow_float_truncate"; + if (options->allow_invalid_utf8) out += ", allow_invalid_utf8"; + return out + ")"; + } + + if (auto options = GetStrptimeOptions(*call)) { + return out + "format=" + options->format + + ", unit=" + internal::ToString(options->unit) + ")"; + } + + return out + "{NON-REPRESENTABLE OPTIONS})"; +} + +void PrintTo(const Expression& expr, std::ostream* os) { + *os << expr.ToString(); + if (expr.IsBound()) { + *os << "[bound]"; + } +} + +bool Expression::Equals(const Expression& other) const { + if (Identical(*this, other)) return true; + + if (impl_->index() != other.impl_->index()) { + return false; + } + + if (auto lit = literal()) { + return lit->Equals(*other.literal()); + } + + if (auto ref = field_ref()) { + return ref->Equals(*other.field_ref()); + } + + auto call = CallNotNull(*this); + auto other_call = CallNotNull(other); + + if (call->function_name != other_call->function_name || + call->kernel != other_call->kernel) { + return false; + } + + for (size_t i = 0; i < call->arguments.size(); ++i) { + if (!call->arguments[i].Equals(other_call->arguments[i])) { + return false; + } + } + + if (call->options == other_call->options) return true; + + if (auto options = GetSetLookupOptions(*call)) { + auto other_options = GetSetLookupOptions(*other_call); + return options->value_set == other_options->value_set && + options->skip_nulls == other_options->skip_nulls; + } + + if (auto options = GetCastOptions(*call)) { + auto other_options = GetCastOptions(*other_call); + for (auto safety_opt : { + &compute::CastOptions::allow_int_overflow, + &compute::CastOptions::allow_time_truncate, + &compute::CastOptions::allow_time_overflow, + &compute::CastOptions::allow_decimal_truncate, + &compute::CastOptions::allow_float_truncate, + &compute::CastOptions::allow_invalid_utf8, + }) { + if (options->*safety_opt != other_options->*safety_opt) return false; + } + return options->to_type->Equals(other_options->to_type); + } + + if (auto options = GetStructOptions(*call)) { + auto other_options = GetStructOptions(*other_call); + return options->field_names == other_options->field_names; + } + + if (auto options = GetStrptimeOptions(*call)) { + auto other_options = GetStrptimeOptions(*other_call); + return options->format == other_options->format && + options->unit == other_options->unit; + } + + ARROW_LOG(WARNING) << "comparing unknown FunctionOptions for function " + << call->function_name; + return false; +} + +size_t Expression::hash() const { + if (auto lit = literal()) { + if (lit->is_scalar()) { + return Scalar::Hash::hash(*lit->scalar()); + } + return 0; + } + + if (auto ref = field_ref()) { + return ref->hash(); + } + + auto call = CallNotNull(*this); + + size_t out = std::hash<std::string>{}(call->function_name); + for (const auto& arg : call->arguments) { + out ^= arg.hash(); + } + return out; +} + +bool Expression::IsBound() const { + if (descr().type == nullptr) return false; + + if (auto lit = literal()) return true; + + if (auto ref = field_ref()) return true; + + auto call = CallNotNull(*this); + + for (const Expression& arg : call->arguments) { + if (!arg.IsBound()) return false; + } + + return call->kernel != nullptr; +} + +bool Expression::IsScalarExpression() const { + if (auto lit = literal()) { + return lit->is_scalar(); + } + + // FIXME handle case where a list's item field is referenced + if (auto ref = field_ref()) return true; + + auto call = CallNotNull(*this); + + for (const Expression& arg : call->arguments) { + if (!arg.IsScalarExpression()) return false; + } + + if (call->function) { + return call->function->kind() == compute::Function::SCALAR; + } + + // this expression is not bound; make a best guess based on + // the default function registry + if (auto function = compute::GetFunctionRegistry() + ->GetFunction(call->function_name) + .ValueOr(nullptr)) { + return function->kind() == compute::Function::SCALAR; + } + + // unknown function or other error; conservatively return false + return false; +} + +bool Expression::IsNullLiteral() const { + if (auto lit = literal()) { + if (lit->null_count() == lit->length()) { + return true; + } + } + + return false; +} + +bool Expression::IsSatisfiable() const { + if (descr().type && descr().type->id() == Type::NA) { + return false; + } + + if (auto lit = literal()) { + if (lit->null_count() == lit->length()) { + return false; + } + + if (lit->is_scalar() && lit->type()->id() == Type::BOOL) { + return lit->scalar_as<BooleanScalar>().value; + } + } + + if (auto ref = field_ref()) { + return true; + } + + return true; +} + +inline bool KernelStateIsImmutable(const std::string& function) { Review comment: Not currently used, since no scalar function has mutable state. I don't think "immutable state" is contradictory. Distinguishing between precomputed data and state which the kernel happens not to mutate seems like unnecessary complication to me; it would require finding a place other than KernelState to keep (for example) `is_in`'s LUT. ---------------------------------------------------------------- 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