use of net.sf.jsqlparser.expression.Function in project dbeaver by serge-rider.
the class SQLQueryTransformerCount method transformQuery.
@Override
public SQLQuery transformQuery(SQLDataSource dataSource, SQLQuery query) throws DBException {
try {
Statement statement = CCJSqlParserUtil.parse(query.getQuery());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
List<SelectItem> selectItems = new ArrayList<>();
Function countFunc = new Function();
countFunc.setName("count");
countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
SelectItem countItem = new SelectExpressionItem(countFunc);
selectItems.add(countItem);
select.setSelectItems(selectItems);
return new SQLQuery(dataSource, select.toString(), query, false);
} else {
throw new DBException("Query [" + query.getQuery() + "] can't be modified");
}
} catch (JSQLParserException e) {
throw new DBException("Can't transform query to SELECT count(*)", e);
}
}
use of net.sf.jsqlparser.expression.Function in project herddb by diennea.
the class SQLAggregator method createOutputColumns.
private static Column[] createOutputColumns(List<SelectItem> selectItems, DataScanner wrapped) throws StatementExecutionException {
Column[] columns = new Column[selectItems.size()];
int i = 0;
for (SelectItem item : selectItems) {
boolean done = false;
if (item instanceof SelectExpressionItem) {
SelectExpressionItem sei = (SelectExpressionItem) item;
Expression expression = sei.getExpression();
if (expression instanceof Function) {
Function f = (Function) expression;
String fieldName = f.toString();
if (sei.getAlias() != null && sei.getAlias().getName() != null) {
fieldName = sei.getAlias().getName();
}
if (fieldName == null) {
fieldName = "field" + i;
}
Column aggregated = BuiltinFunctions.toAggregatedOutputColumn(fieldName, f);
if (aggregated != null) {
columns[i] = aggregated;
done = true;
}
} else if (expression instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) expression;
String name = c.getColumnName();
boolean found = false;
for (Column co : wrapped.getSchema()) {
if (co.name.equals(name)) {
columns[i] = co;
found = true;
break;
}
}
if (!found) {
throw new StatementExecutionException("cannot find column " + name + " is upstream scanner");
}
done = true;
} else {
throw new StatementExecutionException("unhandled aggregate query selectable item:" + expression);
}
}
i++;
if (!done) {
throw new StatementExecutionException("unhandled aggregate function " + item);
}
}
return columns;
}
use of net.sf.jsqlparser.expression.Function 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);
}
use of net.sf.jsqlparser.expression.Function 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);
}
use of net.sf.jsqlparser.expression.Function in project dbeaver by dbeaver.
the class SQLQueryTransformerCount method tryInjectCount.
private SQLQuery tryInjectCount(SQLDataSource 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 (!CommonUtils.isEmpty(select.getGroupByColumnReferences())) {
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);
}
}
Aggregations