use of herddb.model.RecordFunction in project herddb by diennea.
the class InsertOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
Bytes newValue = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
int index = 0;
List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<String> valuesColumns = new ArrayList<>();
for (Column column : table.getColumns()) {
Object value = row.get(index++);
if (value != null) {
ConstantExpression exp = new ConstantExpression(value);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(exp);
}
valuesColumns.add(column.name);
valuesExpressions.add(exp);
}
}
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);
DMLStatement insertStatement = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues);
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(insertStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
newValue = _result.getNewvalue();
}
if (updateCount > 1 && returnValues) {
if (transactionId > 0) {
// usually the first record will be rolledback with transaction failure
throw new StatementExecutionException("cannot 'return values' on multi-values insert");
} else {
throw new StatementExecutionException("cannot 'return values' on multi-values insert, at least record could have been written because autocommit=true");
}
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.RecordFunction in project herddb by diennea.
the class SQLPlanner method buildInsertStatement.
private ExecutionPlan buildInsertStatement(String defaultTableSpace, Insert s, boolean returnValues) throws StatementExecutionException {
String tableSpace = s.getTable().getSchemaName();
String tableName = s.getTable().getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
ItemsList itemlist = s.getItemsList();
if (itemlist instanceof ExpressionList) {
int index = 0;
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
ExpressionList list = (ExpressionList) itemlist;
if (s.getColumns() != null) {
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
Expression expression;
try {
expression = list.getExpressions().get(index++);
} catch (IndexOutOfBoundsException badQuery) {
throw new StatementExecutionException("bad number of VALUES in INSERT clause");
}
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
} else {
for (Column column : table.columns) {
Expression expression = list.getExpressions().get(index++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
}
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(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
try {
return ExecutionPlan.simple(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues));
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
} else if (itemlist instanceof MultiExpressionList) {
if (returnValues) {
throw new StatementExecutionException("cannot 'return values' on multi-values insert");
}
MultiExpressionList multilist = (MultiExpressionList) itemlist;
List<InsertStatement> inserts = new ArrayList<>();
for (ExpressionList list : multilist.getExprList()) {
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
int index = 0;
if (s.getColumns() != null) {
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
Expression expression;
try {
expression = list.getExpressions().get(index++);
} catch (IndexOutOfBoundsException badQuery) {
throw new StatementExecutionException("bad number of VALUES in INSERT clause");
}
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
} else {
for (Column column : table.columns) {
Expression expression = list.getExpressions().get(index++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
}
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(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
InsertStatement insert = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction);
inserts.add(insert);
}
try {
return ExecutionPlan.multiInsert(inserts);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
} else {
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
Select select = s.getSelect();
ExecutionPlan datasource = buildSelectStatement(defaultTableSpace, select, true, -1);
if (s.getColumns() == null) {
throw new StatementExecutionException("for INSERT ... SELECT you have to declare the columns to be filled in (use INSERT INTO TABLE(c,c,c,) SELECT .....)");
}
IntHolder holder = new IntHolder(1);
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
JdbcParameter readFromResultSetAsJdbcParameter = new JdbcParameter();
readFromResultSetAsJdbcParameter.setIndex(holder.value++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(readFromResultSetAsJdbcParameter);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, readFromResultSetAsJdbcParameter));
}
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(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
try {
return ExecutionPlan.dataManipulationFromSelect(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues), datasource);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
}
use of herddb.model.RecordFunction 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);
}
}
use of herddb.model.RecordFunction in project herddb by diennea.
the class TableManager method executeUpdate.
private StatementExecutionResult executeUpdate(UpdateStatement update, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException, DataStorageManagerException {
AtomicInteger updateCount = new AtomicInteger();
Holder<Bytes> lastKey = new Holder<>();
Holder<byte[]> lastValue = new Holder<>();
/*
an update can succeed only if the row is valid, the key is contains in the "keys" structure
the update will simply override the value of the row, assigning a null page to the row
the update can have a 'where' predicate which is to be evaluated against the decoded row, the update will be executed only if the predicate returns boolean 'true' value (CAS operation)
locks: the update uses a lock on the the key
*/
RecordFunction function = update.getFunction();
long transactionId = transaction != null ? transaction.transactionId : 0;
Predicate predicate = update.getPredicate();
ScanStatement scan = new ScanStatement(table.tablespace, table, predicate);
accessTableData(scan, context, new ScanResultOperation() {
@Override
public void accept(Record actual) throws StatementExecutionException, LogNotAvailableException, DataStorageManagerException {
byte[] newValue = function.computeNewValue(actual, context, tableContext);
final long size = DataPage.estimateEntrySize(actual.key, newValue);
if (size > maxLogicalPageSize) {
throw new RecordTooBigException("New version of record " + actual.key + " is to big to be update: new size " + size + ", actual size " + DataPage.estimateEntrySize(actual) + ", max size " + maxLogicalPageSize);
}
LogEntry entry = LogEntryFactory.update(table, actual.key.data, newValue, transaction);
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
lastKey.value = actual.key;
lastValue.value = newValue;
updateCount.incrementAndGet();
}
}, transaction, true, true);
return new DMLStatementExecutionResult(transactionId, updateCount.get(), lastKey.value, update.isReturnValues() ? (lastValue.value != null ? Bytes.from_array(lastValue.value) : null) : null);
}
use of herddb.model.RecordFunction 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