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

NIFI-12828 Add mapping for BIT SQL Type in DataTypeUtils #8445

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,7 @@ public static DataType getDataTypeFromSQLTypeValue(final int sqlType) {
case Types.BIGINT:
return RecordFieldType.BIGINT.getDataType();
case Types.BOOLEAN:
case Types.BIT:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mapping for the BIT datatype should be changed to Integer. This is because the BIT datatype can represent not only BIT(1) but also BIT(n>1), making it impossible to distinguish between them. Therefore, using Integer provides a more suitable and consistent mapping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @Lehel44 and @mattyb149

Have a look on below sample table definition (Postgres14)

CREATE TABLE test_table (
    id int4 NOT NULL,
    active bool NULL,
    col_bit bit(1) NULL,
    col_bit1 bit(1) NULL,
    col_varbit varbit(2) NULL,
    CONSTRAINT temp_market_cap_pkey PRIMARY KEY (id)
);

We can use something like below:

# DataTypeUtils.java
case Types.BOOLEAN:
    return RecordFieldType.BOOLEAN.getDataType();
case Types.BIT:
    return RecordFieldType.INT.getDataType();

String typeName = columnResultSet.getString("TYPE_NAME");
final int dataType;
if (typeName.equalsIgnoreCase("bool")) {
    dataType = 16;
} else {
    dataType = columnResultSet.getInt("DATA_TYPE");
}

then below will be result

colName postgresType javaSqlType javaSqlTypeName finalType
id int 4 int4 INT
active bool -7 bool BOOLEAN
col_bit bit(1) -7 bit INT
col_bit1 bit(2) -7 bit INT
col_varbit varbit(2) 1111 varbit STRING

let me know your thoughts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravinarayansingh I tried it and it looks good to me!

return RecordFieldType.BOOLEAN.getDataType();
case Types.TINYINT:
return RecordFieldType.BYTE.getDataType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ public void testGetSQLTypeValueWithBigDecimal() {
@Test
public void testGetDataTypeFromSQLTypeValue() {
assertEquals(RecordFieldType.STRING.getDataType(), DataTypeUtils.getDataTypeFromSQLTypeValue(Types.CLOB));
assertEquals(RecordFieldType.BOOLEAN.getDataType(), DataTypeUtils.getDataTypeFromSQLTypeValue(Types.BIT));
assertEquals(RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()), DataTypeUtils.getDataTypeFromSQLTypeValue(Types.BLOB));
assertEquals(RecordFieldType.STRING.getDataType(), DataTypeUtils.getDataTypeFromSQLTypeValue(Types.CHAR));
}
Expand Down