gortiz commented on code in PR #14337: URL: https://github.com/apache/pinot/pull/14337#discussion_r1836107425
########## pinot-core/src/main/java/org/apache/pinot/core/util/NumberUtils.java: ########## @@ -0,0 +1,224 @@ +/** + * 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.pinot.core.util; + +/** + * Utility class with various number related methods. + */ +public class NumberUtils { + + public static final NumberFormatException NULL_EXCEPTION = new NumberFormatException("Can't parse null string"); + public static final NumberFormatException EXP_EXCEPTION = new NumberFormatException("Wrong exponent"); + public static final NumberFormatException EXCEPTION = new NumberFormatException("Can't parse value"); + + private static final long[] POWERS_OF_10 = new long[]{ + 1L, + 10L, + 100L, + 1000L, + 10000L, + 100000L, + 1000000L, + 10000000L, + 100000000L, + 1000000000L, + 10000000000L, + 100000000000L, + 1000000000000L, + 10000000000000L, + 100000000000000L, + 1000000000000000L, + 10000000000000000L, + 100000000000000000L, + 1000000000000000000L, + }; + + private NumberUtils() { + } + + /** + * Parses whole input char sequence. + * Throws static, pre-allocated NumberFormatException. + * If proper stack trace is required, caller has to catch it and throw another exception. + * @param cs char sequence to parse + * @return parsed long value + */ + public static long parseLong(CharSequence cs) { + if (cs == null) { + throw NULL_EXCEPTION; + } + return parseLong(cs, 0, cs.length()); + } + + /** + * Parses input char sequence between given indices. + * Throws static, pre-allocated NumberFormatException. + * If proper stack trace is required, caller has to catch it and throw another exception. + * @param cs char sequence to parse + * @param start start index (inclusive) + * @param end end index (exclusive) + * @return parsed long value + */ + public static long parseLong(CharSequence cs, int start, int end) { + if (cs == null) { + throw NULL_EXCEPTION; + } Review Comment: I think this is over-engineering. We should throw new exceptions by default. In most cases, performance is not going to be that problematic given that the code either won't be in the hotpath or an invalid value will abort the execution. In these cases throwing a new exception would be more useful to resolve a possible bug or problem in the dataset. Instead I suggest to have one `public static long parseLongFast(CharSequence cs, int start, int end) throws OurCheckedNumberFormatException`. Then optionally add a `public static long parseLong(CharSequence cs, int start, int end)` that catches `OurCheckedNumberFormatException` and re-throws it as a new `NumberFormatException`. By doing that we would have the ability to call the fast code that doesn't allocate but in that case we will need to deal with the exception. In cases where performance is not so critical or invalid values would abort the execution we could call `parseLong`. I think we can repeat that with all other methods in this class. -- 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]
