chengw-netflix commented on code in PR #3741:
URL: https://github.com/apache/cassandra/pull/3741#discussion_r1919383237


##########
src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:
##########
@@ -0,0 +1,434 @@
+/*
+ * 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.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+
+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.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.MINUTES;
+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_time(val, 'ms', 's')
+     * format_time(val, 'h') = format_time(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 Double.MAX_VALUE, If the conversion produces 
overflown value, Double.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<Double, String> convertedValue = convertValue(value);
+                return 
UTF8Type.instance.fromString(format(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);
+
+            double convertedValue = convertValue(value, sourceUnit, 
targetUnit);
+            return UTF8Type.instance.fromString(format(convertedValue) + ' ' + 
targetUnitAsString);
+        }
+
+        private TimeUnit validateUnit(String unitAsString)
+        {
+            try
+            {
+                return DurationSpec.fromSymbol(unitAsString);
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidRequestException(ex.getMessage());
+            }
+        }
+
+        private Pair<Double, String> convertValue(long valueToConvert)
+        {
+            String[] units = {"d", "h", "m", "s"};
+            TimeUnit[] timeUnits = {DAYS, HOURS, MINUTES, SECONDS};
+
+            for (int i = 0; i < timeUnits.length; i++)
+            {
+                double convertedValue = convertValue(valueToConvert, 
MILLISECONDS, timeUnits[i]);

Review Comment:
   yeah. I agree it's a more direct and efficient. 



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