@Test public void testRuntimeDecodingFailure() { assertRuntimeDecodingFailure(() -> fieldValueDecoderFor(BigintType.BIGINT, "blah").getLong()); }
@Test public void testSupportedDataTypeValidation() { // supported types singleColumnDecoder(BigintType.BIGINT); singleColumnDecoder(IntegerType.INTEGER); singleColumnDecoder(SmallintType.SMALLINT); singleColumnDecoder(TinyintType.TINYINT); singleColumnDecoder(BooleanType.BOOLEAN); singleColumnDecoder(DoubleType.DOUBLE); singleColumnDecoder(createUnboundedVarcharType()); singleColumnDecoder(createVarcharType(100)); // some unsupported types assertUnsupportedColumnTypeException(() -> singleColumnDecoder(RealType.REAL)); assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4))); assertUnsupportedColumnTypeException(() -> singleColumnDecoder(VarbinaryType.VARBINARY)); }
private void singleColumnDecoder(Type columnType) { singleColumnDecoder(columnType, "0", null, null, false, false, false); }
@Test public void testNulls() { String csv = ",,,"; CsvRowDecoder rowDecoder = new CsvRowDecoder(); DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", VarcharType.VARCHAR, "0", null, null, false, false, false); DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", BigintType.BIGINT, "1", null, null, false, false, false); DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", DoubleType.DOUBLE, "2", null, null, false, false, false); DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BooleanType.BOOLEAN, "3", null, null, false, false, false); List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4); Set<FieldValueProvider> providers = new HashSet<>(); boolean corrupt = rowDecoder.decodeRow(csv.getBytes(StandardCharsets.UTF_8), null, providers, columns, buildMap(columns)); assertFalse(corrupt); assertEquals(providers.size(), columns.size()); checkValue(providers, row1, ""); checkValue(providers, row2, 0); checkValue(providers, row3, 0.0d); checkValue(providers, row4, false); } }
@Test public void testInvalidExtraneousParameters() { assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "0", "format", null, false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("unexpected data format 'format' defined for column 'column'"); assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "0", null, "hint", false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("unexpected format hint 'hint' defined for column 'column'"); assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "0", null, null, false, false, true)) .isInstanceOf(PrestoException.class) .hasMessageMatching("unexpected internal column 'column'"); }
@Test public void testSimple() { String csv = "\"row 1\",row2,\"row3\",100,\"200\",300,4.5"; CsvRowDecoder rowDecoder = new CsvRowDecoder(); DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", VarcharType.VARCHAR, "0", null, null, false, false, false); DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", VarcharType.VARCHAR, "1", null, null, false, false, false); DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", VarcharType.VARCHAR, "2", null, null, false, false, false); DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BigintType.BIGINT, "3", null, null, false, false, false); DecoderTestColumnHandle row5 = new DecoderTestColumnHandle("", 4, "row5", BigintType.BIGINT, "4", null, null, false, false, false); DecoderTestColumnHandle row6 = new DecoderTestColumnHandle("", 5, "row6", BigintType.BIGINT, "5", null, null, false, false, false); DecoderTestColumnHandle row7 = new DecoderTestColumnHandle("", 6, "row7", DoubleType.DOUBLE, "6", null, null, false, false, false); List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4, row5, row6, row7); Set<FieldValueProvider> providers = new HashSet<>(); boolean corrupt = rowDecoder.decodeRow(csv.getBytes(StandardCharsets.UTF_8), null, providers, columns, buildMap(columns)); assertFalse(corrupt); assertEquals(providers.size(), columns.size()); checkValue(providers, row1, "row 1"); checkValue(providers, row2, "row2"); checkValue(providers, row3, "row3"); checkValue(providers, row4, 100); checkValue(providers, row5, 200); checkValue(providers, row6, 300); checkValue(providers, row7, 4.5d); }
@Test public void testWrongMappingDefined() { assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, null, null, null, false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("mapping not defined for column 'column'"); assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "x", null, null, false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("invalid mapping 'x' for column 'column'"); assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "-1", null, null, false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("invalid mapping '-1' for column 'column'"); assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "1:1", null, null, false, false, false)) .isInstanceOf(PrestoException.class) .hasMessageMatching("invalid mapping '1:1' for column 'column'"); }
@Test public void testBoolean() { String csv = "True,False,0,1,\"0\",\"1\",\"true\",\"false\""; CsvRowDecoder rowDecoder = new CsvRowDecoder(); DecoderTestColumnHandle row1 = new DecoderTestColumnHandle("", 0, "row1", BooleanType.BOOLEAN, "0", null, null, false, false, false); DecoderTestColumnHandle row2 = new DecoderTestColumnHandle("", 1, "row2", BooleanType.BOOLEAN, "1", null, null, false, false, false); DecoderTestColumnHandle row3 = new DecoderTestColumnHandle("", 2, "row3", BooleanType.BOOLEAN, "2", null, null, false, false, false); DecoderTestColumnHandle row4 = new DecoderTestColumnHandle("", 3, "row4", BooleanType.BOOLEAN, "3", null, null, false, false, false); DecoderTestColumnHandle row5 = new DecoderTestColumnHandle("", 4, "row5", BooleanType.BOOLEAN, "4", null, null, false, false, false); DecoderTestColumnHandle row6 = new DecoderTestColumnHandle("", 5, "row6", BooleanType.BOOLEAN, "5", null, null, false, false, false); DecoderTestColumnHandle row7 = new DecoderTestColumnHandle("", 6, "row7", BooleanType.BOOLEAN, "6", null, null, false, false, false); DecoderTestColumnHandle row8 = new DecoderTestColumnHandle("", 7, "row8", BooleanType.BOOLEAN, "7", null, null, false, false, false); List<DecoderColumnHandle> columns = ImmutableList.of(row1, row2, row3, row4, row5, row6, row7, row8); Set<FieldValueProvider> providers = new HashSet<>(); boolean corrupt = rowDecoder.decodeRow(csv.getBytes(StandardCharsets.UTF_8), null, providers, columns, buildMap(columns)); assertFalse(corrupt); assertEquals(providers.size(), columns.size()); checkValue(providers, row1, true); checkValue(providers, row2, false); checkValue(providers, row3, false); checkValue(providers, row4, false); checkValue(providers, row5, false); checkValue(providers, row6, false); checkValue(providers, row7, true); checkValue(providers, row8, false); }