|
@@ -8,6 +8,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
|
|
|
import org.elasticsearch.xpack.sql.expression.Expression;
|
|
|
import org.elasticsearch.xpack.sql.expression.Nullability;
|
|
|
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
|
|
|
+import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
|
|
|
import org.elasticsearch.xpack.sql.tree.NodeInfo;
|
|
|
import org.elasticsearch.xpack.sql.tree.Source;
|
|
|
import org.elasticsearch.xpack.sql.type.DataType;
|
|
@@ -18,9 +19,7 @@ import java.time.temporal.ChronoField;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
-import java.util.function.Function;
|
|
|
-
|
|
|
-import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.BinaryDateTimeProcessor.BinaryDateOperation.TRUNC;
|
|
|
+import java.util.function.UnaryOperator;
|
|
|
|
|
|
public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
|
|
@@ -34,7 +33,7 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
.with(ChronoField.MONTH_OF_YEAR, 1)
|
|
|
.with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
.toLocalDate().atStartOfDay(dt.getZone());
|
|
|
- },"millennia"),
|
|
|
+ },"millennia"),
|
|
|
CENTURY(dt -> {
|
|
|
int year = dt.getYear();
|
|
|
int firstYearOfCentury = year - (year % 100);
|
|
@@ -43,7 +42,7 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
.with(ChronoField.MONTH_OF_YEAR, 1)
|
|
|
.with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
.toLocalDate().atStartOfDay(dt.getZone());
|
|
|
- }, "centuries"),
|
|
|
+ }, "centuries"),
|
|
|
DECADE(dt -> {
|
|
|
int year = dt.getYear();
|
|
|
int firstYearOfDecade = year - (year % 10);
|
|
@@ -52,7 +51,7 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
.with(ChronoField.MONTH_OF_YEAR, 1)
|
|
|
.with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
.toLocalDate().atStartOfDay(dt.getZone());
|
|
|
- }, "decades"),
|
|
|
+ }, "decades"),
|
|
|
YEAR(dt -> dt
|
|
|
.with(ChronoField.MONTH_OF_YEAR, 1)
|
|
|
.with(ChronoField.DAY_OF_MONTH, 1)
|
|
@@ -65,14 +64,14 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
.with(ChronoField.MONTH_OF_YEAR, firstMonthOfQuarter)
|
|
|
.with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
.toLocalDate().atStartOfDay(dt.getZone());
|
|
|
- }, "quarters", "qq", "q"),
|
|
|
+ }, "quarters", "qq", "q"),
|
|
|
MONTH(dt -> dt
|
|
|
- .with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
- .toLocalDate().atStartOfDay(dt.getZone()),
|
|
|
+ .with(ChronoField.DAY_OF_MONTH, 1)
|
|
|
+ .toLocalDate().atStartOfDay(dt.getZone()),
|
|
|
"months", "mm", "m"),
|
|
|
WEEK(dt -> dt
|
|
|
- .with(ChronoField.DAY_OF_WEEK, 1)
|
|
|
- .toLocalDate().atStartOfDay(dt.getZone()),
|
|
|
+ .with(ChronoField.DAY_OF_WEEK, 1)
|
|
|
+ .toLocalDate().atStartOfDay(dt.getZone()),
|
|
|
"weeks", "wk", "ww"),
|
|
|
DAY(dt -> dt.toLocalDate().atStartOfDay(dt.getZone()), "days", "dd", "d"),
|
|
|
HOUR(dt -> {
|
|
@@ -86,16 +85,16 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
return dt.toLocalDate().atStartOfDay(dt.getZone())
|
|
|
.with(ChronoField.HOUR_OF_DAY, hour)
|
|
|
.with(ChronoField.MINUTE_OF_HOUR, minute);
|
|
|
- }, "minutes", "mi", "n"),
|
|
|
+ }, "minutes", "mi", "n"),
|
|
|
SECOND(dt -> dt.with(ChronoField.NANO_OF_SECOND, 0), "seconds", "ss", "s"),
|
|
|
MILLISECOND(dt -> {
|
|
|
int micros = dt.get(ChronoField.MICRO_OF_SECOND);
|
|
|
return dt.with(ChronoField.MILLI_OF_SECOND, (micros / 1000));
|
|
|
- }, "milliseconds", "ms"),
|
|
|
+ }, "milliseconds", "ms"),
|
|
|
MICROSECOND(dt -> {
|
|
|
int nanos = dt.getNano();
|
|
|
return dt.with(ChronoField.MICRO_OF_SECOND, (nanos / 1000));
|
|
|
- }, "microseconds", "mcs"),
|
|
|
+ }, "microseconds", "mcs"),
|
|
|
NANOSECOND(dt -> dt, "nanoseconds", "ns");
|
|
|
|
|
|
private static final Map<String, Part> NAME_TO_PART;
|
|
@@ -106,10 +105,10 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
VALID_VALUES = DateTimeField.initializeValidValues(values());
|
|
|
}
|
|
|
|
|
|
- private Function<ZonedDateTime, ZonedDateTime> truncateFunction;
|
|
|
+ private UnaryOperator<ZonedDateTime> truncateFunction;
|
|
|
private Set<String> aliases;
|
|
|
|
|
|
- Part(Function<ZonedDateTime, ZonedDateTime> truncateFunction, String... aliases) {
|
|
|
+ Part(UnaryOperator<ZonedDateTime> truncateFunction, String... aliases) {
|
|
|
this.truncateFunction = truncateFunction;
|
|
|
this.aliases = Set.of(aliases);
|
|
|
}
|
|
@@ -133,7 +132,7 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
}
|
|
|
|
|
|
public DateTrunc(Source source, Expression truncateTo, Expression timestamp, ZoneId zoneId) {
|
|
|
- super(source, truncateTo, timestamp, zoneId, TRUNC);
|
|
|
+ super(source, truncateTo, timestamp, zoneId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -157,23 +156,28 @@ public class DateTrunc extends BinaryDateTimeFunction {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected boolean resolveDateTimeField(String dateTimeField) {
|
|
|
- return Part.resolve(dateTimeField) != null;
|
|
|
+ protected String scriptMethodName() {
|
|
|
+ return "dateTrunc";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected List<String> findSimilarDateTimeFields(String dateTimeField) {
|
|
|
- return Part.findSimilar(dateTimeField);
|
|
|
+ public Object fold() {
|
|
|
+ return DateTruncProcessor.process(left().fold(), right().fold(), zoneId());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected String scriptMethodName() {
|
|
|
- return "dateTrunc";
|
|
|
+ protected Pipe createPipe(Pipe left, Pipe right, ZoneId zoneId) {
|
|
|
+ return new DateTruncPipe(source(), this, left, right, zoneId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object fold() {
|
|
|
- return DateTruncProcessor.process(left().fold(), right().fold(), zoneId());
|
|
|
+ protected boolean resolveDateTimeField(String dateTimeField) {
|
|
|
+ return Part.resolve(dateTimeField) != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected List<String> findSimilarDateTimeFields(String dateTimeField) {
|
|
|
+ return Part.findSimilar(dateTimeField);
|
|
|
}
|
|
|
|
|
|
@Override
|