nitinware commented on code in PR #41753: URL: https://github.com/apache/beam/pull/41753#discussion_r4748663258
########## sdks/java/io/clickhouse/src/main/java/org/apache/beam/sdk/io/clickhouse/ClickHouseRowReader.java: ########## @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.io.clickhouse; + +import static org.apache.beam.sdk.io.clickhouse.ClickHouseReadValidationException.check; + +import com.clickhouse.data.ClickHouseColumn; +import com.clickhouse.data.ClickHouseDataType; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.IntFunction; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.values.Row; +import org.joda.time.Instant; + +/** Worker-side binding of result metadata and conversion to the caller's output schema. */ +final class ClickHouseRowReader { + private final Schema schema; + private final List<Binding> bindings; + + private ClickHouseRowReader(Schema schema, List<Binding> bindings) { + this.schema = schema; + this.bindings = bindings; + } + + /** Reject unsupported Beam output types without requiring database metadata. */ + static void validateSchema(Schema schema) { + for (Schema.Field field : schema.getFields()) { + switch (field.getType().getTypeName()) { + case BYTE: + case INT16: + case INT32: + case INT64: + case FLOAT: + case DOUBLE: + case STRING: + case BOOLEAN: + case DATETIME: + break; + default: + throw new ClickHouseReadValidationException( + "Unsupported Beam output type for ClickHouse read: " + field.getType().getTypeName()); + } + } + } + + /** Bind before iterating, so metadata errors are also detected for an empty result. */ + static ClickHouseRowReader bind( + Schema schema, com.clickhouse.client.api.metadata.TableSchema resultSchema) { + validateSchema(schema); + Map<String, Integer> indices = new HashMap<>(); + List<ClickHouseColumn> columns = resultSchema.getColumns(); + for (int i = 0; i < columns.size(); i++) { + String name = columns.get(i).getColumnName(); + check(indices.put(name, i) == null, "Duplicate result column name '%s'", name); Review Comment: Updated the binding Javadoc and public withQuery() documentation to clarify that duplicate result column names are rejected, including columns outside the output schema. ########## sdks/java/io/clickhouse/src/main/java/org/apache/beam/sdk/io/clickhouse/ClickHousePartitionValidator.java: ########## @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.io.clickhouse; + +import static org.apache.beam.sdk.io.clickhouse.ClickHouseReadValidationException.check; + +import com.clickhouse.client.api.Client; +import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; +import com.clickhouse.client.api.query.QueryResponse; +import com.clickhouse.client.api.query.QuerySettings; +import com.clickhouse.data.ClickHouseColumn; +import com.clickhouse.data.ClickHouseDataType; +import com.clickhouse.data.ClickHouseFormat; +import java.io.IOException; + +/** Worker-side validation of the actual database partition column, independent of output schema. */ +final class ClickHousePartitionValidator { + private ClickHousePartitionValidator() {} + + static void validate(Client client, ClickHouseReadConfiguration configuration) + throws IOException { + String table = configuration.table(); + String partitionColumn = configuration.partitionColumn(); + if (table == null || partitionColumn == null) { + throw new IllegalArgumentException( + "Table and partition column are required for metadata validation"); + } + QuerySettings settings = + new QuerySettings().setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes); + try { + ClickHouseReadResources.checkInterrupted(); + try (QueryResponse response = + ClickHouseReadResources.await( + client.query(ClickHouseReadSql.describeTable(table), settings)); + ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response)) { + boolean found = false; + while (true) { + ClickHouseReadResources.checkInterrupted(); + if (!reader.hasNext()) { + break; + } + reader.next(); + String name = reader.getString("name"); + if (partitionColumn.equals(name)) { + ClickHouseColumn actual = ClickHouseColumn.of(name, reader.getString("type")); + validateColumn(actual); + found = true; + } + } + if (!found) { + throw new ClickHouseReadValidationException( + "Partition column '" + partitionColumn + "' is missing from ClickHouse table"); + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("ClickHouse partition metadata validation interrupted", e); + } catch (Exception e) { + throw new IOException("Unable to read ClickHouse partition metadata", e); + } Review Comment: Fixed. ClickHouseReadValidationException is now rethrown directly instead of being wrapped in IOException. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
