kriti-sc commented on code in PR #2886:
URL: https://github.com/apache/iggy/pull/2886#discussion_r3204424915


##########
core/connectors/sinks/clickhouse_sink/src/sink.rs:
##########
@@ -0,0 +1,153 @@
+/* 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.
+ */
+
+use crate::body::{build_json_body, build_row_binary_body, build_string_body};
+use crate::{ClickHouseSink, InsertFormat, client::ClickHouseClient};
+use async_trait::async_trait;
+use iggy_connector_sdk::{ConsumedMessage, Error, MessagesMetadata, Sink, 
TopicMetadata};
+use tracing::{debug, error, info, warn};
+
+#[async_trait]
+impl Sink for ClickHouseSink {
+    // ─── open 
────────────────────────────────────────────────────────────────
+
+    async fn open(&mut self) -> Result<(), Error> {
+        info!(
+            "Opening ClickHouse sink connector ID: {} → {}/{} (format: {:?})",
+            self.id, self.config.url, self.config.table, self.insert_format,
+        );
+
+        let client = ClickHouseClient::new(
+            self.config.url.clone(),
+            self.database().to_owned(),
+            self.username(),
+            self.password(),
+            self.timeout(),
+        )?;
+
+        client.ping().await?;
+        info!("ClickHouse sink ID: {} — ping OK", self.id);
+
+        // For RowBinary mode, fetch and validate the table schema at startup.
+        // This fails fast if the table doesn't exist or contains unsupported 
types.
+        if self.insert_format == InsertFormat::RowBinary {
+            let schema = client.fetch_schema(&self.config.table).await?;
+            info!(
+                "ClickHouse sink ID: {} — loaded schema ({} columns) for table 
'{}'",
+                self.id,
+                schema.len(),
+                self.config.table
+            );
+            self.table_schema = Some(schema);
+        }
+
+        self.client = Some(client);
+        Ok(())
+    }
+
+    // ─── consume 
─────────────────────────────────────────────────────────────
+
+    async fn consume(
+        &self,
+        topic_metadata: &TopicMetadata,
+        messages_metadata: MessagesMetadata,
+        messages: Vec<ConsumedMessage>,
+    ) -> Result<(), Error> {
+        if messages.is_empty() {
+            return Ok(());
+        }
+
+        debug!(
+            "ClickHouse sink ID: {} received {} messages from {}/{} partition 
{} offset {}",
+            self.id,
+            messages.len(),
+            topic_metadata.stream,
+            topic_metadata.topic,
+            messages_metadata.partition_id,
+            messages_metadata.current_offset,
+        );
+
+        let client = self.get_client()?;
+        let table = &self.config.table;
+        let format_name = self
+            .insert_format
+            .clickhouse_format_name(self.string_format);
+
+        let body = match self.insert_format {
+            InsertFormat::JsonEachRow => build_json_body(&messages),
+            InsertFormat::RowBinary => {
+                let schema = self.table_schema.as_deref().ok_or_else(|| {
+                    error!("RowBinary mode but table schema is not loaded");
+                    Error::InitError("Table schema not loaded".into())
+                })?;
+                build_row_binary_body(&messages, schema)?
+            }
+            InsertFormat::StringPassthrough => build_string_body(&messages, 
self.string_format),
+        };
+
+        if body.is_empty() {

Review Comment:
   Err does not makes sense here as that would cause the batch to be retried. 
We don't want that because if messages are unserializable, a retry will not 
solve that. 
   
   Changed the `warn!`s to `error!`s. 
   
   lmk @hubcio 



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