Skip to content

Commit 6a5d0d0

Browse files
authored
GH-3312: Support uuid read converter for parquet thrift (#3313)
1 parent f50dd6c commit 6a5d0d0

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

parquet-thrift/src/main/java/org/apache/parquet/thrift/ThriftRecordConverter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,25 @@ public ByteBuffer readBinary() throws TException {
412412
/**
413413
* converts Binary into Enum
414414
*/
415+
static class FieldUUIDConverter extends PrimitiveConverter {
416+
417+
private final List<TProtocol> events;
418+
419+
public FieldUUIDConverter(List<TProtocol> events, ThriftField field) {
420+
this.events = events;
421+
}
422+
423+
@Override
424+
public void addBinary(final Binary value) {
425+
events.add(new ParquetProtocol("readBinary() uuid") {
426+
@Override
427+
public ByteBuffer readBinary() throws TException {
428+
return ByteBuffer.wrap(value.getBytes());
429+
}
430+
});
431+
}
432+
}
433+
415434
static class FieldEnumConverter extends PrimitiveConverter {
416435

417436
private final List<TProtocol> events;
@@ -960,6 +979,8 @@ private Converter newConverter(List<TProtocol> events, Type type, ThriftField fi
960979
return new FieldStringConverter(events, field);
961980
case ENUM:
962981
return new FieldEnumConverter(events, field);
982+
case UUID:
983+
return new FieldUUIDConverter(events, field);
963984
default:
964985
return new FieldPrimitiveConverter(events, field);
965986
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.thrift;
20+
21+
import static org.junit.Assert.*;
22+
23+
import java.nio.ByteBuffer;
24+
import java.util.ArrayList;
25+
import java.util.UUID;
26+
import org.apache.parquet.io.api.Binary;
27+
import org.apache.parquet.thrift.struct.ThriftField;
28+
import org.apache.parquet.thrift.struct.ThriftType;
29+
import org.apache.thrift.protocol.TProtocol;
30+
import org.junit.Test;
31+
32+
// Test to verify UUID converter support in ThriftRecordConverter.
33+
public class TestUUIDRecordConverterFailure {
34+
35+
@Test
36+
public void testUUIDConverterSupport() {
37+
ThriftType.UUIDType uuidType = new ThriftType.UUIDType();
38+
ThriftField uuidField = new ThriftField("id", (short) 1, ThriftField.Requirement.REQUIRED, uuidType);
39+
40+
UUID testUUID = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
41+
byte[] uuidBytes = uuidToBytes(testUUID);
42+
Binary binary = Binary.fromConstantByteArray(uuidBytes);
43+
44+
ThriftRecordConverter.FieldUUIDConverter uuidConverter =
45+
new ThriftRecordConverter.FieldUUIDConverter(new ArrayList<TProtocol>(), uuidField);
46+
47+
try {
48+
uuidConverter.addBinary(binary);
49+
assertTrue("UUID converter handled binary data", true);
50+
} catch (UnsupportedOperationException e) {
51+
fail("UUID converter should handle binary data, but got: " + e.getMessage());
52+
}
53+
}
54+
55+
private byte[] uuidToBytes(UUID uuid) {
56+
ByteBuffer buffer = ByteBuffer.allocate(16);
57+
buffer.putLong(uuid.getMostSignificantBits());
58+
buffer.putLong(uuid.getLeastSignificantBits());
59+
return buffer.array();
60+
}
61+
}

0 commit comments

Comments
 (0)