Skip to content

Commit

Permalink
SQL: Make sure now() always uses milliseconds precision (#36877)
Browse files Browse the repository at this point in the history
* This change is to account for different system clock implementations
or different Java versions (for Java 8, milliseconds precision is used;
for Java 9+ a system specific clock implementation is used which can
have greater precision than what we need here).
  • Loading branch information
astefan committed Dec 20, 2018
1 parent 9f75b6e commit 1236461
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@
import org.elasticsearch.xpack.sql.session.Configuration;
import org.elasticsearch.xpack.sql.util.DateUtils;

import java.time.Clock;
import java.time.Duration;
import java.time.ZonedDateTime;

public class TestUtils {

private TestUtils() {}

public static final Configuration TEST_CFG = new Configuration(DateUtils.UTC, Protocol.FETCH_SIZE,
Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, null, Mode.PLAIN, null, null);

/**
* Returns the current UTC date-time with milliseconds precision.
* In Java 9+ (as opposed to Java 8) the {@code Clock} implementation uses system's best clock implementation (which could mean
* that the precision of the clock can be milliseconds, microseconds or nanoseconds), whereas in Java 8
* {@code System.currentTimeMillis()} is always used. To account for these differences, this method defines a new {@code Clock}
* which will offer a value for {@code ZonedDateTime.now()} set to always have milliseconds precision.
*
* @return {@link ZonedDateTime} instance for the current date-time with milliseconds precision in UTC
*/
public static final ZonedDateTime now() {
return ZonedDateTime.now(Clock.tick(Clock.system(DateUtils.UTC), Duration.ofMillis(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.TestUtils;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.type.DataTypeConversion.Conversion;
import org.elasticsearch.xpack.sql.util.DateUtils;

import java.time.ZonedDateTime;

Expand Down Expand Up @@ -79,7 +79,6 @@ public void testConversionToLong() {
assertEquals("cannot cast [0xff] to [Long]", e.getMessage());
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/35683")
public void testConversionToDate() {
DataType to = DATE;
{
Expand Down Expand Up @@ -111,7 +110,7 @@ public void testConversionToDate() {
assertEquals(dateTime(18000000L), conversion.convert("1970-01-01T00:00:00-05:00"));

// double check back and forth conversion
ZonedDateTime dt = ZonedDateTime.now(DateUtils.UTC);
ZonedDateTime dt = TestUtils.now();
Conversion forward = conversionFor(DATE, KEYWORD);
Conversion back = conversionFor(KEYWORD, DATE);
assertEquals(dt, back.convert(forward.convert(dt)));
Expand Down

0 comments on commit 1236461

Please sign in to comment.