private Schema buildNullSchema(Schema schema) { Schema.Builder builder = Schema.builder(); builder.addFields( schema.getFields().stream().map(f -> f.withNullable(true)).collect(Collectors.toList())); return builder.build(); }
private Schema generateSchema( FieldTypeGenerator fieldTypeGenerator, SourceOfRandomness random, GenerationStatus status) { return IntStream.range(0, status.size() + 1) .mapToObj( i -> Field.of("field_" + i, fieldTypeGenerator.generate(random, status)) .withNullable(true)) .collect(toSchema()); } }
@Test public void testParseCreateExternalTable_minimal() throws Exception { TestTableProvider tableProvider = new TestTableProvider(); BeamSqlEnv env = BeamSqlEnv.withTableProvider(tableProvider); env.executeDdl("CREATE EXTERNAL TABLE person (id INT) TYPE text"); assertEquals( Table.builder() .name("person") .type("text") .schema( Stream.of(Schema.Field.of("id", CalciteUtils.INTEGER).withNullable(true)) .collect(toSchema())) .properties(new JSONObject()) .build(), tableProvider.getTables().get("person")); }
Schema type = Stream.of( Schema.Field.of("f_byte", FieldType.BYTE).withNullable(true), Schema.Field.of("f_int16", FieldType.INT16).withNullable(true), Schema.Field.of("f_int32", FieldType.INT32).withNullable(true), Schema.Field.of("f_int64", FieldType.INT64).withNullable(true), Schema.Field.of("f_decimal", FieldType.DECIMAL).withNullable(true), Schema.Field.of("f_float", FieldType.FLOAT).withNullable(true), Schema.Field.of("f_double", FieldType.DOUBLE).withNullable(true), Schema.Field.of("f_string", FieldType.STRING).withNullable(true), Schema.Field.of("f_datetime", FieldType.DATETIME).withNullable(true), Schema.Field.of("f_boolean", FieldType.BOOLEAN).withNullable(true), Schema.Field.of("f_array", FieldType.array(FieldType.DATETIME)).withNullable(true), Schema.Field.of("f_map", FieldType.map(FieldType.INT32, FieldType.DOUBLE)) .withNullable(true)) .collect(toSchema());
assertEquals( Stream.of( Field.of("id", INTEGER).withDescription("id").withNullable(true), Field.of("name", VARCHAR).withDescription("name").withNullable(true), Field.of("age", INTEGER).withDescription("age").withNullable(true), Field.of( "address", .addNullableField("country", VARCHAR) .build())) .withNullable(true), Field.of( "addressAngular", .addNullableField("country", VARCHAR) .build())) .withNullable(true), Field.of("isRobot", BOOLEAN).withNullable(true)) .collect(toSchema()), table.getSchema());
@Test public void testExecute_createTableWithPrefixMapField() throws Exception { InMemoryMetaStore metaStore = new InMemoryMetaStore(); metaStore.registerProvider(new TextTableProvider()); BeamSqlCli cli = new BeamSqlCli().metaStore(metaStore); cli.execute( "CREATE EXTERNAL TABLE person (\n" + "id int COMMENT 'id', \n" + "name varchar COMMENT 'name', \n" + "age int COMMENT 'age', \n" + "tags MAP<VARCHAR, VARCHAR>, \n" + "nestedMap MAP<INTEGER, MAP<VARCHAR, INTEGER>> \n" + ") \n" + "TYPE 'text' \n" + "COMMENT '' LOCATION '/home/admin/orders'"); Table table = metaStore.getTables().get("person"); assertNotNull(table); assertEquals( Stream.of( Field.of("id", INTEGER).withDescription("id").withNullable(true), Field.of("name", VARCHAR).withDescription("name").withNullable(true), Field.of("age", INTEGER).withDescription("age").withNullable(true), Field.of("tags", Schema.FieldType.map(VARCHAR, VARCHAR)).withNullable(true), Field.of( "nestedMap", Schema.FieldType.map(INTEGER, Schema.FieldType.map(VARCHAR, INTEGER))) .withNullable(true)) .collect(toSchema()), table.getSchema()); }
@Test public void testExecute_createTableWithPrefixArrayField() throws Exception { InMemoryMetaStore metaStore = new InMemoryMetaStore(); metaStore.registerProvider(new TextTableProvider()); BeamSqlCli cli = new BeamSqlCli().metaStore(metaStore); cli.execute( "CREATE EXTERNAL TABLE person (\n" + "id int COMMENT 'id', \n" + "name varchar COMMENT 'name', \n" + "age int COMMENT 'age', \n" + "tags ARRAY<VARCHAR>, \n" + "matrix ARRAY<ARRAY<INTEGER>> \n" + ") \n" + "TYPE 'text' \n" + "COMMENT '' LOCATION '/home/admin/orders'"); Table table = metaStore.getTables().get("person"); assertNotNull(table); assertEquals( Stream.of( Field.of("id", INTEGER).withDescription("id").withNullable(true), Field.of("name", VARCHAR).withDescription("name").withNullable(true), Field.of("age", INTEGER).withDescription("age").withNullable(true), Field.of("tags", Schema.FieldType.array(VARCHAR)).withNullable(true), Field.of("matrix", Schema.FieldType.array(Schema.FieldType.array(INTEGER))) .withNullable(true)) .collect(toSchema()), table.getSchema()); }
@Test public void testExecute_createTextTable() throws Exception { InMemoryMetaStore metaStore = new InMemoryMetaStore(); metaStore.registerProvider(new TextTableProvider()); BeamSqlCli cli = new BeamSqlCli().metaStore(metaStore); cli.execute( "CREATE EXTERNAL TABLE person (\n" + "id int COMMENT 'id', \n" + "name varchar COMMENT 'name', \n" + "age int COMMENT 'age') \n" + "TYPE 'text' \n" + "COMMENT '' LOCATION '/home/admin/orders'"); Table table = metaStore.getTables().get("person"); assertNotNull(table); assertEquals( Stream.of( Field.of("id", INTEGER).withDescription("id").withNullable(true), Field.of("name", VARCHAR).withDescription("name").withNullable(true), Field.of("age", INTEGER).withDescription("age").withNullable(true)) .collect(toSchema()), table.getSchema()); }
private static Table mockTable( String name, String type, String comment, JSONObject properties, String location) { return Table.builder() .name(name) .type(type) .comment(comment) .location(location) .schema( Stream.of( Schema.Field.of("id", CalciteUtils.INTEGER) .withNullable(true) .withDescription("id"), Schema.Field.of("name", CalciteUtils.VARCHAR) .withNullable(true) .withDescription("name")) .collect(toSchema())) .properties(properties) .build(); } }
public static Schema.Field toField(String name, RelDataType calciteType) { return Schema.Field.of(name, toFieldType(calciteType)).withNullable(calciteType.isNullable()); }