use of herddb.model.planner.BindableTableScanOp in project herddb by diennea.
the class CalcitePlanner method planUpdate.
private PlannerOp planUpdate(EnumerableTableModify dml, boolean returnValues) {
PlannerOp input = convertRelNode(dml.getInput(), null, 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> expressions = new ArrayList<>(sourceExpressionList.size());
for (RexNode node : sourceExpressionList) {
CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
expressions.add(exp);
}
RecordFunction function = new SQLRecordFunction(updateColumnList, table, expressions);
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);
}
}
use of herddb.model.planner.BindableTableScanOp 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);
IndexOperation op = scanForIndexAccess(where, table, tableSpaceManager);
predicate.setIndexOperation(op);
CompiledSQLExpression filterPk = findFiltersOnPrimaryKey(table, where);
if (filterPk != null) {
filterPk = remapPositionalAccessToToPrimaryKeyAccessor(filterPk, table, scan);
}
predicate.setPrimaryKeyFilter(filterPk);
}
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.planner.BindableTableScanOp in project herddb by diennea.
the class CalcitePlanner method planDelete.
private PlannerOp planDelete(EnumerableTableModify dml) {
PlannerOp input = convertRelNode(dml.getInput(), null, false);
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();
DeleteStatement delete = null;
if (input instanceof TableScanOp) {
delete = new DeleteStatement(tableSpace, tableName, null, null);
} else if (input instanceof FilterOp) {
FilterOp filter = (FilterOp) input;
if (filter.getInput() instanceof TableScanOp) {
SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
delete = new DeleteStatement(tableSpace, tableName, null, pred);
}
} else if (input instanceof BindableTableScanOp) {
BindableTableScanOp filter = (BindableTableScanOp) input;
Predicate pred = filter.getStatement().getPredicate();
delete = new DeleteStatement(tableSpace, tableName, null, pred);
}
if (delete != null) {
return new SimpleDeleteOp(delete);
} else {
return new DeleteOp(tableSpace, tableName, input);
}
}
use of herddb.model.planner.BindableTableScanOp in project herddb by diennea.
the class CalcitePlannerTest method simplePlansTests.
@Test
public void simplePlansTests() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
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);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='mykey'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
assertInstanceOf(plan(manager, "select * from tblspace1.tsql"), TableScanOp.class);
assertInstanceOf(plan(manager, "select * from tblspace1.tsql where n1=1"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "select n1 from tblspace1.tsql"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=? where k1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=? where k1=? and n1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=?" + " where n1 in (select b.n1*2 from tblspace1.tsql b)"), UpdateOp.class);
assertInstanceOf(plan(manager, "delete from tblspace1.tsql where k1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "delete from tblspace1.tsql where k1=? and n1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "delete from tblspace1.tsql where n1 in (select b.n1*2 from tblspace1.tsql b)"), DeleteOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)"), SimpleInsertOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?),(?,?)"), InsertOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1 limit 10"), LimitedSortedBindableTableScanOp.class);
{
BindableTableScanOp plan = assertInstanceOf(plan(manager, "select * from tblspace1.tsql where k1=?"), BindableTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(IdentityProjection.class));
assertNull(plan.getStatement().getComparator());
}
{
SortedBindableTableScanOp plan = assertInstanceOf(plan(manager, "select n1,k1 from tblspace1.tsql order by n1"), SortedBindableTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(ZeroCopyProjection.class));
ZeroCopyProjection zero = (ZeroCopyProjection) projection;
assertArrayEquals(new String[] { "n1", "k1" }, zero.getFieldNames());
assertEquals("n1", zero.getColumns()[0].name);
assertEquals("k1", zero.getColumns()[1].name);
TupleComparator comparator = plan.getStatement().getComparator();
System.out.println("comparator:" + comparator);
assertThat(comparator, instanceOf(SortOp.class));
SortOp sortOp = (SortOp) comparator;
assertFalse(sortOp.isOnlyPrimaryKeyAndAscending());
}
{
SortedBindableTableScanOp plan = assertInstanceOf(plan(manager, "select n1,k1 from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(ZeroCopyProjection.class));
ZeroCopyProjection zero = (ZeroCopyProjection) projection;
assertArrayEquals(new String[] { "n1", "k1" }, zero.getFieldNames());
assertEquals("n1", zero.getColumns()[0].name);
assertEquals("k1", zero.getColumns()[1].name);
TupleComparator comparator = plan.getStatement().getComparator();
System.out.println("comparator:" + comparator);
assertThat(comparator, instanceOf(SortOp.class));
SortOp sortOp = (SortOp) comparator;
assertTrue(sortOp.isOnlyPrimaryKeyAndAscending());
}
{
SortedTableScanOp plan = assertInstanceOf(plan(manager, "select * from tblspace1.tsql order by n1"), SortedTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(IdentityProjection.class));
IdentityProjection zero = (IdentityProjection) projection;
assertArrayEquals(new String[] { "k1", "n1", "s1" }, zero.getFieldNames());
assertEquals("k1", zero.getColumns()[0].name);
assertEquals("n1", zero.getColumns()[1].name);
assertEquals("s1", zero.getColumns()[2].name);
TupleComparator comparator = plan.getStatement().getComparator();
System.out.println("comparator:" + comparator);
assertThat(comparator, instanceOf(SortOp.class));
SortOp sortOp = (SortOp) comparator;
assertFalse(sortOp.isOnlyPrimaryKeyAndAscending());
}
{
SortedTableScanOp plan = assertInstanceOf(plan(manager, "select * from tblspace1.tsql order by k1"), SortedTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(IdentityProjection.class));
IdentityProjection zero = (IdentityProjection) projection;
assertArrayEquals(new String[] { "k1", "n1", "s1" }, zero.getFieldNames());
assertEquals("k1", zero.getColumns()[0].name);
assertEquals("n1", zero.getColumns()[1].name);
assertEquals("s1", zero.getColumns()[2].name);
TupleComparator comparator = plan.getStatement().getComparator();
System.out.println("comparator:" + comparator);
assertThat(comparator, instanceOf(SortOp.class));
SortOp sortOp = (SortOp) comparator;
assertTrue(sortOp.isOnlyPrimaryKeyAndAscending());
}
}
}
use of herddb.model.planner.BindableTableScanOp in project herddb by diennea.
the class CalcitePlannerTest method dirtySQLPlansTests.
@Test
public void dirtySQLPlansTests() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
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);
execute(manager, "\n\nCREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "\n\nINSERT INTO tblspace1.tsql (k1,n1) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='mykey'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
assertInstanceOf(plan(manager, "-- comment\nselect * from tblspace1.tsql"), TableScanOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\nselect * from tblspace1.tsql where n1=1"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "\n\nselect n1 from tblspace1.tsql"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "-- comment\nupdate tblspace1.tsql set n1=? where k1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\nupdate tblspace1.tsql set n1=? where k1=? and n1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=?" + " where n1 in (select b.n1*2 from tblspace1.tsql b)"), UpdateOp.class);
assertInstanceOf(plan(manager, "-- comment\ndelete from tblspace1.tsql where k1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\ndelete from tblspace1.tsql where k1=? and n1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "\n\ndelete from tblspace1.tsql where n1 in (select b.n1*2 from tblspace1.tsql b)"), DeleteOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)"), SimpleInsertOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?),(?,?)"), InsertOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1 limit 10"), LimitedSortedBindableTableScanOp.class);
BindableTableScanOp plan = (BindableTableScanOp) assertInstanceOf(plan(manager, "select * from tblspace1.tsql where k1=?"), BindableTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(IdentityProjection.class));
assertNull(plan.getStatement().getComparator());
}
}
Aggregations