use of herddb.model.DataScannerException 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.DataScannerException in project herddb by diennea.
the class ServerSideConnectionPeer method handleOpenScanner.
private void handleOpenScanner(Message message, Channel _channel) {
String tableSpace = (String) message.parameters.get("tableSpace");
Long tx = (Long) message.parameters.get("tx");
long txId = tx != null ? tx : 0;
String query = (String) message.parameters.get("query");
String scannerId = (String) message.parameters.get("scannerId");
int fetchSize = 10;
if (message.parameters.containsKey("fetchSize")) {
fetchSize = (Integer) message.parameters.get("fetchSize");
}
int maxRows = 0;
if (message.parameters.containsKey("maxRows")) {
maxRows = (Integer) message.parameters.get("maxRows");
}
List<Object> parameters = (List<Object>) message.parameters.get("params");
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "openScanner txId+" + txId + ", fetchSize " + fetchSize + ", maxRows " + maxRows + "," + query + " with " + parameters);
}
try {
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, true, true, false, maxRows);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, query + " -> " + translatedQuery.plan.mainStatement);
}
TransactionContext transactionContext = new TransactionContext(txId);
if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement || translatedQuery.plan.joinStatements != null) {
ScanResult scanResult = (ScanResult) server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
DataScanner dataScanner = scanResult.dataScanner;
ServerSideScannerPeer scanner = new ServerSideScannerPeer(dataScanner);
String[] columns = dataScanner.getFieldNames();
List<DataAccessor> records = dataScanner.consume(fetchSize);
TuplesList tuplesList = new TuplesList(columns, records);
boolean last = dataScanner.isFinished();
LOGGER.log(Level.FINEST, "sending first {0} records to scanner {1} query {2}", new Object[] { records.size(), scannerId, query });
if (!last) {
scanners.put(scannerId, scanner);
}
_channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unsupported query type for scan " + query + ": PLAN is " + translatedQuery.plan)));
}
} catch (DataScannerException | RuntimeException err) {
LOGGER.log(Level.SEVERE, "error on scanner " + scannerId + ": " + err, err);
scanners.remove(scannerId);
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class DBManager method executeDataScannerPlan.
private StatementExecutionResult executeDataScannerPlan(ExecutionPlan plan, DataScanner result, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
ScanResult scanResult;
if (plan.mainAggregator != null) {
scanResult = new ScanResult(transactionContext.transactionId, plan.mainAggregator.aggregate(result, context));
} else {
scanResult = new ScanResult(transactionContext.transactionId, result);
}
if (plan.comparator != null) {
// SORT is to be applied before limits
MaterializedRecordSet sortedSet = recordSetFactory.createRecordSet(scanResult.dataScanner.getFieldNames(), scanResult.dataScanner.getSchema());
try {
scanResult.dataScanner.forEach(sortedSet::add);
sortedSet.writeFinished();
sortedSet.sort(plan.comparator);
scanResult.dataScanner.close();
scanResult = new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, sortedSet));
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
if (plan.limits != null) {
try {
return new ScanResult(transactionContext.transactionId, new LimitedDataScanner(scanResult.dataScanner, plan.limits, context));
} catch (DataScannerException limitError) {
throw new StatementExecutionException(limitError);
}
} else {
return scanResult;
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class DBManager method executePlan.
public StatementExecutionResult executePlan(ExecutionPlan plan, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
context.setManager(this);
plan.validateContext(context);
if (plan.mainStatement instanceof ScanStatement) {
DataScanner result = scan((ScanStatement) plan.mainStatement, context, transactionContext);
// transction can be auto generated during the scan
transactionContext = new TransactionContext(result.transactionId);
return executeDataScannerPlan(plan, result, context, transactionContext);
} else if (plan.dataSource != null) {
// INSERT from SELECT
try {
ScanResult data = (ScanResult) executePlan(plan.dataSource, context, transactionContext);
int insertCount = 0;
try {
// transction can be auto generated during the scan
transactionContext = new TransactionContext(data.transactionId);
while (data.dataScanner.hasNext()) {
DataAccessor tuple = data.dataScanner.next();
SQLStatementEvaluationContext tmp_context = new SQLStatementEvaluationContext("--", Arrays.asList(tuple.getValues()));
DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(plan.mainStatement, tmp_context, transactionContext);
insertCount += res.getUpdateCount();
}
} finally {
data.dataScanner.close();
}
return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
} else if (plan.joinStatements != null) {
List<DataScanner> scanResults = new ArrayList<>();
for (ScanStatement statement : plan.joinStatements) {
DataScanner result = scan(statement, context, transactionContext);
// transction can be auto generated during the scan
transactionContext = new TransactionContext(result.transactionId);
scanResults.add(result);
}
return executeJoinedScansPlan(scanResults, context, transactionContext, plan);
} else if (plan.insertStatements != null) {
int insertCount = 0;
for (InsertStatement insert : plan.insertStatements) {
DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(insert, context, transactionContext);
// transction can be auto generated during the loop
transactionContext = new TransactionContext(res.transactionId);
insertCount += res.getUpdateCount();
}
return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
} else {
return executeStatement(plan.mainStatement, context, transactionContext);
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class TableManager method checkForeignKeyConstraintsAsChildTable.
private void checkForeignKeyConstraintsAsChildTable(ForeignKeyDef fk, DataAccessor values, StatementEvaluationContext context, Transaction transaction) throws StatementExecutionException {
// We are creating a SQL query and then using DBManager
// using an SQL query will let us leverage the SQL Planner
// and use the best index to perform the execution
// the SQL Planner will cache the plan, and the plan will also be
// invalidated consistently during DML operations.
String query = childForeignKeyQueries.computeIfAbsent(fk.name, (l -> {
Table parentTable = tableSpaceManager.getTableManagerByUUID(fk.parentTableId).getTable();
// with '*' we are not going to perform projections or copies
StringBuilder q = new StringBuilder("SELECT * FROM ");
q.append(delimit(parentTable.tablespace));
q.append(".");
q.append(delimit(parentTable.name));
q.append(" WHERE ");
for (int i = 0; i < fk.parentTableColumns.length; i++) {
if (i > 0) {
q.append(" AND ");
}
q.append(delimit(fk.parentTableColumns[i]));
q.append("=?");
}
return q.toString();
}));
final List<Object> valuesToMatch = new ArrayList<>(fk.columns.length);
boolean allNulls = true;
for (int i = 0; i < fk.columns.length; i++) {
Object value = values.get(fk.columns[i]);
allNulls = allNulls && value == null;
valuesToMatch.add(value);
}
if (allNulls) {
// all of the values are null, so no check on the parent table
return;
}
TransactionContext tx = transaction != null ? new TransactionContext(transaction.transactionId) : TransactionContext.NO_TRANSACTION;
boolean fkOk;
try (DataScanner scan = tableSpaceManager.getDbmanager().executeSimpleQuery(tableSpaceManager.getTableSpaceName(), query, valuesToMatch, // only one record
1, // keep read locks in TransactionContext
true, tx, null)) {
List<DataAccessor> resultSet = scan.consume();
fkOk = !resultSet.isEmpty();
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
if (!fkOk) {
throw new ForeignKeyViolationException(fk.name, "foreignKey " + table.name + "." + fk.name + " violated");
}
}
Aggregations