This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-12525-870209e004de973ea49ce00f9599093821909427 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 113cea2a9c67878dd1f21e2e57f3add0f4603a51 Author: Jast <[email protected]> AuthorDate: Wed Nov 25 12:45:54 2026 +0000 [Improve][API] Use constant-time column lookups in AbstractSchema (#12525) Co-authored-by: jast <[email protected]> Co-authored-by: multica-agent <[email protected]> --- .../api/table/catalog/AbstractSchema.java | 50 +++++++++++- .../api/table/catalog/AbstractSchemaTest.java | 91 ++++++++++++++++++++++ .../schema/TableSchemaKryoSerializationTest.java | 80 +++++++++++++++++++ 3 files changed, 217 insertions(+), 4 deletions(-) diff --git a/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java b/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java index 3ed52733a3..98116e72cb 100644 --- a/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java +++ b/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java @@ -22,12 +22,18 @@ import org.apache.seatunnel.api.table.type.SeaTunnelRowType; import lombok.AccessLevel; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; +import lombok.ToString; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; /** Represent a physical table schema. */ @@ -39,9 +45,22 @@ public class AbstractSchema implements Serializable { @Getter(AccessLevel.PRIVATE) protected final List<String> columnNames; + /** Lazily built column name to index cache, keeps schema lookups constant time. */ + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) + @ToString.Exclude + @EqualsAndHashCode.Exclude + private transient volatile Map<String, Integer> columnIndexCache; + public AbstractSchema(List<Column> columns) { - this.columns = columns; - this.columnNames = columns.stream().map(Column::getName).collect(Collectors.toList()); + // Reject null up front: a null list is a caller bug, and this constructor failed fast + // with an NPE before the defensive copy was introduced. + // Copy the lists so later mutation of the caller's list cannot change this schema or + // invalidate the lazily built lookup caches. + // Kryo populates collections during deserialization. Keep the internal copy mutable; + // getColumns() still exposes a read-only view. + this.columns = new ArrayList<>(Objects.requireNonNull(columns, "columns")); + this.columnNames = this.columns.stream().map(Column::getName).collect(Collectors.toList()); } // Lombok requires a no-arg constructor for @Data annotation to work properly @@ -69,7 +88,8 @@ public class AbstractSchema implements Serializable { } public int indexOf(String columnName) { - return columnNames.indexOf(columnName); + Integer index = getColumnIndexCache().get(columnName); + return index == null ? -1 : index; } public Column getColumn(String columnName) { @@ -77,10 +97,32 @@ public class AbstractSchema implements Serializable { } public boolean contains(String columnName) { - return columnNames.contains(columnName); + return indexOf(columnName) != -1; } public List<Column> getColumns() { return Collections.unmodifiableList(columns); } + + /** + * Builds the name to index cache only after the whole map is ready. Duplicate names keep the + * first column so lookups match the former linear scan semantics. + */ + private Map<String, Integer> getColumnIndexCache() { + Map<String, Integer> cache = columnIndexCache; + if (cache == null) { + synchronized (this) { + cache = columnIndexCache; + if (cache == null) { + Map<String, Integer> indexes = new HashMap<>(columnNames.size()); + for (int i = 0; i < columnNames.size(); i++) { + indexes.putIfAbsent(columnNames.get(i), i); + } + cache = indexes; + columnIndexCache = cache; + } + } + } + return cache; + } } diff --git a/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/catalog/AbstractSchemaTest.java b/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/catalog/AbstractSchemaTest.java new file mode 100644 index 0000000000..af132a0fc8 --- /dev/null +++ b/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/catalog/AbstractSchemaTest.java @@ -0,0 +1,91 @@ +/* + * 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.seatunnel.api.table.catalog; + +import org.apache.seatunnel.api.table.type.BasicType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class AbstractSchemaTest { + + private static Column column(String name) { + return PhysicalColumn.of(name, BasicType.STRING_TYPE, 1L, true, null, ""); + } + + @Test + void testLookups() { + TableSchema schema = + TableSchema.builder() + .column(column("id")) + .column(column("name")) + .column(column("age")) + .build(); + + Assertions.assertEquals(0, schema.indexOf("id")); + Assertions.assertEquals(2, schema.indexOf("age")); + Assertions.assertEquals(-1, schema.indexOf("missing")); + + Assertions.assertSame(schema.getColumns().get(1), schema.getColumn("name")); + Assertions.assertTrue(schema.contains("id")); + Assertions.assertFalse(schema.contains("missing")); + } + + @Test + void testDuplicateColumnNamesKeepFirstMatch() { + // A schema should not contain duplicate names, but if it does the lookup must keep the + // first occurrence, matching the previous linear scan semantics. + List<Column> columns = new ArrayList<>(); + Column first = column("dup"); + columns.add(first); + columns.add(column("other")); + columns.add(column("dup")); + TableSchema schema = new TableSchema(columns, null, null); + + Assertions.assertEquals(0, schema.indexOf("dup")); + Assertions.assertSame(first, schema.getColumn("dup")); + } + + @Test + void testNullColumnsFailsFast() { + // A null columns list is a caller bug; this constructor failed fast with an NPE before + // the defensive copy was introduced and must keep doing so. + Assertions.assertThrows( + NullPointerException.class, () -> new TableSchema(null, null, null)); + } + + @Test + void testDefensivelyCopiesConstructorColumns() { + List<Column> columns = new ArrayList<>(); + columns.add(column("id")); + TableSchema schema = new TableSchema(columns, null, null); + + Assertions.assertEquals(0, schema.indexOf("id")); + + // Mutating the list passed to the constructor must not change the schema or its caches. + columns.add(column("extra")); + Assertions.assertEquals(-1, schema.indexOf("extra")); + Assertions.assertEquals(1, schema.getColumns().size()); + Assertions.assertThrows( + UnsupportedOperationException.class, + () -> schema.getColumns().add(column("another"))); + } +} diff --git a/seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-common/src/test/java/org/apache/seatunnel/translation/flink/schema/TableSchemaKryoSerializationTest.java b/seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-common/src/test/java/org/apache/seatunnel/translation/flink/schema/TableSchemaKryoSerializationTest.java new file mode 100644 index 0000000000..ac019e55d0 --- /dev/null +++ b/seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-common/src/test/java/org/apache/seatunnel/translation/flink/schema/TableSchemaKryoSerializationTest.java @@ -0,0 +1,80 @@ +/* + * 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.seatunnel.translation.flink.schema; + +import org.apache.seatunnel.api.table.catalog.PhysicalColumn; +import org.apache.seatunnel.api.table.catalog.TableSchema; +import org.apache.seatunnel.api.table.type.BasicType; + +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer; +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataOutputSerializer; + +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class TableSchemaKryoSerializationTest { + + @Test + void testPopulatedSchemaRoundTripRebuildsColumnLookups() throws Exception { + TableSchema schema = + TableSchema.builder() + .column( + PhysicalColumn.of( + "id", BasicType.LONG_TYPE, 20L, false, null, null)) + .column( + PhysicalColumn.of( + "name", BasicType.STRING_TYPE, 255L, true, null, null)) + .build(); + // Warm the transient lookup cache before sending the schema between Flink operators. + assertEquals(1, schema.indexOf("name")); + + TableSchema restored = roundTrip(schema); + + assertEquals(schema.getColumns(), restored.getColumns()); + assertEquals(0, restored.indexOf("id")); + assertEquals(1, restored.indexOf("name")); + assertEquals(-1, restored.indexOf("missing")); + assertThrows( + UnsupportedOperationException.class, + () -> restored.getColumns().add(schema.getColumn("id"))); + } + + @Test + void testEmptySchemaRoundTrip() throws Exception { + TableSchema restored = roundTrip(TableSchema.builder().build()); + + assertEquals(Collections.emptyList(), restored.getColumns()); + assertEquals(-1, restored.indexOf("missing")); + assertFalse(restored.contains("missing")); + } + + private static TableSchema roundTrip(TableSchema schema) throws Exception { + KryoSerializer<TableSchema> serializer = + new KryoSerializer<>(TableSchema.class, new ExecutionConfig()); + DataOutputSerializer output = new DataOutputSerializer(128); + serializer.serialize(schema, output); + return serializer.deserialize(new DataInputDeserializer(output.getCopyOfBuffer())); + } +}
