Search in sources :

Example 1 with ProjectOp

use of herddb.model.planner.ProjectOp 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);
    }
}
Also used : SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp) UpdateOp(herddb.model.planner.UpdateOp) FilterOp(herddb.model.planner.FilterOp) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ScanStatement(herddb.model.commands.ScanStatement) UpdateStatement(herddb.model.commands.UpdateStatement) PlannerOp(herddb.model.planner.PlannerOp) Table(herddb.model.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) FilteredTableScanOp(herddb.model.planner.FilteredTableScanOp) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) TableScanOp(herddb.model.planner.TableScanOp) ProjectOp(herddb.model.planner.ProjectOp) FilteredTableScanOp(herddb.model.planner.FilteredTableScanOp) SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp) RexNode(org.apache.calcite.rex.RexNode) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Example 2 with ProjectOp

use of herddb.model.planner.ProjectOp in project herddb by diennea.

the class CalcitePlanner method planProject.

private PlannerOp planProject(EnumerableProject op, RelDataType rowType) {
    PlannerOp input = convertRelNode(op.getInput(), null, false);
    final List<RexNode> projects = op.getProjects();
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    Projection projection = buildProjection(projects, _rowType, false, null);
    return new ProjectOp(projection, input);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ProjectOp(herddb.model.planner.ProjectOp) Projection(herddb.model.Projection) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

PlannerOp (herddb.model.planner.PlannerOp)2 ProjectOp (herddb.model.planner.ProjectOp)2 RexNode (org.apache.calcite.rex.RexNode)2 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)1 Predicate (herddb.model.Predicate)1 Projection (herddb.model.Projection)1 RecordFunction (herddb.model.RecordFunction)1 Table (herddb.model.Table)1 ScanStatement (herddb.model.commands.ScanStatement)1 UpdateStatement (herddb.model.commands.UpdateStatement)1 BindableTableScanOp (herddb.model.planner.BindableTableScanOp)1 FilterOp (herddb.model.planner.FilterOp)1 FilteredTableScanOp (herddb.model.planner.FilteredTableScanOp)1 SimpleUpdateOp (herddb.model.planner.SimpleUpdateOp)1 TableScanOp (herddb.model.planner.TableScanOp)1 UpdateOp (herddb.model.planner.UpdateOp)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1 ArrayList (java.util.ArrayList)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1