Search in sources :

Example 6 with StatementExecutionException

use of herddb.model.StatementExecutionException in project herddb by diennea.

the class SQLPlanner method isAggregateFunction.

private boolean isAggregateFunction(Expression expression) throws StatementExecutionException {
    if (!(expression instanceof Function)) {
        return false;
    }
    Function function = (Function) expression;
    String name = function.getName().toLowerCase();
    if (BuiltinFunctions.isAggregateFunction(function.getName())) {
        return true;
    }
    if (BuiltinFunctions.isScalarFunction(function.getName())) {
        return false;
    }
    throw new StatementExecutionException("unsupported function " + name);
}
Also used : Function(net.sf.jsqlparser.expression.Function) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) StatementExecutionException(herddb.model.StatementExecutionException)

Example 7 with StatementExecutionException

use of herddb.model.StatementExecutionException in project herddb by diennea.

the class SQLPlanner method buildCreateTableStatement.

private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
    String tableSpace = s.getTable().getSchemaName();
    String tableName = s.getTable().getName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    try {
        boolean foundPk = false;
        Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
        Set<String> primaryKey = new HashSet<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                    for (String n : index.getColumnsNames()) {
                        n = n.toLowerCase();
                        tablebuilder.primaryKey(n);
                        primaryKey.add(n);
                        foundPk = true;
                    }
                }
            }
        }
        int position = 0;
        for (ColumnDefinition cf : s.getColumnDefinitions()) {
            String columnName = cf.getColumnName().toLowerCase();
            int type;
            String dataType = cf.getColDataType().getDataType();
            type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList());
            tablebuilder.column(columnName, type, position++);
            if (cf.getColumnSpecStrings() != null) {
                List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecStrings());
                boolean auto_increment = decodeAutoIncrement(columnSpecs);
                if (columnSpecs.contains("PRIMARY")) {
                    foundPk = true;
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                if (auto_increment && primaryKey.contains(cf.getColumnName())) {
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
            }
        }
        if (!foundPk) {
            tablebuilder.column("_pk", ColumnTypes.LONG, position++);
            tablebuilder.primaryKey("_pk", true);
        }
        Table table = tablebuilder.build();
        List<herddb.model.Index> otherIndexes = new ArrayList<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                } else if (index.getType().equalsIgnoreCase("INDEX")) {
                    String indexName = index.getName().toLowerCase();
                    String indexType = convertIndexType(null);
                    herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).type(indexType).uuid(UUID.randomUUID().toString()).table(tableName).tablespace(tableSpace);
                    for (String columnName : index.getColumnsNames()) {
                        columnName = columnName.toLowerCase();
                        Column column = table.getColumn(columnName);
                        if (column == null) {
                            throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
                        }
                        builder.column(column.name, column.type);
                    }
                    otherIndexes.add(builder.build());
                }
            }
        }
        CreateTableStatement statement = new CreateTableStatement(table, otherIndexes);
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
    }
}
Also used : Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTableStatement(herddb.model.commands.CreateTableStatement) ArrayList(java.util.ArrayList) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Column(herddb.model.Column) HashSet(java.util.HashSet)

Example 8 with StatementExecutionException

use of herddb.model.StatementExecutionException in project herddb by diennea.

the class JdbcQueryRewriter method visit.

@Override
public void visit(InExpression inExpression) {
    inExpression.getLeftExpression().accept(this);
    if (inExpression.getLeftItemsList() != null) {
        inExpression.getLeftItemsList().accept(this);
    }
    if (inExpression.getRightItemsList() instanceof SubSelect) {
        SubSelect ss = (SubSelect) inExpression.getRightItemsList();
        if (!(ss.getSelectBody() instanceof PlainSelect)) {
            throw new StatementExecutionException("unsupported operand " + inExpression.getClass() + " with subquery of type " + ss.getClass() + "(" + ss + ")");
        }
        visit(ss);
    }
}
Also used : PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) LateralSubSelect(net.sf.jsqlparser.statement.select.LateralSubSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) StatementExecutionException(herddb.model.StatementExecutionException)

Example 9 with StatementExecutionException

use of herddb.model.StatementExecutionException in project herddb by diennea.

the class ServerSideConnectionPeer method handleRequestTableRestore.

private void handleRequestTableRestore(Message message, Channel _channel) {
    try {
        String tableSpace = (String) message.parameters.get("tableSpace");
        byte[] table = (byte[]) message.parameters.get("table");
        long dumpLedgerId = (long) message.parameters.get("dumpLedgerId");
        long dumpOffset = (long) message.parameters.get("dumpOffset");
        Table tableSchema = Table.deserialize(table);
        tableSchema = Table.builder().cloning(tableSchema).tablespace(tableSpace).build();
        server.getManager().getTableSpaceManager(tableSpace).beginRestoreTable(tableSchema.serialize(), new LogSequenceNumber(dumpLedgerId, dumpOffset));
        _channel.sendReplyMessage(message, Message.ACK(null));
    } catch (StatementExecutionException err) {
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Table(herddb.model.Table) Message(herddb.network.Message) LogSequenceNumber(herddb.log.LogSequenceNumber) StatementExecutionException(herddb.model.StatementExecutionException)

Example 10 with StatementExecutionException

use of herddb.model.StatementExecutionException in project herddb by diennea.

the class SQLExpressionCompiler method compileExpression.

// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
    if (exp instanceof BinaryExpression) {
        CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
        if (compiled != null) {
            return compiled;
        }
    }
    if (exp instanceof net.sf.jsqlparser.schema.Column) {
        return compileColumnExpression(validatedTableAlias, exp);
    } else if (exp instanceof StringValue) {
        return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
    } else if (exp instanceof LongValue) {
        try {
            return new ConstantExpression(((LongValue) exp).getValue());
        } catch (NumberFormatException largeNumber) {
            return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
        }
    } else if (exp instanceof DoubleValue) {
        return new ConstantExpression(((DoubleValue) exp).getValue());
    } else if (exp instanceof TimestampValue) {
        return new ConstantExpression(((TimestampValue) exp).getValue());
    } else if (exp instanceof NullValue) {
        return new ConstantExpression(null);
    } else if (exp instanceof TimeKeyExpression) {
        TimeKeyExpression ext = (TimeKeyExpression) exp;
        if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
            return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
        } else {
            throw new StatementExecutionException("unhandled expression " + exp);
        }
    } else if (exp instanceof JdbcParameter) {
        int index = ((JdbcParameter) exp).getIndex() - 1;
        return new JdbcParameterExpression(index);
    } else if (exp instanceof AndExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
    } else if (exp instanceof OrExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
    } else if (exp instanceof Function) {
        return CompiledFunction.create((Function) exp, validatedTableAlias);
    } else if (exp instanceof Addition) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
    } else if (exp instanceof Subtraction) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
    } else if (exp instanceof Multiplication) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
    } else if (exp instanceof Division) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
    } else if (exp instanceof Parenthesis) {
        Parenthesis p = (Parenthesis) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
        return new CompiledParenthesisExpression(p.isNot(), inner);
    } else if (exp instanceof EqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
    } else if (exp instanceof NotEqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
    } else if (exp instanceof MinorThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
    } else if (exp instanceof MinorThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
    } else if (exp instanceof GreaterThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
    } else if (exp instanceof GreaterThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
    } else if (exp instanceof LikeExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
    } else if (exp instanceof Between) {
        return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
    } else if (exp instanceof SignedExpression) {
        SignedExpression s = (SignedExpression) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
        return new CompiledSignedExpression(s.getSign(), inner);
    } else if (exp instanceof InExpression) {
        InExpression in = (InExpression) exp;
        return CompiledInExpression.create(in, validatedTableAlias);
    } else if (exp instanceof IsNullExpression) {
        IsNullExpression i = (IsNullExpression) exp;
        CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
        return new CompiledIsNullExpression(i.isNot(), left);
    } else if (exp instanceof CaseExpression) {
        return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
    }
    throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
Also used : Arrays(java.util.Arrays) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) BigDecimal(java.math.BigDecimal) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) RexLiteral(org.apache.calcite.rex.RexLiteral) NullValue(net.sf.jsqlparser.expression.NullValue) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Expression(net.sf.jsqlparser.expression.Expression) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) RexInputRef(org.apache.calcite.rex.RexInputRef) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) List(java.util.List) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) BinaryExpressionBuilder(herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CalcitePlanner(herddb.sql.CalcitePlanner) RexCall(org.apache.calcite.rex.RexCall) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) BuiltinFunctions(herddb.sql.functions.BuiltinFunctions) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) CURRENT_TIMESTAMP(herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) ArrayList(java.util.ArrayList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Function(net.sf.jsqlparser.expression.Function) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) LongValue(net.sf.jsqlparser.expression.LongValue) AbstractMap(java.util.AbstractMap) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) Between(net.sf.jsqlparser.expression.operators.relational.Between) Collections(java.util.Collections) StringValue(net.sf.jsqlparser.expression.StringValue) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) StatementExecutionException(herddb.model.StatementExecutionException) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NullValue(net.sf.jsqlparser.expression.NullValue) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) StringValue(net.sf.jsqlparser.expression.StringValue) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Between(net.sf.jsqlparser.expression.operators.relational.Between) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) Function(net.sf.jsqlparser.expression.Function) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SignedExpression(net.sf.jsqlparser.expression.SignedExpression)

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