Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions parquet-avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.12.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-databind.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,8 @@ public Optional<Schema> visit(
@Override
public Optional<Schema> visit(
LogicalTypeAnnotation.VariantLogicalTypeAnnotation variantLogicalType) {
String name = parquetGroupType.getName();
List<Schema.Field> fields = new ArrayList<>();
fields.add(new Schema.Field("metadata", Schema.create(Schema.Type.BYTES)));
fields.add(new Schema.Field("value", Schema.create(Schema.Type.BYTES)));
return of(Schema.createRecord(name, null, namespace(name, names), false, fields));
return of(
convertFields(parquetGroupType.getName(), parquetGroupType.getFields(), names));
}
})
.orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.ByteBuffer;
import java.util.function.Consumer;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.generic.GenericData;
import org.apache.parquet.Preconditions;
import org.apache.parquet.io.api.Converter;
Expand All @@ -34,21 +35,27 @@
* Converter for Variant values.
*/
class AvroVariantConverter extends GroupConverter implements VariantConverters.ParentConverter<VariantBuilder> {
private static final Schema VARIANT_SCHEMA = SchemaBuilder.record("VariantRecord")
.fields()
.name("metadata")
.type()
.bytesType()
.noDefault()
.name("value")
.type()
.bytesType()
.noDefault()
.endRecord();

private final ParentValueContainer parent;
private final Schema avroSchema;
private final GenericData model;
private final int metadataPos;
private final int valuePos;
private final GroupConverter wrappedConverter;

private VariantBuilder builder = null;
private ImmutableMetadata metadata = null;

AvroVariantConverter(ParentValueContainer parent, GroupType variantGroup, Schema avroSchema, GenericData model) {
this.parent = parent;
this.avroSchema = avroSchema;
this.metadataPos = avroSchema.getField("metadata").pos();
this.valuePos = avroSchema.getField("value").pos();
this.model = model;
this.wrappedConverter = VariantConverters.newVariantConverter(variantGroup, this::setMetadata, this);
}
Expand Down Expand Up @@ -77,9 +84,9 @@ public void end() {

builder.appendNullIfEmpty();

Object record = model.newRecord(null, avroSchema);
model.setField(record, "metadata", metadataPos, metadata.getEncodedBuffer());
model.setField(record, "value", valuePos, builder.encodedValue());
Object record = model.newRecord(null, VARIANT_SCHEMA);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need this change that we will produce a fixed schema of value and metadata rather than reading the original avro schema.

Also, we also fix the issue that value field may be missing from the schema since that is allowed, i.e., typed_value exists but value doesn't. We don't read value field for the position and the output schema should be fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change the expected Avro schema. We can reject it, but the contract is to use the schema that was passed in. I think the earlier code is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here what we are passing in the shredded schema which may/may not have value or typed_value field.

But we want to generate a variant which maps to a record of value and metadata fields, right?

image

Copy link
Contributor

@cashmand cashmand Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent was the parquet schema should be the shredding schema, but the Avro schema provided for Variant should always be a record of (value, metadata), even if the parquet schema doesn't contain value. So as @rdblue said, we should reject the schema if that isn't what was provided for a Variant column.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cashmand I followed up with Ryan and I got more context around Avro.

For some reader path, seems we are passing AvroSchema as a shredded schema which should not happen. Let me checkout more how that happened. The AvroSchema here should always be a record of (value, metadata).

model.setField(record, "metadata", 0, metadata.getEncodedBuffer());
model.setField(record, "value", 1, builder.encodedValue());
parent.add(record);

this.builder = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.parquet.variant;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.parquet.Preconditions;

public class JsonUtil {

private JsonUtil() {}

private static final JsonFactory FACTORY = new JsonFactoryBuilder()
.configure(JsonFactory.Feature.INTERN_FIELD_NAMES, false)
.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false)
.build();
private static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);

public static ObjectMapper mapper() {
return MAPPER;
}

public static int getInt(String property, JsonNode node) {
Preconditions.checkArgument(node.has(property), "Cannot parse missing int: %s", property);
JsonNode pNode = node.get(property);
Preconditions.checkArgument(
pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToInt(),
"Cannot parse to an integer value: %s: %s",
property,
pNode);
return pNode.asInt();
}

public static String getString(String property, JsonNode node) {
Preconditions.checkArgument(node.has(property), "Cannot parse missing string: %s", property);
JsonNode pNode = node.get(property);
Preconditions.checkArgument(
pNode != null && !pNode.isNull() && pNode.isTextual(),
"Cannot parse to a string value: %s: %s",
property,
pNode);
return pNode.asText();
}

public static String getStringOrNull(String property, JsonNode node) {
if (!node.has(property)) {
return null;
}
JsonNode pNode = node.get(property);
if (pNode != null && pNode.isNull()) {
return null;
}
return getString(property, node);
}
}
Loading