|
13 | 13 | import junit.framework.TestCase; |
14 | 14 | import org.apache.avro.Schema; |
15 | 15 | import org.apache.avro.generic.GenericData; |
| 16 | +import org.apache.avro.util.Utf8; |
16 | 17 | import org.jetbrains.annotations.NotNull; |
17 | 18 | import org.junit.Test; |
18 | 19 |
|
@@ -192,4 +193,116 @@ public void testTimestamp() throws IOException { |
192 | 193 | } |
193 | 194 | } |
194 | 195 | } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testStringArray() throws IOException { |
| 199 | + final Schema avroSchema = getSchema("stringarray.avsc"); |
| 200 | + |
| 201 | + final String[] names = new String[] {"A", "B", "C", "Result"}; |
| 202 | + final Class<?>[] types = new Class[] {int.class, String.class, double.class, String[].class}; |
| 203 | + final TableDefinition definition = TableDefinition.from(Arrays.asList(names), Arrays.asList(types)); |
| 204 | + |
| 205 | + final GenericData.Record genericRecord = new GenericData.Record(avroSchema); |
| 206 | + genericRecord.put("A", 123); |
| 207 | + genericRecord.put("B", "hello"); |
| 208 | + genericRecord.put("C", 45.67); |
| 209 | + genericRecord.put("Result", |
| 210 | + new GenericData.Array<>(avroSchema.getField("Result").schema(), Arrays.asList("pass", "ok", "done"))); |
| 211 | + |
| 212 | + final Map<String, String> colMap = new HashMap<>(); |
| 213 | + colMap.put("A", "A"); |
| 214 | + colMap.put("B", "B"); |
| 215 | + colMap.put("C", "C"); |
| 216 | + colMap.put("Result", "Result"); |
| 217 | + |
| 218 | + try (final WritableObjectChunk<Object, Values> inputValues = |
| 219 | + WritableObjectChunk.makeWritableChunk(1)) { |
| 220 | + inputValues.setSize(0); |
| 221 | + inputValues.add(genericRecord); |
| 222 | + |
| 223 | + final WritableChunk[] output = new WritableChunk[names.length]; |
| 224 | + try (final SafeCloseableArray ignored = new SafeCloseableArray(output)) { |
| 225 | + output[0] = WritableIntChunk.makeWritableChunk(1); |
| 226 | + output[1] = WritableObjectChunk.makeWritableChunk(1); |
| 227 | + output[2] = WritableDoubleChunk.makeWritableChunk(1); |
| 228 | + output[3] = WritableObjectChunk.makeWritableChunk(1); |
| 229 | + |
| 230 | + for (WritableChunk wc : output) { |
| 231 | + wc.setSize(0); |
| 232 | + } |
| 233 | + |
| 234 | + final GenericRecordChunkAdapter adapter = GenericRecordChunkAdapter.make(definition, |
| 235 | + (idx) -> output[idx].getChunkType(), colMap, Pattern.compile(Pattern.quote(".")), avroSchema, |
| 236 | + true); |
| 237 | + adapter.handleChunk(inputValues, output); |
| 238 | + |
| 239 | + TestCase.assertEquals(1, output[0].size()); |
| 240 | + TestCase.assertEquals(1, output[1].size()); |
| 241 | + TestCase.assertEquals(1, output[2].size()); |
| 242 | + TestCase.assertEquals(1, output[3].size()); |
| 243 | + |
| 244 | + TestCase.assertEquals(123, output[0].asIntChunk().get(0)); |
| 245 | + TestCase.assertEquals("hello", output[1].asObjectChunk().get(0)); |
| 246 | + TestCase.assertEquals(45.67, output[2].asDoubleChunk().get(0)); |
| 247 | + TestCase.assertTrue(Arrays.equals(new String[] {"pass", "ok", "done"}, |
| 248 | + (String[]) output[3].asObjectChunk().get(0))); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + public void testUtf8Array() throws IOException { |
| 255 | + final Schema avroSchema = getSchema("stringarray.avsc"); |
| 256 | + |
| 257 | + final String[] names = new String[] {"A", "B", "C", "Result"}; |
| 258 | + final Class<?>[] types = new Class[] {int.class, String.class, double.class, String[].class}; |
| 259 | + final TableDefinition definition = TableDefinition.from(Arrays.asList(names), Arrays.asList(types)); |
| 260 | + |
| 261 | + final GenericData.Record genericRecord = new GenericData.Record(avroSchema); |
| 262 | + genericRecord.put("A", 123); |
| 263 | + genericRecord.put("B", "hello"); |
| 264 | + genericRecord.put("C", 45.67); |
| 265 | + genericRecord.put("Result", new GenericData.Array<>(avroSchema.getField("Result").schema(), |
| 266 | + Arrays.asList(new Utf8("pass"), new Utf8("ok"), new Utf8("done")))); |
| 267 | + |
| 268 | + final Map<String, String> colMap = new HashMap<>(); |
| 269 | + colMap.put("A", "A"); |
| 270 | + colMap.put("B", "B"); |
| 271 | + colMap.put("C", "C"); |
| 272 | + colMap.put("Result", "Result"); |
| 273 | + |
| 274 | + try (final WritableObjectChunk<Object, Values> inputValues = |
| 275 | + WritableObjectChunk.makeWritableChunk(1)) { |
| 276 | + inputValues.setSize(0); |
| 277 | + inputValues.add(genericRecord); |
| 278 | + |
| 279 | + final WritableChunk[] output = new WritableChunk[names.length]; |
| 280 | + try (final SafeCloseableArray ignored = new SafeCloseableArray(output)) { |
| 281 | + output[0] = WritableIntChunk.makeWritableChunk(1); |
| 282 | + output[1] = WritableObjectChunk.makeWritableChunk(1); |
| 283 | + output[2] = WritableDoubleChunk.makeWritableChunk(1); |
| 284 | + output[3] = WritableObjectChunk.makeWritableChunk(1); |
| 285 | + |
| 286 | + for (WritableChunk wc : output) { |
| 287 | + wc.setSize(0); |
| 288 | + } |
| 289 | + |
| 290 | + final GenericRecordChunkAdapter adapter = GenericRecordChunkAdapter.make(definition, |
| 291 | + (idx) -> output[idx].getChunkType(), colMap, Pattern.compile(Pattern.quote(".")), avroSchema, |
| 292 | + true); |
| 293 | + adapter.handleChunk(inputValues, output); |
| 294 | + |
| 295 | + TestCase.assertEquals(1, output[0].size()); |
| 296 | + TestCase.assertEquals(1, output[1].size()); |
| 297 | + TestCase.assertEquals(1, output[2].size()); |
| 298 | + TestCase.assertEquals(1, output[3].size()); |
| 299 | + |
| 300 | + TestCase.assertEquals(123, output[0].asIntChunk().get(0)); |
| 301 | + TestCase.assertEquals("hello", output[1].asObjectChunk().get(0)); |
| 302 | + TestCase.assertEquals(45.67, output[2].asDoubleChunk().get(0)); |
| 303 | + TestCase.assertTrue(Arrays.equals(new String[] {"pass", "ok", "done"}, |
| 304 | + (String[]) output[3].asObjectChunk().get(0))); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
195 | 308 | } |
0 commit comments