Package org.apache.calcite.sql.advise
Class SqlAdvisor
- java.lang.Object
-
- org.apache.calcite.sql.advise.SqlAdvisor
-
public class SqlAdvisor extends java.lang.Object
An assistant which offers hints and corrections to a partially-formed SQL statement. It is used in the SQL editor user-interface.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
SqlAdvisor.ValidateErrorInfo
An inner class that represents error message text and position info of a validator or parser exception
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
HINT_TOKEN
static org.slf4j.Logger
LOGGER
private SqlParser.Config
parserConfig
private org.apache.calcite.avatica.util.Casing
prevPreferredCasing
private java.lang.String
prevWord
private java.util.List<java.lang.String>
reservedWordsList
private java.util.Set<java.lang.String>
reservedWordsSet
private static java.lang.String
UPPER_HINT_TOKEN
private SqlValidatorWithHints
validator
-
Constructor Summary
Constructors Constructor Description SqlAdvisor(SqlValidatorWithHints validator)
Deprecated.SqlAdvisor(SqlValidatorWithHints validator, SqlParser.Config parserConfig)
Creates a SqlAdvisor with a validator instance and given parser configuration
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
applyCasing(java.lang.String value, org.apache.calcite.avatica.util.Casing casing)
protected SqlNode
collectParserError(java.lang.String sql, java.util.List<SqlAdvisor.ValidateErrorInfo> errorList)
Attempts to parse a SQL statement and adds to the errorList if any syntax error is found.private void
ensureReservedAndKeyWords()
java.util.List<SqlMoniker>
getCompletionHints(java.lang.String sql, int cursor, java.lang.String[] replaced)
Gets completion hints for a partially completed or syntactically incorrect sql statement with cursor pointing to the position where completion hints are requested.java.util.List<SqlMoniker>
getCompletionHints(java.lang.String sql, SqlParserPos pos)
Gets completion hints for a syntactically correct sql statement with dummy SqlIdentifierjava.util.List<SqlMoniker>
getCompletionHints0(java.lang.String sql, int cursor)
protected SqlAbstractParserImpl.Metadata
getParserMetadata()
Returns the underlying Parser metadata.private org.apache.calcite.avatica.util.Casing
getPreferredCasing(java.lang.String word)
Returns casing which is preferred for replacement.SqlMoniker
getQualifiedName(java.lang.String sql, int cursor)
Gets the fully qualified name for aSqlIdentifier
at a given position of a sql statement.java.lang.String
getReplacement(SqlMoniker hint, boolean quoted, org.apache.calcite.avatica.util.Casing preferredCasing)
java.lang.String
getReplacement(SqlMoniker hint, java.lang.String word)
java.util.List<java.lang.String>
getReservedAndKeyWords()
Return an array of SQL reserved and keywordsprivate java.util.Set<java.lang.String>
getReservedAndKeyWordsSet()
private static boolean
isSelectListItem(SqlNode root, SqlParserPos pos, java.lang.String hintToken)
boolean
isValid(java.lang.String sql)
Attempts to complete and validate a given partially completed sql statement, and returns whether it is valid.private boolean
matchesUnquoted(java.lang.String name, java.lang.String idToAppend)
protected SqlNode
parseQuery(java.lang.String sql)
Wrapper function to parse a SQL query (SELECT or VALUES, but not INSERT, UPDATE, DELETE, CREATE, DROP etc.), throwing aSqlParseException
if the statement is not syntactically valid.private char
quoteEnd()
private char
quoteStart()
java.lang.String
simplifySql(java.lang.String sql, int cursor)
Turns a partially completed or syntactically incorrect sql statement into a simplified, valid one that can be passed into getCompletionHints()private SqlNode
tryParse(java.lang.String sql, java.util.List<SqlMoniker> hintList)
Tries to parse a SQL statement.java.util.List<SqlAdvisor.ValidateErrorInfo>
validate(java.lang.String sql)
Attempts to parse and validate a SQL statement.
-
-
-
Field Detail
-
LOGGER
public static final org.slf4j.Logger LOGGER
-
HINT_TOKEN
private static final java.lang.String HINT_TOKEN
- See Also:
- Constant Field Values
-
UPPER_HINT_TOKEN
private static final java.lang.String UPPER_HINT_TOKEN
-
validator
private final SqlValidatorWithHints validator
-
parserConfig
private final SqlParser.Config parserConfig
-
prevWord
private java.lang.String prevWord
-
prevPreferredCasing
private org.apache.calcite.avatica.util.Casing prevPreferredCasing
-
reservedWordsSet
private java.util.Set<java.lang.String> reservedWordsSet
-
reservedWordsList
private java.util.List<java.lang.String> reservedWordsList
-
-
Constructor Detail
-
SqlAdvisor
@Deprecated public SqlAdvisor(SqlValidatorWithHints validator)
Deprecated.Creates a SqlAdvisor with a validator instance- Parameters:
validator
- Validator
-
SqlAdvisor
public SqlAdvisor(SqlValidatorWithHints validator, SqlParser.Config parserConfig)
Creates a SqlAdvisor with a validator instance and given parser configuration- Parameters:
validator
- ValidatorparserConfig
- parser config
-
-
Method Detail
-
quoteStart
private char quoteStart()
-
quoteEnd
private char quoteEnd()
-
getCompletionHints
public java.util.List<SqlMoniker> getCompletionHints(java.lang.String sql, int cursor, java.lang.String[] replaced)
Gets completion hints for a partially completed or syntactically incorrect sql statement with cursor pointing to the position where completion hints are requested.Writes into
replaced[0]
the string that is being replaced. Includes the cursor and the preceding identifier. For example, ifsql
is "select abc^de from t", setsreplaced[0]
to "abc". If the cursor is in the middle of whitespace, the replaced string is empty. The replaced string is never null.- Parameters:
sql
- A partial or syntactically incorrect sql statement for which to retrieve completion hintscursor
- to indicate the 0-based cursor position in the query atreplaced
- String which is being replaced (output)- Returns:
- completion hints
-
getCompletionHints0
public java.util.List<SqlMoniker> getCompletionHints0(java.lang.String sql, int cursor)
-
getPreferredCasing
private org.apache.calcite.avatica.util.Casing getPreferredCasing(java.lang.String word)
Returns casing which is preferred for replacement. For instance,en => ename, EN => ENAME
. When input has mixed case,Casing.UNCHANGED
is returned.- Parameters:
word
- input word- Returns:
- preferred casing when replacing input word
-
getReplacement
public java.lang.String getReplacement(SqlMoniker hint, java.lang.String word)
-
getReplacement
public java.lang.String getReplacement(SqlMoniker hint, boolean quoted, org.apache.calcite.avatica.util.Casing preferredCasing)
-
matchesUnquoted
private boolean matchesUnquoted(java.lang.String name, java.lang.String idToAppend)
-
applyCasing
private java.lang.String applyCasing(java.lang.String value, org.apache.calcite.avatica.util.Casing casing)
-
getCompletionHints
public java.util.List<SqlMoniker> getCompletionHints(java.lang.String sql, SqlParserPos pos)
Gets completion hints for a syntactically correct sql statement with dummy SqlIdentifier- Parameters:
sql
- A syntactically correct sql statement for which to retrieve completion hintspos
- to indicate the line and column position in the query at which completion hints need to be retrieved. For example, "select a.ename, b.deptno from sales.emp a join sales.dept b "on a.deptno=b.deptno where empno=1"; setting pos to 'Line 1, Column 17' returns all the possible column names that can be selected from sales.dept table setting pos to 'Line 1, Column 31' returns all the possible table names in 'sales' schema- Returns:
- an array of hints (
SqlMoniker
) that can fill in at the indicated position
-
isSelectListItem
private static boolean isSelectListItem(SqlNode root, SqlParserPos pos, java.lang.String hintToken)
-
tryParse
private SqlNode tryParse(java.lang.String sql, java.util.List<SqlMoniker> hintList)
Tries to parse a SQL statement.If succeeds, returns the parse tree node; if fails, populates the list of hints and returns null.
- Parameters:
sql
- SQL statementhintList
- List of hints suggesting allowable tokens at the point of failure- Returns:
- Parse tree if succeeded, null if parse failed
-
getQualifiedName
public SqlMoniker getQualifiedName(java.lang.String sql, int cursor)
Gets the fully qualified name for aSqlIdentifier
at a given position of a sql statement.- Parameters:
sql
- A syntactically correct sql statement for which to retrieve a fully qualified SQL identifier namecursor
- to indicate the 0-based cursor position in the query that represents a SQL identifier for which its fully qualified name is to be returned.- Returns:
- a
SqlMoniker
that contains the fully qualified name of the specified SQL identifier, returns null if none is found or the SQL statement is invalid.
-
isValid
public boolean isValid(java.lang.String sql)
Attempts to complete and validate a given partially completed sql statement, and returns whether it is valid.- Parameters:
sql
- A partial or syntactically incorrect sql statement to validate- Returns:
- whether SQL statement is valid
-
validate
public java.util.List<SqlAdvisor.ValidateErrorInfo> validate(java.lang.String sql)
Attempts to parse and validate a SQL statement. Throws the first exception encountered. The error message of this exception is to be displayed on the UI- Parameters:
sql
- A user-input sql statement to be validated- Returns:
- a List of ValidateErrorInfo (null if sql is valid)
-
simplifySql
public java.lang.String simplifySql(java.lang.String sql, int cursor)
Turns a partially completed or syntactically incorrect sql statement into a simplified, valid one that can be passed into getCompletionHints()- Parameters:
sql
- A partial or syntactically incorrect sql statementcursor
- to indicate column position in the query at which completion hints need to be retrieved.- Returns:
- a completed, valid (and possibly simplified SQL statement
-
getReservedAndKeyWords
public java.util.List<java.lang.String> getReservedAndKeyWords()
Return an array of SQL reserved and keywords- Returns:
- an of SQL reserved and keywords
-
getReservedAndKeyWordsSet
private java.util.Set<java.lang.String> getReservedAndKeyWordsSet()
-
ensureReservedAndKeyWords
private void ensureReservedAndKeyWords()
-
getParserMetadata
protected SqlAbstractParserImpl.Metadata getParserMetadata()
Returns the underlying Parser metadata.To use a different parser (recognizing a different dialect of SQL), derived class should override.
- Returns:
- metadata
-
parseQuery
protected SqlNode parseQuery(java.lang.String sql) throws SqlParseException
Wrapper function to parse a SQL query (SELECT or VALUES, but not INSERT, UPDATE, DELETE, CREATE, DROP etc.), throwing aSqlParseException
if the statement is not syntactically valid.- Parameters:
sql
- SQL statement- Returns:
- parse tree
- Throws:
SqlParseException
- if not syntactically valid
-
collectParserError
protected SqlNode collectParserError(java.lang.String sql, java.util.List<SqlAdvisor.ValidateErrorInfo> errorList)
Attempts to parse a SQL statement and adds to the errorList if any syntax error is found. This implementation usesSqlParser
. Subclass can re-implement this with a different parser implementation- Parameters:
sql
- A user-input sql statement to be parsederrorList
- AList
of error to be added to- Returns:
SqlNode
that is root of the parse tree, null if the sql is not valid
-
-