public Builder addNullableField(String name, FieldType type) { fields.add(Field.nullable(name, type)); return this; }
private static Table mockTable(String name) { JSONObject properties = new JSONObject(); properties.put("bootstrap.servers", "localhost:9092"); JSONArray topics = new JSONArray(); topics.add("topic1"); topics.add("topic2"); properties.put("topics", topics); return Table.builder() .name(name) .comment(name + " table") .location("kafka://localhost:2181/brokers?topic=test") .schema( Stream.of( Schema.Field.nullable("id", Schema.FieldType.INT32), Schema.Field.nullable("name", Schema.FieldType.STRING)) .collect(toSchema())) .type("kafka") .properties(properties) .build(); } }
private static Table mockTable(String name, String type) { return Table.builder() .name(name) .comment(name + " table") .location("/home/admin/" + name) .schema( Stream.of( Schema.Field.nullable("id", Schema.FieldType.INT32), Schema.Field.nullable("name", Schema.FieldType.STRING)) .collect(toSchema())) .type(type) .properties(new JSONObject()) .build(); }
private static Table fakeTable(String name) { return Table.builder() .name(name) .comment(name + " table") .location("project:dataset.table") .schema( Stream.of( Schema.Field.nullable("id", Schema.FieldType.INT32), Schema.Field.nullable("name", Schema.FieldType.STRING)) .collect(toSchema())) .type("bigquery") .build(); } }
@Test(expected = IllegalArgumentException.class) public void testIgnoreNullFail() { Schema inputSchema = Schema.of(Schema.Field.nullable("f0", Schema.FieldType.INT32)); Schema outputSchema = Schema.of(Schema.Field.of("f0", Schema.FieldType.INT32)); Cast.castRow(Row.withSchema(inputSchema).addValue(null).build(), inputSchema, outputSchema); }
@Test public void testNarrowingNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Narrowing.of().apply(input, output); assertThat(errors, empty()); } }
@Test public void testWideningNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Widening.of().apply(input, output); Cast.CompatibilityError expected = Cast.CompatibilityError.create( Arrays.asList("f0"), "Can't cast nullable field to non-nullable field"); assertThat(errors, containsInAnyOrder(expected)); }