Unverified Commit 53f8f662 authored by suxinshuo's avatar suxinshuo Committed by GitHub

[Fix-3494][metadata] Fix clickhouse query table description information failure (#3504)

parent 26aa980e
......@@ -38,6 +38,7 @@ public enum ColumnType {
INT("int", "INT NOT NULL"),
JAVA_LANG_LONG("java.lang.Long", "BIGINT"),
LONG("long", "BIGINT NOT NULL"),
BIG_INTEGER("java.math.BigInteger", "BIGINT"),
JAVA_LANG_FLOAT("java.lang.Float", "FLOAT"),
FLOAT("float", "FLOAT NOT NULL"),
JAVA_LANG_DOUBLE("java.lang.Double", "DOUBLE"),
......
......@@ -20,6 +20,10 @@
package org.dinky.metadata.convert;
import org.dinky.data.enums.ColumnType;
import org.dinky.data.model.Column;
import org.dinky.metadata.enums.ClickHouseDataTypeEnum;
import java.util.Objects;
/**
* ClickHouseTypeConvert
......@@ -28,30 +32,20 @@ import org.dinky.data.enums.ColumnType;
*/
public class ClickHouseTypeConvert extends AbstractJdbcTypeConvert {
// Use mysql now,and welcome to fix it.
public ClickHouseTypeConvert() {
this.convertMap.clear();
register("tinyint", ColumnType.BYTE);
register("smallint", ColumnType.SHORT, ColumnType.JAVA_LANG_SHORT);
register("bigint unsigned", ColumnType.DECIMAL);
register("numeric", ColumnType.DECIMAL);
register("decimal", ColumnType.DECIMAL);
register("bigint", ColumnType.LONG, ColumnType.JAVA_LANG_LONG);
register("int unsigned", ColumnType.LONG);
register("float", ColumnType.FLOAT, ColumnType.JAVA_LANG_FLOAT);
register("double", ColumnType.DOUBLE, ColumnType.JAVA_LANG_DOUBLE);
register("boolean", ColumnType.BOOLEAN, ColumnType.JAVA_LANG_BOOLEAN);
register("tinyint(1)", ColumnType.BOOLEAN, ColumnType.JAVA_LANG_BOOLEAN);
register("datetime", ColumnType.TIMESTAMP);
register("date", ColumnType.DATE);
register("time", ColumnType.TIME);
register("char", ColumnType.STRING);
register("text", ColumnType.STRING);
register("binary", ColumnType.BYTES);
register("blob", ColumnType.BYTES);
register("int", ColumnType.INT, ColumnType.INTEGER);
register("mediumint", ColumnType.INT, ColumnType.INTEGER);
register("smallint unsigned", ColumnType.INT, ColumnType.INTEGER);
@Override
public ColumnType convert(Column column) {
if (Objects.isNull(column)) {
return ColumnType.STRING;
}
String type = column.getType();
if (Objects.isNull(type)) {
return ColumnType.STRING;
}
ColumnType columnType = ClickHouseDataTypeEnum.of(type).getColumnType();
if (Objects.nonNull(columnType)) {
return columnType;
}
return ColumnType.STRING;
}
@Override
......
......@@ -27,6 +27,7 @@ import org.dinky.metadata.ast.Clickhouse20CreateTableStatement;
import org.dinky.metadata.config.AbstractJdbcConfig;
import org.dinky.metadata.convert.ClickHouseTypeConvert;
import org.dinky.metadata.convert.ITypeConvert;
import org.dinky.metadata.enums.ClickHouseDataTypeEnum;
import org.dinky.metadata.enums.DriverType;
import org.dinky.metadata.parser.Clickhouse20StatementParser;
import org.dinky.metadata.query.ClickHouseQuery;
......@@ -41,6 +42,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -51,11 +53,14 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.Token;
import lombok.extern.slf4j.Slf4j;
/**
* ClickHouseDriver
*
* @since 2021/7/21 17:14
*/
@Slf4j
public class ClickHouseDriver extends AbstractJdbcDriver {
@Override
......@@ -196,26 +201,8 @@ public class ClickHouseDriver extends AbstractJdbcDriver {
field.setName(columnName);
if (columnList.contains(dbQuery.columnType())) {
String columnType = results.getString(dbQuery.columnType());
if (columnType.indexOf("Nullable") >= 0) {
field.setNullable(true);
columnType = columnType.replaceAll("Nullable\\(", "").replaceAll("\\)", "");
}
if (columnType.contains("(")) {
String type = columnType.replaceAll("\\(.*\\)", "");
if (!columnType.contains(",")) {
Integer length = Integer.valueOf(columnType.replaceAll("\\D", ""));
field.setLength(length);
} else {
// some database does not have precision
if (dbQuery.precision() != null) {
// 例如浮点类型的长度和精度是一样的,decimal(10,2)
field.setLength(results.getInt(dbQuery.precision()));
}
}
field.setType(type);
} else {
field.setType(columnType);
}
field.setNullable(ClickHouseDataTypeEnum.isNullable(columnType));
field.setType(columnType);
}
if (columnList.contains(dbQuery.columnComment())
&& Asserts.isNotNull(results.getString(dbQuery.columnComment()))) {
......@@ -223,42 +210,27 @@ public class ClickHouseDriver extends AbstractJdbcDriver {
results.getString(dbQuery.columnComment()).replaceAll("\"|'", "");
field.setComment(columnComment);
}
if (columnList.contains(dbQuery.columnLength())) {
int length = results.getInt(dbQuery.columnLength());
if (!results.wasNull()) {
field.setLength(length);
}
}
if (columnList.contains(dbQuery.characterSet())) {
field.setCharacterSet(results.getString(dbQuery.characterSet()));
}
if (columnList.contains(dbQuery.collation())) {
field.setCollation(results.getString(dbQuery.collation()));
}
if (columnList.contains(dbQuery.columnPosition())) {
field.setPosition(results.getInt(dbQuery.columnPosition()));
}
if (columnList.contains(dbQuery.precision())) {
field.setPrecision(results.getInt(dbQuery.precision()));
}
if (columnList.contains(dbQuery.scale())) {
field.setScale(results.getInt(dbQuery.scale()));
}
if (columnList.contains(dbQuery.defaultValue())) {
field.setDefaultValue(results.getString(dbQuery.defaultValue()));
ClickHouseDataTypeEnum clickHouseDataTypeEnum = ClickHouseDataTypeEnum.of(field.getType());
Integer length = clickHouseDataTypeEnum.getLength(field.getType());
if (Objects.nonNull(length)) {
field.setLength(length);
}
if (columnList.contains(dbQuery.autoIncrement())) {
field.setAutoIncrement(
Asserts.isEqualsIgnoreCase(results.getString(dbQuery.autoIncrement()), "auto_increment"));
Integer scale = clickHouseDataTypeEnum.getScale(field.getType());
if (Objects.nonNull(scale)) {
field.setScale(scale);
}
if (columnList.contains(dbQuery.defaultValue())) {
field.setDefaultValue(results.getString(dbQuery.defaultValue()));
Integer precision = clickHouseDataTypeEnum.getPrecision(field.getType());
if (Objects.nonNull(precision)) {
field.setPrecision(precision);
}
field.setJavaType(getTypeConvert().convert(field, config));
field.setJavaType(getTypeConvert().convert(field));
columns.add(field);
}
} catch (SQLException e) {
e.printStackTrace();
log.error("ClickHouseDriver listColumns error.", e);
} finally {
close(preparedStatement, results);
}
......
......@@ -39,7 +39,8 @@ public class ClickHouseQuery extends AbstractDBQuery {
*/
@Override
public String tablesSql(String schemaName) {
return "select name from system.tables where 1=1 and database='" + schemaName + "'";
return "select name, total_rows, engine, metadata_modification_time, comment "
+ "from system.tables where 1=1 and database='" + schemaName + "'";
}
/**
......@@ -51,11 +52,7 @@ public class ClickHouseQuery extends AbstractDBQuery {
*/
@Override
public String columnsSql(String schemaName, String tableName) {
return "select * from system.columns where 1=1 and database='"
+ schemaName
+ "' and table='"
+ tableName
+ "'";
return "select * from system.columns where 1=1 and database='" + schemaName + "' and table='" + tableName + "'";
}
@Override
......@@ -107,4 +104,24 @@ public class ClickHouseQuery extends AbstractDBQuery {
public String isPK() {
return "1";
}
@Override
public String columnPosition() {
return "position";
}
@Override
public String engine() {
return "engine";
}
@Override
public String rows() {
return "total_rows";
}
@Override
public String updateTime() {
return "metadata_modification_time";
}
}
/*
*
* 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.dinky.metadata.enums;
import static org.dinky.metadata.enums.ClickHouseDataTypeEnum.*;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ClickHouseDataTypeEnumTest {
@Test
public void removeTypeTest() {
String dataType = "Array(Int32)";
String res = ClickHouseDataTypeEnum.removeType(dataType, Lists.newArrayList(Nullable, Array));
assertEquals("int32", res);
}
@Test
public void removeTypeTest2() {
String dataType = "FixedString(6)";
String res = ClickHouseDataTypeEnum.removeType(dataType, Lists.newArrayList(Nullable, FixedString));
assertEquals("6", res);
}
@Test
public void removeTypeTest3() {
String dataType = "Decimal(6)";
String res = ClickHouseDataTypeEnum.removeType(dataType, Lists.newArrayList(Nullable, Decimal));
assertEquals("6", res);
}
@Test
public void ofTest() {
String dataType = "Decimal(6)";
ClickHouseDataTypeEnum res = of(dataType);
assertEquals(Decimal, res);
}
@Test
public void ofTest2() {
String dataType = "Decimal32(6)";
ClickHouseDataTypeEnum res = of(dataType);
assertEquals(Decimal32, res);
}
@Test
public void ofTest4() {
String dataType = "UInt32";
ClickHouseDataTypeEnum res = of(dataType);
assertEquals(UInt32, res);
}
@Test
public void getLengthTest() {
Integer length = FixedString.getLength("FixedString(10)");
assertEquals(10, length.intValue());
}
@Test
public void getScaleTest() {
Integer scale = Decimal.getScale("Decimal(10, 2)");
assertEquals(2, scale.intValue());
}
@Test
public void getPrecisionTest() {
Integer precision = Decimal.getPrecision("Decimal(10, 2)");
assertEquals(10, precision.intValue());
}
}
......@@ -250,6 +250,7 @@ const Result = (props: any) => {
<Table
columns={getColumns(data.columns)}
size='small'
scroll={{ x: 'max-content' }}
dataSource={data.rowData?.map((item: any, index: number) => {
return { ...item, key: index };
})}
......
......@@ -150,6 +150,7 @@ const SQLQuery: React.FC<SQLQueryProps> = (props) => {
loading={loading}
{...PROTABLE_OPTIONS_PUBLIC}
size={'small'}
scroll={{ x: 'max-content' }}
search={false}
pagination={{
defaultPageSize: 15,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment