Search in sources :

Example 6 with CompiledSQLExpression

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

the class JSQLParserPlanner method buildUpdateStatement.

private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update update, boolean returnValues) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table table = update.getTable();
    // no alias for UPDATE!
    checkSupported(table.getAlias() == null);
    OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
    Table tableImpl = tableManager.getTable();
    checkSupported(update.getSelect() == null);
    checkSupported(update.getJoins() == null);
    checkSupported(update.getOrderByElements() == null);
    checkSupported(update.getReturningExpressionList() == null);
    checkSupported(update.getStartJoins() == null || update.getStartJoins().isEmpty());
    List<Expression> projects = update.getExpressions();
    List<CompiledSQLExpression> expressions = new ArrayList<>(projects.size());
    int index = 0;
    List<String> updateColumnList = new ArrayList<>(projects.size());
    for (net.sf.jsqlparser.schema.Column column : update.getColumns()) {
        checkSupported(column.getTable() == null);
        String columnName = fixMySqlBackTicks(column.getColumnName().toLowerCase());
        checkSupported(!tableImpl.isPrimaryKeyColumn(columnName));
        updateColumnList.add(columnName);
        CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(projects.get(index), tableSchema);
        expressions.add(exp);
        index++;
    }
    RecordFunction function = new SQLRecordFunction(updateColumnList, tableImpl, expressions);
    Predicate where = null;
    if (update.getWhere() != null) {
        CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(update.getWhere(), tableSchema);
        if (whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, update, tableSpaceManager);
            where = sqlWhere;
        }
    }
    PlannerOp op = new SimpleUpdateOp(new UpdateStatement(tableSchema.tableSpace, tableSchema.name, null, function, where).setReturnValues(returnValues));
    return optimizePlan(op);
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) AbstractTableManager(herddb.core.AbstractTableManager) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) TableSpaceManager(herddb.core.TableSpaceManager) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) OpSchema(herddb.sql.expressions.OpSchema) SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp)

Example 7 with CompiledSQLExpression

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

the class JSQLParserPlanner method planSort.

private PlannerOp planSort(PlannerOp input, OpSchema columns, List<OrderByElement> fieldCollations) {
    boolean[] directions = new boolean[fieldCollations.size()];
    boolean[] nullLastdirections = new boolean[fieldCollations.size()];
    int[] fields = new int[fieldCollations.size()];
    int i = 0;
    for (OrderByElement col : fieldCollations) {
        OrderByElement.NullOrdering nullDirection = col.getNullOrdering();
        CompiledSQLExpression columnPos = SQLParserExpressionCompiler.compileExpression(col.getExpression(), columns);
        checkSupported(columnPos instanceof AccessCurrentRowExpression);
        AccessCurrentRowExpression pos = (AccessCurrentRowExpression) columnPos;
        int index = pos.getIndex();
        directions[i] = col.isAsc();
        // default is NULL LAST
        nullLastdirections[i] = nullDirection == OrderByElement.NullOrdering.NULLS_LAST || nullDirection == null;
        fields[i++] = index;
    }
    return new SortOp(input, directions, fields, nullLastdirections);
}
Also used : AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) SortOp(herddb.model.planner.SortOp)

Example 8 with CompiledSQLExpression

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

the class JSQLParserPlanner method buildDeleteStatement.

private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete delete) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table table = delete.getTable();
    // no alias for DELETE!
    checkSupported(table.getAlias() == null);
    OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
    Table tableImpl = tableManager.getTable();
    checkSupported(delete.getLimit() == null);
    checkSupported(delete.getJoins() == null);
    checkSupported(delete.getOrderByElements() == null);
    checkSupported(delete.getTables() == null || delete.getTables().isEmpty());
    Predicate where = null;
    if (delete.getWhere() != null) {
        CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(delete.getWhere(), tableSchema);
        if (whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, delete, tableSpaceManager);
            where = sqlWhere;
        }
    }
    PlannerOp op = new SimpleDeleteOp(new DeleteStatement(tableSchema.tableSpace, tableSchema.name, null, where));
    return optimizePlan(op);
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) DeleteStatement(herddb.model.commands.DeleteStatement) Predicate(herddb.model.Predicate) AbstractTableManager(herddb.core.AbstractTableManager) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) TableSpaceManager(herddb.core.TableSpaceManager) OpSchema(herddb.sql.expressions.OpSchema)

Example 9 with CompiledSQLExpression

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

the class JSQLParserPlanner method buildProjection.

private Projection buildProjection(final List<SelectExpressionItem> projects, final boolean allowIdentity, final OpSchema 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.columns.length == fieldNames.length;
    for (SelectExpressionItem node : projects) {
        int type = ColumnTypes.ANYTYPE;
        CompiledSQLExpression exp;
        String alias = null;
        if (node.getAlias() != null) {
            alias = fixMySqlBackTicks(node.getAlias().getName().toLowerCase());
            checkSupported(node.getAlias().getAliasColumns() == null);
        }
        if (node.getExpression() instanceof net.sf.jsqlparser.schema.Column && !isBooleanLiteral((net.sf.jsqlparser.schema.Column) node.getExpression())) {
            net.sf.jsqlparser.schema.Column col = (net.sf.jsqlparser.schema.Column) node.getExpression();
            // checkSupported(col.getTable() == null);
            String columnName = fixMySqlBackTicks(col.getColumnName());
            String tableAlias = extractTableName(col);
            if (alias == null) {
                alias = columnName;
            }
            IntHolder indexInSchema = new IntHolder(-1);
            ColumnRef found = findColumnInSchema(tableAlias, columnName, tableSchema, indexInSchema);
            if (indexInSchema.value == -1 || found == null) {
                String nameInError = tableAlias != null ? tableAlias + "." + columnName : columnName;
                throw new StatementExecutionException("Column " + nameInError + " not found in target table (schema " + tableSchema + ")");
            }
            exp = new AccessCurrentRowExpression(indexInSchema.value, found.type);
            type = found.type;
        } else {
            exp = SQLParserExpressionCompiler.compileExpression(node.getExpression(), tableSchema);
            if (alias == null) {
                alias = "col" + i;
            }
        }
        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(alias, type);
        identity = identity && col.name.equals(tableSchema.columns[i].name);
        fieldNames[i] = alias;
        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 : SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) Column(herddb.model.Column) IntHolder(herddb.utils.IntHolder) ColumnRef(herddb.sql.expressions.ColumnRef)

Example 10 with CompiledSQLExpression

use of herddb.sql.expressions.CompiledSQLExpression 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);
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) 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) ArrayList(java.util.ArrayList) Projection(herddb.model.Projection) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TableSpaceManager(herddb.core.TableSpaceManager) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode) ScanStatement(herddb.model.commands.ScanStatement) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

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