Skip to content

Commit 1236461

Browse files
authored
SQL: Make sure now() always uses milliseconds precision (#36877)
* 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).
1 parent 9f75b6e commit 1236461

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/TestUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,27 @@
1111
import org.elasticsearch.xpack.sql.session.Configuration;
1212
import org.elasticsearch.xpack.sql.util.DateUtils;
1313

14+
import java.time.Clock;
15+
import java.time.Duration;
16+
import java.time.ZonedDateTime;
17+
1418
public class TestUtils {
1519

1620
private TestUtils() {}
1721

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

25+
/**
26+
* Returns the current UTC date-time with milliseconds precision.
27+
* In Java 9+ (as opposed to Java 8) the {@code Clock} implementation uses system's best clock implementation (which could mean
28+
* that the precision of the clock can be milliseconds, microseconds or nanoseconds), whereas in Java 8
29+
* {@code System.currentTimeMillis()} is always used. To account for these differences, this method defines a new {@code Clock}
30+
* which will offer a value for {@code ZonedDateTime.now()} set to always have milliseconds precision.
31+
*
32+
* @return {@link ZonedDateTime} instance for the current date-time with milliseconds precision in UTC
33+
*/
34+
public static final ZonedDateTime now() {
35+
return ZonedDateTime.now(Clock.tick(Clock.system(DateUtils.UTC), Duration.ofMillis(1)));
36+
}
2137
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/DataTypeConversionTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import org.elasticsearch.test.ESTestCase;
99
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
10+
import org.elasticsearch.xpack.sql.TestUtils;
1011
import org.elasticsearch.xpack.sql.expression.Literal;
1112
import org.elasticsearch.xpack.sql.type.DataTypeConversion.Conversion;
12-
import org.elasticsearch.xpack.sql.util.DateUtils;
1313

1414
import java.time.ZonedDateTime;
1515

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

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

113112
// double check back and forth conversion
114-
ZonedDateTime dt = ZonedDateTime.now(DateUtils.UTC);
113+
ZonedDateTime dt = TestUtils.now();
115114
Conversion forward = conversionFor(DATE, KEYWORD);
116115
Conversion back = conversionFor(KEYWORD, DATE);
117116
assertEquals(dt, back.convert(forward.convert(dt)));

0 commit comments

Comments
 (0)