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

The following examples show how to use org.apache.calcite.sql.parser.SqlParserUtil. 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: SqlCollation.java    From Bats with 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 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 #3
Source File: TestSQLResource.java    From dremio-oss with 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 #4
Source File: TestSQLResource.java    From dremio-oss with 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 #5
Source File: TestSQLResource.java    From dremio-oss with 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 #6
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 #7
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 #8
Source File: TestSQLResource.java    From dremio-oss with 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 #9
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 #10
Source File: MycatCalciteMySqlNodeVisitor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
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 #11
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 #12
Source File: DrillParserUtil.java    From Bats with 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 #13
Source File: AbstractSqlTester.java    From calcite with 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 File: AbstractSqlTester.java    From calcite with 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 File: SqlCollation.java    From calcite with Apache License 2.0 5 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);
  this.locale = parseValues.getLocale();
  this.strength = parseValues.getStrength().toLowerCase(Locale.ROOT);
  this.collationName = generateCollationName(charset);
}
 
Example #16
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 #17
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 #18
Source File: TestSQLResource.java    From dremio-oss with 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());
}
 
Example #19
Source File: TestSQLResource.java    From dremio-oss with 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 #20
Source File: Hoist.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Variable(String originalSql, int ordinal, SqlNode node) {
  this.originalSql = Objects.requireNonNull(originalSql);
  this.ordinal = ordinal;
  this.node = Objects.requireNonNull(node);
  final SqlParserPos pos = node.getParserPosition();
  start = SqlParserUtil.lineColToIndex(originalSql,
      pos.getLineNum(), pos.getColumnNum());
  end = SqlParserUtil.lineColToIndex(originalSql,
      pos.getEndLineNum(), pos.getEndColumnNum()) + 1;

  Preconditions.checkArgument(ordinal >= 0);
  Preconditions.checkArgument(start >= 0);
  Preconditions.checkArgument(start <= end);
  Preconditions.checkArgument(end <= originalSql.length());
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 = getSqlParser(sap.sql).parseStmt();
		Util.discard(sqlNode);
	} catch (Throwable ex) {
		thrown = ex;
	}

	checkEx(expectedMsgPattern, sap, thrown);
}
 
Example #27
Source File: ParserUtil.java    From dremio-oss with 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 #28
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);
  }
}
 
Example #29
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 #30
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());
    }
  }
}