|
5 | 5 | import com.clickhouse.data.ClickHouseColumn; |
6 | 6 | import com.clickhouse.data.format.BinaryStreamUtils; |
7 | 7 | import org.testng.Assert; |
| 8 | +import org.testng.annotations.DataProvider; |
8 | 9 | import org.testng.annotations.Test; |
9 | 10 |
|
10 | 11 | import java.io.ByteArrayInputStream; |
11 | 12 | import java.io.ByteArrayOutputStream; |
12 | 13 | import java.io.IOException; |
13 | 14 | import java.math.BigDecimal; |
| 15 | +import java.math.BigInteger; |
14 | 16 | import java.util.TimeZone; |
15 | 17 |
|
16 | 18 | public class SerializerUtilsPrimitiveSerializationTests { |
@@ -45,6 +47,70 @@ public void testSerializeDecimalTargetFromGenericNumberValue() throws IOExceptio |
45 | 47 | Assert.assertEquals(reader.getBigDecimal("value"), new BigDecimal("987654.321")); |
46 | 48 | } |
47 | 49 |
|
| 50 | + @Test(dataProvider = "floatColumnValues") |
| 51 | + public void testSerializeFloatColumnFromVariousNumberTypes(String type, Object value, double expected) |
| 52 | + throws IOException { |
| 53 | + RowBinaryWithNamesAndTypesFormatReader reader = serializeSingleValue(type, value); |
| 54 | + |
| 55 | + reader.next(); |
| 56 | + |
| 57 | + Assert.assertEquals(reader.getDouble("value"), expected, 0.0); |
| 58 | + } |
| 59 | + |
| 60 | + @DataProvider(name = "floatColumnValues") |
| 61 | + private Object[][] floatColumnValues() { |
| 62 | + return new Object[][] { |
| 63 | + // A Double supplied for a Float32 column (and a Float for a Float64 column) used to |
| 64 | + // throw ClassCastException; any Number now narrows through Number#floatValue()/ |
| 65 | + // doubleValue(), matching how the Int* branches accept any Number. |
| 66 | + {"Float32", 1.5d, 1.5}, |
| 67 | + {"Float32", -2.5d, -2.5}, |
| 68 | + {"Float64", 1.5f, 1.5}, |
| 69 | + {"Float64", -2.5f, -2.5}, |
| 70 | + |
| 71 | + // Same-type value keeps working unchanged (these already passed before the fix). |
| 72 | + {"Float32", 1.5f, 1.5}, |
| 73 | + {"Float64", 2.5d, 2.5}, |
| 74 | + |
| 75 | + // Other Number subtypes are accepted for both float widths. |
| 76 | + {"Float32", 3, 3.0}, |
| 77 | + {"Float32", 7L, 7.0}, |
| 78 | + {"Float32", (short) 5, 5.0}, |
| 79 | + {"Float32", (byte) 2, 2.0}, |
| 80 | + {"Float32", BigInteger.valueOf(9), 9.0}, |
| 81 | + {"Float32", new BigDecimal("1.5"), 1.5}, |
| 82 | + {"Float32", new CustomNumber("2.5"), 2.5}, |
| 83 | + {"Float64", 3, 3.0}, |
| 84 | + {"Float64", 7L, 7.0}, |
| 85 | + {"Float64", (short) 5, 5.0}, |
| 86 | + {"Float64", (byte) 2, 2.0}, |
| 87 | + {"Float64", BigInteger.valueOf(9), 9.0}, |
| 88 | + {"Float64", new BigDecimal("1.25"), 1.25}, |
| 89 | + {"Float64", new CustomNumber("2.5"), 2.5}, |
| 90 | + |
| 91 | + // String and Boolean are accepted too, matching the other numeric column branches. |
| 92 | + {"Float32", "1.5", 1.5}, |
| 93 | + {"Float32", true, 1.0}, |
| 94 | + {"Float32", false, 0.0}, |
| 95 | + {"Float64", "1.5", 1.5}, |
| 96 | + {"Float64", true, 1.0}, |
| 97 | + {"Float64", false, 0.0}, |
| 98 | + |
| 99 | + // Out-of-Float-range values (now reachable through the fix) narrow like a primitive |
| 100 | + // cast: a Double beyond Float range becomes Float32 infinity rather than throwing. |
| 101 | + {"Float32", Double.MAX_VALUE, Double.POSITIVE_INFINITY}, |
| 102 | + {"Float32", -Double.MAX_VALUE, Double.NEGATIVE_INFINITY}, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testSerializeFloatColumnRejectsUnsupportedValue() { |
| 108 | + Assert.assertThrows(IllegalArgumentException.class, |
| 109 | + () -> serializeSingleValue("Float32", new Object())); |
| 110 | + Assert.assertThrows(IllegalArgumentException.class, |
| 111 | + () -> serializeSingleValue("Float64", new Object())); |
| 112 | + } |
| 113 | + |
48 | 114 | private RowBinaryWithNamesAndTypesFormatReader serializeSingleValue(String type, Object value) |
49 | 115 | throws IOException { |
50 | 116 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
0 commit comments