Skip to content

Commit 0e10d04

Browse files
authored
[PECOBLR-839] Fixed getString for boolean column (#977)
## Description <!-- Provide a brief summary of the changes made and the issue they aim to address.--> Added a toString function in BITConverter to fix getString for boolean columns. Fixed toBoolean to handle null. Added tests for both the functions of BITConverter ## Testing <!-- Describe how the changes have been tested--> Added unit tests ## Additional Notes to the Reviewer <!-- Share any additional context or insights that may help the reviewer understand the changes better. This could include challenges faced, limitations, or compromises made during the development process. Also, mention any areas of the code that you would like the reviewer to focus on specifically. --> Fixes #956
1 parent 27b656b commit 0e10d04

3 files changed

Lines changed: 139 additions & 2 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
- Databricks SDK dependency upgraded to latest version 0.60.0
1515

1616
### Fixed
17-
-
17+
- Fixed `ResultSet.getString` for Boolean columns in Metadata result set.
1818
---
1919
*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/BitConverter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ public boolean toBoolean(Object object) throws DatabricksSQLException {
1717
return Boolean.parseBoolean((String) object);
1818
}
1919
throw new DatabricksSQLException(
20-
"Unsupported type for conversion to BIT: " + object.getClass(),
20+
"Unsupported type for conversion to BIT: " + (object == null ? "null" : object.getClass()),
21+
DatabricksDriverErrorCode.UNSUPPORTED_OPERATION);
22+
}
23+
24+
@Override
25+
public String toString(Object object) throws DatabricksSQLException {
26+
if (object instanceof Boolean) {
27+
return object.toString();
28+
}
29+
throw new DatabricksSQLException(
30+
"Unsupported type for conversion to String: "
31+
+ (object == null ? "null" : object.getClass()),
2132
DatabricksDriverErrorCode.UNSUPPORTED_OPERATION);
2233
}
2334
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.databricks.jdbc.api.impl.converters;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.databricks.jdbc.exception.DatabricksSQLException;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
import org.junit.jupiter.params.provider.ValueSource;
10+
11+
public class BitConverterTest {
12+
13+
private final BitConverter bitConverter = new BitConverter();
14+
15+
@ParameterizedTest
16+
@ValueSource(booleans = {true, false})
17+
public void testToStringWithBooleanPrimitives(boolean input) throws DatabricksSQLException {
18+
String expected = input ? "true" : "false";
19+
assertEquals(expected, bitConverter.toString(input));
20+
}
21+
22+
@ParameterizedTest
23+
@CsvSource({"true, true", "false, false"})
24+
public void testToStringWithBooleanObjects(String input, String expected)
25+
throws DatabricksSQLException {
26+
Boolean booleanInput = Boolean.parseBoolean(input);
27+
assertEquals(expected, bitConverter.toString(booleanInput));
28+
}
29+
30+
@ParameterizedTest
31+
@CsvSource({
32+
"null, null",
33+
"Object, Object",
34+
"int[], class [I",
35+
"String[], class [Ljava.lang.String;"
36+
})
37+
public void testToStringWithUnsupportedTypes(String typeDescription, String expectedInMessage) {
38+
Object input = getTestObject(typeDescription);
39+
40+
DatabricksSQLException exception =
41+
assertThrows(DatabricksSQLException.class, () -> bitConverter.toString(input));
42+
43+
assertTrue(exception.getMessage().contains("Unsupported type for conversion to String"));
44+
assertTrue(exception.getMessage().contains(expectedInMessage));
45+
assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState());
46+
}
47+
48+
@Test
49+
public void testToBooleanWithBooleanTrue() throws DatabricksSQLException {
50+
assertTrue(bitConverter.toBoolean(true));
51+
}
52+
53+
@Test
54+
public void testToBooleanWithBooleanFalse() throws DatabricksSQLException {
55+
assertFalse(bitConverter.toBoolean(false));
56+
}
57+
58+
@Test
59+
public void testToBooleanWithBooleanObject() throws DatabricksSQLException {
60+
Boolean trueObject = Boolean.TRUE;
61+
Boolean falseObject = Boolean.FALSE;
62+
63+
assertTrue(bitConverter.toBoolean(trueObject));
64+
assertFalse(bitConverter.toBoolean(falseObject));
65+
}
66+
67+
@ParameterizedTest
68+
@CsvSource({"0, false", "1, true", "-1, true", "42, true", "0.0, false", "3.14, true"})
69+
public void testToBooleanWithNumbers(String numberStr, boolean expected)
70+
throws DatabricksSQLException {
71+
Number number =
72+
numberStr.contains(".") ? Double.parseDouble(numberStr) : Integer.parseInt(numberStr);
73+
assertEquals(expected, bitConverter.toBoolean(number));
74+
}
75+
76+
@ParameterizedTest
77+
@CsvSource({
78+
"true, true",
79+
"TRUE, true",
80+
"True, true",
81+
"false, false",
82+
"FALSE, false",
83+
"False, false",
84+
"anything else, false",
85+
"'', false",
86+
"1, false",
87+
"0, false"
88+
})
89+
public void testToBooleanWithStrings(String input, boolean expected)
90+
throws DatabricksSQLException {
91+
assertEquals(expected, bitConverter.toBoolean(input));
92+
}
93+
94+
@ParameterizedTest
95+
@CsvSource({
96+
"null, null",
97+
"Object, Object",
98+
"int[], class [I",
99+
"String[], class [Ljava.lang.String;"
100+
})
101+
public void testToBooleanWithUnsupportedTypes(String typeDescription, String expectedInMessage) {
102+
Object input = getTestObject(typeDescription);
103+
104+
DatabricksSQLException exception =
105+
assertThrows(DatabricksSQLException.class, () -> bitConverter.toBoolean(input));
106+
107+
assertTrue(exception.getMessage().contains("Unsupported type for conversion to BIT"));
108+
assertTrue(exception.getMessage().contains(expectedInMessage));
109+
assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState());
110+
}
111+
112+
private Object getTestObject(String typeDescription) {
113+
switch (typeDescription) {
114+
case "null":
115+
return null;
116+
case "Object":
117+
return new Object();
118+
case "int[]":
119+
return new int[] {1, 2, 3};
120+
case "String[]":
121+
return new String[] {"a", "b"};
122+
default:
123+
throw new IllegalArgumentException("Unknown type: " + typeDescription);
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)