Package org.apache.calcite.sql.parser
Class SqlParserUtil
- java.lang.Object
-
- org.apache.calcite.sql.parser.SqlParserUtil
-
public final class SqlParserUtil extends java.lang.Object
Utility methods relating to parsing SQL.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SqlParserUtil.Format
Pre-initializedDateFormat
objects, to be used within the current thread, becauseDateFormat
is not thread-safe.private static class
SqlParserUtil.OldTokenSequenceImpl
Implementation ofSqlSpecialOperator.TokenSequence
.static class
SqlParserUtil.ParsedCollation
The components of a collation definition, per the SQL standard.static class
SqlParserUtil.StringAndPos
Contains a string, the offset of a token within the string, and a parser position containing the beginning and end line number.private static class
SqlParserUtil.TokenSequenceImpl
Implementation ofSqlSpecialOperator.TokenSequence
based on an existing parser.static class
SqlParserUtil.ToTreeListItem
Class that holds aSqlOperator
and aSqlParserPos
.
-
Field Summary
Fields Modifier and Type Field Description (package private) static org.slf4j.Logger
LOGGER
-
Constructor Summary
Constructors Modifier Constructor Description private
SqlParserUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String
addCarets(java.lang.String sql, int line, int col, int endLine, int endCol)
Converts a string to a string with one or two carets in it.static void
checkDateFormat(java.lang.String pattern)
Checks if the date/time format is validstatic char
checkUnicodeEscapeChar(java.lang.String s)
Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.private static SqlNode
convert(PrecedenceClimbingParser.Token token)
static SqlParserUtil.StringAndPos
findPos(java.lang.String sql)
Looks for one or two carets in a SQL string, and if present, converts them into a parser position.static java.lang.String
getCharacterSet(java.lang.String s)
static java.lang.String
getTokenVal(java.lang.String token)
static int[]
indexToLineCol(java.lang.String sql, int i)
Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.static long
intervalToMillis(java.lang.String literal, SqlIntervalQualifier intervalQualifier)
static long
intervalToMillis(SqlIntervalLiteral.IntervalValue interval)
Converts the interval value into a millisecond representation.static long
intervalToMonths(java.lang.String literal, SqlIntervalQualifier intervalQualifier)
static long
intervalToMonths(SqlIntervalLiteral.IntervalValue interval)
Converts the interval value into a months representation.static int
lineColToIndex(java.lang.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
nextLine(java.lang.String sql, int j)
static byte[]
parseBinaryString(java.lang.String s)
Deprecated.static SqlParserUtil.ParsedCollation
parseCollation(java.lang.String in)
Extracts the values from a collation name.static java.sql.Date
parseDate(java.lang.String s)
Deprecated.this method is not localized for Farrago standardsstatic SqlDateLiteral
parseDateLiteral(java.lang.String s, SqlParserPos pos)
static java.math.BigDecimal
parseDecimal(java.lang.String s)
static java.math.BigDecimal
parseInteger(java.lang.String s)
static SqlIntervalLiteral
parseIntervalLiteral(SqlParserPos pos, int sign, java.lang.String s, SqlIntervalQualifier intervalQualifier)
static int
parsePositiveInt(java.lang.String value)
Parses a positive int.static java.lang.String
parseString(java.lang.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).static java.sql.Time
parseTime(java.lang.String s)
Deprecated.Does not parse SQL:99 millisecondsstatic SqlTimeLiteral
parseTimeLiteral(java.lang.String s, SqlParserPos pos)
static java.sql.Timestamp
parseTimestamp(java.lang.String s)
Deprecated.this method is not localized for Farrago standardsstatic SqlTimestampLiteral
parseTimestampLiteral(java.lang.String s, SqlParserPos pos)
static <T> void
replaceSublist(java.util.List<T> list, int start, int end, T o)
Replaces a range of elements in a list with a single element.static java.lang.String
rightTrim(java.lang.String s, char c)
Deprecated.static java.lang.String
strip(java.lang.String s, java.lang.String startQuote, java.lang.String endQuote, java.lang.String escape, org.apache.calcite.avatica.util.Casing casing)
Unquotes a quoted string, using different quotes for beginning and end.static SqlNode[]
toNodeArray(java.util.List<SqlNode> list)
static SqlNode[]
toNodeArray(SqlNodeList list)
static java.lang.String[]
toStringArray(java.util.List<java.lang.String> list)
Deprecated.static SqlNode
toTree(java.util.List<java.lang.Object> list)
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 java.lang.String
trim(java.lang.String s, java.lang.String chars)
Trims a string for given characters from left and right.
-
-
-
Method Detail
-
getCharacterSet
public static java.lang.String getCharacterSet(java.lang.String s)
- Returns:
- the character-set prefix of an sql string literal; returns null if there is none
-
parseString
public static java.lang.String parseString(java.lang.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).
-
parseDecimal
public static java.math.BigDecimal parseDecimal(java.lang.String s)
-
parseInteger
public static java.math.BigDecimal parseInteger(java.lang.String s)
-
parseDate
@Deprecated public static java.sql.Date parseDate(java.lang.String s)
Deprecated.this method is not localized for Farrago standards
-
parseTime
@Deprecated public static java.sql.Time parseTime(java.lang.String s)
Deprecated.Does not parse SQL:99 milliseconds
-
parseTimestamp
@Deprecated public static java.sql.Timestamp parseTimestamp(java.lang.String s)
Deprecated.this method is not localized for Farrago standards
-
parseDateLiteral
public static SqlDateLiteral parseDateLiteral(java.lang.String s, SqlParserPos pos)
-
parseTimeLiteral
public static SqlTimeLiteral parseTimeLiteral(java.lang.String s, SqlParserPos pos)
-
parseTimestampLiteral
public static SqlTimestampLiteral parseTimestampLiteral(java.lang.String s, SqlParserPos pos)
-
parseIntervalLiteral
public static SqlIntervalLiteral parseIntervalLiteral(SqlParserPos pos, int sign, java.lang.String s, SqlIntervalQualifier intervalQualifier)
-
checkDateFormat
public static void checkDateFormat(java.lang.String pattern)
Checks if the date/time format is valid- 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(java.lang.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(java.lang.String literal, SqlIntervalQualifier intervalQualifier)
-
parsePositiveInt
public static int parsePositiveInt(java.lang.String value)
Parses a positive int. All characters have to be digits.- Throws:
java.lang.NumberFormatException
- if invalid number or leading '-'- See Also:
Integer.parseInt(String)
-
parseBinaryString
@Deprecated public static byte[] parseBinaryString(java.lang.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 java.lang.String strip(java.lang.String s, java.lang.String startQuote, java.lang.String endQuote, java.lang.String escape, org.apache.calcite.avatica.util.Casing casing)
Unquotes a quoted string, using different quotes for beginning and end.
-
trim
public static java.lang.String trim(java.lang.String s, java.lang.String chars)
Trims a string for given characters from left and right. E.g.trim("aBaac123AabC","abBcC")
returns"123A"
.
-
findPos
public static SqlParserUtil.StringAndPos findPos(java.lang.String sql)
Looks for one or two carets in a SQL string, and if present, converts them into a parser position.Examples:
- findPos("xxx^yyy") yields {"xxxyyy", position 3, line 1 column 4}
- findPos("xxxyyy") yields {"xxxyyy", null}
- findPos("xxx^yy^y") yields {"xxxyyy", position 3, line 4 column 4 through line 1 column 6}
-
indexToLineCol
public static int[] indexToLineCol(java.lang.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(java.lang.String sql, int j)
-
lineColToIndex
public static int lineColToIndex(java.lang.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 java.lang.String addCarets(java.lang.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 java.lang.String getTokenVal(java.lang.String token)
-
parseCollation
public static SqlParserUtil.ParsedCollation parseCollation(java.lang.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 java.lang.String[] toStringArray(java.util.List<java.lang.String> list)
Deprecated.
-
toNodeArray
public static SqlNode[] toNodeArray(SqlNodeList list)
-
rightTrim
@Deprecated public static java.lang.String rightTrim(java.lang.String s, char c)
Deprecated.
-
replaceSublist
public static <T> void replaceSublist(java.util.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}
thenreplaceSublist(list, X, 1, 4)
returns{A, X, E}
.
-
toTree
public static SqlNode toTree(java.util.List<java.lang.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 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
-
convert
private static SqlNode convert(PrecedenceClimbingParser.Token token)
-
checkUnicodeEscapeChar
public static char checkUnicodeEscapeChar(java.lang.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
-
-