Java Code Examples for org.apache.calcite.sql.parser.SqlParserPos#getColumnNum()

The following examples show how to use org.apache.calcite.sql.parser.SqlParserPos#getColumnNum() . 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: SqlConverter.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param sql
 *          the SQL sent to the server
 * @param pos
 *          the position of the error
 * @return The sql with a ^ character under the error
 */
static String formatSQLParsingError(String sql, SqlParserPos pos) {
  if (pos == null) {
    return sql;
  }
  StringBuilder sb = new StringBuilder();
  String[] lines = sql.split("\n");
  for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    sb.append(line).append("\n");
    if (i == (pos.getLineNum() - 1)) {
      for (int j = 0; j < pos.getColumnNum() - 1; j++) {
        sb.append(" ");
      }
      sb.append("^\n");
    }
  }
  return sb.toString();
}
 
Example 2
Source File: CalciteParser.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public static Pair<Integer, Integer> getReplacePos(SqlNode node, String inputSql) {
    if (inputSql == null) {
        return Pair.newPair(0, 0);
    }
    String[] lines = inputSql.split("\n");
    SqlParserPos pos = node.getParserPosition();
    int lineStart = pos.getLineNum();
    int lineEnd = pos.getEndLineNum();
    int columnStart = pos.getColumnNum() - 1;
    int columnEnd = pos.getEndColumnNum();
    //for the case that sql is multi lines
    for (int i = 0; i < lineStart - 1; i++) {
        columnStart += lines[i].length() + 1;
    }
    for (int i = 0; i < lineEnd - 1; i++) {
        columnEnd += lines[i].length() + 1;
    }
    //for calcite's bug CALCITE-1875
    Pair<Integer, Integer> startEndPos = getPosWithBracketsCompletion(inputSql, columnStart, columnEnd);
    return startEndPos;
}
 
Example 3
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static SqlNode parseSingleStatementImpl(String sql, ParserConfig parserConfig, boolean isInnerQuery) {
  SqlNodeList list = parseMultipleStatementsImpl(sql, parserConfig, isInnerQuery);
  if (list.size() > 1) {
    SqlParserPos pos = list.get(1).getParserPosition();
    int col = pos.getColumnNum();
    String first = sql.substring(0, col);
    String second = sql.substring(col-1);

    UserException.Builder builder = UserException.parseError();
    builder.message("Unable to parse multiple queries. First query is %s. Rest of submission is %s", first, second);
    throw builder.buildSilently();
  }
  SqlNode newNode = list.get(0).accept(STRING_LITERAL_CONVERTER);
  return newNode;
}
 
Example 4
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param sql
 *          the SQL sent to the server
 * @param pos
 *          the position of the error
 * @return The sql with a ^ character under the error
 */
static String formatSQLParsingError(String sql, SqlParserPos pos) {
  StringBuilder sb = new StringBuilder();
  String[] lines = sql.split("\n");
  for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    sb.append(line).append("\n");
    if (i == (pos.getLineNum() - 1)) {
      for (int j = 0; j < pos.getColumnNum() - 1; j++) {
        sb.append(" ");
      }
      sb.append("^\n");
    }
  }
  return sb.toString();
}
 
Example 5
Source File: CalciteParser.java    From kylin with Apache License 2.0 6 votes vote down vote up
public static Pair<Integer, Integer> getReplacePos(SqlNode node, String inputSql) {
    if (inputSql == null) {
        return Pair.newPair(0, 0);
    }
    String[] lines = inputSql.split("\n");
    SqlParserPos pos = node.getParserPosition();
    int lineStart = pos.getLineNum();
    int lineEnd = pos.getEndLineNum();
    int columnStart = pos.getColumnNum() - 1;
    int columnEnd = pos.getEndColumnNum();
    //for the case that sql is multi lines
    for (int i = 0; i < lineStart - 1; i++) {
        columnStart += lines[i].length() + 1;
    }
    for (int i = 0; i < lineEnd - 1; i++) {
        columnEnd += lines[i].length() + 1;
    }
    //for calcite's bug CALCITE-1875
    Pair<Integer, Integer> startEndPos = getPosWithBracketsCompletion(inputSql, columnStart, columnEnd);
    return startEndPos;
}
 
Example 6
Source File: SqlValidatorFeatureTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void validateFeature(
    Feature feature,
    SqlParserPos context) {
  if (feature.equals(disabledFeature)) {
    CalciteException ex =
        new CalciteException(
            FEATURE_DISABLED,
            null);
    if (context == null) {
      throw ex;
    }
    throw new CalciteContextException(
        "location",
        ex,
        context.getLineNum(),
        context.getColumnNum(),
        context.getEndLineNum(),
        context.getEndColumnNum());
  }
}
 
Example 7
Source File: SqlAdvisor.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ValidateErrorInfo with a SqlParserPos and an error
 * string.
 *
 * @param pos      Error position
 * @param errorMsg Error message
 */
public ValidateErrorInfo(
    SqlParserPos pos,
    String errorMsg) {
  this.startLineNum = pos.getLineNum();
  this.startColumnNum = pos.getColumnNum();
  this.endLineNum = pos.getEndLineNum();
  this.endColumnNum = pos.getEndColumnNum();
  this.errorMsg = errorMsg;
}
 
Example 8
Source File: SqlUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an exception with context.
 */
public static CalciteContextException newContextException(
    final SqlParserPos pos,
    Resources.ExInst<?> e) {
  int line = pos.getLineNum();
  int col = pos.getColumnNum();
  int endLine = pos.getEndLineNum();
  int endCol = pos.getEndColumnNum();
  return newContextException(line, col, endLine, endCol, e);
}
 
Example 9
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ValidateErrorInfo with a SqlParserPos and an error
 * string.
 *
 * @param pos      Error position
 * @param errorMsg Error message
 */
public ValidateErrorInfo(
    SqlParserPos pos,
    String errorMsg) {
  this.startLineNum = pos.getLineNum();
  this.startColumnNum = pos.getColumnNum();
  this.endLineNum = pos.getEndLineNum();
  this.endColumnNum = pos.getEndColumnNum();
  this.errorMsg = errorMsg;
}
 
Example 10
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an exception with context.
 */
public static CalciteContextException newContextException(
    final SqlParserPos pos,
    Resources.ExInst<?> e) {
  int line = pos.getLineNum();
  int col = pos.getColumnNum();
  int endLine = pos.getEndLineNum();
  int endCol = pos.getEndColumnNum();
  return newContextException(line, col, endLine, endCol, e);
}
 
Example 11
Source File: SqlAdvisor.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * 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<>();
  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);
  String hintToken =
      parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN;
  if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) {
    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;
}
 
Example 12
Source File: SqlValidatorImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void throwException(SqlParserPos parserPos) {
  throw new CalciteContextException("Failure parsing the query",
                                    new SqlValidatorException("Flatten is not supported as part of join condition", null),
                                    parserPos.getLineNum(), parserPos.getEndLineNum(),
                                    parserPos.getColumnNum(), parserPos.getEndColumnNum());
}
 
Example 13
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * 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<>();
  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);
  String hintToken =
      parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN;
  if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) {
    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;
}