Search in sources :

Example 16 with JoinInfo

use of org.apache.calcite.rel.core.JoinInfo in project calcite by apache.

the class RelMdUniqueKeys method getUniqueKeys.

public Set<ImmutableBitSet> getUniqueKeys(Join rel, RelMetadataQuery mq, boolean ignoreNulls) {
    final RelNode left = rel.getLeft();
    final RelNode right = rel.getRight();
    // first add the different combinations of concatenated unique keys
    // from the left and the right, adjusting the right hand side keys to
    // reflect the addition of the left hand side
    // 
    // NOTE zfong 12/18/06 - If the number of tables in a join is large,
    // the number of combinations of unique key sets will explode.  If
    // that is undesirable, use RelMetadataQuery.areColumnsUnique() as
    // an alternative way of getting unique key information.
    final Set<ImmutableBitSet> retSet = new HashSet<>();
    final Set<ImmutableBitSet> leftSet = mq.getUniqueKeys(left, ignoreNulls);
    Set<ImmutableBitSet> rightSet = null;
    final Set<ImmutableBitSet> tmpRightSet = mq.getUniqueKeys(right, ignoreNulls);
    int nFieldsOnLeft = left.getRowType().getFieldCount();
    if (tmpRightSet != null) {
        rightSet = new HashSet<>();
        for (ImmutableBitSet colMask : tmpRightSet) {
            ImmutableBitSet.Builder tmpMask = ImmutableBitSet.builder();
            for (int bit : colMask) {
                tmpMask.set(bit + nFieldsOnLeft);
            }
            rightSet.add(tmpMask.build());
        }
        if (leftSet != null) {
            for (ImmutableBitSet colMaskRight : rightSet) {
                for (ImmutableBitSet colMaskLeft : leftSet) {
                    retSet.add(colMaskLeft.union(colMaskRight));
                }
            }
        }
    }
    // locate the columns that participate in equijoins
    final JoinInfo joinInfo = rel.analyzeCondition();
    // determine if either or both the LHS and RHS are unique on the
    // equijoin columns
    final Boolean leftUnique = mq.areColumnsUnique(left, joinInfo.leftSet(), ignoreNulls);
    final Boolean rightUnique = mq.areColumnsUnique(right, joinInfo.rightSet(), ignoreNulls);
    // generating
    if ((rightUnique != null) && rightUnique && (leftSet != null) && !(rel.getJoinType().generatesNullsOnLeft())) {
        retSet.addAll(leftSet);
    }
    // same as above except left and right are reversed
    if ((leftUnique != null) && leftUnique && (rightSet != null) && !(rel.getJoinType().generatesNullsOnRight())) {
        retSet.addAll(rightSet);
    }
    return retSet;
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashSet(java.util.HashSet)

Example 17 with JoinInfo

use of org.apache.calcite.rel.core.JoinInfo in project calcite by apache.

the class SortJoinTransposeRule method matches.

// ~ Methods ----------------------------------------------------------------
@Override
public boolean matches(RelOptRuleCall call) {
    final Sort sort = call.rel(0);
    final Join join = call.rel(1);
    final RelMetadataQuery mq = call.getMetadataQuery();
    final JoinInfo joinInfo = JoinInfo.of(join.getLeft(), join.getRight(), join.getCondition());
    // condition, we bail out
    if (join.getJoinType() == JoinRelType.LEFT) {
        if (sort.getCollation() != RelCollations.EMPTY) {
            for (RelFieldCollation relFieldCollation : sort.getCollation().getFieldCollations()) {
                if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) {
                    return false;
                }
            }
        }
        if (sort.offset != null && !RelMdUtil.areColumnsDefinitelyUnique(mq, join.getRight(), joinInfo.rightSet())) {
            return false;
        }
    } else if (join.getJoinType() == JoinRelType.RIGHT) {
        if (sort.getCollation() != RelCollations.EMPTY) {
            for (RelFieldCollation relFieldCollation : sort.getCollation().getFieldCollations()) {
                if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) {
                    return false;
                }
            }
        }
        if (sort.offset != null && !RelMdUtil.areColumnsDefinitelyUnique(mq, join.getLeft(), joinInfo.leftSet())) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) Join(org.apache.calcite.rel.core.Join) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin)

Example 18 with JoinInfo

use of org.apache.calcite.rel.core.JoinInfo in project drill by apache.

the class DrillSemiJoinRel method copy.

@Override
public DrillSemiJoinRel copy(RelTraitSet traitSet, RexNode condition, RelNode left, RelNode right, JoinRelType joinType, boolean semiJoinDone) {
    Preconditions.checkArgument(joinType == JoinRelType.SEMI);
    final JoinInfo joinInfo = JoinInfo.of(left, right, condition);
    Preconditions.checkArgument(joinInfo.isEqui());
    return new DrillSemiJoinRel(getCluster(), traitSet, left, right, condition, joinInfo.leftKeys, joinInfo.rightKeys);
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo)

Example 19 with JoinInfo

use of org.apache.calcite.rel.core.JoinInfo in project herddb by diennea.

the class CalcitePlanner method planEnumerableMergeJoin.

private PlannerOp planEnumerableMergeJoin(EnumerableMergeJoin op, RelDataType rowType) {
    // please note that EnumerableMergeJoin has a condition field which actually is not useful
    PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false, false);
    final JoinInfo analyzeCondition = op.analyzeCondition();
    int[] leftKeys = analyzeCondition.leftKeys.toIntArray();
    int[] rightKeys = analyzeCondition.rightKeys.toIntArray();
    boolean generateNullsOnLeft = op.getJoinType().generatesNullsOnLeft();
    boolean generateNullsOnRight = op.getJoinType().generatesNullsOnRight();
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<CompiledSQLExpression> nonEquiConditions = convertJoinNonEquiConditions(analyzeCondition);
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    return new JoinOp(fieldNames, columns, leftKeys, left, rightKeys, right, generateNullsOnLeft, generateNullsOnRight, true, nonEquiConditions);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) RelDataType(org.apache.calcite.rel.type.RelDataType) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Column(herddb.model.Column) JoinOp(herddb.model.planner.JoinOp) SemiJoinOp(herddb.model.planner.SemiJoinOp) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

Example 20 with JoinInfo

use of org.apache.calcite.rel.core.JoinInfo in project herddb by diennea.

the class CalcitePlanner method planEnumerableHashJoin.

private PlannerOp planEnumerableHashJoin(EnumerableHashJoin op, RelDataType rowType) {
    // please note that EnumerableSemiJoin has a condition field which actually is not useful
    PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false, false);
    final JoinInfo analyzeCondition = op.analyzeCondition();
    int[] leftKeys = analyzeCondition.leftKeys.toIntArray();
    int[] rightKeys = analyzeCondition.rightKeys.toIntArray();
    boolean generateNullsOnLeft = op.getJoinType().generatesNullsOnLeft();
    boolean generateNullsOnRight = op.getJoinType().generatesNullsOnRight();
    List<CompiledSQLExpression> nonEquiConditions = convertJoinNonEquiConditions(analyzeCondition);
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    if (op.isSemiJoin()) {
        return new SemiJoinOp(fieldNames, columns, leftKeys, left, rightKeys, right);
    } else {
        return new JoinOp(fieldNames, columns, leftKeys, left, rightKeys, right, generateNullsOnLeft, generateNullsOnRight, false, nonEquiConditions);
    }
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) SemiJoinOp(herddb.model.planner.SemiJoinOp) JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Column(herddb.model.Column) JoinOp(herddb.model.planner.JoinOp) SemiJoinOp(herddb.model.planner.SemiJoinOp) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

Aggregations

JoinInfo (org.apache.calcite.rel.core.JoinInfo)21 RelNode (org.apache.calcite.rel.RelNode)12 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)8 Join (org.apache.calcite.rel.core.Join)7 ArrayList (java.util.ArrayList)6 RexNode (org.apache.calcite.rex.RexNode)6 RelOptCluster (org.apache.calcite.plan.RelOptCluster)5 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 Project (org.apache.calcite.rel.core.Project)4 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 Column (herddb.model.Column)3 JoinOp (herddb.model.planner.JoinOp)3 NestedLoopJoinOp (herddb.model.planner.NestedLoopJoinOp)3 PlannerOp (herddb.model.planner.PlannerOp)3 SemiJoinOp (herddb.model.planner.SemiJoinOp)3 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)3 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)3 InvalidRelException (org.apache.calcite.rel.InvalidRelException)3