|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending |
| 3 | +// |
| 4 | +package io.deephaven.iceberg.util; |
| 5 | + |
| 6 | +import io.deephaven.annotations.CopyableStyle; |
| 7 | +import io.deephaven.engine.table.TableDefinition; |
| 8 | +import org.apache.iceberg.Snapshot; |
| 9 | +import org.immutables.value.Value; |
| 10 | +import org.immutables.value.Value.Immutable; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.OptionalLong; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class provides instructions intended for reading Iceberg catalogs and tables. The default values documented in |
| 18 | + * this class may change in the future. As such, callers may wish to explicitly set the values. |
| 19 | + */ |
| 20 | +@Immutable |
| 21 | +@CopyableStyle |
| 22 | +public abstract class IcebergReadInstructions { |
| 23 | + /** |
| 24 | + * The default {@link IcebergReadInstructions} to use when reading Iceberg data files. Providing this will use |
| 25 | + * system defaults for cloud provider-specific parameters |
| 26 | + */ |
| 27 | + @SuppressWarnings("unused") |
| 28 | + public static final IcebergReadInstructions DEFAULT = builder().build(); |
| 29 | + |
| 30 | + public static Builder builder() { |
| 31 | + return ImmutableIcebergReadInstructions.builder(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The {@link TableDefinition} to use when reading Iceberg data files. |
| 36 | + */ |
| 37 | + public abstract Optional<TableDefinition> tableDefinition(); |
| 38 | + |
| 39 | + /** |
| 40 | + * The data instructions to use for reading the Iceberg data files (might be S3Instructions or other cloud |
| 41 | + * provider-specific instructions). |
| 42 | + */ |
| 43 | + public abstract Optional<Object> dataInstructions(); |
| 44 | + |
| 45 | + /** |
| 46 | + * A {@link Map map} of rename instructions from Iceberg to Deephaven column names to use when reading the Iceberg |
| 47 | + * data files. |
| 48 | + */ |
| 49 | + public abstract Map<String, String> columnRenames(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Return a copy of this instructions object with the column renames replaced by {@code entries}. |
| 53 | + */ |
| 54 | + public abstract IcebergReadInstructions withColumnRenames(Map<String, ? extends String> entries); |
| 55 | + |
| 56 | + /** |
| 57 | + * The {@link IcebergUpdateMode} mode to use when reading the Iceberg data files. Default is |
| 58 | + * {@link IcebergUpdateMode#staticMode()}. |
| 59 | + */ |
| 60 | + @Value.Default |
| 61 | + public IcebergUpdateMode updateMode() { |
| 62 | + return IcebergUpdateMode.staticMode(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * The identifier of the snapshot to load for reading. If both this and {@link #snapshot()} are provided, the |
| 67 | + * {@link Snapshot#snapshotId()} should match this. Otherwise, only one of them should be provided. If neither is |
| 68 | + * provided, the latest snapshot will be loaded. |
| 69 | + */ |
| 70 | + public abstract OptionalLong snapshotId(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Return a copy of this instructions object with the snapshot ID replaced by {@code value}. |
| 74 | + */ |
| 75 | + public abstract IcebergReadInstructions withSnapshotId(long value); |
| 76 | + |
| 77 | + /** |
| 78 | + * The snapshot to load for reading. If both this and {@link #snapshotId()} are provided, the |
| 79 | + * {@link Snapshot#snapshotId()} should match the {@link #snapshotId()}. Otherwise, only one of them should be |
| 80 | + * provided. If neither is provided, the latest snapshot will be loaded. |
| 81 | + */ |
| 82 | + public abstract Optional<Snapshot> snapshot(); |
| 83 | + |
| 84 | + /** |
| 85 | + * Return a copy of this instructions object with the snapshot replaced by {@code value}. |
| 86 | + */ |
| 87 | + public abstract IcebergReadInstructions withSnapshot(Snapshot value); |
| 88 | + |
| 89 | + public interface Builder { |
| 90 | + Builder tableDefinition(TableDefinition tableDefinition); |
| 91 | + |
| 92 | + Builder dataInstructions(Object s3Instructions); |
| 93 | + |
| 94 | + Builder putColumnRenames(String key, String value); |
| 95 | + |
| 96 | + Builder putAllColumnRenames(Map<String, ? extends String> entries); |
| 97 | + |
| 98 | + Builder updateMode(IcebergUpdateMode updateMode); |
| 99 | + |
| 100 | + Builder snapshotId(long snapshotId); |
| 101 | + |
| 102 | + Builder snapshot(Snapshot snapshot); |
| 103 | + |
| 104 | + IcebergReadInstructions build(); |
| 105 | + } |
| 106 | + |
| 107 | + @Value.Check |
| 108 | + final void checkSnapshotId() { |
| 109 | + if (snapshotId().isPresent() && snapshot().isPresent() && |
| 110 | + snapshotId().getAsLong() != snapshot().get().snapshotId()) { |
| 111 | + throw new IllegalArgumentException("If both snapshotID and snapshot are provided, the snapshot Ids " + |
| 112 | + "must match, found " + snapshotId().getAsLong() + " and " + snapshot().get().snapshotId()); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments