Search in sources :

Example 31 with SqlPrettyWriter

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);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SQLException(java.sql.SQLException) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 32 with SqlPrettyWriter

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();
}
Also used : SqlComputedColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlComputedColumn) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNode(org.apache.calcite.sql.SqlNode) ExtendedSqlNode(org.apache.flink.sql.parser.ExtendedSqlNode)

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