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
Modifier and TypeClassDescriptionstatic class
Thrown byreplaceEscapedChars(String)
.static class
The components of a collation definition, per the SQL standard.static class
Class that holds aSqlOperator
and aSqlParserPos
. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Converts a string to a string with one or two carets in it.static boolean
allowsIdentifier
(String[] tokenImage, int[][] expectedTokenSequences) Returns whether the reported ParseException tokenImage allows SQL identifier.static void
checkDateFormat
(String pattern) Checks if the date/time format is valid, throws if not.static char
Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.static StringAndPos
Deprecated.static @Nullable String
Returns the character-set prefix of a SQL string literal; returns null if there is none.static @Nullable String
getTokenVal
(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 long
intervalToMillis
(String literal, SqlIntervalQualifier intervalQualifier) static long
Converts the interval value into a millisecond representation.static long
intervalToMonths
(String literal, SqlIntervalQualifier intervalQualifier) static long
Converts the interval value into a months representation.static boolean
isHexDigit
(char ch) Returns true if the specified character is a base-16 digit.static boolean
isOctalDigit
(char ch) Returns true if the specific character is a base-8 digit.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).static int
static SqlNode
Parses string to array literal usingSqlParserImpl
parser.static byte[]
Deprecated.parseCollation
(String in) Extracts the values from a collation name.static String
Converts the contents of a SQL quoted character literal with C-style escapes into the corresponding Java string representation.static Date
Deprecated.this method is not localized for Farrago standardsstatic SqlDateLiteral
parseDateLiteral
(String s, SqlParserPos pos) static BigDecimal
static SqlNumericLiteral
parseDecimalLiteral
(String s, SqlParserPos pos) static BigDecimal
static SqlIntervalLiteral
parseIntervalLiteral
(SqlParserPos pos, int sign, String s, SqlIntervalQualifier intervalQualifier) static int
parsePositiveInt
(String value) Parses a positive int.static String
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).static Time
Deprecated.Does not parse SQL:99 millisecondsstatic SqlTimeLiteral
parseTimeLiteral
(String s, SqlParserPos pos) static Timestamp
Deprecated.this method is not localized for Farrago standardsstatic SqlTimestampLiteral
parseTimestampLiteral
(String s, SqlParserPos pos) static SqlTimestampTzLiteral
static SqlTimestampLiteral
static SqlTimeTzLiteral
parseTimeTzLiteral
(String s, SqlParserPos pos) static String
replaceEscapedChars
(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> void
replaceSublist
(List<T> list, int start, int end, T o) Replaces a range of elements in a list with a single element.static String
Deprecated.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.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.static SqlNodeList
Converts "ROW (1, 2)" to "(1, 2)" and "3" to "(3)".static String
Converts 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 SqlNode
Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.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.static String
Trims 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) -
parseTimestampTzLiteral
-
parseIntervalLiteral
public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos, int sign, String s, SqlIntervalQualifier intervalQualifier) -
parseArrayLiteral
Parses string to array literal usingSqlParserImpl
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
Checks if the date/time format is valid, throws if not.- Parameters:
pattern
-SimpleDateFormat
pattern
-
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
startQuote
is provided,endQuote
andescape
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
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)
. -
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^)". -
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
-