Skip to content

Commit

Permalink
Minor clean up of datetime and other classes (#198)
Browse files Browse the repository at this point in the history
Signed-off-by: MitchellGale-BitQuill <mitchellg@bitquilltech.com>
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
MitchellGale and Yury-Fridlyand authored Jan 30, 2023
1 parent 9d4a841 commit a6b4f48
Show file tree
Hide file tree
Showing 42 changed files with 1,473 additions and 2,975 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
package org.opensearch.sql.ast.statement;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opensearch.sql.ast.AbstractNodeVisitor;

/**
* Explain Statement.
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Explain extends Statement {

private final Statement statement;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/opensearch/sql/ast/tree/Limit.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@RequiredArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
@EqualsAndHashCode(callSuper = false)
public class Limit extends UnresolvedPlan {
private UnresolvedPlan child;
private final Integer limit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -68,7 +69,7 @@ public LocalDateTime datetimeValue() {

@Override
public Instant timestampValue() {
return ZonedDateTime.of(date, timeValue(), ExprTimestampValue.ZONE).toInstant();
return ZonedDateTime.of(date, timeValue(), UTC_ZONE_ID).toInstant();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,34 @@

package org.opensearch.sql.data.model;

import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_WITH_TZ;
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.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import lombok.RequiredArgsConstructor;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.exception.SemanticCheckException;


@RequiredArgsConstructor
public class ExprDatetimeValue extends AbstractExprValue {
private final LocalDateTime datetime;

private static final DateTimeFormatter FORMATTER_VARIABLE_NANOS;
private static final int MIN_FRACTION_SECONDS = 0;
private static final int MAX_FRACTION_SECONDS = 9;

static {
FORMATTER_VARIABLE_NANOS = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH:mm:ss[xxx]")
.appendFraction(
ChronoField.NANO_OF_SECOND,
MIN_FRACTION_SECONDS,
MAX_FRACTION_SECONDS,
true)
.toFormatter();
}

/**
* Constructor with datetime string as input.
*/
public ExprDatetimeValue(String datetime) {
try {
this.datetime = LocalDateTime.parse(datetime, FORMATTER_VARIABLE_NANOS);
this.datetime = LocalDateTime.parse(datetime, DATE_TIME_FORMATTER_WITH_TZ);
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", datetime));
Expand All @@ -70,7 +57,7 @@ public LocalTime timeValue() {

@Override
public Instant timestampValue() {
return ZonedDateTime.of(datetime, ExprTimestampValue.ZONE).toInstant();
return ZonedDateTime.of(datetime, UTC_ZONE_ID).toInstant();
}

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

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;
Expand Down Expand Up @@ -66,7 +67,7 @@ public LocalDateTime datetimeValue(FunctionProperties functionProperties) {
}

public Instant timestampValue(FunctionProperties functionProperties) {
return ZonedDateTime.of(dateValue(functionProperties), timeValue(), ExprTimestampValue.ZONE)
return ZonedDateTime.of(dateValue(functionProperties), timeValue(), UTC_ZONE_ID)
.toInstant();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,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.ZoneId;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
Expand All @@ -27,10 +27,6 @@
*/
@RequiredArgsConstructor
public class ExprTimestampValue extends AbstractExprValue {
/**
* todo. only support UTC now.
*/
public static final ZoneId ZONE = ZoneId.of("UTC");

private final Instant timestamp;

Expand All @@ -40,7 +36,7 @@ public class ExprTimestampValue extends AbstractExprValue {
public ExprTimestampValue(String timestamp) {
try {
this.timestamp = LocalDateTime.parse(timestamp, DATE_TIME_FORMATTER_VARIABLE_NANOS)
.atZone(ZONE)
.atZone(UTC_ZONE_ID)
.toInstant();
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("timestamp:%s in unsupported format, please "
Expand All @@ -51,9 +47,9 @@ public ExprTimestampValue(String timestamp) {

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

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

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

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

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

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_LONG_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_SHORT_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_STRICT_WITH_TZ;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;
import static org.opensearch.sql.utils.DateTimeUtils.extractDate;
import static org.opensearch.sql.utils.DateTimeUtils.extractDateTime;

Expand Down Expand Up @@ -111,6 +112,7 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(datediff());
repository.register(datetime());
repository.register(date_add());
repository.register(date_format());
repository.register(date_sub());
repository.register(day());
repository.register(dayName());
Expand Down Expand Up @@ -148,11 +150,10 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(time_to_sec());
repository.register(timediff());
repository.register(timestamp());
repository.register(to_days());
repository.register(utc_date());
repository.register(utc_time());
repository.register(utc_timestamp());
repository.register(date_format());
repository.register(to_days());
repository.register(unix_timestamp());
repository.register(week(BuiltinFunctionName.WEEK));
repository.register(week(BuiltinFunctionName.WEEK_OF_YEAR));
Expand Down Expand Up @@ -962,7 +963,6 @@ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, Ex
return new ExprDatetimeValue(
zonedDateTime.withZoneSameInstant(convertedToTz).toLocalDateTime());


// Catches exception for invalid timezones.
// ex. "+0:00" is an invalid timezone and would result in this exception being thrown.
} catch (ExpressionEvaluationException | DateTimeException e) {
Expand Down Expand Up @@ -1010,7 +1010,6 @@ private ExprValue exprDateDiff(FunctionProperties functionProperties,
private ExprValue exprDateTime(ExprValue dateTime, ExprValue timeZone) {
String defaultTimeZone = TimeZone.getDefault().getID();


try {
LocalDateTime ldtFormatted =
LocalDateTime.parse(dateTime.stringValue(), DATE_TIME_FORMATTER_STRICT_WITH_TZ);
Expand Down Expand Up @@ -1124,7 +1123,7 @@ private ExprValue exprFromUnixTime(ExprValue time) {
private LocalDateTime exprFromUnixTimeImpl(ExprValue time) {
return LocalDateTime.ofInstant(
Instant.ofEpochSecond((long)Math.floor(time.doubleValue())),
ZoneId.of("UTC"))
UTC_ZONE_ID)
.withNano((int)((time.doubleValue() % 1) * 1E9));
}

Expand Down Expand Up @@ -1439,7 +1438,7 @@ private ExprValue exprUtcTime(FunctionProperties functionProperties) {
*/
private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) {
var zdt = ZonedDateTime.now(functionProperties.getQueryStartClock())
.withZoneSameInstant(ZoneId.of("UTC"));
.withZoneSameInstant(UTC_ZONE_ID);
return new ExprDatetimeValue(zdt.toLocalDateTime());
}

Expand Down
Loading

0 comments on commit a6b4f48

Please sign in to comment.