Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
- Databricks SDK dependency upgraded to latest version 0.60.0

### Fixed
-
- Fixed `ResultSet.getString` for Boolean columns in Metadata result set.
---
*Note: When making changes, please add your change under the appropriate section with a brief description.*
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ public boolean toBoolean(Object object) throws DatabricksSQLException {
return Boolean.parseBoolean((String) object);
}
throw new DatabricksSQLException(
"Unsupported type for conversion to BIT: " + object.getClass(),
"Unsupported type for conversion to BIT: " + (object == null ? "null" : object.getClass()),
DatabricksDriverErrorCode.UNSUPPORTED_OPERATION);
}

@Override
public String toString(Object object) throws DatabricksSQLException {
if (object instanceof Boolean) {
return object.toString();
}
throw new DatabricksSQLException(
Comment thread
msrathore-db marked this conversation as resolved.
"Unsupported type for conversion to String: "
+ (object == null ? "null" : object.getClass()),
DatabricksDriverErrorCode.UNSUPPORTED_OPERATION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.databricks.jdbc.api.impl.converters;

import static org.junit.jupiter.api.Assertions.*;

import com.databricks.jdbc.exception.DatabricksSQLException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

public class BitConverterTest {

private final BitConverter bitConverter = new BitConverter();

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testToStringWithBooleanPrimitives(boolean input) throws DatabricksSQLException {
String expected = input ? "true" : "false";
assertEquals(expected, bitConverter.toString(input));
}

@ParameterizedTest
@CsvSource({"true, true", "false, false"})
public void testToStringWithBooleanObjects(String input, String expected)
throws DatabricksSQLException {
Boolean booleanInput = Boolean.parseBoolean(input);
assertEquals(expected, bitConverter.toString(booleanInput));
}

@ParameterizedTest
@CsvSource({
"null, null",
"Object, Object",
"int[], class [I",
"String[], class [Ljava.lang.String;"
})
public void testToStringWithUnsupportedTypes(String typeDescription, String expectedInMessage) {
Object input = getTestObject(typeDescription);

DatabricksSQLException exception =
assertThrows(DatabricksSQLException.class, () -> bitConverter.toString(input));

assertTrue(exception.getMessage().contains("Unsupported type for conversion to String"));
assertTrue(exception.getMessage().contains(expectedInMessage));
assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState());
}

@Test
public void testToBooleanWithBooleanTrue() throws DatabricksSQLException {
assertTrue(bitConverter.toBoolean(true));
}

@Test
public void testToBooleanWithBooleanFalse() throws DatabricksSQLException {
assertFalse(bitConverter.toBoolean(false));
}

@Test
public void testToBooleanWithBooleanObject() throws DatabricksSQLException {
Boolean trueObject = Boolean.TRUE;
Boolean falseObject = Boolean.FALSE;

assertTrue(bitConverter.toBoolean(trueObject));
assertFalse(bitConverter.toBoolean(falseObject));
}

@ParameterizedTest
@CsvSource({"0, false", "1, true", "-1, true", "42, true", "0.0, false", "3.14, true"})
public void testToBooleanWithNumbers(String numberStr, boolean expected)
throws DatabricksSQLException {
Number number =
numberStr.contains(".") ? Double.parseDouble(numberStr) : Integer.parseInt(numberStr);
assertEquals(expected, bitConverter.toBoolean(number));
}

@ParameterizedTest
@CsvSource({
"true, true",
"TRUE, true",
"True, true",
"false, false",
"FALSE, false",
"False, false",
"anything else, false",
"'', false",
"1, false",
"0, false"
})
public void testToBooleanWithStrings(String input, boolean expected)
throws DatabricksSQLException {
assertEquals(expected, bitConverter.toBoolean(input));
}

@ParameterizedTest
@CsvSource({
"null, null",
"Object, Object",
"int[], class [I",
"String[], class [Ljava.lang.String;"
})
public void testToBooleanWithUnsupportedTypes(String typeDescription, String expectedInMessage) {
Object input = getTestObject(typeDescription);

DatabricksSQLException exception =
assertThrows(DatabricksSQLException.class, () -> bitConverter.toBoolean(input));

assertTrue(exception.getMessage().contains("Unsupported type for conversion to BIT"));
assertTrue(exception.getMessage().contains(expectedInMessage));
assertEquals("UNSUPPORTED_OPERATION", exception.getSQLState());
}

private Object getTestObject(String typeDescription) {
switch (typeDescription) {
case "null":
return null;
case "Object":
return new Object();
case "int[]":
return new int[] {1, 2, 3};
case "String[]":
return new String[] {"a", "b"};
default:
throw new IllegalArgumentException("Unknown type: " + typeDescription);
}
}
}
Loading