|
| 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.StartupMode; |
| 23 | +import org.apache.paimon.schema.TableSchema; |
| 24 | +import org.apache.paimon.table.source.StreamDataTableScan; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * Chain-table-aware extension of {@link FallbackReadFileStoreTable}. Inherits the batch read |
| 30 | + * behavior (partition-level fallback between the current branch and {@link ChainGroupReadTable}), |
| 31 | + * and additionally overrides {@link #newStreamScan()} to return a chain-aware {@link |
| 32 | + * ChainTableStreamScan} that performs a partition-level full load followed by incremental |
| 33 | + * delta-only streaming. |
| 34 | + */ |
| 35 | +public class ChainTableFileStoreTable extends FallbackReadFileStoreTable { |
| 36 | + |
| 37 | + public ChainTableFileStoreTable(FileStoreTable wrapped, FileStoreTable other) { |
| 38 | + super(wrapped, other, true); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public StreamDataTableScan newStreamScan() { |
| 43 | + CoreOptions coreOptions = wrapped.coreOptions(); |
| 44 | + |
| 45 | + StartupMode effectiveMode = coreOptions.startupMode(); |
| 46 | + boolean hasConsumerProgress = |
| 47 | + coreOptions.consumerId() != null && !coreOptions.consumerIgnoreProgress(); |
| 48 | + if (effectiveMode != StartupMode.LATEST_FULL || hasConsumerProgress) { |
| 49 | + String reason = |
| 50 | + describeUnsupportedMode(coreOptions, effectiveMode, hasConsumerProgress); |
| 51 | + throw new UnsupportedOperationException( |
| 52 | + "Chain table streaming read does not support startup mode '" |
| 53 | + + reason |
| 54 | + + "'. " |
| 55 | + + "Chain table streaming only supports the default 'latest-full' mode, which first " |
| 56 | + + "produces a partition-level full result and then continuously reads incremental " |
| 57 | + + "data from the delta branch.\n" |
| 58 | + + "Suggestions:\n" |
| 59 | + + " - To use chain table streaming: remove the explicit scan mode/position settings " |
| 60 | + + "so that the default 'latest-full' mode is used.\n" |
| 61 | + + " - To use standard streaming read without chain table logic: read from a " |
| 62 | + + "specific branch table (e.g., 't$branch_delta') instead of the main table."); |
| 63 | + } |
| 64 | + |
| 65 | + // Inherited other() returns the ChainGroupReadTable directly. |
| 66 | + ChainGroupReadTable chainGroupReadTable = (ChainGroupReadTable) other(); |
| 67 | + |
| 68 | + return new ChainTableStreamScan(chainGroupReadTable); |
| 69 | + } |
| 70 | + |
| 71 | + private static String describeUnsupportedMode( |
| 72 | + CoreOptions coreOptions, StartupMode effectiveMode, boolean hasConsumerProgress) { |
| 73 | + if (hasConsumerProgress) { |
| 74 | + return "consumer-id with existing progress"; |
| 75 | + } |
| 76 | + switch (effectiveMode) { |
| 77 | + case LATEST: |
| 78 | + return "scan.mode=latest"; |
| 79 | + case FROM_SNAPSHOT: |
| 80 | + if (coreOptions.scanSnapshotId() != null) { |
| 81 | + return "scan.snapshot-id=" + coreOptions.scanSnapshotId(); |
| 82 | + } |
| 83 | + if (coreOptions.scanTagName() != null) { |
| 84 | + return "scan.tag-name=" + coreOptions.scanTagName(); |
| 85 | + } |
| 86 | + if (coreOptions.scanWatermark() != null) { |
| 87 | + return "scan.watermark=" + coreOptions.scanWatermark(); |
| 88 | + } |
| 89 | + return "from-snapshot"; |
| 90 | + case FROM_TIMESTAMP: |
| 91 | + if (coreOptions.scanTimestampMills() != null) { |
| 92 | + return "scan.timestamp-millis=" + coreOptions.scanTimestampMills(); |
| 93 | + } |
| 94 | + if (coreOptions.scanTimestamp() != null) { |
| 95 | + return "scan.timestamp=" + coreOptions.scanTimestamp(); |
| 96 | + } |
| 97 | + return "from-timestamp"; |
| 98 | + default: |
| 99 | + return effectiveMode.name().toLowerCase().replace('_', '-'); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public FileStoreTable copy(Map<String, String> dynamicOptions) { |
| 105 | + return new ChainTableFileStoreTable( |
| 106 | + wrapped.copy(dynamicOptions), other().copy(rewriteOtherOptions(dynamicOptions))); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public FileStoreTable copy(TableSchema newTableSchema) { |
| 111 | + return new ChainTableFileStoreTable( |
| 112 | + wrapped.copy(newTableSchema), |
| 113 | + other().copy(newTableSchema.copy(rewriteOtherOptions(newTableSchema.options())))); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public FileStoreTable copyWithoutTimeTravel(Map<String, String> dynamicOptions) { |
| 118 | + return new ChainTableFileStoreTable( |
| 119 | + wrapped.copyWithoutTimeTravel(dynamicOptions), |
| 120 | + other().copyWithoutTimeTravel(rewriteOtherOptions(dynamicOptions))); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public FileStoreTable copyWithLatestSchema() { |
| 125 | + return new ChainTableFileStoreTable( |
| 126 | + wrapped.copyWithLatestSchema(), other().copyWithLatestSchema()); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public FileStoreTable switchToBranch(String branchName) { |
| 131 | + return new ChainTableFileStoreTable(switchWrappedToBranch(branchName), other()); |
| 132 | + } |
| 133 | +} |
0 commit comments