|
| 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 | +package org.apache.beam.sdk.extensions.sql.meta.provider.delta; |
| 19 | + |
| 20 | +import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import org.apache.beam.sdk.extensions.sql.TableUtils; |
| 29 | +import org.apache.beam.sdk.extensions.sql.meta.BeamSqlTableFilter; |
| 30 | +import org.apache.beam.sdk.extensions.sql.meta.DefaultTableFilter; |
| 31 | +import org.apache.beam.sdk.extensions.sql.meta.SchemaBaseBeamTable; |
| 32 | +import org.apache.beam.sdk.extensions.sql.meta.Table; |
| 33 | +import org.apache.beam.sdk.managed.Managed; |
| 34 | +import org.apache.beam.sdk.values.PBegin; |
| 35 | +import org.apache.beam.sdk.values.PCollection; |
| 36 | +import org.apache.beam.sdk.values.POutput; |
| 37 | +import org.apache.beam.sdk.values.Row; |
| 38 | +import org.apache.beam.vendor.calcite.v1_40_0.org.apache.calcite.rex.RexNode; |
| 39 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; |
| 40 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; |
| 41 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 42 | + |
| 43 | +class DeltaTable extends SchemaBaseBeamTable { |
| 44 | + @VisibleForTesting static final String VERSION_FIELD = "version"; |
| 45 | + @VisibleForTesting static final String TIMESTAMP_FIELD = "timestamp"; |
| 46 | + @VisibleForTesting static final String HADOOP_CONFIG_FIELD = "hadoop_config"; |
| 47 | + @VisibleForTesting static final String HADOOP_CONFIG_CAMEL_FIELD = "hadoopConfig"; |
| 48 | + |
| 49 | + static final String BEAM_WRITE_PROPERTY = "beam.write."; |
| 50 | + static final String BEAM_READ_PROPERTY = "beam.read."; |
| 51 | + |
| 52 | + @VisibleForTesting final String tableLocation; |
| 53 | + @VisibleForTesting final @Nullable Long version; |
| 54 | + @VisibleForTesting final @Nullable String timestamp; |
| 55 | + @VisibleForTesting final @Nullable Map<String, String> hadoopConfig; |
| 56 | + |
| 57 | + DeltaTable(Table table) { |
| 58 | + this( |
| 59 | + checkArgumentNotNull( |
| 60 | + table.getLocation(), |
| 61 | + "Delta Lake table location must be specified (catalog-based tables are not supported)."), |
| 62 | + table); |
| 63 | + } |
| 64 | + |
| 65 | + DeltaTable(String tableLocation, Table table) { |
| 66 | + super(table.getSchema()); |
| 67 | + this.schema = table.getSchema(); |
| 68 | + this.tableLocation = tableLocation; |
| 69 | + |
| 70 | + Long parsedVersion = null; |
| 71 | + String parsedTimestamp = null; |
| 72 | + Map<String, String> parsedHadoopConfig = new HashMap<>(); |
| 73 | + |
| 74 | + ObjectNode properties = table.getProperties(); |
| 75 | + for (Map.Entry<String, JsonNode> property : properties.properties()) { |
| 76 | + String key = property.getKey(); |
| 77 | + String lowerKey = key.toLowerCase(); |
| 78 | + JsonNode val = property.getValue(); |
| 79 | + |
| 80 | + if (lowerKey.startsWith(BEAM_WRITE_PROPERTY)) { |
| 81 | + // TODO: Support writing to Delta Lake tables once a Delta Lake sink is |
| 82 | + // available. |
| 83 | + throw new IllegalArgumentException( |
| 84 | + String.format( |
| 85 | + "Beam write property '%s' is not supported. Writing to Delta Lake tables is currently not supported.", |
| 86 | + key)); |
| 87 | + } else if (lowerKey.startsWith(BEAM_READ_PROPERTY)) { |
| 88 | + // none supported yet |
| 89 | + throw new IllegalArgumentException("Unknown Beam read property: " + key); |
| 90 | + } else if (lowerKey.equalsIgnoreCase(VERSION_FIELD)) { |
| 91 | + parsedVersion = parseVersion(val); |
| 92 | + } else if (lowerKey.equalsIgnoreCase(TIMESTAMP_FIELD)) { |
| 93 | + parsedTimestamp = val.asText(); |
| 94 | + } else if (lowerKey.equalsIgnoreCase(HADOOP_CONFIG_FIELD) |
| 95 | + || lowerKey.equalsIgnoreCase(HADOOP_CONFIG_CAMEL_FIELD)) { |
| 96 | + parseHadoopConfig(val, parsedHadoopConfig); |
| 97 | + } else { |
| 98 | + throw new IllegalArgumentException(String.format("Unknown property '%s'", key)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (parsedVersion != null && parsedTimestamp != null) { |
| 103 | + throw new IllegalArgumentException("Cannot set both version and timestamp."); |
| 104 | + } |
| 105 | + |
| 106 | + this.version = parsedVersion; |
| 107 | + this.timestamp = parsedTimestamp; |
| 108 | + this.hadoopConfig = parsedHadoopConfig.isEmpty() ? null : parsedHadoopConfig; |
| 109 | + } |
| 110 | + |
| 111 | + private static Long parseVersion(JsonNode val) { |
| 112 | + if (val.isNumber()) { |
| 113 | + return val.asLong(); |
| 114 | + } |
| 115 | + return Long.parseLong(val.asText()); |
| 116 | + } |
| 117 | + |
| 118 | + private static void parseHadoopConfig(JsonNode val, Map<String, String> targetMap) { |
| 119 | + if (val.isObject()) { |
| 120 | + Map<String, String> map = |
| 121 | + TableUtils.getObjectMapper() |
| 122 | + .convertValue(val, new TypeReference<Map<String, String>>() {}); |
| 123 | + if (map != null) { |
| 124 | + targetMap.putAll(map); |
| 125 | + } |
| 126 | + } else if (val.isTextual()) { |
| 127 | + try { |
| 128 | + Map<String, String> map = |
| 129 | + TableUtils.getObjectMapper() |
| 130 | + .readValue(val.asText(), new TypeReference<Map<String, String>>() {}); |
| 131 | + if (map != null) { |
| 132 | + targetMap.putAll(map); |
| 133 | + } |
| 134 | + } catch (Exception e) { |
| 135 | + throw new IllegalArgumentException("Failed to parse hadoop_config string as JSON", e); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public PCollection<Row> buildIOReader(PBegin begin) { |
| 142 | + return begin |
| 143 | + .apply(Managed.read(Managed.DELTA_LAKE).withConfig(getBaseConfig())) |
| 144 | + .getSinglePCollection(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public PCollection<Row> buildIOReader( |
| 149 | + PBegin begin, BeamSqlTableFilter filters, List<String> fieldNames) { |
| 150 | + // TODO: Support predicate pushdown and column pruning when supported by DeltaIO |
| 151 | + // / Managed Delta |
| 152 | + // Lake source. |
| 153 | + String error = "%s does not support predicate/project push-down, yet non-empty %s is passed."; |
| 154 | + if (!(filters instanceof DefaultTableFilter)) { |
| 155 | + throw new UnsupportedOperationException( |
| 156 | + String.format(error, this.getClass().getName(), "'filters'")); |
| 157 | + } |
| 158 | + if (!fieldNames.isEmpty()) { |
| 159 | + throw new UnsupportedOperationException( |
| 160 | + String.format(error, this.getClass().getName(), "'fieldNames'")); |
| 161 | + } |
| 162 | + return buildIOReader(begin); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public POutput buildIOWriter(PCollection<Row> input) { |
| 167 | + // TODO: Support writing to Delta Lake tables once a Delta Lake sink is |
| 168 | + // available. |
| 169 | + throw new UnsupportedOperationException( |
| 170 | + "Writing to Delta Lake tables is currently not supported."); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public PCollection.IsBounded isBounded() { |
| 175 | + return PCollection.IsBounded.BOUNDED; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public BeamSqlTableFilter constructFilter(List<RexNode> filter) { |
| 180 | + // TODO: Support predicate pushdown when supported by DeltaIO / Managed Delta |
| 181 | + // Lake source. |
| 182 | + return new DefaultTableFilter(filter); |
| 183 | + } |
| 184 | + |
| 185 | + private Map<String, Object> getBaseConfig() { |
| 186 | + ImmutableMap.Builder<String, Object> managedConfigBuilder = ImmutableMap.builder(); |
| 187 | + managedConfigBuilder.put("table", tableLocation); |
| 188 | + if (version != null) { |
| 189 | + managedConfigBuilder.put(VERSION_FIELD, version); |
| 190 | + } |
| 191 | + if (timestamp != null) { |
| 192 | + managedConfigBuilder.put(TIMESTAMP_FIELD, timestamp); |
| 193 | + } |
| 194 | + if (hadoopConfig != null && !hadoopConfig.isEmpty()) { |
| 195 | + managedConfigBuilder.put(HADOOP_CONFIG_FIELD, hadoopConfig); |
| 196 | + } |
| 197 | + return managedConfigBuilder.build(); |
| 198 | + } |
| 199 | +} |
0 commit comments