use of herddb.model.planner.NestedLoopJoinOp in project herddb by diennea.
the class CalcitePlanner method planEnumerableNestedLoopJoin.
private PlannerOp planEnumerableNestedLoopJoin(EnumerableNestedLoopJoin op, RelDataType rowType) {
PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
PlannerOp right = convertRelNode(op.getRight(), null, false, false);
CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
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;
}
return new NestedLoopJoinOp(fieldNames, columns, left, right, condition, op.getJoinType(), false);
}
use of herddb.model.planner.NestedLoopJoinOp in project herddb by diennea.
the class SimpleJoinTest method testStartTransactionInJoinBranch.
@Test
public void testStartTransactionInJoinBranch() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.table1 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.table2 (k2 string primary key,n2 int,s2 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('a',1,'A')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('b',2,'B')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('c',3,'A')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('d',4,'A')", Collections.emptyList());
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM" + " tblspace1.table1 t1" + " JOIN tblspace1.table2 t2" + " ON t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList(), true, true, false, -1);
translated.context.setForceRetainReadLock(true);
if (manager.getPlanner() instanceof CalcitePlanner) {
assertThat(translated.plan.originalRoot, instanceOf(NestedLoopJoinOp.class));
NestedLoopJoinOp join = (NestedLoopJoinOp) translated.plan.originalRoot;
assertThat(join.getLeft(), instanceOf(SimpleScanOp.class));
assertThat(join.getRight(), instanceOf(SimpleScanOp.class));
}
// we want that the left branch of the join starts the transactoion
ScanResult scanResult = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.AUTOTRANSACTION_TRANSACTION));
List<DataAccessor> tuples = scanResult.dataScanner.consumeAndClose();
assertTrue(scanResult.transactionId > 0);
for (DataAccessor t : tuples) {
System.out.println("t:" + t);
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
TestUtils.commitTransaction(manager, "tblspace1", scanResult.transactionId);
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM" + " tblspace1.table1 t1" + " JOIN tblspace1.table2 t2" + " ON t1.n1 > 0" + " and t2.n2 >= 1" + " order by t1.n1, t2.n2", Collections.emptyList(), true, true, false, // with order by
-1);
translated.context.setForceRetainReadLock(true);
assertThat(translated.plan.originalRoot, instanceOf(SortOp.class));
if (manager.getPlanner() instanceof CalcitePlanner) {
NestedLoopJoinOp join = (NestedLoopJoinOp) ((SortOp) translated.plan.originalRoot).getInput();
assertThat(join.getLeft(), instanceOf(SimpleScanOp.class));
assertThat(join.getRight(), instanceOf(SimpleScanOp.class));
} else {
JoinOp join = (JoinOp) ((SortOp) translated.plan.originalRoot).getInput();
assertThat(join.getLeft(), instanceOf(SimpleScanOp.class));
assertThat(join.getRight(), instanceOf(SimpleScanOp.class));
}
// we want that the left branch of the join starts the transactoion
ScanResult scanResult = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.AUTOTRANSACTION_TRANSACTION));
List<DataAccessor> tuples = scanResult.dataScanner.consumeAndClose();
assertTrue(scanResult.transactionId > 0);
for (DataAccessor t : tuples) {
System.out.println("t:" + t);
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
TestUtils.commitTransaction(manager, "tblspace1", scanResult.transactionId);
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT t1.s1 FROM" + " tblspace1.table1 t1" + " JOIN tblspace1.table2 t2" + " ON t1.n1 > 0" + " and t2.n2 >= 1" + " order by t1.n1, t2.n2", Collections.emptyList(), true, true, false, // with order by on non selected fields
-1);
translated.context.setForceRetainReadLock(true);
assertThat(translated.plan.originalRoot, instanceOf(ProjectOp.class));
SortOp sort = (SortOp) ((ProjectOp) translated.plan.originalRoot).getInput();
assertThat(((ProjectOp) translated.plan.originalRoot).getProjection(), instanceOf(ProjectOp.ZeroCopyProjection.class));
if (manager.getPlanner() instanceof CalcitePlanner) {
NestedLoopJoinOp join = (NestedLoopJoinOp) sort.getInput();
assertThat(join.getLeft(), instanceOf(SimpleScanOp.class));
assertThat(join.getRight(), instanceOf(SimpleScanOp.class));
} else {
JoinOp join = (JoinOp) sort.getInput();
assertThat(join.getLeft(), instanceOf(SimpleScanOp.class));
assertThat(join.getRight(), instanceOf(SimpleScanOp.class));
}
// we want that the left branch of the join starts the transactoion
ScanResult scanResult = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.AUTOTRANSACTION_TRANSACTION));
List<DataAccessor> tuples = scanResult.dataScanner.consumeAndClose();
assertTrue(scanResult.transactionId > 0);
for (DataAccessor t : tuples) {
System.out.println("t:" + t);
assertEquals(1, t.getFieldNames().length);
assertEquals("s1", t.getFieldNames()[0]);
}
assertEquals(4, tuples.size());
TestUtils.commitTransaction(manager, "tblspace1", scanResult.transactionId);
}
}
}
Aggregations