You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/18 06:51:44 UTC

[GitHub] [arrow] westonpace commented on a diff in pull request #13613: ARROW-15582: [C++] Add support for registering standard Substrait functions

westonpace commented on code in PR #13613:
URL: https://github.com/apache/arrow/pull/13613#discussion_r923016345


##########
cpp/src/arrow/engine/substrait/function_test.cc:
##########
@@ -0,0 +1,156 @@
+// 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 <memory>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include <google/protobuf/util/json_util.h>
+
+#include "arrow/array.h"
+#include "arrow/array/builder_binary.h"
+#include "arrow/compute/cast.h"
+#include "arrow/compute/exec/options.h"
+#include "arrow/compute/exec/util.h"
+#include "arrow/engine/substrait/function_internal.h"
+#include "arrow/engine/substrait/plan_internal.h"
+#include "arrow/engine/substrait/serde.h"
+#include "arrow/engine/substrait/test_plan_builder.h"
+#include "arrow/engine/substrait/type_internal.h"
+#include "arrow/record_batch.h"
+#include "arrow/table.h"
+#include "arrow/testing/future_util.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/type.h"
+
+namespace arrow {
+
+namespace engine {
+
+struct FunctionTestCase {
+  ExtensionSet::Id function_id;
+  std::vector<std::string> arguments;
+  std::vector<std::shared_ptr<DataType>> data_types;
+};
+
+struct ValidFunctionTestCase {
+  ExtensionSet::Id function_id;
+  std::vector<std::string> arguments;
+  std::vector<std::shared_ptr<DataType>> data_types;
+  std::string expected_output;
+  std::shared_ptr<DataType> expected_output_type;
+};
+
+std::shared_ptr<Array> GetArray(const std::string& value,
+                                const std::shared_ptr<DataType>& data_type) {
+  StringBuilder str_builder;
+  ARROW_EXPECT_OK(str_builder.Append(value));
+  EXPECT_OK_AND_ASSIGN(std::shared_ptr<Array> value_str, str_builder.Finish());
+  EXPECT_OK_AND_ASSIGN(Datum value_datum, compute::Cast(value_str, data_type));
+  return value_datum.make_array();
+}
+
+std::shared_ptr<Table> GetInputTable(
+    const std::vector<std::string>& arguments,
+    const std::vector<std::shared_ptr<DataType>>& data_types) {
+  std::vector<std::shared_ptr<Array>> columns;
+  std::vector<std::shared_ptr<Field>> fields;
+  EXPECT_EQ(arguments.size(), data_types.size());
+  for (std::size_t i = 0; i < arguments.size(); i++) {
+    if (data_types[i]) {
+      columns.push_back(GetArray(arguments[i], data_types[i]));
+      fields.push_back(field("arg_" + std::to_string(i), data_types[i]));
+    }
+  }
+  std::shared_ptr<RecordBatch> batch =
+      RecordBatch::Make(schema(std::move(fields)), 1, columns);
+  EXPECT_OK_AND_ASSIGN(std::shared_ptr<Table> table, Table::FromRecordBatches({batch}));
+  return table;
+}
+
+std::shared_ptr<Table> GetOutputTable(const std::string& output_value,
+                                      const std::shared_ptr<DataType>& output_type) {
+  std::vector<std::shared_ptr<Array>> columns(1);
+  std::vector<std::shared_ptr<Field>> fields(1);
+  columns[0] = GetArray(output_value, output_type);
+  fields[0] = field("output", output_type);
+  std::shared_ptr<RecordBatch> batch =
+      RecordBatch::Make(schema(std::move(fields)), 1, columns);
+  EXPECT_OK_AND_ASSIGN(std::shared_ptr<Table> table, Table::FromRecordBatches({batch}));
+  return table;
+}
+
+void CheckValidTestCases(const std::vector<ValidFunctionTestCase>& valid_cases) {
+  for (const ValidFunctionTestCase& test_case : valid_cases) {
+    std::shared_ptr<Table> input_table =
+        GetInputTable(test_case.arguments, test_case.data_types);
+    std::cout << "Input Table: " << input_table->ToString() << std::endl;
+    ASSERT_OK_AND_ASSIGN(std::shared_ptr<Buffer> substrait,
+                         internal::CreateScanProjectSubstrait(
+                             test_case.function_id, input_table, test_case.arguments,
+                             test_case.data_types, *test_case.expected_output_type));
+    ASSERT_OK_AND_ASSIGN(auto plan_json, internal::SubstraitToJSON("Plan", *substrait));
+    std::cout << plan_json << std::endl;
+    std::shared_ptr<Table> output_table;
+    std::shared_ptr<compute::SinkNodeConsumer> consumer =
+        std::make_shared<compute::TableSinkNodeConsumer>(&output_table,
+                                                         default_memory_pool());
+
+    // Mock table provider that ignores the table name and returns input_table
+    NamedTableProvider table_provider = [input_table](const std::vector<std::string>&) {
+      std::shared_ptr<compute::ExecNodeOptions> options =
+          std::make_shared<compute::TableSourceNodeOptions>(input_table);
+      return compute::Declaration("table_source", {}, options, "mock_source");
+    };
+
+    ASSERT_OK_AND_ASSIGN(
+        std::shared_ptr<compute::ExecPlan> plan,
+        DeserializePlan(*substrait, std::move(consumer), default_extension_id_registry(),
+                        /*ext_set_out=*/nullptr, std::move(table_provider)));
+    std::cout << "Plan: " << plan->ToString() << std::endl;
+    ASSERT_OK(plan->StartProducing());
+    ASSERT_FINISHES_OK(plan->finished());
+    std::cout << "Output Table: " << output_table->ToString() << std::endl;
+
+    // Could also modify the Substrait plan with an emit to drop the leading columns
+    ASSERT_OK_AND_ASSIGN(output_table,
+                         output_table->SelectColumns({output_table->num_columns() - 1}));
+
+    std::shared_ptr<Table> expected_output =
+        GetOutputTable(test_case.expected_output, test_case.expected_output_type);
+    AssertTablesEqual(*expected_output, *output_table, /*same_chunk_layout=*/false);
+  }
+}
+
+TEST(FunctionMapping, ValidCases) {
+  const std::vector<ValidFunctionTestCase> valid_test_cases = {
+      {{kSubstraitArithmeticFunctionsUri, "add"},
+       {"SILENT", "127", "10"},
+       {nullptr, int8(), int8()},
+       "-119",
+       int8()}};
+  CheckValidTestCases(valid_test_cases);
+  // const std::vector<FunctionTestCase> error_test_cases = {
+  //     {{kSubstraitArithmeticFunctionsUri, "add"},
+  //      {"ERROR", "127", "10"},
+  //      {nullptr, int8(), int8()}}};

Review Comment:
   I uncommented this and implemented these cases.



-- 
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: github-unsubscribe@arrow.apache.org

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