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