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);
}
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();
}
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();
}
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());
}
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());
}
Aggregations