SQL: change the default precision for CURRENT_TIMESTAMP function (#39391)

(cherry picked from commit dbb93310b083226c96e4bde3eef0079eb01cbca9)
This commit is contained in:
Andrei Stefan 2019-02-27 09:48:10 +02:00 committed by Andrei Stefan
parent 4deb69e9e4
commit 542e2c55f6
3 changed files with 16 additions and 2 deletions

View File

@ -153,7 +153,8 @@ CURRENT_TIMESTAMP(precision <1>)
Returns the date/time when the current query reached the server.
As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
parameter for rounding the second fractional digits (nanoseconds).
parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
meaning a milliseconds precision current date/time will be returned.
This method always returns the same value for its every occurrence within the same query.

View File

@ -34,7 +34,7 @@ public class CurrentDateTime extends CurrentFunction {
}
static ZonedDateTime nanoPrecision(ZonedDateTime zdt, Expression precisionExpression) {
int precision = precisionExpression != null ? Foldables.intValueOf(precisionExpression) : 0;
int precision = precisionExpression != null ? Foldables.intValueOf(precisionExpression) : 3;
int nano = zdt.getNano();
if (precision >= 0 && precision < 10) {
// remove the remainder

View File

@ -9,10 +9,12 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.TestUtils;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.session.Configuration;
import org.elasticsearch.xpack.sql.tree.AbstractNodeTestCase;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.util.Objects;
import static org.elasticsearch.xpack.sql.tree.Source.EMPTY;
@ -62,4 +64,15 @@ public class CurrentDateTimeTests extends AbstractNodeTestCase<CurrentDateTime,
assertEquals(123_456_780, CurrentDateTime.nanoPrecision(zdt, Literal.of(EMPTY, 8)).getNano());
assertEquals(123_456_789, CurrentDateTime.nanoPrecision(zdt, Literal.of(EMPTY, 9)).getNano());
}
public void testDefaultPrecision() {
Configuration configuration = TestUtils.randomConfiguration();
// null precision means default precision
CurrentDateTime cdt = new CurrentDateTime(EMPTY, null, configuration);
ZonedDateTime now = configuration.now();
assertEquals(now.get(ChronoField.MILLI_OF_SECOND), ((ZonedDateTime) cdt.fold()).get(ChronoField.MILLI_OF_SECOND));
ZonedDateTime zdt = ZonedDateTime.parse("2019-02-26T12:34:56.123456789Z");
assertEquals(123_000_000, CurrentDateTime.nanoPrecision(zdt, null).getNano());
}
}