kszucs commented on a change in pull request #11707: URL: https://github.com/apache/arrow/pull/11707#discussion_r768035823
########## File path: cpp/src/arrow/engine/substrait/serde_test.cc ########## @@ -0,0 +1,475 @@ +// 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/engine/substrait/serde.h" + +#include <google/protobuf/descriptor.h> +#include <google/protobuf/util/json_util.h> +#include <google/protobuf/util/type_resolver_util.h> +#include <gtest/gtest.h> + +#include "arrow/compute/exec/expression_internal.h" +#include "arrow/engine/substrait/extension_types.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" +#include "arrow/util/key_value_metadata.h" + +namespace arrow { +namespace engine { + +const std::shared_ptr<Schema> kBoringSchema = schema({ + field("bool", boolean()), + field("i8", int8()), + field("i32", int32()), + field("i32_req", int32(), /*nullable=*/false), + field("u32", uint32()), + field("i64", int64()), + field("f32", float32()), + field("f32_req", float32(), /*nullable=*/false), + field("f64", float64()), + field("date64", date64()), + field("str", utf8()), + field("list_i32", list(int32())), + field("struct_i32_str", struct_({ + field("i32", int32()), + field("str", utf8()), + })), + field("dict_str", dictionary(int32(), utf8())), + field("dict_i32", dictionary(int32(), int32())), + field("ts_ns", timestamp(TimeUnit::NANO)), +}); + +std::shared_ptr<DataType> StripFieldNames(std::shared_ptr<DataType> type) { + if (type->id() == Type::STRUCT) { + FieldVector fields(type->num_fields()); + for (size_t i = 0; i < fields.size(); ++i) { + fields[i] = type->field(i)->WithName(""); + } + return struct_(std::move(fields)); + } + + if (type->id() == Type::LIST) { + return list(type->field(0)->WithName("")); + } + + return type; +} + +// map to an index-only field reference +inline FieldRef BoringRef(FieldRef ref) { + auto path = *ref.FindOne(*kBoringSchema); + return {std::move(path)}; +} + +inline compute::Expression UseBoringRefs(const compute::Expression& expr) { + if (expr.literal()) return expr; + + if (auto ref = expr.field_ref()) { + return compute::field_ref(*ref->FindOne(*kBoringSchema)); + } + + auto modified_call = *CallNotNull(expr); + for (auto& arg : modified_call.arguments) { + arg = UseBoringRefs(arg); + } + return compute::Expression{std::move(modified_call)}; +} + +google::protobuf::util::TypeResolver* GetGeneratedTypeResolver() { + static std::unique_ptr<google::protobuf::util::TypeResolver> type_resolver; + if (!type_resolver) { + type_resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool( + /*url_prefix=*/"", google::protobuf::DescriptorPool::generated_pool())); + } + return type_resolver.get(); +} + +std::shared_ptr<Buffer> SubstraitFromJSON(util::string_view type_name, Review comment: Can we make this exposable since pyarrow won't have a substrait producer, at least in the beginning? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
