// Convert the query in CTAS statement into a RelNode
SqlNode validatedQuery = validateNode(sqlCreateTable.getQuery());
RelNode relQuery = convertToRel(validatedQuery);
List<String> tblFiledNames = sqlCreateTable.getFieldNames();
RelDataType queryRowType = relQuery.getRowType();
if (tblFiledNames.size() > 0) {
// Field count should match.
if (tblFiledNames.size() != queryRowType.getFieldCount())
return DirectPlan.createDirectPlan(context, false,
"Table's field list and the table's query field list have different counts.");
// CTAS's query field list shouldn't have "*" when table's field list is specified.
for(String field : queryRowType.getFieldNames()) {
if (field.equals("*"))
return DirectPlan.createDirectPlan(context, false,
"Table's query field list has a '*', which is invalid when table's field list is specified.");
}
}
// if the CTAS statement has table fields lists (ex. below), add a project rel to rename the query fields.
// Ex. CREATE TABLE tblname(col1, medianOfCol2, avgOfCol3) AS
// SELECT col1, median(col2), avg(col3) FROM sourcetbl GROUP BY col1 ;
if (tblFiledNames.size() > 0) {
// create rowtype to which the select rel needs to be casted.
RelDataType rowType = new DrillFixedRelDataTypeImpl(planner.getTypeFactory(), tblFiledNames);
relQuery = RelOptUtil.createCastRel(relQuery, rowType, true);
}
SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(),