Search in sources :

Example 6 with ScanResult

use of herddb.model.ScanResult in project herddb by diennea.

the class ThetaJoinOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    transactionContext = new TransactionContext(resLeft.transactionId);
    ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    final long resTransactionId = resRight.transactionId;
    final String[] fieldNamesFromLeft = resLeft.dataScanner.getFieldNames();
    final String[] fieldNamesFromRight = resRight.dataScanner.getFieldNames();
    final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
    Enumerable<DataAccessor> result = EnumerableDefaults.thetaJoin(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), predicate(resultProjection, context), resultProjection, generateNullsOnLeft, generateNullsOnRight);
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resTransactionId, fieldNames, columns, result);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) AbstractDataAccessor(herddb.utils.AbstractDataAccessor)

Example 7 with ScanResult

use of herddb.model.ScanResult in project herddb by diennea.

the class ValuesOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    Iterator<List<CompiledSQLExpression>> it = tuples.iterator();
    DataScanner res = new DataScanner(transactionContext.transactionId, fieldNames, columns) {

        @Override
        public boolean hasNext() throws DataScannerException {
            return it.hasNext();
        }

        @Override
        public DataAccessor next() throws DataScannerException {
            Object[] values = new Object[fieldNames.length];
            List<CompiledSQLExpression> tuple = it.next();
            for (int i = 0; i < values.length; i++) {
                Object value = tuple.get(i).evaluate(DataAccessor.NULL, context);
                values[i] = value;
            }
            return new Tuple(fieldNames, values);
        }
    };
    return new ScanResult(transactionContext.transactionId, res);
}
Also used : ScanResult(herddb.model.ScanResult) DataScanner(herddb.model.DataScanner) List(java.util.List) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Tuple(herddb.model.Tuple)

Example 8 with ScanResult

use of herddb.model.ScanResult in project herddb by diennea.

the class SecondaryIndexAccessSuite method createIndexOnEmptyTable.

@Test
public void createIndexOnEmptyTable() 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);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        Index index = Index.builder().onTable(table).type(indexType).column("name", ColumnTypes.STRING).build();
        // create index, initially it will be empty
        CreateIndexStatement st3 = new CreateIndexStatement(index);
        manager.executeStatement(st3, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('a','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('b','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('c','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('d','n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('e','n2')", Collections.emptyList());
        TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
        ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
        assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
        try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
            assertEquals(3, scan1.consume().size());
        }
    }
}
Also used : ScanResult(herddb.model.ScanResult) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 9 with ScanResult

use of herddb.model.ScanResult in project herddb by diennea.

the class DBManager method executeDataScannerPlan.

private StatementExecutionResult executeDataScannerPlan(ExecutionPlan plan, DataScanner result, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
    ScanResult scanResult;
    if (plan.mainAggregator != null) {
        scanResult = new ScanResult(transactionContext.transactionId, plan.mainAggregator.aggregate(result, context));
    } else {
        scanResult = new ScanResult(transactionContext.transactionId, result);
    }
    if (plan.comparator != null) {
        // SORT is to be applied before limits
        MaterializedRecordSet sortedSet = recordSetFactory.createRecordSet(scanResult.dataScanner.getFieldNames(), scanResult.dataScanner.getSchema());
        try {
            scanResult.dataScanner.forEach(sortedSet::add);
            sortedSet.writeFinished();
            sortedSet.sort(plan.comparator);
            scanResult.dataScanner.close();
            scanResult = new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, sortedSet));
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    if (plan.limits != null) {
        try {
            return new ScanResult(transactionContext.transactionId, new LimitedDataScanner(scanResult.dataScanner, plan.limits, context));
        } catch (DataScannerException limitError) {
            throw new StatementExecutionException(limitError);
        }
    } else {
        return scanResult;
    }
}
Also used : ScanResult(herddb.model.ScanResult) LimitedDataScanner(herddb.model.LimitedDataScanner) StatementExecutionException(herddb.model.StatementExecutionException) DataScannerException(herddb.model.DataScannerException)

Example 10 with ScanResult

use of herddb.model.ScanResult in project herddb by diennea.

the class DBManager method executePlan.

public StatementExecutionResult executePlan(ExecutionPlan plan, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
    context.setManager(this);
    plan.validateContext(context);
    if (plan.mainStatement instanceof ScanStatement) {
        DataScanner result = scan((ScanStatement) plan.mainStatement, context, transactionContext);
        // transction can be auto generated during the scan
        transactionContext = new TransactionContext(result.transactionId);
        return executeDataScannerPlan(plan, result, context, transactionContext);
    } else if (plan.dataSource != null) {
        // INSERT from SELECT
        try {
            ScanResult data = (ScanResult) executePlan(plan.dataSource, context, transactionContext);
            int insertCount = 0;
            try {
                // transction can be auto generated during the scan
                transactionContext = new TransactionContext(data.transactionId);
                while (data.dataScanner.hasNext()) {
                    DataAccessor tuple = data.dataScanner.next();
                    SQLStatementEvaluationContext tmp_context = new SQLStatementEvaluationContext("--", Arrays.asList(tuple.getValues()));
                    DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(plan.mainStatement, tmp_context, transactionContext);
                    insertCount += res.getUpdateCount();
                }
            } finally {
                data.dataScanner.close();
            }
            return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    } else if (plan.joinStatements != null) {
        List<DataScanner> scanResults = new ArrayList<>();
        for (ScanStatement statement : plan.joinStatements) {
            DataScanner result = scan(statement, context, transactionContext);
            // transction can be auto generated during the scan
            transactionContext = new TransactionContext(result.transactionId);
            scanResults.add(result);
        }
        return executeJoinedScansPlan(scanResults, context, transactionContext, plan);
    } else if (plan.insertStatements != null) {
        int insertCount = 0;
        for (InsertStatement insert : plan.insertStatements) {
            DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(insert, context, transactionContext);
            // transction can be auto generated during the loop
            transactionContext = new TransactionContext(res.transactionId);
            insertCount += res.getUpdateCount();
        }
        return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
    } else {
        return executeStatement(plan.mainStatement, context, transactionContext);
    }
}
Also used : ScanResult(herddb.model.ScanResult) DataScanner(herddb.model.DataScanner) LimitedDataScanner(herddb.model.LimitedDataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) SQLStatementEvaluationContext(herddb.sql.SQLStatementEvaluationContext) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ScanStatement(herddb.model.commands.ScanStatement) DataScannerException(herddb.model.DataScannerException)

Aggregations

ScanResult (herddb.model.ScanResult)18 DataScanner (herddb.model.DataScanner)14 DataScannerException (herddb.model.DataScannerException)11 StatementExecutionException (herddb.model.StatementExecutionException)11 StatementExecutionResult (herddb.model.StatementExecutionResult)10 DataAccessor (herddb.utils.DataAccessor)10 TransactionContext (herddb.model.TransactionContext)9 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)5 DMLStatement (herddb.model.DMLStatement)4 LimitedDataScanner (herddb.model.LimitedDataScanner)4 Table (herddb.model.Table)4 SimpleDataScanner (herddb.core.SimpleDataScanner)3 ScanStatement (herddb.model.commands.ScanStatement)3 ArrayList (java.util.ArrayList)3 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)2 Column (herddb.model.Column)2 NotLeaderException (herddb.model.NotLeaderException)2 InsertStatement (herddb.model.commands.InsertStatement)2 RawKeyEquals (herddb.model.predicates.RawKeyEquals)2 TranslatedQuery (herddb.sql.TranslatedQuery)2