|
25 | 25 | import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FLOAT; |
26 | 26 | import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT32; |
27 | 27 | import static org.junit.Assert.assertEquals; |
| 28 | +import static org.junit.Assert.assertFalse; |
| 29 | +import static org.junit.Assert.assertTrue; |
28 | 30 |
|
29 | 31 | import java.io.IOException; |
30 | 32 | import java.nio.ByteBuffer; |
|
44 | 46 | import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainFloatDictionaryValuesWriter; |
45 | 47 | import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainIntegerDictionaryValuesWriter; |
46 | 48 | import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainLongDictionaryValuesWriter; |
| 49 | +import org.apache.parquet.column.values.dictionary.PlainValuesDictionary.PlainBooleanDictionary; |
47 | 50 | import org.apache.parquet.column.values.fallback.FallbackValuesWriter; |
48 | 51 | import org.apache.parquet.column.values.plain.BinaryPlainValuesReader; |
49 | 52 | import org.apache.parquet.column.values.plain.PlainValuesReader; |
@@ -678,6 +681,72 @@ public void testZeroValues() throws IOException { |
678 | 681 | } |
679 | 682 | } |
680 | 683 |
|
| 684 | + @Test |
| 685 | + public void testBooleanDictionary() throws IOException { |
| 686 | + // Create a dictionary page with boolean values (false, true) |
| 687 | + // Bit-packed: bit 0 = false (0), bit 1 = true (1) => byte = 0b00000010 = 0x02 |
| 688 | + BytesInput bytes = BytesInput.from(new byte[] {0x02}); |
| 689 | + DictionaryPage dictionaryPage = new DictionaryPage(bytes, 2, PLAIN); |
| 690 | + |
| 691 | + PlainBooleanDictionary dictionary = new PlainBooleanDictionary(dictionaryPage); |
| 692 | + |
| 693 | + // Verify dictionary decoding |
| 694 | + assertFalse(dictionary.decodeToBoolean(0)); |
| 695 | + assertTrue(dictionary.decodeToBoolean(1)); |
| 696 | + assertEquals(1, dictionary.getMaxId()); |
| 697 | + } |
| 698 | + |
| 699 | + @Test |
| 700 | + public void testBooleanDictionarySingleValue() throws IOException { |
| 701 | + // Test dictionary with only true value |
| 702 | + // Bit-packed: bit 0 = true (1) => byte = 0b00000001 = 0x01 |
| 703 | + BytesInput bytesTrue = BytesInput.from(new byte[] {0x01}); |
| 704 | + DictionaryPage dictionaryPageTrue = new DictionaryPage(bytesTrue, 1, PLAIN); |
| 705 | + |
| 706 | + PlainBooleanDictionary dictionaryTrue = new PlainBooleanDictionary(dictionaryPageTrue); |
| 707 | + |
| 708 | + assertTrue(dictionaryTrue.decodeToBoolean(0)); |
| 709 | + assertEquals(0, dictionaryTrue.getMaxId()); |
| 710 | + |
| 711 | + // Test dictionary with only false value |
| 712 | + // Bit-packed: bit 0 = false (0) => byte = 0b00000000 = 0x00 |
| 713 | + BytesInput bytesFalse = BytesInput.from(new byte[] {0x00}); |
| 714 | + DictionaryPage dictionaryPageFalse = new DictionaryPage(bytesFalse, 1, PLAIN); |
| 715 | + |
| 716 | + PlainBooleanDictionary dictionaryFalse = new PlainBooleanDictionary(dictionaryPageFalse); |
| 717 | + |
| 718 | + assertFalse(dictionaryFalse.decodeToBoolean(0)); |
| 719 | + assertEquals(0, dictionaryFalse.getMaxId()); |
| 720 | + } |
| 721 | + |
| 722 | + @Test |
| 723 | + public void testBooleanDictionaryToString() throws IOException { |
| 724 | + // Bit-packed: bit 0 = false (0), bit 1 = true (1) => byte = 0b00000010 = 0x02 |
| 725 | + BytesInput bytes = BytesInput.from(new byte[] {0x02}); |
| 726 | + DictionaryPage dictionaryPage = new DictionaryPage(bytes, 2, PLAIN); |
| 727 | + |
| 728 | + PlainBooleanDictionary dictionary = new PlainBooleanDictionary(dictionaryPage); |
| 729 | + |
| 730 | + String str = dictionary.toString(); |
| 731 | + Assert.assertTrue(str.contains("PlainBooleanDictionary")); |
| 732 | + Assert.assertTrue(str.contains("0 => false")); |
| 733 | + Assert.assertTrue(str.contains("1 => true")); |
| 734 | + } |
| 735 | + |
| 736 | + @Test |
| 737 | + public void testBooleanDictionaryWithDictionaryEncoding() throws IOException { |
| 738 | + // Test with PLAIN_DICTIONARY encoding (both PLAIN and PLAIN_DICTIONARY should work) |
| 739 | + // Bit-packed: bit 0 = true (1), bit 1 = false (0) => byte = 0b00000001 = 0x01 |
| 740 | + BytesInput bytes = BytesInput.from(new byte[] {0x01}); |
| 741 | + DictionaryPage dictionaryPage = new DictionaryPage(bytes, 2, PLAIN_DICTIONARY); |
| 742 | + |
| 743 | + PlainBooleanDictionary dictionary = new PlainBooleanDictionary(dictionaryPage); |
| 744 | + |
| 745 | + assertEquals(true, dictionary.decodeToBoolean(0)); |
| 746 | + assertEquals(false, dictionary.decodeToBoolean(1)); |
| 747 | + assertEquals(1, dictionary.getMaxId()); |
| 748 | + } |
| 749 | + |
681 | 750 | private DictionaryValuesReader initDicReader(ValuesWriter cw, PrimitiveTypeName type) throws IOException { |
682 | 751 | final DictionaryPage dictionaryPage = cw.toDictPageAndClose().copy(); |
683 | 752 | final ColumnDescriptor descriptor = new ColumnDescriptor(new String[] {"foo"}, type, 0, 0); |
|
0 commit comments