Search in sources :

Example 1 with SqlProperty

use of org.apache.flink.sql.parser.SqlProperty in project flink by apache.

the class RichSqlHiveInsert method getPartKeyToSpec.

private static Map<SqlIdentifier, SqlProperty> getPartKeyToSpec(SqlNodeList staticSpec) {
    Map<SqlIdentifier, SqlProperty> res = new HashMap<>();
    if (staticSpec != null) {
        for (SqlNode node : staticSpec) {
            SqlProperty spec = (SqlProperty) node;
            res.put(spec.getKey(), spec);
        }
    }
    return res;
}
Also used : SqlProperty(org.apache.flink.sql.parser.SqlProperty) HashMap(java.util.HashMap) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlProperty

use of org.apache.flink.sql.parser.SqlProperty in project flink by apache.

the class RichSqlInsert method getStaticPartitionKVs.

/**
 * Get static partition key value pair as strings.
 *
 * <p>For character literals we return the unquoted and unescaped values. For other types we use
 * {@link SqlLiteral#toString()} to get the string format of the value literal. If the string
 * format is not what you need, use {@link #getStaticPartitions()}.
 *
 * @return the mapping of column names to values of partition specifications, returns an empty
 *     map if there is no partition specifications.
 */
public LinkedHashMap<String, String> getStaticPartitionKVs() {
    LinkedHashMap<String, String> ret = new LinkedHashMap<>();
    if (this.staticPartitions.size() == 0) {
        return ret;
    }
    for (SqlNode node : this.staticPartitions.getList()) {
        SqlProperty sqlProperty = (SqlProperty) node;
        Comparable comparable = SqlLiteral.value(sqlProperty.getValue());
        String value = comparable instanceof NlsString ? ((NlsString) comparable).getValue() : comparable.toString();
        ret.put(sqlProperty.getKey().getSimple(), value);
    }
    return ret;
}
Also used : SqlProperty(org.apache.flink.sql.parser.SqlProperty) NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) LinkedHashMap(java.util.LinkedHashMap) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlProperty

use of org.apache.flink.sql.parser.SqlProperty in project flink by apache.

the class RichSqlHiveInsert method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.startList(SqlWriter.FrameTypeEnum.SELECT);
    String insertKeyword = "INSERT INTO";
    if (isUpsert()) {
        insertKeyword = "UPSERT INTO";
    } else if (isOverwrite()) {
        insertKeyword = "INSERT OVERWRITE";
    }
    writer.sep(insertKeyword);
    final int opLeft = getOperator().getLeftPrec();
    final int opRight = getOperator().getRightPrec();
    getTargetTable().unparse(writer, opLeft, opRight);
    if (getTargetColumnList() != null) {
        getTargetColumnList().unparse(writer, opLeft, opRight);
    }
    writer.newlineAndIndent();
    if (allPartKeys != null && allPartKeys.size() > 0) {
        writer.keyword("PARTITION");
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (SqlNode node : allPartKeys) {
            writer.sep(",", false);
            SqlIdentifier partKey = (SqlIdentifier) node;
            SqlProperty spec = partKeyToSpec.get(partKey);
            if (spec != null) {
                spec.unparse(writer, leftPrec, rightPrec);
            } else {
                partKey.unparse(writer, leftPrec, rightPrec);
            }
        }
        writer.endList(frame);
        writer.newlineAndIndent();
    }
    getSource().unparse(writer, 0, 0);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlProperty(org.apache.flink.sql.parser.SqlProperty) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)3 SqlProperty (org.apache.flink.sql.parser.SqlProperty)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 SqlWriter (org.apache.calcite.sql.SqlWriter)1 NlsString (org.apache.calcite.util.NlsString)1