public RelNode flattenTypes( RelNode rootRel, boolean restructure) { RelStructuredTypeFlattener typeFlattener = new RelStructuredTypeFlattener(rexBuilder); RelNode newRootRel = typeFlattener.rewrite(rootRel, restructure); // There are three maps constructed during convertQuery which need to to // be maintained for use in decorrelation. 1. mapRefRelToCorVar: - map a // rel node to the coorrelated variables it references 2. // mapCorVarToCorRel: - map a correlated variable to the correlatorRel // providing it 3. mapFieldAccessToCorVar: - map a rex field access to // the cor var it represents. because typeFlattener does not clone or // modify a correlated field access this map does not need to be // updated. typeFlattener.updateRelInMap(mapRefRelToCorVar); typeFlattener.updateRelInMap(mapCorVarToCorRel); return newRootRel; }
public void rewriteRel(FilterRel rel) { RelNode newRel = CalcRel.createFilter( getNewForOldRel(rel.getChild()), flattenFieldAccesses(rel.getCondition())); setNewForOldRel(rel, newRel); }
public Integer apply(Integer oldInput) { return getNewForOldInput(oldInput); } },
public RelNode flattenTypes( RelNode rootRel, boolean restructure) { RelStructuredTypeFlattener typeFlattener = new RelStructuredTypeFlattener(rexBuilder, createToRelContext()); return typeFlattener.rewrite(rootRel, restructure); }
public void rewriteRel(CalcRel rel) { final RelNode newChild = getNewForOldRel(rel.getChild()); for (RexNode expr : program.getExprList()) { programBuilder.registerInput( flattenFieldAccesses(expr)); final List<String> flattenedFieldNameList = new ArrayList<String>(); List<String> fieldNames = rel.getRowType().getFieldNames(); flattenProjections( program.getProjectList(), fieldNames, programBuilder.addCondition( new RexLocalRef( getNewForOldInput(conditionRef.getIndex()), conditionRef.getType())); newProgram, Collections.<RelCollation>emptyList()); setNewForOldRel(rel, newRel);
if (exp instanceof RexInputRef) { RexInputRef inputRef = (RexInputRef) exp; int newOffset = getNewForOldInput(inputRef.getIndex()); flattenedFieldNames.add(fieldName); } else if (isConstructor(exp) || exp.isA(SqlKind.CAST)) { flattenNullLiteral( exp.getType(), flattenedExps, flattenProjections( call.getOperands(), Collections.<String>nCopies( exp = flattenFieldAccesses(exp); flattenedExps.add(exp); flattenedFieldNames.add(fieldName);
public void rewriteRel(ProjectRel rel) { final List<RexNode> flattenedExpList = new ArrayList<RexNode>(); final List<String> flattenedFieldNameList = new ArrayList<String>(); List<String> fieldNames = rel.getRowType().getFieldNames(); flattenProjections( rel.getProjects(), fieldNames, "", flattenedExpList, flattenedFieldNameList); RelNode newRel = CalcRel.createProject( getNewForOldRel(rel.getChild()), flattenedExpList, flattenedFieldNameList); setNewForOldRel(rel, newRel); }
public void rewriteRel(SortRel rel) { RelCollation oldCollation = rel.getCollation(); final RelNode oldChild = rel.getChild(); final RelNode newChild = getNewForOldRel(oldChild); final Mappings.TargetMapping mapping = getNewForOldInputMapping(oldChild); // validate for (RelFieldCollation field : oldCollation.getFieldCollations()) { int oldInput = field.getFieldIndex(); RelDataType sortFieldType = oldChild.getRowType().getFieldList().get(oldInput).getType(); if (sortFieldType.isStruct()) { // TODO jvs 10-Feb-2005 throw Util.needToImplement("sorting on structured types"); } } SortRel newRel = new SortRel( rel.getCluster(), rel.getCluster().traitSetOf(Convention.NONE), newChild, RexUtil.apply(mapping, oldCollation)); setNewForOldRel(rel, newRel); }
public void rewriteRel(CorrelatorRel rel) { Iterator oldCorrelations = rel.getCorrelations().iterator(); ArrayList<CorrelatorRel.Correlation> newCorrelations = new ArrayList<CorrelatorRel.Correlation>(); while (oldCorrelations.hasNext()) { CorrelatorRel.Correlation c = (CorrelatorRel.Correlation) oldCorrelations.next(); RelDataType corrFieldType = rel.getLeft().getRowType().getFieldList().get(c.getOffset()) .getType(); if (corrFieldType.isStruct()) { throw Util.needToImplement("correlation on structured type"); } newCorrelations.add( new CorrelatorRel.Correlation( c.getId(), getNewForOldInput(c.getOffset()))); } CorrelatorRel newRel = new CorrelatorRel( rel.getCluster(), getNewForOldRel(rel.getLeft()), getNewForOldRel(rel.getRight()), rel.getCondition(), newCorrelations, rel.getJoinType()); setNewForOldRel(rel, newRel); }
public void rewriteGeneric(RelNode rel) { RelNode newRel = rel.copy(rel.getTraitSet(), rel.getInputs()); List<RelNode> oldInputs = rel.getInputs(); for (int i = 0; i < oldInputs.size(); ++i) { newRel.replaceInput( i, getNewForOldRel(oldInputs.get(i))); } setNewForOldRel(rel, newRel); }
/** * Maps the ordinal of a field pre-flattening to the ordinal of the * corresponding field post-flattening, and optionally returns its type. * * @param oldOrdinal Pre-flattening ordinal * @return Post-flattening ordinal */ protected int getNewForOldInput(int oldOrdinal) { assert currentRel != null; int newOrdinal = 0; // determine which input rel oldOrdinal references, and adjust // oldOrdinal to be relative to that input rel RelNode oldInput = null; for (RelNode oldInput1 : currentRel.getInputs()) { RelDataType oldInputType = oldInput1.getRowType(); int n = oldInputType.getFieldCount(); if (oldOrdinal < n) { oldInput = oldInput1; break; } RelNode newInput = getNewForOldRel(oldInput1); newOrdinal += newInput.getRowType().getFieldCount(); oldOrdinal -= n; } assert oldInput != null; RelDataType oldInputType = oldInput.getRowType(); newOrdinal += calculateFlattenedOffset(oldInputType, oldOrdinal); return newOrdinal; }
public RelNode rewrite(RelNode root, boolean restructure) { // Perform flattening. oldToNewRelMap = new HashMap<RelNode, RelNode>(); visitor.visit(root, 0, null); RelNode flattened = getNewForOldRel(root); flattenedRootType = flattened.getRowType(); // If requested, add an additional projection which puts // everything back into structured form for return to the // client. restructured = false; List<RexNode> structuringExps = null; if (restructure) { iRestructureInput = 0; structuringExps = restructureFields(root.getRowType()); } if (restructured) { // REVIEW jvs 23-Mar-2005: How do we make sure that this // implementation stays in Java? Fennel can't handle // structured types. return CalcRel.createProject( flattened, structuringExps, root.getRowType().getFieldNames()); } else { return flattened; } }
/** * Returns a mapping between old and new fields. * * @param oldRel Old relational expression * @return Mapping between fields of old and new */ private Mappings.TargetMapping getNewForOldInputMapping(RelNode oldRel) { final RelNode newRel = getNewForOldRel(oldRel); return Mappings.target( new Function1<Integer, Integer>() { public Integer apply(Integer oldInput) { return getNewForOldInput(oldInput); } }, oldRel.getRowType().getFieldCount(), newRel.getRowType().getFieldCount()); }
private void flattenProjections( List<? extends RexNode> exps, List<String> fieldNames, String prefix, List<RexNode> flattenedExps, List<String> flattenedFieldNames) { for (int i = 0; i < exps.size(); ++i) { RexNode exp = exps.get(i); String fieldName = (fieldNames == null || fieldNames.get(i) == null) ? ("$" + i) : fieldNames.get(i); if (!prefix.equals("")) { fieldName = prefix + "$" + fieldName; } flattenProjection( exp, fieldName, flattenedExps, flattenedFieldNames); } }
public void rewriteRel(CalcRel rel) final RelNode newChild = getNewForOldRel(rel.getChild()); for (RexNode expr : program.getExprList()) { programBuilder.registerInput( flattenFieldAccesses(expr)); final List<String> flattenedFieldNameList = new ArrayList<String>(); List<String> fieldNames = rel.getRowType().getFieldNames(); flattenProjections( program.getProjectList(), fieldNames, programBuilder.addCondition( new RexLocalRef( getNewForOldInput(conditionRef.getIndex()), conditionRef.getType())); newProgram, Collections.<RelCollation>emptyList()); setNewForOldRel(rel, newRel);
if (exp instanceof RexInputRef) { RexInputRef inputRef = (RexInputRef) exp; int newOffset = getNewForOldInput(inputRef.getIndex()); flattenedFieldNames.add(fieldName); } else if (isConstructor(exp) || exp.isA(RexKind.Cast)) { flattenNullLiteral( exp.getType(), flattenedExps, flattenProjections( call.getOperands(), Collections.<String>nCopies( exp = flattenFieldAccesses(exp); flattenedExps.add(exp); flattenedFieldNames.add(fieldName);
public RelNode flattenTypes( RelNode rootRel, boolean restructure) { RelStructuredTypeFlattener typeFlattener = new RelStructuredTypeFlattener(rexBuilder, createToRelContext()); return typeFlattener.rewrite(rootRel, restructure); }
public void rewriteRel(ProjectRel rel) { final List<RexNode> flattenedExpList = new ArrayList<RexNode>(); final List<String> flattenedFieldNameList = new ArrayList<String>(); List<String> fieldNames = rel.getRowType().getFieldNames(); flattenProjections( rel.getProjects(), fieldNames, "", flattenedExpList, flattenedFieldNameList); RelNode newRel = CalcRel.createProject( getNewForOldRel(rel.getChild()), flattenedExpList, flattenedFieldNameList); setNewForOldRel(rel, newRel); }
public void rewriteRel(SortRel rel) { RelCollation oldCollation = rel.getCollation(); final RelNode oldChild = rel.getChild(); final RelNode newChild = getNewForOldRel(oldChild); final Mappings.TargetMapping mapping = getNewForOldInputMapping(oldChild); // validate for (RelFieldCollation field : oldCollation.getFieldCollations()) { int oldInput = field.getFieldIndex(); RelDataType sortFieldType = oldChild.getRowType().getFieldList().get(oldInput).getType(); if (sortFieldType.isStruct()) { // TODO jvs 10-Feb-2005 throw Util.needToImplement("sorting on structured types"); } } RelCollation newCollation = RexUtil.apply(mapping, oldCollation); SortRel newRel = new SortRel( rel.getCluster(), rel.getCluster().traitSetOf(Convention.NONE).plus(newCollation), newChild, newCollation, rel.offset, rel.fetch); setNewForOldRel(rel, newRel); }
public void rewriteRel(CorrelatorRel rel) { final List<Correlation> newCorrelations = new ArrayList<Correlation>(); for (Correlation c : rel.getCorrelations()) { RelDataType corrFieldType = rel.getLeft().getRowType().getFieldList().get(c.getOffset()) .getType(); if (corrFieldType.isStruct()) { throw Util.needToImplement("correlation on structured type"); } newCorrelations.add( new Correlation( c.getId(), getNewForOldInput(c.getOffset()))); } CorrelatorRel newRel = new CorrelatorRel( rel.getCluster(), getNewForOldRel(rel.getLeft()), getNewForOldRel(rel.getRight()), rel.getCondition(), newCorrelations, rel.getJoinType()); setNewForOldRel(rel, newRel); }