Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepare for Metadata columns refactor #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions databend-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>


</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.databend.client.data;

import java.sql.Types;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

// data type that reflect java.sql.type
public enum DatabendDataType {
BOOLEAN(Types.BOOLEAN, DatabendTypes.BOOLEAN, true, false, 1, 0, 0, false, "Boolean", "BOOL"),
INTEGER(Types.INTEGER, DatabendTypes.INTEGER, true, false, 0, 0, 0, false, "Int32", "INTEGER",
"INT", "Int8", "Int16", "UInt16", "UInt32"),
INT_8(Types.INTEGER, DatabendTypes.INT8, true, false, 0, 0, 0, false,
"INT", "Int8"),
U_INT_8(Types.TINYINT, DatabendTypes.UINT8, true, false, 3, 0, 0, false, "UInt8"),
INT_16(Types.INTEGER, DatabendTypes.INT16, true, false, 0, 0, 0, false, "Int16"),
UNSIGNED_BIG_INT_16(Types.BIGINT, DatabendTypes.UINT16, false, false, 0, 0, 0, false, "UInt16"),
INT_32(Types.INTEGER, DatabendTypes.INT32, true, false, 0, 0, 0, false, "Int32"),
UNSIGNED_BIG_INT_32(Types.BIGINT, DatabendTypes.UINT32, false, false, 0, 0, 0, false, "UInt32"),
INT_64(Types.INTEGER, DatabendTypes.INT64, true, false, 0, 0, 0, false, "Int64"),
UNSIGNED_BIG_INT_64(Types.BIGINT, DatabendTypes.UINT64, false, false, 0, 0, 0, false, "UInt64"),
DOUBLE(Types.DOUBLE, DatabendTypes.FLOAT64, true, false, 0, 0, 0, false,
"Float64", "DOUBLE"),
FLOAT(Types.FLOAT, DatabendTypes.FLOAT32, true, false, 0, 0, 0, false,
"Float32", "Float"),

STRING(Types.VARCHAR, DatabendTypes.STRING, false, true, 0, 0, 0, false, "String", "VARCHAR"),

DATE(Types.DATE, DatabendTypes.DATE, false, false, 0, 0, 0, true, "Date"),
TIMESTAMP(Types.TIMESTAMP, DatabendTypes.TIMESTAMP, false, false, 6, 0, 0, true, "DateTime", "TIMESTAMP"),

NULL(Types.NULL, DatabendTypes.NULL, false, false, 0, 0, 0, false, "NULL"),
ARRAY(Types.ARRAY, DatabendTypes.ARRAY, false, true, 0, 0, 0, false, "Array"),
MAP(Types.OTHER, DatabendTypes.MAP, false, true, 0, 0, 0, false, "Map"),
TUPLE(Types.OTHER, DatabendTypes.TUPLE, false, true, 0, 0, 0, false, "Tuple"),
;

private static final Map<String, DatabendDataType> typeNameOrAliasToType;

static {
typeNameOrAliasToType = new HashMap<>();
for (DatabendDataType dataType : values()) {
Arrays.stream(dataType.aliases).forEach(alias -> typeNameOrAliasToType.put(alias.toUpperCase(), dataType));
}
}

private final int sqlType;
private final String displayName;
private final boolean signed;
private final boolean caseSensitive;
private final int precision;

private final int minScale;
private final int maxScale;

private final boolean time;
private final String[] aliases;

DatabendDataType(int sqlType, String displayName, boolean signed,
boolean caseSensitive, int precision, int minScale, int maxScale, boolean isTime, String... aliases) {
this.sqlType = sqlType;
this.displayName = displayName;
this.signed = signed;
this.caseSensitive = caseSensitive;
this.precision = precision;
this.maxScale = maxScale;
this.aliases = aliases;
this.time = isTime;
this.minScale = minScale;
}

public static DatabendDataType ofType(String type) {
String formattedType = type.trim().toUpperCase();
return Optional.ofNullable(typeNameOrAliasToType.get(formattedType)).orElse(NULL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

package com.databend.client.data;

public final class DatabendTypes
{

public final class DatabendTypes {
public static final String NULL = "null";
public static final String NULLABLE = "nullable";
public static final String BOOLEAN = "boolean";
static final String INTEGER = "integer";
public static final String INT8 = "int8";
public static final String INT16 = "int16";
public static final String INT32 = "int32";
Expand All @@ -36,6 +37,8 @@ public final class DatabendTypes
public static final String STRING = "string";
public static final String STRUCT = "struct";
public static final String ARRAY = "array";
public static final String TUPLE = "tuple";
public static final String MAP = "map";
public static final String VARIANT = "variant";
public static final String VARIANT_ARRAY = "variantarray";
public static final String VARIANT_OBJECT = "variantobject";
Expand Down
6 changes: 6 additions & 0 deletions databend-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<artifactId>okhttp-tls</artifactId>
<version>${dep.okhttp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.databend.jdbc;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand All @@ -10,6 +11,7 @@
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -18,6 +20,7 @@
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION;
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MAJOR;
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MINOR;
import static com.databend.jdbc.metadata.MetadataColumns.*;
import static java.util.Objects.requireNonNull;

public class DatabendDatabaseMetaData implements DatabaseMetaData
Expand Down
65 changes: 65 additions & 0 deletions databend-jdbc/src/main/java/com/databend/jdbc/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.databend.jdbc;

import lombok.Builder;
import lombok.Value;
import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.List;

/**
* Represents a SQL query that can be sent to Databend
*/
@Builder
@Value
public class Query {
String select;
String from;
String innerJoin;
String orderBy;
List<String> conditions;

/**
* Parse the object to a SQL query that can be sent to Firebolt
*
* @return SQL query that can be sent to Firebolt
*/
public String toSql() {
StringBuilder query = new StringBuilder();
if (StringUtils.isBlank(select)) {
throw new IllegalStateException("Cannot create query: SELECT cannot be blank");
}
if (StringUtils.isBlank(from)) {
throw new IllegalStateException("Cannot create query: FROM cannot be blank");
}

query.append("SELECT ").append(select);
query.append(" FROM ").append(from);
if (StringUtils.isNotBlank(innerJoin)) {
query.append(" JOIN ").append(innerJoin);
}
query.append(getConditionsPart());
if (StringUtils.isNotBlank(orderBy)) {
query.append(" order by ").append(orderBy);
}
return query.toString();
}

private String getConditionsPart() {
StringBuilder agg = new StringBuilder();
Iterator<String> iter = conditions.iterator();
if (iter.hasNext()) {
agg.append(" WHERE ");
}
if (iter.hasNext()) {
String entry = iter.next();
agg.append(entry);
}
while (iter.hasNext()) {
String entry = iter.next();
agg.append(" AND ").append(entry);
}
return agg.toString();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.databend.jdbc.metadata;

import lombok.experimental.UtilityClass;

/**
* Column names to include in the {@link java.sql.ResultSet} as specified in the
* {@link java.sql.DatabaseMetaData} javadoc
*/
@UtilityClass
public class MetadataColumns {
public static final String TABLE_CAT = "TABLE_CAT";
public static final String TABLE_CATALOG = "TABLE_CATALOG";
public static final String TABLE_SCHEM = "TABLE_SCHEM";
public static final String TABLE_NAME = "TABLE_NAME";
public static final String COLUMN_NAME = "COLUMN_NAME";
public static final String DATA_TYPE = "DATA_TYPE";
public static final String TYPE_NAME = "TYPE_NAME";
public static final String COLUMN_SIZE = "COLUMN_SIZE";
public static final String BUFFER_LENGTH = "BUFFER_LENGTH";
public static final String DECIMAL_DIGITS = "DECIMAL_DIGITS";
public static final String NULLABLE = "NULLABLE";
public static final String REMARKS = "REMARKS";
public static final String COLUMN_DEF = "COLUMN_DEF";
public static final String CHAR_OCTET_LENGTH = "CHAR_OCTET_LENGTH";
public static final String ORDINAL_POSITION = "ORDINAL_POSITION";
public static final String IS_NULLABLE = "IS_NULLABLE";
public static final String SCOPE_CATALOG = "SCOPE_CATALOG";
public static final String SCOPE_SCHEMA = "SCOPE_SCHEMA";
public static final String SCOPE_TABLE = "SCOPE_TABLE";
public static final String SOURCE_DATA_TYPE = "SOURCE_DATA_TYPE";
public static final String IS_AUTOINCREMENT = "IS_AUTOINCREMENT";
public static final String IS_GENERATEDCOLUMN = "IS_GENERATEDCOLUMN";
public static final String TABLE_TYPE = "TABLE_TYPE";
public static final String TYPE_CAT = "TYPE_CAT";
public static final String TYPE_SCHEM = "TYPE_SCHEM";
public static final String SELF_REFERENCING_COL_NAME = "SELF_REFERENCING_COL_NAME";
public static final String REF_GENERATION = "REF_GENERATION";
public static final int COMMON_RADIX = 10;
public static final String PRECISION = "PRECISION";
public static final String LITERAL_PREFIX = "LITERAL_PREFIX";
public static final String LITERAL_SUFFIX = "LITERAL_SUFFIX";
public static final String CREATE_PARAMS = "CREATE_PARAMS";
public static final String CASE_SENSITIVE = "CASE_SENSITIVE";
public static final String SEARCHABLE = "SEARCHABLE";
public static final String UNSIGNED_ATTRIBUTE = "UNSIGNED_ATTRIBUTE";
public static final String FIXED_PREC_SCALE = "FIXED_PREC_SCALE";
public static final String AUTO_INCREMENT = "AUTO_INCREMENT";
public static final String LOCAL_TYPE_NAME = "LOCAL_TYPE_NAME";
public static final String MINIMUM_SCALE = "MINIMUM_SCALE";
public static final String MAXIMUM_SCALE = "MAXIMUM_SCALE";
public static final String SQL_DATA_TYPE = "SQL_DATA_TYPE";
public static final String SQL_DATETIME_SUB = "SQL_DATETIME_SUB";
public static final String NUM_PREC_RADIX = "NUM_PREC_RADIX";

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.databend.jdbc.metadata;

import com.databend.jdbc.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MetadataUtil {
public String getColumnsQuery(String schemaPattern, String tableNamePattern, String columnNamePattern) {
Query.QueryBuilder queryBuilder = Query.builder().select(
"table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position")
.from("information_schema.columns");

List<String> conditions = new ArrayList<>();
Optional.ofNullable(tableNamePattern)
.ifPresent(pattern -> conditions.add(String.format("table_name LIKE '%s'", pattern)));
Optional.ofNullable(columnNamePattern)
.ifPresent(pattern -> conditions.add(String.format("column_name LIKE '%s'", pattern)));
Optional.ofNullable(schemaPattern)
.ifPresent(pattern -> conditions.add(String.format("table_schema LIKE '%s'", pattern)));
return queryBuilder.conditions(conditions).build().toSql();
}

}