@Test public void testStandardize() { Map<String, Object> map = Collections.emptyMap(); assertNull(DataTypeTransformer.standardize(COLUMN, map, true)); assertNull(DataTypeTransformer.standardize(COLUMN, map, false)); String expectedValue = "testValue"; map = Collections.singletonMap("testKey", expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, map, true), expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, map, false), expectedValue); Object[] expectedValues = new Object[]{"testValue1", "testValue2"}; map = new HashMap<>(); map.put("testKey1", "testValue1"); map.put("testKey2", "testValue2"); try { DataTypeTransformer.standardize(COLUMN, map, true); fail(); } catch (Exception e) { } assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, map, false), expectedValues); List<Object> list = Collections.emptyList(); assertNull(DataTypeTransformer.standardize(COLUMN, list, true)); assertNull(DataTypeTransformer.standardize(COLUMN, list, false)); list = Collections.singletonList(expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, list, true), expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, list, false), expectedValue); list = Arrays.asList(expectedValues); try { DataTypeTransformer.standardize(COLUMN, list, true); fail(); } catch (Exception e) { } assertEquals((Object[]) DataTypeTransformer.standardize(COLUMN, list, false), expectedValues); Object[] values = new Object[0]; assertNull(DataTypeTransformer.standardize(COLUMN, values, true)); assertNull(DataTypeTransformer.standardize(COLUMN, values, false)); values = new Object[]{expectedValue}; assertEquals(DataTypeTransformer.standardize(COLUMN, values, true), expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, values, false), expectedValue); values = new Object[]{"testValue1", "testValue2"}; try { DataTypeTransformer.standardize(COLUMN, values, true); fail(); } catch (Exception e) { } assertEquals((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues); map = Collections.singletonMap("testKey", Collections.emptyList()); assertNull(DataTypeTransformer.standardize(COLUMN, map, true)); assertNull(DataTypeTransformer.standardize(COLUMN, map, false)); map = Collections.singletonMap("testKey", Collections.singletonList(expectedValue)); assertEquals(DataTypeTransformer.standardize(COLUMN, map, true), expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, map, false), expectedValue); map = new HashMap<>(); map.put("testKey1", Collections.emptyMap()); map.put("testKey2", Collections.singletonMap("testKey", expectedValue)); assertEquals(DataTypeTransformer.standardize(COLUMN, map, true), expectedValue); assertEquals(DataTypeTransformer.standardize(COLUMN, map, false), expectedValue); map = Collections.singletonMap("testKey", Arrays.asList(expectedValues)); try { DataTypeTransformer.standardize(COLUMN, map, true); fail(); } catch (Exception e) { } assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, map, false), expectedValues); map = new HashMap<>(); map.put("testKey1", Collections.emptyMap()); map.put("testKey2", Collections.singletonList("testValue1")); map.put("testKey3", new Object[]{"testValue2"}); try { DataTypeTransformer.standardize(COLUMN, map, true); fail(); } catch (Exception e) { } assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, map, false), expectedValues); list = Arrays .asList(Collections.singletonMap("testKey", "testValue1"), Collections.singletonMap("testKey", "testValue2"), Collections.emptyMap()); try { DataTypeTransformer.standardize(COLUMN, list, true); fail(); } catch (Exception e) { } assertEquals((Object[]) DataTypeTransformer.standardize(COLUMN, list, false), expectedValues); values = new Object[]{Collections.singletonMap("testKey", "testValue1"), Collections.singletonMap("testKey", "testValue2")}; try { DataTypeTransformer.standardize(COLUMN, values, true); fail(); } catch (Exception e) { } assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues); values = new Object[]{new Object[0], Collections.singletonList( Collections.singletonMap("testKey", "testValue1")), Collections.singletonMap("testKey", Arrays.asList(new Object[]{"testValue2"}, Collections.emptyMap()))}; try { DataTypeTransformer.standardize(COLUMN, values, true); fail(); } catch (Exception e) { } assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues); }
@VisibleForTesting @Nullable static Object standardize(String column, @Nullable Object value, boolean isSingleValue) { if (value == null) { return null; } if (value instanceof Collection) { return standardizeCollection(column, (Collection) value, isSingleValue); } if (value instanceof Map) { return standardizeCollection(column, ((Map) value).values(), isSingleValue); } if (value instanceof Object[]) { Object[] values = (Object[]) value; int numValues = values.length; if (numValues == 0) { return null; } if (numValues == 1) { return standardize(column, values[0], isSingleValue); } List<Object> standardizedValues = new ArrayList<>(numValues); for (Object singleValue : values) { Object standardizedValue = standardize(column, singleValue, true); if (standardizedValue != null) { standardizedValues.add(standardizedValue); } } int numStandardizedValues = standardizedValues.size(); if (numStandardizedValues == 0) { return null; } if (numStandardizedValues == 1) { return standardizedValues.get(0); } Preconditions.checkState(!isSingleValue, "Cannot read single-value from Object[]: %s for column: %s", Arrays.toString(values), column); return standardizedValues.toArray(); } return value; }
DataTypeTransformer implements RecordTransformer { @VisibleForTesting @Nullable static Object standardize(String column, @Nullable Object value, boolean isSingleValue) { if (value == null) { return null; } if (value instanceof Collection) { return standardizeCollection(column, (Collection) value, isSingleValue); } if (value instanceof Map) { return standardizeCollection(column, ((Map) value).values(), isSingleValue); } if (value instanceof Object[]) { Object[] values = (Object[]) value; int numValues = values.length; if (numValues == 0) { return null; } if (numValues == 1) { return standardize(column, values[0], isSingleValue); } List<Object> standardizedValues = new ArrayList<>(numValues); for (Object singleValue : values) { Object standardizedValue = standardize(column, singleValue, true); if (standardizedValue != null) { standardizedValues.add(standardizedValue); } } int numStandardizedValues = standardizedValues.size(); if (numStandardizedValues == 0) { return null; } if (numStandardizedValues == 1) { return standardizedValues.get(0); } Preconditions.checkState(!isSingleValue, "Cannot read single-value from Object[]: %s for column: %s", Arrays.toString(values), column); return standardizedValues.toArray(); } return value; } }
DataTypeTransformer implements RecordTransformer { @VisibleForTesting @Nullable static Object standardize(String column, @Nullable Object value, boolean isSingleValue) { if (value == null) { return null; } if (value instanceof Collection) { return standardizeCollection(column, (Collection) value, isSingleValue); } if (value instanceof Map) { return standardizeCollection(column, ((Map) value).values(), isSingleValue); } if (value instanceof Object[]) { Object[] values = (Object[]) value; int numValues = values.length; if (numValues == 0) { return null; } if (numValues == 1) { return standardize(column, values[0], isSingleValue); } List<Object> standardizedValues = new ArrayList<>(numValues); for (Object singleValue : values) { Object standardizedValue = standardize(column, singleValue, true); if (standardizedValue != null) { standardizedValues.add(standardizedValue); } } int numStandardizedValues = standardizedValues.size(); if (numStandardizedValues == 0) { return null; } if (numStandardizedValues == 1) { return standardizedValues.get(0); } Preconditions.checkState(!isSingleValue, "Cannot read single-value from Object[]: %s for column: %s", Arrays.toString(values), column); return standardizedValues.toArray(); } return value; } DataTypeTransformer(Schema schema); }
DataTypeTransformer implements RecordTransformer { @VisibleForTesting @Nullable static Object standardize(String column, @Nullable Object value, boolean isSingleValue) { if (value == null) { return null; } if (value instanceof Collection) { return standardizeCollection(column, (Collection) value, isSingleValue); } if (value instanceof Map) { return standardizeCollection(column, ((Map) value).values(), isSingleValue); } if (value instanceof Object[]) { Object[] values = (Object[]) value; int numValues = values.length; if (numValues == 0) { return null; } if (numValues == 1) { return standardize(column, values[0], isSingleValue); } List<Object> standardizedValues = new ArrayList<>(numValues); for (Object singleValue : values) { Object standardizedValue = standardize(column, singleValue, true); if (standardizedValue != null) { standardizedValues.add(standardizedValue); } } int numStandardizedValues = standardizedValues.size(); if (numStandardizedValues == 0) { return null; } if (numStandardizedValues == 1) { return standardizedValues.get(0); } Preconditions.checkState(!isSingleValue, "Cannot read single-value from Object[]: %s for column: %s", Arrays.toString(values), column); return standardizedValues.toArray(); } return value; } DataTypeTransformer(Schema schema); @Override GenericRow transform(GenericRow record); }
DataTypeTransformer implements RecordTransformer { @VisibleForTesting @Nullable static Object standardize(String column, @Nullable Object value, boolean isSingleValue) { if (value == null) { return null; } if (value instanceof Collection) { return standardizeCollection(column, (Collection) value, isSingleValue); } if (value instanceof Map) { return standardizeCollection(column, ((Map) value).values(), isSingleValue); } if (value instanceof Object[]) { Object[] values = (Object[]) value; int numValues = values.length; if (numValues == 0) { return null; } if (numValues == 1) { return standardize(column, values[0], isSingleValue); } List<Object> standardizedValues = new ArrayList<>(numValues); for (Object singleValue : values) { Object standardizedValue = standardize(column, singleValue, true); if (standardizedValue != null) { standardizedValues.add(standardizedValue); } } int numStandardizedValues = standardizedValues.size(); if (numStandardizedValues == 0) { return null; } if (numStandardizedValues == 1) { return standardizedValues.get(0); } Preconditions.checkState(!isSingleValue, "Cannot read single-value from Object[]: %s for column: %s", Arrays.toString(values), column); return standardizedValues.toArray(); } return value; } DataTypeTransformer(Schema schema); @Override GenericRow transform(GenericRow record);  }