org.apache.calcite.rel.rel2sql.RelToSqlConverter Java Examples

The following examples show how to use org.apache.calcite.rel.rel2sql.RelToSqlConverter. 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: QuerySqlStatisticProvider.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected String toSql(RelNode rel, SqlDialect dialect) {
  final RelToSqlConverter converter = new RelToSqlConverter(dialect);
  SqlImplementor.Result result = converter.visitChild(0, rel);
  final SqlNode sqlNode = result.asStatement();
  final String sql = sqlNode.toSqlString(dialect).getSql();
  sqlConsumer.accept(sql);
  return sql;
}
 
Example #2
Source File: PigConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Pig script to a list of SQL statements.
 *
 * @param pigQuery Pig script
 * @param writer The SQL writer to decide dialect and format of SQL statements
 * @throws IOException Exception during parsing or translating Pig
 */
private List<String> pigToSql(String pigQuery, SqlWriter writer)
    throws IOException {
  final RelToSqlConverter sqlConverter =
      new PigRelToSqlConverter(writer.getDialect());
  final List<RelNode> finalRels = pigQuery2Rel(pigQuery);
  final List<String> sqlStatements = new ArrayList<>();
  for (RelNode rel : finalRels) {
    final SqlNode sqlNode = sqlConverter.visitChild(0, rel).asStatement();
    sqlNode.unparse(writer, 0, 0);
    sqlStatements.add(writer.toString());
  }
  return sqlStatements;
}
 
Example #3
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
protected void execAndCompPlan(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);
        RelNode calcitePlan = (RelNode) QueryContextFacade.current().getCalcitePlan();
        if (calcitePlan == null)
            throw new NullPointerException();

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        RelToSqlConverter converter = new RelToSqlConverter(CALCITE);
        SqlNode sqlNode = converter.visitChild(0, calcitePlan.getInput(0)).asStatement();
        String optimizedSQL = sqlNode.toSqlString(CALCITE).getSql();
        String expectedSQL = Strings.join(Files.readLines(
                new File(sqlFile.getParent(), sqlFile.getName() + ".expected"), Charset.forName("utf-8")), "\n");
        Assert.assertEquals(expectedSQL, optimizedSQL);
        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example #4
Source File: KylinTestBase.java    From kylin with Apache License 2.0 4 votes vote down vote up
protected void execAndCompPlan(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);
        RelNode calcitePlan = (RelNode) QueryContextFacade.current().getCalcitePlan();
        if (calcitePlan == null)
            throw new NullPointerException();

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        RelToSqlConverter converter = new RelToSqlConverter(CALCITE);
        SqlNode sqlNode = converter.visitChild(0, calcitePlan.getInput(0)).asStatement();
        String optimizedSQL = sqlNode.toSqlString(CALCITE).getSql();
        String expectedSQL = Strings.join(Files.readLines(
                new File(sqlFile.getParent(), sqlFile.getName() + ".expected"), Charset.forName("utf-8")), "\n");
        Assert.assertEquals(expectedSQL, optimizedSQL);
        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}