Search in sources :

Example 1 with NestedLoopJoinOp

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);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

Example 2 with NestedLoopJoinOp

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);
        }
    }
}
Also used : ScanResult(herddb.model.ScanResult) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) ProjectOp(herddb.model.planner.ProjectOp) RawString(herddb.utils.RawString) SortOp(herddb.model.planner.SortOp) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp) JoinOp(herddb.model.planner.JoinOp) Test(org.junit.Test)

Aggregations

NestedLoopJoinOp (herddb.model.planner.NestedLoopJoinOp)2 DBManager (herddb.core.DBManager)1 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)1 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)1 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)1 Column (herddb.model.Column)1 ScanResult (herddb.model.ScanResult)1 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)1 JoinOp (herddb.model.planner.JoinOp)1 PlannerOp (herddb.model.planner.PlannerOp)1 ProjectOp (herddb.model.planner.ProjectOp)1 SortOp (herddb.model.planner.SortOp)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1 DataAccessor (herddb.utils.DataAccessor)1 RawString (herddb.utils.RawString)1 List (java.util.List)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 Test (org.junit.Test)1