Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 106 additions & 19 deletions api/src/main/java/org/apache/iceberg/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ public boolean equals(Object o) {
return false;
}

TimestampType timestampType = (TimestampType) o;
return adjustToUTC == timestampType.adjustToUTC;
TimestampType that = (TimestampType) o;
return adjustToUTC == that.adjustToUTC;
}

@Override
Expand Down Expand Up @@ -331,8 +331,8 @@ public boolean equals(Object o) {
return false;
}

FixedType fixedType = (FixedType) o;
return length == fixedType.length;
FixedType that = (FixedType) o;
return length == that.length;
}

@Override
Expand Down Expand Up @@ -415,42 +415,117 @@ public int hashCode() {

public static class NestedField implements Serializable {
public static NestedField optional(int id, String name, Type type) {
return new NestedField(true, id, name, type, null);
return new NestedField(true, id, name, type, null, null);
}

public static NestedField optional(int id, String name, Type type, String doc) {
return new NestedField(true, id, name, type, doc);
return new NestedField(true, id, name, type, null, doc);
}

public static NestedField optional(int id, String name, Type type, Object defaultValue, String doc) {
return new NestedField(true, id, name, type, defaultValue, doc);
}

public static NestedField required(int id, String name, Type type) {
return new NestedField(false, id, name, type, null);
return new NestedField(false, id, name, type, null, null);
}

public static NestedField required(int id, String name, Type type, String doc) {
return new NestedField(false, id, name, type, doc);
return new NestedField(false, id, name, type, null, doc);
}

public static NestedField required(int id, String name, Type type, Object defaultValue, String doc) {
return new NestedField(false, id, name, type, defaultValue, doc);
}

public static NestedField of(int id, boolean isOptional, String name, Type type) {
return new NestedField(isOptional, id, name, type, null);
return new NestedField(isOptional, id, name, type, null, null);
}

public static NestedField of(int id, boolean isOptional, String name, Type type, String doc) {
return new NestedField(isOptional, id, name, type, doc);
return new NestedField(isOptional, id, name, type, null, doc);
}

public static NestedField of(int id, boolean isOptional, String name, Type type, Object defaultValue, String doc) {
return new NestedField(isOptional, id, name, type, defaultValue, doc);
}

private static void validateDefaultValueForRequiredField(Object defaultValue, String fieldName) {
Preconditions.checkArgument(defaultValue != null,
"Cannot create NestedField with a null default for the required field: " + fieldName);
}

private static void validateDefaultValue(Object defaultValue, Type type) {
if (defaultValue == null) {
return;
}
switch (type.typeId()) {
case STRUCT:
Preconditions.checkArgument(defaultValue instanceof Map,
"defaultValue should be a Map from fields names to values, for StructType");
Map<String, Object> defaultStruct = (Map<String, Object>) defaultValue;
if (defaultStruct.isEmpty()) {
return;
}
Preconditions.checkArgument(defaultStruct.size() == type.asStructType().fields().size());
for (String fieldName : defaultStruct.keySet()) {
NestedField.validateDefaultValue(defaultStruct.get(fieldName), type.asStructType().field(fieldName).type);
}
break;

case LIST:
Preconditions.checkArgument(defaultValue instanceof List,
"defaultValue should be an List of Objects, for ListType");
List<Object> defaultList = (List<Object>) defaultValue;
if (defaultList.size() == 0) {
return;
}
defaultList.forEach(dv -> NestedField.validateDefaultValue(dv, type.asListType().elementField.type));
break;

case MAP:
Preconditions.checkArgument(defaultValue instanceof Map,
"defaultValue should be an instance of Map for MapType");
Map<Object, Object> defaultMap = (Map<Object, Object>) defaultValue;
if (defaultMap.isEmpty()) {
return;
}
for (Map.Entry e : defaultMap.entrySet()) {
NestedField.validateDefaultValue(e.getKey(), type.asMapType().keyField.type);
NestedField.validateDefaultValue(e.getValue(), type.asMapType().valueField.type);
}
break;

case FIXED:
case BINARY:
Preconditions.checkArgument(defaultValue instanceof byte[],
"defaultValue should be an instance of byte[] for TypeId.%s, but defaultValue.class = %s",
type.typeId().name(), defaultValue.getClass().getCanonicalName());
break;

default:
Preconditions.checkArgument(type.typeId().javaClass().isInstance(defaultValue),
"defaultValue should be and instance of %s for TypeId.%s, but defaultValue.class = %s",
type.typeId().javaClass(), type.typeId().name(), defaultValue.getClass().getCanonicalName());
}
}

private final boolean isOptional;
private final int id;
private final String name;
private final Type type;
private final Object defaultValue;
private final String doc;

private NestedField(boolean isOptional, int id, String name, Type type, String doc) {
private NestedField(boolean isOptional, int id, String name, Type type, Object defaultValue, String doc) {
Preconditions.checkNotNull(name, "Name cannot be null");
Preconditions.checkNotNull(type, "Type cannot be null");
validateDefaultValue(defaultValue, type);
this.isOptional = isOptional;
this.id = id;
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
this.doc = doc;
}

Expand All @@ -462,7 +537,7 @@ public NestedField asOptional() {
if (isOptional) {
return this;
}
return new NestedField(true, id, name, type, doc);
return new NestedField(true, id, name, type, defaultValue, doc);
}

public boolean isRequired() {
Expand All @@ -473,7 +548,15 @@ public NestedField asRequired() {
if (!isOptional) {
return this;
}
return new NestedField(false, id, name, type, doc);
return new NestedField(false, id, name, type, defaultValue, doc);
}

public boolean hasDefaultValue() {
return defaultValue != null;
}

public Object getDefaultValue() {
return defaultValue;
}

public int fieldId() {
Expand All @@ -496,6 +579,7 @@ public String doc() {
public String toString() {
return String.format("%d: %s: %s %s",
id, name, isOptional ? "optional" : "required", type) +
(hasDefaultValue() ? ", default value: " + defaultValue + ", " : "") +
(doc != null ? " (" + doc + ")" : "");
}

Expand All @@ -514,6 +598,8 @@ public boolean equals(Object o) {
return false;
} else if (!name.equals(that.name)) {
return false;
} else if (!Objects.equals(defaultValue, that.defaultValue)) {
return false;
} else if (!Objects.equals(doc, that.doc)) {
return false;
}
Expand All @@ -522,7 +608,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(NestedField.class, id, isOptional, name, type);
return hasDefaultValue() ? Objects.hash(NestedField.class, id, isOptional, name, type, defaultValue) :
Objects.hash(NestedField.class, id, isOptional, name, type);
}
}

Expand Down Expand Up @@ -741,8 +828,8 @@ public boolean equals(Object o) {
return false;
}

ListType listType = (ListType) o;
return elementField.equals(listType.elementField);
ListType that = (ListType) o;
return elementField.equals(that.elementField);
}

@Override
Expand Down Expand Up @@ -859,11 +946,11 @@ public boolean equals(Object o) {
return false;
}

MapType mapType = (MapType) o;
if (!keyField.equals(mapType.keyField)) {
MapType that = (MapType) o;
if (!keyField.equals(that.keyField)) {
return false;
}
return valueField.equals(mapType.valueField);
return valueField.equals(that.valueField);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.types;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.apache.iceberg.types.Types.NestedField;
import static org.apache.iceberg.types.Types.StructType;

public class TestDefaultValuesForContainerTypes {

static NestedField intFieldType;
static NestedField stringFieldType;
static StructType structType;

@BeforeClass
public static void beforeClass() {
intFieldType = NestedField.optional(0, "optionalIntField", Types.IntegerType.get());
stringFieldType = NestedField.required(1, "requiredStringField", Types.StringType.get());
structType = StructType.of(Arrays.asList(intFieldType, stringFieldType));
}

@Test
public void testStructTypeDefault() {
Map<String, Object> structDefaultvalue = Maps.newHashMap();
structDefaultvalue.put(intFieldType.name(), Integer.valueOf(1));
structDefaultvalue.put(stringFieldType.name(), "two");
NestedField structField = NestedField.optional(2, "optionalStructField", structType, structDefaultvalue, "doc");
Assert.assertTrue(structField.hasDefaultValue());
Assert.assertEquals(structDefaultvalue, structField.getDefaultValue());
}

@Test (expected = IllegalArgumentException.class)
public void testStructTypeDefaultInvalidFieldsTypes() {
Map<String, Object> structDefaultvalue = Maps.newHashMap();
structDefaultvalue.put(intFieldType.name(), "one");
structDefaultvalue.put(stringFieldType.name(), "two");
NestedField.optional(2, "optionalStructField", structType, structDefaultvalue, "doc");
}

@Test (expected = IllegalArgumentException.class)
public void testStructTypeDefaultInvalidNumberFields() {
Map<String, Object> structDefaultvalue = Maps.newHashMap();
structDefaultvalue.put(intFieldType.name(), Integer.valueOf(1));
structDefaultvalue.put(stringFieldType.name(), "two");
structDefaultvalue.put("extraFieldName", "three");
NestedField.optional(2, "optionalStructField", structType, structDefaultvalue, "doc");
}

@Test (expected = IllegalArgumentException.class)
public void testStructTypeDefaultInvalidObjectType() {
List<Object> structDefaultvalue = Lists.newArrayList();
structDefaultvalue.add(Integer.valueOf(1));
structDefaultvalue.add("two");
NestedField.optional(2, "optionalStructField", structType, structDefaultvalue, "doc");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.types;

import org.apache.iceberg.types.Types.NestedField;
import org.junit.Assert;
import org.junit.Test;

import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.apache.iceberg.types.Types.NestedField.required;


public class TestNestedFieldDefaultValues {

private final int id = 1;
private final String fieldName = "fieldName";
private final Type fieldType = Types.IntegerType.get();
private final String doc = "field doc";
private final Integer defaultValue = 100;

@Test
public void testConstructorsValidCases() {
// optional-constructors
Assert.assertFalse(optional(id, fieldName, fieldType).hasDefaultValue());
Assert.assertFalse(optional(id, fieldName, fieldType, doc).hasDefaultValue());
NestedField nestedFieldWithDefault = optional(id, fieldName, fieldType, defaultValue, doc);
Assert.assertTrue(nestedFieldWithDefault.hasDefaultValue());
Assert.assertEquals(defaultValue, nestedFieldWithDefault.getDefaultValue());
nestedFieldWithDefault = optional(id, fieldName, fieldType, defaultValue, null);
Assert.assertTrue(nestedFieldWithDefault.hasDefaultValue());
Assert.assertEquals(defaultValue, nestedFieldWithDefault.getDefaultValue());

// required-constructors
Assert.assertFalse(required(id, fieldName, fieldType).hasDefaultValue());
Assert.assertFalse(required(id, fieldName, fieldType, doc).hasDefaultValue());
Assert.assertFalse(required(id, fieldName, fieldType, null, doc).hasDefaultValue());
nestedFieldWithDefault = required(id, fieldName, fieldType, defaultValue, doc);
Assert.assertTrue(nestedFieldWithDefault.hasDefaultValue());
Assert.assertEquals(defaultValue, nestedFieldWithDefault.getDefaultValue());
nestedFieldWithDefault = required(id, fieldName, fieldType, defaultValue, null);
Assert.assertTrue(nestedFieldWithDefault.hasDefaultValue());
Assert.assertEquals(defaultValue, nestedFieldWithDefault.getDefaultValue());

// of-constructors
Assert.assertFalse(NestedField.of(id, true, fieldName, fieldType).hasDefaultValue());
Assert.assertFalse(NestedField.of(id, true, fieldName, fieldType, doc).hasDefaultValue());
nestedFieldWithDefault = NestedField.of(id, true, fieldName, fieldType, defaultValue, doc);
Assert.assertTrue(nestedFieldWithDefault.hasDefaultValue());
Assert.assertEquals(defaultValue, nestedFieldWithDefault.getDefaultValue());
}

@Test (expected = IllegalArgumentException.class)
public void testOptionalWithInvalidDefaultValueClass() {
// class of default value does not match class of type
Long wrongClassDefaultValue = 100L;
optional(id, fieldName, fieldType, wrongClassDefaultValue, doc);
}

@Test (expected = IllegalArgumentException.class)
public void testReqiredWithInvalidDefaultValueClass() {
// class of default value does not match class of type
Long wrongClassDefaultValue = 100L;
required(id, fieldName, fieldType, wrongClassDefaultValue, doc);
}
}

Loading