Skip to content

Commit

Permalink
Core: Refactor to enable moving Variant interfaces to API. (#12167)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdblue authored Feb 4, 2025
1 parent eb286de commit 2510fba
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 167 deletions.
34 changes: 34 additions & 0 deletions core/src/main/java/org/apache/iceberg/variants/LogicalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.variants;

enum LogicalType {
NULL,
BOOLEAN,
EXACT_NUMERIC,
FLOAT,
DOUBLE,
DATE,
TIMESTAMPTZ,
TIMESTAMPNTZ,
BINARY,
STRING,
ARRAY,
OBJECT
}
103 changes: 103 additions & 0 deletions core/src/main/java/org/apache/iceberg/variants/PhysicalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.variants;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;

public enum PhysicalType {
NULL(LogicalType.NULL, Void.class),
BOOLEAN_TRUE(LogicalType.BOOLEAN, Boolean.class),
BOOLEAN_FALSE(LogicalType.BOOLEAN, Boolean.class),
INT8(LogicalType.EXACT_NUMERIC, Byte.class),
INT16(LogicalType.EXACT_NUMERIC, Short.class),
INT32(LogicalType.EXACT_NUMERIC, Integer.class),
INT64(LogicalType.EXACT_NUMERIC, Long.class),
DOUBLE(LogicalType.DOUBLE, Double.class),
DECIMAL4(LogicalType.EXACT_NUMERIC, BigDecimal.class),
DECIMAL8(LogicalType.EXACT_NUMERIC, BigDecimal.class),
DECIMAL16(LogicalType.EXACT_NUMERIC, BigDecimal.class),
DATE(LogicalType.DATE, Integer.class),
TIMESTAMPTZ(LogicalType.TIMESTAMPTZ, Long.class),
TIMESTAMPNTZ(LogicalType.TIMESTAMPNTZ, Long.class),
FLOAT(LogicalType.FLOAT, Float.class),
BINARY(LogicalType.BINARY, ByteBuffer.class),
STRING(LogicalType.STRING, String.class),
ARRAY(LogicalType.ARRAY, List.class),
OBJECT(LogicalType.OBJECT, Map.class);

private final LogicalType logicalType;
private final Class<?> javaClass;

PhysicalType(LogicalType logicalType, Class<?> javaClass) {
this.logicalType = logicalType;
this.javaClass = javaClass;
}

LogicalType toLogicalType() {
return logicalType;
}

public Class<?> javaClass() {
return javaClass;
}

public static PhysicalType from(int primitiveType) {
switch (primitiveType) {
case Primitives.TYPE_NULL:
return NULL;
case Primitives.TYPE_TRUE:
return BOOLEAN_TRUE;
case Primitives.TYPE_FALSE:
return BOOLEAN_FALSE;
case Primitives.TYPE_INT8:
return INT8;
case Primitives.TYPE_INT16:
return INT16;
case Primitives.TYPE_INT32:
return INT32;
case Primitives.TYPE_INT64:
return INT64;
case Primitives.TYPE_DATE:
return DATE;
case Primitives.TYPE_TIMESTAMPTZ:
return TIMESTAMPTZ;
case Primitives.TYPE_TIMESTAMPNTZ:
return TIMESTAMPNTZ;
case Primitives.TYPE_FLOAT:
return FLOAT;
case Primitives.TYPE_DOUBLE:
return DOUBLE;
case Primitives.TYPE_DECIMAL4:
return DECIMAL4;
case Primitives.TYPE_DECIMAL8:
return DECIMAL8;
case Primitives.TYPE_DECIMAL16:
return DECIMAL16;
case Primitives.TYPE_BINARY:
return BINARY;
case Primitives.TYPE_STRING:
return STRING;
}

throw new UnsupportedOperationException("Unknown primitive physical type: " + primitiveType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.variants.Variants.PhysicalType;
import org.apache.iceberg.variants.Variants.Primitives;

class PrimitiveWrapper<T> implements VariantPrimitive<T> {
private static final byte NULL_HEADER = VariantUtil.primitiveHeader(Primitives.TYPE_NULL);
Expand Down
43 changes: 43 additions & 0 deletions core/src/main/java/org/apache/iceberg/variants/Primitives.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.variants;

class Primitives {
static final int TYPE_NULL = 0;
static final int TYPE_TRUE = 1;
static final int TYPE_FALSE = 2;
static final int TYPE_INT8 = 3;
static final int TYPE_INT16 = 4;
static final int TYPE_INT32 = 5;
static final int TYPE_INT64 = 6;
static final int TYPE_DOUBLE = 7;
static final int TYPE_DECIMAL4 = 8;
static final int TYPE_DECIMAL8 = 9;
static final int TYPE_DECIMAL16 = 10;
static final int TYPE_DATE = 11;
static final int TYPE_TIMESTAMPTZ = 12; // equivalent to timestamptz
static final int TYPE_TIMESTAMPNTZ = 13; // equivalent to timestamp
static final int TYPE_FLOAT = 14;
static final int TYPE_BINARY = 15;
static final int TYPE_STRING = 16;

static final int PRIMITIVE_TYPE_SHIFT = 2;

private Primitives() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ public ByteBuffer buffer() {
return metadata;
}

@Override
public int sizeInBytes() {
return buffer().remaining();
}

@Override
public int writeTo(ByteBuffer buffer, int offset) {
ByteBuffer value = buffer();
VariantUtil.writeBufferAbsolute(buffer, offset, value);
return value.remaining();
}

@Override
public String toString() {
return VariantMetadata.asString(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ static SerializedPrimitive from(ByteBuffer value, int header) {
}

private final ByteBuffer value;
private final Variants.PhysicalType type;
private final PhysicalType type;
private Object primitive = null;

private SerializedPrimitive(ByteBuffer value, int header) {
this.value = value;
this.type = Variants.PhysicalType.from(header >> PRIMITIVE_TYPE_SHIFT);
this.type = PhysicalType.from(header >> PRIMITIVE_TYPE_SHIFT);
}

private Object read() {
Expand Down Expand Up @@ -111,7 +111,7 @@ private Object read() {
}

@Override
public Variants.PhysicalType type() {
public PhysicalType type() {
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private SerializedShortString(ByteBuffer value, int header) {
}

@Override
public Variants.PhysicalType type() {
return Variants.PhysicalType.STRING;
public PhysicalType type() {
return PhysicalType.STRING;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface VariantArray extends VariantValue {
int numElements();

@Override
default Variants.PhysicalType type() {
return Variants.PhysicalType.ARRAY;
default PhysicalType type() {
return PhysicalType.ARRAY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/
package org.apache.iceberg.variants;

import java.nio.ByteBuffer;
import java.util.NoSuchElementException;

/** A variant metadata dictionary. */
public interface VariantMetadata extends Variants.Serialized {
public interface VariantMetadata {
/** Returns the ID for a {@code name} in the dictionary, or -1 if not present. */
int id(String name);

Expand All @@ -35,6 +36,20 @@ public interface VariantMetadata extends Variants.Serialized {
/** Returns the size of the metadata dictionary. */
int dictionarySize();

/** Returns the serialized size in bytes of this value. */
int sizeInBytes();

/**
* Writes this value to the buffer at the given offset, ignoring the buffer's position and limit.
*
* <p>{@link #sizeInBytes()} bytes will be written to the buffer.
*
* @param buffer a ByteBuffer to write serialized metadata into
* @param offset starting offset to write serialized metadata
* @return the number of bytes written
*/
int writeTo(ByteBuffer buffer, int offset);

static String asString(VariantMetadata metadata) {
StringBuilder builder = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public interface VariantObject extends VariantValue {
int numFields();

@Override
default Variants.PhysicalType type() {
return Variants.PhysicalType.OBJECT;
default PhysicalType type() {
return PhysicalType.OBJECT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static int sizeOf(int maxValue) {
}

static byte primitiveHeader(int primitiveType) {
return (byte) (primitiveType << Variants.Primitives.PRIMITIVE_TYPE_SHIFT);
return (byte) (primitiveType << Primitives.PRIMITIVE_TYPE_SHIFT);
}

static byte objectHeader(boolean isLarge, int fieldIdSize, int offsetSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.iceberg.variants;

import java.nio.ByteBuffer;
import org.apache.iceberg.variants.Variants.PhysicalType;

/** A variant value. */
public interface VariantValue {
Expand Down
Loading

0 comments on commit 2510fba

Please sign in to comment.