|
19 | 19 | package org.apache.parquet.avro; |
20 | 20 |
|
21 | 21 | import static org.apache.parquet.avro.AvroTestUtil.optional; |
| 22 | +import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64; |
| 23 | +import static org.apache.parquet.schema.Type.Repetition.REQUIRED; |
22 | 24 | import static org.junit.Assert.assertEquals; |
23 | 25 | import static org.junit.Assert.assertNotNull; |
24 | 26 |
|
|
61 | 63 | import org.apache.parquet.conf.ParquetConfiguration; |
62 | 64 | import org.apache.parquet.conf.PlainParquetConfiguration; |
63 | 65 | import org.apache.parquet.example.data.Group; |
| 66 | +import org.apache.parquet.example.data.GroupFactory; |
| 67 | +import org.apache.parquet.example.data.simple.SimpleGroupFactory; |
64 | 68 | import org.apache.parquet.hadoop.ParquetReader; |
65 | 69 | import org.apache.parquet.hadoop.ParquetWriter; |
66 | 70 | import org.apache.parquet.hadoop.api.WriteSupport; |
| 71 | +import org.apache.parquet.hadoop.example.ExampleParquetWriter; |
67 | 72 | import org.apache.parquet.hadoop.example.GroupReadSupport; |
68 | 73 | import org.apache.parquet.hadoop.util.HadoopCodecs; |
69 | 74 | import org.apache.parquet.io.InputFile; |
70 | 75 | import org.apache.parquet.io.LocalInputFile; |
71 | 76 | import org.apache.parquet.io.LocalOutputFile; |
72 | 77 | import org.apache.parquet.io.api.Binary; |
73 | 78 | import org.apache.parquet.io.api.RecordConsumer; |
| 79 | +import org.apache.parquet.schema.LogicalTypeAnnotation; |
| 80 | +import org.apache.parquet.schema.MessageType; |
74 | 81 | import org.apache.parquet.schema.MessageTypeParser; |
| 82 | +import org.apache.parquet.schema.PrimitiveType; |
75 | 83 | import org.junit.Assert; |
76 | 84 | import org.junit.Rule; |
77 | 85 | import org.junit.Test; |
@@ -400,6 +408,55 @@ public void testFixedDecimalValues() throws Exception { |
400 | 408 | Assert.assertEquals("Content should match", expected, records); |
401 | 409 | } |
402 | 410 |
|
| 411 | + @Test |
| 412 | + public void testDecimalInt64Values() throws Exception { |
| 413 | + |
| 414 | + File file = temp.newFile("test_decimal_int64_values.parquet"); |
| 415 | + file.delete(); |
| 416 | + Path path = new Path(file.toString()); |
| 417 | + |
| 418 | + MessageType parquetSchema = new MessageType( |
| 419 | + "test_decimal_int64_values", |
| 420 | + new PrimitiveType(REQUIRED, INT64, "decimal_salary").withLogicalTypeAnnotation(LogicalTypeAnnotation.decimalType(1, 10))); |
| 421 | + |
| 422 | + try (ParquetWriter<Group> writer = |
| 423 | + ExampleParquetWriter.builder(path).withType(parquetSchema).build()) { |
| 424 | + |
| 425 | + GroupFactory factory = new SimpleGroupFactory(parquetSchema); |
| 426 | + |
| 427 | + Group group1 = factory.newGroup(); |
| 428 | + group1.add("decimal_salary", 234L); |
| 429 | + writer.write(group1); |
| 430 | + |
| 431 | + Group group2 = factory.newGroup(); |
| 432 | + group2.add("decimal_salary", 1203L); |
| 433 | + writer.write(group2); |
| 434 | + } |
| 435 | + |
| 436 | + GenericData decimalSupport = new GenericData(); |
| 437 | + decimalSupport.addLogicalTypeConversion(new Conversions.DecimalConversion()); |
| 438 | + |
| 439 | + List<GenericRecord> records = Lists.newArrayList(); |
| 440 | + try (ParquetReader<GenericRecord> reader = AvroParquetReader.<GenericRecord>builder(path) |
| 441 | + .withDataModel(decimalSupport) |
| 442 | + .build()) { |
| 443 | + GenericRecord rec; |
| 444 | + while ((rec = reader.read()) != null) { |
| 445 | + records.add(rec); |
| 446 | + } |
| 447 | + } |
| 448 | + |
| 449 | + Assert.assertEquals("Should read 2 records", 2, records.size()); |
| 450 | + |
| 451 | + Object firstSalary = records.get(0).get("decimal_salary"); |
| 452 | + Object secondSalary = records.get(1).get("decimal_salary"); |
| 453 | + |
| 454 | + Assert.assertTrue( |
| 455 | + "Should be BigDecimal, but is " + firstSalary.getClass(), firstSalary instanceof BigDecimal); |
| 456 | + Assert.assertEquals("Should be 23.4, but is " + firstSalary, new BigDecimal("23.4"), firstSalary); |
| 457 | + Assert.assertEquals("Should be 120.3, but is " + secondSalary, new BigDecimal("120.3"), secondSalary); |
| 458 | + } |
| 459 | + |
403 | 460 | @Test |
404 | 461 | public void testAll() throws Exception { |
405 | 462 | Schema schema = |
|
0 commit comments