|
42 | 42 | import org.apache.parquet.io.api.Binary;
|
43 | 43 | import org.apache.parquet.io.api.RecordConsumer;
|
44 | 44 | import org.apache.parquet.schema.GroupType;
|
| 45 | +import org.apache.parquet.schema.LogicalTypeAnnotation; |
45 | 46 | import org.apache.parquet.schema.LogicalTypeAnnotation.UUIDLogicalTypeAnnotation;
|
46 | 47 | import org.apache.parquet.schema.MessageType;
|
47 | 48 | import org.apache.parquet.schema.Type;
|
| 49 | +import org.apache.parquet.variant.Variant; |
| 50 | +import org.apache.parquet.variant.VariantValueWriter; |
48 | 51 | import org.slf4j.Logger;
|
49 | 52 | import org.slf4j.LoggerFactory;
|
50 | 53 |
|
@@ -181,9 +184,79 @@ public void write(T record) {
|
181 | 184 | }
|
182 | 185 |
|
183 | 186 | private void writeRecord(GroupType schema, Schema avroSchema, Object record) {
|
184 |
| - recordConsumer.startGroup(); |
185 |
| - writeRecordFields(schema, avroSchema, record); |
186 |
| - recordConsumer.endGroup(); |
| 187 | + if (schema.getLogicalTypeAnnotation() instanceof LogicalTypeAnnotation.VariantLogicalTypeAnnotation) { |
| 188 | + writeVariantFields(schema, avroSchema, record); |
| 189 | + } else { |
| 190 | + recordConsumer.startGroup(); |
| 191 | + writeRecordFields(schema, avroSchema, record); |
| 192 | + recordConsumer.endGroup(); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Return true if schema and avroSchema have the same field names, in the same order. |
| 197 | + private static boolean schemaMatches(GroupType schema, Schema avroSchema) { |
| 198 | + List<Schema.Field> avroFields = avroSchema.getFields(); |
| 199 | + if (schema.getFieldCount() != avroFields.size()) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + for (int i = 0; i < avroFields.size(); i += 1) { |
| 204 | + if (!avroFields.get(i).name().equals(schema.getFieldName(i))) { |
| 205 | + return false; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + private void writeVariantFields(GroupType schema, Schema avroSchema, Object record) { |
| 213 | + List<Type> fields = schema.getFields(); |
| 214 | + List<Schema.Field> avroFields = avroSchema.getFields(); |
| 215 | + |
| 216 | + if (schemaMatches(schema, avroSchema)) { |
| 217 | + // If the Avro schema matches the Parquet schema, the shredding matches and writeRecordFields can be used. |
| 218 | + // writeRecordFields will validate that the field types match. |
| 219 | + recordConsumer.startGroup(); |
| 220 | + writeRecordFields(schema, avroSchema, record); |
| 221 | + recordConsumer.endGroup(); |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + boolean binarySchema = true; |
| 226 | + ByteBuffer metadata = null; |
| 227 | + ByteBuffer value = null; |
| 228 | + // Extract the value and metadata binary. |
| 229 | + for (int index = 0; index < avroFields.size(); index++) { |
| 230 | + Schema.Field avroField = avroFields.get(index); |
| 231 | + Schema fieldSchema = AvroSchemaConverter.getNonNull(avroField.schema()); |
| 232 | + if (!fieldSchema.getType().equals(Schema.Type.BYTES)) { |
| 233 | + binarySchema = false; |
| 234 | + break; |
| 235 | + } |
| 236 | + Type fieldType = fields.get(index); |
| 237 | + if (fieldType.getName().equals("value")) { |
| 238 | + Object valueObj = model.getField(record, avroField.name(), index); |
| 239 | + Preconditions.checkArgument( |
| 240 | + valueObj instanceof ByteBuffer, |
| 241 | + "Expected ByteBuffer for value, but got " + valueObj.getClass()); |
| 242 | + value = (ByteBuffer) valueObj; |
| 243 | + } else if (fieldType.getName().equals("metadata")) { |
| 244 | + Object metadataObj = model.getField(record, avroField.name(), index); |
| 245 | + Preconditions.checkArgument( |
| 246 | + metadataObj instanceof ByteBuffer, |
| 247 | + "Expected metadata to be a ByteBuffer, but got " + metadataObj.getClass()); |
| 248 | + metadata = (ByteBuffer) metadataObj; |
| 249 | + } else { |
| 250 | + binarySchema = false; |
| 251 | + break; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + if (binarySchema) { |
| 256 | + VariantValueWriter.write(recordConsumer, schema, new Variant(value, metadata)); |
| 257 | + } else { |
| 258 | + throw new RuntimeException("Invalid Avro schema for Variant logical type: " + schema.getName()); |
| 259 | + } |
187 | 260 | }
|
188 | 261 |
|
189 | 262 | private void writeRecordFields(GroupType schema, Schema avroSchema, Object record) {
|
|
0 commit comments