Skip to content

Commit 19b854d

Browse files
Suggestion: modify tinyint to string conversion (#1053)
## Description Currently the conversion from tinyint to string is done using `String(byte[])` which depends on the platform's default charset and also is [unspecified](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#String-byte:A-) when the bytes are not valid in the default charset (e.g. may produce "?" instead of a sensible value). ## Testing Tested with the existing unit tests. ## Additional Notes to the Reviewer - --------- Signed-off-by: Ivan Vankov <ivan.vankov@workato.com> Co-authored-by: Samikshya Chand <148681192+samikshya-db@users.noreply.github.com>
1 parent e32739b commit 19b854d

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
### Fixed
1212
- Fixed: Errors in table creation when using BIGINT, SMALLINT, TINYINT, or VOID types.
1313
- Fixed: PreparedStatement.getMetaData() now correctly reports TINYINT columns as Types.TINYINT (java.lang.Byte) instead of Types.SMALLINT (java.lang.Integer).
14+
- Fixed: TINYINT to String conversion to return numeric representation (e.g., "65") instead of character representation (e.g., "A").
1415
---
1516
*Note: When making changes, please add your change under the appropriate section with a brief description.*

src/main/java/com/databricks/jdbc/api/impl/converters/ByteConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public byte[] toByteArray(Object object) throws DatabricksSQLException {
6666

6767
@Override
6868
public String toString(Object object) throws DatabricksSQLException {
69-
return new String(new byte[] {toByte(object)});
69+
return String.valueOf(toByte(object));
7070
}
7171
}

src/test/java/com/databricks/jdbc/api/impl/converters/ByteConverterTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.math.BigDecimal;
88
import java.math.BigInteger;
99
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.CsvSource;
1012

1113
public class ByteConverterTest {
1214
private final byte NON_ZERO_OBJECT = 65;
@@ -74,9 +76,10 @@ public void testConvertToChar() {
7476
assertTrue(exception.getMessage().contains("Unsupported char conversion operation"));
7577
}
7678

77-
@Test
78-
public void testConvertToString() throws DatabricksSQLException {
79-
assertEquals(new ByteConverter().toString(NON_ZERO_OBJECT), "A");
79+
@ParameterizedTest
80+
@CsvSource({"-128, -128", "-1, -1", "0, 0", "1, 1", "10, 10", "65, 65", "127, 127"})
81+
public void testConvertToString(byte input, String expected) throws DatabricksSQLException {
82+
assertEquals(new ByteConverter().toString(input), expected);
8083
}
8184

8285
@Test

0 commit comments

Comments
 (0)