-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-34893][SS] Support session window natively #33081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
551bf52
78c6f2d
79f2197
1359123
0f8ad99
37c98de
441d6e5
b9c0357
a4fa37b
e7a2a37
bbade35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * Represent the session window. | ||
| * | ||
| * @param timeColumn the start time of session window | ||
| * @param gapDuration the duration of session gap, meaning the session will close if there is | ||
| * no new element appeared within "the last element in session + gap". | ||
| */ | ||
| case class SessionWindow(timeColumn: Expression, gapDuration: Long) extends UnaryExpression | ||
| with ImplicitCastInputTypes | ||
| with Unevaluable | ||
| with NonSQLExpression { | ||
|
|
||
| ////////////////////////// | ||
| // SQL Constructors | ||
| ////////////////////////// | ||
|
|
||
| def this(timeColumn: Expression, gapDuration: Expression) = { | ||
| this(timeColumn, TimeWindow.parseExpression(gapDuration)) | ||
| } | ||
|
|
||
| override def child: Expression = timeColumn | ||
| override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType) | ||
| override def dataType: DataType = new StructType() | ||
| .add(StructField("start", TimestampType)) | ||
| .add(StructField("end", TimestampType)) | ||
|
|
||
| // This expression is replaced in the analyzer. | ||
| override lazy val resolved = false | ||
|
|
||
| /** Validate the inputs for the gap duration in addition to the input data type. */ | ||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| val dataTypeCheck = super.checkInputDataTypes() | ||
| if (dataTypeCheck.isSuccess) { | ||
| if (gapDuration <= 0) { | ||
| return TypeCheckFailure(s"The window duration ($gapDuration) must be greater than 0.") | ||
| } | ||
| } | ||
| dataTypeCheck | ||
| } | ||
|
|
||
| override protected def withNewChildInternal(newChild: Expression): Expression = | ||
| copy(timeColumn = newChild) | ||
| } | ||
|
|
||
| object SessionWindow { | ||
| val marker = "spark.sessionWindow" | ||
|
|
||
| def apply( | ||
| timeColumn: Expression, | ||
| gapDuration: String): SessionWindow = { | ||
| SessionWindow(timeColumn, | ||
| TimeWindow.getIntervalInMicroSeconds(gapDuration)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1610,6 +1610,27 @@ object SQLConf { | |
| .checkValue(v => Set(1, 2).contains(v), "Valid versions are 1 and 2") | ||
| .createWithDefault(2) | ||
|
|
||
| val STREAMING_SESSION_WINDOW_MERGE_SESSIONS_IN_LOCAL_PARTITION = | ||
| buildConf("spark.sql.streaming.sessionWindow.merge.sessions.in.local.partition") | ||
|
||
| .internal() | ||
| .doc("When true, streaming session window sorts and merge sessions in local partition " + | ||
| "prior to shuffle. This is to reduce the rows to shuffle, but only beneficial when " + | ||
| "there're lots of rows in a batch being assigned to same sessions.") | ||
| .version("3.2.0") | ||
| .booleanConf | ||
|
||
| .createWithDefault(false) | ||
|
|
||
| val STREAMING_SESSION_WINDOW_STATE_FORMAT_VERSION = | ||
| buildConf("spark.sql.streaming.sessionWindow.stateFormatVersion") | ||
| .internal() | ||
| .doc("State format version used by streaming session window in a streaming query. " + | ||
| "State between versions are tend to be incompatible, so state format version shouldn't " + | ||
| "be modified after running.") | ||
| .version("3.2.0") | ||
| .intConf | ||
| .checkValue(v => Set(1).contains(v), "Valid version is 1") | ||
| .createWithDefault(1) | ||
|
|
||
| val UNSUPPORTED_OPERATION_CHECK_ENABLED = | ||
| buildConf("spark.sql.streaming.unsupportedOperationCheck") | ||
| .internal() | ||
|
|
@@ -3676,6 +3697,9 @@ class SQLConf extends Serializable with Logging { | |
|
|
||
| def fastHashAggregateRowMaxCapacityBit: Int = getConf(FAST_HASH_AGGREGATE_MAX_ROWS_CAPACITY_BIT) | ||
|
|
||
| def streamingSessionWindowMergeSessionInLocalPartition: Boolean = | ||
| getConf(STREAMING_SESSION_WINDOW_MERGE_SESSIONS_IN_LOCAL_PARTITION) | ||
|
|
||
| def datetimeJava8ApiEnabled: Boolean = getConf(DATETIME_JAVA8API_ENABLED) | ||
|
|
||
| def uiExplainMode: String = getConf(UI_EXPLAIN_MODE) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -324,7 +324,9 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] { | |
| throw QueryCompilationErrors.groupAggPandasUDFUnsupportedByStreamingAggError() | ||
| } | ||
|
|
||
| val stateVersion = conf.getConf(SQLConf.STREAMING_AGGREGATION_STATE_FORMAT_VERSION) | ||
| val sessionWindowOption = namedGroupingExpressions.find { p => | ||
| p.metadata.contains(SessionWindow.marker) | ||
| } | ||
|
|
||
| // Ideally this should be done in `NormalizeFloatingNumbers`, but we do it here because | ||
| // `groupingExpressions` is not extracted during logical phase. | ||
|
|
@@ -335,12 +337,29 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] { | |
| } | ||
| } | ||
|
|
||
| AggUtils.planStreamingAggregation( | ||
| normalizedGroupingExpressions, | ||
| aggregateExpressions.map(expr => expr.asInstanceOf[AggregateExpression]), | ||
| rewrittenResultExpressions, | ||
| stateVersion, | ||
| planLater(child)) | ||
| sessionWindowOption match { | ||
| case Some(sessionWindow) => | ||
| val stateVersion = conf.getConf(SQLConf.STREAMING_SESSION_WINDOW_STATE_FORMAT_VERSION) | ||
|
|
||
| AggUtils.planStreamingAggregationForSession( | ||
| normalizedGroupingExpressions, | ||
| sessionWindow, | ||
| aggregateExpressions.map(expr => expr.asInstanceOf[AggregateExpression]), | ||
| rewrittenResultExpressions, | ||
| stateVersion, | ||
| conf.streamingSessionWindowMergeSessionInLocalPartition, | ||
| planLater(child)) | ||
|
|
||
| case None => | ||
| val stateVersion = conf.getConf(SQLConf.STREAMING_AGGREGATION_STATE_FORMAT_VERSION) | ||
|
||
|
|
||
| AggUtils.planStreamingAggregation( | ||
| normalizedGroupingExpressions, | ||
| aggregateExpressions.map(expr => expr.asInstanceOf[AggregateExpression]), | ||
| rewrittenResultExpressions, | ||
| stateVersion, | ||
| planLater(child)) | ||
| } | ||
|
|
||
| case _ => Nil | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a few simple comments here? e.g, what
gapDurationstands for.