|
45 | 45 | import org.apache.paimon.types.RowType; |
46 | 46 |
|
47 | 47 | import org.apache.parquet.hadoop.ParquetFileReader; |
| 48 | +import org.apache.parquet.schema.LogicalTypeAnnotation; |
48 | 49 | import org.apache.parquet.schema.MessageType; |
| 50 | +import org.apache.parquet.schema.Type; |
49 | 51 | import org.junit.jupiter.api.BeforeEach; |
50 | 52 | import org.junit.jupiter.api.Test; |
51 | 53 | import org.junit.jupiter.api.io.TempDir; |
| 54 | +import org.junit.jupiter.params.ParameterizedTest; |
| 55 | +import org.junit.jupiter.params.provider.ValueSource; |
52 | 56 |
|
53 | 57 | import java.io.IOException; |
54 | 58 | import java.util.ArrayList; |
@@ -545,9 +549,43 @@ protected void verifyShreddingSchema(RowType... expectShreddedTypes) throws IOEx |
545 | 549 | fileIO, file, fileIO.getFileSize(file), new Options())) { |
546 | 550 | MessageType schema = reader.getFooter().getFileMetaData().getSchema(); |
547 | 551 | for (int i = 0; i < expectShreddedTypes.length; i++) { |
548 | | - assertThat(VariantUtils.variantFileType(schema.getType(i))) |
| 552 | + assertThat( |
| 553 | + VariantMetadataUtils.addVariantMetadata( |
| 554 | + VariantUtils.variantFileType(schema.getType(i)))) |
549 | 555 | .isEqualTo(variantShreddingSchema(expectShreddedTypes[i])); |
550 | 556 | } |
551 | 557 | } |
552 | 558 | } |
| 559 | + |
| 560 | + @ParameterizedTest |
| 561 | + @ValueSource(booleans = {false, true}) |
| 562 | + public void testVariantTypeAnnotation(boolean inferShredding) throws Exception { |
| 563 | + Options options = new Options(); |
| 564 | + options.set( |
| 565 | + CoreOptions.VARIANT_INFER_SHREDDING_SCHEMA.key(), String.valueOf(inferShredding)); |
| 566 | + ParquetFileFormat format = createFormat(options); |
| 567 | + RowType writeType = DataTypes.ROW(DataTypes.FIELD(0, "v", DataTypes.VARIANT())); |
| 568 | + |
| 569 | + FormatWriterFactory factory = format.createWriterFactory(writeType); |
| 570 | + writeRows( |
| 571 | + factory, |
| 572 | + GenericRow.of(GenericVariant.fromJson("{\"name\":\"Alice\"}")), |
| 573 | + GenericRow.of(GenericVariant.fromJson("{\"name\":\"Bob\"}"))); |
| 574 | + |
| 575 | + // Verify that the Parquet schema contains LogicalTypeAnnotation.variantType |
| 576 | + try (ParquetFileReader reader = |
| 577 | + ParquetUtil.getParquetReader( |
| 578 | + fileIO, file, fileIO.getFileSize(file), new Options())) { |
| 579 | + MessageType schema = reader.getFooter().getFileMetaData().getSchema(); |
| 580 | + Type variantField = schema.getType(0); |
| 581 | + |
| 582 | + // The variant field should be a group type |
| 583 | + assertThat(variantField.isPrimitive()).isFalse(); |
| 584 | + LogicalTypeAnnotation logicalType = variantField.getLogicalTypeAnnotation(); |
| 585 | + |
| 586 | + // The variant type should have variant annotation |
| 587 | + assertThat(logicalType) |
| 588 | + .isInstanceOf(LogicalTypeAnnotation.VariantLogicalTypeAnnotation.class); |
| 589 | + } |
| 590 | + } |
553 | 591 | } |
0 commit comments