|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.connector.jdbc.clickhouse.database.dialect; |
| 20 | + |
| 21 | +import org.apache.flink.annotation.Internal; |
| 22 | +import org.apache.flink.connector.jdbc.core.database.dialect.AbstractDialect; |
| 23 | +import org.apache.flink.connector.jdbc.core.database.dialect.JdbcDialectConverter; |
| 24 | +import org.apache.flink.table.types.logical.LogicalTypeRoot; |
| 25 | +import org.apache.flink.table.types.logical.RowType; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.EnumSet; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +import static java.lang.String.format; |
| 34 | + |
| 35 | +/** JDBC dialect for ClickHouse. */ |
| 36 | +@Internal |
| 37 | +public class ClickHouseDialect extends AbstractDialect { |
| 38 | + |
| 39 | + private static final long serialVersionUID = 1L; |
| 40 | + |
| 41 | + // Define MAX/MIN precision of TIMESTAMP type according to ClickHouse docs: |
| 42 | + // https://clickhouse.com/docs/sql-reference/data-types/datetime64 |
| 43 | + private static final int MAX_TIMESTAMP_PRECISION = 9; |
| 44 | + private static final int MIN_TIMESTAMP_PRECISION = 0; |
| 45 | + |
| 46 | + // Define MAX/MIN precision of DECIMAL type according to ClickHouse docs: |
| 47 | + // https://clickhouse.com/docs/sql-reference/data-types/decimal |
| 48 | + private static final int MAX_DECIMAL_PRECISION = 76; |
| 49 | + private static final int MIN_DECIMAL_PRECISION = 1; |
| 50 | + |
| 51 | + @Override |
| 52 | + public JdbcDialectConverter getRowConverter(RowType rowType) { |
| 53 | + return new ClickHouseDialectConverter(rowType); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getLimitClause(long limit) { |
| 58 | + return "LIMIT " + limit; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Optional<String> defaultDriverName() { |
| 63 | + return Optional.of("com.clickhouse.jdbc.ClickHouseDriver"); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String dialectName() { |
| 68 | + return "ClickHouse"; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String quoteIdentifier(String identifier) { |
| 73 | + return identifier; |
| 74 | + } |
| 75 | + |
| 76 | + // ClickHouse does not support Upsert statements |
| 77 | + // Instead you can create a table with ReplacingMergeTree engine; |
| 78 | + // https://clickhouse.com/docs/engines/table-engines/mergetree-family/replacingmergetree |
| 79 | + @Override |
| 80 | + public Optional<String> getUpsertStatement( |
| 81 | + String tableName, String[] fieldNames, String[] uniqueKeyFields) { |
| 82 | + return Optional.empty(); |
| 83 | + } |
| 84 | + |
| 85 | + // ClickHouse pkField cannot be updated via sql |
| 86 | + @Override |
| 87 | + public String getUpdateStatement( |
| 88 | + String tableName, String[] fieldNames, String[] conditionFields) { |
| 89 | + String setClause = |
| 90 | + Arrays.stream(fieldNames) |
| 91 | + .filter(item -> !Arrays.asList(conditionFields).contains(item)) |
| 92 | + .map(f -> format("%s = :%s", quoteIdentifier(f), f)) |
| 93 | + .collect(Collectors.joining(", ")); |
| 94 | + String conditionClause = |
| 95 | + Arrays.stream(conditionFields) |
| 96 | + .map(f -> format("%s = :%s", quoteIdentifier(f), f)) |
| 97 | + .collect(Collectors.joining(" AND ")); |
| 98 | + return "UPDATE " |
| 99 | + + quoteIdentifier(tableName) |
| 100 | + + " SET " |
| 101 | + + setClause |
| 102 | + + " WHERE " |
| 103 | + + conditionClause; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Optional<Range> decimalPrecisionRange() { |
| 108 | + return Optional.of(Range.of(MIN_DECIMAL_PRECISION, MAX_DECIMAL_PRECISION)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Optional<Range> timestampPrecisionRange() { |
| 113 | + return Optional.of(Range.of(MIN_TIMESTAMP_PRECISION, MAX_TIMESTAMP_PRECISION)); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Set<LogicalTypeRoot> supportedTypes() { |
| 118 | + // The data types used in ClickHouse are list at: |
| 119 | + // https://clickhouse.com/docs/sql-reference/data-types |
| 120 | + |
| 121 | + return EnumSet.of( |
| 122 | + LogicalTypeRoot.CHAR, |
| 123 | + LogicalTypeRoot.VARCHAR, |
| 124 | + LogicalTypeRoot.BOOLEAN, |
| 125 | + LogicalTypeRoot.DECIMAL, |
| 126 | + LogicalTypeRoot.TINYINT, |
| 127 | + LogicalTypeRoot.SMALLINT, |
| 128 | + LogicalTypeRoot.INTEGER, |
| 129 | + LogicalTypeRoot.BIGINT, |
| 130 | + LogicalTypeRoot.FLOAT, |
| 131 | + LogicalTypeRoot.DOUBLE, |
| 132 | + LogicalTypeRoot.DATE, |
| 133 | + LogicalTypeRoot.MAP, |
| 134 | + LogicalTypeRoot.ARRAY, |
| 135 | + LogicalTypeRoot.TIME_WITHOUT_TIME_ZONE, |
| 136 | + LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE, |
| 137 | + LogicalTypeRoot.TIMESTAMP_WITH_TIME_ZONE); |
| 138 | + } |
| 139 | +} |
0 commit comments