Skip to content

Commit

Permalink
Remove Datetime data type (#1980)
Browse files Browse the repository at this point in the history
* Remove Datetime data type (#336)

* removed datetime type, updated tests and documentation

Signed-off-by: Matthew Wells <[email protected]>

* removed duplicate test code, replaced calls of ZoneOffset.UTC with a constant

Signed-off-by: Matthew Wells <[email protected]>

* readded test and edited it to return timestamp, fixed minor checkstyle difference

Signed-off-by: Matthew Wells <[email protected]>

* converted all utc timezone/zone id to be ZoneOffset.UTC

Signed-off-by: Matthew Wells <[email protected]>

* Spotless Apply

Signed-off-by: Matthew Wells <[email protected]>

* Added tests back in and updated to work with timestamp

Signed-off-by: Matthew Wells <[email protected]>

* Spotless Apply

Signed-off-by: Matthew Wells <[email protected]>

* removed duplicate tests, renamed test

Signed-off-by: Matthew Wells <[email protected]>

---------

Signed-off-by: Matthew Wells <[email protected]>
  • Loading branch information
matthewryanwells authored Aug 21, 2023
1 parent 752da21 commit 25beda0
Show file tree
Hide file tree
Showing 82 changed files with 1,063 additions and 2,611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
package org.opensearch.sql.data.model;

import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;

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.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -57,14 +56,9 @@ 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();
return ZonedDateTime.of(date, timeValue(), ZoneOffset.UTC).toInstant();
}

@Override
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 @@ -7,12 +7,11 @@

import static java.time.format.DateTimeFormatter.ISO_LOCAL_TIME;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.Objects;
Expand Down Expand Up @@ -57,12 +56,8 @@ 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();
return ZonedDateTime.of(dateValue(functionProperties), timeValue(), ZoneOffset.UTC).toInstant();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_VARIABLE_NANOS;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_WITHOUT_NANO;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
Expand All @@ -32,7 +32,7 @@ public ExprTimestampValue(String timestamp) {
try {
this.timestamp =
LocalDateTime.parse(timestamp, DATE_TIME_FORMATTER_VARIABLE_NANOS)
.atZone(UTC_ZONE_ID)
.atZone(ZoneOffset.UTC)
.toInstant();
} catch (DateTimeParseException e) {
throw new SemanticCheckException(
Expand All @@ -42,13 +42,18 @@ public ExprTimestampValue(String timestamp) {
}
}

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

@Override
public String value() {
return timestamp.getNano() == 0
? DATE_TIME_FORMATTER_WITHOUT_NANO
.withZone(UTC_ZONE_ID)
.withZone(ZoneOffset.UTC)
.format(timestamp.truncatedTo(ChronoUnit.SECONDS))
: DATE_TIME_FORMATTER_VARIABLE_NANOS.withZone(UTC_ZONE_ID).format(timestamp);
: DATE_TIME_FORMATTER_VARIABLE_NANOS.withZone(ZoneOffset.UTC).format(timestamp);
}

@Override
Expand All @@ -63,17 +68,12 @@ public Instant timestampValue() {

@Override
public LocalDate dateValue() {
return timestamp.atZone(UTC_ZONE_ID).toLocalDate();
return timestamp.atZone(ZoneOffset.UTC).toLocalDate();
}

@Override
public LocalTime timeValue() {
return timestamp.atZone(UTC_ZONE_ID).toLocalTime();
}

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

@Override
Expand All @@ -88,12 +88,12 @@ public String toString() {

@Override
public int compare(ExprValue other) {
return timestamp.compareTo(other.timestampValue().atZone(UTC_ZONE_ID).toInstant());
return timestamp.compareTo(other.timestampValue().atZone(ZoneOffset.UTC).toInstant());
}

@Override
public boolean equal(ExprValue other) {
return timestamp.equals(other.timestampValue().atZone(UTC_ZONE_ID).toInstant());
return timestamp.equals(other.timestampValue().atZone(ZoneOffset.UTC).toInstant());
}

@Override
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
Loading

0 comments on commit 25beda0

Please sign in to comment.