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