Search in sources :

Example 1 with HiveParserWindowingSpec

use of org.apache.flink.table.planner.delegation.hive.copy.HiveParserWindowingSpec in project flink by apache.

the class HiveParserCalcitePlanner method genSelectForWindowing.

private RelNode genSelectForWindowing(HiveParserQB qb, RelNode srcRel, HashSet<ColumnInfo> newColumns) throws SemanticException {
    HiveParserWindowingSpec wSpec = !qb.getAllWindowingSpecs().isEmpty() ? qb.getAllWindowingSpecs().values().iterator().next() : null;
    if (wSpec == null) {
        return null;
    }
    // 1. Get valid Window Function Spec
    wSpec.validateAndMakeEffective();
    List<HiveParserWindowingSpec.WindowExpressionSpec> windowExpressions = wSpec.getWindowExpressions();
    if (windowExpressions == null || windowExpressions.isEmpty()) {
        return null;
    }
    HiveParserRowResolver inputRR = relToRowResolver.get(srcRel);
    // 2. Get RexNodes for original Projections from below
    List<RexNode> projsForWindowSelOp = new ArrayList<>(HiveParserUtils.getProjsFromBelowAsInputRef(srcRel));
    // 3. Construct new Row Resolver with everything from below.
    HiveParserRowResolver outRR = new HiveParserRowResolver();
    if (!HiveParserRowResolver.add(outRR, inputRR)) {
        LOG.warn("Duplicates detected when adding columns to RR: see previous message");
    }
    // 4. Walk through Window Expressions & Construct RexNodes for those. Update out_rwsch
    final HiveParserQBParseInfo qbp = qb.getParseInfo();
    final String selClauseName = qbp.getClauseNames().iterator().next();
    final boolean cubeRollupGrpSetPresent = (!qbp.getDestRollups().isEmpty() || !qbp.getDestGroupingSets().isEmpty() || !qbp.getDestCubes().isEmpty());
    for (HiveParserWindowingSpec.WindowExpressionSpec winExprSpec : windowExpressions) {
        if (!qbp.getDestToGroupBy().isEmpty()) {
            // Special handling of grouping function
            winExprSpec.setExpression(rewriteGroupingFunctionAST(getGroupByForClause(qbp, selClauseName), winExprSpec.getExpression(), !cubeRollupGrpSetPresent));
        }
        if (outRR.getExpression(winExprSpec.getExpression()) == null) {
            Pair<RexNode, TypeInfo> rexAndType = getWindowRexAndType(winExprSpec, srcRel);
            projsForWindowSelOp.add(rexAndType.getKey());
            // 6.2.2 Update Output Row Schema
            ColumnInfo oColInfo = new ColumnInfo(getColumnInternalName(projsForWindowSelOp.size()), rexAndType.getValue(), null, false);
            outRR.putExpression(winExprSpec.getExpression(), oColInfo);
            newColumns.add(oColInfo);
        }
    }
    return genSelectRelNode(projsForWindowSelOp, outRR, srcRel, windowExpressions);
}
Also used : ArrayList(java.util.ArrayList) ColumnInfo(org.apache.hadoop.hive.ql.exec.ColumnInfo) HiveParserWindowingSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserWindowingSpec) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) HiveParserQBParseInfo(org.apache.flink.table.planner.delegation.hive.copy.HiveParserQBParseInfo) HiveParserRowResolver(org.apache.flink.table.planner.delegation.hive.copy.HiveParserRowResolver) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

ArrayList (java.util.ArrayList)1 RexNode (org.apache.calcite.rex.RexNode)1 HiveParserQBParseInfo (org.apache.flink.table.planner.delegation.hive.copy.HiveParserQBParseInfo)1 HiveParserRowResolver (org.apache.flink.table.planner.delegation.hive.copy.HiveParserRowResolver)1 HiveParserWindowingSpec (org.apache.flink.table.planner.delegation.hive.copy.HiveParserWindowingSpec)1 ColumnInfo (org.apache.hadoop.hive.ql.exec.ColumnInfo)1 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)1