Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Added datetime data type and basic functions for the conversion from string type to date and time types #694

Merged
merged 16 commits into from
Aug 26, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -34,19 +36,15 @@
*/
@RequiredArgsConstructor
public class ExprDateValue extends AbstractExprValue {
/**
* todo. only support UTC now.
*/
private static final ZoneId ZONE = ZoneId.of("UTC");
private final Instant date;

private final LocalDate date;

/**
* Constructor of ExprDateValue.
*/
public ExprDateValue(String date) {
try {
LocalDate localDate = LocalDate.parse(date);
this.date = localDate.atStartOfDay(ZONE).toInstant();
this.date = LocalDate.parse(date);
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("date:%s in unsupported format, please use "
+ "yyyy-MM-dd", date));
Expand All @@ -55,7 +53,7 @@ public ExprDateValue(String date) {

@Override
public String value() {
return DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZONE).format(date);
return DateTimeFormatter.ISO_LOCAL_DATE.format(date);
}

@Override
Expand All @@ -64,8 +62,23 @@ public ExprType type() {
}

@Override
public ZonedDateTime dateValue() {
return date.atZone(ZONE);
public LocalDate dateValue() {
return date;
}

@Override
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(), ZoneId.systemDefault()).toInstant();
}

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

@Override
public int compare(ExprValue other) {
return date.compareTo(other.dateValue().toInstant());
return date.compareTo(other.dateValue());
}

@Override
public boolean equal(ExprValue other) {
return date.atZone(ZONE).equals(other.dateValue());
return date.equals(other.dateValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
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.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ExprDatetimeValue extends AbstractExprValue {
private static final DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss");
private final LocalDateTime datetime;

/**
* Constructor with datetime string as input.
*/
public ExprDatetimeValue(String datetime) {
try {
this.datetime = LocalDateTime.parse(datetime, formatter);
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss", datetime));
}
}

@Override
public LocalDateTime datetimeValue() {
return datetime;
}

@Override
public LocalDate dateValue() {
return datetime.toLocalDate();
}

@Override
public LocalTime timeValue() {
return datetime.toLocalTime();
}

@Override
public Instant timestampValue() {
return ZonedDateTime.of(datetime, ZoneId.of("UTC")).toInstant();
}

@Override
public int compare(ExprValue other) {
return datetime.compareTo(other.datetimeValue());
}

@Override
public boolean equal(ExprValue other) {
return datetime.equals(other.datetimeValue());
}
dai-chen marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String value() {
return String.format("%s %s", DateTimeFormatter.ISO_DATE.format(datetime),
DateTimeFormatter.ISO_TIME.format(datetime));
}

@Override
public ExprType type() {
return ExprCoreType.DATETIME;
}

@Override
public String toString() {
return String.format("DATETIME '%s'", value());
}

@Override
public int hashCode() {
return Objects.hashCode(datetime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
*/
@RequiredArgsConstructor
public class ExprTimeValue extends AbstractExprValue {
/**
* todo. only support UTC now.
*/
private static final ZoneId ZONE = ZoneId.of("UTC");
private final LocalTime time;

/**
Expand All @@ -53,7 +49,7 @@ public ExprTimeValue(String time) {

@Override
public String value() {
return DateTimeFormatter.ISO_LOCAL_TIME.withZone(ZONE).format(time);
return DateTimeFormatter.ISO_LOCAL_TIME.format(time);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

/**
Expand Down Expand Up @@ -74,6 +75,21 @@ public Instant timestampValue() {
return timestamp;
}

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

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

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

@Override
public String toString() {
return String.format("TIMESTAMP '%s'", value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.amazon.opendistroforelasticsearch.sql.storage.bindingtuple.BindingTuple;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAmount;
Expand Down Expand Up @@ -142,11 +144,19 @@ default LocalTime timeValue() {
/**
* Get date value.
*/
default ZonedDateTime dateValue() {
default LocalDate dateValue() {
throw new ExpressionEvaluationException(
"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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException;
import com.google.common.annotations.VisibleForTesting;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
Expand Down Expand Up @@ -128,6 +133,8 @@ 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 Expand Up @@ -166,12 +173,33 @@ public static Boolean getBooleanValue(ExprValue exprValue) {
}

/**
* Get {@link ZonedDateTime} from ExprValue of Date type.
* Get {@link LocalDate} from ExprValue of Date type.
*/
public static ZonedDateTime getDateValue(ExprValue exprValue) {
public static LocalDate getDateValue(ExprValue exprValue) {
return exprValue.dateValue();
}

/**
* Get {@link LocalTime} from ExprValue of Time type.
*/
public static LocalTime getTimeValue(ExprValue exprValue) {
chloe-zh marked this conversation as resolved.
Show resolved Hide resolved
return exprValue.timeValue();
}

/**
* Get {@link LocalDateTime} from ExprValue of Datetime type.
*/
public static LocalDateTime getDatetimeValue(ExprValue exprValue) {
return exprValue.datetimeValue();
}

/**
* Get {@link Instant} from ExprValue of Timestamp type.
*/
public static Instant getTimestampValue(ExprValue exprValue) {
return exprValue.timestampValue();
}

public static TemporalAmount getIntervalValue(ExprValue exprValue) {
return exprValue.intervalValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum ExprCoreType implements ExprType {
TIMESTAMP,
DATE,
TIME,
DATETIME,
INTERVAL,

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,19 @@ public FunctionExpression multiply(Expression... expressions) {
}

public FunctionExpression dayofmonth(Expression... expressions) {
return (FunctionExpression)
repository.compile(BuiltinFunctionName.DAYOFMONTH.getName(), Arrays.asList(expressions));
return function(BuiltinFunctionName.DAYOFMONTH, expressions);
}

public FunctionExpression date(Expression... expressions) {
return function(BuiltinFunctionName.DATE, expressions);
}

public FunctionExpression time(Expression... expressions) {
return function(BuiltinFunctionName.TIME, expressions);
}

public FunctionExpression timestamp(Expression... expressions) {
return function(BuiltinFunctionName.TIMESTAMP, expressions);
}

public FunctionExpression divide(Expression... expressions) {
Expand Down
Loading