-
Notifications
You must be signed in to change notification settings - Fork 3k
Data: Fix Parquet and Avro defaults date/time representation #11811
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
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||
| /* | ||||||
| * 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.iceberg.data; | ||||||
|
|
||||||
| import java.nio.ByteBuffer; | ||||||
| import org.apache.iceberg.types.Type; | ||||||
| import org.apache.iceberg.types.Types; | ||||||
| import org.apache.iceberg.util.ByteBuffers; | ||||||
| import org.apache.iceberg.util.DateTimeUtil; | ||||||
|
|
||||||
| /** Utility methods for working with Iceberg's generic data model */ | ||||||
| public class GenericDataUtil { | ||||||
| private GenericDataUtil() {} | ||||||
|
|
||||||
| /** | ||||||
| * Convert a value from Iceberg's internal data model to the generic data model. | ||||||
| * | ||||||
| * @param type a data type | ||||||
| * @param value value to convert | ||||||
| * @return the value in the generic data model representation | ||||||
| */ | ||||||
| public static Object internalToGeneric(Type type, Object value) { | ||||||
| if (null == value) { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| switch (type.typeId()) { | ||||||
| case DATE: | ||||||
| return DateTimeUtil.dateFromDays((Integer) value); | ||||||
|
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.
Suggested change
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. Value is an
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. My reasoning is similar, but the other way around: As you mentioned, the method accepts an Casting to an
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 agree with Ryan's reasoning. Casting to |
||||||
| case TIME: | ||||||
| return DateTimeUtil.timeFromMicros((Long) value); | ||||||
|
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.
Suggested change
|
||||||
| case TIMESTAMP: | ||||||
| if (((Types.TimestampType) type).shouldAdjustToUTC()) { | ||||||
| return DateTimeUtil.timestamptzFromMicros((Long) value); | ||||||
|
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.
Suggested change
|
||||||
| } else { | ||||||
| return DateTimeUtil.timestampFromMicros((Long) value); | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
| case FIXED: | ||||||
| return ByteBuffers.toByteArray((ByteBuffer) value); | ||||||
| } | ||||||
|
|
||||||
| return value; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import org.apache.iceberg.avro.SupportsRowPosition; | ||
| import org.apache.iceberg.avro.ValueReader; | ||
| import org.apache.iceberg.avro.ValueReaders; | ||
| import org.apache.iceberg.data.GenericDataUtil; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
@@ -97,7 +98,8 @@ public ValueReader<?> record(Type partner, Schema record, List<ValueReader<?>> f | |
|
|
||
| Types.StructType expected = partner.asStructType(); | ||
| List<Pair<Integer, ValueReader<?>>> readPlan = | ||
| ValueReaders.buildReadPlan(expected, record, fieldReaders, idToConstant); | ||
| ValueReaders.buildReadPlan( | ||
| expected, record, fieldReaders, idToConstant, GenericDataUtil::internalToGeneric); | ||
|
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. This change is only needed for the generic data model and not for the internal data model because defaults are already using the correct classes for internal. |
||
|
|
||
| return GenericReaders.struct(readPlan, expected); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This raises a question: should we also convert the constants passed by
idToConstant?I think we should but not yet. The problem is that this conversion probably can't be done twice because of assumptions in the conversion (like casting strings to
UTF8Stringin Spark) so it is worth a separate PR to refactor all of the conversions to be done in one place. That will also be different for each file format so it could get messy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do that in a separate PR indeed 👍