Search in sources :

Example 1 with Conversion

use of org.hibernate.query.sqm.tree.expression.Conversion in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitBasicValuedPath.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SqmPath
@Override
public Expression visitBasicValuedPath(SqmBasicValuedSimplePath<?> sqmPath) {
    final BasicValuedPathInterpretation<?> path = prepareReusablePath(sqmPath, () -> BasicValuedPathInterpretation.from(sqmPath, this, this, jpaQueryComplianceEnabled));
    Expression result = path;
    if (isDuration(sqmPath.getNodeType())) {
        // Durations are stored (at least by default)
        // in a NUMERIC column in seconds with fractional
        // seconds in the decimal places
        // which we need to convert to the given unit
        // 
        // This does not work at all for a Duration
        // mapped to a VARCHAR column, in which case
        // we would need to parse the weird format
        // defined by java.time.Duration (a bit hard
        // to do without some custom function).
        // Nor does it work for databases which have
        // a well-defined INTERVAL type, but that is
        // something we could implement.
        // first let's apply the propagated scale
        Expression scaledExpression = applyScale(toSqlExpression(path));
        if (adjustedTimestamp != null) {
            if (appliedByUnit != null) {
                throw new IllegalStateException();
            }
            // we're adding this variable duration to the
            // given date or timestamp, producing an
            // adjusted date or timestamp
            result = timestampadd().expression((ReturnableType<?>) adjustedTimestampType, new DurationUnit(SECOND, basicType(Long.class)), scaledExpression, adjustedTimestamp);
        } else if (appliedByUnit != null) {
            // we're applying the 'by unit' operator,
            // producing a literal scalar value, so
            // we must convert this duration from
            // nanoseconds to the given unit
            JdbcMappingContainer durationType = scaledExpression.getExpressionType();
            Duration duration;
            if (durationType.getJdbcMappings().get(0).getJdbcType().isInterval()) {
                // For interval types, we need to extract the epoch for integer arithmetic for the 'by unit' operator
                duration = new Duration(extractEpoch(scaledExpression), SECOND, (BasicValuedMapping) durationType);
            } else {
                // The absolute value of the expression is in seconds
                // as the fractional seconds are in the fraction part as can be seen in DurationJavaType
                duration = new Duration(scaledExpression, SECOND, (BasicValuedMapping) durationType);
            }
            TemporalUnit appliedUnit = appliedByUnit.getUnit().getUnit();
            BasicValuedMapping scalarType = (BasicValuedMapping) appliedByUnit.getNodeType();
            result = new Conversion(duration, appliedUnit, scalarType);
        } else {
            // a "bare" Duration value in nanoseconds
            result = scaledExpression;
        }
    }
    return withTreatRestriction(result, sqmPath);
}
Also used : ReturnableType(org.hibernate.query.ReturnableType) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) JdbcMappingContainer(org.hibernate.metamodel.mapping.JdbcMappingContainer) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) TemporalUnit(org.hibernate.query.sqm.TemporalUnit) SqmDurationUnit(org.hibernate.query.sqm.tree.expression.SqmDurationUnit) DurationUnit(org.hibernate.sql.ast.tree.expression.DurationUnit) SqmToDuration(org.hibernate.query.sqm.tree.expression.SqmToDuration) Duration(org.hibernate.sql.ast.tree.expression.Duration) TypeConfiguration.isDuration(org.hibernate.type.spi.TypeConfiguration.isDuration) Conversion(org.hibernate.query.sqm.tree.expression.Conversion)

Example 2 with Conversion

use of org.hibernate.query.sqm.tree.expression.Conversion in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitToDuration.

@Override
public Object visitToDuration(SqmToDuration<?> toDuration) {
    // TODO: do we need to temporarily set appliedByUnit
    // to null before we recurse down the tree?
    // and what about scale?
    Expression magnitude = toSqlExpression(toDuration.getMagnitude().accept(this));
    DurationUnit unit = (DurationUnit) toDuration.getUnit().accept(this);
    // let's start by applying the propagated scale
    // so we don't forget to do it in what follows
    Expression scaledMagnitude = applyScale(magnitude);
    if (adjustedTimestamp != null) {
        // adjusted date or timestamp
        if (appliedByUnit != null) {
            throw new IllegalStateException();
        }
        return timestampadd().expression(// TODO should be adjustedTimestamp.getType()
        (ReturnableType<?>) adjustedTimestampType, unit, scaledMagnitude, adjustedTimestamp);
    } else {
        BasicValuedMapping durationType = (BasicValuedMapping) toDuration.getNodeType();
        Duration duration;
        if (scaledMagnitude.getExpressionType().getJdbcMappings().get(0).getJdbcType().isInterval()) {
            duration = new Duration(extractEpoch(scaledMagnitude), SECOND, durationType);
        } else {
            duration = new Duration(scaledMagnitude, unit.getUnit(), durationType);
        }
        if (appliedByUnit != null) {
            // we're applying the 'by unit' operator,
            // producing a literal scalar value in
            // the given unit
            TemporalUnit appliedUnit = appliedByUnit.getUnit().getUnit();
            BasicValuedMapping scalarType = (BasicValuedMapping) appliedByUnit.getNodeType();
            return new Conversion(duration, appliedUnit, scalarType);
        } else {
            // a "bare" Duration value (gets rendered as nanoseconds)
            return duration;
        }
    }
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) TemporalUnit(org.hibernate.query.sqm.TemporalUnit) SqmDurationUnit(org.hibernate.query.sqm.tree.expression.SqmDurationUnit) DurationUnit(org.hibernate.sql.ast.tree.expression.DurationUnit) SqmToDuration(org.hibernate.query.sqm.tree.expression.SqmToDuration) Duration(org.hibernate.sql.ast.tree.expression.Duration) TypeConfiguration.isDuration(org.hibernate.type.spi.TypeConfiguration.isDuration) Conversion(org.hibernate.query.sqm.tree.expression.Conversion)

Aggregations

BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)2 TemporalUnit (org.hibernate.query.sqm.TemporalUnit)2 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)2 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)2 Conversion (org.hibernate.query.sqm.tree.expression.Conversion)2 SqmDurationUnit (org.hibernate.query.sqm.tree.expression.SqmDurationUnit)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)2 SqmToDuration (org.hibernate.query.sqm.tree.expression.SqmToDuration)2 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)2 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)2 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)2 Duration (org.hibernate.sql.ast.tree.expression.Duration)2 DurationUnit (org.hibernate.sql.ast.tree.expression.DurationUnit)2 Expression (org.hibernate.sql.ast.tree.expression.Expression)2 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)2 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)2 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)2 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)2 TypeConfiguration.isDuration (org.hibernate.type.spi.TypeConfiguration.isDuration)2