1fanwang opened a new issue, #17913:
URL: https://github.com/apache/iceberg/issues/17913
At commit `6164440663e3f7b1bae92a1a710ca5233755cd7d`, Iceberg's generic ORC
writer accepts a schema containing `struct<>` and writes a file. The generic
ORC reader cannot read that file with the same Iceberg schema.
## Reproducer
Add this test as
`data/src/test/java/org/apache/iceberg/data/orc/TestEmptyStructRepro.java`:
```java
/*
* 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.data.orc;
import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.apache.iceberg.types.Types.NestedField.required;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import org.apache.iceberg.Files;
import org.apache.iceberg.Schema;
import org.apache.iceberg.data.GenericRecord;
import org.apache.iceberg.data.Record;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.FileAppender;
import org.apache.iceberg.orc.ORC;
import org.apache.iceberg.orc.ORCSchemaUtil;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class TestEmptyStructRepro {
@TempDir private Path temp;
private static final Schema SCHEMA =
new Schema(
required(1, "id", Types.LongType.get()),
optional(2, "empty", Types.StructType.of()));
@Test
public void showOrcSchemaConversion() {
System.out.println("ICEBERG SCHEMA: " + SCHEMA);
System.out.println("ORC TYPEDESC : " + ORCSchemaUtil.convert(SCHEMA));
}
@Test
public void writeThenReadEmptyStruct() throws Exception {
GenericRecord rec = GenericRecord.create(SCHEMA);
rec.setField("id", 1L);
rec.setField("empty", GenericRecord.create(Types.StructType.of()));
File testFile = temp.resolve("empty-struct-" + System.nanoTime() +
".orc").toFile();
System.out.println(">>> STEP 1: ORC WRITE");
try (FileAppender<Record> writer =
ORC.write(Files.localOutput(testFile))
.schema(SCHEMA)
.createWriterFunc(GenericOrcWriter::buildWriter)
.build()) {
writer.add(rec);
}
System.out.println(">>> STEP 1 OK: wrote " + testFile.length() + "
bytes");
System.out.println(">>> STEP 2: ORC READ");
List<Record> rows = Lists.newArrayList();
try (CloseableIterable<Record> reader =
ORC.read(Files.localInput(testFile))
.project(SCHEMA)
.createReaderFunc(fileSchema ->
GenericOrcReader.buildReader(SCHEMA, fileSchema))
.build()) {
reader.forEach(rows::add);
}
System.out.println(">>> STEP 2 OK: read rows = " + rows);
}
}
```
Run with JDK 21:
```shell
./gradlew :iceberg-data:test --tests
"org.apache.iceberg.data.orc.TestEmptyStructRepro"
```
The writer succeeds, then reader construction fails:
```text
ICEBERG SCHEMA: table {
1: id: required long
2: empty: optional struct<>
}
ORC TYPEDESC : struct<id:bigint,empty:struct<>>
>>> STEP 1: ORC WRITE
>>> STEP 1 OK: wrote 305 bytes
>>> STEP 2: ORC READ
java.lang.IllegalArgumentException: Missing ORC reader for field empty (2)
at
org.apache.iceberg.orc.OrcValueReaders$StructReader.<init>(OrcValueReaders.java:224)
at
org.apache.iceberg.data.orc.GenericOrcReaders$StructReader.<init>(GenericOrcReaders.java:268)
at
org.apache.iceberg.data.orc.GenericOrcReader.buildReader(GenericOrcReader.java:48)
```
## Expected behavior
`Types.StructType.of()` permits zero fields, and `ORCSchemaUtil.convert()`
emits `struct<>`. The ORC write and read paths should handle that schema
consistently.
The file-format behavior needs a decision:
1. Reject empty structs at the ORC write boundary with an unsupported-schema
error before creating a file.
2. Bind a zero-child struct while reading and return an empty record.
The second option preserves Iceberg's current type behavior. The first may
be more consistent with formats that cannot persist empty structs.
## Related work
* https://github.com/apache/iceberg/pull/11755
* https://github.com/apache/iceberg/pull/15841
* https://github.com/apache/iceberg/issues/17650
* https://github.com/apache/iceberg/pull/17683
* https://github.com/apache/iceberg/issues/16212
* https://github.com/apache/iceberg/issues/14697
* https://issues.apache.org/jira/browse/SPARK-20593
Relevant source:
*
https://github.com/apache/iceberg/blob/6164440663e3f7b1bae92a1a710ca5233755cd7d/api/src/main/java/org/apache/iceberg/types/Types.java#L1014-L1037
*
https://github.com/apache/iceberg/blob/6164440663e3f7b1bae92a1a710ca5233755cd7d/orc/src/main/java/org/apache/iceberg/orc/ORCSchemaUtil.java#L218-L228
*
https://github.com/apache/iceberg/blob/6164440663e3f7b1bae92a1a710ca5233755cd7d/orc/src/main/java/org/apache/iceberg/orc/OrcValueReaders.java#L183-L228
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]