Package org.apache.calcite.sql.advise
Class SqlAdvisor
java.lang.Object
org.apache.calcite.sql.advise.SqlAdvisor
An assistant which offers hints and corrections to a partially-formed SQL
 statement. It is used in the SQL editor user-interface.
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classText and position info of a validator or parser exception.
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionSqlAdvisor(SqlValidatorWithHints validator) Deprecated.SqlAdvisor(SqlValidatorWithHints validator, SqlParser.Config parserConfig) Creates a SqlAdvisor with a validator instance and given parser configuration.
- 
Method SummaryModifier and TypeMethodDescriptionprotected @Nullable SqlNodecollectParserError(String sql, List<SqlAdvisor.ValidateErrorInfo> errorList) Attempts to parse a SQL statement and adds to the errorList if any syntax error is found.getCompletionHints(String sql, int cursor, 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.getCompletionHints(String sql, SqlParserPos pos) Gets completion hints for a syntactically correct SQL statement with dummySqlIdentifier.getCompletionHints0(String sql, int cursor) protected org.apache.calcite.sql.parser.SqlAbstractParserImpl.MetadataReturns the underlying Parser metadata.@Nullable SqlMonikergetQualifiedName(String sql, int cursor) Gets the fully qualified name for aSqlIdentifierat a given position of a sql statement.getReplacement(SqlMoniker hint, boolean quoted, org.apache.calcite.avatica.util.Casing preferredCasing) getReplacement(SqlMoniker hint, String word) Returns an array of SQL reserved and keywords.booleanAttempts to complete and validate a given partially completed sql statement, and returns whether it is valid.protected SqlNodeparseQuery(String sql) Wrapper function to parse a SQL query (SELECT or VALUES, but not INSERT, UPDATE, DELETE, CREATE, DROP etc.), throwing aSqlParseExceptionif the statement is not syntactically valid.simplifySql(String sql, int cursor) Turns a partially completed or syntactically incorrect sql statement into a simplified, valid one that can be passed intogetCompletionHints(String, SqlParserPos).@Nullable List<SqlAdvisor.ValidateErrorInfo>Attempts to parse and validate a SQL statement.
- 
Field Details- 
LOGGERpublic static final org.slf4j.Logger LOGGER
 
- 
- 
Constructor Details- 
SqlAdvisorDeprecated.Creates a SqlAdvisor with a validator instance.- Parameters:
- validator- Validator
 
- 
SqlAdvisorCreates a SqlAdvisor with a validator instance and given parser configuration.- Parameters:
- validator- Validator
- parserConfig- parser config
 
 
- 
- 
Method Details- 
getCompletionHintsGets 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, ifsqlis "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 hints
- cursor- to indicate the 0-based cursor position in the query at
- replaced- String which is being replaced (output)
- Returns:
- completion hints
 
- 
getCompletionHints0
- 
getReplacement
- 
getReplacementpublic String getReplacement(SqlMoniker hint, boolean quoted, org.apache.calcite.avatica.util.Casing preferredCasing) 
- 
getCompletionHintsGets completion hints for a syntactically correct SQL statement with dummySqlIdentifier.- Parameters:
- sql- A syntactically correct sql statement for which to retrieve completion hints
- pos- 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
 
- 
getQualifiedNameGets the fully qualified name for aSqlIdentifierat a given position of a sql statement.- Parameters:
- sql- A syntactically correct sql statement for which to retrieve a fully qualified SQL identifier name
- cursor- 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 SqlMonikerthat contains the fully qualified name of the specified SQL identifier, returns null if none is found or the SQL statement is invalid.
 
- 
isValidAttempts 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
 
- 
validateAttempts 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)
 
- 
simplifySqlTurns a partially completed or syntactically incorrect sql statement into a simplified, valid one that can be passed intogetCompletionHints(String, SqlParserPos).- Parameters:
- sql- A partial or syntactically incorrect SQL statement
- cursor- Indicates the position in the query at which completion hints need to be retrieved
- Returns:
- a completed, valid (and possibly simplified SQL statement
 
- 
getReservedAndKeyWords@EnsuresNonNull({"reservedWordsSet","reservedWordsList"}) public List<String> getReservedAndKeyWords()Returns an array of SQL reserved and keywords.- Returns:
- an of SQL reserved and keywords
 
- 
getParserMetadataprotected org.apache.calcite.sql.parser.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
 
- 
parseQueryWrapper function to parse a SQL query (SELECT or VALUES, but not INSERT, UPDATE, DELETE, CREATE, DROP etc.), throwing aSqlParseExceptionif the statement is not syntactically valid.- Parameters:
- sql- SQL statement
- Returns:
- parse tree
- Throws:
- SqlParseException- if not syntactically valid
 
- 
collectParserErrorprotected @Nullable SqlNode collectParserError(String sql, 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
 
- 
SqlAdvisor(SqlValidatorWithHints, SqlParser.Config)