Search in sources :

Example 1 with JoinCondition

use of org.apache.drill.common.logical.data.JoinCondition in project drill by apache.

the class HashJoinPrel method getHashJoinPop.

private PhysicalOperator getHashJoinPop(PhysicalPlanCreator creator, RelNode left, RelNode right, List<Integer> leftKeys, List<Integer> rightKeys) throws IOException {
    final List<String> fields = getRowType().getFieldNames();
    assert isUnique(fields);
    final List<String> leftFields = left.getRowType().getFieldNames();
    final List<String> rightFields = right.getRowType().getFieldNames();
    PhysicalOperator leftPop = ((Prel) left).getPhysicalOperator(creator);
    PhysicalOperator rightPop = ((Prel) right).getPhysicalOperator(creator);
    JoinRelType jtype = this.getJoinType();
    List<JoinCondition> conditions = Lists.newArrayList();
    buildJoinConditions(conditions, leftFields, rightFields, leftKeys, rightKeys);
    HashJoinPOP hjoin = new HashJoinPOP(leftPop, rightPop, conditions, jtype);
    return creator.addMetadata(this, hjoin);
}
Also used : JoinRelType(org.apache.calcite.rel.core.JoinRelType) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) HashJoinPOP(org.apache.drill.exec.physical.config.HashJoinPOP) JoinCondition(org.apache.drill.common.logical.data.JoinCondition)

Example 2 with JoinCondition

use of org.apache.drill.common.logical.data.JoinCondition in project drill by axbaretto.

the class DrillJoinRel method convert.

public static DrillJoinRel convert(Join join, ConversionContext context) throws InvalidRelException {
    RelNode left = context.toRel(join.getLeft());
    RelNode right = context.toRel(join.getRight());
    List<RexNode> joinConditions = new ArrayList<RexNode>();
    // right fields appear after the LHS fields.
    final int rightInputOffset = left.getRowType().getFieldCount();
    for (JoinCondition condition : join.getConditions()) {
        RelDataTypeField leftField = left.getRowType().getField(ExprHelper.getFieldName(condition.getLeft()), true, false);
        RelDataTypeField rightField = right.getRowType().getField(ExprHelper.getFieldName(condition.getRight()), true, false);
        joinConditions.add(context.getRexBuilder().makeCall(SqlStdOperatorTable.EQUALS, context.getRexBuilder().makeInputRef(leftField.getType(), leftField.getIndex()), context.getRexBuilder().makeInputRef(rightField.getType(), rightInputOffset + rightField.getIndex())));
    }
    RexNode rexCondition = RexUtil.composeConjunction(context.getRexBuilder(), joinConditions, false);
    DrillJoinRel joinRel = new DrillJoinRel(context.getCluster(), context.getLogicalTraits(), left, right, rexCondition, join.getJoinType());
    return joinRel;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) JoinCondition(org.apache.drill.common.logical.data.JoinCondition)

Example 3 with JoinCondition

use of org.apache.drill.common.logical.data.JoinCondition in project drill by axbaretto.

the class MergeJoinPrel method getPhysicalOperator.

@Override
public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException {
    final List<String> fields = getRowType().getFieldNames();
    assert isUnique(fields);
    final int leftCount = left.getRowType().getFieldCount();
    final List<String> leftFields = fields.subList(0, leftCount);
    final List<String> rightFields = fields.subList(leftCount, fields.size());
    PhysicalOperator leftPop = ((Prel) left).getPhysicalOperator(creator);
    PhysicalOperator rightPop = ((Prel) right).getPhysicalOperator(creator);
    JoinRelType jtype = this.getJoinType();
    List<JoinCondition> conditions = Lists.newArrayList();
    buildJoinConditions(conditions, leftFields, rightFields, leftKeys, rightKeys);
    MergeJoinPOP mjoin = new MergeJoinPOP(leftPop, rightPop, conditions, jtype);
    return creator.addMetadata(this, mjoin);
}
Also used : JoinRelType(org.apache.calcite.rel.core.JoinRelType) MergeJoinPOP(org.apache.drill.exec.physical.config.MergeJoinPOP) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) JoinCondition(org.apache.drill.common.logical.data.JoinCondition)

Example 4 with JoinCondition

use of org.apache.drill.common.logical.data.JoinCondition in project drill by apache.

the class MergeJoinBatch method generateNewWorker.

private JoinWorker generateNewWorker() {
    final ClassGenerator<JoinWorker> cg = CodeGenerator.getRoot(JoinWorker.TEMPLATE_DEFINITION, context.getOptions());
    cg.getCodeGenerator().plainJavaCapable(true);
    // cg.getCodeGenerator().saveCodeForDebugging(true);
    final ErrorCollector collector = new ErrorCollectorImpl();
    // Generate members and initialization code
    // ///////////////////////////////////////
    // declare and assign JoinStatus member
    cg.setMappingSet(setupMapping);
    JClass joinStatusClass = cg.getModel().ref(JoinStatus.class);
    JVar joinStatus = cg.clazz.field(JMod.NONE, joinStatusClass, "status");
    cg.getSetupBlock().assign(JExpr._this().ref(joinStatus), JExpr.direct("status"));
    // declare and assign outgoing VectorContainer member
    JClass vectorContainerClass = cg.getModel().ref(VectorContainer.class);
    JVar outgoingVectorContainer = cg.clazz.field(JMod.NONE, vectorContainerClass, "outgoing");
    cg.getSetupBlock().assign(JExpr._this().ref(outgoingVectorContainer), JExpr.direct("outgoing"));
    // declare and assign incoming left RecordBatch member
    JClass recordBatchClass = cg.getModel().ref(RecordIterator.class);
    JVar incomingLeftRecordBatch = cg.clazz.field(JMod.NONE, recordBatchClass, "incomingLeft");
    cg.getSetupBlock().assign(JExpr._this().ref(incomingLeftRecordBatch), joinStatus.ref("left"));
    // declare and assign incoming right RecordBatch member
    JVar incomingRightRecordBatch = cg.clazz.field(JMod.NONE, recordBatchClass, "incomingRight");
    cg.getSetupBlock().assign(JExpr._this().ref(incomingRightRecordBatch), joinStatus.ref("right"));
    // declare 'incoming' member so VVReadExpr generated code can point to the left or right batch
    JVar incomingRecordBatch = cg.clazz.field(JMod.NONE, recordBatchClass, "incoming");
    /*
     * Materialize expressions on both sides of the join condition. Check if both the sides
     * have the same return type, if not then inject casts so that comparison function will work as
     * expected
     */
    LogicalExpression[] leftExpr = new LogicalExpression[conditions.size()];
    LogicalExpression[] rightExpr = new LogicalExpression[conditions.size()];
    IterOutcome lastLeftStatus = status.getLeftStatus();
    IterOutcome lastRightStatus = status.getRightStatus();
    for (int i = 0; i < conditions.size(); i++) {
        JoinCondition condition = conditions.get(i);
        leftExpr[i] = materializeExpression(condition.getLeft(), lastLeftStatus, leftIterator, collector);
        rightExpr[i] = materializeExpression(condition.getRight(), lastRightStatus, rightIterator, collector);
    }
    // call to throw an exception. In this case we can safely skip adding the casts
    if (lastRightStatus != IterOutcome.NONE) {
        JoinUtils.addLeastRestrictiveCasts(leftExpr, leftIterator, rightExpr, rightIterator, context);
    }
    // generate doCompare() method
    // ///////////////////////////////////////
    generateDoCompare(cg, incomingRecordBatch, leftExpr, incomingLeftRecordBatch, rightExpr, incomingRightRecordBatch, collector);
    // generate copyLeft()
    // ////////////////////
    cg.setMappingSet(copyLeftMapping);
    int vectorId = 0;
    if (worker == null || !status.left.finished()) {
        for (VectorWrapper<?> vw : leftIterator) {
            MajorType inputType = vw.getField().getType();
            MajorType outputType;
            if (joinType == JoinRelType.RIGHT && inputType.getMode() == DataMode.REQUIRED) {
                outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
            } else {
                outputType = inputType;
            }
            // TODO (DRILL-4011): Factor out CopyUtil and use it here.
            TypedFieldId inTypedFieldId = new TypedFieldId.Builder().finalType(inputType).addId(vectorId).build();
            JVar vvIn = cg.declareVectorValueSetupAndMember("incomingLeft", inTypedFieldId);
            TypedFieldId outTypedFieldId = new TypedFieldId.Builder().finalType(outputType).addId(vectorId).build();
            JVar vvOut = cg.declareVectorValueSetupAndMember("outgoing", outTypedFieldId);
            // todo: check result of copyFromSafe and grow allocation
            cg.getEvalBlock().add(vvOut.invoke("copyFromSafe").arg(copyLeftMapping.getValueReadIndex()).arg(copyLeftMapping.getValueWriteIndex()).arg(vvIn));
            cg.rotateBlock();
            ++vectorId;
        }
    }
    // generate copyRight()
    // /////////////////////
    cg.setMappingSet(copyRightMappping);
    int rightVectorBase = vectorId;
    if (status.getRightStatus() != IterOutcome.NONE && (worker == null || !status.right.finished())) {
        for (VectorWrapper<?> vw : rightIterator) {
            MajorType inputType = vw.getField().getType();
            MajorType outputType;
            if (joinType == JoinRelType.LEFT && inputType.getMode() == DataMode.REQUIRED) {
                outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
            } else {
                outputType = inputType;
            }
            // TODO (DRILL-4011): Factor out CopyUtil and use it here.
            TypedFieldId inTypedFieldId = new TypedFieldId.Builder().finalType(inputType).addId(vectorId - rightVectorBase).build();
            JVar vvIn = cg.declareVectorValueSetupAndMember("incomingRight", inTypedFieldId);
            TypedFieldId outTypedFieldId = new TypedFieldId.Builder().finalType(outputType).addId(vectorId).build();
            JVar vvOut = cg.declareVectorValueSetupAndMember("outgoing", outTypedFieldId);
            // todo: check result of copyFromSafe and grow allocation
            cg.getEvalBlock().add(vvOut.invoke("copyFromSafe").arg(copyRightMappping.getValueReadIndex()).arg(copyRightMappping.getValueWriteIndex()).arg(vvIn));
            cg.rotateBlock();
            ++vectorId;
        }
    }
    JoinWorker w = context.getImplementationClass(cg);
    try {
        w.setupJoin(context, status, this.container);
    } catch (SchemaChangeException e) {
        throw schemaChangeException(e, logger);
    }
    return w;
}
Also used : JClass(com.sun.codemodel.JClass) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JoinCondition(org.apache.drill.common.logical.data.JoinCondition) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) JVar(com.sun.codemodel.JVar)

Example 5 with JoinCondition

use of org.apache.drill.common.logical.data.JoinCondition in project drill by apache.

the class TestHashJoinOutcome method testHashJoinWhenProbeIsNONE.

/**
 * Testing for DRILL-6755: No Hash Table is built when the first probe batch is NONE
 */
@Test
public void testHashJoinWhenProbeIsNONE() {
    inputOutcomesLeft.add(RecordBatch.IterOutcome.NONE);
    inputOutcomesRight.add(RecordBatch.IterOutcome.OK_NEW_SCHEMA);
    inputOutcomesRight.add(RecordBatch.IterOutcome.OK);
    inputOutcomesRight.add(RecordBatch.IterOutcome.NONE);
    // for the probe side input - use multiple batches (to check that they are all cleared/drained)
    final List<VectorContainer> buildSideinputContainer = new ArrayList<>(5);
    buildSideinputContainer.add(emptyInputRowSetRight.container());
    buildSideinputContainer.add(nonEmptyInputRowSetRight.container());
    RowSet.SingleRowSet secondInputRowSetRight = operatorFixture.rowSetBuilder(inputSchemaRight).addRow(456).build();
    RowSet.SingleRowSet thirdInputRowSetRight = operatorFixture.rowSetBuilder(inputSchemaRight).addRow(789).build();
    buildSideinputContainer.add(secondInputRowSetRight.container());
    buildSideinputContainer.add(thirdInputRowSetRight.container());
    final MockRecordBatch mockInputBatchRight = new MockRecordBatch(operatorFixture.getFragmentContext(), opContext, buildSideinputContainer, inputOutcomesRight, batchSchemaRight);
    final MockRecordBatch mockInputBatchLeft = new MockRecordBatch(operatorFixture.getFragmentContext(), opContext, inputContainerLeft, inputOutcomesLeft, batchSchemaLeft);
    List<JoinCondition> conditions = Lists.newArrayList();
    conditions.add(new JoinCondition(SqlKind.EQUALS.toString(), FieldReference.getWithQuotedRef("leftcol"), FieldReference.getWithQuotedRef("rightcol")));
    HashJoinPOP hjConf = new HashJoinPOP(null, null, conditions, JoinRelType.INNER);
    HashJoinBatch hjBatch = new HashJoinBatch(hjConf, operatorFixture.getFragmentContext(), mockInputBatchLeft, mockInputBatchRight);
    RecordBatch.IterOutcome gotOutcome = hjBatch.next();
    assertSame(gotOutcome, RecordBatch.IterOutcome.OK_NEW_SCHEMA);
    gotOutcome = hjBatch.next();
    assertSame(gotOutcome, RecordBatch.IterOutcome.NONE);
    secondInputRowSetRight.clear();
    thirdInputRowSetRight.clear();
    buildSideinputContainer.clear();
}
Also used : MockRecordBatch(org.apache.drill.exec.physical.impl.MockRecordBatch) RecordBatch(org.apache.drill.exec.record.RecordBatch) ArrayList(java.util.ArrayList) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) HashJoinPOP(org.apache.drill.exec.physical.config.HashJoinPOP) MockRecordBatch(org.apache.drill.exec.physical.impl.MockRecordBatch) VectorContainer(org.apache.drill.exec.record.VectorContainer) JoinCondition(org.apache.drill.common.logical.data.JoinCondition) OperatorTest(org.apache.drill.categories.OperatorTest) Test(org.junit.Test)

Aggregations

JoinCondition (org.apache.drill.common.logical.data.JoinCondition)14 JoinRelType (org.apache.calcite.rel.core.JoinRelType)5 PhysicalOperator (org.apache.drill.exec.physical.base.PhysicalOperator)5 HashJoinPOP (org.apache.drill.exec.physical.config.HashJoinPOP)5 ArrayList (java.util.ArrayList)4 RexNode (org.apache.calcite.rex.RexNode)4 JClass (com.sun.codemodel.JClass)2 JVar (com.sun.codemodel.JVar)2 RelNode (org.apache.calcite.rel.RelNode)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 SqlKind (org.apache.calcite.sql.SqlKind)2 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)2 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2 MergeJoinPOP (org.apache.drill.exec.physical.config.MergeJoinPOP)2 MockRecordBatch (org.apache.drill.exec.physical.impl.MockRecordBatch)2 RecordBatch (org.apache.drill.exec.record.RecordBatch)2 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)2 OperatorTest (org.apache.drill.categories.OperatorTest)1