Cyanty commented on code in PR #9906:
URL: https://github.com/apache/seatunnel/pull/9906#discussion_r2394791081
##########
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:
> The sqltype is come from SeaTunnel. Why it can used in clickhouse sql
directly? We should use `ClickhouseTypeConverter` to convert.
Hi @Hisoka-X , Thank you for the review.
After re-reading the documentation, I see that my implementation only
considered the case where the table already exists, and my handling of
`schema_save_mode` was too simplistic.
I will rework the code to properly handle all modes by using the SeaTunnel
Catalog API. This will allow for more extensive test case coverage. I'll push
the improvements in my next commit. Of course, for me, this might require more
time to read the code and correct the logic.
Thanks again for your guidance and help!
--
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]