use of org.apache.drill.exec.planner.types.RelDataTypeHolder in project drill by axbaretto.
the class View method getRowType.
public RelDataType getRowType(RelDataTypeFactory factory) {
// if there are no fields defined, this is a dynamic view.
if (isDynamic()) {
return new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory);
}
List<RelDataType> types = Lists.newArrayList();
List<String> names = Lists.newArrayList();
for (FieldType field : fields) {
names.add(field.getName());
RelDataType type;
if (SqlTypeFamily.INTERVAL_YEAR_MONTH == field.getType().getFamily() || SqlTypeFamily.INTERVAL_DAY_TIME == field.getType().getFamily()) {
type = factory.createSqlIntervalType(field.getIntervalQualifier());
} else if (field.getPrecision() == null && field.getScale() == null) {
type = factory.createSqlType(field.getType());
} else if (field.getPrecision() != null && field.getScale() == null) {
type = factory.createSqlType(field.getType(), field.getPrecision());
} else {
type = factory.createSqlType(field.getType(), field.getPrecision(), field.getScale());
}
if (field.getIsNullable()) {
types.add(factory.createTypeWithNullability(type, true));
} else {
types.add(type);
}
}
return factory.createStructType(types, names);
}
use of org.apache.drill.exec.planner.types.RelDataTypeHolder in project drill by axbaretto.
the class RewriteProjectToFlatten method visitProject.
@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
ProjectPrel project = node;
List<RexNode> exprList = new ArrayList<>();
boolean rewrite = false;
List<RelDataTypeField> relDataTypes = new ArrayList<>();
int i = 0;
RexNode flatttenExpr = null;
for (RexNode rex : project.getChildExps()) {
RexNode newExpr = rex;
if (rex instanceof RexCall) {
RexCall function = (RexCall) rex;
String functionName = function.getOperator().getName();
if (functionName.equalsIgnoreCase("flatten")) {
rewrite = true;
if (function.getOperands().size() != 1) {
throw new RelConversionException("Flatten expression expects a single input.");
}
newExpr = function.getOperands().get(0);
RexBuilder builder = new RexBuilder(factory);
flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
}
}
relDataTypes.add(project.getRowType().getFieldList().get(i));
i++;
exprList.add(newExpr);
}
if (rewrite == true) {
// TODO - figure out what is the right setting for the traits
Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
return flatten;
}
Prel child = ((Prel) project.getInput()).accept(this, null);
return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
use of org.apache.drill.exec.planner.types.RelDataTypeHolder in project drill by apache.
the class View method getRowType.
/**
* If view fields are present then attempts to gather them
* into struct type, otherwise returns extension of {@link DynamicRecordType}.
*
* @param factory factory for rel data types creation
* @return struct type that describes names and types of all
* view fields or extension of {@link DynamicRecordType}
* when view fields are empty
*/
public RelDataType getRowType(RelDataTypeFactory factory) {
// if there are no fields defined, this is a dynamic view.
if (isDynamic()) {
return new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory);
}
List<RelDataType> types = new ArrayList<>(fields.size());
List<String> names = new ArrayList<>(fields.size());
for (Field field : fields) {
names.add(field.getName());
types.add(getType(field, factory));
}
return factory.createStructType(types, names);
}
use of org.apache.drill.exec.planner.types.RelDataTypeHolder in project drill by axbaretto.
the class RexVisitorComplexExprSplitter method visitCall.
@Override
public RexNode visitCall(RexCall call) {
String functionName = call.getOperator().getName();
List<RexNode> newOps = new ArrayList<>();
for (RexNode operand : call.operands) {
newOps.add(operand.accept(this));
}
if (funcReg.isFunctionComplexOutput(functionName)) {
RexBuilder builder = new RexBuilder(factory);
RexNode ret = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), lastUsedIndex);
lastUsedIndex++;
complexExprs.add(call.clone(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), newOps));
return ret;
}
return call.clone(call.getType(), newOps);
}
use of org.apache.drill.exec.planner.types.RelDataTypeHolder in project drill by axbaretto.
the class SplitUpComplexExpressions method visitProject.
@Override
public Prel visitProject(ProjectPrel project, Object unused) throws RelConversionException {
// Apply the rule to the child
RelNode originalInput = ((Prel) project.getInput(0)).accept(this, null);
project = (ProjectPrel) project.copy(project.getTraitSet(), Lists.newArrayList(originalInput));
List<RexNode> exprList = new ArrayList<>();
List<RelDataTypeField> relDataTypes = new ArrayList<>();
List<RelDataTypeField> origRelDataTypes = new ArrayList<>();
int i = 0;
final int lastColumnReferenced = PrelUtil.getLastUsedColumnReference(project.getProjects());
if (lastColumnReferenced == -1) {
return project;
}
final int lastRexInput = lastColumnReferenced + 1;
RexVisitorComplexExprSplitter exprSplitter = new RexVisitorComplexExprSplitter(factory, funcReg, lastRexInput);
for (RexNode rex : project.getChildExps()) {
origRelDataTypes.add(project.getRowType().getFieldList().get(i));
i++;
exprList.add(rex.accept(exprSplitter));
}
List<RexNode> complexExprs = exprSplitter.getComplexExprs();
if (complexExprs.size() == 1 && findTopComplexFunc(project.getChildExps()).size() == 1) {
return project;
}
ProjectPrel childProject;
List<RexNode> allExprs = new ArrayList<>();
int exprIndex = 0;
List<String> fieldNames = originalInput.getRowType().getFieldNames();
for (int index = 0; index < lastRexInput; index++) {
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
if (fieldNames.get(index).contains(SchemaPath.DYNAMIC_STAR)) {
relDataTypes.add(new RelDataTypeFieldImpl(fieldNames.get(index), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
} else {
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
exprIndex++;
}
}
RexNode currRexNode;
int index = lastRexInput - 1;
// if the projection expressions contained complex outputs, split them into their own individual projects
if (complexExprs.size() > 0) {
while (complexExprs.size() > 0) {
if (index >= lastRexInput) {
allExprs.remove(allExprs.size() - 1);
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
}
index++;
exprIndex++;
currRexNode = complexExprs.remove(0);
allExprs.add(currRexNode);
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + exprIndex, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
childProject = new ProjectPrel(project.getCluster(), project.getTraitSet(), originalInput, ImmutableList.copyOf(allExprs), new RelRecordType(relDataTypes));
originalInput = childProject;
}
// copied from above, find a better way to do this
allExprs.remove(allExprs.size() - 1);
RexBuilder builder = new RexBuilder(factory);
allExprs.add(builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
relDataTypes.add(new RelDataTypeFieldImpl("EXPR$" + index, allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
}
return (Prel) project.copy(project.getTraitSet(), originalInput, exprList, new RelRecordType(origRelDataTypes));
}
Aggregations