Fix Sybase duration arithmetic problem

This commit is contained in:
Christian Beikov 2023-08-04 16:11:35 +02:00
parent 35fa14a666
commit b57fbb1245
2 changed files with 28 additions and 1 deletions

View File

@ -103,7 +103,7 @@ public class IntegralTimestampaddFunction
private Expression convertedArgument(DurationUnit field, TemporalUnit unit, Expression magnitude) {
final BasicValuedMapping expressionType = (BasicValuedMapping) magnitude.getExpressionType();
final String conversionFactor = field.getUnit().conversionFactor( unit, dialect );
final String conversionFactor = field.getUnit().conversionFactorFull( unit, dialect );
return conversionFactor.isEmpty()
? magnitude
: new BinaryArithmeticExpression(

View File

@ -197,6 +197,33 @@ public enum TemporalUnit {
}
}
public String conversionFactorFull(TemporalUnit unit, Dialect dialect) {
if ( unit == this ) {
//same unit, nothing to do
return "";
}
if ( unit.normalized() != normalized() ) {
throw new SemanticException("Illegal unit conversion " + this + " to " + unit);
}
long from = normalizationFactor( dialect );
long to = unit.normalizationFactor( dialect );
if ( from == to ) {
// the units represent the same amount of time
return "";
}
else {
// if from < to, then this unit represents a
// smaller amount of time than the given unit
// we are converting to (so we're going to
// need to use division)
return (from < to ? "/" : "*")
+ (from < to ? to / from : from / to);
}
}
/**
* The conversion factor required to convert this
* unit to its {@link #normalized()} unit.