Skip to content

Commit

Permalink
Feature/new types support (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-dispa authored Mar 13, 2023
1 parent 801e6ef commit d490988
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void shouldReturnColumns() throws SQLException {
contains(schemaName, schemaName, schemaName, schemaName, schemaName));
assertThat(result.get(REMARKS), contains(null, null, null, null, null));
assertThat(result.get(NULLABLE), contains("0", "1", "1", "1", "0"));
assertThat(result.get(DECIMAL_DIGITS), contains("0", "0", "0", "0", "0"));
assertThat(result.get(DECIMAL_DIGITS), contains(null, null, null, null, null));
assertThat(result.get(SQL_DATETIME_SUB), contains(null, null, null, null, null));
assertThat(result.get(NUM_PREC_RADIX), contains("10", "10", "10", "10", "10"));
assertThat(result.get(IS_GENERATEDCOLUMN), contains("NO", "NO", "NO", "NO", "NO"));
Expand All @@ -121,7 +121,7 @@ void shouldReturnColumns() throws SQLException {
assertThat(result.get(SCOPE_SCHEMA), contains(null, null, null, null, null));
assertThat(result.get(ORDINAL_POSITION), contains("1", "2", "3", "4", "5"));
assertThat(result.get(TYPE_NAME),
contains("BIGINT", "TIMESTAMP", "STRING", "BOOLEAN", "INTEGER"));
contains("bigint", "timestamp", "string", "boolean", "integer"));
assertThat(result.get(DATA_TYPE), contains("-5", "93", "12", "16", "4"));
assertThat(result.get(COLUMN_SIZE), contains("20", "19", "0", "1", "11"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ void shouldFailSQLInjectionAttempt() throws SQLException {
private QueryResult createExpectedResult(List<List<?>> expectedRows) {
return QueryResult.builder().databaseName(ConnectionInfo.getInstance().getDatabase())
.tableName("prepared_statement_test")
.columns(Arrays.asList(QueryResult.Column.builder().name("sales").type(FireboltDataType.INT_64).build(),
QueryResult.Column.builder().name("make").type(FireboltDataType.STRING).build()))
.columns(Arrays.asList(QueryResult.Column.builder().name("sales").type(FireboltDataType.BIG_INT).build(),
QueryResult.Column.builder().name("make").type(FireboltDataType.TEXT).build()))
.rows(expectedRows).build();

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/firebolt/jdbc/QueryResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public String toString() {
this.appendWithListValues(stringBuilder, columns.stream().map(Column::getName).collect(Collectors.toList()));
stringBuilder.append(NEXT_LINE);
this.appendWithListValues(stringBuilder, columns.stream().map(Column::getType)
.map(FireboltDataType::getInternalName).collect(Collectors.toList()));
.map(FireboltDataType::getAliases).map( aliases -> aliases[0]).collect(Collectors.toList()));
stringBuilder.append(NEXT_LINE);

for (int i = 0; i < rows.size(); i++) {
Expand Down
130 changes: 66 additions & 64 deletions src/main/java/com/firebolt/jdbc/metadata/FireboltDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FireboltDatabaseMetadata implements DatabaseMetaData {
private static final String PUBLIC_SCHEMA_NAME = "public";
private static final String INFORMATION_SCHEMA_NAME = "information_schema";
private static final String CATALOG_SCHEMA_NAME = "catalog";
private static final String QUOTE = "'";
private final String url;
private final FireboltConnection connection;
private String databaseVersion;
Expand All @@ -50,22 +51,22 @@ public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLExce
List<String> informationSchemaRow = Arrays.asList(INFORMATION_SCHEMA_NAME, dbName);
List<String> catalogRow = Arrays.asList(CATALOG_SCHEMA_NAME, dbName);
return FireboltResultSet.of(QueryResult.builder()
.columns(Arrays.asList(QueryResult.Column.builder().name(TABLE_SCHEM).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_CATALOG).type(STRING).build()))
.columns(Arrays.asList(QueryResult.Column.builder().name(TABLE_SCHEM).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_CATALOG).type(TEXT).build()))
.rows(Arrays.asList(publicRow, informationSchemaRow, catalogRow)).build());
}

@Override
public ResultSet getTableTypes() throws SQLException {
return FireboltResultSet.of(QueryResult.builder()
.columns(Collections.singletonList(QueryResult.Column.builder().name(TABLE_TYPE).type(STRING).build()))
.columns(Collections.singletonList(QueryResult.Column.builder().name(TABLE_TYPE).type(TEXT).build()))
.rows(Arrays.asList(Arrays.asList("TABLE"), Arrays.asList("VIEW"))).build());
}

@Override
public ResultSet getCatalogs() throws SQLException {
return FireboltResultSet.of(QueryResult.builder()
.columns(Collections.singletonList(QueryResult.Column.builder().name(TABLE_CAT).type(STRING).build()))
.columns(Collections.singletonList(QueryResult.Column.builder().name(TABLE_CAT).type(TEXT).build()))
.rows(Collections.singletonList(Collections.singletonList(connection.getCatalog()))).build());
}

Expand Down Expand Up @@ -98,30 +99,30 @@ public boolean supportsTransactionIsolationLevel(int level) throws SQLException
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
throws SQLException {
List<QueryResult.Column> columns = Arrays.asList(
QueryResult.Column.builder().name(TABLE_CAT).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_SCHEM).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(COLUMN_NAME).type(STRING).build(),
QueryResult.Column.builder().name(DATA_TYPE).type(INT_32).build(),
QueryResult.Column.builder().name(TYPE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(COLUMN_SIZE).type(INT_32).build(),
QueryResult.Column.builder().name(BUFFER_LENGTH).type(INT_32).build(),
QueryResult.Column.builder().name(DECIMAL_DIGITS).type(INT_32).build(),
QueryResult.Column.builder().name(NUM_PREC_RADIX).type(INT_32).build(),
QueryResult.Column.builder().name(NULLABLE).type(INT_32).build(),
QueryResult.Column.builder().name(REMARKS).type(STRING).build(),
QueryResult.Column.builder().name(COLUMN_DEF).type(STRING).build(),
QueryResult.Column.builder().name(SQL_DATA_TYPE).type(INT_32).build(),
QueryResult.Column.builder().name(SQL_DATETIME_SUB).type(INT_32).build(),
QueryResult.Column.builder().name(CHAR_OCTET_LENGTH).type(INT_32).build(),
QueryResult.Column.builder().name(ORDINAL_POSITION).type(INT_32).build(),
QueryResult.Column.builder().name(IS_NULLABLE).type(STRING).build(),
QueryResult.Column.builder().name(SCOPE_CATALOG).type(STRING).build(),
QueryResult.Column.builder().name(SCOPE_SCHEMA).type(STRING).build(),
QueryResult.Column.builder().name(SCOPE_TABLE).type(STRING).build(),
QueryResult.Column.builder().name(SOURCE_DATA_TYPE).type(INT_32).build(),
QueryResult.Column.builder().name(IS_AUTOINCREMENT).type(STRING).build(),
QueryResult.Column.builder().name(IS_GENERATEDCOLUMN).type(STRING).build());
QueryResult.Column.builder().name(TABLE_CAT).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_SCHEM).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(COLUMN_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(DATA_TYPE).type(INTEGER).build(),
QueryResult.Column.builder().name(TYPE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(COLUMN_SIZE).type(INTEGER).build(),
QueryResult.Column.builder().name(BUFFER_LENGTH).type(INTEGER).build(),
QueryResult.Column.builder().name(DECIMAL_DIGITS).type(INTEGER).build(),
QueryResult.Column.builder().name(NUM_PREC_RADIX).type(INTEGER).build(),
QueryResult.Column.builder().name(NULLABLE).type(INTEGER).build(),
QueryResult.Column.builder().name(REMARKS).type(TEXT).build(),
QueryResult.Column.builder().name(COLUMN_DEF).type(TEXT).build(),
QueryResult.Column.builder().name(SQL_DATA_TYPE).type(INTEGER).build(),
QueryResult.Column.builder().name(SQL_DATETIME_SUB).type(INTEGER).build(),
QueryResult.Column.builder().name(CHAR_OCTET_LENGTH).type(INTEGER).build(),
QueryResult.Column.builder().name(ORDINAL_POSITION).type(INTEGER).build(),
QueryResult.Column.builder().name(IS_NULLABLE).type(TEXT).build(),
QueryResult.Column.builder().name(SCOPE_CATALOG).type(TEXT).build(),
QueryResult.Column.builder().name(SCOPE_SCHEMA).type(TEXT).build(),
QueryResult.Column.builder().name(SCOPE_TABLE).type(TEXT).build(),
QueryResult.Column.builder().name(SOURCE_DATA_TYPE).type(INTEGER).build(),
QueryResult.Column.builder().name(IS_AUTOINCREMENT).type(TEXT).build(),
QueryResult.Column.builder().name(IS_GENERATEDCOLUMN).type(TEXT).build());

List<List<?>> rows = new ArrayList<>();
String query = MetadataUtil.getColumnsQuery(schemaPattern, tableNamePattern, columnNamePattern);
Expand All @@ -131,14 +132,16 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
List<?> row;
Column columnInfo = Column.of(columnDescription.getString("data_type"),
columnDescription.getString("column_name"));
row = Arrays.asList(connection.getCatalog(), columnDescription.getString("table_schema"), // schema
row = Arrays.asList(connection.getCatalog(), // TABLE_CAT
columnDescription.getString("table_schema"), // schema
columnDescription.getString("table_name"), // table name
columnDescription.getString("column_name"), // column name
String.valueOf(columnInfo.getType().getDataType().getSqlType()), // sql data type
columnInfo.getType().getCompactTypeName(), // shorter type name
String.valueOf(columnInfo.getType().getPrecision()), // column size
String.valueOf(columnInfo.getType().getPrecision()),// column size
null, // buffer length (not used, see Javadoc)
String.valueOf(columnInfo.getType().getScale()), String.valueOf(COMMON_RADIX), // radix
String.valueOf(columnInfo.getType().getScale()), // DECIMAL_DIGITS
String.valueOf(COMMON_RADIX), // radix
isColumnNullable(columnDescription) ? columnNullable : columnNoNulls
, null, // description of the column
StringUtils.isNotBlank(columnDescription.getString("column_default"))
Expand Down Expand Up @@ -179,16 +182,16 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
.flatMap(Collection::stream).collect(Collectors.toList());

return FireboltResultSet.of(QueryResult.builder()
.columns(Arrays.asList(QueryResult.Column.builder().name(TABLE_CAT).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_SCHEM).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(TABLE_TYPE).type(STRING).build(),
QueryResult.Column.builder().name(REMARKS).type(STRING).build(),
QueryResult.Column.builder().name(TYPE_CAT).type(STRING).build(),
QueryResult.Column.builder().name(TYPE_SCHEM).type(STRING).build(),
QueryResult.Column.builder().name(TYPE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(SELF_REFERENCING_COL_NAME).type(STRING).build(),
QueryResult.Column.builder().name(REF_GENERATION).type(STRING).build()))
.columns(Arrays.asList(QueryResult.Column.builder().name(TABLE_CAT).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_SCHEM).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(TABLE_TYPE).type(TEXT).build(),
QueryResult.Column.builder().name(REMARKS).type(TEXT).build(),
QueryResult.Column.builder().name(TYPE_CAT).type(TEXT).build(),
QueryResult.Column.builder().name(TYPE_SCHEM).type(TEXT).build(),
QueryResult.Column.builder().name(TYPE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(SELF_REFERENCING_COL_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(REF_GENERATION).type(TEXT).build()))
.rows(rows).build());
}

Expand Down Expand Up @@ -223,35 +226,33 @@ private List<List<?>> getTables(String catalog, String schemaPattern, String tab
@Override
public ResultSet getTypeInfo() throws SQLException {
List<QueryResult.Column> columns = Arrays.asList(
QueryResult.Column.builder().name(TYPE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(DATA_TYPE).type(INT_32).build(),
QueryResult.Column.builder().name(PRECISION).type(INT_32).build(),
QueryResult.Column.builder().name(LITERAL_PREFIX).type(STRING).build(),
QueryResult.Column.builder().name(LITERAL_SUFFIX).type(STRING).build(),
QueryResult.Column.builder().name(CREATE_PARAMS).type(STRING).build(),
QueryResult.Column.builder().name(NULLABLE).type(INT_32).build(),
QueryResult.Column.builder().name(TYPE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(DATA_TYPE).type(INTEGER).build(),
QueryResult.Column.builder().name(PRECISION).type(INTEGER).build(),
QueryResult.Column.builder().name(LITERAL_PREFIX).type(TEXT).build(),
QueryResult.Column.builder().name(LITERAL_SUFFIX).type(TEXT).build(),
QueryResult.Column.builder().name(CREATE_PARAMS).type(TEXT).build(),
QueryResult.Column.builder().name(NULLABLE).type(INTEGER).build(),
QueryResult.Column.builder().name(CASE_SENSITIVE).type(BOOLEAN).build(),
QueryResult.Column.builder().name(SEARCHABLE).type(INT_32).build(),
QueryResult.Column.builder().name(SEARCHABLE).type(INTEGER).build(),
QueryResult.Column.builder().name(UNSIGNED_ATTRIBUTE).type(BOOLEAN).build(),
QueryResult.Column.builder().name(FIXED_PREC_SCALE).type(BOOLEAN).build(),
QueryResult.Column.builder().name(AUTO_INCREMENT).type(BOOLEAN).build(),
QueryResult.Column.builder().name(LOCAL_TYPE_NAME).type(STRING).build(),
QueryResult.Column.builder().name(MINIMUM_SCALE).type(INT_32).build(),
QueryResult.Column.builder().name(MAXIMUM_SCALE).type(INT_32).build(),
QueryResult.Column.builder().name(SQL_DATA_TYPE).type(INT_32).build(),
QueryResult.Column.builder().name(SQL_DATETIME_SUB).type(INT_32).build(),
QueryResult.Column.builder().name(NUM_PREC_RADIX).type(INT_32).build());
QueryResult.Column.builder().name(LOCAL_TYPE_NAME).type(TEXT).build(),
QueryResult.Column.builder().name(MINIMUM_SCALE).type(INTEGER).build(),
QueryResult.Column.builder().name(MAXIMUM_SCALE).type(INTEGER).build(),
QueryResult.Column.builder().name(SQL_DATA_TYPE).type(INTEGER).build(),
QueryResult.Column.builder().name(SQL_DATETIME_SUB).type(INTEGER).build(),
QueryResult.Column.builder().name(NUM_PREC_RADIX).type(INTEGER).build());

List<List<?>> rows = new ArrayList<>();
List<FireboltDataType> usableTypes = Arrays.asList(INT_32, INT_64, FLOAT_32, FLOAT_64, STRING, DATE,
TIMESTAMP, FireboltDataType.TIMESTAMP_WITH_TIMEZONE, DECIMAL, ARRAY, TUPLE, BYTEA, BOOLEAN);
List<FireboltDataType> usableTypes = Arrays.asList(INTEGER, BIG_INT, REAL, DOUBLE_PRECISION, TEXT, DATE,
TIMESTAMP, NUMERIC, ARRAY, TUPLE, BYTEA, BOOLEAN);
usableTypes
.forEach(
type -> rows.add(Arrays.asList(type.getDisplayName(), type.getSqlType(),
type.getDefaultPrecision(), type.getSqlType() == VARCHAR ? "'" : null, // LITERAL_PREFIX
// - ' for
// VARCHAR
type.getSqlType() == VARCHAR ? "'" : null, // LITERAL_SUFFIX - ' for VARCHAR
type.getPrecision(), QUOTE, // LITERAL_PREFIX
QUOTE, // LITERAL_SUFFIX
null, // Description of the creation parameters - can be null (can set if needed
// in the future)
typeNullableUnknown, // It depends - A type can be nullable or not depending on
Expand All @@ -260,13 +261,14 @@ public ResultSet getTypeInfo() throws SQLException {
: typePredBasic, /*
* SEARCHABLE - LIKE can only be used for VARCHAR
*/
false, false, // FIXED_PREC_SCALE - indicates if the type can be a money value.
!type.isSigned(),
false, // FIXED_PREC_SCALE - indicates if the type can be a money value.
// Always
// false as we do not have a money type
false, // AUTO_INCREMENT
null, // LOCAL_TYPE_NAME
null, // MINIMUM_SCALE - There is no minimum scale
type.getDefaultScale(), // MAXIMUM_SCALE
type.getMinScale(), // MINIMUM_SCALE
type.getMaxScale(), // MAXIMUM_SCALE
null, // SQL_DATA_TYPE - Not needed - reserved for future use
null, // SQL_DATETIME_SUB - Not needed - reserved for future use
COMMON_RADIX)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class FieldTypeConverter {
CLASS_TO_CONVERT_FUNCTION = new HashMap<>();

CLASS_TO_CONVERT_FUNCTION.put(String.class, (value, columnType, column) -> {
verify(String.class, columnType, BaseType.STRING);
return BaseType.STRING.transform(value, column);
verify(String.class, columnType, BaseType.TEXT);
return BaseType.TEXT.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(Integer.class, (value, columnType, column) -> {
verify(Integer.class, columnType, BaseType.INTEGER, BaseType.SHORT);
Expand All @@ -40,7 +40,7 @@ public class FieldTypeConverter {
return BaseType.LONG.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(Double.class, (value, columnType, column) -> {
verify(Double.class, columnType, BaseType.DOUBLE, BaseType.FLOAT);
verify(Double.class, columnType, BaseType.DOUBLE, BaseType.REAL);
return BaseType.DOUBLE.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(Boolean.class, (value, columnType, column) -> {
Expand All @@ -56,13 +56,13 @@ public class FieldTypeConverter {
return BaseType.BIGINT.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(Float.class, (value, columnType, column) -> {
verify(Float.class, columnType, BaseType.FLOAT);
return BaseType.FLOAT.transform(value, column);
verify(Float.class, columnType, BaseType.REAL);
return BaseType.REAL.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(BigDecimal.class, (value, columnType, column) -> {
verify(BigDecimal.class, columnType, BaseType.BIGINT, BaseType.DECIMAL, BaseType.INTEGER, BaseType.FLOAT,
verify(BigDecimal.class, columnType, BaseType.BIGINT, BaseType.NUMERIC, BaseType.INTEGER, BaseType.REAL,
BaseType.DOUBLE);
return BaseType.DECIMAL.transform(value, column);
return BaseType.NUMERIC.transform(value, column);
});
CLASS_TO_CONVERT_FUNCTION.put(Date.class, (value, columnType, column) -> {
verify(Date.class, columnType, BaseType.DATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public String getString(int columnIndex) throws SQLException {
// We do not need to escape when the type is BYTEA
return this.getValueAtColumn(columnIndex);
} else {
return BaseType.STRING.transform(this.getValueAtColumn(columnIndex));
return BaseType.TEXT.transform(this.getValueAtColumn(columnIndex));
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public long getLong(int colNum) throws SQLException {

@Override
public float getFloat(int columnIndex) throws SQLException {
Float value = BaseType.FLOAT.transform(getValueAtColumn(columnIndex));
Float value = BaseType.REAL.transform(getValueAtColumn(columnIndex));
return value == null ? 0 : value;
}

Expand Down Expand Up @@ -263,7 +263,7 @@ public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
if (StringUtils.isEmpty(value)) {
return null;
}
return BaseType.DECIMAL.transform(value);
return BaseType.NUMERIC.transform(value);
}

@Override
Expand Down
Loading

0 comments on commit d490988

Please sign in to comment.