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;
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations