3131import java .nio .ByteBuffer ;
3232import java .time .DayOfWeek ;
3333import java .time .Instant ;
34+ import java .time .LocalDate ;
3435import java .time .ZoneId ;
3536import java .time .ZonedDateTime ;
3637import java .time .temporal .ChronoUnit ;
38+ import java .util .ArrayList ;
3739import java .util .Collections ;
3840import java .util .List ;
3941import java .util .Objects ;
@@ -85,6 +87,7 @@ abstract static class ExtractTimeCompareFilter extends TimeFilter {
8587 private final transient Function <Long , Long > EXTRACT_TIMESTAMP_MS_PART ;
8688 private final transient Function <Long , Long > EXTRACT_TIMESTAMP_US_PART ;
8789 private final transient Function <Long , Long > EXTRACT_TIMESTAMP_NS_PART ;
90+ protected final transient Function <Integer , Long > GET_YEAR_TIMESTAMP ;
8891
8992 // calculate extraction of time
9093 protected final transient Function <Long , Long > evaluateFunction ;
@@ -105,20 +108,32 @@ protected ExtractTimeCompareFilter(
105108 EXTRACT_TIMESTAMP_MS_PART = timestamp -> Math .floorMod (timestamp , 1000_000L ) / 1000 ;
106109 EXTRACT_TIMESTAMP_US_PART = timestamp -> Math .floorMod (timestamp , 1000L );
107110 EXTRACT_TIMESTAMP_NS_PART = timestamp -> 0L ;
111+ GET_YEAR_TIMESTAMP =
112+ year ->
113+ Math .multiplyExact (
114+ LocalDate .of (year , 1 , 1 ).atStartOfDay (zoneId ).toEpochSecond (), 1000_000L );
108115 break ;
109116 case NANOSECONDS :
110117 CAST_TIMESTAMP_TO_MS = timestamp -> timestamp / 1000000 ;
111118 EXTRACT_TIMESTAMP_MS_PART =
112119 timestamp -> Math .floorMod (timestamp , 1000_000_000L ) / 1000_000 ;
113120 EXTRACT_TIMESTAMP_US_PART = timestamp -> Math .floorMod (timestamp , 1000_000L ) / 1000 ;
114121 EXTRACT_TIMESTAMP_NS_PART = timestamp -> Math .floorMod (timestamp , 1000L );
122+ GET_YEAR_TIMESTAMP =
123+ year ->
124+ Math .multiplyExact (
125+ LocalDate .of (year , 1 , 1 ).atStartOfDay (zoneId ).toEpochSecond (), 1000_000_000L );
115126 break ;
116127 case MILLISECONDS :
117128 default :
118129 CAST_TIMESTAMP_TO_MS = timestamp -> timestamp ;
119130 EXTRACT_TIMESTAMP_MS_PART = timestamp -> Math .floorMod (timestamp , 1000L );
120131 EXTRACT_TIMESTAMP_US_PART = timestamp -> 0L ;
121132 EXTRACT_TIMESTAMP_NS_PART = timestamp -> 0L ;
133+ GET_YEAR_TIMESTAMP =
134+ year ->
135+ Math .multiplyExact (
136+ LocalDate .of (year , 1 , 1 ).atStartOfDay (zoneId ).toEpochSecond (), 1000L );
122137 break ;
123138 }
124139 evaluateFunction = constructEvaluateFunction (field , zoneId );
@@ -322,15 +337,9 @@ public boolean timeSatisfy(long time) {
322337
323338 @ Override
324339 public boolean satisfyStartEndTime (long startTime , long endTime ) {
325- boolean ltEq =
326- !(truncatedEqualsFunction .apply (startTime , endTime )
327- && evaluateFunction .apply (startTime ) < constant
328- && evaluateFunction .apply (endTime ) < constant );
329- boolean gtEq =
330- !(truncatedEqualsFunction .apply (startTime , endTime )
331- && evaluateFunction .apply (startTime ) < constant
332- && evaluateFunction .apply (endTime ) < constant );
333- return ltEq && gtEq ;
340+ return !(truncatedEqualsFunction .apply (startTime , endTime )
341+ && (evaluateFunction .apply (endTime ) < constant
342+ || evaluateFunction .apply (startTime ) > constant ));
334343 }
335344
336345 @ Override
@@ -340,6 +349,16 @@ public boolean containStartEndTime(long startTime, long endTime) {
340349 && evaluateFunction .apply (endTime ) == constant ;
341350 }
342351
352+ @ Override
353+ public List <TimeRange > getTimeRanges () {
354+ if (field == Field .YEAR ) {
355+ int year = (int ) constant ;
356+ return Collections .singletonList (
357+ new TimeRange (GET_YEAR_TIMESTAMP .apply (year ), GET_YEAR_TIMESTAMP .apply (year + 1 ) - 1 ));
358+ }
359+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
360+ }
361+
343362 @ Override
344363 public Filter reverse () {
345364 return new ExtractTimeNotEq (constant , field , zoneId , currPrecision );
@@ -368,22 +387,28 @@ public boolean timeSatisfy(long time) {
368387
369388 @ Override
370389 public boolean satisfyStartEndTime (long startTime , long endTime ) {
371- boolean lt =
372- !(truncatedEqualsFunction .apply (startTime , endTime )
373- && evaluateFunction .apply (startTime ) >= constant
374- && evaluateFunction .apply (endTime ) >= constant );
375- boolean gt =
376- !(truncatedEqualsFunction .apply (startTime , endTime )
377- && evaluateFunction .apply (startTime ) <= constant
378- && evaluateFunction .apply (endTime ) <= constant );
379- return lt || gt ;
390+ return !(truncatedEqualsFunction .apply (startTime , endTime )
391+ && evaluateFunction .apply (startTime ) == constant
392+ && evaluateFunction .apply (endTime ) == constant );
380393 }
381394
382395 @ Override
383396 public boolean containStartEndTime (long startTime , long endTime ) {
384- return !(truncatedEqualsFunction .apply (startTime , endTime )
385- && evaluateFunction .apply (startTime ) == constant
386- && evaluateFunction .apply (endTime ) == constant );
397+ return truncatedEqualsFunction .apply (startTime , endTime )
398+ && (evaluateFunction .apply (startTime ) > constant
399+ || evaluateFunction .apply (endTime ) < constant );
400+ }
401+
402+ @ Override
403+ public List <TimeRange > getTimeRanges () {
404+ if (field == Field .YEAR ) {
405+ List <TimeRange > res = new ArrayList <>();
406+ int year = (int ) constant ;
407+ res .add (new TimeRange (Long .MIN_VALUE , GET_YEAR_TIMESTAMP .apply (year ) - 1 ));
408+ res .add (new TimeRange (GET_YEAR_TIMESTAMP .apply (year + 1 ), Long .MAX_VALUE ));
409+ return res ;
410+ }
411+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
387412 }
388413
389414 @ Override
@@ -415,17 +440,25 @@ public boolean timeSatisfy(long time) {
415440 @ Override
416441 public boolean satisfyStartEndTime (long startTime , long endTime ) {
417442 return !(truncatedEqualsFunction .apply (startTime , endTime )
418- && evaluateFunction .apply (startTime ) >= constant
419- && evaluateFunction .apply (endTime ) >= constant );
443+ && evaluateFunction .apply (startTime ) >= constant );
420444 }
421445
422446 @ Override
423447 public boolean containStartEndTime (long startTime , long endTime ) {
424448 return truncatedEqualsFunction .apply (startTime , endTime )
425- && evaluateFunction .apply (startTime ) < constant
426449 && evaluateFunction .apply (endTime ) < constant ;
427450 }
428451
452+ @ Override
453+ public List <TimeRange > getTimeRanges () {
454+ if (field == Field .YEAR ) {
455+ int year = (int ) constant ;
456+ return Collections .singletonList (
457+ new TimeRange (Long .MIN_VALUE , GET_YEAR_TIMESTAMP .apply (year ) - 1 ));
458+ }
459+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
460+ }
461+
429462 @ Override
430463 public Filter reverse () {
431464 return new ExtractTimeGtEq (constant , field , zoneId , currPrecision );
@@ -455,17 +488,25 @@ public boolean timeSatisfy(long time) {
455488 @ Override
456489 public boolean satisfyStartEndTime (long startTime , long endTime ) {
457490 return !(truncatedEqualsFunction .apply (startTime , endTime )
458- && evaluateFunction .apply (startTime ) > constant
459- && evaluateFunction .apply (endTime ) > constant );
491+ && evaluateFunction .apply (startTime ) > constant );
460492 }
461493
462494 @ Override
463495 public boolean containStartEndTime (long startTime , long endTime ) {
464496 return truncatedEqualsFunction .apply (startTime , endTime )
465- && evaluateFunction .apply (startTime ) <= constant
466497 && evaluateFunction .apply (endTime ) <= constant ;
467498 }
468499
500+ @ Override
501+ public List <TimeRange > getTimeRanges () {
502+ if (field == Field .YEAR ) {
503+ int year = (int ) constant ;
504+ return Collections .singletonList (
505+ new TimeRange (Long .MIN_VALUE , GET_YEAR_TIMESTAMP .apply (year + 1 ) - 1 ));
506+ }
507+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
508+ }
509+
469510 @ Override
470511 public Filter reverse () {
471512 return new ExtractTimeGt (constant , field , zoneId , currPrecision );
@@ -495,15 +536,23 @@ public boolean timeSatisfy(long time) {
495536 @ Override
496537 public boolean satisfyStartEndTime (long startTime , long endTime ) {
497538 return !(truncatedEqualsFunction .apply (startTime , endTime )
498- && evaluateFunction .apply (startTime ) <= constant
499539 && evaluateFunction .apply (endTime ) <= constant );
500540 }
501541
502542 @ Override
503543 public boolean containStartEndTime (long startTime , long endTime ) {
504544 return truncatedEqualsFunction .apply (startTime , endTime )
505- && evaluateFunction .apply (startTime ) > constant
506- && evaluateFunction .apply (endTime ) > constant ;
545+ && evaluateFunction .apply (startTime ) > constant ;
546+ }
547+
548+ @ Override
549+ public List <TimeRange > getTimeRanges () {
550+ if (field == Field .YEAR ) {
551+ int year = (int ) constant ;
552+ return Collections .singletonList (
553+ new TimeRange (GET_YEAR_TIMESTAMP .apply (year + 1 ), Long .MAX_VALUE ));
554+ }
555+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
507556 }
508557
509558 @ Override
@@ -535,15 +584,23 @@ public boolean timeSatisfy(long time) {
535584 @ Override
536585 public boolean satisfyStartEndTime (long startTime , long endTime ) {
537586 return !(truncatedEqualsFunction .apply (startTime , endTime )
538- && evaluateFunction .apply (startTime ) < constant
539587 && evaluateFunction .apply (endTime ) < constant );
540588 }
541589
542590 @ Override
543591 public boolean containStartEndTime (long startTime , long endTime ) {
544592 return truncatedEqualsFunction .apply (startTime , endTime )
545- && evaluateFunction .apply (startTime ) >= constant
546- && evaluateFunction .apply (endTime ) >= constant ;
593+ && evaluateFunction .apply (startTime ) >= constant ;
594+ }
595+
596+ @ Override
597+ public List <TimeRange > getTimeRanges () {
598+ if (field == Field .YEAR ) {
599+ int year = (int ) constant ;
600+ return Collections .singletonList (
601+ new TimeRange (GET_YEAR_TIMESTAMP .apply (year ), Long .MAX_VALUE ));
602+ }
603+ return Collections .singletonList (new TimeRange (Long .MIN_VALUE , Long .MAX_VALUE ));
547604 }
548605
549606 @ Override
0 commit comments