use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project calcite by apache.
the class ExtensionDdlExecutor method populate.
/**
* Populates the table called {@code name} by executing {@code query}.
*/
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
// Generate, prepare and execute an "INSERT INTO table query" statement.
// (It's a bit inefficient that we convert from SqlNode to SQL and back
// again.)
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(Objects.requireNonNull(Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath())).plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringBuilder buf = new StringBuilder();
final SqlPrettyWriter w = new SqlPrettyWriter(SqlPrettyWriter.config().withDialect(CalciteSqlDialect.DEFAULT).withAlwaysUseParentheses(false), buf);
buf.append("INSERT INTO ");
name.unparse(w, 0, 0);
buf.append(" ");
query.unparse(w, 0, 0);
final String sql = buf.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepareStatement(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw Util.throwAsRuntime(e);
}
}
use of org.apache.calcite.sql.pretty.SqlPrettyWriter in project flink-mirror by flink-ci.
the class SqlCreateTable method getColumnSqlString.
/**
* Returns the projection format of the DDL columns(including computed columns). i.e. the
* following DDL:
*
* <pre>
* create table tbl1(
* col1 int,
* col2 varchar,
* col3 as to_timestamp(col2)
* ) with (
* 'connector' = 'csv'
* )
* </pre>
*
* <p>is equivalent with query "col1, col2, to_timestamp(col2) as col3", caution that the
* "computed column" operands have been reversed.
*/
public String getColumnSqlString() {
SqlPrettyWriter writer = new SqlPrettyWriter(SqlPrettyWriter.config().withDialect(AnsiSqlDialect.DEFAULT).withAlwaysUseParentheses(true).withSelectListItemsOnSeparateLines(false).withIndentation(0));
writer.startList("", "");
for (SqlNode column : columnList) {
writer.sep(",");
SqlTableColumn tableColumn = (SqlTableColumn) column;
if (tableColumn instanceof SqlComputedColumn) {
SqlComputedColumn computedColumn = (SqlComputedColumn) tableColumn;
computedColumn.getExpr().unparse(writer, 0, 0);
writer.keyword("AS");
}
tableColumn.getName().unparse(writer, 0, 0);
}
return writer.toString();
}
Aggregations