You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2021/10/18 13:40:42 UTC

[GitHub] [cassandra] blerer commented on a change in pull request #1275: [CASSANDRA- 17029] Added function toDate to convert from toDate

blerer commented on a change in pull request #1275:
URL: https://github.com/apache/cassandra/pull/1275#discussion_r730930303



##########
File path: src/java/org/apache/cassandra/cql3/functions/TimeFcts.java
##########
@@ -162,6 +164,28 @@ public ByteBuffer execute(ProtocolVersion protocolVersion, List<ByteBuffer> para
        };
    }
 
+    /**
+     * Creates a function that convert a value of the specified type into a <code>DATE</code>.
+     * @param type the temporal type
+     * @return a function that convert a value of the specified type into a <code>DATE</code>.
+     */
+    public static final NativeScalarFunction toDate(LongType type)
+    {
+        return new NativeScalarFunction("todate", SimpleDateType.instance, type)
+        {
+            public ByteBuffer execute(ProtocolVersion protocolVersion, List<ByteBuffer> parameters)
+            {
+                ByteBuffer bb = parameters.get(0);
+                if (bb == null || !bb.hasRemaining())
+                    return null;
+
+                long milliSeconds = new BigInteger(bb.array()).longValue();

Review comment:
       You should use `LongType.instance.toLong(bb)`. There is no guaranty that the `ByteBuffer `has an array and you should rely on the C* type to avoid bad surprises.  

##########
File path: test/unit/org/apache/cassandra/cql3/functions/TimeFctsTest.java
##########
@@ -141,6 +142,16 @@ public void testTimestampToDate()
         assertEquals(DATE.toInstant().toEpochMilli(), SimpleDateType.instance.toTimeInMillis(output));
     }
 
+    @Test
+    public void testBigIntegerToDate()
+    {
+        long milliSeconds = DATE_TIME.toInstant().toEpochMilli();
+
+        ByteBuffer input = IntegerType.instance.fromString(Long.toString(milliSeconds));

Review comment:
       You shouls use `LongType.instance.decompose(milliseconds)`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org