Skip to content

Commit dbd8bb8

Browse files
committed
Test Variant read from files
1 parent 0a86015 commit dbd8bb8

File tree

5 files changed

+386
-13
lines changed

5 files changed

+386
-13
lines changed

parquet-avro/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@
133133
<version>${powermock.version}</version>
134134
<scope>test</scope>
135135
</dependency>
136+
<dependency>
137+
<groupId>org.assertj</groupId>
138+
<artifactId>assertj-core</artifactId>
139+
<version>3.25.3</version>
140+
<scope>test</scope>
141+
</dependency>
142+
<dependency>
143+
<groupId>org.junit.jupiter</groupId>
144+
<artifactId>junit-jupiter-params</artifactId>
145+
<version>5.12.2</version>
146+
<scope>test</scope>
147+
</dependency>
136148
</dependencies>
137149

138150
<build>

parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private static Converter newConverter(
395395
}
396396
return newStringConverter(schema, model, parent, validator);
397397
case RECORD:
398-
if (type.getLogicalTypeAnnotation() instanceof LogicalTypeAnnotation.VariantLogicalTypeAnnotation) {
398+
if (type.getName().equals("var") || type.getLogicalTypeAnnotation() instanceof LogicalTypeAnnotation.VariantLogicalTypeAnnotation) {
399399
return new AvroVariantConverter(parent, type.asGroupType(), schema, model);
400400
} else {
401401
return new AvroRecordConverter(parent, type.asGroupType(), schema, model, validator);

parquet-avro/src/main/java/org/apache/parquet/avro/AvroVariantConverter.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.parquet.io.api.GroupConverter;
2828
import org.apache.parquet.schema.GroupType;
2929
import org.apache.parquet.variant.ImmutableMetadata;
30+
import org.apache.parquet.variant.Variant;
3031
import org.apache.parquet.variant.VariantBuilder;
3132
import org.apache.parquet.variant.VariantConverters;
3233

@@ -35,21 +36,21 @@
3536
*/
3637
class AvroVariantConverter extends GroupConverter implements VariantConverters.ParentConverter<VariantBuilder> {
3738
private final ParentValueContainer parent;
38-
private final Schema avroSchema;
39-
private final GenericData model;
40-
private final int metadataPos;
41-
private final int valuePos;
39+
// private final Schema avroSchema;
40+
// private final GenericData model;
41+
// private final int metadataPos;
42+
// private final int valuePos;
4243
private final GroupConverter wrappedConverter;
4344

4445
private VariantBuilder builder = null;
4546
private ImmutableMetadata metadata = null;
4647

4748
AvroVariantConverter(ParentValueContainer parent, GroupType variantGroup, Schema avroSchema, GenericData model) {
4849
this.parent = parent;
49-
this.avroSchema = avroSchema;
50-
this.metadataPos = avroSchema.getField("metadata").pos();
51-
this.valuePos = avroSchema.getField("value").pos();
52-
this.model = model;
50+
// this.avroSchema = avroSchema;
51+
// this.metadataPos = avroSchema.getField("metadata").pos();
52+
// this.valuePos = avroSchema.getField("value").pos();
53+
// this.model = model;
5354
this.wrappedConverter = VariantConverters.newVariantConverter(variantGroup, this::setMetadata, this);
5455
}
5556

@@ -77,10 +78,12 @@ public void end() {
7778

7879
builder.appendNullIfEmpty();
7980

80-
Object record = model.newRecord(null, avroSchema);
81-
model.setField(record, "metadata", metadataPos, metadata.getEncodedBuffer());
82-
model.setField(record, "value", valuePos, builder.encodedValue());
83-
parent.add(record);
81+
Variant variant = builder.build();
82+
parent.add(variant);
83+
// Object record = model.newRecord(null, avroSchema);
84+
// model.setField(record, "metadata", metadataPos, metadata.getEncodedBuffer());
85+
// model.setField(record, "value", valuePos, builder.encodedValue());
86+
// parent.add(record);
8487

8588
this.builder = null;
8689
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.variant;
20+
21+
import com.fasterxml.jackson.core.JsonFactory;
22+
import com.fasterxml.jackson.core.JsonFactoryBuilder;
23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import org.apache.parquet.Preconditions;
26+
27+
28+
public class JsonUtil {
29+
30+
private JsonUtil() {}
31+
32+
private static final JsonFactory FACTORY =
33+
new JsonFactoryBuilder()
34+
.configure(JsonFactory.Feature.INTERN_FIELD_NAMES, false)
35+
.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false)
36+
.build();
37+
private static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);
38+
39+
public static ObjectMapper mapper() {
40+
return MAPPER;
41+
}
42+
43+
public static int getInt(String property, JsonNode node) {
44+
Preconditions.checkArgument(node.has(property), "Cannot parse missing int: %s", property);
45+
JsonNode pNode = node.get(property);
46+
Preconditions.checkArgument(
47+
pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToInt(),
48+
"Cannot parse to an integer value: %s: %s",
49+
property,
50+
pNode);
51+
return pNode.asInt();
52+
}
53+
54+
public static String getString(String property, JsonNode node) {
55+
Preconditions.checkArgument(node.has(property), "Cannot parse missing string: %s", property);
56+
JsonNode pNode = node.get(property);
57+
Preconditions.checkArgument(
58+
pNode != null && !pNode.isNull() && pNode.isTextual(),
59+
"Cannot parse to a string value: %s: %s",
60+
property,
61+
pNode);
62+
return pNode.asText();
63+
}
64+
65+
public static String getStringOrNull(String property, JsonNode node) {
66+
if (!node.has(property)) {
67+
return null;
68+
}
69+
JsonNode pNode = node.get(property);
70+
if (pNode != null && pNode.isNull()) {
71+
return null;
72+
}
73+
return getString(property, node);
74+
}
75+
76+
}

0 commit comments

Comments
 (0)