Search in sources :

Example 1 with SqlComputedColumn

use of org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlComputedColumn 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)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)1 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)1 ExtendedSqlNode (org.apache.flink.sql.parser.ExtendedSqlNode)1 SqlComputedColumn (org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlComputedColumn)1