use of org.apache.calcite.rel.type.RelDataTypeFieldImpl in project drill by axbaretto.
the class DrillProjectRel method convert.
public static DrillProjectRel convert(Project project, ConversionContext context) throws InvalidRelException {
RelNode input = context.toRel(project.getInput());
List<RelDataTypeField> fields = Lists.newArrayList();
List<RexNode> exps = Lists.newArrayList();
for (NamedExpression expr : project.getSelections()) {
fields.add(new RelDataTypeFieldImpl(expr.getRef().getRootSegment().getPath(), fields.size(), context.getTypeFactory().createSqlType(SqlTypeName.ANY)));
exps.add(context.toRex(expr.getExpr()));
}
return new DrillProjectRel(context.getCluster(), context.getLogicalTraits(), input, exps, new RelRecordType(fields));
}
use of org.apache.calcite.rel.type.RelDataTypeFieldImpl 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));
}
use of org.apache.calcite.rel.type.RelDataTypeFieldImpl in project drill by axbaretto.
the class DrillFixedRelDataTypeImpl method addField.
private void addField(String columnName) {
RelDataTypeField newField = new RelDataTypeFieldImpl(columnName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true));
fields.add(newField);
}
use of org.apache.calcite.rel.type.RelDataTypeFieldImpl in project hazelcast by hazelcast.
the class HazelcastTable method computeRowType.
private RelDataType computeRowType(List<RexNode> projects) {
List<RelDataTypeField> typeFields = new ArrayList<>(projects.size());
for (int i = 0; i < projects.size(); i++) {
RexNode project = projects.get(i);
RelDataTypeField fieldType;
if (project instanceof RexInputRef) {
TableField field = target.getField(((RexInputRef) project).getIndex());
fieldType = new RelDataTypeFieldImpl(field.getName(), i, project.getType());
if (field.isHidden()) {
hiddenFieldNames.add(field.getName());
}
} else {
fieldType = new RelDataTypeFieldImpl("EXPR$" + i, i, project.getType());
}
typeFields.add(fieldType);
}
return new RelRecordType(StructKind.PEEK_FIELDS, typeFields, false);
}
use of org.apache.calcite.rel.type.RelDataTypeFieldImpl in project calcite by apache.
the class JavaTypeFactoryImpl method createStructType.
public RelDataType createStructType(Class type) {
final List<RelDataTypeField> list = new ArrayList<>();
for (Field field : type.getFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
// FIXME: watch out for recursion
final Type fieldType = fieldType(field);
list.add(new RelDataTypeFieldImpl(field.getName(), list.size(), createType(fieldType)));
}
}
return canonize(new JavaRecordType(list, type));
}
Aggregations