use of herddb.sql.expressions.JdbcParameterExpression in project herddb by diennea.
the class CalcitePlanner method planInsert.
private PlannerOp planInsert(EnumerableTableModify dml, boolean returnValues) {
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).setReturnValues(returnValues);
}
}
}
}
if (statement != null) {
return new SimpleInsertOp(statement);
}
PlannerOp input = convertRelNode(dml.getInput(), null, false);
try {
return new InsertOp(tableSpace, tableName, input, returnValues);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
Aggregations