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).
This commit is contained in:
parent
9f75b6ea58
commit
1236461e3e
|
@ -11,6 +11,10 @@ import org.elasticsearch.xpack.sql.proto.Protocol;
|
|||
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() {}
|
||||
|
@ -18,4 +22,16 @@ public class 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)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,9 @@ package org.elasticsearch.xpack.sql.type;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -79,7 +79,6 @@ public class DataTypeConversionTests extends ESTestCase {
|
|||
assertEquals("cannot cast [0xff] to [Long]", e.getMessage());
|
||||
}
|
||||
|
||||
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/35683")
|
||||
public void testConversionToDate() {
|
||||
DataType to = DATE;
|
||||
{
|
||||
|
@ -111,7 +110,7 @@ public class DataTypeConversionTests extends ESTestCase {
|
|||
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)));
|
||||
|
|
Loading…
Reference in New Issue