Java Code Examples for org.apache.calcite.sql.parser.SqlParserUtil

The following examples show how to use org.apache.calcite.sql.parser.SqlParserUtil. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlCollation.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Collation by its name and its coercibility
 *
 * @param collation    Collation specification
 * @param coercibility Coercibility
 */
public SqlCollation(
    String collation,
    Coercibility coercibility) {
  this.coercibility = coercibility;
  SqlParserUtil.ParsedCollation parseValues =
      SqlParserUtil.parseCollation(collation);
  Charset charset = parseValues.getCharset();
  this.wrappedCharset = SerializableCharset.forCharset(charset);
  locale = parseValues.getLocale();
  strength = parseValues.getStrength();
  String c =
      charset.name().toUpperCase(Locale.ROOT) + "$" + locale.toString();
  if ((strength != null) && (strength.length() > 0)) {
    c += "$" + strength;
  }
  collationName = c;
}
 
Example 2
Source Project: Bats   Source File: DrillParserUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static SqlNode createCondition(SqlNode left, SqlOperator op, SqlNode right) {

    // if one of the operands is null, return the other
    if (left == null) {
      return right;
    } else if (right == null) {
      return left;
    }

    List<Object> listCondition = new ArrayList<>(CONDITION_LIST_CAPACITY);
    listCondition.add(left);
    listCondition.add(new SqlParserUtil.ToTreeListItem(op, SqlParserPos.ZERO));
    listCondition.add(right);

    return SqlParserUtil.toTree(listCondition);
  }
 
Example 3
public boolean visit(SQLBetweenExpr x) {
    SQLExpr testExpr = x.getTestExpr();
    SqlOperator sqlOperator = SqlStdOperatorTable.BETWEEN;
    if (x.isNot()) {
        sqlOperator = SqlStdOperatorTable.NOT_BETWEEN;
    }
    SqlNode sqlNode = convertToSqlNode(testExpr);
    SqlNode sqlNodeBegin = convertToSqlNode(x.getBeginExpr());
    SqlNode sqlNodeEnd = convertToSqlNode(x.getEndExpr());
    ArrayList<SqlNode> sqlNodes = new ArrayList<SqlNode>(3);
    sqlNodes.add(sqlNode);
    sqlNodes.add(sqlNodeBegin);
    sqlNodes.add(sqlNodeEnd);
    this.sqlNode = new SqlBasicCall(sqlOperator, SqlParserUtil.toNodeArray(sqlNodes), SqlParserPos.ZERO);
    return false;
}
 
Example 4
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPDSFullDatasetCompletionNoPeriod() 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(2, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 2; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("TABLE")) {
      assertEquals("cp.\"tpch/supplier.parquet\"", suggestion.getName());
    } else if (suggestion.getType().equals("SCHEMA")) {
      assertEquals("cp", suggestion.getName());
    }
  }
}
 
Example 6
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPartialColumnCompletionWithAlias() throws Exception {
  final String partialQuery = "SELECT t1.s^ from testSpace.supplier t1";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, asList("mysrc"));

  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 (int i = 0; i < 2; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    if (suggestion.getType().equals("COLUMN")) {
      assertTrue(expectedColumns.contains(suggestion.getName()));
    }
  }
}
 
Example 7
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 8
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSuggestFromSchemaSeparator() throws Exception {
  final String partialQuery = "Select * from mysrc.^";
  final SqlParserUtil.StringAndPos stringAndPos = SqlParserUtil.findPos(partialQuery);
  final SuggestionResponse returnedSuggestions = testSuggestSQL(stringAndPos.sql, stringAndPos.cursor, new ArrayList<String>());

  logAdvisorResponse(returnedSuggestions);
  assertNotNull(returnedSuggestions);
  assertNotNull(returnedSuggestions.getSuggestions());
  assertEquals(3, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 3; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    assertEquals("TABLE", suggestion.getType());
    assertEquals(String.format("mysrc.ds%d", i + 1), suggestion.getName());
  }
}
 
Example 9
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSuggestFromPartialDataset() throws Exception {
  final String partialQuery = "Select * from mysrc.d^";
  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(3, returnedSuggestions.getSuggestions().size());
  for (int i = 0; i < 3; i++) {
    SuggestionResponse.Suggestion suggestion = returnedSuggestions.getSuggestions().get(i);
    assertEquals("TABLE", suggestion.getType());
    assertEquals(String.format("mysrc.ds%d", i + 1), suggestion.getName());
  }
}
 
Example 10
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 11
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 12
Source Project: calcite   Source File: SqlAdvisorTest.java    License: 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 13
Source Project: calcite   Source File: AbstractSqlTester.java    License: Apache License 2.0 6 votes vote down vote up
public void assertExceptionIsThrown(String sql, String expectedMsgPattern) {
  final SqlValidator validator;
  final SqlNode sqlNode;
  final SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
  try {
    sqlNode = parseQuery(sap.sql);
    validator = getValidator();
  } catch (Throwable e) {
    checkParseEx(e, expectedMsgPattern, sap.sql);
    return;
  }

  Throwable thrown = null;
  try {
    validator.validate(sqlNode);
  } catch (Throwable ex) {
    thrown = ex;
  }

  SqlTests.checkEx(thrown, expectedMsgPattern, sap, SqlTests.Stage.VALIDATE);
}
 
Example 14
Source Project: calcite   Source File: AbstractSqlTester.java    License: Apache License 2.0 6 votes vote down vote up
public void checkIntervalConv(String sql, String expected) {
  SqlValidator validator = getValidator();
  final SqlCall n = (SqlCall) parseAndValidate(validator, sql);

  SqlNode node = null;
  for (int i = 0; i < n.operandCount(); i++) {
    node = stripAs(n.operand(i));
    if (node instanceof SqlCall) {
      node = ((SqlCall) node).operand(0);
      break;
    }
  }

  assertNotNull(node);
  SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
  long l =
      interval.getIntervalQualifier().isYearMonth()
          ? SqlParserUtil.intervalToMonths(interval)
          : SqlParserUtil.intervalToMillis(interval);
  String actual = l + "";
  assertEquals(expected, actual);
}
 
Example 15
Source Project: flink   Source File: FlinkDDLDataTypeTest.java    License: 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 = getSqlParser(sap.sql).parseStmt();
		Util.discard(sqlNode);
	} catch (Throwable ex) {
		thrown = ex;
	}

	checkEx(expectedMsgPattern, sap, thrown);
}
 
Example 16
Source Project: dremio-oss   Source File: ParserUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static SqlNode createCondition(SqlNode left, SqlOperator op, SqlNode right) {

    // if one of the operands is null, return the other
    if (left == null || right == null) {
      return left != null ? left : right;
    }

    List<Object> listCondition = Lists.newArrayList();
    listCondition.add(left);
    listCondition.add(new SqlParserUtil.ToTreeListItem(op, SqlParserPos.ZERO));
    listCondition.add(right);

    return SqlParserUtil.toTree(listCondition);
  }
 
Example 17
Source Project: dremio-oss   Source File: TestSQLAnalyzer.java    License: 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);
  }
}
 
Example 18
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 19
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 20
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void spacePDSPartialSchemaCompletion() throws Exception {
  final String partialQuery = "SELECT * from c^";
  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.CATALOGS",
      "INFORMATION_SCHEMA.COLUMNS",
      "cp.\"tpch/supplier.parquet\""
      ));

  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("cp", suggestion.getName());
    }
  }
}
 
Example 21
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 22
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 23
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 24
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 25
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 26
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 27
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 28
Source Project: dremio-oss   Source File: TestSQLResource.java    License: 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 29
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 5 votes vote down vote up
@Test // Error message identical to current. (unrecognized * intead of missing keyword FROM) Can be improved.
public void testErrorIncompleteFrom() throws Exception {
  final String partialQuery = "Select * fro^";
  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("Unknown identifier '*'", firstError.getMessage());
  assertEquals(new QueryError.Range(1,8,2,9), firstError.getRange());
}
 
Example 30
Source Project: dremio-oss   Source File: TestSQLResource.java    License: Apache License 2.0 5 votes vote down vote up
@Test // Current error-handling wraps this error in a generic parse error.
public void testErrorIncompleteSelect() throws Exception {
  final String partialQuery = "Sel^";
  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("Non-query expression encountered in illegal context", firstError.getMessage());
  assertEquals(new QueryError.Range(1,1,2,4), firstError.getRange());
}