Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.spark.source;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.PartitionField;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.StructLike;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.transforms.Transform;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.Decimal;
import org.apache.spark.unsafe.types.UTF8String;
class PartitionKey implements StructLike {
private final PartitionSpec spec;
private final int size;
private final Object[] partitionTuple;
private final Transform[] transforms;
private final Accessor[] accessors;
@SuppressWarnings("unchecked")
PartitionKey(PartitionSpec spec, Schema inputSchema) {
this.spec = spec;
List fields = spec.fields();
this.size = fields.size();
this.partitionTuple = new Object[size];
this.transforms = new Transform[size];
this.accessors = (Accessor[]) Array.newInstance(Accessor.class, size);
Schema schema = spec.schema();
Map> newAccessors = buildAccessors(inputSchema);
for (int i = 0; i < size; i += 1) {
PartitionField field = fields.get(i);
Accessor accessor = newAccessors.get(field.sourceId());
if (accessor == null) {
throw new RuntimeException(
"Cannot build accessor for field: " + schema.findField(field.sourceId()));
}
this.accessors[i] = accessor;
this.transforms[i] = field.transform();
}
}
private PartitionKey(PartitionKey toCopy) {
this.spec = toCopy.spec;
this.size = toCopy.size;
this.partitionTuple = new Object[toCopy.partitionTuple.length];
this.transforms = toCopy.transforms;
this.accessors = toCopy.accessors;
for (int i = 0; i < partitionTuple.length; i += 1) {
this.partitionTuple[i] = defensiveCopyIfNeeded(toCopy.partitionTuple[i]);
}
}
private Object defensiveCopyIfNeeded(Object obj) {
if (obj instanceof UTF8String) {
// bytes backing the UTF8 string might be reused
byte[] bytes = ((UTF8String) obj).getBytes();
return UTF8String.fromBytes(Arrays.copyOf(bytes, bytes.length));
}
return obj;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < partitionTuple.length; i += 1) {
if (i > 0) {
sb.append(", ");
}
sb.append(partitionTuple[i]);
}
sb.append("]");
return sb.toString();
}
PartitionKey copy() {
return new PartitionKey(this);
}
String toPath() {
return spec.partitionToPath(this);
}
@SuppressWarnings("unchecked")
void partition(InternalRow row) {
for (int i = 0; i < partitionTuple.length; i += 1) {
Transform