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

Code before:

TEST_F(TestTable, CombineChunksZeroRow) {
  MakeExample1(10);

  ASSERT_OK_AND_ASSIGN(auto table, Table::FromRecordBatches(schema_, {}));
  ASSERT_EQ(0, table->num_rows());

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

  ASSERT_OK_AND_ASSIGN(auto batch, table->CombineChunksToBatch());
  ASSERT_OK_AND_ASSIGN(auto expected, RecordBatch::MakeEmpty(schema_));
  ASSERT_NO_FATAL_FAILURE(AssertBatchesEqual(*expected, *batch, /*verbose=*/true));
}

TEST_F(TestTable, CombineChunks) {
  MakeExample1(10);
  auto batch1 = RecordBatch::Make(schema_, 10, arrays_);

  MakeExample1(15);
  auto batch2 = RecordBatch::Make(schema_, 15, arrays_);

  ASSERT_OK_AND_ASSIGN(auto table, Table::FromRecordBatches({batch1, batch2}));
  for (int i = 0; i < table->num_columns(); ++i) {
    ASSERT_EQ(2, table->column(i)->num_chunks());
  }

  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}));

Review comment (suggestion):
```suggestion
  auto table_same_inf = TableFromJSON(
```

Code after:

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

  ASSERT_TRUE(table->Equals(*other_table, options));
  ASSERT_FALSE(table->Equals(*other_table, options.signed_zeros_equal(false)));
}

TEST(TestTableEqualityFloatType, Infinity) {
  auto schema = ::arrow::schema({field("f0", int32()), field("f1", float64())});
  auto table = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": 5.0}, {"f0": 3, "f1": Inf}])"});
  auto table_different_inf = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": 5.0}, {"f0": 3, "f1": -Inf}])"});
  auto table_same_inf = TableFromJSON(
      schema, {R"([{"f0": 1, "f1": 4.0}, {"f0": 2, "f1": 5.0}, {"f0": 3, "f1": Inf}])"});

  ASSERT_FALSE(table->Equals(*table_different_inf));
  ASSERT_TRUE(table->Equals(*table_same_inf));
}

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)));