-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-53687][SQL][SS][SDP] Introduce WATERMARK clause in SQL statement #52428
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
base: master
Are you sure you want to change the base?
Changes from all commits
e0b1f5d
65d9f21
988cc60
b8602bb
20a6a32
a161007
eaf357c
ccd7457
e5dda95
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 |
---|---|---|
|
@@ -376,8 +376,10 @@ createPipelineDatasetHeader | |
; | ||
|
||
streamRelationPrimary | ||
: STREAM multipartIdentifier optionsClause? tableAlias #streamTableName | ||
| STREAM LEFT_PAREN multipartIdentifier RIGHT_PAREN optionsClause? tableAlias #streamTableName | ||
: STREAM multipartIdentifier optionsClause? watermarkClause? | ||
tableAlias #streamTableName | ||
| STREAM LEFT_PAREN multipartIdentifier RIGHT_PAREN | ||
optionsClause? watermarkClause? tableAlias #streamTableName | ||
; | ||
|
||
setResetStatement | ||
|
@@ -921,6 +923,10 @@ lateralView | |
: LATERAL VIEW (OUTER)? qualifiedName LEFT_PAREN (expression (COMMA expression)*)? RIGHT_PAREN tblName=identifier (AS? colName+=identifier (COMMA colName+=identifier)*)? | ||
; | ||
|
||
watermarkClause | ||
: WATERMARK colName=namedExpression DELAY OF delay=interval | ||
; | ||
|
||
setQuantifier | ||
: DISTINCT | ||
| ALL | ||
|
@@ -995,9 +1001,11 @@ identifierComment | |
relationPrimary | ||
: streamRelationPrimary #streamRelation | ||
| identifierReference temporalClause? | ||
optionsClause? sample? tableAlias #tableName | ||
| LEFT_PAREN query RIGHT_PAREN sample? tableAlias #aliasedQuery | ||
| LEFT_PAREN relation RIGHT_PAREN sample? tableAlias #aliasedRelation | ||
optionsClause? sample? watermarkClause? tableAlias #tableName | ||
| LEFT_PAREN query RIGHT_PAREN sample? watermarkClause? | ||
tableAlias #aliasedQuery | ||
| LEFT_PAREN relation RIGHT_PAREN sample? | ||
watermarkClause? tableAlias #aliasedRelation | ||
| inlineTable #inlineTableDefault2 | ||
| functionTable #tableValuedFunction | ||
; | ||
|
@@ -1006,6 +1014,8 @@ optionsClause | |
: WITH options=propertyList | ||
; | ||
|
||
// Unlike all other types of expression for relation, we do not support watermarkClause for | ||
// inlineTable. | ||
inlineTable | ||
: VALUES expression (COMMA expression)* tableAlias | ||
; | ||
|
@@ -1042,10 +1052,13 @@ functionTableArgument | |
| functionArgument | ||
; | ||
|
||
// This is only used in relationPrimary where having watermarkClause makes sense. If this becomes | ||
// referred by other clause, please check wheter watermarkClause makes sense to the clause. | ||
// If not, consider separate this rule. | ||
functionTable | ||
: funcName=functionName LEFT_PAREN | ||
(functionTableArgument (COMMA functionTableArgument)*)? | ||
RIGHT_PAREN tableAlias | ||
RIGHT_PAREN watermarkClause? tableAlias | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the doc https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-watermark, it looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouch I think it's missed in that doc. |
||
; | ||
|
||
tableAlias | ||
|
@@ -1793,6 +1806,7 @@ ansiNonReserved | |
| DEFAULT | ||
| DEFINED | ||
| DEFINER | ||
| DELAY | ||
| DELETE | ||
| DELIMITED | ||
| DESC | ||
|
@@ -2035,6 +2049,7 @@ ansiNonReserved | |
| WEEK | ||
| WEEKS | ||
| WHILE | ||
| WATERMARK | ||
| WINDOW | ||
| WITHOUT | ||
| YEAR | ||
|
@@ -2160,6 +2175,7 @@ nonReserved | |
| DEFAULT | ||
| DEFINED | ||
| DEFINER | ||
| DELAY | ||
| DELETE | ||
| DELIMITED | ||
| DESC | ||
|
@@ -2439,6 +2455,7 @@ nonReserved | |
| VIEW | ||
| VIEWS | ||
| VOID | ||
| WATERMARK | ||
| WEEK | ||
| WEEKS | ||
| WHILE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.analysis | ||
|
||
import org.apache.spark.sql.AnalysisException | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, NamedExpression} | ||
import org.apache.spark.sql.catalyst.plans.logical.{EventTimeWatermark, LogicalPlan, Project} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.catalyst.trees.TreePattern | ||
|
||
/** | ||
* Resolve [[UnresolvedEventTimeWatermark]] to [[EventTimeWatermark]]. | ||
*/ | ||
object ResolveEventTimeWatermark extends Rule[LogicalPlan] { | ||
override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUpWithPruning( | ||
_.containsPattern(TreePattern.UNRESOLVED_EVENT_TIME_WATERMARK), ruleId) { | ||
|
||
case u: UnresolvedEventTimeWatermark if u.eventTimeColExpr.resolved && u.childrenResolved => | ||
val uuid = java.util.UUID.randomUUID() | ||
|
||
if (u.eventTimeColExpr.isInstanceOf[MultiAlias]) { | ||
throw new AnalysisException( | ||
errorClass = "CANNOT_USE_MULTI_ALIASES_IN_WATERMARK_CLAUSE", | ||
messageParameters = Map() | ||
) | ||
} | ||
|
||
val namedExpression = u.eventTimeColExpr match { | ||
case e: NamedExpression => e | ||
case e: Expression => UnresolvedAlias(e) | ||
} | ||
|
||
if (u.child.outputSet.contains(namedExpression)) { | ||
// We don't need to have projection since the attribute being referenced will be available. | ||
EventTimeWatermark(uuid, namedExpression.toAttribute, u.delay, u.child) | ||
} else { | ||
// We need to inject projection as we can't find the matching column directly in the | ||
// child output. | ||
val proj = Project(Seq(namedExpression, UnresolvedStar(None)), u.child) | ||
val attrRef = proj.projectList.head.toAttribute | ||
EventTimeWatermark(uuid, attrRef, u.delay, proj) | ||
} | ||
} | ||
} |
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.
watermarkClause
is already defined instreamRelationPrimary
. Do we still need it here? Is it also applied for non-stream relation?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.
relation here can be a temp view which could be technically streaming without STREAM keyword