You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "zhangshenghang (via GitHub)" <gi...@apache.org> on 2026/09/28 14:06:09 UTC

[PR] [Improve][API] Use constant-time column lookups in AbstractSchema [seatunnel]

zhangshenghang opened a new pull request, #12525:
URL: https://github.com/apache/seatunnel/pull/12525

   ### Purpose of this pull request
   
   Close #12521.
   
   `AbstractSchema#indexOf`, `#getColumn` and `#contains` scan the `columnNames` list linearly on every call. These lookups sit on hot paths (for example sink writers resolving field indexes per table), and for wide tables with hundreds of columns the repeated O(n) scans add significant overhead.
   
   This PR builds a lazily-initialized name-to-index map (double-checked, thread safe) and uses it for the lookups:
   
   - `indexOf`/`contains` become hash lookups; `getColumn` reuses `indexOf` and keeps its previous semantics (including the exception behavior for missing columns).
   - Duplicate column names keep the first occurrence, matching the former linear-scan semantics.
   - The constructor now takes a defensive copy of the column list (unmodifiable), so external mutation of the caller's list can no longer change the schema or invalidate the cache.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No behavior change is intended. The only observable difference is that mutating the list passed to the schema constructor after construction no longer affects the schema (previously an undocumented aliasing hazard), and `getColumns()` now returns an unmodifiable view of an internal copy.
   
   ### How was this patch tested?
   
   - Added `AbstractSchemaTest` covering lookups, duplicate-name first-match semantics, and the defensive copy.
   - `mvn -pl seatunnel-api test`: 404 tests, all passing.


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][API] Use constant-time column lookups in AbstractSchema [seatunnel]

Posted by "dybyte (via GitHub)" <gi...@apache.org>.
dybyte commented on code in PR #12525:
URL: https://github.com/apache/seatunnel/pull/12525#discussion_r4124957868


##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java:
##########
@@ -69,18 +86,41 @@ public String[] getFieldNames() {
     }
 
     public int indexOf(String columnName) {
-        return columnNames.indexOf(columnName);
+        Integer index = getColumnIndexCache().get(columnName);
+        return index == null ? -1 : index;
     }
 
     public Column getColumn(String columnName) {
         return columns.get(indexOf(columnName));
     }
 
     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;
+    }

Review Comment:
   Is the lock needed? The map is deterministic and immutable after publication, so a racy single-check with the volatile write should be enough.



##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java:
##########
@@ -39,9 +44,21 @@ 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());
+        // Copy the lists so later mutation of the caller's list cannot change this schema or
+        // invalidate the lazily built lookup caches.
+        this.columns =
+                columns == null
+                        ? Collections.emptyList()
+                        : Collections.unmodifiableList(new ArrayList<>(columns));
+        this.columnNames = this.columns.stream().map(Column::getName).collect(Collectors.toList());
     }

Review Comment:
   This used to throw an NPE, but now silently becomes an empty schema. Is that intended? If not, `Objects.requireNonNull` might be safer, since a null here is likely a caller bug.



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][API] Use constant-time column lookups in AbstractSchema [seatunnel]

Posted by "nzw921rx (via GitHub) via commits" <co...@seatunnel.apache.org>.
nzw921rx merged PR #12525:
URL: https://github.com/apache/seatunnel/pull/12525


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][API] Use constant-time column lookups in AbstractSchema [seatunnel]

Posted by "DanielLeens (via GitHub)" <gi...@apache.org>.
DanielLeens commented on PR #12525:
URL: https://github.com/apache/seatunnel/pull/12525#issuecomment-6266766510

   The CI fact cited in my previous review has changed: Build for the current unchanged head is now passing. CI was previously the only remaining blocker; CI is now passing, so my technical conclusion changes to approve. I do not see a new source-side blocker from Daniel's review. This branch is behind the latest dev base, which I have recorded as merge metadata only; because the current head CI is green, that fact alone does not call for an additional source change. I have read-only review controls, so this COMMENT does not change GitHub's displayed review decision; a write-capable maintainer must perform any final approval or dismissal action.


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Improve][API] Use constant-time column lookups in AbstractSchema [seatunnel]

Posted by "nzw921rx (via GitHub) via commits" <co...@seatunnel.apache.org>.
nzw921rx commented on code in PR #12525:
URL: https://github.com/apache/seatunnel/pull/12525#discussion_r4695075990


##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/table/catalog/AbstractSchema.java:
##########
@@ -69,18 +86,41 @@ public String[] getFieldNames() {
     }
 
     public int indexOf(String columnName) {
-        return columnNames.indexOf(columnName);
+        Integer index = getColumnIndexCache().get(columnName);
+        return index == null ? -1 : index;
     }
 
     public Column getColumn(String columnName) {
         return columns.get(indexOf(columnName));
     }
 
     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;
+    }

Review Comment:
   The approach here has minimal performance loss, but it strictly ensures that `cache` is initialized only once. I support adding this lock as it is a safe practice!



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org