use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.
the class FunctionalIndexHelper method convertRowTypeForIndexScan.
/**
* For IndexScan in non-covering case, rowType to return contains only row_key('_id') of primary table.
* so the rowType for IndexScan should be converted from [Primary_table.row_key, primary_table.indexed_col]
* to [indexTable.row_key(primary_table.indexed_col), indexTable.<primary_key.row_key> (Primary_table.row_key)]
* This will impact the columns of scan, the rowType of ScanRel
*
* @param origScan
* @param idxMarker the IndexableExprMarker that has analyzed original index condition on top of index scan
* @param idxScan
* @return
*/
public static RelDataType convertRowTypeForIndexScan(DrillScanRelBase origScan, IndexableExprMarker idxMarker, IndexGroupScan idxScan, FunctionalIndexInfo functionInfo) {
RelDataTypeFactory typeFactory = origScan.getCluster().getTypeFactory();
List<RelDataTypeField> fields = new ArrayList<>();
Set<SchemaPath> rowPaths = new LinkedHashSet<>();
// row_key in the rowType of scan on primary table
RelDataTypeField rowkey_primary;
RelRecordType newRowType = null;
DbGroupScan scan = (DbGroupScan) IndexPlanUtils.getGroupScan(origScan);
// first add row_key of primary table,
rowkey_primary = new RelDataTypeFieldImpl(scan.getRowKeyName(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY));
fields.add(rowkey_primary);
Map<RexNode, LogicalExpression> idxExprMap = idxMarker.getIndexableExpression();
for (LogicalExpression indexedExpr : idxExprMap.values()) {
if (indexedExpr instanceof SchemaPath) {
rowPaths.add((SchemaPath) indexedExpr);
} else if (indexedExpr instanceof CastExpression) {
SchemaPath newPath = functionInfo.getNewPathFromExpr(indexedExpr);
if (newPath != null) {
rowPaths.add(newPath);
}
}
}
for (SchemaPath newPath : rowPaths) {
fields.add(new RelDataTypeFieldImpl(newPath.getRootSegmentPath(), fields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
}
// update columns of groupscan accordingly
Set<RelDataTypeField> rowfields = Sets.newLinkedHashSet();
final List<SchemaPath> columns = Lists.newArrayList();
for (RelDataTypeField f : fields) {
SchemaPath path = SchemaPath.parseFromString(f.getName());
rowfields.add(new RelDataTypeFieldImpl(path.getRootSegmentPath(), rowfields.size(), typeFactory.createSqlType(SqlTypeName.ANY)));
columns.add(path);
}
idxScan.setColumns(columns);
// rowtype does not take the whole path, but only the rootSegment of the SchemaPath
newRowType = new RelRecordType(Lists.newArrayList(rowfields));
return newRowType;
}
use of org.apache.calcite.rel.type.RelRecordType in project drill by apache.
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.RelRecordType in project drill by apache.
the class SplitUpComplexExpressions method visitProject.
@Override
public Prel visitProject(final ProjectPrel project, Object unused) throws RelConversionException {
final Prel oldInput = (Prel) project.getInput(0);
RelNode newInput = oldInput.accept(this, unused);
ProjectPrel newProject = (ProjectPrel) project.copy(project.getTraitSet(), Lists.newArrayList(newInput));
final int lastColumnReferenced = PrelUtil.getLastUsedColumnReference(newProject.getProjects());
if (lastColumnReferenced == -1) {
return newProject;
}
List<RelDataTypeField> projectFields = newProject.getRowType().getFieldList();
List<RelDataTypeField> origRelDataTypes = new ArrayList<>();
List<RexNode> exprList = new ArrayList<>();
final int lastRexInput = lastColumnReferenced + 1;
RexVisitorComplexExprSplitter exprSplitter = new RexVisitorComplexExprSplitter(funcReg, rexBuilder, lastRexInput);
int i = 0;
for (RexNode rex : newProject.getChildExps()) {
RelDataTypeField originField = projectFields.get(i++);
RexNode splitRex = rex.accept(exprSplitter);
origRelDataTypes.add(originField);
exprList.add(splitRex);
}
final List<RexNode> complexExprs = exprSplitter.getComplexExprs();
if (complexExprs.size() == 1 && findTopComplexFunc(newProject.getChildExps()).size() == 1) {
return newProject;
}
// if the projection expressions contained complex outputs, split them into their own individual projects
if (complexExprs.size() > 0) {
List<RexNode> allExprs = new ArrayList<>();
int exprIndex = 0;
List<String> fieldNames = newInput.getRowType().getFieldNames();
List<RelDataTypeField> relDataTypes = new ArrayList<>();
for (int index = 0; index < lastRexInput; index++) {
allExprs.add(rexBuilder.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(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
exprIndex++;
}
}
RexNode currRexNode;
int index = lastRexInput - 1;
while (complexExprs.size() > 0) {
if (index >= lastRexInput) {
RexInputRef newLastRex = rexBuilder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index);
// replace last rex with new one
allExprs.set(allExprs.size() - 1, newLastRex);
}
index++;
exprIndex++;
currRexNode = complexExprs.remove(0);
allExprs.add(currRexNode);
relDataTypes.add(new RelDataTypeFieldImpl(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
RelRecordType childProjectType = new RelRecordType(relDataTypes);
ProjectPrel childProject = new ProjectPrel(newProject.getCluster(), newProject.getTraitSet(), newInput, ImmutableList.copyOf(allExprs), childProjectType);
newInput = childProject;
}
allExprs.set(allExprs.size() - 1, rexBuilder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), index));
relDataTypes.add(new RelDataTypeFieldImpl(getExprName(exprIndex), allExprs.size(), factory.createSqlType(SqlTypeName.ANY)));
}
return (Prel) project.copy(project.getTraitSet(), newInput, exprList, new RelRecordType(origRelDataTypes));
}
Aggregations