-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Dai <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...spark-integration/src/main/scala/org/opensearch/flint/spark/function/TumbleFunction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.function | ||
|
||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.catalyst.FunctionIdentifier | ||
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo} | ||
import org.apache.spark.sql.functions.window | ||
|
||
/** | ||
* Tumble windowing function that groups row into fixed interval window without overlap. | ||
*/ | ||
object TumbleFunction { | ||
|
||
val identifier: FunctionIdentifier = FunctionIdentifier("tumble") | ||
|
||
val exprInfo: ExpressionInfo = new ExpressionInfo(classOf[Column].getCanonicalName, "window") | ||
|
||
val functionBuilder: Seq[Expression] => Expression = | ||
(children: Seq[Expression]) => { | ||
// Delegate actual implementation to window() function | ||
val timeColumn = children.head | ||
val windowDuration = children(1) | ||
window(new Column(timeColumn), windowDuration.toString()).expr | ||
} | ||
|
||
/** | ||
* Function description to register current function to Spark extension. | ||
*/ | ||
val description: (FunctionIdentifier, ExpressionInfo, FunctionBuilder) = | ||
(identifier, exprInfo, functionBuilder) | ||
} |
17 changes: 17 additions & 0 deletions
17
...-integration/src/test/scala/org/opensearch/flint/spark/function/TumbleFunctionSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.function | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
|
||
import org.apache.spark.FlintSuite | ||
|
||
class TumbleFunctionSuite extends FlintSuite with Matchers { | ||
|
||
test("should require both column name and window expression as arguments") { | ||
// TumbleFunction.functionBuilder(AttributeReference()) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...g-test/src/test/scala/org/opensearch/flint/spark/FlintSparkWindowingFunctionITSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark | ||
|
||
import java.sql.Timestamp | ||
|
||
import org.apache.spark.FlintSuite | ||
import org.apache.spark.sql.{QueryTest, Row} | ||
|
||
class FlintSparkWindowingFunctionITSuite extends QueryTest with FlintSuite { | ||
|
||
test("tumble windowing function") { | ||
val inputDF = spark | ||
.createDataFrame( | ||
Seq( | ||
(1L, "2023-01-01 00:00:00"), | ||
(2L, "2023-01-01 00:09:00"), | ||
(3L, "2023-01-01 00:15:00"))) | ||
.toDF("id", "timestamp") | ||
|
||
val resultDF = inputDF.selectExpr("TUMBLE(timestamp, '10 minutes') AS window") | ||
val expectedData = Seq( | ||
Row(Row(timestamp("2023-01-01 00:00:00"), timestamp("2023-01-01 00:10:00"))), | ||
Row(Row(timestamp("2023-01-01 00:00:00"), timestamp("2023-01-01 00:10:00"))), | ||
Row(Row(timestamp("2023-01-01 00:10:00"), timestamp("2023-01-01 00:20:00")))) | ||
|
||
checkAnswer(resultDF, expectedData) | ||
} | ||
|
||
private def timestamp(ts: String): Timestamp = Timestamp.valueOf(ts) | ||
} |