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;
}
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));
}
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);
}
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());
}
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());
}
Aggregations