Skip to content

Commit

Permalink
SQL: change the default precision for CURRENT_TIMESTAMP function (#39391
Browse files Browse the repository at this point in the history
)

(cherry picked from commit dbb9331)
  • Loading branch information
astefan committed Feb 27, 2019
1 parent bf7e8a6 commit 3a84ccc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/reference/sql/functions/date-time.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected NodeInfo<CurrentDateTime> info() {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
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;
Expand Down Expand Up @@ -62,4 +64,15 @@ public void testNanoPrecision() {
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());
}
}

0 comments on commit 3a84ccc

Please sign in to comment.