Search in sources :

Example 21 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method assertExprPrintsTo.

protected void assertExprPrintsTo(boolean newlines, final String sql, String expected) {
    final SqlCall valuesCall = (SqlCall) parseQuery("VALUES (" + sql + ")");
    final SqlCall rowCall = valuesCall.operand(0);
    final SqlNode node = rowCall.operand(0);
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setAlwaysUseParentheses(false);
    if (newlines) {
        prettyWriter.setCaseClausesOnNewLines(true);
    }
    String actual = prettyWriter.format(node);
    getDiffRepos().assertEquals("formatted", expected, actual);
    // Now parse the result, and make sure it is structurally equivalent
    // to the original.
    final String actual2 = actual.replaceAll("`", "\"");
    final SqlNode valuesCall2 = parseQuery("VALUES (" + actual2 + ")");
    assertTrue(valuesCall.equalsDeep(valuesCall2, Litmus.THROW));
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 22 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method assertPrintsTo.

protected void assertPrintsTo(boolean newlines, final String sql, String expected) {
    final SqlNode node = parseQuery(sql);
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setAlwaysUseParentheses(false);
    if (newlines) {
        prettyWriter.setCaseClausesOnNewLines(true);
    }
    String actual = prettyWriter.format(node);
    getDiffRepos().assertEquals("formatted", expected, actual);
    // Now parse the result, and make sure it is structurally equivalent
    // to the original.
    final String actual2 = actual.replaceAll("`", "\"");
    final SqlNode node2 = parseQuery(actual2);
    assertTrue(node.equalsDeep(node2, Litmus.THROW));
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 23 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testParenthesizeAllExprs.

@Test
public void testParenthesizeAllExprs() throws Exception {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setAlwaysUseParentheses(true);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Example 24 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testIndent8.

@Test
public void testIndent8() throws Exception {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setIndentation(8);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Example 25 with SqlPrettyWriter

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

the class SqlPrettyWriterTest method testClausesNotOnNewLine.

@Test
public void testClausesNotOnNewLine() throws Exception {
    final SqlPrettyWriter prettyWriter = new SqlPrettyWriter(AnsiSqlDialect.DEFAULT);
    prettyWriter.setClauseStartsLine(false);
    checkSimple(prettyWriter, "${desc}", "${formatted}");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Test(org.junit.Test)

Aggregations

SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)30 SqlNode (org.apache.calcite.sql.SqlNode)12 Test (org.junit.Test)12 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