@Test public void testToRowValueBoolean() { DataType field = DataTypes.BooleanType; assertEquals("Invalid Boolean", true, RowUtils.toRowValue(true, field)); assertEquals("Invalid 'true'", true, RowUtils.toRowValue("true", field)); assertEquals("Invalid 'true'", true, RowUtils.toRowValue("TrUe", field)); assertEquals("Invalid 'false'", false, RowUtils.toRowValue("false", field)); assertEquals("Invalid 'false'", false, RowUtils.toRowValue("FaLsE", field)); try { RowUtils.toRowValue(123, field); fail("Expected a RuntimeException for invalid type"); } catch (RuntimeException e) { assertThat(e.getMessage(), CoreMatchers.containsString("Invalid or unrecognized input format")); } }
public static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata) { String typeName = type.typeName(); switch (typeName) { case "binary": if (item instanceof ByteBuffer) { return ((ByteBuffer) item).array(); } else if (item instanceof byte[]) { return item; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format", type)); } case "boolean": if (item instanceof Boolean) { return item; } String str = item.toString(); if ("true".equals(str.toLowerCase())) { return Boolean.TRUE; } else if ("false".equals(str.toLowerCase())) { return Boolean.FALSE; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "date": if (item instanceof Long) { return new java.sql.Date((Long) item); } else if (item instanceof String) { return new java.sql.Date(DateTime.parse((String) item).getMillis()); } else if (item instanceof Date) { return new java.sql.Date(((Date) item).getTime()); } else if (item instanceof DateTime) { return new java.sql.Date(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "timestamp": if (item instanceof Long) { return new Timestamp((Long) item); } else if (item instanceof String) { return new Timestamp(getDateTimeParser(metadata).parse((String) item).getMillis()); } else if (item instanceof Date) { return new Timestamp(((Date) item).getTime()); } else if (item instanceof DateTime) { return new Timestamp(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "double": if (item instanceof Double) { return item; } else if (item instanceof Number) { return ((Number) item).doubleValue(); } else { try { return Double.parseDouble((String) item); } catch (Exception ex) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "float": if (item instanceof Float) { return item; } else if (item instanceof Number) { return ((Number) item).floatValue(); } else { try { return Float.parseFloat((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "integer": if (item instanceof Integer) { return item; } else if (item instanceof Number) { return ((Number) item).intValue(); } else { try { return Integer.parseInt((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "long": if (item instanceof Long) { return item; } else if (item instanceof Number) { return ((Number) item).longValue(); } else { try { return Long.parseLong((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "null": if (item == null) { return null; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "byte": if (item instanceof Byte) { return item; } else if (item instanceof Number) { return ((Number) item).byteValue(); } else { try { return Byte.parseByte((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "short": if (item instanceof Short) { return item; } else if (item instanceof Number) { return ((Number) item).shortValue(); } else { try { return Short.parseShort((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "string": return item.toString(); case "array": DataType elementType = ((ArrayType) type).elementType(); ArrayList<Object> arrayList = new ArrayList<>(); if (item instanceof List) { for (Object value : (List<?>) item) { if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else if (item instanceof Row) { Row row = (Row) item; for (int i = 0; i < row.length(); i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "map": MapType mapType = (MapType) type; DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); if (item instanceof Map) { Map<?, ?> map = (Map<?, ?>) item; for (Object k : map.keySet()) { Object key, value; try { key = toRowValue(k, keyType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized key format: %s[%s]", type, keyType, k)); } value = map.get(k); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(key, value); } return hashMap; } else if (item instanceof Row) { Row row = (Row) item; StructType schema = row.schema(); if (null == schema) { throw new RuntimeException(String.format("Type[%s] - Invalid Row format, no schema found: Row[%s]", type, item)); } String[] fieldNames = schema.fieldNames(); for (int i = 0; i < fieldNames.length; i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(fieldNames[i], value); } return hashMap; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "struct": ArrayList<Object> valueList = new ArrayList<>(); if (item instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> input = (Map<Object, Object>) item; for (StructField f : ((StructType) type).fields()) { if (input.containsKey(f.name())) { Object value = input.get(f.name()); if (null != value) { try { value = toRowValue(value, f.dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, f.dataType(), value)); } } else if (!f.nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, f.dataType())); } valueList.add(value); } else { throw new RuntimeException(String.format("Type[%s] - Key not found on input: %s[%s]", type, f.name(), f.dataType())); } } } else if (item instanceof List) { @SuppressWarnings("unchecked") List<Object> input = (List<Object>) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input List: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else if (item instanceof Row) { Row input = (Row) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input Row: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } return RowFactory.create(valueList.toArray()); default: if (type.typeName().startsWith("decimal")) { if (item instanceof Long) { return BigDecimal.valueOf((Long) item, ((DecimalType) type).scale()); } else if (item instanceof Double) { return BigDecimal.valueOf((Double) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof String) { return new BigDecimal((String) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof BigDecimal) { return item; } else if (item instanceof BigInteger) { return new BigDecimal((BigInteger) item, ((DecimalType) type).scale()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } else { throw new RuntimeException(String.format("Type[%s] - StructField DataType unrecognized or not yet implemented", type)); } } }
RowUtils { public static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata) { String typeName = type.typeName(); switch (typeName) { case "binary": if (item instanceof ByteBuffer) { return ((ByteBuffer) item).array(); } else if (item instanceof byte[]) { return item; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format", type)); } case "boolean": if (item instanceof Boolean) { return item; } String str = item.toString(); if ("true".equals(str.toLowerCase())) { return Boolean.TRUE; } else if ("false".equals(str.toLowerCase())) { return Boolean.FALSE; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "date": if (item instanceof Long) { return new java.sql.Date((Long) item); } else if (item instanceof String) { return new java.sql.Date(DateTime.parse((String) item).getMillis()); } else if (item instanceof Date) { return new java.sql.Date(((Date) item).getTime()); } else if (item instanceof DateTime) { return new java.sql.Date(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "timestamp": if (item instanceof Long) { return new Timestamp((Long) item); } else if (item instanceof String) { return new Timestamp(getDateTimeParser(metadata).parse((String) item).getMillis()); } else if (item instanceof Date) { return new Timestamp(((Date) item).getTime()); } else if (item instanceof DateTime) { return new Timestamp(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "double": if (item instanceof Double) { return item; } else if (item instanceof Number) { return ((Number) item).doubleValue(); } else { try { return Double.parseDouble((String) item); } catch (Exception ex) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "float": if (item instanceof Float) { return item; } else if (item instanceof Number) { return ((Number) item).floatValue(); } else { try { return Float.parseFloat((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "integer": if (item instanceof Integer) { return item; } else if (item instanceof Number) { return ((Number) item).intValue(); } else { try { return Integer.parseInt((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "long": if (item instanceof Long) { return item; } else if (item instanceof Number) { return ((Number) item).longValue(); } else { try { return Long.parseLong((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "null": if (item == null) { return null; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "byte": if (item instanceof Byte) { return item; } else if (item instanceof Number) { return ((Number) item).byteValue(); } else { try { return Byte.parseByte((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "short": if (item instanceof Short) { return item; } else if (item instanceof Number) { return ((Number) item).shortValue(); } else { try { return Short.parseShort((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "string": return item.toString(); case "array": DataType elementType = ((ArrayType) type).elementType(); ArrayList<Object> arrayList = new ArrayList<>(); if (item instanceof List) { for (Object value : (List<?>) item) { if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else if (item instanceof Row) { Row row = (Row) item; for (int i = 0; i < row.length(); i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "map": MapType mapType = (MapType) type; DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); if (item instanceof Map) { Map<?, ?> map = (Map<?, ?>) item; for (Object k : map.keySet()) { Object key, value; try { key = toRowValue(k, keyType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized key format: %s[%s]", type, keyType, k)); } value = map.get(k); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(key, value); } return hashMap; } else if (item instanceof Row) { Row row = (Row) item; StructType schema = row.schema(); if (null == schema) { throw new RuntimeException(String.format("Type[%s] - Invalid Row format, no schema found: Row[%s]", type, item)); } String[] fieldNames = schema.fieldNames(); for (int i = 0; i < fieldNames.length; i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(fieldNames[i], value); } return hashMap; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "struct": ArrayList<Object> valueList = new ArrayList<>(); if (item instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> input = (Map<Object, Object>) item; for (StructField f : ((StructType) type).fields()) { if (input.containsKey(f.name())) { Object value = input.get(f.name()); if (null != value) { try { value = toRowValue(value, f.dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, f.dataType(), value)); } } else if (!f.nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, f.dataType())); } valueList.add(value); } else { throw new RuntimeException(String.format("Type[%s] - Key not found on input: %s[%s]", type, f.name(), f.dataType())); } } } else if (item instanceof List) { @SuppressWarnings("unchecked") List<Object> input = (List<Object>) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input List: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else if (item instanceof Row) { Row input = (Row) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input Row: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } return RowFactory.create(valueList.toArray()); default: if (type.typeName().startsWith("decimal")) { if (item instanceof Long) { return BigDecimal.valueOf((Long) item, ((DecimalType) type).scale()); } else if (item instanceof Double) { return BigDecimal.valueOf((Double) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof String) { return new BigDecimal((String) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof BigDecimal) { return item; } else if (item instanceof BigInteger) { return new BigDecimal((BigInteger) item, ((DecimalType) type).scale()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } else { throw new RuntimeException(String.format("Type[%s] - StructField DataType unrecognized or not yet implemented", type)); } } } }
RowUtils { public static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata) { String typeName = type.typeName(); switch (typeName) { case "binary": if (item instanceof ByteBuffer) { return ((ByteBuffer) item).array(); } else if (item instanceof byte[]) { return item; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format", type)); } case "boolean": if (item instanceof Boolean) { return item; } String str = item.toString(); if ("true".equals(str.toLowerCase())) { return Boolean.TRUE; } else if ("false".equals(str.toLowerCase())) { return Boolean.FALSE; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "date": if (item instanceof Long) { return new java.sql.Date((Long) item); } else if (item instanceof String) { return new java.sql.Date(DateTime.parse((String) item).getMillis()); } else if (item instanceof Date) { return new java.sql.Date(((Date) item).getTime()); } else if (item instanceof DateTime) { return new java.sql.Date(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "timestamp": if (item instanceof Long) { return new Timestamp((Long) item); } else if (item instanceof String) { return new Timestamp(getDateTimeParser(metadata).parse((String) item).getMillis()); } else if (item instanceof Date) { return new Timestamp(((Date) item).getTime()); } else if (item instanceof DateTime) { return new Timestamp(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "double": if (item instanceof Double) { return item; } else if (item instanceof Number) { return ((Number) item).doubleValue(); } else { try { return Double.parseDouble((String) item); } catch (Exception ex) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "float": if (item instanceof Float) { return item; } else if (item instanceof Number) { return ((Number) item).floatValue(); } else { try { return Float.parseFloat((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "integer": if (item instanceof Integer) { return item; } else if (item instanceof Number) { return ((Number) item).intValue(); } else { try { return Integer.parseInt((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "long": if (item instanceof Long) { return item; } else if (item instanceof Number) { return ((Number) item).longValue(); } else { try { return Long.parseLong((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "null": if (item == null) { return null; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "byte": if (item instanceof Byte) { return item; } else if (item instanceof Number) { return ((Number) item).byteValue(); } else { try { return Byte.parseByte((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "short": if (item instanceof Short) { return item; } else if (item instanceof Number) { return ((Number) item).shortValue(); } else { try { return Short.parseShort((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "string": return item.toString(); case "array": DataType elementType = ((ArrayType) type).elementType(); ArrayList<Object> arrayList = new ArrayList<>(); if (item instanceof List) { for (Object value : (List<?>) item) { if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else if (item instanceof Row) { Row row = (Row) item; for (int i = 0; i < row.length(); i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "map": MapType mapType = (MapType) type; DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); if (item instanceof Map) { Map<?, ?> map = (Map<?, ?>) item; for (Object k : map.keySet()) { Object key, value; try { key = toRowValue(k, keyType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized key format: %s[%s]", type, keyType, k)); } value = map.get(k); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(key, value); } return hashMap; } else if (item instanceof Row) { Row row = (Row) item; StructType schema = row.schema(); if (null == schema) { throw new RuntimeException(String.format("Type[%s] - Invalid Row format, no schema found: Row[%s]", type, item)); } String[] fieldNames = schema.fieldNames(); for (int i = 0; i < fieldNames.length; i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(fieldNames[i], value); } return hashMap; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "struct": ArrayList<Object> valueList = new ArrayList<>(); if (item instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> input = (Map<Object, Object>) item; for (StructField f : ((StructType) type).fields()) { if (input.containsKey(f.name())) { Object value = input.get(f.name()); if (null != value) { try { value = toRowValue(value, f.dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, f.dataType(), value)); } } else if (!f.nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, f.dataType())); } valueList.add(value); } else { throw new RuntimeException(String.format("Type[%s] - Key not found on input: %s[%s]", type, f.name(), f.dataType())); } } } else if (item instanceof List) { @SuppressWarnings("unchecked") List<Object> input = (List<Object>) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input List: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else if (item instanceof Row) { Row input = (Row) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input Row: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } return RowFactory.create(valueList.toArray()); default: if (type.typeName().startsWith("decimal")) { if (item instanceof Long) { return BigDecimal.valueOf((Long) item, ((DecimalType) type).scale()); } else if (item instanceof Double) { return BigDecimal.valueOf((Double) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof String) { return new BigDecimal((String) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof BigDecimal) { return item; } else if (item instanceof BigInteger) { return new BigDecimal((BigInteger) item, ((DecimalType) type).scale()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } else { throw new RuntimeException(String.format("Type[%s] - StructField DataType unrecognized or not yet implemented", type)); } } }  }
RowUtils { public static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata) { String typeName = type.typeName(); switch (typeName) { case "binary": if (item instanceof ByteBuffer) { return ((ByteBuffer) item).array(); } else if (item instanceof byte[]) { return item; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format", type)); } case "boolean": if (item instanceof Boolean) { return item; } String str = item.toString(); if ("true".equals(str.toLowerCase())) { return Boolean.TRUE; } else if ("false".equals(str.toLowerCase())) { return Boolean.FALSE; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "date": if (item instanceof Long) { return new java.sql.Date((Long) item); } else if (item instanceof String) { return new java.sql.Date(DateTime.parse((String) item).getMillis()); } else if (item instanceof Date) { return new java.sql.Date(((Date) item).getTime()); } else if (item instanceof DateTime) { return new java.sql.Date(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "timestamp": if (item instanceof Long) { return new Timestamp((Long) item); } else if (item instanceof String) { return new Timestamp(getDateTimeParser(metadata).parse((String) item).getMillis()); } else if (item instanceof Date) { return new Timestamp(((Date) item).getTime()); } else if (item instanceof DateTime) { return new Timestamp(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "double": if (item instanceof Double) { return item; } else if (item instanceof Number) { return ((Number) item).doubleValue(); } else { try { return Double.parseDouble((String) item); } catch (Exception ex) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "float": if (item instanceof Float) { return item; } else if (item instanceof Number) { return ((Number) item).floatValue(); } else { try { return Float.parseFloat((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "integer": if (item instanceof Integer) { return item; } else if (item instanceof Number) { return ((Number) item).intValue(); } else { try { return Integer.parseInt((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "long": if (item instanceof Long) { return item; } else if (item instanceof Number) { return ((Number) item).longValue(); } else { try { return Long.parseLong((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "null": if (item == null) { return null; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "byte": if (item instanceof Byte) { return item; } else if (item instanceof Number) { return ((Number) item).byteValue(); } else { try { return Byte.parseByte((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "short": if (item instanceof Short) { return item; } else if (item instanceof Number) { return ((Number) item).shortValue(); } else { try { return Short.parseShort((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "string": return item.toString(); case "array": DataType elementType = ((ArrayType) type).elementType(); ArrayList<Object> arrayList = new ArrayList<>(); if (item instanceof List) { for (Object value : (List<?>) item) { if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else if (item instanceof Row) { Row row = (Row) item; for (int i = 0; i < row.length(); i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "map": MapType mapType = (MapType) type; DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); if (item instanceof Map) { Map<?, ?> map = (Map<?, ?>) item; for (Object k : map.keySet()) { Object key, value; try { key = toRowValue(k, keyType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized key format: %s[%s]", type, keyType, k)); } value = map.get(k); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(key, value); } return hashMap; } else if (item instanceof Row) { Row row = (Row) item; StructType schema = row.schema(); if (null == schema) { throw new RuntimeException(String.format("Type[%s] - Invalid Row format, no schema found: Row[%s]", type, item)); } String[] fieldNames = schema.fieldNames(); for (int i = 0; i < fieldNames.length; i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(fieldNames[i], value); } return hashMap; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "struct": ArrayList<Object> valueList = new ArrayList<>(); if (item instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> input = (Map<Object, Object>) item; for (StructField f : ((StructType) type).fields()) { if (input.containsKey(f.name())) { Object value = input.get(f.name()); if (null != value) { try { value = toRowValue(value, f.dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, f.dataType(), value)); } } else if (!f.nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, f.dataType())); } valueList.add(value); } else { throw new RuntimeException(String.format("Type[%s] - Key not found on input: %s[%s]", type, f.name(), f.dataType())); } } } else if (item instanceof List) { @SuppressWarnings("unchecked") List<Object> input = (List<Object>) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input List: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else if (item instanceof Row) { Row input = (Row) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input Row: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } return RowFactory.create(valueList.toArray()); default: if (type.typeName().startsWith("decimal")) { if (item instanceof Long) { return BigDecimal.valueOf((Long) item, ((DecimalType) type).scale()); } else if (item instanceof Double) { return BigDecimal.valueOf((Double) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof String) { return new BigDecimal((String) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof BigDecimal) { return item; } else if (item instanceof BigInteger) { return new BigDecimal((BigInteger) item, ((DecimalType) type).scale()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } else { throw new RuntimeException(String.format("Type[%s] - StructField DataType unrecognized or not yet implemented", type)); } } }  static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata); static Object toRowValue(Object item, DataType type); static Row subsetRow(Row row, StructType subsetSchema); static Row set(Row row, String fieldName, Object replacement); static Row append(Row row, Object value); static Row append(Row row, String fieldName, DataType fieldType, boolean nullable, Object value); static Row append(Row row, Row append); static Row remove(Row row, String fieldName); static Row remove(Row row, List<String> fieldNames); static Object[] valuesFor(Row row); static boolean different(Row first, Row second, List<String> valueFieldNames); static Column[] toColumnArray(List<String> columnList); }
RowUtils { public static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata) { String typeName = type.typeName(); switch (typeName) { case "binary": if (item instanceof ByteBuffer) { return ((ByteBuffer) item).array(); } else if (item instanceof byte[]) { return item; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format", type)); } case "boolean": if (item instanceof Boolean) { return item; } String str = item.toString(); if ("true".equals(str.toLowerCase())) { return Boolean.TRUE; } else if ("false".equals(str.toLowerCase())) { return Boolean.FALSE; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "date": if (item instanceof Long) { return new java.sql.Date((Long) item); } else if (item instanceof String) { return new java.sql.Date(DateTime.parse((String) item).getMillis()); } else if (item instanceof Date) { return new java.sql.Date(((Date) item).getTime()); } else if (item instanceof DateTime) { return new java.sql.Date(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "timestamp": if (item instanceof Long) { return new Timestamp((Long) item); } else if (item instanceof String) { return new Timestamp(getDateTimeParser(metadata).parse((String) item).getMillis()); } else if (item instanceof Date) { return new Timestamp(((Date) item).getTime()); } else if (item instanceof DateTime) { return new Timestamp(((DateTime) item).getMillis()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "double": if (item instanceof Double) { return item; } else if (item instanceof Number) { return ((Number) item).doubleValue(); } else { try { return Double.parseDouble((String) item); } catch (Exception ex) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "float": if (item instanceof Float) { return item; } else if (item instanceof Number) { return ((Number) item).floatValue(); } else { try { return Float.parseFloat((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "integer": if (item instanceof Integer) { return item; } else if (item instanceof Number) { return ((Number) item).intValue(); } else { try { return Integer.parseInt((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "long": if (item instanceof Long) { return item; } else if (item instanceof Number) { return ((Number) item).longValue(); } else { try { return Long.parseLong((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "null": if (item == null) { return null; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "byte": if (item instanceof Byte) { return item; } else if (item instanceof Number) { return ((Number) item).byteValue(); } else { try { return Byte.parseByte((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "short": if (item instanceof Short) { return item; } else if (item instanceof Number) { return ((Number) item).shortValue(); } else { try { return Short.parseShort((String) item); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } case "string": return item.toString(); case "array": DataType elementType = ((ArrayType) type).elementType(); ArrayList<Object> arrayList = new ArrayList<>(); if (item instanceof List) { for (Object value : (List<?>) item) { if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else if (item instanceof Row) { Row row = (Row) item; for (int i = 0; i < row.length(); i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, elementType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, elementType, value)); } } else if (!((ArrayType) type).containsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, elementType)); } arrayList.add(value); } return arrayList; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "map": MapType mapType = (MapType) type; DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); if (item instanceof Map) { Map<?, ?> map = (Map<?, ?>) item; for (Object k : map.keySet()) { Object key, value; try { key = toRowValue(k, keyType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized key format: %s[%s]", type, keyType, k)); } value = map.get(k); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(key, value); } return hashMap; } else if (item instanceof Row) { Row row = (Row) item; StructType schema = row.schema(); if (null == schema) { throw new RuntimeException(String.format("Type[%s] - Invalid Row format, no schema found: Row[%s]", type, item)); } String[] fieldNames = schema.fieldNames(); for (int i = 0; i < fieldNames.length; i++) { Object value = row.get(i); if (null != value) { try { value = toRowValue(value, valueType); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, valueType, value)); } } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); } hashMap.put(fieldNames[i], value); } return hashMap; } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } case "struct": ArrayList<Object> valueList = new ArrayList<>(); if (item instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> input = (Map<Object, Object>) item; for (StructField f : ((StructType) type).fields()) { if (input.containsKey(f.name())) { Object value = input.get(f.name()); if (null != value) { try { value = toRowValue(value, f.dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, f.dataType(), value)); } } else if (!f.nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, f.dataType())); } valueList.add(value); } else { throw new RuntimeException(String.format("Type[%s] - Key not found on input: %s[%s]", type, f.name(), f.dataType())); } } } else if (item instanceof List) { @SuppressWarnings("unchecked") List<Object> input = (List<Object>) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input List: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized element format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Element cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else if (item instanceof Row) { Row input = (Row) item; StructField[] fields = ((StructType) type).fields(); if (fields.length != input.size()) { throw new RuntimeException(String.format("Type[%s] - Invalid size of input Row: %s", type, item)); } for (int i = 0; i < fields.length; i++) { Object value = input.get(i); if (null != value) { try { value = toRowValue(value, fields[i].dataType()); } catch (Exception e) { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized value format: %s[%s]", type, fields[i].dataType(), value)); } } else if (!fields[i].nullable()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, fields[i].dataType())); } valueList.add(value); } } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } return RowFactory.create(valueList.toArray()); default: if (type.typeName().startsWith("decimal")) { if (item instanceof Long) { return BigDecimal.valueOf((Long) item, ((DecimalType) type).scale()); } else if (item instanceof Double) { return BigDecimal.valueOf((Double) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof String) { return new BigDecimal((String) item).setScale(((DecimalType) type).scale(), RoundingMode.HALF_DOWN); } else if (item instanceof BigDecimal) { return item; } else if (item instanceof BigInteger) { return new BigDecimal((BigInteger) item, ((DecimalType) type).scale()); } else { throw new RuntimeException(String.format("Type[%s] - Invalid or unrecognized input format: %s", type, item)); } } else { throw new RuntimeException(String.format("Type[%s] - StructField DataType unrecognized or not yet implemented", type)); } } }  static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata); static Object toRowValue(Object item, DataType type); static Row subsetRow(Row row, StructType subsetSchema); static Row set(Row row, String fieldName, Object replacement); static Row append(Row row, Object value); static Row append(Row row, String fieldName, DataType fieldType, boolean nullable, Object value); static Row append(Row row, Row append); static Row remove(Row row, String fieldName); static Row remove(Row row, List<String> fieldNames); static Object[] valuesFor(Row row); static boolean different(Row first, Row second, List<String> valueFieldNames); static Column[] toColumnArray(List<String> columnList);  }