Skip to content

[PECOBLR-388] added real, numeric, char and bigint support in setObject method#818

Merged
shivam2680 merged 3 commits into
mainfrom
shivam2680/ps-sql-types
May 12, 2025
Merged

[PECOBLR-388] added real, numeric, char and bigint support in setObject method#818
shivam2680 merged 3 commits into
mainfrom
shivam2680/ps-sql-types

Conversation

@shivam2680

Copy link
Copy Markdown
Collaborator

Description

Added support for REAL, NUMERIC, CHAR and BIGINT data types in PreparedStatement.setObject() method.
setObject(int parameterIndex, Object x, int targetSqlType) allows users to insert values of specific type in queries.

Testing

Unit test
manual test
script:

static Stream<Integer> sqlTypesProvider() {
    return Stream.of(
        Types.BIT,
        Types.TINYINT,
        Types.SMALLINT,
        Types.INTEGER,
        Types.BIGINT,
        Types.FLOAT,
        Types.REAL,
        Types.DOUBLE,
        Types.NUMERIC,
        Types.DECIMAL,
        Types.CHAR,
        Types.VARCHAR,
        Types.DATE,
        Types.TIME,
        Types.TIMESTAMP,
        Types.BINARY,
        Types.NULL,
        Types.OTHER);
  }

  @ParameterizedTest
  @MethodSource("sqlTypesProvider")
  void testSqlTypes(int sqlType) throws SQLException {
      try (Statement stmt = con.createStatement()) {
        stmt.execute("DROP TABLE IF EXISTS main.ms_testing_schema.test_long");
      }
      try (Statement stmt = con.createStatement()) {
        stmt.execute("CREATE TABLE IF NOT EXISTS main.ms_testing_schema.test_long (val VARIANT)");
      }
      String insertSql = "INSERT INTO main.ms_testing_schema.test_long VALUES (?)";
      try (PreparedStatement ps = con.prepareStatement(insertSql)) {
        ps.setObject(1, getSampleValueForType(sqlType), sqlType);
        ps.executeUpdate();
      }
      String selectSql = "SELECT * FROM main.ms_testing_schema.test_long";
      try (ResultSet rs = con.createStatement().executeQuery(selectSql)) {
        printResultSet(rs);
      }
    }
  }

Additional Notes to the Reviewer

@shivam2680 shivam2680 added the bug Something isn't working label May 7, 2025
case DatabricksTypeUtil.INT:
return ColumnInfoTypeName.INT;
case DatabricksTypeUtil.BIGINT:
case DatabricksTypeUtil.LONG:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can we add for REAL also?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is not needed, because we first convert java.Sql.Types into DatabricksType and then into columnInfoType. So, real is already converted to float in getDatabricksTypeFromSQLType, which is passed to getColumnInfoType.

@shivam2680 shivam2680 requested a review from gopalldb May 8, 2025 10:02
@shivam2680

Copy link
Copy Markdown
Collaborator Author

will add integration tests in a different PR

@shivam2680 shivam2680 merged commit 32f96bb into databricks:main May 12, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants