Repository: apache/arrow
File: cpp/src/arrow/table_test.cc (C++)

Code before:
  }

  ASSERT_OK_AND_ASSIGN(auto compacted, table->CombineChunks());

  EXPECT_TRUE(compacted->Equals(*table));
  for (int i = 0; i < compacted->num_columns(); ++i) {
    EXPECT_EQ(1, compacted->column(i)->num_chunks());
  }
}

TEST_F(TestTable, LARGE_MEMORY_TEST(CombineChunksStringColumn)) {
  schema_ = schema({field("str", utf8())});
  arrays_ = {nullptr};

  std::string value(1 << 16, '-');

  auto num_rows = kBinaryMemoryLimit / static_cast<int64_t>(value.size());
  StringBuilder builder;
  ASSERT_OK(builder.Resize(num_rows));
  ASSERT_OK(builder.ReserveData(value.size() * num_rows));
  for (int i = 0; i < num_rows; ++i) builder.UnsafeAppend(value);
  ASSERT_OK(builder.Finish(&arrays_[0]));

  auto batch = RecordBatch::Make(schema_, num_rows, arrays_);

  ASSERT_OK_AND_ASSIGN(auto table, Table::FromRecordBatches({batch, batch}));
  ASSERT_EQ(table->column(0)->num_chunks(), 2);

  ASSERT_OK_AND_ASSIGN(auto compacted, table->CombineChunks());
  EXPECT_TRUE(compacted->Equals(*table));

  // can't compact these columns any further; they contain too much character data
  ASSERT_EQ(compacted->column(0)->num_chunks(), 2);
}

TEST_F(TestTable, ConcatenateTables) {
  const int64_t length = 10;

  MakeExample1(length);
  auto batch1 = RecordBatch::Make(schema_, length, arrays_);

  // generate different data
  MakeExample1(length);
  auto batch2 = RecordBatch::Make(schema_, length, arrays_);

  ASSERT_OK_AND_ASSIGN(auto t1, Table::FromRecordBatches({batch1}));
  ASSERT_OK_AND_ASSIGN(auto t2, Table::FromRecordBatches({batch2}));

  ASSERT_OK_AND_ASSIGN(auto result, ConcatenateTables({t1, t2}));
  ASSERT_OK_AND_ASSIGN(auto expected, Table::FromRecordBatches({batch1, batch2}));
  AssertTablesEqual(*expected, *result);

Review comment (suggestion):
```suggestion
  ASSERT_TRUE(table->Equals(*other_table, options.use_atol(true).atol(1e-3)));
```

Code after:

TEST(TestTableEqualityFloatType, NaN) {
  auto schema = ::arrow::schema({field("f0", int32()), field("f1", float64())});
  auto table = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": NaN}, {"f0": 3, "f1": 6.0}])"});
  auto other_table = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": NaN}, {"f0": 3, "f1": 6.0}])"});
  auto options = EqualOptions::Defaults();

  ASSERT_FALSE(table->Equals(*other_table, options));
  ASSERT_TRUE(table->Equals(*other_table, options.nans_equal(true)));
}

TEST(TestTableEqualityFloatType, Approximate) {
  auto schema = ::arrow::schema({field("f0", int32()), field("f1", float64())});
  auto table = TableFromJSON(
      schema,
      {R"([{"f0": 1, "f1": 4.0001}, {"f0": 2, "f1": 5.0001}, {"f0": 3, "f1": 6.0001}])"});
  auto other_table = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": 5.0}, {"f0": 3, "f1": 6.0}])"});
  auto options = EqualOptions::Defaults();

  ASSERT_FALSE(table->Equals(*other_table, options));

  ASSERT_TRUE(table->Equals(*other_table, options.use_atol(true).atol(1e-3)));

  ASSERT_FALSE(table->Equals(*other_table, options.use_atol(true).atol(1e-5)));
}

TEST(TestTableEqualitySameAddress, NonFloatType) {
  auto schema = ::arrow::schema({field("f0", int32()), field("f1", uint8())});
  auto table = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4}, {"f0": 2, "f1": 5}, {"f0": 3, "f1": 6}])"});
  auto other_table = table;
  auto options = EqualOptions::Defaults();

  ASSERT_TRUE(table->Equals(*other_table, options));
  ASSERT_TRUE(table->Equals(*other_table, options.nans_equal(true)));
}

TEST(TestTableEqualitySameAddress, NestedTypesWithoutFloatType) {
  auto schema = ::arrow::schema(
      {field("f0", int32()), field("f1", struct_({{"f2", utf8()}, {"f3", int64()}}))});
  auto table = TableFromJSON(
      schema,
      {R"([{"f0": 1, "f1": {"f2": "4", "f3": 7}}, {"f0": 2, "f1": {"f2": "5", "f3": 8}}, {"f0": 3, "f1": {"f2" : "6", "f3": 9}}])"});
  auto other_table = table;
  auto options = EqualOptions::Defaults();

  ASSERT_TRUE(table->Equals(*other_table, options));
  ASSERT_TRUE(table->Equals(*other_table, options.nans_equal(true)));