Package org.apache.calcite.sql.parser
Class SqlParserUtil
java.lang.Object
org.apache.calcite.sql.parser.SqlParserUtil
Utility methods relating to parsing SQL.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown byreplaceEscapedChars(String).static classThe components of a collation definition, per the SQL standard.static classClass that holds aSqlOperatorand aSqlParserPos. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringConverts a string to a string with one or two carets in it.static booleanallowsIdentifier(String[] tokenImage, int[][] expectedTokenSequences) Returns whether the reported ParseException tokenImage allows SQL identifier.static voidcheckDateFormat(String pattern) Checks if the date/time format is valid, throws if not.static charChecks a UESCAPE string for validity, and returns the escape character if no exception is thrown.static StringescapeCarets(String sql) Given a string with carets, double each of them.static StringAndPosDeprecated.static @Nullable StringReturns the character-set prefix of a SQL string literal; returns null if there is none.static @Nullable StringgetTokenVal(String token) static int[]indexToLineCol(String sql, int i) Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.static longintervalToMillis(String literal, SqlIntervalQualifier intervalQualifier) static longConverts the interval value into a millisecond representation.static longintervalToMonths(String literal, SqlIntervalQualifier intervalQualifier) static longConverts the interval value into a months representation.static booleanisHexDigit(char ch) Returns true if the specified character is a base-16 digit.static booleanisOctalDigit(char ch) Returns true if the specific character is a base-8 digit.static intlineColToIndex(String sql, int line, int column) Finds the position (0-based) in a string which corresponds to a given line and column (1-based).static intstatic SqlNodeParses string to array literal usingSqlParserImplparser.static byte[]Deprecated.parseCollation(String in) Extracts the values from a collation name.static StringConverts the contents of a SQL quoted character literal with C-style escapes into the corresponding Java string representation.static DateDeprecated.this method is not localized for Farrago standardsstatic SqlDateLiteralparseDateLiteral(String s, SqlParserPos pos) static BigDecimalstatic SqlNumericLiteralparseDecimalLiteral(String s, SqlParserPos pos) static BigDecimalstatic SqlIntervalLiteralparseIntervalLiteral(SqlParserPos pos, int sign, String s, SqlIntervalQualifier intervalQualifier) static intparsePositiveInt(String value) Parses a positive int.static StringConverts the contents of an sql quoted string literal into the corresponding Java string representation (removing leading and trailing quotes and unescaping internal doubled quotes).static TimeDeprecated.Does not parse SQL:99 millisecondsstatic SqlTimeLiteralparseTimeLiteral(String s, SqlParserPos pos) static TimestampDeprecated.this method is not localized for Farrago standardsstatic SqlTimestampLiteralparseTimestampLiteral(String s, SqlParserPos pos) static SqlTimestampTzLiteralstatic SqlTimestampLiteralstatic SqlTimeTzLiteralparseTimeTzLiteral(String s, SqlParserPos pos) static SqlUuidLiteralparseUuidLiteral(String s, SqlParserPos pos) static StringreplaceEscapedChars(String input) Converts the contents of a character literal with escapes like those used in the C programming language to the corresponding Java string representation.static <T> voidreplaceSublist(List<T> list, int start, int end, T o) Replaces a range of elements in a list with a single element.static StringDeprecated.static Stringstrip(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.static StringstripQuotes(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.static SqlNodeListConverts "ROW (1, 2)" to "(1, 2)" and "3" to "(3)".static StringConverts an identifier to a particular casing.static SqlNode[]toNodeArray(List<SqlNode> list) static SqlNode[]toNodeArray(SqlNodeList list) static String[]toStringArray(List<String> list) Deprecated.static @Nullable SqlNodeConverts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.static SqlNodetoTreeEx(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.static StringTrims a string for given characters from left and right.
-
Method Details
-
getCharacterSet
Returns the character-set prefix of a SQL string literal; returns null if there is none. -
parseString
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
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
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
-
parseInteger
-
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.this method is not localized for Farrago standards -
parseTime
Deprecated.Does not parse SQL:99 milliseconds -
parseTimestamp
Deprecated.this method is not localized for Farrago standards -
parseDateLiteral
-
parseDecimalLiteral
-
parseTimeLiteral
-
parseTimeTzLiteral
-
parseTimestampLiteral
-
parseTimestampWithLocalTimeZoneLiteral
public static SqlTimestampLiteral parseTimestampWithLocalTimeZoneLiteral(String s, SqlParserPos pos) -
parseUuidLiteral
-
parseTimestampTzLiteral
-
parseIntervalLiteral
public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos, int sign, String s, SqlIntervalQualifier intervalQualifier) -
parseArrayLiteral
Parses string to array literal usingSqlParserImplparser. 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
Checks if the date/time format is valid, throws if not.- Parameters:
pattern-SimpleDateFormatpattern
-
intervalToMillis
Converts the interval value into a millisecond representation.- Parameters:
interval- Interval- Returns:
- a long value that represents millisecond equivalent of the interval value.
-
intervalToMillis
-
intervalToMonths
Converts the interval value into a months representation.- Parameters:
interval- Interval- Returns:
- a long value that represents months equivalent of the interval value.
-
intervalToMonths
-
parsePositiveInt
Parses a positive int. All characters have to be digits.- Throws:
NumberFormatException- if invalid number or leading '-'- See Also:
-
parseBinaryString
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
startQuoteis provided,endQuoteandescapemust 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
Converts an identifier to a particular casing. -
trim
Trims a string for given characters from left and right. E.g.trim("aBaac123AabC","abBcC")returns"123A". -
findPos
Deprecated. -
indexToLineCol
Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.Converse of
lineColToIndex(String, int, int). -
nextLine
-
lineColToIndex
Finds the position (0-based) in a string which corresponds to a given line and column (1-based).Converse of
indexToLineCol(String, int). -
escapeCarets
Given a string with carets, double each of them. (This should probably be more sophisticated, and ignore carets within string literals). -
addCarets
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^)". Existing carets are escaped by doubling. -
getTokenVal
-
parseCollation
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. -
toNodeArray
-
toNodeArray
-
stripRow
Converts "ROW (1, 2)" to "(1, 2)" and "3" to "(3)". -
rightTrim
Deprecated. -
replaceSublist
Replaces a range of elements in a list with a single element. For example, if list contains{A, B, C, D, E}thenreplaceSublist(list, X, 1, 4)returns{A, X, E}. -
toTree
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 notSqlKind.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
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
Returns whether the reported ParseException tokenImage allows SQL identifier.- Parameters:
tokenImage- The allowed tokens from the ParseExceptionexpectedTokenSequences- Expected token sequences- Returns:
- true if SQL identifier is allowed
-