cassandracqlcqlshscylla

How to convert the value returned from writetime() into datetime in Scylla CQL?


I would like to convert the integer timestamp returned by writetime() to a readable datetime format in Scylla cqlsh.

After some Googling I found the following answer that works for Cassandra: TOTIMESTAMP(MINTIMEUUID(WRITETIME(val)/1000))

Similarly, I tried the same in Scylla CQL, but the usage of the arithmetic operator throws a syntax error: SELECT WRITETIME(val)/1000 ...
SyntaxException: line 1:26 no viable alternative at input ''

I am a little confused as the simple select statement SELECT val/1000 fails with the same error.


Solution

  • Based on the SELECT docs:

    A SELECT statement contains at least a selection clause ...

    Selection clause

    The select_clause determines which columns need to be queried and returned in the result-set, as well as any transformation to apply to this result before returning. It consists of a comma-separated list of selectors or, alternatively, of the wildcard character (*) to select all the columns defined in the table.

    Selectors

    A selector can be one of the following:

    • A column name of the table selected to retrieve the values for that column.
    • A casting, which allows you to convert a nested selector to a (compatible) type.
    • A function call, where the arguments are selector themselves.
    • A call to the COUNT, which counts all non-null results.

    And the Support arithmetic operators issue at github Scylla currently does not support literals and arithmetic functions in the SELECT.

    One workaround to try is to enable the experimental features and UDF functions:

    # scylla.yaml
    experimental_features:
        - udf
    
    enable_user_defined_functions: true
    

    and use Lua scripting (see the example in the docs). For example the following worked for me:

    CREATE OR REPLACE FUNCTION divBy1000(dividend bigint)
      RETURNS NULL ON NULL INPUT
      RETURNS bigint
      LANGUAGE LUA
      AS 'return dividend/1000;';
    
    select blobAsTimestamp(bigintAsBlob(divBy1000(writetime(average_size))))
    from monkeyspecies;
    

    Or you can handle transformations on the client side (also WHERE clause is a little bit less restrictive as far as I remember).