cloud-fan commented on code in PR #36066: URL: https://github.com/apache/spark/pull/36066#discussion_r844034697
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ToNumberParser.scala: ########## @@ -0,0 +1,517 @@ +/* + * 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.spark.sql.catalyst.util + +import scala.collection.mutable + +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.types.{Decimal, DecimalType} +import org.apache.spark.unsafe.types.UTF8String + +// This object contains some definitions of characters and tokens for the parser below. +object ToNumberParser { + final val ANGLE_BRACKET_CLOSE = '>' + final val ANGLE_BRACKET_OPEN = '<' + final val COMMA_LETTER = 'G' + final val COMMA_SIGN = ',' + final val DOLLAR_LETTER = 'L' + final val DOLLAR_SIGN = '$' + final val MINUS_SIGN = '-' + final val NINE_DIGIT = '9' + final val OPTIONAL_PLUS_OR_MINUS_LETTER = 'S' + final val PLUS_SIGN = '+' + final val POINT_LETTER = 'D' + final val POINT_SIGN = '.' + final val POUND_SIGN = '#' + final val ZERO_DIGIT = '0' + + final val OPTIONAL_MINUS_STRING = "MI" + final val WRAPPING_ANGLE_BRACKETS_TO_NEGATIVE_NUMBER = "PR" + + final val OPTIONAL_MINUS_STRING_START = 'M' + final val OPTIONAL_MINUS_STRING_END = 'I' + final val WRAPPING_ANGLE_BRACKETS_TO_NEGATIVE_NUMBER_START = 'P' + final val WRAPPING_ANGLE_BRACKETS_TO_NEGATIVE_NUMBER_END = 'R' + + // This class represents one or more characters that we expect to be present in the input string + // based on the format string. + abstract class InputToken() + // Represents some number of digits (0-9). + abstract class Digits extends InputToken + // Represents exactly 'num' digits (0-9). + case class ExactlyAsManyDigits(num: Int) extends Digits + // Represents at most 'num' digits (0-9). + case class AtMostAsManyDigits(num: Int) extends Digits + // Represents one decimal point (.). + case class DecimalPoint() extends InputToken + // Represents one thousands separator (,). + case class ThousandsSeparator() extends InputToken + // Represents one or more groups of digits (0-9) with thousands separators (,) between each group. + case class DigitGroups(tokens: Seq[InputToken]) extends InputToken + // Represents one dollar sign ($). + case class DollarSign() extends InputToken + // Represents one optional plus sign (+) or minus sign (-). + case class OptionalPlusOrMinusSign() extends InputToken + // Represents one optional minus sign (-). + case class OptionalMinusSign() extends InputToken + // Represents one opening angle bracket (<). + case class OpeningAngleBracket() extends InputToken + // Represents one closing angle bracket (>). + case class ClosingAngleBracket() extends InputToken + // Represents any unrecognized character other than the above. + case class InvalidUnrecognizedCharacter(char: Char) extends InputToken +} + +/** + * This class represents a parser to implement the to_number SQL function. + * + * It works by consuming an input string and a format string. This class accepts the format string + * as a field, and proceeds to iterate through the format string to generate a sequence of tokens + * (or throw an exception if the format string is invalid). Then when the function is called with an + * input string, this class steps through the sequence of tokens and compares them against the input + * string, returning a Scala Decimal object if they match (or throwing an exception otherwise). + * + * @param originNumberFormat the format string describing the expected inputs. + * @param errorOnFail true if evaluation should throw an exception if the input string fails to + * match the format string. Otherwise, returns NULL instead. + */ +class ToNumberParser(originNumberFormat: String, errorOnFail: Boolean) extends Serializable { + import ToNumberParser._ + + // Consumes the format string and produce a sequence of input tokens expected from each input + // string. + private lazy val inputTokens: Seq[InputToken] = { + var tokens = mutable.Seq.empty[InputToken] + var i = 0 + var reachedDecimalPoint = false + val format = originNumberFormat + val len = originNumberFormat.length + while (i < len) { + val char: Char = originNumberFormat(i) + char match { + case ZERO_DIGIT => + val prevI = i + do { + i += 1 + } while (i < len && (format(i) == ZERO_DIGIT || format(i) == NINE_DIGIT)) + if (reachedDecimalPoint) { + tokens :+= AtMostAsManyDigits(i - prevI) + } else { + tokens :+= ExactlyAsManyDigits(i - prevI) + } + case NINE_DIGIT => + val prevI = i + do { + i += 1 + } while (i < len && (format(i) == ZERO_DIGIT || format(i) == NINE_DIGIT)) + tokens :+= AtMostAsManyDigits(i - prevI) + case POINT_SIGN | POINT_LETTER => + tokens :+= DecimalPoint() + reachedDecimalPoint = true + i += 1 + case COMMA_SIGN | COMMA_LETTER => + tokens :+= ThousandsSeparator() + i += 1 + case DOLLAR_LETTER | DOLLAR_SIGN => + tokens :+= DollarSign() + i += 1 + case OPTIONAL_PLUS_OR_MINUS_LETTER => + tokens :+= OptionalPlusOrMinusSign() + i += 1 + case OPTIONAL_MINUS_STRING_START if i < len - 1 && + OPTIONAL_MINUS_STRING_END == originNumberFormat(i + 1) => + tokens :+= OptionalMinusSign() + i += 2 + case WRAPPING_ANGLE_BRACKETS_TO_NEGATIVE_NUMBER_START if i < len - 1 && + WRAPPING_ANGLE_BRACKETS_TO_NEGATIVE_NUMBER_END == originNumberFormat(i + 1) => + tokens +:= OpeningAngleBracket() Review Comment: is `+:=` prepend? maybe we should use explicit function names... -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org