Skip to content

Commit

Permalink
Model timestampdiff with interval as result with null duration unit
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Mar 25, 2022
1 parent de21820 commit 6b8fdf3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
Expand Up @@ -516,6 +516,9 @@ public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType,

@Override
public String timestampdiffPattern(TemporalUnit unit, TemporalType fromTemporalType, TemporalType toTemporalType) {
if ( unit == null ) {
return "(?3-?2)";
}
switch (unit) {
case YEAR:
return "(extract(year from ?3)-extract(year from ?2))";
Expand Down
Expand Up @@ -398,6 +398,9 @@ public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType,

@Override
public String timestampdiffPattern(TemporalUnit unit, TemporalType fromTemporalType, TemporalType toTemporalType) {
if ( unit == null ) {
return "(?3-?2)";
}
return "datediff(?1,?2,?3)";
}

Expand Down
Expand Up @@ -330,6 +330,9 @@ public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType,

@Override
public String timestampdiffPattern(TemporalUnit unit, TemporalType fromTemporalType, TemporalType toTemporalType) {
if ( unit == null ) {
return "(?3-?2)";
}
if ( toTemporalType != TemporalType.TIMESTAMP && fromTemporalType != TemporalType.TIMESTAMP && unit == DAY ) {
// special case: subtraction of two dates
// results in an integer number of days
Expand Down
Expand Up @@ -11,6 +11,7 @@

import org.hibernate.dialect.Dialect;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void render(
final Expression from = (Expression) arguments.get( 1 );
final Expression to = (Expression) arguments.get( 2 );

patternRenderer( field.getUnit(), from, to ).render( sqlAppender, arguments, walker );
patternRenderer( field == null ? null : field.getUnit(), from, to ).render( sqlAppender, arguments, walker );
}

private PatternRenderer patternRenderer(TemporalUnit unit, Expression from, Expression to) {
Expand All @@ -99,7 +100,9 @@ public SelfRenderingFunctionSqlAstExpression expression(
impliedResultType != null
? impliedResultType
: (ReturnableType<?>) field.getExpressionType().getJdbcMapping(),
field.getExpressionType()
field != null
? field.getExpressionType()
: (JdbcMappingContainer) impliedResultType
);
}

Expand Down
Expand Up @@ -5453,10 +5453,20 @@ private Object transformDatetimeArithmetic(SqmBinaryArithmetic<?> expression) {
// the diff, and then the subsequent add

DurationUnit unit = new DurationUnit( baseUnit, diffResultType );
Expression magnitude = applyScale( timestampdiff().expression( null, unit, right, left ) );
BasicValuedMapping durationType = (BasicValuedMapping) expression.getNodeType();
Expression scaledMagnitude = applyScale(
timestampdiff().expression(
(ReturnableType<?>) expression.getNodeType(),
durationType.getJdbcMapping().getJdbcType().isInterval() ? null : unit,
right,
left
)
);
return timestampadd().expression(
(ReturnableType<?>) adjustedTimestampType, //TODO should be adjustedTimestamp.getType()
unit, magnitude, adjustedTimestamp
unit,
scaledMagnitude,
adjustedTimestamp
);
}
else if ( appliedByUnit != null ) {
Expand All @@ -5470,10 +5480,14 @@ else if ( appliedByUnit != null ) {
// a plain "bare" Duration
DurationUnit unit = new DurationUnit( baseUnit, diffResultType );
BasicValuedMapping durationType = (BasicValuedMapping) expression.getNodeType();
Expression scaledMagnitude = applyScale( timestampdiff().expression(
(ReturnableType<?>) expression.getNodeType(),
unit, right, left
) );
Expression scaledMagnitude = applyScale(
timestampdiff().expression(
(ReturnableType<?>) expression.getNodeType(),
durationType.getJdbcMapping().getJdbcType().isInterval() ? null : unit,
right,
left
)
);
return new Duration( scaledMagnitude, baseUnit, durationType );
}
}
Expand Down
Expand Up @@ -4439,10 +4439,12 @@ public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeti
@Override
public void visitDuration(Duration duration) {
duration.getMagnitude().accept( this );
// Convert to NANOSECOND because DurationJavaType requires values in that unit
appendSql(
duration.getUnit().conversionFactor( NANOSECOND, getDialect() )
);
if ( !duration.getExpressionType().getJdbcMapping().getJdbcType().isInterval() ) {
// Convert to NANOSECOND because DurationJavaType requires values in that unit
appendSql(
duration.getUnit().conversionFactor( NANOSECOND, getDialect() )
);
}
}

@Override
Expand Down

0 comments on commit 6b8fdf3

Please sign in to comment.