Class SqlParserUtil

java.lang.Object
org.apache.calcite.sql.parser.SqlParserUtil

public final class SqlParserUtil extends Object
Utility methods relating to parsing SQL.
  • Method Details

    • getCharacterSet

      public static @Nullable String getCharacterSet(String s)
      Returns the character-set prefix of a SQL string literal; returns null if there is none.
    • parseString

      public static String parseString(String s)
      Converts the contents of an sql quoted string literal into the corresponding Java string representation (removing leading and trailing quotes and unescaping internal doubled quotes).
    • parseCString

      public static String parseCString(String s) throws SqlParserUtil.MalformedUnicodeEscape
      Converts the contents of a SQL quoted character literal with C-style escapes into the corresponding Java string representation.
      Throws:
      SqlParserUtil.MalformedUnicodeEscape - if input contains invalid unicode escapes
    • replaceEscapedChars

      public static String replaceEscapedChars(String input) throws SqlParserUtil.MalformedUnicodeEscape
      Converts the contents of a character literal with escapes like those used in the C programming language to the corresponding Java string representation.

      If the literal "E'a\tc'" occurs in the SQL source text, then this method will be invoked with the string "a\tc" (4 characters) and will return a Java string with the three characters 'a', TAB, 'b'.

      The format is the same as the Postgres; see Postgres 4.1.2.2. String Constants With C-Style Escapes.

      Parameters:
      input - String that contains C-style escapes
      Returns:
      String with escapes converted into Java characters
      Throws:
      SqlParserUtil.MalformedUnicodeEscape - if input contains invalid unicode escapes
    • parseDecimal

      public static BigDecimal parseDecimal(String s)
    • parseInteger

      public static BigDecimal parseInteger(String s)
    • isOctalDigit

      public static boolean isOctalDigit(char ch)
      Returns true if the specific character is a base-8 digit.
    • isHexDigit

      public static boolean isHexDigit(char ch)
      Returns true if the specified character is a base-16 digit.
    • parseDate

      @Deprecated public static Date parseDate(String s)
      Deprecated.
      this method is not localized for Farrago standards
    • parseTime

      @Deprecated public static Time parseTime(String s)
      Deprecated.
      Does not parse SQL:99 milliseconds
    • parseTimestamp

      @Deprecated public static Timestamp parseTimestamp(String s)
      Deprecated.
      this method is not localized for Farrago standards
    • parseDateLiteral

      public static SqlDateLiteral parseDateLiteral(String s, SqlParserPos pos)
    • parseDecimalLiteral

      public static SqlNumericLiteral parseDecimalLiteral(String s, SqlParserPos pos)
    • parseTimeLiteral

      public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos)
    • parseTimeTzLiteral

      public static SqlTimeTzLiteral parseTimeTzLiteral(String s, SqlParserPos pos)
    • parseTimestampLiteral

      public static SqlTimestampLiteral parseTimestampLiteral(String s, SqlParserPos pos)
    • parseTimestampWithLocalTimeZoneLiteral

      public static SqlTimestampLiteral parseTimestampWithLocalTimeZoneLiteral(String s, SqlParserPos pos)
    • parseTimestampTzLiteral

      public static SqlTimestampTzLiteral parseTimestampTzLiteral(String s, SqlParserPos pos)
    • parseIntervalLiteral

      public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos, int sign, String s, SqlIntervalQualifier intervalQualifier)
    • parseArrayLiteral

      public static SqlNode parseArrayLiteral(String s) throws SqlParseException
      Parses string to array literal using SqlParserImpl parser. String format description can be found at the link
      Parameters:
      s - a string to parse
      Returns:
      a array value
      Throws:
      SqlParseException - if there is a parse error
    • checkDateFormat

      public static void checkDateFormat(String pattern)
      Checks if the date/time format is valid, throws if not.
      Parameters:
      pattern - SimpleDateFormat pattern
    • intervalToMillis

      public static long intervalToMillis(SqlIntervalLiteral.IntervalValue interval)
      Converts the interval value into a millisecond representation.
      Parameters:
      interval - Interval
      Returns:
      a long value that represents millisecond equivalent of the interval value.
    • intervalToMillis

      public static long intervalToMillis(String literal, SqlIntervalQualifier intervalQualifier)
    • intervalToMonths

      public static long intervalToMonths(SqlIntervalLiteral.IntervalValue interval)
      Converts the interval value into a months representation.
      Parameters:
      interval - Interval
      Returns:
      a long value that represents months equivalent of the interval value.
    • intervalToMonths

      public static long intervalToMonths(String literal, SqlIntervalQualifier intervalQualifier)
    • parsePositiveInt

      public static int parsePositiveInt(String value)
      Parses a positive int. All characters have to be digits.
      Throws:
      NumberFormatException - if invalid number or leading '-'
      See Also:
    • parseBinaryString

      @Deprecated public static byte[] parseBinaryString(String s)
      Deprecated.
      Parses a Binary string. SQL:99 defines a binary string as a hexstring with EVEN nbr of hex digits.
    • strip

      public static String strip(String s, @Nullable String startQuote, @Nullable String endQuote, @Nullable String escape, org.apache.calcite.avatica.util.Casing casing)
      Converts a quoted identifier, unquoted identifier, or quoted string to a string of its contents.

      First, if startQuote is provided, endQuote and escape must also be provided, and this method removes quotes.

      Finally, converts the string to the provided casing.

    • stripQuotes

      public static String stripQuotes(String s, String startQuote, String endQuote, String escape, org.apache.calcite.avatica.util.Casing casing)
      Unquotes a quoted string, using different quotes for beginning and end.
    • toCase

      public static String toCase(String s, org.apache.calcite.avatica.util.Casing casing)
      Converts an identifier to a particular casing.
    • trim

      public static String trim(String s, String chars)
      Trims a string for given characters from left and right. E.g. trim("aBaac123AabC","abBcC") returns "123A".
    • findPos

      @Deprecated public static StringAndPos findPos(String sql)
      Deprecated.
    • indexToLineCol

      public static int[] indexToLineCol(String sql, int i)
      Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.

      Converse of lineColToIndex(String, int, int).

    • nextLine

      public static int nextLine(String sql, int j)
    • lineColToIndex

      public static int lineColToIndex(String sql, int line, int column)
      Finds the position (0-based) in a string which corresponds to a given line and column (1-based).

      Converse of indexToLineCol(String, int).

    • addCarets

      public static String addCarets(String sql, int line, int col, int endLine, int endCol)
      Converts a string to a string with one or two carets in it. For example, addCarets("values (foo)", 1, 9, 1, 12) yields "values (^foo^)".
    • getTokenVal

      public static @Nullable String getTokenVal(String token)
    • parseCollation

      public static SqlParserUtil.ParsedCollation parseCollation(String in)
      Extracts the values from a collation name.

      Collation names are on the form charset$locale$strength.

      Parameters:
      in - The collation name
      Returns:
      A SqlParserUtil.ParsedCollation
    • toStringArray

      @Deprecated public static String[] toStringArray(List<String> list)
      Deprecated.
    • toNodeArray

      public static SqlNode[] toNodeArray(List<SqlNode> list)
    • toNodeArray

      public static SqlNode[] toNodeArray(SqlNodeList list)
    • stripRow

      public static SqlNodeList stripRow(SqlNode n)
      Converts "ROW (1, 2)" to "(1, 2)" and "3" to "(3)".
    • rightTrim

      @Deprecated public static String rightTrim(String s, char c)
      Deprecated.
    • replaceSublist

      public static <T> void replaceSublist(List<T> list, int start, int end, T o)
      Replaces a range of elements in a list with a single element. For example, if list contains {A, B, C, D, E} then replaceSublist(list, X, 1, 4) returns {A, X, E}.
    • toTree

      public static @Nullable SqlNode toTree(List<@Nullable Object> list)
      Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
    • toTreeEx

      public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list, int start, int minPrec, SqlKind stopperKind)
      Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
      Parameters:
      list - List of operands and operators. This list is modified as expressions are reduced.
      start - Position of first operand in the list. Anything to the left of this (besides the immediately preceding operand) is ignored. Generally use value 1.
      minPrec - Minimum precedence to consider. If the method encounters an operator of lower precedence, it doesn't reduce any further.
      stopperKind - If not SqlKind.OTHER, stop reading the list if we encounter a token of this kind.
      Returns:
      the root node of the tree which the list condenses into
    • checkUnicodeEscapeChar

      public static char checkUnicodeEscapeChar(String s)
      Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.
      Parameters:
      s - UESCAPE string to check
      Returns:
      validated escape character
    • allowsIdentifier

      public static boolean allowsIdentifier(String[] tokenImage, int[][] expectedTokenSequences)
      Returns whether the reported ParseException tokenImage allows SQL identifier.
      Parameters:
      tokenImage - The allowed tokens from the ParseException
      expectedTokenSequences - Expected token sequences
      Returns:
      true if SQL identifier is allowed