use of herddb.model.StatementExecutionException in project herddb by diennea.
the class SQLExpressionCompiler method compileColumnExpression.
private static CompiledSQLExpression compileColumnExpression(String validatedTableAlias, Expression exp) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) exp;
if (validatedTableAlias != null) {
if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(validatedTableAlias)) {
throw new StatementExecutionException("invalid column name " + c.getColumnName() + " invalid table name " + c.getTable().getName() + ", expecting " + validatedTableAlias);
}
}
String columnName = c.getColumnName();
if (BuiltinFunctions.BOOLEAN_TRUE.equalsIgnoreCase(columnName)) {
return new ConstantExpression(Boolean.TRUE);
} else if (BuiltinFunctions.BOOLEAN_FALSE.equalsIgnoreCase(columnName)) {
return new ConstantExpression(Boolean.FALSE);
} else {
return new ColumnExpression(columnName);
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class CompiledFunction method create.
public static CompiledFunction create(Function f, String validatedTableAlias) {
String name = f.getName();
List<Expression> params = null;
if (f.getParameters() != null) {
params = f.getParameters().getExpressions();
}
switch(name) {
case BuiltinFunctions.COUNT:
case BuiltinFunctions.SUM:
case BuiltinFunctions.MIN:
case BuiltinFunctions.MAX:
// AGGREGATED FUNCTION
return new CompiledFunction(name, null);
case BuiltinFunctions.LOWER:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.UPPER:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.ABS:
{
if (params == null || params.size() != 1) {
throw new StatementExecutionException("function " + name + " must have one parameter");
}
break;
}
case BuiltinFunctions.ROUND:
{
if (params == null || (params.size() != 1 && params.size() != 2)) {
throw new StatementExecutionException("function " + name + " must have one or two parameters");
}
break;
}
default:
throw new StatementExecutionException("unhandled function " + name);
}
List<CompiledSQLExpression> compiledParams = new ArrayList<>();
for (Expression exp : f.getParameters().getExpressions()) {
compiledParams.add(compileExpression(validatedTableAlias, exp));
}
return new CompiledFunction(name, compiledParams);
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class CompiledInExpression method evaluate.
@Override
public Object evaluate(herddb.utils.DataAccessor bean, StatementEvaluationContext context) throws StatementExecutionException {
Object leftValue = left.evaluate(bean, context);
boolean res = false;
if (inExpressions != null) {
for (CompiledSQLExpression exp : inExpressions) {
Object expValue = exp.evaluate(bean, context);
if (objectEquals(leftValue, expValue)) {
res = true;
break;
}
}
} else if (inSubSelectPlain != null) {
List<DataAccessor> subQueryResult = context.executeSubquery(inSubSelectPlain);
for (DataAccessor t : subQueryResult) {
String[] fieldNames = t.getFieldNames();
if (fieldNames.length > 1) {
throw new StatementExecutionException("subquery returned more than one column");
}
Object tuple_value = t.get(fieldNames[0]);
if (objectEquals(leftValue, tuple_value)) {
res = true;
break;
}
}
} else {
throw new StatementExecutionException("Internal error");
}
if (not) {
return !res;
} else {
return res;
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class CompiledCaseExpression method create.
public static CompiledCaseExpression create(String validatedTableAlias, CaseExpression caseExpression) {
Expression switchExpression = caseExpression.getSwitchExpression();
if (switchExpression != null) {
throw new StatementExecutionException("unhandled expression CASE SWITCH, type " + caseExpression.getClass() + ": " + caseExpression);
}
List<Entry<CompiledSQLExpression, CompiledSQLExpression>> whens = null;
if (caseExpression.getWhenClauses() != null) {
whens = new ArrayList<>();
for (Expression when : caseExpression.getWhenClauses()) {
WhenClause whenClause = (WhenClause) when;
CompiledSQLExpression whenCondition = compileExpression(validatedTableAlias, whenClause.getWhenExpression());
if (whenCondition == null) {
return null;
}
CompiledSQLExpression thenExpr = compileExpression(validatedTableAlias, whenClause.getThenExpression());
whens.add(new AbstractMap.SimpleImmutableEntry<>(whenCondition, thenExpr));
}
}
Expression elseExp = caseExpression.getElseExpression();
if (elseExp != null) {
CompiledSQLExpression elseExpression = compileExpression(validatedTableAlias, elseExp);
if (elseExpression == null) {
return null;
}
return new CompiledCaseExpression(whens, elseExpression);
} else {
return new CompiledCaseExpression(whens, null);
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class BookkeeperFailuresTest method testBookieNotAvailableDuringTransaction.
@Test
public void testBookieNotAvailableDuringTransaction() throws Exception {
ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
try (Server server = new Server(serverconfig_1)) {
server.start();
server.waitForStandaloneBoot();
Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
// create table is done out of the transaction (this is very like autocommit=true)
server.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
StatementExecutionResult executeStatement = server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long transactionId = executeStatement.transactionId;
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
BookkeeperCommitLog log = (BookkeeperCommitLog) tableSpaceManager.getLog();
long ledgerId = log.getLastSequenceNumber().ledgerId;
assertTrue(ledgerId >= 1);
Transaction transaction = tableSpaceManager.getTransactions().stream().filter(t -> t.transactionId == transactionId).findFirst().get();
// Transaction will synch, so every addEntry will be acked, but will not be "confirmed" yet
transaction.synch();
try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList(), new TransactionContext(transactionId))) {
assertEquals(3, scan.consume().size());
}
try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
// no record, but the table exists!
assertEquals(0, scan.consume().size());
}
// we do not want auto-recovery
server.getManager().setActivatorPauseStatus(true);
testEnv.stopBookie();
// transaction will continue and see the failure only the time of the commit
try {
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
// this will piggyback the LAC for the transaction
System.out.println("Insert of c,4 OK");
} catch (StatementExecutionException expected) {
System.out.println("Insert of c,4 failed " + expected);
// in can happen that the log gets closed
assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
}
try {
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
// this will piggyback the LAC for the transaction
System.out.println("Insert of c,5 OK");
} catch (StatementExecutionException expected) {
System.out.println("Insert of c,5 failed " + expected);
// in can happen that the log gets closed
assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
}
try {
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 6)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
// this will piggyback the LAC for the transaction
System.out.println("Insert of c,6 OK");
} catch (StatementExecutionException expected) {
System.out.println("Insert of c,6 failed " + expected);
// in can happen that the log gets closed
assertEquals(herddb.log.LogNotAvailableException.class, expected.getCause().getClass());
}
try {
server.getManager().executeStatement(new CommitTransactionStatement(TableSpace.DEFAULT, transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// this will fail alweays
fail();
} catch (StatementExecutionException expected) {
System.out.println("Commit failed as expected:" + expected);
}
testEnv.startBookie(false);
while (true) {
System.out.println("status leader:" + tableSpaceManager.isLeader() + " failed:" + tableSpaceManager.isFailed());
if (tableSpaceManager.isFailed()) {
break;
}
Thread.sleep(100);
}
try (BookKeeper bk = createBookKeeper();
LedgerHandle handle = bk.openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, "herddb".getBytes(StandardCharsets.UTF_8))) {
BookKeeperAdmin admin = new BookKeeperAdmin(bk);
try {
LedgerMetadata ledgerMetadata = admin.getLedgerMetadata(handle);
System.out.println("current ledger metadata before recovery: " + ledgerMetadata);
} finally {
admin.close();
}
}
server.getManager().setActivatorPauseStatus(false);
server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
while (true) {
TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
System.out.println("tableSpaceManager_after_failure:" + tableSpaceManager_after_failure);
System.out.println("tableSpaceManager:" + tableSpaceManager);
if (tableSpaceManager_after_failure != null && tableSpaceManager_after_failure != tableSpaceManager) {
break;
}
Thread.sleep(1000);
server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
}
TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
Assert.assertNotNull(tableSpaceManager_after_failure);
assertNotSame(tableSpaceManager_after_failure, tableSpaceManager);
assertTrue(!tableSpaceManager_after_failure.isFailed());
// the insert should succeed because the trasaction has been rolledback automatically
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
}
}
Aggregations