|
1 | 1 | package com.databricks.jdbc.api.impl.converters; |
2 | 2 |
|
| 3 | +import static com.databricks.jdbc.api.impl.converters.ArrowToJavaObjectConverter.convert; |
3 | 4 | import static com.databricks.jdbc.api.impl.converters.ArrowToJavaObjectConverter.getZoneIdFromTimeZoneOpt; |
4 | 5 | import static com.databricks.jdbc.common.util.DatabricksTypeUtil.VARIANT; |
5 | 6 | import static org.junit.jupiter.api.Assertions.*; |
@@ -43,6 +44,89 @@ public void testNullObjectConversion() throws SQLException { |
43 | 44 | assertNull(convertedObject); |
44 | 45 | } |
45 | 46 |
|
| 47 | + @Test |
| 48 | + public void testNullHandlingInVarCharVector() throws Exception { |
| 49 | + // Create a VarCharVector with 3 values: non-null, null, and empty string |
| 50 | + disableArrowNullChecking(); |
| 51 | + VarCharVector vector = new VarCharVector("varCharVector", this.bufferAllocator); |
| 52 | + vector.allocateNew(4); |
| 53 | + |
| 54 | + // Set first value: "hello" |
| 55 | + vector.set(0, "hello".getBytes()); |
| 56 | + |
| 57 | + // Second value: null (don't set it, which makes it null by default) |
| 58 | + |
| 59 | + // Third value: empty string (explicitly set as "") |
| 60 | + vector.set(2, "".getBytes()); |
| 61 | + |
| 62 | + // Fourth value: set to null |
| 63 | + vector.setNull(3); |
| 64 | + |
| 65 | + // Set vector value count |
| 66 | + vector.setValueCount(4); |
| 67 | + |
| 68 | + // Case 1: Non-null value |
| 69 | + assertFalse(vector.isNull(0)); |
| 70 | + assertEquals("hello", convert(vector, 0, ColumnInfoTypeName.STRING, "STRING")); |
| 71 | + |
| 72 | + // Case 2: Null value |
| 73 | + assertTrue(vector.isNull(1)); |
| 74 | + assertNull(convert(vector, 1, ColumnInfoTypeName.STRING, "STRING")); |
| 75 | + |
| 76 | + // Case 3: Empty string (should not be treated as null) |
| 77 | + assertFalse(vector.isNull(2)); |
| 78 | + assertEquals( |
| 79 | + "", |
| 80 | + convert( |
| 81 | + vector, |
| 82 | + 2, |
| 83 | + ColumnInfoTypeName.STRING, |
| 84 | + "STRING")); // Empty string should be empty, not null |
| 85 | + |
| 86 | + // Case 4: Explicitly set to null |
| 87 | + assertTrue(vector.isNull(3)); |
| 88 | + String valueWithoutCheck = (String) convert(vector, 3, ColumnInfoTypeName.STRING, "STRING"); |
| 89 | + // This assertion is expected to fail - it shows the problem when isNull check is removed |
| 90 | + assertNull(valueWithoutCheck); |
| 91 | + |
| 92 | + enableArrowNullChecking(); |
| 93 | + } |
| 94 | + |
| 95 | + private void disableArrowNullChecking() { |
| 96 | + System.setProperty("arrow.enable_null_check_for_get", "false"); |
| 97 | + } |
| 98 | + |
| 99 | + private void enableArrowNullChecking() { |
| 100 | + System.setProperty("arrow.enable_null_check_for_get", "true"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testByteVectorWithNullChecks() throws Exception { |
| 105 | + TinyIntVector vector = new TinyIntVector("tinyIntVector", this.bufferAllocator); |
| 106 | + vector.allocateNew(3); |
| 107 | + |
| 108 | + // First value: explicitly set to null |
| 109 | + vector.setNull(0); |
| 110 | + |
| 111 | + // Second value: skip setting it, which makes it null by default |
| 112 | + |
| 113 | + // Third value: set to 0 |
| 114 | + vector.set(2, 0); |
| 115 | + |
| 116 | + vector.setValueCount(3); |
| 117 | + |
| 118 | + // Test our converter with proper null handling |
| 119 | + assertTrue(vector.isNull(0)); |
| 120 | + assertNull(convert(vector, 0, ColumnInfoTypeName.BYTE, "BYTE")); |
| 121 | + |
| 122 | + assertTrue(vector.isNull(1)); |
| 123 | + assertNull(convert(vector, 1, ColumnInfoTypeName.BYTE, "BYTE")); |
| 124 | + |
| 125 | + // The zero value should still be correctly identified as 0, not null |
| 126 | + assertFalse(vector.isNull(2)); |
| 127 | + assertEquals((byte) 0, convert(vector, 2, ColumnInfoTypeName.BYTE, "BYTE")); |
| 128 | + } |
| 129 | + |
46 | 130 | @Test |
47 | 131 | public void testByteConversion() throws SQLException { |
48 | 132 | TinyIntVector tinyIntVector = new TinyIntVector("tinyIntVector", this.bufferAllocator); |
|
0 commit comments