|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.table; |
| 20 | + |
| 21 | +import org.apache.paimon.CoreOptions; |
| 22 | +import org.apache.paimon.CoreOptions.ChangelogProducer; |
| 23 | +import org.apache.paimon.CoreOptions.StartupMode; |
| 24 | +import org.apache.paimon.schema.TableSchema; |
| 25 | +import org.apache.paimon.table.source.ReadBuilder; |
| 26 | +import org.apache.paimon.table.source.ReadBuilderImpl; |
| 27 | +import org.apache.paimon.table.source.StreamDataTableScan; |
| 28 | + |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Chain-table-aware wrapper for {@link FileStoreTable}. Wraps the {@link |
| 33 | + * FallbackReadFileStoreTable} that already handles batch reads via {@link ChainGroupReadTable}, and |
| 34 | + * additionally overrides {@link #newStreamScan()} to return a chain-aware {@link |
| 35 | + * ChainTableStreamScan} that performs a full chain-merge load followed by incremental delta-only |
| 36 | + * streaming. |
| 37 | + * |
| 38 | + * <p>This class exists because {@link FallbackReadFileStoreTable} is a general-purpose fallback |
| 39 | + * mechanism (used by {@code scan.fallback-branch}, {@code scan.primary-branch}, and chain tables). |
| 40 | + * Chain-table-specific streaming logic is isolated here to avoid polluting the generic fallback. |
| 41 | + */ |
| 42 | +public class ChainTableFileStoreTable extends DelegatedFileStoreTable { |
| 43 | + |
| 44 | + public ChainTableFileStoreTable(FileStoreTable wrapped) { |
| 45 | + super(wrapped); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public StreamDataTableScan newStreamScan() { |
| 50 | + CoreOptions coreOptions = wrapped.coreOptions(); |
| 51 | + |
| 52 | + // When the user specifies an explicit starting position (e.g., scan.snapshot-id, |
| 53 | + // scan.timestamp-millis, scan.mode=latest, consumer-id with existing progress), |
| 54 | + // delegate to the standard streaming scan. The chain-table-aware scan only supports |
| 55 | + // the default behavior: full chain-merge load then incremental delta streaming. |
| 56 | + StartupMode effectiveMode = coreOptions.startupMode(); |
| 57 | + boolean hasConsumerProgress = |
| 58 | + coreOptions.consumerId() != null && !coreOptions.consumerIgnoreProgress(); |
| 59 | + if (effectiveMode != StartupMode.LATEST_FULL || hasConsumerProgress) { |
| 60 | + return wrapped.newStreamScan(); |
| 61 | + } |
| 62 | + |
| 63 | + // Navigate through the wrapper hierarchy: |
| 64 | + // wrapped = FallbackReadFileStoreTable |
| 65 | + // other() = ChainGroupReadTable |
| 66 | + FileStoreTable other = ((FallbackReadFileStoreTable) wrapped).other(); |
| 67 | + ChainGroupReadTable chainGroupReadTable = (ChainGroupReadTable) other; |
| 68 | + |
| 69 | + // Chain table streaming read requires a changelog producer on the delta branch. |
| 70 | + // Without changelog, the incremental phase cannot correctly propagate changes. |
| 71 | + FileStoreTable deltaTable = chainGroupReadTable.other(); |
| 72 | + ChangelogProducer changelogProducer = deltaTable.coreOptions().changelogProducer(); |
| 73 | + if (changelogProducer == ChangelogProducer.NONE) { |
| 74 | + throw new IllegalArgumentException( |
| 75 | + "Chain table streaming read requires a changelog producer " |
| 76 | + + "(e.g., 'changelog-producer' = 'input') on the delta branch. " |
| 77 | + + "Currently the delta branch has 'changelog-producer' = 'none'."); |
| 78 | + } |
| 79 | + |
| 80 | + TableSchema schema = ((AbstractFileStoreTable) chainGroupReadTable.wrapped()).tableSchema; |
| 81 | + return new ChainTableStreamScan(chainGroupReadTable, schema); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public ReadBuilder newReadBuilder() { |
| 86 | + return new ReadBuilderImpl(this) { |
| 87 | + @Override |
| 88 | + public StreamDataTableScan newStreamScan() { |
| 89 | + return ChainTableFileStoreTable.this.newStreamScan(); |
| 90 | + } |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public FileStoreTable copy(Map<String, String> dynamicOptions) { |
| 96 | + return new ChainTableFileStoreTable(wrapped.copy(dynamicOptions)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public FileStoreTable copy(TableSchema newTableSchema) { |
| 101 | + return new ChainTableFileStoreTable(wrapped.copy(newTableSchema)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public FileStoreTable copyWithoutTimeTravel(Map<String, String> dynamicOptions) { |
| 106 | + return new ChainTableFileStoreTable(wrapped.copyWithoutTimeTravel(dynamicOptions)); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public FileStoreTable copyWithLatestSchema() { |
| 111 | + return new ChainTableFileStoreTable(wrapped.copyWithLatestSchema()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public FileStoreTable switchToBranch(String branchName) { |
| 116 | + return new ChainTableFileStoreTable(wrapped.switchToBranch(branchName)); |
| 117 | + } |
| 118 | +} |
0 commit comments