Search in sources :

Example 6 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.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 7 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter in project flink by apache.

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)

Example 8 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter in project hazelcast by hazelcast.

the class SqlCreateMapping method unparse.

public static String unparse(Mapping mapping) {
    SqlPrettyWriter writer = new SqlPrettyWriter(SqlPrettyWriter.config());
    writer.keyword("CREATE MAPPING");
    writer.identifier(mapping.name(), true);
    // external name defaults to mapping name - omit it if it's equal
    if (mapping.externalName() != null && !mapping.externalName().equals(mapping.name())) {
        writer.keyword("EXTERNAL NAME");
        writer.identifier(mapping.externalName(), true);
    }
    List<MappingField> fields = mapping.fields();
    if (fields.size() > 0) {
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (MappingField field : fields) {
            printIndent(writer);
            writer.identifier(field.name(), true);
            writer.print(field.type().getTypeFamily().toString());
            if (field.externalName() != null) {
                writer.print(" ");
                writer.keyword("EXTERNAL NAME");
                writer.identifier(field.externalName(), true);
            }
        }
        writer.newlineAndIndent();
        writer.endList(frame);
    }
    writer.newlineAndIndent();
    writer.keyword("TYPE");
    writer.print(mapping.type());
    Map<String, String> options = mapping.options();
    if (options.size() > 0) {
        writer.newlineAndIndent();
        writer.keyword("OPTIONS");
        SqlWriter.Frame withFrame = writer.startList("(", ")");
        for (Map.Entry<String, String> option : options.entrySet()) {
            printIndent(writer);
            writer.literal(writer.getDialect().quoteStringLiteral(option.getKey()));
            writer.print("= ");
            writer.literal(writer.getDialect().quoteStringLiteral(option.getValue()));
        }
        writer.newlineAndIndent();
        writer.endList(withFrame);
    }
    return writer.toString();
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) MappingField(com.hazelcast.sql.impl.schema.MappingField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter in project hazelcast by hazelcast.

the class UnparseTest method checkQuery.

private void checkQuery(String query) {
    final SqlNode node = context.parse(query).getNode();
    final SqlPrettyWriter writer = new SqlPrettyWriter(SqlPrettyWriter.config());
    node.unparse(writer, 0, 0);
    final String result = writer.toSqlString().toString();
    assertEquals(query, result);
    assertNotNull(context.parse(result).getNode());
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with SqlPrettyWriter

use of org.apache.beam.vendor.calcite.v1_28_0.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());
}
Also used : SqlWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)22 Test (org.junit.Test)13 SqlNode (org.apache.calcite.sql.SqlNode)7 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 SqlString (org.apache.calcite.sql.util.SqlString)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)2 SqlWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter)2 SqlPrettyWriter (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.pretty.SqlPrettyWriter)2 SqlCall (org.apache.calcite.sql.SqlCall)2 SqlNodeList (org.apache.calcite.sql.SqlNodeList)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 SqlWriter (org.apache.calcite.sql.SqlWriter)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 MappingField (com.hazelcast.sql.impl.schema.MappingField)1