snuyanzin commented on a change in pull request #16684: URL: https://github.com/apache/flink/pull/16684#discussion_r757469474
########## File path: flink-connectors/flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/dialect/ClickHouseDialect.java ########## @@ -0,0 +1,160 @@ +/* + * 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. + */ + +package org.apache.flink.connector.jdbc.dialect; + +import org.apache.flink.connector.jdbc.internal.converter.ClickHouseRowConverter; +import org.apache.flink.connector.jdbc.internal.converter.JdbcRowConverter; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.RowType; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static java.lang.String.format; + +/** JDBC dialect for ClickHouse. */ +public class ClickHouseDialect extends AbstractDialect { + + private static final long serialVersionUID = 1L; + + // Define MAX/MIN precision of TIMESTAMP type according to ClickHouse docs: + // https://clickhouse.tech/docs/en/sql-reference/functions/type-conversion-functions + private static final int MAX_TIMESTAMP_PRECISION = 6; + private static final int MIN_TIMESTAMP_PRECISION = 1; + + // Define MAX/MIN precision of DECIMAL type according to ClickHouse docs: + // https://clickhouse.tech/docs/en/sql-reference/functions/type-conversion-functions/ + private static final int MAX_DECIMAL_PRECISION = 65; + private static final int MIN_DECIMAL_PRECISION = 1; + + @Override + public boolean canHandle(String url) { + return url.startsWith("jdbc:clickhouse:"); + } + + @Override + public JdbcRowConverter getRowConverter(RowType rowType) { + return new ClickHouseRowConverter(rowType); + } + + @Override + public Optional<String> defaultDriverName() { + return Optional.of("ru.yandex.clickhouse.ClickHouseDriver"); + } + + @Override + public String quoteIdentifier(String identifier) { + if (identifier.contains(".")) { + int dotIndex = identifier.indexOf("."); + return "`" + + identifier.substring(0, dotIndex) + + "`.`" + + identifier.substring(dotIndex + 1) + + "`"; + } + return "`" + identifier + "`"; + } + + @Override + public Optional<String> getUpsertStatement( + String tableName, String[] fieldNames, String[] uniqueKeyFields) { + return Optional.of(getInsertIntoStatement(tableName, fieldNames)); + } + + @Override + public String getUpdateStatement( + String tableName, String[] fieldNames, String[] conditionFields) { + String setClause = + Arrays.stream(fieldNames) + .map(f -> format("%s = :%s", quoteIdentifier(f), f)) + .collect(Collectors.joining(", ")); + String conditionClause = + Arrays.stream(conditionFields) + .map(f -> format("%s = :%s", quoteIdentifier(f), f)) + .collect(Collectors.joining(" AND ")); + return "ALTER TABLE " + + quoteIdentifier(tableName) + + " UPDATE " + + setClause + + " WHERE " + + conditionClause; + } + + @Override + public String getDeleteStatement(String tableName, String[] conditionFields) { + String conditionClause = + Arrays.stream(conditionFields) + .map(f -> format("%s = :%s", quoteIdentifier(f), f)) + .collect(Collectors.joining(" AND ")); + return "ALTER TABLE " + quoteIdentifier(tableName) + " DELETE WHERE " + conditionClause; + } + + @Override + public String dialectName() { + return "ClickHouse"; + } + + @Override + public int maxDecimalPrecision() { + return MAX_DECIMAL_PRECISION; + } + + @Override + public int minDecimalPrecision() { + return MIN_DECIMAL_PRECISION; + } + + @Override + public int maxTimestampPrecision() { + return MAX_TIMESTAMP_PRECISION; + } + + @Override + public int minTimestampPrecision() { + return MIN_TIMESTAMP_PRECISION; + } + + @Override + public List<LogicalTypeRoot> unsupportedTypes() { Review comment: I fear this should be reworked as https://issues.apache.org/jira/browse/FLINK-24253 introduced some major changes here ... -- 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]
