Search in sources :

Example 11 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.

the class SqlPrettyWriterFixture method check.

SqlPrettyWriterFixture check() {
    final SqlWriterConfig config = transform.apply(SqlPrettyWriter.config().withDialect(AnsiSqlDialect.DEFAULT));
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(config);
    final SqlNode node;
    if (expression) {
        final SqlCall valuesCall = (SqlCall) parseQuery("VALUES (" + sql + ")");
        final SqlCall rowCall = valuesCall.operand(0);
        node = rowCall.operand(0);
    } else {
        node = parseQuery(sql);
    }
    // Describe settings
    if (desc != null) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        prettyWriter.describe(pw, true);
        pw.flush();
        final String desc = sw.toString();
        diffRepos().assertEquals("desc", this.desc, desc);
    }
    // Format
    final String formatted = prettyWriter.format(node);
    diffRepos().assertEquals("formatted", this.formatted, formatted);
    // Now parse the result, and make sure it is structurally equivalent
    // to the original.
    final String actual2 = formatted.replace("`", "\"");
    final SqlNode node2;
    if (expression) {
        final SqlCall valuesCall = (SqlCall) parseQuery("VALUES (" + actual2 + ")");
        final SqlCall rowCall = valuesCall.operand(0);
        node2 = rowCall.operand(0);
    } else {
        node2 = parseQuery(actual2);
    }
    assertTrue(node.equalsDeep(node2, Litmus.THROW));
    return this;
}
Also used : StringWriter(java.io.StringWriter) SqlCall(org.apache.calcite.sql.SqlCall) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlNode(org.apache.calcite.sql.SqlNode) PrintWriter(java.io.PrintWriter)

Example 12 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.

the class SqlPrettyWriterTest method main.

public static void main(String[] args) throws SqlParseException {
    final String sql = "select x as a, b as b, c as c, d," + " 'mixed-Case string'," + " unquotedCamelCaseId," + " \"quoted id\" " + "from" + " (select *" + " from t" + " where x = y and a > 5" + " group by z, zz" + " window w as (partition by c)," + "  w1 as (partition by c,d order by a, b" + "   range between interval '2:2' hour to minute preceding" + "    and interval '1' day following)) " + "order by gg desc nulls last, hh asc";
    final SqlNode node = SqlParser.create(sql).parseQuery();
    final SqlWriterConfig config = SqlPrettyWriter.config().withLineFolding(SqlWriterConfig.LineFolding.STEP).withSelectFolding(SqlWriterConfig.LineFolding.TALL).withFromFolding(SqlWriterConfig.LineFolding.TALL).withWhereFolding(SqlWriterConfig.LineFolding.TALL).withHavingFolding(SqlWriterConfig.LineFolding.TALL).withIndentation(4).withClauseEndsLine(true);
    System.out.println(new SqlPrettyWriter(config).format(node));
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 13 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.

the class PigConverter method pigToSql.

/**
 * Converts a Pig script to a list of SQL statements.
 *
 * @param pigQuery Pig script
 * @param sqlDialect Dialect of SQL language
 * @throws IOException Exception during parsing or translating Pig
 */
public List<String> pigToSql(String pigQuery, SqlDialect sqlDialect) throws IOException {
    final SqlWriterConfig config = SqlPrettyWriter.config().withQuoteAllIdentifiers(false).withAlwaysUseParentheses(false).withSelectListItemsOnSeparateLines(false).withIndentation(2).withDialect(sqlDialect);
    final SqlPrettyWriter writer = new SqlPrettyWriter(config);
    return pigToSql(pigQuery, writer);
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig)

Example 14 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project beam by apache.

the class BeamDDLTest method unparseAggregateFunction.

@Test
public void unparseAggregateFunction() {
    SqlIdentifier name = new SqlIdentifier("foo", SqlParserPos.ZERO);
    SqlNode jarPath = SqlLiteral.createCharString("path/to/udf.jar", SqlParserPos.ZERO);
    SqlCreateFunction createFunction = new SqlCreateFunction(SqlParserPos.ZERO, false, name, jarPath, true);
    SqlWriter sqlWriter = new SqlPrettyWriter(BeamBigQuerySqlDialect.DEFAULT);
    createFunction.unparse(sqlWriter, 0, 0);
    assertEquals("CREATE AGGREGATE FUNCTION foo USING JAR 'path/to/udf.jar'", sqlWriter.toSqlString().getSql());
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 15 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project beam by apache.

the class BeamDDLTest method unparseScalarFunction.

@Test
public void unparseScalarFunction() {
    SqlIdentifier name = new SqlIdentifier("foo", SqlParserPos.ZERO);
    SqlNode jarPath = SqlLiteral.createCharString("path/to/udf.jar", SqlParserPos.ZERO);
    SqlCreateFunction createFunction = new SqlCreateFunction(SqlParserPos.ZERO, false, name, jarPath, false);
    SqlWriter sqlWriter = new SqlPrettyWriter(BeamBigQuerySqlDialect.DEFAULT);
    createFunction.unparse(sqlWriter, 0, 0);
    assertEquals("CREATE FUNCTION foo USING JAR 'path/to/udf.jar'", sqlWriter.toSqlString().getSql());
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)29 Test (org.junit.Test)12 SqlNode (org.apache.calcite.sql.SqlNode)11 SqlWriterConfig (org.apache.calcite.sql.SqlWriterConfig)7 SQLException (java.sql.SQLException)4 SqlCall (org.apache.calcite.sql.SqlCall)4 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)4 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 PreparedStatement (java.sql.PreparedStatement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RelRoot (org.apache.calcite.rel.RelRoot)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 SqlString (org.apache.calcite.sql.util.SqlString)3 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)3 Planner (org.apache.calcite.tools.Planner)3 RelConversionException (org.apache.calcite.tools.RelConversionException)3 ValidationException (org.apache.calcite.tools.ValidationException)3 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2