Hisoka-X commented on code in PR #9906:
URL: https://github.com/apache/seatunnel/pull/9906#discussion_r2443021083


##########
seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/sink/client/executor/SqlUtils.java:
##########
@@ -17,24 +17,65 @@
 
 package 
org.apache.seatunnel.connectors.seatunnel.clickhouse.sink.client.executor;
 
+import org.apache.seatunnel.shade.com.google.common.collect.Streams;
+
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+
+import com.clickhouse.client.ClickHouseDataType;
+
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.stream.Collectors;
-
-import static java.lang.String.format;
+import java.util.stream.IntStream;
 
 public class SqlUtils {
+    private static final Map<String, String> TYPE_CASTS =
+            new HashMap<String, String>() {
+                {
+                    String CAST_TEMPLATE = "CAST(? AS %s)";
+                    put(ClickHouseDataType.JSON.name(), 
String.format(CAST_TEMPLATE, "String"));
+                }
+            };
+
+    public static String getTypeCast(String clickHouseDataType) {
+        return TYPE_CASTS.getOrDefault(clickHouseDataType, "?");
+    }
+
     public static String quoteIdentifier(String identifier) {
         return "\"" + identifier + "\"";
     }
 
-    public static String getInsertIntoStatement(String tableName, String[] 
fieldNames) {
+    public static String getInsertIntoStatement(
+            String tableName, SeaTunnelRowType rowType, Map<String, String> 
tableSchema) {
+        String[] fieldNames = rowType.getFieldNames();
+        SeaTunnelDataType<?>[] fieldTypes = rowType.getFieldTypes();
         String columns =
                 Arrays.stream(fieldNames)
                         .map(SqlUtils::quoteIdentifier)
                         .collect(Collectors.joining(", "));
+
+        String[] typeNames =
+                IntStream.range(0, fieldNames.length)
+                        .mapToObj(
+                                i ->
+                                        tableSchema.containsKey(fieldNames[i])
+                                                        && 
ClickHouseDataType.JSON
+                                                                .name()
+                                                                
.equalsIgnoreCase(
+                                                                        
tableSchema.get(
+                                                                               
 fieldNames[i]))
+                                                ? 
ClickHouseDataType.JSON.name()
+                                                : 
fieldTypes[i].getSqlType().name())

Review Comment:
   Thanks @Cyanty !



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