use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaCommands method testCreateWithVariousColumnProperties.
@Test
public void testCreateWithVariousColumnProperties() throws Exception {
File tmpDir = dirTestWatcher.getTmpDir();
File schemaFile = new File(tmpDir, "schema_for_create_with_various_column_properties.schema");
assertFalse(schemaFile.exists());
try {
testBuilder().sqlQuery("create schema ( " + "a int not null default '10', " + "b date format 'yyyy-MM-dd' default '2017-01-31', " + "c varchar properties {'k1' = 'v1', 'k2' = 'v2'}) " + "path '%s'", schemaFile.getPath()).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", schemaFile.getPath())).go();
SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaFile.getPath()));
assertTrue(schemaProvider.exists());
SchemaContainer schemaContainer = schemaProvider.read();
assertNull(schemaContainer.getTable());
TupleMetadata schema = schemaContainer.getSchema();
assertNotNull(schema);
assertEquals(3, schema.size());
ColumnMetadata a = schema.metadata("a");
assertTrue(a.decodeDefaultValue() instanceof Integer);
assertEquals(10, a.decodeDefaultValue());
assertEquals("10", a.defaultValue());
ColumnMetadata b = schema.metadata("b");
assertTrue(b.decodeDefaultValue() instanceof LocalDate);
assertEquals("yyyy-MM-dd", b.format());
assertEquals(LocalDate.parse("2017-01-31"), b.decodeDefaultValue());
assertEquals("2017-01-31", b.defaultValue());
ColumnMetadata c = schema.metadata("c");
Map<String, String> properties = new LinkedHashMap<>();
properties.put("k1", "v1");
properties.put("k2", "v2");
assertEquals(properties, c.properties());
assertEquals(0, schema.properties().size());
} finally {
FileUtils.deleteQuietly(schemaFile);
}
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaCommands method testSuccessfulCreateOrReplaceForTable.
@Test
public void testSuccessfulCreateOrReplaceForTable() throws Exception {
String tableName = "table_for_successful_create_or_replace_for_table";
String table = String.format("dfs.tmp.%s", tableName);
try {
run("create table %s as select 'a' as c from (values(1))", table);
File schemaPath = Paths.get(dirTestWatcher.getDfsTestTmpDir().getPath(), tableName, SchemaProvider.DEFAULT_SCHEMA_NAME).toFile();
assertFalse(schemaPath.exists());
testBuilder().sqlQuery("create schema (c varchar not null) for table %s", table).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", table)).go();
SchemaProvider schemaProvider = new PathSchemaProvider(new Path(schemaPath.getPath()));
assertTrue(schemaProvider.exists());
SchemaContainer schemaContainer = schemaProvider.read();
assertNotNull(schemaContainer.getTable());
assertEquals(String.format("dfs.tmp.`%s`", tableName), schemaContainer.getTable());
assertNotNull(schemaContainer.getSchema());
ColumnMetadata column = schemaContainer.getSchema().metadata("c");
assertFalse(column.isNullable());
assertEquals(TypeProtos.MinorType.VARCHAR, column.type());
testBuilder().sqlQuery("create or replace schema (c varchar) for table %s", table).unOrdered().baselineColumns("ok", "summary").baselineValues(true, String.format("Created schema for [%s]", table)).go();
assertTrue(schemaProvider.exists());
SchemaContainer updatedSchemaContainer = schemaProvider.read();
assertNotNull(updatedSchemaContainer.getTable());
assertEquals(String.format("dfs.tmp.`%s`", tableName), updatedSchemaContainer.getTable());
assertNotNull(updatedSchemaContainer.getSchema());
ColumnMetadata updatedColumn = updatedSchemaContainer.getSchema().metadata("c");
assertTrue(updatedColumn.isNullable());
assertEquals(TypeProtos.MinorType.VARCHAR, updatedColumn.type());
} finally {
run("drop table if exists %s", table);
}
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaTrackerInputSchema method doTestGenericMap.
private void doTestGenericMap(ProjectionSchemaTracker tracker, boolean mapAll) {
assertTrue(tracker.isResolved());
TupleMetadata schema = tracker.internalSchema().toSchema();
assertEquals(MAP_SCHEMA, schema);
ColumnMetadata mapCol = schema.metadata("m");
assertEquals(mapAll, SchemaUtils.isProjectAll(mapCol.tupleSchema()));
assertEquals(MOCK_VALUE, schema.metadata("m").property(MOCK_PROP));
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestSchemaTrackerInputSchema method testStrictWithWildcardWithMap.
/**
* Strict schema with a map and a wildcard: neither the top level
* nor the map allow the reader to add members. Not applicable for
* a reader schema.
*/
@Test
public void testStrictWithWildcardWithMap() {
ProjectionSchemaTracker tracker = trackerFor(RowSetTestUtils.projectAll());
// Mark schema as strict
TupleMetadata provided = MAP_SCHEMA.copy();
SchemaUtils.markStrict(provided);
tracker.applyProvidedSchema(provided);
assertTrue(tracker.isResolved());
// Projection as without strict, except the map is "closed",
// won't accept new members from the reader.
TupleMetadata schema = tracker.internalSchema().toSchema();
assertEquals(MAP_SCHEMA, schema);
ColumnMetadata mapCol = schema.metadata("m");
assertFalse(SchemaUtils.isProjectAll(mapCol.tupleSchema()));
}
use of org.apache.drill.exec.record.metadata.ColumnMetadata in project drill by apache.
the class TestDynamicSchemaFilter method testMapProjectList.
@Test
public void testMapProjectList() {
ProjectionParseResult parseResult = ScanProjectionParser.parse(RowSetTestUtils.projectList("m.x"));
ProjectionFilter filter = new DynamicTupleFilter(parseResult.dynamicSchema, EmptyErrorContext.INSTANCE);
assertFalse(filter.isEmpty());
assertTrue(filter.isProjected(MAP_COL.name()));
ProjResult result = filter.projection(MAP_COL);
assertTrue(result.isProjected);
assertNotNull(result.projection);
assertTrue(result.projection.isDynamic());
assertEquals(MAP_COL.name(), result.projection.name());
assertTrue(result.mapFilter instanceof DynamicTupleFilter);
ProjectionFilter mapFilter = result.mapFilter;
ColumnMetadata x_col = MAP_COL.tupleSchema().metadata("x");
assertTrue(mapFilter.isProjected("x"));
result = mapFilter.projection(x_col);
assertTrue(result.isProjected);
assertNotNull(result.projection);
assertTrue(result.projection.isDynamic());
assertEquals(x_col.name(), result.projection.name());
ColumnMetadata y_col = MAP_COL.tupleSchema().metadata("y");
assertFalse(mapFilter.isProjected("y"));
result = mapFilter.projection(y_col);
assertFalse(result.isProjected);
}
Aggregations