use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class JdbcTable method generateSql.
SqlString generateSql() {
final SqlNodeList selectList = SqlNodeList.SINGLETON_STAR;
SqlSelect node = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList, tableName(), null, null, null, null, null, null, null, null);
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true).withDialect(jdbcSchema.dialect);
final SqlPrettyWriter writer = new SqlPrettyWriter(config);
node.unparse(writer, 0, 0);
return writer.toSqlString();
}
use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class IntervalSqlType method generateTypeString.
// ~ Methods ----------------------------------------------------------------
@Override
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
sb.append("INTERVAL ");
final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(false).withSelectListItemsOnSeparateLines(false).withIndentation(0).withDialect(dialect);
final SqlPrettyWriter writer = new SqlPrettyWriter(config);
intervalQualifier.unparse(writer, 0, 0);
final String sql = writer.toString();
sb.append(new SqlString(dialect, sql).getSql());
}
use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class RelToSqlConverterTest method toSql.
/**
* Converts a relational expression to SQL in a given dialect
* and with a particular writer configuration.
*/
private static String toSql(RelNode root, SqlDialect dialect, UnaryOperator<SqlWriterConfig> transform) {
final RelToSqlConverter converter = new RelToSqlConverter(dialect);
final SqlNode sqlNode = converter.visitRoot(root).asStatement();
return sqlNode.toSqlString(c -> transform.apply(c.withDialect(dialect))).getSql();
}
use of org.apache.calcite.sql.SqlWriterConfig 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.SqlWriterConfig 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));
}
Aggregations