-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Test Variant implementation with external test cases #3258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import org.apache.parquet.io.api.GroupConverter; | ||
| import org.apache.parquet.schema.GroupType; | ||
| import org.apache.parquet.variant.ImmutableMetadata; | ||
| import org.apache.parquet.variant.Variant; | ||
| import org.apache.parquet.variant.VariantBuilder; | ||
| import org.apache.parquet.variant.VariantConverters; | ||
|
|
||
|
|
@@ -35,21 +36,21 @@ | |
| */ | ||
| class AvroVariantConverter extends GroupConverter implements VariantConverters.ParentConverter<VariantBuilder> { | ||
| private final ParentValueContainer parent; | ||
| private final Schema avroSchema; | ||
| private final GenericData model; | ||
| private final int metadataPos; | ||
| private final int valuePos; | ||
|
aihuaxu marked this conversation as resolved.
|
||
| // 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.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); | ||
| } | ||
|
|
||
|
|
@@ -77,10 +78,12 @@ 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()); | ||
| parent.add(record); | ||
| Variant variant = builder.build(); | ||
| parent.add(variant); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cashmand Would this convert to Variant object here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand the question. Are you saying that we should be setting the Variant object directly here, rather than a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right. Seems we should return Variant rather than
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to confirm with you and I will can make the change.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It sounds fine to me, I'd suggest getting @rdblue to approve. Maybe we want to do something similar on the write side, where I think it also currently expects a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed with @rdblue offline and it should return a variant. Let me work on the fix and also address write side as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By reading the mapping in https://iceberg.apache.org/spec/#avro, actually we are expecting a record of metadata and value in Avro for variant. Looks like that we just need to update the test rather than making the code. @rdblue and @cashmand
|
||
| // Object record = model.newRecord(null, avroSchema); | ||
| // model.setField(record, "metadata", metadataPos, metadata.getEncodedBuffer()); | ||
| // model.setField(record, "value", valuePos, builder.encodedValue()); | ||
| // parent.add(record); | ||
|
|
||
| this.builder = null; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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); | ||
| } | ||
|
|
||
| } |

Uh oh!
There was an error while loading. Please reload this page.