Cyanty commented on code in PR #9906:
URL: https://github.com/apache/seatunnel/pull/9906#discussion_r2420157802
##########
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:
> I'm not sure what do you want to do. Could you share some demo?
Thanks.我不确定你想做什么。你能分享一些示例吗?谢谢。
Hi @Hisoka-X , Thank you for your reply.
Use the native ClickHouse `tableSchema` for type mapping in the `INSERT`
sql, instead of using SeaTunnel types. `JSON` columns are now explicitly cast
with `CAST(? AS String)`, all other column types remain as a `?` placeholder.
here is the code change:
```java
// Uses ClickHouse table schema to get type names
String[] typeNames =
Arrays.stream(fieldNames)
.map(name -> tableSchema.getOrDefault(name, ""))
.toArray(String[]::new);
```
The `CAST` for `JSON` is a workaround for older `clickhouse-jdbc`
limitations when `JSON` support was experimental, then this implementation is
somewhat informal, and this might require further discussion.
--
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]