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