Search in sources :

Example 11 with StatementExecutionException

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);
    }
}
Also used : RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException)

Example 12 with StatementExecutionException

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);
}
Also used : SQLExpressionCompiler.compileExpression(herddb.sql.expressions.SQLExpressionCompiler.compileExpression) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException)

Example 13 with StatementExecutionException

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;
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StatementExecutionException(herddb.model.StatementExecutionException)

Example 14 with StatementExecutionException

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);
    }
}
Also used : AbstractMap(java.util.AbstractMap) Entry(java.util.Map.Entry) WhenClause(net.sf.jsqlparser.expression.WhenClause) SQLExpressionCompiler.compileExpression(herddb.sql.expressions.SQLExpressionCompiler.compileExpression) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) Expression(net.sf.jsqlparser.expression.Expression) StatementExecutionException(herddb.model.StatementExecutionException)

Example 15 with StatementExecutionException

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());
        }
    }
}
Also used : Table(herddb.model.Table) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CreateTableStatement(herddb.model.commands.CreateTableStatement) BookKeeper(org.apache.bookkeeper.client.BookKeeper) InsertStatement(herddb.model.commands.InsertStatement) StatementExecutionException(herddb.model.StatementExecutionException) DataScanner(herddb.model.DataScanner) Transaction(herddb.model.Transaction) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) TableSpaceManager(herddb.core.TableSpaceManager) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) Test(org.junit.Test)

Aggregations

StatementExecutionException (herddb.model.StatementExecutionException)163 Table (herddb.model.Table)69 ArrayList (java.util.ArrayList)57 DataScanner (herddb.model.DataScanner)49 TransactionContext (herddb.model.TransactionContext)47 DataStorageManagerException (herddb.storage.DataStorageManagerException)40 List (java.util.List)40 DataScannerException (herddb.model.DataScannerException)39 StatementExecutionResult (herddb.model.StatementExecutionResult)39 Column (herddb.model.Column)37 DataAccessor (herddb.utils.DataAccessor)36 LogNotAvailableException (herddb.log.LogNotAvailableException)35 LogEntry (herddb.log.LogEntry)34 Test (org.junit.Test)34 InsertStatement (herddb.model.commands.InsertStatement)32 Bytes (herddb.utils.Bytes)32 CommitLogResult (herddb.log.CommitLogResult)31 Record (herddb.model.Record)31 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)30 Map (java.util.Map)30