Skip to content

Commit

Permalink
Remove Datetime data type (#336)
Browse files Browse the repository at this point in the history
* removed datetime type, updated tests and documentation

Signed-off-by: Matthew Wells <matthew.wells@improving.com>
  • Loading branch information
matthewryanwells committed Aug 17, 2023
1 parent bb3c340 commit 77d8bb8
Show file tree
Hide file tree
Showing 82 changed files with 2,276 additions and 4,371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.google.common.base.Objects;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -57,11 +56,6 @@ public LocalTime timeValue() {
return LocalTime.of(0, 0, 0);
}

@Override
public LocalDateTime datetimeValue() {
return LocalDateTime.of(date, timeValue());
}

@Override
public Instant timestampValue() {
return ZonedDateTime.of(date, timeValue(), UTC_ZONE_ID).toInstant();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.sql.data.model;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand Down Expand Up @@ -35,27 +36,20 @@ public String stringValue() {
}

@Override
public LocalDateTime datetimeValue() {
public Instant timestampValue() {
try {
return new ExprDatetimeValue(value).datetimeValue();
return new ExprTimestampValue(value).timestampValue();
} catch (SemanticCheckException e) {
try {
return new ExprDatetimeValue(
LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0)))
.datetimeValue();
} catch (SemanticCheckException exception) {
throw new SemanticCheckException(
String.format(
"datetime:%s in unsupported format, please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
value));
}
return new ExprTimestampValue(
LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0)))
.timestampValue();
}
}

@Override
public LocalDate dateValue() {
try {
return new ExprDatetimeValue(value).dateValue();
return new ExprTimestampValue(value).dateValue();
} catch (SemanticCheckException e) {
return new ExprDateValue(value).dateValue();
}
Expand All @@ -64,7 +58,7 @@ public LocalDate dateValue() {
@Override
public LocalTime timeValue() {
try {
return new ExprDatetimeValue(value).timeValue();
return new ExprTimestampValue(value).timeValue();
} catch (SemanticCheckException e) {
return new ExprTimeValue(value).timeValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -57,10 +56,6 @@ public LocalDate dateValue(FunctionProperties functionProperties) {
return LocalDate.now(functionProperties.getQueryStartClock());
}

public LocalDateTime datetimeValue(FunctionProperties functionProperties) {
return LocalDateTime.of(dateValue(functionProperties), timeValue());
}

public Instant timestampValue(FunctionProperties functionProperties) {
return ZonedDateTime.of(dateValue(functionProperties), timeValue(), UTC_ZONE_ID).toInstant();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public ExprTimestampValue(String timestamp) {
}
}

/** localDateTime Constructor. */
public ExprTimestampValue(LocalDateTime localDateTime) {
this.timestamp = localDateTime.atZone(UTC_ZONE_ID).toInstant();
}

@Override
public String value() {
return timestamp.getNano() == 0
Expand Down Expand Up @@ -71,11 +76,6 @@ public LocalTime timeValue() {
return timestamp.atZone(UTC_ZONE_ID).toLocalTime();
}

@Override
public LocalDateTime datetimeValue() {
return timestamp.atZone(UTC_ZONE_ID).toLocalDateTime();
}

@Override
public boolean isDateTime() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAmount;
import java.util.List;
Expand Down Expand Up @@ -133,12 +132,6 @@ default LocalDate dateValue() {
"invalid to get dateValue from value of type " + type());
}

/** Get datetime value. */
default LocalDateTime datetimeValue() {
throw new ExpressionEvaluationException(
"invalid to get datetimeValue from value of type " + type());
}

/** Get interval value. */
default TemporalAmount intervalValue() {
throw new ExpressionEvaluationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -66,10 +67,6 @@ public static ExprValue dateValue(LocalDate value) {
return new ExprDateValue(value);
}

public static ExprValue datetimeValue(LocalDateTime value) {
return new ExprDatetimeValue(value);
}

public static ExprValue timeValue(LocalTime value) {
return new ExprTimeValue(value);
}
Expand Down Expand Up @@ -128,14 +125,14 @@ public static ExprValue fromObjectValue(Object o) {
return floatValue((Float) o);
} else if (o instanceof LocalDate) {
return dateValue((LocalDate) o);
} else if (o instanceof LocalDateTime) {
return datetimeValue((LocalDateTime) o);
} else if (o instanceof LocalTime) {
return timeValue((LocalTime) o);
} else if (o instanceof Instant) {
return timestampValue((Instant) o);
} else if (o instanceof TemporalAmount) {
return intervalValue((TemporalAmount) o);
} else if (o instanceof LocalDateTime) {
return timestampValue(((LocalDateTime) o).toInstant(ZoneOffset.UTC));
} else {
throw new ExpressionEvaluationException("unsupported object " + o.getClass());
}
Expand All @@ -150,8 +147,6 @@ public static ExprValue fromObjectValue(Object o, ExprCoreType type) {
return new ExprDateValue((String) o);
case TIME:
return new ExprTimeValue((String) o);
case DATETIME:
return new ExprDatetimeValue((String) o);
default:
return fromObjectValue(o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public enum ExprCoreType implements ExprType {
/** Date. */
DATE(STRING),
TIME(STRING),
DATETIME(STRING, DATE, TIME),
TIMESTAMP(STRING, DATETIME),
TIMESTAMP(STRING, DATE, TIME),
INTERVAL(UNDEFINED),

/** Struct. */
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,6 @@ public static FunctionExpression castTimestamp(Expression value) {
return compile(FunctionProperties.None, BuiltinFunctionName.CAST_TO_TIMESTAMP, value);
}

public static FunctionExpression castDatetime(Expression value) {
return compile(FunctionProperties.None, BuiltinFunctionName.CAST_TO_DATETIME, value);
}

public static FunctionExpression typeof(Expression value) {
return compile(FunctionProperties.None, BuiltinFunctionName.TYPEOF, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static org.opensearch.sql.data.type.ExprCoreType.ARRAY;
import static org.opensearch.sql.data.type.ExprCoreType.DATE;
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME;
import static org.opensearch.sql.data.type.ExprCoreType.DOUBLE;
import static org.opensearch.sql.data.type.ExprCoreType.FLOAT;
import static org.opensearch.sql.data.type.ExprCoreType.INTEGER;
Expand Down Expand Up @@ -71,9 +70,6 @@ private static DefaultFunctionResolver avg() {
.put(
new FunctionSignature(functionName, Collections.singletonList(DATE)),
(functionProperties, arguments) -> new AvgAggregator(arguments, DATE))
.put(
new FunctionSignature(functionName, Collections.singletonList(DATETIME)),
(functionProperties, arguments) -> new AvgAggregator(arguments, DATETIME))
.put(
new FunctionSignature(functionName, Collections.singletonList(TIME)),
(functionProperties, arguments) -> new AvgAggregator(arguments, TIME))
Expand Down Expand Up @@ -142,9 +138,6 @@ private static DefaultFunctionResolver min() {
.put(
new FunctionSignature(functionName, Collections.singletonList(DATE)),
(functionProperties, arguments) -> new MinAggregator(arguments, DATE))
.put(
new FunctionSignature(functionName, Collections.singletonList(DATETIME)),
(functionProperties, arguments) -> new MinAggregator(arguments, DATETIME))
.put(
new FunctionSignature(functionName, Collections.singletonList(TIME)),
(functionProperties, arguments) -> new MinAggregator(arguments, TIME))
Expand Down Expand Up @@ -177,9 +170,6 @@ private static DefaultFunctionResolver max() {
.put(
new FunctionSignature(functionName, Collections.singletonList(DATE)),
(functionProperties, arguments) -> new MaxAggregator(arguments, DATE))
.put(
new FunctionSignature(functionName, Collections.singletonList(DATETIME)),
(functionProperties, arguments) -> new MaxAggregator(arguments, DATETIME))
.put(
new FunctionSignature(functionName, Collections.singletonList(TIME)),
(functionProperties, arguments) -> new MaxAggregator(arguments, TIME))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Locale;
import org.opensearch.sql.data.model.ExprDateValue;
import org.opensearch.sql.data.model.ExprDatetimeValue;
import org.opensearch.sql.data.model.ExprDoubleValue;
import org.opensearch.sql.data.model.ExprIntegerValue;
import org.opensearch.sql.data.model.ExprNullValue;
Expand Down Expand Up @@ -47,8 +46,6 @@ public AvgState create() {
switch (dataType) {
case DATE:
return new DateAvgState();
case DATETIME:
return new DateTimeAvgState();
case TIMESTAMP:
return new TimestampAvgState();
case TIME:
Expand Down Expand Up @@ -128,28 +125,6 @@ protected AvgState iterate(ExprValue value) {
}
}

protected static class DateTimeAvgState extends AvgState {
@Override
public ExprValue result() {
if (0 == count.integerValue()) {
return ExprNullValue.of();
}

return new ExprDatetimeValue(
new ExprTimestampValue(
Instant.ofEpochMilli(
DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue()))
.datetimeValue());
}

@Override
protected AvgState iterate(ExprValue value) {
total =
DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())).valueOf();
return super.iterate(value);
}
}

protected static class TimestampAvgState extends AvgState {
@Override
public ExprValue result() {
Expand Down
Loading

0 comments on commit 77d8bb8

Please sign in to comment.