Search in sources :

Example 6 with SqlPrettyWriter

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

the class SqlDdlNodes 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(context.getRootSchema().plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
        pw.print("INSERT INTO ");
        name.unparse(w, 0, 0);
        pw.print(" ");
        query.unparse(w, 0, 0);
        pw.flush();
        final String sql = sw.toString();
        final SqlNode query1 = planner.parse(sql);
        final SqlNode query2 = planner.validate(query1);
        final RelRoot r = planner.rel(query2);
        final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
        int rowCount = prepare.executeUpdate();
        Util.discard(rowCount);
        prepare.close();
    } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
        throw new RuntimeException(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) StringWriter(java.io.StringWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) PrintWriter(java.io.PrintWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlPrettyWriter

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

the class SqlNode method toSqlString.

/**
 * Returns the SQL text of the tree of which this <code>SqlNode</code> is
 * the root.
 *
 * @param dialect     Dialect
 * @param forceParens wraps all expressions in parentheses; good for parse
 *                    test, but false by default.
 *
 *                    <p>Typical return values are:</p>
 *                    <ul>
 *                    <li>'It''s a bird!'</li>
 *                    <li>NULL</li>
 *                    <li>12.3</li>
 *                    <li>DATE '1969-04-29'</li>
 *                    </ul>
 */
public SqlString toSqlString(SqlDialect dialect, boolean forceParens) {
    if (dialect == null) {
        dialect = AnsiSqlDialect.DEFAULT;
    }
    SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
    writer.setAlwaysUseParentheses(forceParens);
    writer.setSelectListItemsOnSeparateLines(false);
    writer.setIndentation(0);
    unparse(writer, 0, 0);
    final String sql = writer.toString();
    return new SqlString(dialect, sql);
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

Example 8 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter 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();
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig)

Example 9 with SqlPrettyWriter

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

the class SqlNode method toSqlString.

/**
 * Returns the SQL text of the tree of which this <code>SqlNode</code> is
 * the root.
 *
 * <p>Typical return values are:
 *
 * <ul>
 * <li>'It''s a bird!'
 * <li>NULL
 * <li>12.3
 * <li>DATE '1969-04-29'
 * </ul>
 *
 * @param transform   Transform that sets desired writer configuration
 */
public SqlString toSqlString(UnaryOperator<SqlWriterConfig> transform) {
    final SqlWriterConfig config = transform.apply(SqlPrettyWriter.config());
    SqlPrettyWriter writer = new SqlPrettyWriter(config);
    unparse(writer, 0, 0);
    return writer.toSqlString();
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter)

Example 10 with SqlPrettyWriter

use of org.apache.calcite.sql.pretty.SqlPrettyWriter 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());
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlWriterConfig(org.apache.calcite.sql.SqlWriterConfig) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

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