use of herddb.model.commands.ScanStatement in project herddb by diennea.
the class UpgradeFrom050WithBrinIndexesTest method test.
private void test(TestCase tcase, final DBManager manager, final String tableSpace) throws StatementExecutionException, DataScannerException {
System.out.println("QUERY: " + tcase.query);
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, tcase.query, Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
int size = 0;
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
for (DataAccessor r : scan1.consume()) {
System.out.println("FOUND " + r.toMap());
size++;
}
}
System.out.println("SIZE: " + size);
IndexOperation indexOperation = scan.getPredicate().getIndexOperation();
System.out.println("OPERATION: " + indexOperation);
assertTrue(tcase.expectedIndexAccessType.isAssignableFrom(scan.getPredicate().getIndexOperation().getClass()));
assertEquals(tcase.expectedRecordCount, size);
}
use of herddb.model.commands.ScanStatement in project herddb by diennea.
the class UpgradeFrom050WithBrinIndexesTest method test.
private void test(String file) throws Exception {
File dbdatadir = folder.newFolder("dbdata050_" + file);
try (InputStream in = UpgradeFrom050WithBrinIndexesTest.class.getResourceAsStream(file)) {
ZIPUtils.unZip(in, dbdatadir);
}
final Path dbdata = dbdatadir.toPath().resolve("dbdata");
Path metadataPath = dbdata.resolve("metadata");
Path dataPath = dbdata.resolve("data");
Path logsPath = dbdata.resolve("txlog");
Path tmoDir = dbdata.resolve("tmp");
assertTrue(Files.isDirectory(metadataPath));
assertTrue(Files.isDirectory(dataPath));
assertTrue(Files.isDirectory(logsPath));
Path nodeid = dataPath.resolve("nodeid");
assertTrue(Files.isRegularFile(nodeid));
String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
System.out.println("id:" + id);
String expectedNodeId = "capra";
assertTrue(id.endsWith("\n" + expectedNodeId));
try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
final String tableSpace = "herd";
final String tableName = "testtable";
assertEquals(expectedNodeId, manager.getNodeId());
assertTrue(manager.waitForTablespace(tableSpace, 10000));
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
List<Index> indexes = tableManager.getAvailableIndexes();
for (Index e : indexes) {
System.out.println("INDEX: " + e);
assertEquals(e.type, Index.TYPE_BRIN);
}
assertEquals(4, indexes.size());
for (Column c : tableManager.getTable().getColumns()) {
System.out.println("COLUMN :" + c);
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM " + tableName + " ORDER BY pk,n1,n2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
for (DataAccessor r : scan1.consume()) {
System.out.println("RECORD " + r.toMap());
}
}
}
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1", SecondaryIndexSeek.class, 4), manager, tableSpace);
// this could be SecondaryIndexSeek but we have more indexes and the planner is not so smart
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2=3", SecondaryIndexPrefixScan.class, 2), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2>=3", SecondaryIndexRangeScan.class, 3), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1 and n2=3", SecondaryIndexPrefixScan.class, 1), manager, tableSpace);
}
}
use of herddb.model.commands.ScanStatement in project herddb by diennea.
the class JSQLParserPlanner method buildSelectStatement.
private ExecutionPlan buildSelectStatement(String defaultTableSpace, int maxRows, Select select, boolean forceScan) throws StatementExecutionException {
checkSupported(select.getWithItemsList() == null);
SelectBody selectBody = select.getSelectBody();
PlannerOp op = buildSelectBody(defaultTableSpace, maxRows, selectBody, forceScan).optimize();
// Simplify Scan to Get
if (!forceScan && op instanceof BindableTableScanOp) {
ScanStatement scanStatement = op.unwrap(ScanStatement.class);
if (scanStatement != null && scanStatement.getPredicate() != null) {
Table tableDef = scanStatement.getTableDef();
CompiledSQLExpression where = scanStatement.getPredicate().unwrap(CompiledSQLExpression.class);
SQLRecordKeyFunction keyFunction = IndexUtils.findIndexAccess(where, tableDef.getPrimaryKey(), tableDef, "=", tableDef);
if (keyFunction == null || !keyFunction.isFullPrimaryKey()) {
throw new StatementExecutionException("unsupported GET not on PK (" + keyFunction + ")");
}
GetStatement get = new GetStatement(scanStatement.getTableSpace(), scanStatement.getTable(), keyFunction, scanStatement.getPredicate(), true);
return ExecutionPlan.simple(get);
}
}
return ExecutionPlan.simple(new SQLPlannedOperationStatement(op), op);
}
use of herddb.model.commands.ScanStatement in project herddb by diennea.
the class CalcitePlanner method planBindableTableScan.
private PlannerOp planBindableTableScan(BindableTableScan scan, RelDataType rowType) {
if (rowType == null) {
rowType = scan.getRowType();
}
final String tableSpace = scan.getTable().getQualifiedName().get(0);
final TableImpl tableImpl = (TableImpl) scan.getTable().unwrap(org.apache.calcite.schema.Table.class);
Table table = tableImpl.tableManager.getTable();
SQLRecordPredicate predicate = null;
if (!scan.filters.isEmpty()) {
CompiledSQLExpression where = null;
if (scan.filters.size() == 1) {
RexNode expr = scan.filters.get(0);
where = SQLExpressionCompiler.compileExpression(expr);
} else {
CompiledSQLExpression[] operands = new CompiledSQLExpression[scan.filters.size()];
int i = 0;
for (RexNode expr : scan.filters) {
CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(expr);
operands[i++] = condition;
}
where = new CompiledMultiAndExpression(operands);
}
predicate = new SQLRecordPredicate(table, null, where);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
IndexUtils.discoverIndexOperations(tableSpace, where, table, predicate, scan, tableSpaceManager);
}
List<RexNode> projections = new ArrayList<>(scan.projects.size());
int i = 0;
for (int fieldpos : scan.projects) {
projections.add(new RexInputRef(fieldpos, rowType.getFieldList().get(i++).getType()));
}
Projection projection = buildProjection(projections, rowType, true, table.columns);
ScanStatement scanStatement = new ScanStatement(tableSpace, table.name, projection, predicate, null, null);
scanStatement.setTableDef(table);
return new BindableTableScanOp(scanStatement);
}
use of herddb.model.commands.ScanStatement in project herddb by diennea.
the class CalcitePlanner method planUpdate.
private PlannerOp planUpdate(EnumerableTableModify dml, boolean returnValues) {
PlannerOp input = convertRelNode(dml.getInput(), null, false, false);
List<String> updateColumnList = dml.getUpdateColumnList();
List<RexNode> sourceExpressionList = dml.getSourceExpressionList();
final String tableSpace = dml.getTable().getQualifiedName().get(0);
final String tableName = dml.getTable().getQualifiedName().get(1);
final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
Table table = tableImpl.tableManager.getTable();
List<CompiledSQLExpression> expressionsForValue = new ArrayList<>(sourceExpressionList.size());
List<CompiledSQLExpression> expressionsForKey = new ArrayList<>(sourceExpressionList.size());
List<String> updateColumnListInValue = new ArrayList<>(updateColumnList.size());
List<String> updateColumnListInPk = new ArrayList<>();
for (int i = 0; i < updateColumnList.size(); i++) {
String columnName = updateColumnList.get(i);
boolean isPk = table.isPrimaryKeyColumn(columnName);
RexNode node = sourceExpressionList.get(i);
CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
if (isPk) {
updateColumnListInPk.add(columnName);
expressionsForKey.add(exp);
} else {
updateColumnListInValue.add(columnName);
expressionsForValue.add(exp);
}
}
if (expressionsForKey.isEmpty()) {
// standard UPDATE, we are not updating any column in the PK
RecordFunction function = new SQLRecordFunction(updateColumnListInValue, table, expressionsForValue);
UpdateStatement update = null;
if (input instanceof TableScanOp) {
update = new UpdateStatement(tableSpace, tableName, null, function, null);
} else if (input instanceof FilterOp) {
FilterOp filter = (FilterOp) input;
if (filter.getInput() instanceof TableScanOp) {
SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
} else if (input instanceof ProjectOp) {
ProjectOp proj = (ProjectOp) input;
if (proj.getInput() instanceof TableScanOp) {
update = new UpdateStatement(tableSpace, tableName, null, function, null);
} else if (proj.getInput() instanceof FilterOp) {
FilterOp filter = (FilterOp) proj.getInput();
if (filter.getInput() instanceof TableScanOp) {
SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
} else if (proj.getInput() instanceof FilteredTableScanOp) {
FilteredTableScanOp filter = (FilteredTableScanOp) proj.getInput();
Predicate pred = filter.getPredicate();
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
} else if (proj.getInput() instanceof BindableTableScanOp) {
BindableTableScanOp filter = (BindableTableScanOp) proj.getInput();
ScanStatement scan = filter.getStatement();
if (scan.getComparator() == null && scan.getLimits() == null && scan.getTableDef() != null) {
Predicate pred = scan.getPredicate();
update = new UpdateStatement(tableSpace, tableName, null, function, pred);
}
}
}
if (update != null) {
return new SimpleUpdateOp(update.setReturnValues(returnValues));
} else {
return new UpdateOp(tableSpace, tableName, input, returnValues, function);
}
} else {
// bad stuff ! we are updating the PK, we need to transform this to a sequence of delete and inserts
// ReplaceOp won't execute the two statements atomically
RecordFunction functionForValue = new SQLRecordFunction(updateColumnListInValue, table, expressionsForValue);
SQLRecordKeyFunction functionForKey = new SQLRecordKeyFunction(updateColumnListInPk, expressionsForKey, table);
return new ReplaceOp(tableSpace, tableName, input, returnValues, functionForKey, functionForValue);
}
}
Aggregations