Skip to content

Commit

Permalink
Merge pull request #10924 from sherfert
Browse files Browse the repository at this point in the history
 Orderability for TemporalValues
  • Loading branch information
thobe committed Feb 2, 2018
2 parents 4d5917c + 7999470 commit e812bcc
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
}
}

public int compareTo( DateTimeValue other )
{

return value.compareTo( other.value );
}

@Override
public String prettyPrint()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ private DateValue( LocalDate value )
this.value = value;
}

public int compareTo( DateValue other )
{

return value.compareTo( other.value );
}

@Override
LocalDate temporal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -191,10 +192,20 @@ public abstract static class Compiler<Input> extends DurationBuilder<Input,Metho

private static final DurationValue ZERO = new DurationValue( 0, 0, 0, 0 );
private static final List<TemporalUnit> UNITS = unmodifiableList( asList( MONTHS, DAYS, SECONDS, NANOS ) );
// This comparator is safe until 292,271,023,045 years. After that, we have an overflow.
private static final Comparator<DurationValue> COMPARATOR = Comparator.comparingLong(DurationValue::averageLengthInSeconds)
// nanos are guaranteed to be smaller than NANOS_PER_SECOND
.thenComparingLong( d -> d.nanos )
// At this point, the durations have the same length and we compare by the individual fields.
.thenComparingLong( d -> d.months )
.thenComparingLong( d -> d.days )
.thenComparingLong( d -> d.seconds );

static final int NANOS_PER_SECOND = 1_000_000_000;
static final long SECONDS_PER_DAY = DAYS.getDuration().getSeconds();
/** 30.4375 days = 30 days, 10 hours, 30 minutes */
private static final double AVERAGE_DAYS_PER_MONTH = 365.25 / 12;
private static final double AVERAGE_DAYS_PER_MONTH = 365.2425 / 12;
private static final long AVG_SECONDS_PER_MONTH = 2_629_746;
private final long months;
private final long days;
private final long seconds;
Expand All @@ -203,7 +214,7 @@ public abstract static class Compiler<Input> extends DurationBuilder<Input,Metho
private static DurationValue newDuration( long months, long days, long seconds, int nanos )
{
return seconds == 0 && days == 0 && months == 0 && nanos == 0 // ordered by probability of non-zero
? ZERO : new DurationValue( months, days, seconds, nanos );
? ZERO : new DurationValue( months, days, seconds, nanos );
}

private DurationValue( long months, long days, long seconds, long nanos )
Expand All @@ -226,6 +237,16 @@ else if ( seconds > 0 && nanos < 0 )
this.nanos = (int) nanos;
}

public int compareTo( DurationValue other )
{
return COMPARATOR.compare( this, other );
}

private long averageLengthInSeconds()
{
return this.seconds + this.days * SECONDS_PER_DAY + this.months * AVG_SECONDS_PER_MONTH;
}

long nanosOfDay()
{
return (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + nanos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ private LocalDateTimeValue( LocalDateTime value )
this.value = value;
}

public int compareTo( LocalDateTimeValue other )
{

return value.compareTo( other.value );
}

@Override
LocalDateTime temporal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ private LocalTimeValue( LocalTime value )
this.value = value;
}

public int compareTo( LocalTimeValue other )
{

return value.compareTo( other.value );
}

@Override
LocalTime temporal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ private TimeValue( OffsetTime value )
this.value = value;
}

public int compareTo( TimeValue other )
{

return value.compareTo( other.value );
}

@Override
OffsetTime temporal()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public int compare( Value v1, Value v2 )
// Currently only Point
return ((PointValue) v1).compareTo( (PointValue) v2 );

case ZONED_DATE_TIME:
return ((DateTimeValue) v1).compareTo( (DateTimeValue) v2 );

case LOCAL_DATE_TIME:
return ((LocalDateTimeValue) v1).compareTo( (LocalDateTimeValue) v2 );

case DATE:
return ((DateValue) v1).compareTo( (DateValue) v2 );

case ZONED_TIME:
return ((TimeValue) v1).compareTo( (TimeValue) v2 );

case LOCAL_TIME:
return ((LocalTimeValue) v1).compareTo( (LocalTimeValue) v2 );

case DURATION:
return ((DurationValue) v1).compareTo( (DurationValue) v2 );

case TEXT:
return ((TextValue) v1).compareTo( (TextValue) v2 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
import org.neo4j.values.virtual.VirtualValueTestUtil;

import static java.lang.String.format;
import static org.neo4j.values.storable.DateTimeValue.datetime;
import static org.neo4j.values.storable.DateValue.date;
import static org.neo4j.values.storable.DurationValue.duration;
import static org.neo4j.values.storable.LocalDateTimeValue.localDateTime;
import static org.neo4j.values.storable.LocalTimeValue.localTime;
import static org.neo4j.values.storable.TimeValue.time;
import static org.neo4j.values.storable.Values.pointValue;
import static org.neo4j.values.storable.Values.stringArray;
import static org.neo4j.values.storable.Values.stringValue;
Expand Down Expand Up @@ -119,6 +125,12 @@ public class AnyValueComparatorTest

// SCALARS
pointValue( CoordinateReferenceSystem.Cartesian, 1.0, 1.0 ),
datetime(2018, 2, 2, 0, 0, 0, 0, "+00:00"),
localDateTime(2018, 2, 2, 0, 0, 0, 0),
date(2018, 2, 1),
time(12, 0, 0, 0, "+00:00"),
localTime(0, 0, 0, 1),
duration(0, 0, 0, 0),
"hello",
true,
1L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public void shouldMultiplyDurationByFloat()
duration( 0, 0, 0, 500_000_000 ),
duration( 0, 0, 1, 0 ).mul( doubleValue( 0.5 ) ) );
assertEquals( duration( 0, 0, 43200, 0 ), duration( 0, 1, 0, 0 ).mul( doubleValue( 0.5 ) ) );
assertEquals( duration( 0, 15, 18900, 0 ), duration( 1, 0, 0, 0 ).mul( doubleValue( 0.5 ) ) );
assertEquals( duration( 0, 15, 18873, 0 ), duration( 1, 0, 0, 0 ).mul( doubleValue( 0.5 ) ) );
}

@Test
Expand All @@ -357,7 +357,7 @@ public void shouldDivideDuration()
duration( 0, 0, 0, 500_000_000 ),
duration( 0, 0, 1, 0 ).div( longValue( 2 ) ) );
assertEquals( duration( 0, 0, 43200, 0 ), duration( 0, 1, 0, 0 ).div( longValue( 2 ) ) );
assertEquals( duration( 0, 15, 18900, 0 ), duration( 1, 0, 0, 0 ).div( longValue( 2 ) ) );
assertEquals( duration( 0, 15, 18873, 0 ), duration( 1, 0, 0, 0 ).div( longValue( 2 ) ) );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
import static java.lang.String.format;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;
import static org.neo4j.values.storable.DateTimeValue.datetime;
import static org.neo4j.values.storable.DateValue.date;
import static org.neo4j.values.storable.DurationValue.duration;
import static org.neo4j.values.storable.LocalDateTimeValue.localDateTime;
import static org.neo4j.values.storable.LocalTimeValue.localTime;
import static org.neo4j.values.storable.TimeValue.time;
import static org.neo4j.values.storable.Values.pointValue;

public class ValueComparisonTest
Expand Down Expand Up @@ -72,6 +78,87 @@ public class ValueComparisonTest
pointValue( Cartesian, 1.0, 2.0 ),
pointValue( Cartesian, 2.0, 1.0 ),

// DateTime and the likes
datetime(2018, 2, 2, 0, 0, 0, 0, "+00:00"),
datetime(2018, 2, 2, 1, 30, 0, 0, "+01:00"),
datetime(2018, 2, 2, 1, 0, 0, 0, "+00:00"),
localDateTime(2018, 2, 2, 0, 0, 0, 0),
localDateTime(2018, 2, 2, 0, 0, 0, 1),
localDateTime(2018, 2, 2, 0, 0, 1, 0),
localDateTime(2018, 2, 2, 0, 1, 0, 0),
localDateTime(2018, 2, 2, 1, 0, 0, 0),
date(2018, 2, 1),
date(2018, 2, 2),
time(12, 0, 0, 0, "+00:00"),
time(13, 30, 0, 0, "+01:00"),
time(13, 0, 0, 0, "+00:00"),
localTime(0, 0, 0, 1),
localTime(0, 0, 0, 3),

// Duration
duration(0, 0, 0, 0),
duration(0, 0, 0, 1),
duration(0, 0, 1, 0),
duration(0, 0, 60, 0),
duration(0, 0, 60 * 60, 0),
duration(0, 0, 60 * 60 * 24, 0),
duration(0, 1, 0, 0),
duration(0, 0, 60 * 60 * 24, 1),
duration(0, 1, 60 * 60 * 24, 0),
duration(0, 2, 0, 0),
duration(0, 1, 60 * 60 * 24, 1),
duration(0, 10, 60 * 60 * 24, 2_000_000_500),
duration(0, 11, 2, 500), // Same duration as above, but higher days value
duration(0, 10, 60 * 60 * 24, 2_000_000_501),
duration(0, 27, 0, 0),
duration(1, 0, 0, 0),
duration(0, 31, 0, 0),
duration(0, 59, 0, 0),
duration(2, 0, 0, 0),
duration(0, 62, 0, 0),
duration(0, 89, 0, 0),
duration(3, 0, 0, 0),
duration(0, 92, 0, 0),
duration(0, 120, 0, 0),
duration(4, 0, 0, 0),
duration(0, 123, 0, 0),
duration(0, 150, 0, 0),
duration(5, 0, 0, 0),
duration(0, 153, 0, 0),
duration(0, 181, 0, 0),
duration(6, 0, 0, 0),
duration(0, 184, 0, 0),
duration(0, 212, 0, 0),
duration(7, 0, 0, 0),
duration(0, 215, 0, 0),
duration(0, 242, 0, 0),
duration(8, 0, 0, 0),
duration(0, 245, 0, 0),
duration(0, 273, 0, 0),
duration(9, 0, 0, 0),
duration(0, 276, 0, 0),
duration(0, 303, 0, 0),
duration(10, 0, 0, 0),
duration(0, 306, 0, 0),
duration(0, 334, 0, 0),
duration(11, 0, 0, 0),
duration(0, 337, 0, 0),
duration(0, 365, 0, 0),
duration(12, 0, 0, 0),
duration(0, 366, 0, 0),
duration(0, 1460, 0, 0),
duration(12 * 4, 0, 0, 0),
duration(0, 1461, 0, 0),
duration(0, 36_524, 0, 0),
duration(12 * 100, 0, 0, 0),
duration(0, 36_525, 0, 0),
duration(0, 146_097, 0, 0),
duration(12 * 400, 0, 0, 0), // same duration as line above, but higher number of months
duration(0, 146_097, 0, 1),
duration(9999999999L * 12, 0, 0, 0),
duration(9999999999L * 12, 0, 0, 1),
duration(9999999999L * 12, 0, 0, 2),

// STRING
"",
Character.MIN_VALUE,
Expand Down

0 comments on commit e812bcc

Please sign in to comment.