nitinware commented on code in PR #41753:
URL: https://github.com/apache/beam/pull/41753#discussion_r4748673633


##########
sdks/java/io/clickhouse/src/main/java/org/apache/beam/sdk/io/clickhouse/ClickHouseReadSql.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+
+/** Worker-local SQL and bound values; never included in descriptor or 
diagnostic strings. */
+final class ClickHouseReadSql {
+  private final String sql;
+  private final Map<String, Object> parameters;
+
+  private ClickHouseReadSql(String sql, Map<String, Object> parameters) {
+    this.sql = sql;
+    this.parameters = Collections.unmodifiableMap(new 
LinkedHashMap<>(parameters));
+  }
+
+  String sql() {
+    return sql;
+  }
+
+  Map<String, Object> parameters() {
+    return parameters;
+  }
+
+  static ClickHouseReadSql forPartition(
+      ClickHouseReadConfiguration configuration, ClickHouseReadPartition 
partition) {
+    ClickHouseReadRange range = partition.range();
+    String query = configuration.query();
+    if (query != null) {
+      checkArgument(range == null, "Query mode requires a single-query work 
item");
+      return new ClickHouseReadSql(query, Collections.emptyMap());
+    }
+    String table = configuration.table();
+    String column = configuration.partitionColumn();
+    Schema schema = configuration.schema();
+    if (range == null || table == null || column == null || schema == null) {

Review Comment:
   Removed the redundant schema null check, retaining the checks for the range, 
table, and partition column.



##########
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;

Review Comment:
   Added break after finding and validating the partition column. The reader 
and response are still closed by try-with-resources.



-- 
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]

Reply via email to