Search in sources :

Example 11 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.

the class CalcitePlanner method buildProjection.

private Projection buildProjection(final List<RexNode> projects, final RelDataType rowType, final boolean allowIdentity, final Column[] tableSchema) {
    boolean allowZeroCopyProjection = true;
    List<CompiledSQLExpression> fields = new ArrayList<>(projects.size());
    Column[] columns = new Column[projects.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    int[] zeroCopyProjections = new int[fieldNames.length];
    boolean identity = allowIdentity && tableSchema != null && tableSchema.length == fieldNames.length;
    for (RexNode node : projects) {
        CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
        if (exp instanceof AccessCurrentRowExpression) {
            AccessCurrentRowExpression accessCurrentRowExpression = (AccessCurrentRowExpression) exp;
            int mappedIndex = accessCurrentRowExpression.getIndex();
            zeroCopyProjections[i] = mappedIndex;
            if (i != mappedIndex) {
                identity = false;
            }
        } else {
            allowZeroCopyProjection = false;
        }
        fields.add(exp);
        Column col = Column.column(rowType.getFieldNames().get(i).toLowerCase(), convertToHerdType(node.getType()));
        identity = identity && col.name.equals(tableSchema[i].name);
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    if (allowZeroCopyProjection) {
        if (identity) {
            return Projection.IDENTITY(fieldNames, columns);
        }
        return new ProjectOp.ZeroCopyProjection(fieldNames, columns, zeroCopyProjections);
    } else {
        return new ProjectOp.BasicProjection(fieldNames, columns, fields);
    }
}
Also used : ArrayList(java.util.ArrayList) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Column(herddb.model.Column) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression 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);
    }
}
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) ReplaceOp(herddb.model.planner.ReplaceOp) 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) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) 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 13 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.

the class CalcitePlanner method planLimit.

private PlannerOp planLimit(EnumerableLimit op, RelDataType rowType) {
    PlannerOp input = convertRelNode(op.getInput(), rowType, false, false);
    CompiledSQLExpression maxRows = SQLExpressionCompiler.compileExpression(op.fetch);
    CompiledSQLExpression offset = SQLExpressionCompiler.compileExpression(op.offset);
    return new LimitOp(input, maxRows, offset);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) LimitOp(herddb.model.planner.LimitOp)

Example 14 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.

the class CalcitePlanner method planValues.

private PlannerOp planValues(EnumerableValues op) {
    List<List<CompiledSQLExpression>> tuples = new ArrayList<>(op.getTuples().size());
    RelDataType rowType = op.getRowType();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    for (ImmutableList<RexLiteral> tuple : op.getTuples()) {
        List<CompiledSQLExpression> row = new ArrayList<>(tuple.size());
        for (RexLiteral node : tuple) {
            CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
            row.add(exp);
        }
        tuples.add(row);
    }
    int i = 0;
    String[] fieldNames = new String[fieldList.size()];
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName(), convertToHerdType(field.getType()));
        fieldNames[i] = field.getName();
        columns[i++] = col;
    }
    return new ValuesOp(manager.getNodeId(), fieldNames, columns, tuples);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) ValuesOp(herddb.model.planner.ValuesOp) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Column(herddb.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 15 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.

the class CalcitePlanner method planEnumerableNestedLoopJoin.

private PlannerOp planEnumerableNestedLoopJoin(EnumerableNestedLoopJoin op, RelDataType rowType) {
    PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false, false);
    CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
    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 NestedLoopJoinOp(fieldNames, columns, left, right, condition, op.getJoinType(), false);
}
Also used : 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) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

Aggregations

CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)34 Column (herddb.model.Column)17 ArrayList (java.util.ArrayList)16 Table (herddb.model.Table)15 PlannerOp (herddb.model.planner.PlannerOp)15 StatementExecutionException (herddb.model.StatementExecutionException)13 RecordFunction (herddb.model.RecordFunction)9 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)9 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)8 ConstantExpression (herddb.sql.expressions.ConstantExpression)8 TableSpaceManager (herddb.core.TableSpaceManager)7 AccessCurrentRowExpression (herddb.sql.expressions.AccessCurrentRowExpression)7 CompiledMultiAndExpression (herddb.sql.expressions.CompiledMultiAndExpression)7 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)7 AbstractTableManager (herddb.core.AbstractTableManager)6 ScanStatement (herddb.model.commands.ScanStatement)6 Expression (net.sf.jsqlparser.expression.Expression)6 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)6 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)6 RelDataType (org.apache.calcite.rel.type.RelDataType)6