Java Code Examples for org.apache.calcite.sql.validate.SqlMonikerType#KEYWORD
The following examples show how to use
org.apache.calcite.sql.validate.SqlMonikerType#KEYWORD .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLResource.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Builds the response object for query suggestions. * * @param suggestionList The suggestion list returned from the SqlAdvisor. * * @return The built SuggestionResponse object or null if there are no suggestions. */ public SuggestionResponse buildSuggestionResponse(List<SqlMoniker> suggestionList) { // Return empty response in REST request if (suggestionList == null || suggestionList.isEmpty()) { return null; } // Create and populate suggestion response list List<SuggestionResponse.Suggestion> suggestions = new ArrayList<>(); for (SqlMoniker hint : suggestionList) { // Quote the identifiers if they are not keywords or functions, // and are required to be quoted. List<String> qualifiedNames = hint.getFullyQualifiedNames(); if ((hint.getType() != SqlMonikerType.KEYWORD) && (hint.getType() != SqlMonikerType.FUNCTION)) { qualifiedNames = qualifiedNames.stream().map(name -> quoteIdentifier(name)).collect(Collectors.toList()); } suggestions.add( new SuggestionResponse.Suggestion(Joiner.on(".").join(qualifiedNames),hint.getType().name())); } SuggestionResponse response = new SuggestionResponse(suggestions); return response; }
Example 2
Source File: SqlAdvisor.java From Bats with Apache License 2.0 | 4 votes |
public String getReplacement(SqlMoniker hint, boolean quoted, Casing preferredCasing) { String name = Util.last(hint.getFullyQualifiedNames()); boolean isKeyword = hint.getType() == SqlMonikerType.KEYWORD; // If replacement has mixed case, we need to quote it (or not depending // on quotedCasing/unquotedCasing quoted &= !isKeyword; if (!quoted && !isKeyword && getReservedAndKeyWordsSet().contains(name)) { quoted = true; } StringBuilder sb = new StringBuilder(name.length() + (quoted ? 2 : 0)); if (!isKeyword && !Util.isValidJavaIdentifier(name)) { // needs quotes ==> quoted quoted = true; } String idToAppend = name; if (!quoted) { // id ==preferredCasing==> preferredId ==unquotedCasing==> recasedId // if recasedId matches id, then use preferredId String preferredId = applyCasing(name, preferredCasing); if (isKeyword || matchesUnquoted(name, preferredId)) { idToAppend = preferredId; } else { // Check if we can use unquoted identifier as is: for instance, unquotedCasing==UNCHANGED quoted = !matchesUnquoted(name, idToAppend); } } if (quoted) { sb.append(quoteStart()); } sb.append(idToAppend); if (quoted) { sb.append(quoteEnd()); } return sb.toString(); }
Example 3
Source File: SqlAdvisor.java From Bats with Apache License 2.0 | 4 votes |
/** * Gets completion hints for a syntactically correct sql statement with dummy * SqlIdentifier * * @param sql A syntactically correct sql statement for which to retrieve * completion hints * @param 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 * @return an array of hints ({@link SqlMoniker}) that can fill in at the * indicated position */ public List<SqlMoniker> getCompletionHints(String sql, SqlParserPos pos) { // First try the statement they gave us. If this fails, just return // the tokens which were expected at the failure point. List<SqlMoniker> hintList = new ArrayList<>(); SqlNode sqlNode = tryParse(sql, hintList); if (sqlNode == null) { return hintList; } // Now construct a statement which is bound to fail. (Character 7 BEL // is not legal in any SQL statement.) final int x = pos.getColumnNum() - 1; sql = sql.substring(0, x) + " \07" + sql.substring(x); tryParse(sql, hintList); final SqlMoniker star = new SqlMonikerImpl(ImmutableList.of("*"), SqlMonikerType.KEYWORD); String hintToken = parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN; if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) { hintList.remove(star); } // Add the identifiers which are expected at the point of interest. try { validator.validate(sqlNode); } catch (Exception e) { // mask any exception that is thrown during the validation, i.e. // try to continue even if the sql is invalid. we are doing a best // effort here to try to come up with the requested completion // hints Util.swallow(e, LOGGER); } final List<SqlMoniker> validatorHints = validator.lookupHints(sqlNode, pos); hintList.addAll(validatorHints); return hintList; }
Example 4
Source File: SqlAdvisor.java From calcite with Apache License 2.0 | 4 votes |
public String getReplacement(SqlMoniker hint, boolean quoted, Casing preferredCasing) { String name = Util.last(hint.getFullyQualifiedNames()); boolean isKeyword = hint.getType() == SqlMonikerType.KEYWORD; // If replacement has mixed case, we need to quote it (or not depending // on quotedCasing/unquotedCasing quoted &= !isKeyword; if (!quoted && !isKeyword && getReservedAndKeyWordsSet().contains(name)) { quoted = true; } StringBuilder sb = new StringBuilder(name.length() + (quoted ? 2 : 0)); if (!isKeyword && !Util.isValidJavaIdentifier(name)) { // needs quotes ==> quoted quoted = true; } String idToAppend = name; if (!quoted) { // id ==preferredCasing==> preferredId ==unquotedCasing==> recasedId // if recasedId matches id, then use preferredId String preferredId = applyCasing(name, preferredCasing); if (isKeyword || matchesUnquoted(name, preferredId)) { idToAppend = preferredId; } else { // Check if we can use unquoted identifier as is: for instance, unquotedCasing==UNCHANGED quoted = !matchesUnquoted(name, idToAppend); } } if (quoted) { sb.append(quoteStart()); } sb.append(idToAppend); if (quoted) { sb.append(quoteEnd()); } return sb.toString(); }
Example 5
Source File: SqlAdvisor.java From calcite with Apache License 2.0 | 4 votes |
/** * Gets completion hints for a syntactically correct sql statement with dummy * SqlIdentifier * * @param sql A syntactically correct sql statement for which to retrieve * completion hints * @param 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 * @return an array of hints ({@link SqlMoniker}) that can fill in at the * indicated position */ public List<SqlMoniker> getCompletionHints(String sql, SqlParserPos pos) { // First try the statement they gave us. If this fails, just return // the tokens which were expected at the failure point. List<SqlMoniker> hintList = new ArrayList<>(); SqlNode sqlNode = tryParse(sql, hintList); if (sqlNode == null) { return hintList; } // Now construct a statement which is bound to fail. (Character 7 BEL // is not legal in any SQL statement.) final int x = pos.getColumnNum() - 1; sql = sql.substring(0, x) + " \07" + sql.substring(x); tryParse(sql, hintList); final SqlMoniker star = new SqlMonikerImpl(ImmutableList.of("*"), SqlMonikerType.KEYWORD); String hintToken = parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN; if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) { hintList.remove(star); } // Add the identifiers which are expected at the point of interest. try { validator.validate(sqlNode); } catch (Exception e) { // mask any exception that is thrown during the validation, i.e. // try to continue even if the sql is invalid. we are doing a best // effort here to try to come up with the requested completion // hints Util.swallow(e, LOGGER); } final List<SqlMoniker> validatorHints = validator.lookupHints(sqlNode, pos); hintList.addAll(validatorHints); return hintList; }