Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor clean up of datetime and other classes (#198) #1310

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -93,12 +89,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 @@ -501,7 +501,6 @@ public static FunctionExpression to_seconds(Expression... expressions) {
return to_seconds(FunctionProperties.None, expressions);
}


public static FunctionExpression week(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEK, expressions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.opensearch.sql.utils.DateTimeFormatters.SHORT_DATE_LENGTH;
import static org.opensearch.sql.utils.DateTimeFormatters.SINGLE_DIGIT_MONTH_DATE_LENGTH;
import static org.opensearch.sql.utils.DateTimeFormatters.SINGLE_DIGIT_YEAR_DATE_LENGTH;
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 All @@ -67,14 +68,12 @@
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAmount;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.sql.data.model.ExprDateValue;
import org.opensearch.sql.data.model.ExprDatetimeValue;
Expand All @@ -84,11 +83,9 @@
import org.opensearch.sql.data.model.ExprNullValue;
import org.opensearch.sql.data.model.ExprStringValue;
import org.opensearch.sql.data.model.ExprTimeValue;
import org.opensearch.sql.data.model.ExprTimestampValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.exception.ExpressionEvaluationException;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.function.BuiltinFunctionName;
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.expression.function.DefaultFunctionResolver;
Expand Down Expand Up @@ -191,6 +188,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 @@ -236,13 +234,12 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(timestamp());
repository.register(timestampadd());
repository.register(timestampdiff());
repository.register(utc_date());
repository.register(utc_time());
repository.register(utc_timestamp());
repository.register(date_format());
repository.register(to_days());
repository.register(to_seconds());
repository.register(unix_timestamp());
repository.register(utc_date());
repository.register(utc_time());
repository.register(utc_timestamp());
repository.register(week(BuiltinFunctionName.WEEK));
repository.register(week(BuiltinFunctionName.WEEKOFYEAR));
repository.register(week(BuiltinFunctionName.WEEK_OF_YEAR));
Expand Down Expand Up @@ -1286,7 +1283,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 @@ -1334,7 +1330,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 @@ -1490,7 +1485,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 @@ -1994,7 +1989,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