-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Spark: Test reading default values in Spark #11832
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 3 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,126 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import static org.apache.iceberg.types.Types.NestedField.optional; | ||
| import static org.apache.iceberg.types.Types.NestedField.required; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.UUID; | ||
| import java.util.stream.Stream; | ||
| import org.apache.iceberg.avro.AvroDataTest; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.util.DateTimeUtil; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| public class TestSchemaParser extends AvroDataTest { | ||
| @Override | ||
| protected void writeAndValidate(Schema schema) throws IOException { | ||
| Schema serialized = SchemaParser.fromJson(SchemaParser.toJson(schema)); | ||
| assertThat(serialized.asStruct()).isEqualTo(schema.asStruct()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSchemaId() { | ||
| Schema schema = new Schema(34, required(1, "id", Types.LongType.get())); | ||
|
|
||
| Schema serialized = SchemaParser.fromJson(SchemaParser.toJson(schema)); | ||
| assertThat(serialized.schemaId()).isEqualTo(schema.schemaId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIdentifierColumns() { | ||
| Schema schema = | ||
| new Schema( | ||
| Lists.newArrayList( | ||
| required(1, "id-1", Types.LongType.get()), | ||
| required(2, "id-2", Types.LongType.get()), | ||
| optional(3, "data", Types.StringType.get())), | ||
| Sets.newHashSet(1, 2)); | ||
|
|
||
| Schema serialized = SchemaParser.fromJson(SchemaParser.toJson(schema)); | ||
| assertThat(serialized.identifierFieldIds()).isEqualTo(Sets.newHashSet(1, 2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDocStrings() { | ||
| Schema schema = | ||
| new Schema( | ||
| required(1, "id", Types.LongType.get(), "unique identifier"), | ||
| Types.NestedField.optional("data") | ||
| .withId(2) | ||
| .ofType(Types.StringType.get()) | ||
| .withDoc("payload") | ||
| .build()); | ||
|
|
||
| Schema serialized = SchemaParser.fromJson(SchemaParser.toJson(schema)); | ||
| assertThat(serialized.findField("id").doc()).isEqualTo("unique identifier"); | ||
| assertThat(serialized.findField("data").doc()).isEqualTo("payload"); | ||
| } | ||
|
|
||
| private static Stream<Arguments> primitiveTypesAndDefaults() { | ||
| return Stream.of( | ||
| Arguments.of(Types.BooleanType.get(), false), | ||
| Arguments.of(Types.IntegerType.get(), 34), | ||
| Arguments.of(Types.LongType.get(), 4900000000L), | ||
| Arguments.of(Types.FloatType.get(), 12.21F), | ||
| Arguments.of(Types.DoubleType.get(), -0.0D), | ||
| Arguments.of(Types.DateType.get(), DateTimeUtil.isoDateToDays("2024-12-17")), | ||
| // Arguments.of(Types.TimeType.get(), DateTimeUtil.isoTimeToMicros("23:59:59.999999")), | ||
| Arguments.of( | ||
| Types.TimestampType.withZone(), | ||
| DateTimeUtil.isoTimestamptzToMicros("2024-12-17T23:59:59.999999+00:00")), | ||
| Arguments.of( | ||
| Types.TimestampType.withoutZone(), | ||
| DateTimeUtil.isoTimestampToMicros("2024-12-17T23:59:59.999999")), | ||
| Arguments.of(Types.StringType.get(), "iceberg"), | ||
| Arguments.of(Types.UUIDType.get(), UUID.randomUUID()), | ||
| Arguments.of( | ||
| Types.FixedType.ofLength(4), ByteBuffer.wrap(new byte[] {0x0a, 0x0b, 0x0c, 0x0d})), | ||
| Arguments.of(Types.BinaryType.get(), ByteBuffer.wrap(new byte[] {0x0a, 0x0b})), | ||
| Arguments.of(Types.DecimalType.of(9, 2), new BigDecimal("12.34"))); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("primitiveTypesAndDefaults") | ||
| public void testPrimitiveTypeDefaultValues(Type.PrimitiveType type, Object defaultValue) { | ||
| Schema schema = | ||
| new Schema( | ||
| required(1, "id", Types.LongType.get()), | ||
| Types.NestedField.required("col_with_default") | ||
| .withId(2) | ||
| .ofType(type) | ||
| .withInitialDefault(defaultValue) | ||
| .withWriteDefault(defaultValue) | ||
| .build()); | ||
|
|
||
| Schema serialized = SchemaParser.fromJson(SchemaParser.toJson(schema)); | ||
| assertThat(serialized.findField("col_with_default").initialDefault()).isEqualTo(defaultValue); | ||
| assertThat(serialized.findField("col_with_default").writeDefault()).isEqualTo(defaultValue); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,13 +27,11 @@ | |
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.file.Path; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.stream.Stream; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.TypeUtil; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
@@ -42,8 +40,8 @@ | |
| import org.apache.iceberg.types.Types.MapType; | ||
| import org.apache.iceberg.types.Types.StructType; | ||
| import org.apache.iceberg.util.DateTimeUtil; | ||
| import org.apache.spark.sql.internal.SQLConf; | ||
| import org.assertj.core.api.Assumptions; | ||
| import org.assertj.core.api.Condition; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
|
|
@@ -285,8 +283,13 @@ public void testMissingRequiredWithoutDefault() { | |
| .build()); | ||
|
|
||
| assertThatThrownBy(() -> writeAndValidate(writeSchema, expectedSchema)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Missing required field: missing_str"); | ||
| .has( | ||
| new Condition<>( | ||
| t -> | ||
| IllegalArgumentException.class.isInstance(t) | ||
| || IllegalArgumentException.class.isInstance(t.getCause()), | ||
| "Expecting a throwable or cause that is an instance of IllegalArgumentException")) | ||
| .hasMessageContaining("Missing required field: missing_str"); | ||
|
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 was needed to validate the reader failure in |
||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -542,44 +545,4 @@ public void testPrimitiveTypeDefaultValues(Type.PrimitiveType type, Object defau | |
|
|
||
| writeAndValidate(writeSchema, readSchema); | ||
| } | ||
|
|
||
| protected void withSQLConf(Map<String, String> conf, Action action) throws IOException { | ||
|
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 was unused. |
||
| SQLConf sqlConf = SQLConf.get(); | ||
|
|
||
| Map<String, String> currentConfValues = Maps.newHashMap(); | ||
| conf.keySet() | ||
| .forEach( | ||
| confKey -> { | ||
| if (sqlConf.contains(confKey)) { | ||
| String currentConfValue = sqlConf.getConfString(confKey); | ||
| currentConfValues.put(confKey, currentConfValue); | ||
| } | ||
| }); | ||
|
|
||
| conf.forEach( | ||
| (confKey, confValue) -> { | ||
| if (SQLConf.isStaticConfigKey(confKey)) { | ||
| throw new RuntimeException("Cannot modify the value of a static config: " + confKey); | ||
| } | ||
| sqlConf.setConfString(confKey, confValue); | ||
| }); | ||
|
|
||
| try { | ||
| action.invoke(); | ||
| } finally { | ||
| conf.forEach( | ||
| (confKey, confValue) -> { | ||
| if (currentConfValues.containsKey(confKey)) { | ||
| sqlConf.setConfString(confKey, currentConfValues.get(confKey)); | ||
| } else { | ||
| sqlConf.unsetConf(confKey); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| protected interface Action { | ||
| void invoke() throws IOException; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.