Search in sources :

Example 1 with PlannerOp

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

the class CalcitePlanner method planAggregate.

private PlannerOp planAggregate(EnumerableAggregate op, RelDataType rowType, boolean returnValues) {
    List<RelDataTypeField> fieldList = op.getRowType().getFieldList();
    List<AggregateCall> calls = op.getAggCallList();
    String[] fieldnames = new String[fieldList.size()];
    String[] aggtypes = new String[calls.size()];
    Column[] columns = new Column[fieldList.size()];
    List<Integer> groupedFiledsIndexes = op.getGroupSet().toList();
    List<List<Integer>> argLists = new ArrayList<>(calls.size());
    int i = 0;
    int idaggcall = 0;
    for (RelDataTypeField c : fieldList) {
        int type = convertToHerdType(c.getType());
        Column co = Column.column(c.getName(), type);
        columns[i] = co;
        fieldnames[i] = c.getName().toLowerCase();
        i++;
    }
    for (AggregateCall call : calls) {
        aggtypes[idaggcall++] = call.getAggregation().getName();
        argLists.add(call.getArgList());
    }
    PlannerOp input = convertRelNode(op.getInput(), null, returnValues);
    return new AggregateOp(input, fieldnames, columns, aggtypes, argLists, groupedFiledsIndexes);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) AggregateOp(herddb.model.planner.AggregateOp) Column(herddb.model.Column) ArrayList(java.util.ArrayList) ColumnsList(herddb.model.ColumnsList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with PlannerOp

use of herddb.model.planner.PlannerOp 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 3 with PlannerOp

use of herddb.model.planner.PlannerOp 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)

Example 4 with PlannerOp

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

the class CalcitePlanner method planEnumerableThetaJoin.

private PlannerOp planEnumerableThetaJoin(EnumerableThetaJoin op, RelDataType rowType) {
    PlannerOp left = convertRelNode(op.getLeft(), null, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false);
    CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
    boolean generateNullsOnLeft = op.getJoinType().generatesNullsOnLeft();
    boolean generateNullsOnRight = op.getJoinType().generatesNullsOnRight();
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    return new ThetaJoinOp(fieldNames, columns, left, right, condition, generateNullsOnLeft, generateNullsOnRight, false);
}
Also used : ThetaJoinOp(herddb.model.planner.ThetaJoinOp) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 5 with PlannerOp

use of herddb.model.planner.PlannerOp 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);
    }
}
Also used : 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) FilterOp(herddb.model.planner.FilterOp) DeleteStatement(herddb.model.commands.DeleteStatement) Predicate(herddb.model.Predicate) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) DeleteOp(herddb.model.planner.DeleteOp) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Aggregations

PlannerOp (herddb.model.planner.PlannerOp)16 Column (herddb.model.Column)6 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)6 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 Table (herddb.model.Table)4 ScanStatement (herddb.model.commands.ScanStatement)4 ArrayList (java.util.ArrayList)4 RelOptTable (org.apache.calcite.plan.RelOptTable)4 ModifiableTable (org.apache.calcite.schema.ModifiableTable)4 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)4 ScannableTable (org.apache.calcite.schema.ScannableTable)4 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)4 StatementExecutionException (herddb.model.StatementExecutionException)3 FilterOp (herddb.model.planner.FilterOp)3 SemiJoinOp (herddb.model.planner.SemiJoinOp)3 ThetaJoinOp (herddb.model.planner.ThetaJoinOp)3 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)2 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)2 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)2