Search in sources :

Example 6 with SqlMoniker

use of org.apache.calcite.sql.validate.SqlMoniker in project calcite by apache.

the class SqlAdvisor method getCompletionHints.

/**
 * 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<SqlMoniker>();
    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);
    if (hintList.contains(star) && !isSelectListItem(sqlNode, pos)) {
        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;
}
Also used : SqlMoniker(org.apache.calcite.sql.validate.SqlMoniker) ArrayList(java.util.ArrayList) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) CalciteException(org.apache.calcite.runtime.CalciteException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlNode(org.apache.calcite.sql.SqlNode) SqlMonikerImpl(org.apache.calcite.sql.validate.SqlMonikerImpl)

Example 7 with SqlMoniker

use of org.apache.calcite.sql.validate.SqlMoniker in project calcite by apache.

the class CalciteCatalogReader method getAllSchemaObjectNames.

public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
    final CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, names, nameMatcher);
    if (schema == null) {
        return ImmutableList.of();
    }
    final List<SqlMoniker> result = new ArrayList<>();
    // Add root schema if not anonymous
    if (!schema.name.equals("")) {
        result.add(moniker(schema, null, SqlMonikerType.SCHEMA));
    }
    final Map<String, CalciteSchema> schemaMap = schema.getSubSchemaMap();
    for (String subSchema : schemaMap.keySet()) {
        result.add(moniker(schema, subSchema, SqlMonikerType.SCHEMA));
    }
    for (String table : schema.getTableNames()) {
        result.add(moniker(schema, table, SqlMonikerType.TABLE));
    }
    final NavigableSet<String> functions = schema.getFunctionNames();
    for (String function : functions) {
        // views are here as well
        result.add(moniker(schema, function, SqlMonikerType.FUNCTION));
    }
    return result;
}
Also used : SqlMoniker(org.apache.calcite.sql.validate.SqlMoniker) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ArrayList(java.util.ArrayList)

Aggregations

SqlMoniker (org.apache.calcite.sql.validate.SqlMoniker)7 ArrayList (java.util.ArrayList)4 SqlAdvisor (org.apache.calcite.sql.advise.SqlAdvisor)2 SqlParserUtil (org.apache.calcite.sql.parser.SqlParserUtil)2 SqlValidatorWithHints (org.apache.calcite.sql.validate.SqlValidatorWithHints)2 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 CalciteContextException (org.apache.calcite.runtime.CalciteContextException)1 CalciteException (org.apache.calcite.runtime.CalciteException)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)1 SqlMonikerImpl (org.apache.calcite.sql.validate.SqlMonikerImpl)1