-
Notifications
You must be signed in to change notification settings - Fork 27
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
[LI] Fix SPARK ORC projection read for deeply nested union schema and… #154
Changes from 2 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,71 @@ | ||||||
/* | ||||||
* 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.orc; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
import java.util.Optional; | ||||||
import java.util.stream.Collectors; | ||||||
import org.apache.iceberg.types.Types; | ||||||
import org.apache.iceberg.types.Types.NestedField; | ||||||
import org.apache.orc.TypeDescription; | ||||||
|
||||||
public class OrcToIcebergVisitorWithPseudoId extends OrcToIcebergVisitor { | ||||||
|
||||||
private static final String PSEUDO_ICEBERG_FIELD_ID = "-1"; | ||||||
|
||||||
@Override | ||||||
public Optional<NestedField> union(TypeDescription union, List<Optional<NestedField>> options) { | ||||||
union.setAttribute(org.apache.iceberg.orc.ORCSchemaUtil.ICEBERG_ID_ATTRIBUTE, PSEUDO_ICEBERG_FIELD_ID); | ||||||
List<Optional<NestedField>> optionsCopy = new ArrayList<>(options); | ||||||
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. nit:
Suggested change
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. Are you saying the guava api? guava doesn't have this api i just check, it only has 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. and, in fact, the next line, i need to insert an element at the start of the List, so it cannot be immutable. |
||||||
optionsCopy.add(0, Optional.of( | ||||||
Types.NestedField.of(Integer.parseInt(PSEUDO_ICEBERG_FIELD_ID), true, | ||||||
ORCSchemaUtil.ICEBERG_UNION_TAG_FIELD_NAME, Types.IntegerType.get()))); | ||||||
return Optional.of( | ||||||
Types.NestedField.of(Integer.parseInt(PSEUDO_ICEBERG_FIELD_ID), true, currentFieldName(), Types.StructType.of( | ||||||
optionsCopy.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList())))); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<NestedField> record(TypeDescription record, List<String> names, | ||||||
List<Optional<NestedField>> fields) { | ||||||
record.setAttribute(org.apache.iceberg.orc.ORCSchemaUtil.ICEBERG_ID_ATTRIBUTE, PSEUDO_ICEBERG_FIELD_ID); | ||||||
return super.record(record, names, fields); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<NestedField> list(TypeDescription array, Optional<NestedField> element) { | ||||||
array.setAttribute(org.apache.iceberg.orc.ORCSchemaUtil.ICEBERG_ID_ATTRIBUTE, PSEUDO_ICEBERG_FIELD_ID); | ||||||
return super.list(array, element); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<NestedField> map(TypeDescription map, Optional<NestedField> key, | ||||||
Optional<NestedField> value) { | ||||||
map.setAttribute(org.apache.iceberg.orc.ORCSchemaUtil.ICEBERG_ID_ATTRIBUTE, PSEUDO_ICEBERG_FIELD_ID); | ||||||
return super.map(map, key, value); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<NestedField> primitive(TypeDescription primitive) { | ||||||
primitive.setAttribute(org.apache.iceberg.orc.ORCSchemaUtil.ICEBERG_ID_ATTRIBUTE, PSEUDO_ICEBERG_FIELD_ID); | ||||||
return super.primitive(primitive); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,7 +20,7 @@ | |||||||||||
package org.apache.iceberg.spark.data; | ||||||||||||
|
||||||||||||
import java.math.BigDecimal; | ||||||||||||
import java.util.Arrays; | ||||||||||||
import java.util.HashMap; | ||||||||||||
import java.util.List; | ||||||||||||
import java.util.Map; | ||||||||||||
import org.apache.iceberg.orc.ORCSchemaUtil; | ||||||||||||
|
@@ -169,38 +169,26 @@ protected void set(InternalRow struct, int pos, Object value) { | |||||||||||
|
||||||||||||
static class UnionReader implements OrcValueReader<Object> { | ||||||||||||
private final OrcValueReader[] readers; | ||||||||||||
private final Type expectedIcebergSchema; | ||||||||||||
private int[] projectedFieldIdsToIdxInReturnedRow; | ||||||||||||
private boolean isTagFieldProjected; | ||||||||||||
private int numOfFieldsInReturnedRow; | ||||||||||||
private final Type expectedType; | ||||||||||||
private Map<Integer, Integer> idxInExpectedSchemaToIdxInReaders; | ||||||||||||
|
||||||||||||
private UnionReader(List<OrcValueReader<?>> readers, Type expected) { | ||||||||||||
this.readers = new OrcValueReader[readers.size()]; | ||||||||||||
for (int i = 0; i < this.readers.length; i += 1) { | ||||||||||||
this.readers[i] = readers.get(i); | ||||||||||||
} | ||||||||||||
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. nit: simplify
Suggested change
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 can change it to
|
||||||||||||
this.expectedIcebergSchema = expected; | ||||||||||||
this.expectedType = expected; | ||||||||||||
|
||||||||||||
if (this.readers.length > 1) { | ||||||||||||
// Creating an integer array to track the mapping between the index of fields to be projected | ||||||||||||
// and the index of the value for the field stored in the returned row, | ||||||||||||
// if the value for a field equals to Integer.MIN_VALUE, it means the value of this field should not be stored | ||||||||||||
// in the returned row | ||||||||||||
this.projectedFieldIdsToIdxInReturnedRow = new int[readers.size()]; | ||||||||||||
Arrays.fill(this.projectedFieldIdsToIdxInReturnedRow, Integer.MIN_VALUE); | ||||||||||||
this.numOfFieldsInReturnedRow = 0; | ||||||||||||
this.isTagFieldProjected = false; | ||||||||||||
|
||||||||||||
for (Types.NestedField expectedStructField : expectedIcebergSchema.asStructType().fields()) { | ||||||||||||
String fieldName = expectedStructField.name(); | ||||||||||||
if (fieldName.equals(ORCSchemaUtil.ICEBERG_UNION_TAG_FIELD_NAME)) { | ||||||||||||
this.isTagFieldProjected = true; | ||||||||||||
this.numOfFieldsInReturnedRow++; | ||||||||||||
continue; | ||||||||||||
idxInExpectedSchemaToIdxInReaders = new HashMap<>(); | ||||||||||||
// Construct fieldIdxInExpectedSchemaToIdxInReaders | ||||||||||||
for (int i = 0; i < expectedType.asStructType().fields().size(); i += 1) { | ||||||||||||
String fieldName = expectedType.asStructType().fields().get(i).name(); | ||||||||||||
if (!fieldName.equals(ORCSchemaUtil.ICEBERG_UNION_TAG_FIELD_NAME)) { | ||||||||||||
int idxInReader = Integer.parseInt(fieldName | ||||||||||||
.substring(ORCSchemaUtil.ICEBERG_UNION_TYPE_FIELD_NAME_PREFIX_LENGTH)); | ||||||||||||
this.idxInExpectedSchemaToIdxInReaders.put(i, idxInReader); | ||||||||||||
} | ||||||||||||
int projectedFieldIndex = Integer.valueOf(fieldName | ||||||||||||
.substring(ORCSchemaUtil.ICEBERG_UNION_TYPE_FIELD_NAME_PREFIX_LENGTH)); | ||||||||||||
this.projectedFieldIdsToIdxInReturnedRow[projectedFieldIndex] = this.numOfFieldsInReturnedRow++; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
@@ -214,18 +202,21 @@ public Object nonNullRead(ColumnVector vector, int row) { | |||||||||||
if (readers.length == 1) { | ||||||||||||
return value; | ||||||||||||
} else { | ||||||||||||
InternalRow struct = new GenericInternalRow(numOfFieldsInReturnedRow); | ||||||||||||
InternalRow struct = new GenericInternalRow(this.expectedType.asStructType().fields().size()); | ||||||||||||
for (int i = 0; i < struct.numFields(); i += 1) { | ||||||||||||
struct.setNullAt(i); | ||||||||||||
} | ||||||||||||
if (this.isTagFieldProjected) { | ||||||||||||
struct.update(0, fieldIndex); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (this.projectedFieldIdsToIdxInReturnedRow[fieldIndex] != Integer.MIN_VALUE) { | ||||||||||||
struct.update(this.projectedFieldIdsToIdxInReturnedRow[fieldIndex], value); | ||||||||||||
for (int i = 0; i < this.expectedType.asStructType().fields().size(); i += 1) { | ||||||||||||
String fieldName = expectedType.asStructType().fields().get(i).name(); | ||||||||||||
if (fieldName.equals(ORCSchemaUtil.ICEBERG_UNION_TAG_FIELD_NAME)) { | ||||||||||||
struct.update(i, fieldIndex); | ||||||||||||
} else { | ||||||||||||
int idxInReader = this.idxInExpectedSchemaToIdxInReaders.get(i); | ||||||||||||
if (idxInReader == fieldIndex) { | ||||||||||||
struct.update(i, value); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return struct; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
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.
can OrcToIcebergVisitorWithPseudoId be merged with OrcToIcebergVisitor? or do we create a new class for a reason?
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.
The
OrcToIcebergVisitor
is an existing native iceberg visitor. What we are doing here is linkedin specific (at least for now), to transform the orc type back to iceberg type which the orc type can contain unions. All these behavior and logic are not standard in native iceberg and we should use a separate linkedin specific subclass for now.