Java Code Examples for org.apache.calcite.sql.parser.SqlParserUtil#findPos()

The following examples show how to use org.apache.calcite.sql.parser.SqlParserUtil#findPos() . 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: TestSQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test // Suggestions for partial require update to Calcite
public void testSuggestColumnPartial() throws Exception {
  final String partialQuery = "SELECT t.s^ FROM testSpace.supplier t";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("cp"));

  ArrayList<String> expectedColumns = Lists.newArrayList();
  expectedColumns.addAll(
    asList(
      "s_name",
      "s_phone"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(2, returnedSuggestions.getSuggestions().size());
  for (SuggestionResponse.Suggestion suggestion : returnedSuggestions.getSuggestions()) {
      assertTrue(expectedColumns.contains(suggestion.getName()));
  }
}
 
Example 2
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuggestColumn() throws Exception {
  final String partialQuery = "SELECT t.^ FROM testSpace.supplier t";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("cp"));

  ArrayList<String> expectedColumns = Lists.newArrayList();
  expectedColumns.addAll(
    asList(
      "s_name",
      "s_phone"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(3, returnedSuggestions.getSuggestions().size());
  for (SuggestionResponse.Suggestion suggestion : returnedSuggestions.getSuggestions()) {
    if (suggestion.getType().equals("COLUMN")) {
      assertTrue(expectedColumns.contains(suggestion.getName()));
    } else if (suggestion.getType().equals("KEYWORD")) {
      assertEquals("*", suggestion.getName());
    }
  }
}
 
Example 3
Source File: SqlAdvisorTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * 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,
    Map<String, String> replacements) {
  SqlAdvisor advisor = tester.getFactory().createAdvisor();

  SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
  final String[] replaced = {null};
  List<SqlMoniker> results =
      advisor.getCompletionHints(sap.sql, sap.cursor, replaced);
  Assertions.assertEquals(expectedResults, convertCompletionHints(results),
      () -> "Completion hints for " + sql);
  if (expectedWord != null) {
    Assertions.assertEquals(expectedWord, replaced[0], "replaced[0] for " + sql);
  } else {
    assertNotNull(replaced[0]);
  }
  assertReplacements(sql, replacements, advisor, replaced[0], results);
}
 
Example 4
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpaceVDSFullDatasetCompletionNoPeriod() throws Exception {
  final String partialQuery = "SELECT * from testSpace^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(2, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 2; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("TABLE")) {
      assertEquals("testSpace.supplier", suggestion.getName());
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("testSpace", suggestion.getName());
    }
  }
}
 
Example 5
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuggestFromFullSchema() throws Exception {
  final String partialQuery = "Select * from mysrc^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(4, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 4; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("TABLE")) {
      assertEquals(String.format("mysrc.ds%d", i + 1), suggestion.getName());
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("mysrc", suggestion.getName());
    }
  }
}
 
Example 6
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpacePDSPartialDatasetCompletion() throws Exception {
  final String partialQuery = "SELECT * from cp.t^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(1, returnedSuggestions.getSuggestions().size());
  SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(0);
  assertEquals("TABLE", suggestion.getType());
  assertEquals("cp.\"tpch/supplier.parquet\"", suggestion.getName());
}
 
Example 7
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test // Could improve to suggest Dremio specific keywords
public void testSuggestSelectList() throws Exception {
  final String partialQuery = "Select ^ from mysrc.ds1";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  for (int i = 0; i < returnedSuggestions.getSuggestions().size(); i++) {
    assertEquals("KEYWORD", returnedSuggestions.getSuggestions().get(i).getType());
  }
}
 
Example 8
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
public void checkFails(
	String sql,
	String expectedMsgPattern) {
	SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
	Throwable thrown = null;
	try {
		final SqlNode sqlNode;
		sqlNode = factory.createParser(sap.sql).parseStmt();
		Util.discard(sqlNode);
	} catch (Throwable ex) {
		thrown = ex;
	}

	checkEx(expectedMsgPattern, sap, thrown);
}
 
Example 9
Source File: SqlAdvisorJdbcTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void adviseSql(int apiVersion, String sql, Consumer<ResultSet> checker)
    throws SQLException {
  Properties info = new Properties();
  if (apiVersion == 1) {
    info.put("lex", "JAVA");
    info.put("quoting", "DOUBLE_QUOTE");
  } else if (apiVersion == 2) {
    info.put("lex", "SQL_SERVER");
    info.put("quoting", "BRACKET");
  }
  Connection connection =
      DriverManager.getConnection("jdbc:calcite:", info);
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  calciteConnection.setSchema("hr");
  final TableFunction getHints =
      apiVersion == 1 ? new SqlAdvisorGetHintsFunction() : new SqlAdvisorGetHintsFunction2();
  schema.add("get_hints", getHints);
  String getHintsSql;
  if (apiVersion == 1) {
    getHintsSql = "select id, names, type from table(\"s\".\"get_hints\"(?, ?)) as t";
  } else {
    getHintsSql = "select id, names, type, replacement from table([s].[get_hints](?, ?)) as t";
  }

  PreparedStatement ps = connection.prepareStatement(getHintsSql);
  SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
  ps.setString(1, sap.sql);
  ps.setInt(2, sap.cursor);
  final ResultSet resultSet = ps.executeQuery();
  checker.accept(resultSet);
  resultSet.close();
  connection.close();
}
 
Example 10
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuggestFromPartialSchema() throws Exception {
  final String partialQuery = "Select * from m^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  ArrayList<String> expectedTables = Lists.newArrayList();
  expectedTables.addAll(
    asList(
      "mysrc.ds1",
      "mysrc.ds2",
      "mysrc.ds3",
      "sys.materializations",
      "sys.memory",
      "\"sys.cache\".mount_points"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals((expectedTables.size() + 1), returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < (expectedTables.size() + 1); i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("TABLE")) {
      assertTrue(expectedTables.contains(suggestion.getName()));
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("mysrc", suggestion.getName());
    }
  }
}
 
Example 11
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSQLAnalyzeSuggestInfoSchema() throws Exception {
  final String partialQuery = "select * from i^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("@dremio"));

  ArrayList<String> expectedTables = Lists.newArrayList();
  expectedTables.addAll(
    asList(
    "INFORMATION_SCHEMA.CATALOGS",
    "INFORMATION_SCHEMA.COLUMNS",
    "INFORMATION_SCHEMA.SCHEMATA",
    "INFORMATION_SCHEMA.\"TABLES\"",
    "INFORMATION_SCHEMA.VIEWS"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(6, returnedSuggestions.getSuggestions().size());

  for (SuggestionResponse.Suggestion suggestion : returnedSuggestions.getSuggestions()) {
    if (suggestion.getType().equals("TABLE")) {
      assertTrue(expectedTables.contains(suggestion.getName()));
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("INFORMATION_SCHEMA", suggestion.getName());
    }
  }
}
 
Example 12
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test // Range can be improved
public void testErrorUnrecognizedTable() throws Exception {
  final String partialQuery = "Select * from m^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final ValidationResponse returnedSuggestions = testValidateSQL(stringAndPos.sql, stringAndPos.cursor, asList("@dremio"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getErrors());
  QueryError firstError =  returnedSuggestions.getErrors().get(0);
  assertEquals("Table 'M' not found", firstError.getMessage());
  assertEquals(new QueryError.Range(1,15,2,16), firstError.getRange());
}
 
Example 13
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorUnrecognizedColumn() throws Exception {
  final String partialQuery = "SELECT testCol^ FROM testSpace.supplier";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final ValidationResponse returnedSuggestions = testValidateSQL(stringAndPos.sql, stringAndPos.cursor, asList("cp"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getErrors());
  QueryError firstError =  returnedSuggestions.getErrors().get(0);
  assertEquals("Column 'TESTCOL' not found in any table", firstError.getMessage());
  assertEquals(new QueryError.Range(1,8,2,15), firstError.getRange());
}
 
Example 14
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpaceVDSPartialDatasetCompletion() throws Exception {
  final String partialQuery = "SELECT * from testSpace.sup^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(1, returnedSuggestions.getSuggestions().size());
  SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(0);
  assertEquals("TABLE", suggestion.getType());
  assertEquals("testSpace.supplier", suggestion.getName());
}
 
Example 15
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testPDSFullDatasetCompletionWPeriod() throws Exception {
  final String partialQuery = "SELECT * from cp.^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(1, returnedSuggestions.getSuggestions().size());
  SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(0);
  assertEquals("TABLE", suggestion.getType());
  assertEquals("cp.\"tpch/supplier.parquet\"", suggestion.getName());
}
 
Example 16
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpaceVDSFullDatasetCompletionWPeriod() throws Exception {
  final String partialQuery = "SELECT * from testSpace.^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(1, returnedSuggestions.getSuggestions().size());
  SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(0);
  assertEquals("TABLE", suggestion.getType());
  assertEquals("testSpace.supplier", suggestion.getName());
}
 
Example 17
Source File: SqlAdvisorTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * 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 {
  SqlAdvisor advisor = tester.getFactory().createAdvisor();

  SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);

  List<SqlMoniker> results =
      advisor.getCompletionHints(
          sap.sql,
          sap.pos);
  Assertions.assertEquals(
      expectedResults, convertCompletionHints(results));
}
 
Example 18
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpaceVDSPartialSchemaCompletion() throws Exception {
  final String partialQuery = "SELECT * from t^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  ArrayList<String> expectedTables = Lists.newArrayList();
  expectedTables.addAll(
    asList(
      "INFORMATION_SCHEMA.\"TABLES\"",
      "cp.\"tpch/supplier.parquet\"",
      "sys.threads",
      "testSpace.supplier"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(6, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 6; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("TABLE")) {
      assertTrue(expectedTables.contains(suggestion.getName()));
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("testSpace", suggestion.getName());
    } else if (suggestion.getType().equals("KEYWORD")) {
      assertEquals("TABLE", suggestion.getName());
    }
  }
}
 
Example 19
Source File: TestSQLResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpaceFullSchemaCompletion() throws Exception {
  final String partialQuery = "SELECT * from ^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(39, returnedSuggestions.getSuggestions().size());
}
 
Example 20
Source File: TestSQLAnalyzer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuggestion() {
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(sql);
  List<SqlMoniker> suggestions = sqlAnalyzer.suggest(stringAndPos.sql, stringAndPos.cursor);
  assertEquals(expectedSuggestionCount, suggestions.size());
  if (checkSuggestions) {
    assertSuggestions(suggestions);
  }
}