Search in sources :

Example 11 with Expression

use of net.sf.jsqlparser.expression.Expression 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)

Example 12 with Expression

use of net.sf.jsqlparser.expression.Expression 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 Expression

use of net.sf.jsqlparser.expression.Expression 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 14 with Expression

use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.

the class JSQLParserPlanner method buildUpdateStatement.

private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update update, boolean returnValues) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table table = update.getTable();
    // no alias for UPDATE!
    checkSupported(table.getAlias() == null);
    OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
    Table tableImpl = tableManager.getTable();
    checkSupported(update.getSelect() == null);
    checkSupported(update.getJoins() == null);
    checkSupported(update.getOrderByElements() == null);
    checkSupported(update.getReturningExpressionList() == null);
    checkSupported(update.getStartJoins() == null || update.getStartJoins().isEmpty());
    List<Expression> projects = update.getExpressions();
    List<CompiledSQLExpression> expressions = new ArrayList<>(projects.size());
    int index = 0;
    List<String> updateColumnList = new ArrayList<>(projects.size());
    for (net.sf.jsqlparser.schema.Column column : update.getColumns()) {
        checkSupported(column.getTable() == null);
        String columnName = fixMySqlBackTicks(column.getColumnName().toLowerCase());
        checkSupported(!tableImpl.isPrimaryKeyColumn(columnName));
        updateColumnList.add(columnName);
        CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(projects.get(index), tableSchema);
        expressions.add(exp);
        index++;
    }
    RecordFunction function = new SQLRecordFunction(updateColumnList, tableImpl, expressions);
    Predicate where = null;
    if (update.getWhere() != null) {
        CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(update.getWhere(), tableSchema);
        if (whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, update, tableSpaceManager);
            where = sqlWhere;
        }
    }
    PlannerOp op = new SimpleUpdateOp(new UpdateStatement(tableSchema.tableSpace, tableSchema.name, null, function, where).setReturnValues(returnValues));
    return optimizePlan(op);
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) AbstractTableManager(herddb.core.AbstractTableManager) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) TableSpaceManager(herddb.core.TableSpaceManager) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) OpSchema(herddb.sql.expressions.OpSchema) SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp)

Example 15 with Expression

use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.

the class JSQLParserPlanner method planAggregate.

private PlannerOp planAggregate(List<SelectExpressionItem> fieldList, OpSchema inputSchema, PlannerOp input, OpSchema originalTableSchema, GroupByElement groupBy) {
    List<Column> outputSchemaKeysInGroupBy = new ArrayList<>();
    List<String> fieldnamesKeysInGroupBy = new ArrayList<>();
    List<Column> outputSchemaAggregationResults = new ArrayList<>();
    List<String> fieldnamesAggregationResults = new ArrayList<>();
    List<Integer> projectionAggregationResults = new ArrayList<>();
    List<List<Integer>> argLists = new ArrayList<>();
    List<String> aggtypes = new ArrayList<>();
    List<Column> originalOutputSchema = new ArrayList<>();
    List<String> originalFieldNames = new ArrayList<>();
    Set<String> nonAggregateColumns = new HashSet<>();
    int k = 0;
    for (SelectExpressionItem sel : fieldList) {
        Alias alias = sel.getAlias();
        String fieldName = null;
        if (alias != null) {
            checkSupported(alias.getAliasColumns() == null);
            fieldName = fixMySqlBackTicks(alias.getName().toLowerCase());
        }
        Expression exp = sel.getExpression();
        Function fn = SQLParserExpressionCompiler.detectAggregatedFunction(exp);
        if (fn != null) {
            int type = SQLParserExpressionCompiler.getAggregateFunctionType(exp, inputSchema);
            ColumnRef additionalColumn = SQLParserExpressionCompiler.getAggregateFunctionArgument(fn, inputSchema);
            aggtypes.add(fixMySqlBackTicks(fn.getName().toLowerCase()));
            if (additionalColumn != null) {
                // SELECT min(col) -> we have to add "col" to the scan
                IntHolder pos = new IntHolder();
                findColumnInSchema(additionalColumn.tableName, fixMySqlBackTicks(additionalColumn.name), originalTableSchema, pos);
                checkSupported(pos.value >= 0);
                argLists.add(Collections.singletonList(pos.value));
            } else {
                argLists.add(Collections.emptyList());
            }
            if (fieldName == null) {
                fieldName = "expr$" + k;
            }
            Column col = Column.column(fieldName, type);
            outputSchemaAggregationResults.add(col);
            originalFieldNames.add(fieldName);
            originalOutputSchema.add(col);
            fieldnamesAggregationResults.add(fieldName);
            projectionAggregationResults.add(k);
        } else if (exp instanceof net.sf.jsqlparser.schema.Column) {
            net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
            String tableAlias = extractTableName(colRef);
            ColumnRef colInSchema = findColumnInSchema(tableAlias, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, new IntHolder());
            checkSupported(colInSchema != null);
            if (fieldName == null) {
                fieldName = colInSchema.name;
            }
            Column col = Column.column(fieldName, colInSchema.type);
            originalFieldNames.add(fieldName);
            originalOutputSchema.add(col);
            nonAggregateColumns.add(fieldName);
        } else {
            checkSupported(false);
        }
        k++;
    }
    // GROUP BY
    List<Integer> groupedFieldsIndexes = new ArrayList<>();
    List<Integer> projectionForGroupByFields = new ArrayList<>();
    if (groupBy != null) {
        checkSupported(groupBy.getGroupingSets() == null || groupBy.getGroupingSets().isEmpty());
        int posInGroupBy = 0;
        for (Expression exp : groupBy.getGroupByExpressions()) {
            if (exp instanceof net.sf.jsqlparser.schema.Column) {
                net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
                String tableName = extractTableName(colRef);
                IntHolder pos = new IntHolder();
                ColumnRef colInSchema = findColumnInSchema(tableName, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, pos);
                checkSupported(colInSchema != null);
                groupedFieldsIndexes.add(pos.value);
                fieldnamesKeysInGroupBy.add(fixMySqlBackTicks(colRef.getColumnName()));
                outputSchemaKeysInGroupBy.add(colInSchema.toColumn());
                projectionForGroupByFields.add(posInGroupBy);
            } else {
                checkSupported(false);
            }
            posInGroupBy++;
        }
    } else {
        // -> column MUST be in GROUP BY !
        for (String s : nonAggregateColumns) {
            throw new StatementExecutionException("field " + s + " MUST appear in GROUP BY clause");
        }
    }
    PlannerOp op;
    if (!groupedFieldsIndexes.isEmpty()) {
        // this is tricky,
        // the AggregateOp always create a result in the form of
        // FIELD1,FIELD2......AGG1,AGG2
        // Basically it puts all of the results of the aggregation to the right
        // So we have to add a projection that makes the resultset appear
        // as it is defined in the SELECT clause
        List<Column> outputSchema = new ArrayList<>();
        outputSchema.addAll(outputSchemaKeysInGroupBy);
        outputSchema.addAll(outputSchemaAggregationResults);
        Column[] outputSchemaArray = outputSchema.toArray(new Column[0]);
        List<String> aggreateFieldNames = new ArrayList<>();
        aggreateFieldNames.addAll(fieldnamesKeysInGroupBy);
        aggreateFieldNames.addAll(fieldnamesAggregationResults);
        op = new AggregateOp(input, aggreateFieldNames.toArray(new String[0]), outputSchemaArray, aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
        String[] reodereded = originalFieldNames.toArray(new String[0]);
        int[] projections = new int[originalFieldNames.size()];
        int i = 0;
        for (int pos : projectionForGroupByFields) {
            projections[pos] = i++;
        }
        for (int pos : projectionAggregationResults) {
            projections[pos] = i++;
        }
        ProjectOp.ZeroCopyProjection projection = new ProjectOp.ZeroCopyProjection(reodereded, originalOutputSchema.toArray(new Column[0]), projections);
        op = new ProjectOp(projection, op);
    } else {
        // no "GROUP BY", so no need for an additional projection
        // this is the SELECT COUNT(*) FROM TABLE case
        op = new AggregateOp(input, originalFieldNames.toArray(new String[0]), originalOutputSchema.toArray(new Column[0]), aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
    }
    return op;
}
Also used : SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) Function(net.sf.jsqlparser.expression.Function) CompiledFunction(herddb.sql.expressions.CompiledFunction) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) Column(herddb.model.Column) IntHolder(herddb.utils.IntHolder) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) HashSet(java.util.HashSet) PlannerOp(herddb.model.planner.PlannerOp) ProjectOp(herddb.model.planner.ProjectOp) AggregateOp(herddb.model.planner.AggregateOp) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) Alias(net.sf.jsqlparser.expression.Alias) ColumnRef(herddb.sql.expressions.ColumnRef)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)100 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)33 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)30 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)28 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)26 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)21 Column (net.sf.jsqlparser.schema.Column)19 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)17 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)17 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)15 StatementExecutionException (herddb.model.StatementExecutionException)14 NotExpression (net.sf.jsqlparser.expression.NotExpression)14 Table (net.sf.jsqlparser.schema.Table)14 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)12 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)12 Column (herddb.model.Column)11 TimeKeyExpression (net.sf.jsqlparser.expression.TimeKeyExpression)11 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)11 Function (net.sf.jsqlparser.expression.Function)10