Browse Source

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

Andrei Stefan 6 years ago
parent
commit
dbb93310b0

+ 2 - 1
docs/reference/sql/functions/date-time.asciidoc

@@ -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.
 

+ 1 - 1
x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTime.java

@@ -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

+ 13 - 0
x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTimeTests.java

@@ -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());
+    }
 }