Search in sources :

Example 1 with MaterializedRecordSet

use of herddb.core.MaterializedRecordSet in project herddb by diennea.

the class AbstractSystemTableManager method scan.

@Override
public DataScanner scan(ScanStatement statement, StatementEvaluationContext context, Transaction transaction, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    Predicate predicate = statement.getPredicate();
    MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(table.columnNames, table.columns);
    Iterable<Record> data = buildVirtualRecordList(transaction);
    StreamSupport.stream(data.spliterator(), false).filter(record -> {
        return (predicate == null || predicate.evaluate(record, context));
    }).sorted(// enforce sort by PK
    sortByPk).map(r -> r.getDataAccessor(table)).forEach(recordSet::add);
    recordSet.writeFinished();
    recordSet.sort(statement.getComparator());
    recordSet.applyLimits(statement.getLimits(), context);
    recordSet.applyProjection(statement.getProjection(), context);
    return new SimpleDataScanner(transaction, recordSet);
}
Also used : SimpleDataScanner(herddb.core.SimpleDataScanner) AbstractTableManager(herddb.core.AbstractTableManager) TableSpaceManager(herddb.core.TableSpaceManager) LogEntry(herddb.log.LogEntry) Table(herddb.model.Table) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(herddb.model.Transaction) MaterializedRecordSet(herddb.core.MaterializedRecordSet) InvalidTableException(herddb.model.InvalidTableException) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) SQLRecordPredicateFunctions(herddb.utils.SQLRecordPredicateFunctions) Futures(herddb.utils.Futures) DataStorageManagerException(herddb.storage.DataStorageManagerException) Index(herddb.model.Index) DataScanner(herddb.model.DataScanner) Record(herddb.model.Record) CommitLogResult(herddb.log.CommitLogResult) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) LogSequenceNumber(herddb.log.LogSequenceNumber) StatementExecutionResult(herddb.model.StatementExecutionResult) KeyToPageIndex(herddb.index.KeyToPageIndex) Consumer(java.util.function.Consumer) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) Predicate(herddb.model.Predicate) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Comparator(java.util.Comparator) Collections(java.util.Collections) TableManagerStats(herddb.core.stats.TableManagerStats) Statement(herddb.model.Statement) MaterializedRecordSet(herddb.core.MaterializedRecordSet) Record(herddb.model.Record) SimpleDataScanner(herddb.core.SimpleDataScanner) Predicate(herddb.model.Predicate)

Example 2 with MaterializedRecordSet

use of herddb.core.MaterializedRecordSet in project herddb by diennea.

the class NestedLoopJoinOp 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);
    DataScanner leftScanner = resLeft.dataScanner;
    transactionContext = new TransactionContext(resLeft.transactionId);
    ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    DataScanner rightScanner = resRight.dataScanner;
    final JoinType linq4jJoinType = CalciteEnumUtils.toLinq4jJoinType(joinRelType);
    if (!linq4jJoinType.generatesNullsOnLeft() && !rightScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(rightScanner.getFieldNames(), rightScanner.getSchema());
            rightScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(rightScanner.getTransaction(), recordSet);
            rightScanner.close();
            rightScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    final long resTransactionId = resRight.transactionId;
    final String[] fieldNamesFromLeft = leftScanner.getFieldNames();
    final String[] fieldNamesFromRight = rightScanner.getFieldNames();
    final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
    Enumerable<DataAccessor> result = EnumerableDefaults.nestedLoopJoin(leftScanner.createNonRewindableEnumerable(), rightScanner.createRewindOnCloseEnumerable(), predicate(resultProjection, context), resultProjection, linq4jJoinType);
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(rightScanner.getTransaction(), fieldNames, columns, result, leftScanner, rightScanner);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) MaterializedRecordSet(herddb.core.MaterializedRecordSet) DataAccessor(herddb.utils.DataAccessor) JoinType(org.apache.calcite.linq4j.JoinType) StatementExecutionException(herddb.model.StatementExecutionException) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScannerException(herddb.model.DataScannerException)

Example 3 with MaterializedRecordSet

use of herddb.core.MaterializedRecordSet in project herddb by diennea.

the class SemiJoinOp 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);
    DataScanner leftScanner = resLeft.dataScanner;
    DataScanner rightScanner = resRight.dataScanner;
    if (!leftScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(leftScanner.getFieldNames(), leftScanner.getSchema());
            leftScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(leftScanner.getTransaction(), recordSet);
            leftScanner.close();
            leftScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    if (!rightScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(rightScanner.getFieldNames(), rightScanner.getSchema());
            rightScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(rightScanner.getTransaction(), recordSet);
            rightScanner.close();
            rightScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    final long resTransactionId = resRight.transactionId;
    Enumerable<DataAccessor> result = EnumerableDefaults.semiJoin(leftScanner.createRewindOnCloseEnumerable(), rightScanner.createRewindOnCloseEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys));
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(rightScanner.getTransaction(), fieldNames, columns, result, leftScanner, rightScanner);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) MaterializedRecordSet(herddb.core.MaterializedRecordSet) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) SimpleDataScanner(herddb.core.SimpleDataScanner) StatementExecutionException(herddb.model.StatementExecutionException) DataScannerException(herddb.model.DataScannerException)

Example 4 with MaterializedRecordSet

use of herddb.core.MaterializedRecordSet in project herddb by diennea.

the class JoinOp 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;
    DataScanner leftScanner = resLeft.dataScanner;
    final String[] fieldNamesFromLeft = leftScanner.getFieldNames();
    final String[] fieldNamesFromRight = resRight.dataScanner.getFieldNames();
    Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
    final Predicate2 predicate;
    DataScanner rightScanner = resRight.dataScanner;
    if (nonEquiConditions != null && !nonEquiConditions.isEmpty()) {
        if (mergeJoin) {
            throw new IllegalStateException("Unspected nonEquiConditions " + nonEquiConditions + "" + "for merge join");
        }
        predicate = (Predicate2) (Object t0, Object t1) -> {
            DataAccessor da0 = (DataAccessor) t0;
            DataAccessor da1 = (DataAccessor) t1;
            DataAccessor currentRow = resultProjection.apply(da0, da1);
            for (CompiledSQLExpression exp : nonEquiConditions) {
                Object result = exp.evaluate(currentRow, context);
                boolean asBoolean = SQLRecordPredicateFunctions.toBoolean(result);
                if (!asBoolean) {
                    return false;
                }
            }
            return true;
        };
    } else {
        predicate = null;
    }
    if (!mergeJoin && !leftScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(leftScanner.getFieldNames(), leftScanner.getSchema());
            leftScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(leftScanner.getTransaction(), recordSet);
            leftScanner.close();
            leftScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    if (!mergeJoin && !rightScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(rightScanner.getFieldNames(), rightScanner.getSchema());
            rightScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(rightScanner.getTransaction(), recordSet);
            rightScanner.close();
            rightScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    Enumerable<DataAccessor> result = mergeJoin ? EnumerableDefaults.mergeJoin(leftScanner.createNonRewindableEnumerable(), rightScanner.createNonRewindableEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys), resultProjection, generateNullsOnLeft, generateNullsOnRight) : EnumerableDefaults.hashJoin(leftScanner.createRewindOnCloseEnumerable(), rightScanner.createRewindOnCloseEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys), resultProjection, null, generateNullsOnLeft, generateNullsOnRight, predicate);
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(rightScanner.getTransaction(), fieldNames, columns, result, leftScanner, rightScanner);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) MaterializedRecordSet(herddb.core.MaterializedRecordSet) DataAccessor(herddb.utils.DataAccessor) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) Predicate2(org.apache.calcite.linq4j.function.Predicate2) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScannerException(herddb.model.DataScannerException)

Example 5 with MaterializedRecordSet

use of herddb.core.MaterializedRecordSet in project herddb by diennea.

the class SortOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    // TODO merge projection + scan + sort + limit
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    ScanResult downstreamScanResult = (ScanResult) input;
    final DataScanner inputScanner = downstreamScanResult.dataScanner;
    try (DataScanner dataScanner = inputScanner) {
        MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(inputScanner.getFieldNames(), inputScanner.getSchema());
        while (dataScanner.hasNext()) {
            DataAccessor row = dataScanner.next();
            recordSet.add(row);
        }
        recordSet.writeFinished();
        recordSet.sort(this);
        SimpleDataScanner result = new SimpleDataScanner(downstreamScanResult.dataScanner.getTransaction(), recordSet);
        return new ScanResult(downstreamScanResult.transactionId, result);
    } catch (DataScannerException ex) {
        throw new StatementExecutionException(ex);
    }
}
Also used : ScanResult(herddb.model.ScanResult) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) MaterializedRecordSet(herddb.core.MaterializedRecordSet) DataAccessor(herddb.utils.DataAccessor) StatementExecutionResult(herddb.model.StatementExecutionResult) SimpleDataScanner(herddb.core.SimpleDataScanner) StatementExecutionException(herddb.model.StatementExecutionException) DataScannerException(herddb.model.DataScannerException)

Aggregations

MaterializedRecordSet (herddb.core.MaterializedRecordSet)5 SimpleDataScanner (herddb.core.SimpleDataScanner)5 DataScanner (herddb.model.DataScanner)5 StatementExecutionException (herddb.model.StatementExecutionException)5 DataScannerException (herddb.model.DataScannerException)4 ScanResult (herddb.model.ScanResult)4 DataAccessor (herddb.utils.DataAccessor)4 TransactionContext (herddb.model.TransactionContext)3 StatementExecutionResult (herddb.model.StatementExecutionResult)2 AbstractTableManager (herddb.core.AbstractTableManager)1 TableSpaceManager (herddb.core.TableSpaceManager)1 TableManagerStats (herddb.core.stats.TableManagerStats)1 KeyToPageIndex (herddb.index.KeyToPageIndex)1 CommitLogResult (herddb.log.CommitLogResult)1 LogEntry (herddb.log.LogEntry)1 LogSequenceNumber (herddb.log.LogSequenceNumber)1 DDLException (herddb.model.DDLException)1 Index (herddb.model.Index)1 InvalidTableException (herddb.model.InvalidTableException)1 Predicate (herddb.model.Predicate)1