Search in sources :

Example 1 with SqlAdvisor

use of org.apache.calcite.sql.advise.SqlAdvisor in project flink by apache.

the class ParserImpl method getCompletionHints.

public String[] getCompletionHints(String statement, int cursor) {
    List<String> candidates = new ArrayList<>(Arrays.asList(EXTENDED_PARSER.getCompletionHints(statement, cursor)));
    // use sql advisor
    SqlAdvisorValidator validator = validatorSupplier.get().getSqlAdvisorValidator();
    SqlAdvisor advisor = new SqlAdvisor(validator, validatorSupplier.get().config().getParserConfig());
    String[] replaced = new String[1];
    List<String> sqlHints = advisor.getCompletionHints(statement, cursor, replaced).stream().map(item -> item.toIdentifier().toString()).collect(Collectors.toList());
    candidates.addAll(sqlHints);
    return candidates.toArray(new String[0]);
}
Also used : CatalogManager(org.apache.flink.table.catalog.CatalogManager) Arrays(java.util.Arrays) UnresolvedIdentifier(org.apache.flink.table.catalog.UnresolvedIdentifier) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) SqlAdvisorValidator(org.apache.calcite.sql.advise.SqlAdvisorValidator) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) Supplier(java.util.function.Supplier) CalciteParser(org.apache.flink.table.planner.parse.CalciteParser) ArrayList(java.util.ArrayList) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) SqlToOperationConverter(org.apache.flink.table.planner.operations.SqlToOperationConverter) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) ExtendedParser(org.apache.flink.table.planner.parse.ExtendedParser) SqlExprToRexConverter(org.apache.flink.table.planner.calcite.SqlExprToRexConverter) Nullable(javax.annotation.Nullable) Operation(org.apache.flink.table.operations.Operation) TableException(org.apache.flink.table.api.TableException) Parser(org.apache.flink.table.delegation.Parser) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) SqlExprToRexConverterFactory(org.apache.flink.table.planner.calcite.SqlExprToRexConverterFactory) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) Optional(java.util.Optional) RexNodeExpression(org.apache.flink.table.planner.expressions.RexNodeExpression) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) SqlAdvisor(org.apache.calcite.sql.advise.SqlAdvisor) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Collections(java.util.Collections) ArrayList(java.util.ArrayList) SqlAdvisorValidator(org.apache.calcite.sql.advise.SqlAdvisorValidator) SqlAdvisor(org.apache.calcite.sql.advise.SqlAdvisor)

Example 2 with SqlAdvisor

use of org.apache.calcite.sql.advise.SqlAdvisor in project calcite by apache.

the class SqlAdvisorTest method assertComplete.

/**
 * Tests that a given SQL which may be invalid or incomplete simplifies
 * itself and yields the salesTables set of completion hints. This is an
 * integration test of {@link #assertHint} and {@link #assertSimplify}.
 *
 * @param sql             SQL statement
 * @param expectedResults Expected list of hints
 * @param expectedWord    Word that we expect to be replaced, or null if we
 *                        don't care
 */
protected void assertComplete(String sql, String expectedResults, String expectedWord) {
    SqlValidatorWithHints validator = (SqlValidatorWithHints) tester.getValidator();
    SqlAdvisor advisor = tester.getFactory().createAdvisor(validator);
    SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
    final String[] replaced = { null };
    List<SqlMoniker> results = advisor.getCompletionHints(sap.sql, sap.cursor, replaced);
    assertNotNull(replaced[0]);
    assertNotNull(results);
    Assert.assertEquals(expectedResults, convertCompletionHints(results));
    if (expectedWord != null) {
        Assert.assertEquals(expectedWord, replaced[0]);
    }
}
Also used : SqlMoniker(org.apache.calcite.sql.validate.SqlMoniker) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) SqlValidatorWithHints(org.apache.calcite.sql.validate.SqlValidatorWithHints) SqlAdvisor(org.apache.calcite.sql.advise.SqlAdvisor)

Example 3 with SqlAdvisor

use of org.apache.calcite.sql.advise.SqlAdvisor in project calcite by apache.

the class SqlAdvisorTest method assertHint.

/**
 * Checks that a given SQL statement yields the expected set of completion
 * hints.
 *
 * @param sql             SQL statement
 * @param expectedResults Expected list of hints
 * @throws Exception on error
 */
protected void assertHint(String sql, String expectedResults) throws Exception {
    SqlValidatorWithHints validator = (SqlValidatorWithHints) tester.getValidator();
    SqlAdvisor advisor = tester.getFactory().createAdvisor(validator);
    SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
    List<SqlMoniker> results = advisor.getCompletionHints(sap.sql, sap.pos);
    Assert.assertEquals(expectedResults, convertCompletionHints(results));
}
Also used : SqlMoniker(org.apache.calcite.sql.validate.SqlMoniker) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) SqlValidatorWithHints(org.apache.calcite.sql.validate.SqlValidatorWithHints) SqlAdvisor(org.apache.calcite.sql.advise.SqlAdvisor)

Example 4 with SqlAdvisor

use of org.apache.calcite.sql.advise.SqlAdvisor in project calcite by apache.

the class SqlAdvisorTest method assertSimplify.

/**
 * Tests that a given SQL statement simplifies to the salesTables result.
 *
 * @param sql      SQL statement to simplify. The SQL statement must contain
 *                 precisely one caret '^', which marks the location where
 *                 completion is to occur.
 * @param expected Expected result after simplification.
 */
protected void assertSimplify(String sql, String expected) {
    SqlValidatorWithHints validator = (SqlValidatorWithHints) tester.getValidator();
    SqlAdvisor advisor = tester.getFactory().createAdvisor(validator);
    SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
    String actual = advisor.simplifySql(sap.sql, sap.cursor);
    Assert.assertEquals(expected, actual);
}
Also used : SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) SqlValidatorWithHints(org.apache.calcite.sql.validate.SqlValidatorWithHints) SqlAdvisor(org.apache.calcite.sql.advise.SqlAdvisor)

Aggregations

SqlAdvisor (org.apache.calcite.sql.advise.SqlAdvisor)4 SqlParserUtil (org.apache.calcite.sql.parser.SqlParserUtil)3 SqlValidatorWithHints (org.apache.calcite.sql.validate.SqlValidatorWithHints)3 SqlMoniker (org.apache.calcite.sql.validate.SqlMoniker)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlAdvisorValidator (org.apache.calcite.sql.advise.SqlAdvisorValidator)1 TableException (org.apache.flink.table.api.TableException)1 CatalogManager (org.apache.flink.table.catalog.CatalogManager)1 UnresolvedIdentifier (org.apache.flink.table.catalog.UnresolvedIdentifier)1