jvanstraten commented on a change in pull request #12279:
URL: https://github.com/apache/arrow/pull/12279#discussion_r801968651



##########
File path: cpp/src/arrow/engine/substrait/serde_test.cc
##########
@@ -0,0 +1,739 @@
+// 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/dataset/file_base.h"
+#include "arrow/dataset/scanner.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"
+
+using testing::ElementsAre;
+using testing::Eq;
+using testing::HasSubstr;
+
+namespace arrow {
+
+using internal::checked_cast;
+
+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", struct_({
+                        field("i32", int32()),
+                        field("str", utf8()),
+                        field("struct_i32_str",
+                              struct_({field("i32", int32()), field("str", 
utf8())})),
+                    })),
+    field("list_struct", list(struct_({
+                             field("i32", int32()),
+                             field("str", utf8()),
+                             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 (int i = 0; i < type->num_fields(); ++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)};
+}
+
+std::shared_ptr<Buffer> SubstraitFromJSON(util::string_view type_name,
+                                          util::string_view json) {
+  auto maybe_buf = internal::SubstraitFromJSON(type_name, json);
+  DCHECK_OK(maybe_buf.status());
+  return maybe_buf.ValueOrDie();
+}

Review comment:
       I'm guessing Ben wrote it this way to show that failure of 
`internal::SubstraitFromJSON()` is a problem with the tests (i.e. invalid JSON 
in the test case) rather than with the code that's actually under test. I guess 
it also saves a tiny amount of typing (it *is* used a lot). But I can change it 
if you want.




-- 
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]


Reply via email to