Search in sources :

Example 11 with Function

use of net.sf.jsqlparser.expression.Function in project dbeaver by serge-rider.

the class SQLQueryTransformerCount method tryInjectCount.

private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getText());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            if (select.getHaving() != null) {
                throw new DBException("Can't inject COUNT into query with HAVING clause");
            }
            if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
                throw new DBException("Can't inject COUNT into query with GROUP BY clause");
            }
            Distinct selectDistinct = select.getDistinct();
            if (selectDistinct != null) {
                // Remove distinct
                select.setDistinct(null);
            }
            Function countFunc = new Function();
            countFunc.setName("count");
            if (selectDistinct != null) {
                countFunc.setDistinct(true);
                List<Expression> exprs = new ArrayList<>();
                for (SelectItem item : select.getSelectItems()) {
                    if (item instanceof SelectExpressionItem) {
                        exprs.add(((SelectExpressionItem) item).getExpression());
                    }
                }
                if (!exprs.isEmpty()) {
                    countFunc.setParameters(new ExpressionList(exprs));
                }
            }
            countFunc.setAllColumns(true);
            List<SelectItem> selectItems = new ArrayList<>();
            selectItems.add(new SelectExpressionItem(countFunc));
            select.setSelectItems(selectItems);
            select.setOrderByElements(null);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getText() + "] can't be modified");
        }
    } catch (JSQLParserException e) {
        throw new DBException("Can't transform query to SELECT count(*)", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ArrayList(java.util.ArrayList) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 12 with Function

use of net.sf.jsqlparser.expression.Function in project dbeaver by dbeaver.

the class SQLQueryTransformerCount method tryInjectCount.

private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getText());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            if (select.getHaving() != null) {
                throw new DBException("Can't inject COUNT into query with HAVING clause");
            }
            if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
                throw new DBException("Can't inject COUNT into query with GROUP BY clause");
            }
            Distinct selectDistinct = select.getDistinct();
            if (selectDistinct != null) {
                // Remove distinct
                select.setDistinct(null);
            }
            Function countFunc = new Function();
            countFunc.setName("count");
            if (selectDistinct != null) {
                countFunc.setDistinct(true);
                List<Expression> exprs = new ArrayList<>();
                for (SelectItem item : select.getSelectItems()) {
                    if (item instanceof SelectExpressionItem) {
                        exprs.add(((SelectExpressionItem) item).getExpression());
                    }
                }
                if (!exprs.isEmpty()) {
                    countFunc.setParameters(new ExpressionList(exprs));
                }
            }
            countFunc.setAllColumns(true);
            List<SelectItem> selectItems = new ArrayList<>();
            selectItems.add(new SelectExpressionItem(countFunc));
            select.setSelectItems(selectItems);
            select.setOrderByElements(null);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getText() + "] can't be modified");
        }
    } catch (JSQLParserException e) {
        throw new DBException("Can't transform query to SELECT count(*)", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ArrayList(java.util.ArrayList) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 13 with Function

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

the class SQLParserExpressionCompiler method getAggregateFunctionType.

public static int getAggregateFunctionType(Expression exp, OpSchema tableSchema) {
    if (exp instanceof net.sf.jsqlparser.expression.CastExpression) {
        // SELECT CAST(avg(n) as DOUBLE)
        net.sf.jsqlparser.expression.CastExpression c = (net.sf.jsqlparser.expression.CastExpression) exp;
        return JSQLParserPlanner.sqlDataTypeToColumnType(c.getType());
    }
    Function fn = (Function) exp;
    String functionNameLowercase = fn.getName().toLowerCase();
    switch(functionNameLowercase) {
        case COUNT:
            return ColumnTypes.LONG;
        case SUM:
        case SUM0:
        case AVG:
        case MIN:
        case MAX:
            checkSupported(fn.getParameters().getExpressions().size() == 1);
            final Expression first = fn.getParameters().getExpressions().get(0);
            if (first instanceof net.sf.jsqlparser.expression.CastExpression) {
                // SELECT AVG(CAST(n) as DOUBLE))
                net.sf.jsqlparser.expression.CastExpression c = (net.sf.jsqlparser.expression.CastExpression) first;
                return JSQLParserPlanner.sqlDataTypeToColumnType(c.getType());
            }
            checkSupported(first instanceof net.sf.jsqlparser.schema.Column, first.getClass());
            net.sf.jsqlparser.schema.Column cName = (net.sf.jsqlparser.schema.Column) first;
            String tableAlias = extractTableName(cName);
            ColumnRef col = findColumnInSchema(tableAlias, fixMySqlBackTicks(cName.getColumnName()), tableSchema, new IntHolder());
            checkSupported(col != null);
            // SUM of INTEGERS is an INTEGER (this is what Calcite does, but it is smarter than this)
            return col.type;
        default:
            throw new HerdDBInternalException(functionNameLowercase);
    }
}
Also used : HerdDBInternalException(herddb.core.HerdDBInternalException) Function(net.sf.jsqlparser.expression.Function) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) IntHolder(herddb.utils.IntHolder)

Aggregations

Function (net.sf.jsqlparser.expression.Function)13 Expression (net.sf.jsqlparser.expression.Expression)10 ArrayList (java.util.ArrayList)7 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)6 StatementExecutionException (herddb.model.StatementExecutionException)5 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)4 Statement (net.sf.jsqlparser.statement.Statement)4 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)4 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)4 IntHolder (herddb.utils.IntHolder)3 List (java.util.List)3 JSQLParserException (net.sf.jsqlparser.JSQLParserException)3 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)3 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)3 TimeKeyExpression (net.sf.jsqlparser.expression.TimeKeyExpression)3 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)3 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)3 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)3 IsNullExpression (net.sf.jsqlparser.expression.operators.relational.IsNullExpression)3 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)3