Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D732C200C00 for ; Wed, 18 Jan 2017 15:31:38 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D5CDE160B34; Wed, 18 Jan 2017 14:31:38 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E5F1A160B3A for ; Wed, 18 Jan 2017 15:31:37 +0100 (CET) Received: (qmail 15836 invoked by uid 500); 18 Jan 2017 14:31:35 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 15762 invoked by uid 99); 18 Jan 2017 14:31:35 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jan 2017 14:31:35 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 09110189A84 for ; Wed, 18 Jan 2017 14:31:35 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -7.018 X-Spam-Level: X-Spam-Status: No, score=-7.018 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 25654VWNyh5h for ; Wed, 18 Jan 2017 14:31:33 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id A84B45FB73 for ; Wed, 18 Jan 2017 14:31:32 +0000 (UTC) Received: (qmail 13675 invoked by uid 99); 18 Jan 2017 14:31:31 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jan 2017 14:31:31 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id CEC75DFA70; Wed, 18 Jan 2017 14:31:31 +0000 (UTC) From: twalthr To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request #3150: [FLINK-4693][tableApi] Add session group-windows f... Content-Type: text/plain Message-Id: <20170118143131.CEC75DFA70@git1-us-west.apache.org> Date: Wed, 18 Jan 2017 14:31:31 +0000 (UTC) archived-at: Wed, 18 Jan 2017 14:31:39 -0000 Github user twalthr commented on a diff in the pull request: https://github.com/apache/flink/pull/3150#discussion_r96645923 --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/aggregate/DataSetSessionWindowAggregateReduceGroupFunction.scala --- @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.table.runtime.aggregate + +import java.lang.Iterable + +import org.apache.flink.api.common.functions.RichGroupReduceFunction +import org.apache.flink.types.Row +import org.apache.flink.configuration.Configuration +import org.apache.flink.util.{Collector, Preconditions} + +import scala.collection.JavaConversions._ +import org.apache.flink.streaming.api.windowing.windows.TimeWindow + +/** + * It wraps the aggregate logic inside of + * [[org.apache.flink.api.java.operators.GroupReduceOperator]]. It is used for Session time-window + * on batch. + * + * @param aggregates The aggregate functions. + * @param groupKeysMapping The index mapping of group keys between intermediate aggregate Row + * and output Row. + * @param aggregateMapping The index mapping between aggregate function list and aggregated value + * index in output Row. + * @param intermediateRowArity The intermediate row field count. + * @param finalRowArity The output row field count. + * @param finalRowWindowStartPos The relative window-start field position. + * @param finalRowWindowEndPos The relative window-end field position. + * @param gap Session time window gap. + */ +class DataSetSessionWindowAggregateReduceGroupFunction( + aggregates: Array[Aggregate[_ <: Any]], + groupKeysMapping: Array[(Int, Int)], + aggregateMapping: Array[(Int, Int)], + intermediateRowArity: Int, + finalRowArity: Int, + finalRowWindowStartPos: Option[Int], + finalRowWindowEndPos: Option[Int], + gap:Long) + extends RichGroupReduceFunction[Row, Row] { + + private var aggregateBuffer: Row = _ + private var output: Row = _ + private var collector: TimeWindowPropertyCollector = _ + private var intermediateRowWindowStartPos = 0 + private var intermediateRowWindowEndPos = 0 + + override def open(config: Configuration) { + Preconditions.checkNotNull(aggregates) + Preconditions.checkNotNull(groupKeysMapping) + aggregateBuffer = new Row(intermediateRowArity) + intermediateRowWindowStartPos = intermediateRowArity - 2 + intermediateRowWindowEndPos = intermediateRowArity - 1 + output = new Row(finalRowArity) + collector = new TimeWindowPropertyCollector(finalRowWindowStartPos, finalRowWindowEndPos) + } + + /** + * For grouped intermediate aggregate Rows, divide window according to the window-start + * and window-end, merge data (within a unified window) into an aggregate buffer, calculate + * aggregated values output from aggregate buffer, and then set them into output + * Row based on the mapping relationship between intermediate aggregate data and output data. + * + * @param records Grouped intermediate aggregate Rows iterator. + * @param out The collector to hand results to. + * + */ + override def reduce(records: Iterable[Row], out: Collector[Row]): Unit = { + + var last: Row = null + var head: Row = null + var lastWindowEnd: Option[Long] = None + var currentWindowStart: Option[Long] = None + + records.foreach( + (record) => { + currentWindowStart = + Some(record.getField(intermediateRowWindowStartPos).asInstanceOf[Long]) + // initial traversal or opening a new window + if (lastWindowEnd.isEmpty || + (lastWindowEnd.isDefined && currentWindowStart.get > lastWindowEnd.get)) { + + // calculate the current window and open a new window + if (lastWindowEnd.isDefined) { + + // evaluate and emit the current window's result. + doEvaluateAndCollect(out, last, head) + } + // initiate intermediate aggregate value. + aggregates.foreach(_.initiate(aggregateBuffer)) + head = record + } + + aggregates.foreach(_.merge(record, aggregateBuffer)) + last = record + lastWindowEnd = Some(getWindowEnd(last)) + }) + + doEvaluateAndCollect(out, last, head) + + } + + def doEvaluateAndCollect( + out: Collector[Row], + last: Row, + head: Row): Unit = { + // set group keys value to final output. + groupKeysMapping.foreach { + case (after, previous) => + output.setField(after, last.getField(previous)) + } + + // evaluate final aggregate value and set to output. + aggregateMapping.foreach { + case (after, previous) => + output.setField(after, aggregates(previous).evaluate(aggregateBuffer)) + } + + // adds TimeWindow properties to output then emit output + if (finalRowWindowStartPos.isDefined || finalRowWindowEndPos.isDefined) { + val start = + head.getField(intermediateRowWindowStartPos).asInstanceOf[Long] + val end = getWindowEnd(last) + + collector.wrappedCollector = out + collector.timeWindow = new TimeWindow(start, end) --- End diff -- Yes, I think this is nicer. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---