Search in sources :

Example 1 with InsertOp

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

the class CalcitePlanner method planInsert.

private PlannerOp planInsert(EnumerableTableModify dml, boolean returnValues, boolean upsert) {
    final String tableSpace = dml.getTable().getQualifiedName().get(0);
    final String tableName = dml.getTable().getQualifiedName().get(1);
    DMLStatement statement = null;
    if (dml.getInput() instanceof EnumerableProject) {
        // fastest path for insert into TABLE(s,b,c) values(?,?,?)
        EnumerableProject project = (EnumerableProject) dml.getInput();
        if (project.getInput() instanceof EnumerableValues) {
            EnumerableValues values = (EnumerableValues) project.getInput();
            if (values.getTuples().size() == 1) {
                final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
                Table table = tableImpl.tableManager.getTable();
                int index = 0;
                List<RexNode> projects = project.getProjects();
                List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
                List<String> keyExpressionToColumn = new ArrayList<>();
                List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
                List<String> valuesColumns = new ArrayList<>();
                boolean invalid = false;
                for (Column column : table.getColumns()) {
                    CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(projects.get(index));
                    if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression) {
                        boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
                        if (!isAlwaysNull) {
                            if (table.isPrimaryKeyColumn(column.name)) {
                                keyExpressionToColumn.add(column.name);
                                keyValueExpression.add(exp);
                            }
                            valuesColumns.add(column.name);
                            valuesExpressions.add(exp);
                        }
                        index++;
                    } else {
                        invalid = true;
                        break;
                    }
                }
                if (!invalid) {
                    RecordFunction keyfunction;
                    if (keyValueExpression.isEmpty() && table.auto_increment) {
                        keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
                    } else {
                        if (keyValueExpression.size() != table.primaryKey.length) {
                            throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                        }
                        keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, table);
                    }
                    RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, table, valuesExpressions);
                    statement = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction, upsert).setReturnValues(returnValues);
                }
            }
        }
    }
    if (statement != null) {
        return new SimpleInsertOp(statement);
    }
    PlannerOp input = convertRelNode(dml.getInput(), null, false, false);
    try {
        return new InsertOp(tableSpace, tableName, input, returnValues, upsert);
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) Column(herddb.model.Column) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) 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) PlannerOp(herddb.model.planner.PlannerOp) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) EnumerableProject(org.apache.calcite.adapter.enumerable.EnumerableProject) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) EnumerableValues(org.apache.calcite.adapter.enumerable.EnumerableValues) DMLStatement(herddb.model.DMLStatement) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) RexNode(org.apache.calcite.rex.RexNode) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) InsertOp(herddb.model.planner.InsertOp)

Example 2 with InsertOp

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

the class JSQLParserPlanner method planerInsertOrUpsert.

private ExecutionPlan planerInsertOrUpsert(String defaultTableSpace, net.sf.jsqlparser.schema.Table table, List<net.sf.jsqlparser.schema.Column> columns, ItemsList itemsList, boolean returnValues, boolean upsert) throws StatementExecutionException, StatementNotSupportedException {
    OpSchema inputSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(inputSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(inputSchema.name);
    Table tableImpl = tableManager.getTable();
    List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
    List<String> keyExpressionToColumn = new ArrayList<>();
    List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
    List<String> valuesColumns = new ArrayList<>();
    boolean invalid = false;
    int index = 0;
    if (columns == null) {
        // INSERT INTO TABLE VALUES (xxxx) (no column list)
        columns = new ArrayList<>();
        for (Column c : tableImpl.getColumns()) {
            columns.add(new net.sf.jsqlparser.schema.Column(c.name));
        }
    }
    if (itemsList instanceof ExpressionList) {
        List<Expression> values = ((ExpressionList) itemsList).getExpressions();
        for (net.sf.jsqlparser.schema.Column column : columns) {
            CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(values.get(index), inputSchema);
            String columnName = fixMySqlBackTicks(column.getColumnName()).toLowerCase();
            if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression || exp instanceof CompiledFunction) {
                boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
                if (!isAlwaysNull) {
                    if (tableImpl.isPrimaryKeyColumn(columnName)) {
                        keyExpressionToColumn.add(columnName);
                        keyValueExpression.add(exp);
                    }
                    Column columnFromSchema = tableImpl.getColumn(columnName);
                    if (columnFromSchema == null) {
                        throw new StatementExecutionException("Column '" + columnName + "' not found in table " + tableImpl.name);
                    }
                    valuesColumns.add(columnFromSchema.name);
                    valuesExpressions.add(exp);
                }
                index++;
            } else {
                checkSupported(false, "Unsupported expression type " + exp.getClass().getName());
                break;
            }
        }
        // handle default values
        for (Column col : tableImpl.getColumns()) {
            if (!valuesColumns.contains(col.name)) {
                if (col.defaultValue != null) {
                    valuesColumns.add(col.name);
                    CompiledSQLExpression defaultValueExpression = makeDefaultValue(col);
                    valuesExpressions.add(defaultValueExpression);
                } else if (ColumnTypes.isNotNullDataType(col.type) && !tableImpl.auto_increment) {
                    throw new StatementExecutionException("Column '" + col.name + "' has no default value and does not allow NULLs");
                }
            }
        }
        DMLStatement statement;
        if (!invalid) {
            RecordFunction keyfunction;
            if (keyValueExpression.isEmpty() && tableImpl.auto_increment) {
                keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
            } else {
                if (keyValueExpression.size() != tableImpl.primaryKey.length) {
                    throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                }
                keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, tableImpl);
            }
            RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, tableImpl, valuesExpressions);
            statement = new InsertStatement(inputSchema.tableSpace, inputSchema.name, keyfunction, valuesfunction, upsert).setReturnValues(returnValues);
        } else {
            throw new StatementNotSupportedException();
        }
        PlannerOp op = new SimpleInsertOp(statement.setReturnValues(returnValues));
        return optimizePlan(op);
    } else if (itemsList instanceof MultiExpressionList) {
        List<ExpressionList> records = ((MultiExpressionList) itemsList).getExprList();
        ValuesOp values = planValuesForInsertOp(columns, tableImpl, inputSchema, records);
        InsertOp op = new InsertOp(tableImpl.tablespace, tableImpl.name, values, returnValues, upsert);
        return optimizePlan(op);
    } else {
        checkSupported(false);
        return null;
    }
}
Also used : ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ValuesOp(herddb.model.planner.ValuesOp) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) Column(herddb.model.Column) TableSpaceManager(herddb.core.TableSpaceManager) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) CompiledFunction(herddb.sql.expressions.CompiledFunction) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) 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) DMLStatement(herddb.model.DMLStatement) OpSchema(herddb.sql.expressions.OpSchema) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) InsertOp(herddb.model.planner.InsertOp)

Aggregations

AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)2 Column (herddb.model.Column)2 DMLStatement (herddb.model.DMLStatement)2 RecordFunction (herddb.model.RecordFunction)2 StatementExecutionException (herddb.model.StatementExecutionException)2 Table (herddb.model.Table)2 InsertStatement (herddb.model.commands.InsertStatement)2 InsertOp (herddb.model.planner.InsertOp)2 PlannerOp (herddb.model.planner.PlannerOp)2 SimpleInsertOp (herddb.model.planner.SimpleInsertOp)2 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)2 ConstantExpression (herddb.sql.expressions.ConstantExpression)2 JdbcParameterExpression (herddb.sql.expressions.JdbcParameterExpression)2 TypedJdbcParameterExpression (herddb.sql.expressions.TypedJdbcParameterExpression)2 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)2 ArrayList (java.util.ArrayList)2 AbstractTableManager (herddb.core.AbstractTableManager)1 TableSpaceManager (herddb.core.TableSpaceManager)1 ValuesOp (herddb.model.planner.ValuesOp)1 AccessCurrentRowExpression (herddb.sql.expressions.AccessCurrentRowExpression)1