Java Code Examples for org.apache.calcite.sql.SqlNode#toString()

The following examples show how to use org.apache.calcite.sql.SqlNode#toString() . 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: ConvMaster.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
Pair<SqlNode, SqlNode> matchSqlFunc(SqlNode sourceFunc) {
    if (sourceFunc == null || sourceDS == null || targetDS == null)
        return null;

    if (sourceFunc instanceof SqlCall || sourceFunc instanceof SqlIdentifier) {
        String funcName = sourceFunc instanceof SqlCall ? ((SqlCall) sourceFunc).getOperator().getName()
                : sourceFunc.toString();
        List<String> validDefIds = sourceDS.getFuncDefsByName(funcName);
        if (validDefIds != null) {
            for (String defId : validDefIds) {
                SqlNode sourceCandidate = sourceDS.getFuncDefSqlNode(defId);
                if (ExpressionComparator.isNodeEqual(sourceFunc, sourceCandidate, new ParamSqlNodeComparator())) {
                    SqlNode targetTmpl = targetDS.getFuncDefSqlNode(defId);
                    if (targetTmpl != null)
                        return new Pair<>(sourceCandidate, targetDS.getFuncDefSqlNode(defId));
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: ConvMaster.java    From kylin with Apache License 2.0 6 votes vote down vote up
Pair<SqlNode, SqlNode> matchSqlFunc(SqlNode sourceFunc) {
    if (sourceFunc == null || sourceDS == null || targetDS == null)
        return null;

    if (sourceFunc instanceof SqlCall || sourceFunc instanceof SqlIdentifier) {
        String funcName = sourceFunc instanceof SqlCall ? ((SqlCall) sourceFunc).getOperator().getName()
                : sourceFunc.toString();
        List<String> validDefIds = sourceDS.getFuncDefsByName(funcName);
        if (validDefIds != null) {
            for (String defId : validDefIds) {
                SqlNode sourceCandidate = sourceDS.getFuncDefSqlNode(defId);
                if (ExpressionComparator.isNodeEqual(sourceFunc, sourceCandidate, new ParamSqlNodeComparator())) {
                    SqlNode targetTmpl = targetDS.getFuncDefSqlNode(defId);
                    if (targetTmpl != null)
                        return new Pair<>(sourceCandidate, targetDS.getFuncDefSqlNode(defId));
                }
            }
        }
    }
    return null;
}
 
Example 3
Source File: AggChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Void visit(SqlIdentifier id) {
  if (isGroupExpr(id) || id.isStar()) {
    // Star may validly occur in "SELECT COUNT(*) OVER w"
    return null;
  }

  // Is it a call to a parentheses-free function?
  SqlCall call =
      SqlUtil.makeCall(
          validator.getOperatorTable(),
          id);
  if (call != null) {
    return call.accept(this);
  }

  // Didn't find the identifier in the group-by list as is, now find
  // it fully-qualified.
  // TODO: It would be better if we always compared fully-qualified
  // to fully-qualified.
  final SqlQualified fqId = scopes.peek().fullyQualify(id);
  if (isGroupExpr(fqId.identifier)) {
    return null;
  }
  SqlNode originalExpr = validator.getOriginal(id);
  final String exprString = originalExpr.toString();
  throw validator.newValidationError(originalExpr,
      distinct
          ? RESOURCE.notSelectDistinctExpr(exprString)
          : RESOURCE.notGroupExpr(exprString));
}
 
Example 4
Source File: CalciteSqlParser.java    From sylph with Apache License 2.0 5 votes vote down vote up
private TableName parserAs(SqlBasicCall sqlNode)
{
    SqlNode query = sqlNode.getOperands()[0];
    SqlNode alias = sqlNode.getOperands()[1];
    String tableName = "";
    if (query.getKind() == IDENTIFIER) {
        tableName = query.toString();
    }
    else {  //is query 子查询
        sqlParse(query);  //parser 子查询?
    }

    return new TableName(tableName, Optional.ofNullable(alias.toString()));
}
 
Example 5
Source File: Validator.java    From AthenaX with Apache License 2.0 5 votes vote down vote up
/**
 * Unwrap a constant in the AST as a Java Object.
 *
 * <p>The Calcite validator has folded all the constants by this point.
 * Thus the function expects either a SqlLiteral or a SqlIdentifier but not a SqlCall.</p>
 */
private static String unwrapConstant(SqlNode value) {
  if (value == null) {
    return null;
  } else if (value instanceof SqlLiteral) {
    return ((SqlLiteral) value).toValue();
  } else if (value instanceof SqlIdentifier) {
    return value.toString();
  } else {
    throw new IllegalArgumentException("Invalid constant " + value);
  }
}
 
Example 6
Source File: AggChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Void visit(SqlIdentifier id) {
  if (isGroupExpr(id) || id.isStar()) {
    // Star may validly occur in "SELECT COUNT(*) OVER w"
    return null;
  }

  // Is it a call to a parentheses-free function?
  final SqlCall call = validator.makeNullaryCall(id);
  if (call != null) {
    return call.accept(this);
  }

  // Didn't find the identifier in the group-by list as is, now find
  // it fully-qualified.
  // TODO: It would be better if we always compared fully-qualified
  // to fully-qualified.
  final SqlQualified fqId = scopes.peek().fullyQualify(id);
  if (isGroupExpr(fqId.identifier)) {
    return null;
  }
  SqlNode originalExpr = validator.getOriginal(id);
  final String exprString = originalExpr.toString();
  throw validator.newValidationError(originalExpr,
      distinct
          ? RESOURCE.notSelectDistinctExpr(exprString)
          : RESOURCE.notGroupExpr(exprString));
}
 
Example 7
Source File: TestSqlBracketlessSyntax.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Test
public void checkComplexExpressionParsing() throws Exception{
  FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(SqlParser.configBuilder()
          .setLex(Lex.MYSQL)
          .setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH)
          .setParserFactory(ParserImpl.FACTORY)
          .build())
      .defaultSchema(CalciteSchema.createRootSchema(false /* addMetadata */, false /* cache */).plus())
      .convertletTable(new ConvertletTable(new ContextInformation() {
        @Override
        public String getQueryUser() {
          return null;
        }

        @Override
        public String getCurrentDefaultSchema() {
          return null;
        }

        @Override
        public long getQueryStartTime() {
          return 0;
        }

        @Override
        public int getRootFragmentTimeZone() {
          return 0;
        }

        @Override
        public QueryId getLastQueryId() { return null; }

        @Override
        public void registerAdditionalInfo(AdditionalContext object) {

        }

        @Override
        public <T extends AdditionalContext> T getAdditionalInfo(Class<T> claz) {
          return null;
        }
      }))
      .build();
  Planner planner = Frameworks.getPlanner(config);

  SqlNode node = planner.parse(""
      + "select a[4].c \n"
      + "from x.y.z \n"
      + "where a.c.b = 5 and x[2] = 7 \n"
      + "group by d \n"
      + "having a.c < 5 \n"
      + "order by x.a.a.a.a.a");

  String expected = "SELECT `a`[4]['c']\n" +
      "FROM `x`.`y`.`z`\n" +
      "WHERE `a`.`c`['b'] = 5 AND `x`[2] = 7\n" +
      "GROUP BY `d`\n" +
      "HAVING `a`.`c` < 5\n" +
      "ORDER BY `x`.`a`['a']['a']['a']['a']";


  SqlNode rewritten = node.accept(new CompoundIdentifierConverter());
  String rewrittenQuery = rewritten.toString();

  DremioAssert.assertMultiLineStringEquals(expected, rewrittenQuery);
}