use of org.hibernate.query.sqm.NodeBuilder in project hibernate-orm by hibernate.
the class ExtractFunction method extractOffsetUsingFormat.
private SelfRenderingSqmFunction<ZoneOffset> extractOffsetUsingFormat(SqmExpression<?> expressionToExtract, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
final NodeBuilder builder = expressionToExtract.nodeBuilder();
final BasicType<ZoneOffset> offsetType = typeConfiguration.getBasicTypeForJavaType(ZoneOffset.class);
final BasicType<String> stringType = typeConfiguration.getBasicTypeForJavaType(String.class);
final SqmFormat offsetFormat = new SqmFormat(// pattern for timezone offset
"xxx", stringType, builder);
return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("format").generateSqmExpression(asList(expressionToExtract, offsetFormat), offsetType, queryEngine, typeConfiguration);
}
use of org.hibernate.query.sqm.NodeBuilder in project hibernate-orm by hibernate.
the class ExtractFunction method extractNanoseconds.
private SelfRenderingSqmFunction<Long> extractNanoseconds(SqmExpression<?> expressionToExtract, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
final NodeBuilder builder = expressionToExtract.nodeBuilder();
final BasicType<Float> floatType = typeConfiguration.getBasicTypeForJavaType(Float.class);
final SqmExtractUnit<Float> extractSeconds = new SqmExtractUnit<>(SECOND, floatType, builder);
final SqmLiteral<Float> billion = new SqmLiteral<>(1e9f, floatType, builder);
return toLong(new SqmBinaryArithmetic<>(MULTIPLY, generateSqmExpression(asList(extractSeconds, expressionToExtract), floatType, queryEngine, typeConfiguration), billion, floatType, builder), queryEngine, typeConfiguration);
}
use of org.hibernate.query.sqm.NodeBuilder in project hibernate-orm by hibernate.
the class ExtractFunction method extractDateOrTimeUsingCast.
private SelfRenderingSqmFunction<?> extractDateOrTimeUsingCast(SqmExpression<?> expressionToExtract, ReturnableType<?> type, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
final NodeBuilder builder = expressionToExtract.nodeBuilder();
final SqmCastTarget<?> target = new SqmCastTarget<>(type, builder);
return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("cast").generateSqmExpression(asList(expressionToExtract, target), type, queryEngine, typeConfiguration);
}
use of org.hibernate.query.sqm.NodeBuilder in project hibernate-orm by hibernate.
the class ExtractFunction method extractWeek.
private SelfRenderingSqmFunction<Integer> extractWeek(SqmExpression<?> expressionToExtract, SqmExtractUnit<?> field, TemporalUnit dayOf, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
final NodeBuilder builder = field.nodeBuilder();
final BasicType<Integer> intType = typeConfiguration.getBasicTypeForJavaType(Integer.class);
final BasicType<Float> floatType = typeConfiguration.getBasicTypeForJavaType(Float.class);
final SqmExtractUnit<Integer> dayOfUnit = new SqmExtractUnit<>(dayOf, intType, builder);
final SqmExpression<Integer> extractDayOf = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("extract").generateSqmExpression(asList(dayOfUnit, expressionToExtract), intType, queryEngine, typeConfiguration);
final SqmExtractUnit<Integer> dayOfWeekUnit = new SqmExtractUnit<>(DAY_OF_WEEK, intType, builder);
final SqmExpression<Integer> extractDayOfWeek = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("extract").generateSqmExpression(asList(dayOfWeekUnit, expressionToExtract), intType, queryEngine, typeConfiguration);
final SqmLiteral<Float> seven = new SqmLiteral<>(7.0f, floatType, builder);
final SqmLiteral<Integer> one = new SqmLiteral<>(1, intType, builder);
final SqmBinaryArithmetic<Integer> daySubtractionInt = new SqmBinaryArithmetic<>(SUBTRACT, extractDayOf, extractDayOfWeek, intType, builder);
final SqmExpression<?> daySubtraction;
if (dialect.requiresFloatCastingOfIntegerDivision()) {
daySubtraction = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("cast").generateSqmExpression(asList(daySubtractionInt, new SqmCastTarget<>(floatType, builder)), floatType, queryEngine, typeConfiguration);
} else {
daySubtraction = daySubtractionInt;
}
return queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("ceiling").generateSqmExpression(new SqmBinaryArithmetic<>(ADD, new SqmBinaryArithmetic<>(DIVIDE, daySubtraction, seven, floatType, builder), one, intType, builder), // Implicit cast to int
intType, queryEngine, typeConfiguration);
}
use of org.hibernate.query.sqm.NodeBuilder 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;
}
Aggregations