chengw-netflix commented on code in PR #3741: URL: https://github.com/apache/cassandra/pull/3741#discussion_r1917184763
########## 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: I think it's more personal flavor. I would like to be as close as what MySQL does. https://dev.mysql.com/doc/refman/9.1/en/sys-format-bytes.html -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

