FieldParser { public static ByteBuffer parse(String val, ColumnSchema schema) throws ParseException { checkNotNull(val, "Should not be parsing null. Something went terribly wrong."); checkNotNull(schema, "Column metadata is null."); ColumnType type = schema.getType(); if (val.length() == 0 && type != ColumnType.STRING && type != ColumnType.BINARY) { if (schema.getIsNullable()) { return null; } throw new IllegalArgumentException("Expected a value for a" + " non-null SQL column, but no value was given."); } switch (type) { case LONG: return ByteBuffer.wrap(Longs.toByteArray(Long.parseLong(val))); case ULONG: BigInteger n = new BigInteger(val); if (n.compareTo(BigInteger.ZERO) == -1) { throw new IllegalArgumentException("negative value provided for unsigned column. value: " + val); } return ByteBuffer.wrap(Longs.toByteArray(n.longValue())); case DOUBLE: return ByteBuffer.wrap(Bytes.toBytes(Double.parseDouble(val))); case DATE: return extractDate(val, "yyyy-MM-dd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd", "yyyyMMdd"); case TIME: return extractDate(val, "HH:mm:ss", "HH:mm:ss", "HHmmss"); case DATETIME: return extractDate(val, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss", "yyyyMMdd HHmmss"); case DECIMAL: return extractDecimal(val, schema.getPrecision(), schema.getScale()); case STRING: case BINARY: default: return ByteBuffer.wrap(val.getBytes(Charset.forName("UTF-8"))); } } private  FieldParser(); static ByteBuffer parse(String val, ColumnSchema schema);  }
@Test(expected = NullPointerException.class) public void testParseNullValue() throws ParseException { FieldParser.parse(null, ColumnSchema.builder(COLUMN_NAME, ColumnType.LONG).build()); }
@Test(expected = NullPointerException.class) public void testParseNullMetadata() throws ParseException { FieldParser.parse("foo", null); }
@Test public void testParseEmptyValueNullableMetadataLongType() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.LONG) .build(); assertNull(FieldParser.parse(EMPTY_STRING, schema)); }
@Test(expected = IllegalArgumentException.class) public void testParseEmptyValueNonNullableSchemaLongType() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.LONG) .setIsNullable(false) .build(); assertNull(FieldParser.parse(EMPTY_STRING, schema)); }
@Test public void testParseLong() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.LONG) .build(); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x00L)), FieldParser.parse("0", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0xFFFFFFFFFFFFFF85L)), FieldParser.parse("-123", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x7BL)), FieldParser.parse("123", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x7FFFFFFFFFFFFFFFL)), FieldParser.parse("9223372036854775807", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x8000000000000000L)), FieldParser.parse("-9223372036854775808", schema)); }
@Test public void testParseULong() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.ULONG) .build(); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x00L)), FieldParser.parse("0", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x7BL)), FieldParser.parse("123", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0xFFFFFFFFFFFFFFFFL)), FieldParser.parse("18446744073709551615", schema)); }
@Test(expected = IllegalArgumentException.class) public void testParseULongNegativeInput() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.ULONG) .build(); FieldParser.parse("-123", schema); }
@Test public void testParseDoubleZeroValue() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DOUBLE) .build(); assertEquals(ByteBuffer.wrap(Bytes.toBytes(0x00L)), FieldParser.parse("0.0", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0x40283D70A3D70A3DL)), FieldParser.parse("12.12", schema)); assertEquals(ByteBuffer.wrap(Longs.toByteArray(0xC0283D70A3D70A3DL)), FieldParser.parse("-12.12", schema)); }
@Test public void testParseValidDateFormats() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DATE) .build(); final String expectedParsedDate = "1989-05-13"; final List<String> formats = ImmutableList.of(expectedParsedDate, "1989.05.13", "1989/05/13", "19890513"); for (final String format : formats) { assertEquals(ByteBuffer.wrap(expectedParsedDate.getBytes()), FieldParser.parse(format, schema)); } }
@Test(expected = ParseException.class) public void testParseInvalidDateFormat() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DATE) .build(); FieldParser.parse("1989_05_13", schema); }
@Test public void testParseTime() throws Exception { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.TIME) .build(); final String expectedParsedTime = "07:32:15"; final List<String> formats = ImmutableList.of(expectedParsedTime, "073215"); for (final String format : formats) { assertEquals(ByteBuffer.wrap(expectedParsedTime.getBytes()), FieldParser.parse(format, schema)); } }
@Test(expected = ParseException.class) public void testParseInvalidTimeFormat() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.TIME) .build(); FieldParser.parse("07_32_15", schema); }
@Test public void testParseDateTime() throws Exception { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DATETIME) .build(); final String expectedParsedDateTime = "1989-05-13 07:32:15"; final List<String> formats = ImmutableList .of(expectedParsedDateTime, "1989.05.13 07:32:15", "1989/05/13 07:32:15", "19890513 073215"); for (final String format : formats) { assertEquals(ByteBuffer.wrap(expectedParsedDateTime.getBytes()), FieldParser.parse(format, schema)); } }
@Test(expected = ParseException.class) public void testParseInvalidDateTimeFormat() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DATETIME) .build(); FieldParser.parse("1989_05_13_07_32_15", schema); }
@Test public void testParseDecimal() throws Exception { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DECIMAL) .setPrecision(5) .setScale(2) .build(); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x807B2DL), 5, 8)), FieldParser.parse("123.45", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x7F84D2L), 5, 8)), FieldParser.parse("-123.45", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x800000L), 5, 8)), FieldParser.parse("000.00", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x800000L), 5, 8)), FieldParser.parse("-000.00", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x83E763L), 5, 8)), FieldParser.parse("999.99", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x7C189CL), 5, 8)), FieldParser.parse("-999.99", schema)); schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.DECIMAL) .setPrecision(10) .setScale(3) .build(); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x008012D687037AL), 2, 8)), FieldParser.parse("1234567.890", schema)); assertEquals(ByteBuffer.wrap(Arrays.copyOfRange(Bytes.toBytes(0x008000000501F4L), 2, 8)), FieldParser.parse("5.5", schema)); }
@Test public void testParseStringEmptyValue() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.STRING) .setMaxLength(32) .build(); assertEquals(ByteBuffer.wrap(EMPTY_STRING.getBytes(Charsets.UTF_8)), FieldParser.parse(EMPTY_STRING, schema)); }
@Test public void testParseBinaryEmptyValue() throws ParseException { ColumnSchema schema = ColumnSchema .builder(COLUMN_NAME, ColumnType.BINARY) .setMaxLength(32) .build(); assertEquals(ByteBuffer.wrap(EMPTY_STRING.getBytes(Charsets.UTF_8)), FieldParser.parse(EMPTY_STRING, schema)); }