bbotella commented on code in PR #3741:
URL: https://github.com/apache/cassandra/pull/3741#discussion_r1884601141


##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";

Review Comment:
   Should we be more specific with the naming and have it like 
`human_format_time`?



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            if (arguments.size() == 1)
+            {
+                Pair<Long, String> convertedValue = convertValue(value);
+                return UTF8Type.instance.fromString(convertedValue.left + " " 
+ convertedValue.right);
+            }
+
+            TimeUnit sourceUnit;
+            TimeUnit targetUnit;
+            String targetUnitAsString;
+
+            if (arguments.size() == 2)
+            {
+                sourceUnit = MILLISECONDS;
+                targetUnitAsString = arguments.get(1);
+            }
+            else

Review Comment:
   Should we also check for `arguments.size() > 3`? I think we should also 
return an `InvalidRequestException on that case instead of silently ignoring 
the extra args.



##########
doc/modules/cassandra/pages/developing/cql/functions.adoc:
##########
@@ -288,6 +288,159 @@ A number of functions allow to obtain the similarity 
score between vectors of fl
 
 include::cassandra:partial$vector-search/vector_functions.adoc[]
 
+[[human-helper-functions]]
+==== Human helper functions
+
+For user's convenience, there are currently two functions which are converting 
values to more human-friendly
+represetations.
+
+==== format_bytes
+
+This function looks at values in a column as if it was in bytes, and it will 
convert it to whatever a user pleases. There are three ways how to call this 
function

Review Comment:
   nit: final `.`



##########
doc/modules/cassandra/pages/developing/cql/functions.adoc:
##########
@@ -288,6 +288,159 @@ A number of functions allow to obtain the similarity 
score between vectors of fl
 
 include::cassandra:partial$vector-search/vector_functions.adoc[]
 
+[[human-helper-functions]]
+==== Human helper functions
+
+For user's convenience, there are currently two functions which are converting 
values to more human-friendly
+represetations.
+
+==== format_bytes
+
+This function looks at values in a column as if it was in bytes, and it will 
convert it to whatever a user pleases. There are three ways how to call this 
function
+
+Let's have this table:
+
+[source,cql]
+----
+cqlsh> select * from ks.tb ;
+
+ id | val
+----+----------------
+  5 |          60000
+  1 |        1234234
+  2 | 12342341234234
+  4 |          60001
+  7 |           null
+  6 |             43
+  3 |         123423
+
+----
+
+with schema
+
+[source,cql]
+----
+CREATE TABLE ks.tb (
+    id int PRIMARY KEY,
+    val bigint
+)
+----
+
+Imagine that we wanted to look at `val` values as if they were in mebibytes. 
We would like to have more human-friendly output in order to not visually 
divide the values by 1024 in order to get them in respective bigger units. The 
following function call may take just a column itself as an argument, and it 
will

Review Comment:
   TIL what a mebibyte is :-)
   https://simple.wikipedia.org/wiki/Mebibyte



##########
test/unit/org/apache/cassandra/cql3/functions/FormatBytesFctTest.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.CQL3Type;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+
+import static java.util.List.of;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class FormatBytesFctTest extends CQLTester

Review Comment:
   These tests look like could benefit from the usage of QuickTheories. There 
are other tests in the code base using it. Maybe it is worth taking a look to 
increase coverage against unexpected inputs.
   
   https://github.com/quicktheories/QuickTheories



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            if (arguments.size() == 1)
+            {
+                Pair<Long, String> convertedValue = convertValue(value);
+                return UTF8Type.instance.fromString(convertedValue.left + " " 
+ convertedValue.right);
+            }
+
+            TimeUnit sourceUnit;
+            TimeUnit targetUnit;
+            String targetUnitAsString;
+
+            if (arguments.size() == 2)
+            {
+                sourceUnit = MILLISECONDS;
+                targetUnitAsString = arguments.get(1);
+            }
+            else
+            {
+                sourceUnit = validateUnit(arguments.get(1));
+                targetUnitAsString = arguments.get(2);
+            }
+
+            targetUnit = validateUnit(targetUnitAsString);
+
+            long convertedValue = convertValue(value, sourceUnit, targetUnit);
+            return UTF8Type.instance.fromString(convertedValue + " " + 
targetUnitAsString);
+        }
+
+        private TimeUnit validateUnit(String unitAsString)
+        {
+            try
+            {
+                return DurationSpec.fromSymbol(unitAsString);
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidRequestException(ex.getMessage());
+            }
+        }
+
+        private Pair<Long, String> convertValue(long valueToConvert)
+        {
+            long value;
+            if ((value = convertValue(valueToConvert, MILLISECONDS, DAYS)) > 0)
+                return Pair.create(value, "d");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
HOURS)) > 0)
+                return Pair.create(value, "h");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
MINUTES)) > 0)
+                return Pair.create(value, "m");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
SECONDS)) > 0)
+                return Pair.create(value, "s");
+            else
+                return Pair.create(valueToConvert, "ms");
+        }
+
+        private long convertValue(long valueToConvert, TimeUnit sourceUnit, 
TimeUnit targetUnit)
+        {
+            Duration duration = DurationUtils.toDuration(valueToConvert, 
sourceUnit);
+
+            if (targetUnit == NANOSECONDS)
+                return x(duration::toNanos);
+            if (targetUnit == MICROSECONDS)
+                return x(() -> duration.toNanos() / 1000);
+            else if (targetUnit == MILLISECONDS)
+                return x(duration::toMillis);
+            else if (targetUnit == SECONDS)
+                return x(duration::toSeconds);
+            else if (targetUnit == MINUTES)
+                return x(duration::toMinutes);
+            else if (targetUnit == HOURS)
+                return x(duration::toHours);
+            else if (targetUnit == DAYS)
+                return x(duration::toDays);
+            else
+                throw new InvalidRequestException("unsupported target unit " + 
targetUnit);

Review Comment:
   This makes me wonder, should we move the validate unit calls inside this 
method to avoid having to call them from the main execute method?



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)

Review Comment:
   Is there a reason we don't want to support negative times? (as in before 
now?)



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            if (arguments.size() == 1)
+            {
+                Pair<Long, String> convertedValue = convertValue(value);
+                return UTF8Type.instance.fromString(convertedValue.left + " " 
+ convertedValue.right);
+            }
+
+            TimeUnit sourceUnit;
+            TimeUnit targetUnit;
+            String targetUnitAsString;
+
+            if (arguments.size() == 2)
+            {
+                sourceUnit = MILLISECONDS;
+                targetUnitAsString = arguments.get(1);
+            }
+            else
+            {
+                sourceUnit = validateUnit(arguments.get(1));
+                targetUnitAsString = arguments.get(2);
+            }
+
+            targetUnit = validateUnit(targetUnitAsString);
+
+            long convertedValue = convertValue(value, sourceUnit, targetUnit);
+            return UTF8Type.instance.fromString(convertedValue + " " + 
targetUnitAsString);
+        }
+
+        private TimeUnit validateUnit(String unitAsString)
+        {
+            try
+            {
+                return DurationSpec.fromSymbol(unitAsString);
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidRequestException(ex.getMessage());
+            }
+        }
+
+        private Pair<Long, String> convertValue(long valueToConvert)
+        {
+            long value;
+            if ((value = convertValue(valueToConvert, MILLISECONDS, DAYS)) > 0)
+                return Pair.create(value, "d");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
HOURS)) > 0)
+                return Pair.create(value, "h");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
MINUTES)) > 0)
+                return Pair.create(value, "m");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
SECONDS)) > 0)
+                return Pair.create(value, "s");
+            else
+                return Pair.create(valueToConvert, "ms");
+        }
+
+        private long convertValue(long valueToConvert, TimeUnit sourceUnit, 
TimeUnit targetUnit)
+        {
+            Duration duration = DurationUtils.toDuration(valueToConvert, 
sourceUnit);
+
+            if (targetUnit == NANOSECONDS)
+                return x(duration::toNanos);
+            if (targetUnit == MICROSECONDS)
+                return x(() -> duration.toNanos() / 1000);
+            else if (targetUnit == MILLISECONDS)
+                return x(duration::toMillis);
+            else if (targetUnit == SECONDS)
+                return x(duration::toSeconds);
+            else if (targetUnit == MINUTES)
+                return x(duration::toMinutes);
+            else if (targetUnit == HOURS)
+                return x(duration::toHours);
+            else if (targetUnit == DAYS)
+                return x(duration::toDays);
+            else
+                throw new InvalidRequestException("unsupported target unit " + 
targetUnit);
+        }
+
+        // This has a short name to make above code more readable, same 
pattern as for DataStorageSpec
+        private long x(LongSupplier longSupplier)
+        {
+            try
+            {
+                return longSupplier.getAsLong();
+            }
+            catch (ArithmeticException ex)
+            {
+                return Long.MAX_VALUE;
+            }
+        }
+
+        public static FunctionFactory factory()
+        {
+            return new FunctionFactory(FUNCTION_NAME,
+                                       fixed(INT, TINYINT, SMALLINT, BIGINT, 
VARINT, ASCII, TEXT),
+                                       optional(fixed(ASCII)),
+                                       optional(fixed(ASCII)))
+            {
+                @Override
+                protected NativeFunction 
doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> 
receiverType)
+                {
+                    if (argTypes.isEmpty() || argTypes.size() > 3)
+                        throw invalidNumberOfArgumentsException();
+
+                    return new FormatTimeFct(argTypes.toArray(new 
AbstractType<?>[0]));
+                }
+            };
+        }
+    }
+
+    private static final DecimalFormat decimalFormat;
+
+    static
+    {
+        decimalFormat = new DecimalFormat("0");
+        decimalFormat.setRoundingMode(RoundingMode.DOWN);
+    }
+
+    /**
+     * Converts numeric value in a column to a size value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'B' and it will be converted to a 
value of a unit which is closest to it. E.g.
+     * If a value is (1024 * 1024 + 1) then the unit will be in MiB and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'B' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_bytes(val) = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'GiB')
+     * format_bytes(val, 'KiB', 'GiB')
+     * format_bytes(val, 'MiB') = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'GiB') = format_bytes(val, 'B', 'GiB')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_bytes(val, 'GiB', 'B')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: B, KiB, MiB, GiB
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     *
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatBytesFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_bytes";
+
+        private FormatBytesFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            DataStorageUnit sourceUnit;
+            DataStorageUnit targetUnit;
+
+            if (arguments.size() == 1)
+            {
+                sourceUnit = BYTES;
+
+                if (value > FileUtils.ONE_GIB)
+                    targetUnit = GIBIBYTES;
+                else if (value > FileUtils.ONE_MIB)
+                    targetUnit = MEBIBYTES;
+                else if (value > FileUtils.ONE_KIB)
+                    targetUnit = KIBIBYTES;
+                else
+                    targetUnit = BYTES;
+            }
+            else if (arguments.size() == 2)
+            {
+                sourceUnit = BYTES;
+                targetUnit = validateUnit(arguments.get(1));
+            }
+            else
+            {
+                sourceUnit = validateUnit(arguments.get(1));
+                targetUnit = validateUnit(arguments.get(2));
+            }
+
+            long convertedValue = convertValue(value, sourceUnit, targetUnit);
+
+            return UTF8Type.instance.fromString(convertedValue + " " + 
targetUnit.getSymbol());
+        }
+
+        private long convertValue(long valueToConvert, DataStorageUnit 
sourceUnit, DataStorageUnit targetUnit)
+        {
+            if (targetUnit == BYTES)
+                return sourceUnit.toBytes(valueToConvert);
+            else if (targetUnit == KIBIBYTES)
+                return sourceUnit.toKibibytes(valueToConvert);
+            else if (targetUnit == MEBIBYTES)
+                return sourceUnit.toMebibytes(valueToConvert);
+            else if (targetUnit == GIBIBYTES)
+                return sourceUnit.toGibibytes(valueToConvert);
+            else
+                throw new InvalidRequestException("unsupported target unit " + 
targetUnit);

Review Comment:
   Same as before: This makes me wonder, should we move the validate unit calls 
inside this method to avoid having to call them from the main execute method?



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            if (arguments.size() == 1)
+            {
+                Pair<Long, String> convertedValue = convertValue(value);
+                return UTF8Type.instance.fromString(convertedValue.left + " " 
+ convertedValue.right);
+            }
+
+            TimeUnit sourceUnit;
+            TimeUnit targetUnit;
+            String targetUnitAsString;
+
+            if (arguments.size() == 2)
+            {
+                sourceUnit = MILLISECONDS;
+                targetUnitAsString = arguments.get(1);
+            }
+            else
+            {
+                sourceUnit = validateUnit(arguments.get(1));
+                targetUnitAsString = arguments.get(2);
+            }
+
+            targetUnit = validateUnit(targetUnitAsString);
+
+            long convertedValue = convertValue(value, sourceUnit, targetUnit);
+            return UTF8Type.instance.fromString(convertedValue + " " + 
targetUnitAsString);
+        }
+
+        private TimeUnit validateUnit(String unitAsString)
+        {
+            try
+            {
+                return DurationSpec.fromSymbol(unitAsString);
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidRequestException(ex.getMessage());
+            }
+        }
+
+        private Pair<Long, String> convertValue(long valueToConvert)
+        {
+            long value;
+            if ((value = convertValue(valueToConvert, MILLISECONDS, DAYS)) > 0)
+                return Pair.create(value, "d");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
HOURS)) > 0)
+                return Pair.create(value, "h");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
MINUTES)) > 0)
+                return Pair.create(value, "m");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
SECONDS)) > 0)
+                return Pair.create(value, "s");
+            else
+                return Pair.create(valueToConvert, "ms");
+        }
+
+        private long convertValue(long valueToConvert, TimeUnit sourceUnit, 
TimeUnit targetUnit)
+        {
+            Duration duration = DurationUtils.toDuration(valueToConvert, 
sourceUnit);
+
+            if (targetUnit == NANOSECONDS)
+                return x(duration::toNanos);
+            if (targetUnit == MICROSECONDS)
+                return x(() -> duration.toNanos() / 1000);
+            else if (targetUnit == MILLISECONDS)
+                return x(duration::toMillis);
+            else if (targetUnit == SECONDS)
+                return x(duration::toSeconds);
+            else if (targetUnit == MINUTES)
+                return x(duration::toMinutes);
+            else if (targetUnit == HOURS)
+                return x(duration::toHours);
+            else if (targetUnit == DAYS)
+                return x(duration::toDays);
+            else
+                throw new InvalidRequestException("unsupported target unit " + 
targetUnit);
+        }
+
+        // This has a short name to make above code more readable, same 
pattern as for DataStorageSpec
+        private long x(LongSupplier longSupplier)
+        {
+            try
+            {
+                return longSupplier.getAsLong();
+            }
+            catch (ArithmeticException ex)
+            {
+                return Long.MAX_VALUE;
+            }
+        }
+
+        public static FunctionFactory factory()
+        {
+            return new FunctionFactory(FUNCTION_NAME,
+                                       fixed(INT, TINYINT, SMALLINT, BIGINT, 
VARINT, ASCII, TEXT),
+                                       optional(fixed(ASCII)),
+                                       optional(fixed(ASCII)))
+            {
+                @Override
+                protected NativeFunction 
doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> 
receiverType)
+                {
+                    if (argTypes.isEmpty() || argTypes.size() > 3)
+                        throw invalidNumberOfArgumentsException();
+
+                    return new FormatTimeFct(argTypes.toArray(new 
AbstractType<?>[0]));
+                }
+            };
+        }
+    }
+
+    private static final DecimalFormat decimalFormat;
+
+    static
+    {
+        decimalFormat = new DecimalFormat("0");
+        decimalFormat.setRoundingMode(RoundingMode.DOWN);
+    }
+
+    /**
+     * Converts numeric value in a column to a size value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'B' and it will be converted to a 
value of a unit which is closest to it. E.g.
+     * If a value is (1024 * 1024 + 1) then the unit will be in MiB and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'B' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_bytes(val) = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'GiB')
+     * format_bytes(val, 'KiB', 'GiB')
+     * format_bytes(val, 'MiB') = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'GiB') = format_bytes(val, 'B', 'GiB')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_bytes(val, 'GiB', 'B')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: B, KiB, MiB, GiB
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     *
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatBytesFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_bytes";
+
+        private FormatBytesFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            DataStorageUnit sourceUnit;
+            DataStorageUnit targetUnit;
+
+            if (arguments.size() == 1)
+            {
+                sourceUnit = BYTES;
+
+                if (value > FileUtils.ONE_GIB)
+                    targetUnit = GIBIBYTES;
+                else if (value > FileUtils.ONE_MIB)
+                    targetUnit = MEBIBYTES;
+                else if (value > FileUtils.ONE_KIB)
+                    targetUnit = KIBIBYTES;
+                else
+                    targetUnit = BYTES;
+            }
+            else if (arguments.size() == 2)
+            {
+                sourceUnit = BYTES;
+                targetUnit = validateUnit(arguments.get(1));
+            }
+            else

Review Comment:
   Same as before: Should we also check for arguments.size() > 3? I think we 
should also return an `InvalidRequestException` on that case instead of 
silently ignoring the extra args.



##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,426 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.functions;
+
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.text.DecimalFormat;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+
+import org.apache.commons.lang3.time.DurationUtils;
+
+import org.apache.cassandra.config.DataStorageSpec.DataStorageUnit;
+import org.apache.cassandra.config.DurationSpec;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Pair;
+
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MICROSECONDS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.BYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.GIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.KIBIBYTES;
+import static 
org.apache.cassandra.config.DataStorageSpec.DataStorageUnit.MEBIBYTES;
+import static org.apache.cassandra.cql3.CQL3Type.Native.ASCII;
+import static org.apache.cassandra.cql3.CQL3Type.Native.BIGINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.INT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.SMALLINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TEXT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.TINYINT;
+import static org.apache.cassandra.cql3.CQL3Type.Native.VARINT;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.fixed;
+import static org.apache.cassandra.cql3.functions.FunctionParameter.optional;
+
+public class FormatFcts
+{
+    public static void addFunctionsTo(NativeFunctions functions)
+    {
+        functions.add(FormatBytesFct.factory());
+        functions.add(FormatTimeFct.factory());
+    }
+
+    /**
+     * Converts numeric value in a column to a value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'ms' and it will be converted to a 
value of a unit which is closes to it. E.g.
+     * If a value is (60 * 1000 + 1) then the unit will be in minutes and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'ms' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_time(val)
+     * format_time(val, 'm') = format_time(val, 'ms', 'm')
+     * format_time(val, 's', 'm')
+     * format_time(val, 's', 'h')
+     * format_time(val, 's', 'd')
+     * format_time(val, 's') = format_bytes(val, 'ms', 's')
+     * format_time(val, 'h') = format_bytes(val, 'ms', 'h')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_time(val, 'm', 's')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: d, h, m, s, ms, us, µs, ns
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatTimeFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_time";
+
+        private FormatTimeFct(AbstractType<?>... argsTypes)
+        {
+            super(FUNCTION_NAME, UTF8Type.instance, argsTypes);
+        }
+
+        @Override
+        public ByteBuffer execute(Arguments arguments) throws 
InvalidRequestException
+        {
+            if (arguments.get(0) == null)
+                return null;
+
+            if (arguments.containsNulls())
+                throw new InvalidRequestException("none of the arguments may 
be null");
+
+            long value = getValue(arguments);
+
+            if (value < 0)
+                throw new InvalidRequestException("value must be 
non-negative");
+
+            if (arguments.size() == 1)
+            {
+                Pair<Long, String> convertedValue = convertValue(value);
+                return UTF8Type.instance.fromString(convertedValue.left + " " 
+ convertedValue.right);
+            }
+
+            TimeUnit sourceUnit;
+            TimeUnit targetUnit;
+            String targetUnitAsString;
+
+            if (arguments.size() == 2)
+            {
+                sourceUnit = MILLISECONDS;
+                targetUnitAsString = arguments.get(1);
+            }
+            else
+            {
+                sourceUnit = validateUnit(arguments.get(1));
+                targetUnitAsString = arguments.get(2);
+            }
+
+            targetUnit = validateUnit(targetUnitAsString);
+
+            long convertedValue = convertValue(value, sourceUnit, targetUnit);
+            return UTF8Type.instance.fromString(convertedValue + " " + 
targetUnitAsString);
+        }
+
+        private TimeUnit validateUnit(String unitAsString)
+        {
+            try
+            {
+                return DurationSpec.fromSymbol(unitAsString);
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidRequestException(ex.getMessage());
+            }
+        }
+
+        private Pair<Long, String> convertValue(long valueToConvert)
+        {
+            long value;
+            if ((value = convertValue(valueToConvert, MILLISECONDS, DAYS)) > 0)
+                return Pair.create(value, "d");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
HOURS)) > 0)
+                return Pair.create(value, "h");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
MINUTES)) > 0)
+                return Pair.create(value, "m");
+            else if ((value = convertValue(valueToConvert, MILLISECONDS, 
SECONDS)) > 0)
+                return Pair.create(value, "s");
+            else
+                return Pair.create(valueToConvert, "ms");
+        }
+
+        private long convertValue(long valueToConvert, TimeUnit sourceUnit, 
TimeUnit targetUnit)
+        {
+            Duration duration = DurationUtils.toDuration(valueToConvert, 
sourceUnit);
+
+            if (targetUnit == NANOSECONDS)
+                return x(duration::toNanos);
+            if (targetUnit == MICROSECONDS)
+                return x(() -> duration.toNanos() / 1000);
+            else if (targetUnit == MILLISECONDS)
+                return x(duration::toMillis);
+            else if (targetUnit == SECONDS)
+                return x(duration::toSeconds);
+            else if (targetUnit == MINUTES)
+                return x(duration::toMinutes);
+            else if (targetUnit == HOURS)
+                return x(duration::toHours);
+            else if (targetUnit == DAYS)
+                return x(duration::toDays);
+            else
+                throw new InvalidRequestException("unsupported target unit " + 
targetUnit);
+        }
+
+        // This has a short name to make above code more readable, same 
pattern as for DataStorageSpec
+        private long x(LongSupplier longSupplier)
+        {
+            try
+            {
+                return longSupplier.getAsLong();
+            }
+            catch (ArithmeticException ex)
+            {
+                return Long.MAX_VALUE;
+            }
+        }
+
+        public static FunctionFactory factory()
+        {
+            return new FunctionFactory(FUNCTION_NAME,
+                                       fixed(INT, TINYINT, SMALLINT, BIGINT, 
VARINT, ASCII, TEXT),
+                                       optional(fixed(ASCII)),
+                                       optional(fixed(ASCII)))
+            {
+                @Override
+                protected NativeFunction 
doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> 
receiverType)
+                {
+                    if (argTypes.isEmpty() || argTypes.size() > 3)
+                        throw invalidNumberOfArgumentsException();
+
+                    return new FormatTimeFct(argTypes.toArray(new 
AbstractType<?>[0]));
+                }
+            };
+        }
+    }
+
+    private static final DecimalFormat decimalFormat;
+
+    static
+    {
+        decimalFormat = new DecimalFormat("0");
+        decimalFormat.setRoundingMode(RoundingMode.DOWN);
+    }
+
+    /**
+     * Converts numeric value in a column to a size value of specified unit.
+     * <p>
+     * If the function call contains just one argument - value to convert - 
then it will be
+     * looked at as the value is of unit 'B' and it will be converted to a 
value of a unit which is closest to it. E.g.
+     * If a value is (1024 * 1024 + 1) then the unit will be in MiB and 
converted value will be 1.
+     * <p>
+     * If the function call contains two arguments - value to convert and a 
unit - then it will be looked at
+     * as the unit of such value is 'B' and it will be converted into the 
value of the second (unit) argument.
+     * <p>
+     * If the function call contains three arguments - value to covert and 
source and target unit - then the value
+     * will be considered of a unit of the second argument, and it will be 
converted
+     * into a value of the third (unit) argument.
+     * <p>
+     * Examples:
+     * <pre>
+     * format_bytes(val) = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'B', 'GiB')
+     * format_bytes(val, 'KiB', 'GiB')
+     * format_bytes(val, 'MiB') = format_bytes(val, 'B', 'MiB')
+     * format_bytes(val, 'GiB') = format_bytes(val, 'B', 'GiB')
+     * </pre>
+     * <p>
+     * It is possible to convert values of a bigger unit to values of a 
smaller unit, e.g. this is possible:
+     *
+     * <pre>
+     * format_bytes(val, 'GiB', 'B')
+     * </pre>
+     * <p>
+     * Values can be max of Long.MAX_VALUE, If the conversion produces 
overflown value, Long.MAX_VALUE will be returned.
+     * <p>
+     * Supported units are: B, KiB, MiB, GiB
+     * <p>
+     * Supported column types on which this function is possible to be applied:
+     * <pre>INT, TINYINT, SMALLINT, BIGINT, VARINT, ASCII, TEXT</pre>
+     * For ASCII and TEXT types, text of such column has to be a non-negative 
number.
+     * <p>
+     *
+     * The conversion of negative values is not supported.
+     */
+    public static class FormatBytesFct extends NativeScalarFunction
+    {
+        private static final String FUNCTION_NAME = "format_bytes";

Review Comment:
   Should we be more specific with the naming and have it like 
`human_format_bytes`?



-- 
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-unsubscr...@cassandra.apache.org

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


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

Reply via email to