liunaijie commented on code in PR #6975:
URL: https://github.com/apache/seatunnel/pull/6975#discussion_r1642184499


##########
seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/source/ClickhouseSourceFactory.java:
##########
@@ -37,11 +61,60 @@ public String factoryIdentifier() {
         return "Clickhouse";
     }
 
+    @Override
+    public <T, SplitT extends SourceSplit, StateT extends Serializable>
+            TableSource<T, SplitT, StateT> 
createSource(TableSourceFactoryContext context) {
+        ReadonlyConfig readonlyConfig = context.getOptions();
+        List<ClickHouseNode> nodes = 
ClickhouseUtil.createNodes(readonlyConfig);
+
+        String sql = readonlyConfig.get(SQL);
+        ClickHouseNode currentServer = 
nodes.get(ThreadLocalRandom.current().nextInt(nodes.size()));
+        try (ClickHouseClient client = 
ClickHouseClient.newInstance(currentServer.getProtocol());
+                ClickHouseResponse response =
+                        client.connect(currentServer)
+                                
.format(ClickHouseFormat.RowBinaryWithNamesAndTypes)
+                                .query(modifySQLToLimit1(sql))
+                                .executeAndWait()) {
+            TableSchema.Builder builder = TableSchema.builder();
+            int columnSize = response.getColumns().size();
+            for (int i = 0; i < columnSize; i++) {
+                String columnName = 
response.getColumns().get(i).getColumnName();
+                SeaTunnelDataType<?> seaTunnelDataType =
+                        TypeConvertUtil.convert(response.getColumns().get(i));
+                builder.column(
+                        PhysicalColumn.of(
+                                columnName, seaTunnelDataType, Long.MAX_VALUE, 
true, null, null));
+            }
+            CatalogTable catalogTable =
+                    CatalogTable.of(
+                            TableIdentifier.of(
+                                    factoryIdentifier(), 
readonlyConfig.get(DATABASE), "default"),
+                            builder.build(),
+                            Collections.emptyMap(),
+                            Collections.emptyList(),
+                            "",
+                            factoryIdentifier());
+            return () ->
+                    (SeaTunnelSource<T, SplitT, StateT>)
+                            new ClickhouseSource(nodes, catalogTable, sql);
+        } catch (ClickHouseException e) {
+            throw new ClickhouseConnectorException(
+                    SeaTunnelAPIErrorCode.CONFIG_VALIDATION_FAILED,
+                    String.format(
+                            "PluginName: %s, PluginType: %s, Message: %s",
+                            factoryIdentifier(), PluginType.SOURCE, 
e.getMessage()));
+        }
+    }
+

Review Comment:
   done, and i check the jdbc code, if only pass `query` will generate the 
`default.default` table won't parse sql.



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