Add the currentTimestamp, currentDate, currentTime and currentTimeUUID functions

patch by Benjamin Lerer; reviewed by Alex Petrov for CASSANDRA-13132
This commit is contained in:
Benjamin Lerer 2017-03-07 17:49:14 +01:00
parent ae0604f08b
commit f3cd28d859
8 changed files with 53 additions and 7 deletions

View File

@ -1,4 +1,5 @@
4.0
* Add the currentTimestamp, currentDate, currentTime and currentTimeUUID functions (CASSANDRA-13132)
* Remove config option index_interval (CASSANDRA-10671)
* Reduce lock contention for collection types and serializers (CASSANDRA-13271)
* Make it possible to override MessagingService.Verb ids (CASSANDRA-13283)

View File

@ -18,6 +18,8 @@ using the provided 'sstableupgrade' tool.
New features
------------
- The currentTimestamp, currentDate, currentTime and currentTimeUUID functions have been added.
See CASSANDRA-13132
- Support for arithmetic operations between `timestamp`/`date` and `duration` has been added.
See CASSANDRA-11936
- Support for arithmetic operations on number has been added. See CASSANDRA-11935

View File

@ -21,6 +21,14 @@ Changes
The following describes the changes in each version of CQL.
3.4.5
^^^^^
- Adds support for arithmetic operators (:jira:`11935`)
- Adds support for ``+`` and ``-`` operations on dates (:jira:`11936`)
- Adds ``currentTimestamp``, ``currentDate``, ``currentTime`` and ``currentTimeUUID`` functions (:jira:`13132`)
3.4.4
^^^^^
@ -34,7 +42,6 @@ The following describes the changes in each version of CQL.
- Support for ``GROUP BY`` (:jira:`10707`).
- Adds a ``DEFAULT UNSET`` option for ``INSERT JSON`` to ignore omitted columns (:jira:`11424`).
- Allows ``null`` as a legal value for TTL on insert and update. It will be treated as equivalent to inserting a 0 (:jira:`12216`).
- Adds support for arithmetic operators (:jira:`11935`)
3.4.2
^^^^^

View File

@ -143,6 +143,8 @@ time the function is invoked. Note that this method is useful for insertion but
will never return any result by design, since the value returned by ``now()`` is guaranteed to be unique.
``currentTimeUUID`` is an alias of ``now``.
``minTimeuuid`` and ``maxTimeuuid``
###################################
@ -164,8 +166,29 @@ maxTimeuuid('2013-01-01 00:05+0000')``.
particular, the value returned by these 2 methods will not be unique. This means you should only use those methods
for querying (as in the example above). Inserting the result of those methods is almost certainly *a bad idea*.
Datetime functions
``````````````````
Retrieving the current date/time
################################
The following functions can be used to retrieve the date/time at the time where the function is invoked:
===================== ===============
Function name Output type
===================== ===============
``currentTimestamp`` ``timestamp``
``currentDate`` ``date``
``currentTime`` ``time``
``currentTimeUUID`` ``timeUUID``
===================== ===============
For example the last 2 days of data can be retrieved using::
SELECT * FROM myTable WHERE date >= currentDate() - 2d
Time conversion functions
`````````````````````````
#########################
A number of functions are provided to “convert” a ``timeuuid``, a ``timestamp`` or a ``date`` into another ``native``
type.

View File

@ -63,7 +63,7 @@ import static org.apache.cassandra.cql3.statements.RequestValidations.checkTrue;
public class QueryProcessor implements QueryHandler
{
public static final CassandraVersion CQL_VERSION = new CassandraVersion("3.4.4");
public static final CassandraVersion CQL_VERSION = new CassandraVersion("3.4.5");
public static final QueryProcessor instance = new QueryProcessor();

View File

@ -36,7 +36,11 @@ public abstract class TimeFcts
public static Collection<Function> all()
{
return ImmutableList.of(now(TimeUUIDType.instance),
return ImmutableList.of(now("now", TimeUUIDType.instance),
now("currenttimeuuid", TimeUUIDType.instance),
now("currenttimestamp", TimestampType.instance),
now("currentdate", SimpleDateType.instance),
now("currenttime", TimeType.instance),
minTimeuuidFct,
maxTimeuuidFct,
dateOfFct,
@ -50,9 +54,9 @@ public abstract class TimeFcts
toTimestamp(SimpleDateType.instance));
}
public static final Function now(final TemporalType<?> type)
public static final Function now(final String name, final TemporalType<?> type)
{
return new NativeScalarFunction("now", type)
return new NativeScalarFunction(name, type)
{
@Override
public ByteBuffer execute(ProtocolVersion protocolVersion, List<ByteBuffer> parameters)

View File

@ -18,6 +18,9 @@
package org.apache.cassandra.db.marshal;
import java.nio.ByteBuffer;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.apache.cassandra.cql3.Constants;
import org.apache.cassandra.cql3.Term;
@ -75,4 +78,10 @@ public class TimeType extends TemporalType<Long>
{
return TimeSerializer.instance;
}
@Override
public ByteBuffer now()
{
return decompose(LocalTime.now(ZoneOffset.UTC).toNanoOfDay());
}
}

View File

@ -486,7 +486,7 @@ public final class Schema
public Collection<Function> getFunctions(FunctionName name)
{
if (!name.hasKeyspace())
throw new IllegalArgumentException(String.format("Function name must be fully quallified: got %s", name));
throw new IllegalArgumentException(String.format("Function name must be fully qualified: got %s", name));
KeyspaceMetadata ksm = getKeyspaceMetadata(name.keyspace);
return ksm == null