use of org.hibernate.sql.ast.tree.expression.Distinct in project hibernate-orm by hibernate.
the class ListaggGroupConcatEmulation method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, SqlAstTranslator<?> translator) {
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
sqlAppender.appendSql("group_concat(");
final SqlAstNode firstArg = sqlAstArguments.get(0);
final Expression arg;
if (firstArg instanceof Distinct) {
sqlAppender.appendSql("distinct ");
arg = ((Distinct) firstArg).getExpression();
} else {
arg = (Expression) firstArg;
}
if (caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
sqlAppender.appendSql(" then ");
arg.accept(translator);
sqlAppender.appendSql(" else null end");
translator.getCurrentClauseStack().pop();
} else {
arg.accept(translator);
}
if (withinGroup != null && !withinGroup.isEmpty()) {
translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
sqlAppender.appendSql(" order by ");
withinGroup.get(0).accept(translator);
for (int i = 1; i < withinGroup.size(); i++) {
sqlAppender.appendSql(',');
withinGroup.get(i).accept(translator);
}
translator.getCurrentClauseStack().pop();
}
if (sqlAstArguments.size() != 1) {
SqlAstNode separator = sqlAstArguments.get(1);
// group_concat doesn't support the overflow clause, so we just omit it
if (separator instanceof Overflow) {
separator = ((Overflow) separator).getSeparatorExpression();
}
sqlAppender.appendSql(" separator ");
separator.accept(translator);
}
sqlAppender.appendSql(')');
if (!caseWrapper && filter != null) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
use of org.hibernate.sql.ast.tree.expression.Distinct in project hibernate-orm by hibernate.
the class CountFunction method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, SqlAstTranslator<?> translator) {
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
final SqlAstNode arg = sqlAstArguments.get(0);
sqlAppender.appendSql("count(");
final SqlTuple tuple;
if (arg instanceof Distinct) {
sqlAppender.appendSql("distinct ");
final Expression distinctArg = ((Distinct) arg).getExpression();
if ((tuple = SqlTupleContainer.getSqlTuple(distinctArg)) != null) {
final List<? extends Expression> expressions = tuple.getExpressions();
// Single element tuple
if (expressions.size() == 1) {
renderSimpleArgument(sqlAppender, filter, translator, caseWrapper, expressions.get(0));
} else // Emulate tuple distinct count
if (!dialect.supportsTupleDistinctCounts()) {
// count(distinct coalesce(nullif(coalesce(col1 || '', '\0'), ''), '\01') || '\0' || coalesce(nullif(coalesce(col2 || '', '\0'), ''), '\02'))
if (caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
sqlAppender.appendSql(" then ");
translator.getCurrentClauseStack().pop();
}
if (castDistinctStringConcat) {
sqlAppender.appendSql("cast(");
}
sqlAppender.appendSql("coalesce(nullif(coalesce(");
boolean needsConcat = renderCastedArgument(sqlAppender, translator, expressions.get(0));
int argumentNumber = 1;
for (int i = 1; i < expressions.size(); i++, argumentNumber++) {
if (needsConcat) {
// Concat with empty string to get implicit conversion
sqlAppender.appendSql(concatOperator);
sqlAppender.appendSql("''");
}
sqlAppender.appendSql(",'\\0'),''),'\\0");
sqlAppender.appendSql(argumentNumber);
sqlAppender.appendSql("')");
sqlAppender.appendSql(concatOperator);
sqlAppender.appendSql("'\\0'");
sqlAppender.appendSql(concatOperator);
sqlAppender.appendSql("coalesce(nullif(coalesce(");
needsConcat = renderCastedArgument(sqlAppender, translator, expressions.get(i));
}
if (needsConcat) {
// Concat with empty string to get implicit conversion
sqlAppender.appendSql(concatOperator);
sqlAppender.appendSql("''");
}
sqlAppender.appendSql(",'\\0'),''),'\\0");
sqlAppender.appendSql(argumentNumber);
sqlAppender.appendSql("')");
if (castDistinctStringConcat) {
sqlAppender.appendSql(" as ");
sqlAppender.appendSql(concatArgumentCastType);
sqlAppender.appendSql(')');
}
if (caseWrapper) {
sqlAppender.appendSql(" else null end");
}
} else {
renderTupleCountSupported(sqlAppender, filter, translator, caseWrapper, tuple, expressions, dialect.requiresParensForTupleDistinctCounts());
}
} else {
renderSimpleArgument(sqlAppender, filter, translator, caseWrapper, distinctArg);
}
} else {
if (canReplaceWithStar(arg, translator)) {
renderSimpleArgument(sqlAppender, filter, translator, caseWrapper, Star.INSTANCE);
} else if ((tuple = SqlTupleContainer.getSqlTuple(arg)) != null) {
final List<? extends Expression> expressions = tuple.getExpressions();
// Single element tuple
if (expressions.size() == 1) {
renderSimpleArgument(sqlAppender, filter, translator, caseWrapper, expressions.get(0));
} else // Emulate the tuple count with a case when expression
if (!dialect.supportsTupleCounts()) {
sqlAppender.appendSql("case when ");
if (caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" and ");
}
translator.render(expressions.get(0), defaultArgumentRenderingMode);
sqlAppender.appendSql(" is not null");
for (int i = 1; i < expressions.size(); i++) {
sqlAppender.appendSql(" and ");
translator.render(expressions.get(i), defaultArgumentRenderingMode);
sqlAppender.appendSql(" is not null");
}
sqlAppender.appendSql(" then 1 else null end");
} else // Tuple counts are supported
{
renderTupleCountSupported(sqlAppender, filter, translator, caseWrapper, tuple, expressions, dialect.requiresParensForTupleCounts());
}
} else {
renderSimpleArgument(sqlAppender, filter, translator, caseWrapper, arg);
}
}
sqlAppender.appendSql(')');
if (filter != null && !caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
use of org.hibernate.sql.ast.tree.expression.Distinct in project hibernate-orm by hibernate.
the class AvgFunction method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, SqlAstTranslator<?> translator) {
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
sqlAppender.appendSql("avg(");
final Expression arg;
if (sqlAstArguments.get(0) instanceof Distinct) {
sqlAppender.appendSql("distinct ");
arg = ((Distinct) sqlAstArguments.get(0)).getExpression();
} else {
arg = (Expression) sqlAstArguments.get(0);
}
if (caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
renderArgument(sqlAppender, translator, arg);
sqlAppender.appendSql(" else null end)");
} else {
renderArgument(sqlAppender, translator, arg);
sqlAppender.appendSql(')');
if (filter != null) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
}
use of org.hibernate.sql.ast.tree.expression.Distinct in project hibernate-orm by hibernate.
the class ListaggFunction method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, SqlAstTranslator<?> translator) {
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
sqlAppender.appendSql("listagg(");
final SqlAstNode firstArg = sqlAstArguments.get(0);
final Expression arg;
if (firstArg instanceof Distinct) {
sqlAppender.appendSql("distinct ");
arg = ((Distinct) firstArg).getExpression();
} else {
arg = (Expression) firstArg;
}
if (caseWrapper) {
sqlAppender.appendSql("case when ");
translator.getCurrentClauseStack().push(Clause.WHERE);
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
arg.accept(translator);
sqlAppender.appendSql(" else null end");
} else {
arg.accept(translator);
}
if (sqlAstArguments.size() != 1) {
sqlAppender.appendSql(',');
sqlAstArguments.get(1).accept(translator);
}
sqlAppender.appendSql(')');
if (withinGroup != null && !withinGroup.isEmpty()) {
translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
sqlAppender.appendSql(" within group (order by ");
withinGroup.get(0).accept(translator);
for (int i = 1; i < withinGroup.size(); i++) {
sqlAppender.appendSql(',');
withinGroup.get(i).accept(translator);
}
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
} else if (emptyWithinReplacement != null) {
sqlAppender.appendSql(' ');
sqlAppender.appendSql(emptyWithinReplacement);
}
if (!caseWrapper && filter != null) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
use of org.hibernate.sql.ast.tree.expression.Distinct in project hibernate-orm by hibernate.
the class PatternRenderer method render.
private void render(SqlAppender sqlAppender, List<? extends SqlAstNode> args, Predicate filter, List<SortSpecification> withinGroup, Boolean respectNulls, Boolean fromFirst, SqlAstTranslator<?> translator) {
final int numberOfArguments = args.size();
final boolean caseWrapper = filter != null && !translator.supportsFilterClause();
if (numberOfArguments < maxParamIndex) {
LOG.missingArguments(maxParamIndex, numberOfArguments);
}
for (int i = 0; i < chunks.length; i++) {
if (i == varargParam) {
for (int j = i; j < numberOfArguments; j++) {
final SqlAstNode arg = args.get(j);
if (arg != null) {
sqlAppender.appendSql(chunks[i]);
if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
translator.render(arg, argumentRenderingMode);
sqlAppender.appendSql(" else null end");
} else {
translator.render(arg, argumentRenderingMode);
}
}
}
} else if (i < paramIndexes.length) {
final int index = paramIndexes[i] - 1;
final SqlAstNode arg = index < numberOfArguments ? args.get(index) : null;
if (arg != null || i == 0) {
sqlAppender.appendSql(chunks[i]);
}
if (arg != null) {
if (caseWrapper && !(arg instanceof Distinct) && !(arg instanceof Star)) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql("case when ");
filter.accept(translator);
translator.getCurrentClauseStack().pop();
sqlAppender.appendSql(" then ");
translator.render(arg, argumentRenderingMode);
sqlAppender.appendSql(" else null end");
} else {
translator.render(arg, argumentRenderingMode);
}
}
} else {
sqlAppender.appendSql(chunks[i]);
}
}
if (withinGroup != null && !withinGroup.isEmpty()) {
translator.getCurrentClauseStack().push(Clause.WITHIN_GROUP);
sqlAppender.appendSql(" within group (order by");
translator.render(withinGroup.get(0), argumentRenderingMode);
for (int i = 1; i < withinGroup.size(); i++) {
sqlAppender.appendSql(SqlAppender.COMA_SEPARATOR_CHAR);
translator.render(withinGroup.get(0), argumentRenderingMode);
}
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
if (fromFirst != null) {
if (fromFirst) {
sqlAppender.appendSql(" from first");
} else {
sqlAppender.appendSql(" from last");
}
}
if (respectNulls != null) {
if (respectNulls) {
sqlAppender.appendSql(" respect nulls");
} else {
sqlAppender.appendSql(" ignore nulls");
}
}
if (filter != null && !caseWrapper) {
translator.getCurrentClauseStack().push(Clause.WHERE);
sqlAppender.appendSql(" filter (where ");
filter.accept(translator);
sqlAppender.appendSql(')');
translator.getCurrentClauseStack().pop();
}
}
Aggregations