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

Code before:
  ConfirmGroupNodeRoundtrip("variant", LogicalType::Variant());
}

TEST(TestLogicalTypeSerialization, VariantSpecificationVersion) {
  // Confirm that Variant logical type sets specification_version to 1 in thrift serialization
   auto metadata = PrimitiveNode::Make("metadata", Repetition::REQUIRED, Type::BYTE_ARRAY);
  auto value = PrimitiveNode::Make("value", Repetition::REQUIRED, Type::BYTE_ARRAY);
  NodePtr variant_node = GroupNode::Make("variant", Repetition::REQUIRED, {metadata, value},
                                         LogicalType::Variant());

  std::vector<format::SchemaElement> elements;
  ToParquet(reinterpret_cast<GroupNode*>(variant_node.get()), &elements);

  // Verify that logicalType is set and is VARIANT
  ASSERT_EQ(elements[0].name, "variant");
  ASSERT_TRUE(elements[0].__isset.logicalType)
      << "Variant logical type failed to generate a logicalType in thrift";
  ASSERT_TRUE(elements[0].logicalType.__isset.VARIANT)
      << "Variant logical type did not set VARIANT field in thrift";

  // Verify that specification_version is set to 1
  ASSERT_TRUE(elements[0].logicalType.VARIANT.__isset.specification_version)
      << "VariantType specification_version field is not set";
  ASSERT_EQ(elements[0].logicalType.VARIANT.specification_version, 1)
      << "VariantType specification_version should be 1, got: "
      << static_cast<int>(elements[0].logicalType.VARIANT.specification_version);
}

}  // namespace schema

}  // namespace parquet


Review comment (bug):
```suggestion
  ASSERT_EQ(elements[0].logicalType.VARIANT.specification_version, 1);
```

`ASSERT_EQ` will print the lhs and rhs automatically if the expectation fails, so I'm not sure that this additional logging is useful.

Code after:
  ConfirmGroupNodeRoundtrip("list", LogicalType::List());
  ConfirmGroupNodeRoundtrip("variant", LogicalType::Variant());
}

TEST(TestLogicalTypeSerialization, VariantSpecificationVersion) {
  // Confirm that Variant logical type sets specification_version to expected value in
  // thrift serialization
  constexpr int8_t spec_version = 2;
  auto metadata = PrimitiveNode::Make("metadata", Repetition::REQUIRED, Type::BYTE_ARRAY);
  auto value = PrimitiveNode::Make("value", Repetition::REQUIRED, Type::BYTE_ARRAY);
  NodePtr variant_node =
      GroupNode::Make("variant", Repetition::REQUIRED, {metadata, value},
                      LogicalType::Variant(spec_version));

  // Verify variant logical type
  auto logical_type = variant_node->logical_type();
  ASSERT_TRUE(logical_type->is_variant());
  const auto& variant_type = checked_cast<const VariantLogicalType&>(*logical_type);
  ASSERT_EQ(variant_type.spec_version(), spec_version);

  // Verify thrift serialization
  std::vector<format::SchemaElement> elements;
  ToParquet(reinterpret_cast<GroupNode*>(variant_node.get()), &elements);

  // Verify that logicalType is set and is VARIANT
  ASSERT_EQ(elements[0].name, "variant");
  ASSERT_TRUE(elements[0].__isset.logicalType);
  ASSERT_TRUE(elements[0].logicalType.__isset.VARIANT);

  // Verify that specification_version is set properly
  ASSERT_TRUE(elements[0].logicalType.VARIANT.__isset.specification_version);
  ASSERT_EQ(elements[0].logicalType.VARIANT.specification_version, spec_version);
}

}  // namespace schema

}  // namespace parquet