use of org.hibernate.query.sqm.tree.SqmTypedNode in project hibernate-orm by hibernate.
the class SqmCriteriaNodeBuilder method createTrimNode.
private SqmFunction<String> createTrimNode(TrimSpec trimSpecification, SqmExpression<Character> trimCharacter, SqmExpression<String> source) {
if (trimSpecification == null) {
trimSpecification = TrimSpec.BOTH;
}
if (trimCharacter == null) {
trimCharacter = new SqmLiteral<>(' ', getTypeConfiguration().standardBasicTypeForJavaType(Character.class), this);
}
final ArrayList<SqmTypedNode<?>> arguments = new ArrayList<>(3);
arguments.add(new SqmTrimSpecification(trimSpecification, this));
arguments.add(trimCharacter);
arguments.add(source);
return getFunctionDescriptor("trim").generateSqmExpression(arguments, null, getQueryEngine(), getJpaMetamodel().getTypeConfiguration());
}
use of org.hibernate.query.sqm.tree.SqmTypedNode in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitGenericFunctionArguments.
@Override
public List<SqmTypedNode<?>> visitGenericFunctionArguments(HqlParser.GenericFunctionArgumentsContext ctx) {
final int size = ctx.getChildCount();
final int lastIndex = size - 1;
// Shift 1 bit instead of division by 2
final int estimateArgumentCount = size >> 1;
final List<SqmTypedNode<?>> arguments = new ArrayList<>(estimateArgumentCount);
int i = 0;
boolean distinct = false;
final ParseTree firstChild = ctx.getChild(0);
if (firstChild instanceof HqlParser.DatetimeFieldContext) {
arguments.add(toDurationUnit((SqmExtractUnit<?>) firstChild.accept(this)));
i += 2;
} else if (firstChild instanceof TerminalNode) {
distinct = true;
i++;
}
for (; i < size; i += 2) {
// we handle the final argument differently...
if (i == lastIndex) {
arguments.add(visitFinalFunctionArgument(ctx.getChild(i)));
} else {
arguments.add((SqmTypedNode<?>) ctx.getChild(i).accept(this));
}
}
if (distinct) {
final NodeBuilder nodeBuilder = getCreationContext().getNodeBuilder();
if (arguments.size() == 1) {
arguments.set(0, new SqmDistinct<>((SqmExpression<?>) arguments.get(0), nodeBuilder));
} else {
final List<SqmTypedNode<?>> newArguments = new ArrayList<>(1);
// noinspection unchecked
newArguments.add(new SqmDistinct<>(new SqmTuple<>((List<SqmExpression<?>>) (List<?>) arguments, nodeBuilder), nodeBuilder));
return newArguments;
}
}
return arguments;
}
use of org.hibernate.query.sqm.tree.SqmTypedNode in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitGenericFunction.
@Override
public Object visitGenericFunction(HqlParser.GenericFunctionContext ctx) {
final String originalFunctionName = visitGenericFunctionName(ctx.genericFunctionName());
final String functionName = originalFunctionName.toLowerCase();
if (creationOptions.useStrictJpaCompliance() && !JPA_STANDARD_FUNCTIONS.contains(functionName)) {
throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [" + originalFunctionName + "], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
}
final ParseTree argumentChild = ctx.getChild(2);
final List<SqmTypedNode<?>> functionArguments;
if (argumentChild instanceof HqlParser.GenericFunctionArgumentsContext) {
functionArguments = (List<SqmTypedNode<?>>) argumentChild.accept(this);
} else if ("*".equals(argumentChild.getText())) {
functionArguments = Collections.singletonList(new SqmStar(getCreationContext().getNodeBuilder()));
} else {
functionArguments = emptyList();
}
final Boolean fromFirst = getFromFirst(ctx);
final Boolean respectNulls = getRespectNullsClause(ctx);
final SqmOrderByClause withinGroup = getWithinGroup(ctx);
final SqmPredicate filterExpression = getFilterExpression(ctx);
final boolean hasOverClause = ctx.getChild(ctx.getChildCount() - 1) instanceof HqlParser.OverClauseContext;
SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
if (functionTemplate == null) {
FunctionKind functionKind = FunctionKind.NORMAL;
if (withinGroup != null) {
functionKind = FunctionKind.ORDERED_SET_AGGREGATE;
} else if (hasOverClause) {
functionKind = FunctionKind.WINDOW;
} else if (filterExpression != null) {
functionKind = FunctionKind.AGGREGATE;
}
functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null, functionName, functionKind, null, SqlAstNodeRenderingMode.DEFAULT);
} else {
if (hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.NORMAL) {
throw new SemanticException("OVER clause is illegal for normal function: " + functionName);
} else if (!hasOverClause && functionTemplate.getFunctionKind() == FunctionKind.WINDOW) {
throw new SemanticException("OVER clause is mandatory for window-only function: " + functionName);
}
if (respectNulls != null) {
switch(functionName) {
case "lag":
case "lead":
case "first_value":
case "last_value":
case "nth_value":
break;
default:
throw new SemanticException("RESPECT/IGNORE NULLS is illegal for function: " + functionName);
}
}
if (fromFirst != null && !"nth_value".equals(functionName)) {
throw new SemanticException("FROM FIRST/LAST is illegal for function: " + functionName);
}
}
final SqmFunction<?> function;
switch(functionTemplate.getFunctionKind()) {
case ORDERED_SET_AGGREGATE:
function = functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
break;
case AGGREGATE:
function = functionTemplate.generateAggregateSqmExpression(functionArguments, filterExpression, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
break;
case WINDOW:
function = functionTemplate.generateWindowSqmExpression(functionArguments, filterExpression, null, null, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
break;
default:
if (filterExpression != null) {
throw new ParsingException("Illegal use of a FILTER clause for non-aggregate function: " + originalFunctionName);
}
function = functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
break;
}
return applyOverClause(ctx, function);
}
use of org.hibernate.query.sqm.tree.SqmTypedNode in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitJpaNonstandardFunction.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Functions
@Override
public SqmExpression<?> visitJpaNonstandardFunction(HqlParser.JpaNonstandardFunctionContext ctx) {
final String functionName = QuotingHelper.unquoteStringLiteral(ctx.getChild(2).getText()).toLowerCase();
final List<SqmTypedNode<?>> functionArguments;
if (ctx.getChildCount() > 4) {
// noinspection unchecked
functionArguments = (List<SqmTypedNode<?>>) ctx.getChild(4).accept(this);
} else {
functionArguments = emptyList();
}
SqmFunctionDescriptor functionTemplate = getFunctionDescriptor(functionName);
if (functionTemplate == null) {
functionTemplate = new NamedSqmFunctionDescriptor(functionName, true, null, StandardFunctionReturnTypeResolvers.invariant(resolveExpressibleTypeBasic(Object.class)), null);
}
return functionTemplate.generateSqmExpression(functionArguments, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration());
}
use of org.hibernate.query.sqm.tree.SqmTypedNode in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitListaggFunction.
@Override
public Object visitListaggFunction(ListaggFunctionContext ctx) {
if (creationOptions.useStrictJpaCompliance()) {
throw new StrictJpaComplianceViolation("Encountered non-compliant non-standard function call [listagg], but strict JPA " + "compliance was requested; use JPA's FUNCTION(functionName[,...]) " + "syntax name instead", StrictJpaComplianceViolation.Type.FUNCTION_CALL);
}
final SqmFunctionDescriptor functionTemplate = getFunctionDescriptor("listagg");
if (functionTemplate == null) {
throw new SemanticException("The listagg function was not registered for the dialect!");
}
final int argumentStartIndex;
final ParseTree thirdChild = ctx.getChild(2);
final boolean distinct;
if (thirdChild instanceof TerminalNode) {
distinct = true;
argumentStartIndex = 3;
} else {
distinct = false;
argumentStartIndex = 2;
}
final SqmExpression<?> firstArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex).accept(this);
final SqmExpression<?> secondArgument = (SqmExpression<?>) ctx.getChild(argumentStartIndex + 2).accept(this);
final ParseTree overflowCtx = ctx.getChild(argumentStartIndex + 3);
final List<SqmTypedNode<?>> functionArguments = new ArrayList<>(3);
if (distinct) {
functionArguments.add(new SqmDistinct<>(firstArgument, creationContext.getNodeBuilder()));
} else {
functionArguments.add(firstArgument);
}
if (overflowCtx instanceof OnOverflowClauseContext) {
if (overflowCtx.getChildCount() > 3) {
// ON OVERFLOW TRUNCATE
final TerminalNode countNode = (TerminalNode) overflowCtx.getChild(overflowCtx.getChildCount() - 2);
final boolean withCount = countNode.getSymbol().getType() == HqlParser.WITH;
final SqmExpression<?> fillerExpression;
if (overflowCtx.getChildCount() == 6) {
fillerExpression = (SqmExpression<?>) overflowCtx.getChild(3).accept(this);
} else {
// The SQL standard says the default is three periods `...`
fillerExpression = new SqmLiteral<>("...", secondArgument.getNodeType(), secondArgument.nodeBuilder());
}
// noinspection unchecked,rawtypes
functionArguments.add(new SqmOverflow(secondArgument, fillerExpression, withCount));
} else {
// ON OVERFLOW ERROR
functionArguments.add(new SqmOverflow<>(secondArgument, null, false));
}
} else {
functionArguments.add(secondArgument);
}
final SqmOrderByClause withinGroup = getWithinGroup(ctx);
final SqmPredicate filterExpression = getFilterExpression(ctx);
return applyOverClause(ctx, functionTemplate.generateOrderedSetAggregateSqmExpression(functionArguments, filterExpression, withinGroup, null, creationContext.getQueryEngine(), creationContext.getJpaMetamodel().getTypeConfiguration()));
}
Aggregations