steveyzzz commented on code in PR #60629:
URL: https://github.com/apache/spark/pull/60629#discussion_r4630976288
##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -85,7 +85,10 @@ options { tokenVocab = SqlBaseLexer; }
la == DISTRIBUTE || la == SORT || la == LIMIT || la == OFFSET ||
la == AGGREGATE || la == WINDOW || la == LATERAL || la == BIN ||
(la == GAP && _input.LA(3) == FILL && _input.LA(4) == LEFT_PAREN &&
- (_input.LA(5) == PARTITION || _input.LA(5) == ORDER) &&
_input.LA(6) == BY);
+ (_input.LA(5) == PARTITION || _input.LA(5) == ORDER) &&
_input.LA(6) == BY) ||
+ // Requiring an assignment keeps `a | eval`, with a column named
eval, a bitwise OR. DOT
+ // matches a multi-part key, which the parser captures to report a
dedicated error.
+ (la == EVAL && (_input.LA(4) == EQ || _input.LA(4) == DOT));
Review Comment:
Fixed in 1baac45d42. After `| EVAL`, the token that would start the name
can't be `)`, `]` or `END`, and `IDENTIFIER('c')` counts as one name, so `|
EVAL IDENTIFIER('z') = 1` works too. The "pipe EVAL after the single-character
pipe" test in `PlanParserSuite` now covers `(a | eval) = 1`, `(a | eval) == 1`,
`(a | eval).f = 1`, `abs(a | eval) = 1`, `arr[a | eval] = 1` and `CASE WHEN c
THEN a | eval END = 1`, plus the `IDENTIFIER` name and the multi-part key after
a single `|`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -800,6 +808,32 @@ case class UnresolvedStarExceptOrReplace(
filterColumns(expandedCols, exceptPairs)
}
+
+ /**
+ * Expands the star, replacing the columns that match an EXCEPT name and
appending the
+ * replacements of the EXCEPT names that match no column, in EXCEPT list
order.
+ */
+ private def expandStarAppendingMissingReplacements(
+ parameters: ExpandStarParameters): Seq[NamedExpression] = {
+ val replacementList = replacements match {
+ case Some(list) if list.size == excepts.size => list
+ case _ => throw SparkException.internalError(
+ "Appending missing replacements requires one replacement per except")
+ }
+ val expandedAttributes =
AttributeSeq(super.expandStar(parameters).map(_.toAttribute))
+ val (presentExceptsAndReplacements, missingExceptsAndReplacements) =
+ excepts.zip(replacementList).partition { case (exceptParts, _) =>
+ expandedAttributes.resolve(exceptParts, parameters.resolver).isDefined
Review Comment:
`SELECT x, x FROM t |> EVAL x = 1` now fails with `AMBIGUOUS_REFERENCE`,
with a test in `pipe-operators.sql`. I left `SET` unchanged here and am fixing
it the same way in a separate PR, which also looks up each replacement by its
EXCEPT name and adds your out-of-order case to
`UnresolvedStarExceptOrReplaceSuite`. I'll link it here once it's open.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -8512,30 +8514,43 @@ class AstBuilder extends DataTypeAstBuilder
withQueryResultClauses(c, PipeOperator(left), forPipeOperators = true)
}.getOrElse(
visitOperatorPipeAggregate(ctx, left)
- ))))))))))))))
+ )))))))))))))))
}
- private def visitOperatorPipeSet(
- ctx: OperatorPipeRightSideContext, left: LogicalPlan): LogicalPlan = {
+ /**
+ * Builds the pipe operator SET or EVAL, named by `clause`. Both replace an
assigned column that
+ * exists in place. EVAL appends a column that does not exist, as EXTEND
would, where SET fails.
+ */
+ private def visitOperatorPipeSetOrEval(
+ ctx: OperatorPipeRightSideContext, left: LogicalPlan, clause: String):
LogicalPlan = {
+ val assignments = ctx.operatorPipeSetAssignmentSeq()
+ withOrigin(assignments) {
+ if (!assignments.DOT.isEmpty) {
+ operationNotAllowed(
+ s"SQL pipe syntax |> $clause operator with multi-part assignment key
" +
+ s"(only single-part keys are allowed)", assignments)
+ }
+ }
val (setIdentifiers: Seq[String], setTargets: Seq[Expression]) =
- visitOperatorPipeSetAssignmentSeq(ctx.operatorPipeSetAssignmentSeq())
+ visitOperatorPipeSetAssignmentSeq(assignments)
+ val appendMissingColumns = clause == PipeOperators.evalClause
Review Comment:
Done: the call site passes `appendMissingColumns`, and `SET` and `EVAL`
share one branch through `Option(ctx.SET).orElse(Option(ctx.EVAL))`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -8512,30 +8514,43 @@ class AstBuilder extends DataTypeAstBuilder
withQueryResultClauses(c, PipeOperator(left), forPipeOperators = true)
}.getOrElse(
visitOperatorPipeAggregate(ctx, left)
- ))))))))))))))
+ )))))))))))))))
}
- private def visitOperatorPipeSet(
- ctx: OperatorPipeRightSideContext, left: LogicalPlan): LogicalPlan = {
+ /**
+ * Builds the pipe operator SET or EVAL, named by `clause`. Both replace an
assigned column that
+ * exists in place. EVAL appends a column that does not exist, as EXTEND
would, where SET fails.
+ */
+ private def visitOperatorPipeSetOrEval(
+ ctx: OperatorPipeRightSideContext, left: LogicalPlan, clause: String):
LogicalPlan = {
+ val assignments = ctx.operatorPipeSetAssignmentSeq()
+ withOrigin(assignments) {
+ if (!assignments.DOT.isEmpty) {
+ operationNotAllowed(
+ s"SQL pipe syntax |> $clause operator with multi-part assignment key
" +
+ s"(only single-part keys are allowed)", assignments)
+ }
+ }
val (setIdentifiers: Seq[String], setTargets: Seq[Expression]) =
- visitOperatorPipeSetAssignmentSeq(ctx.operatorPipeSetAssignmentSeq())
+ visitOperatorPipeSetAssignmentSeq(assignments)
+ val appendMissingColumns = clause == PipeOperators.evalClause
var plan = left
setIdentifiers.zip(setTargets).foreach {
case (_, _: Alias) =>
Review Comment:
Dropped the branch.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -8512,30 +8514,43 @@ class AstBuilder extends DataTypeAstBuilder
withQueryResultClauses(c, PipeOperator(left), forPipeOperators = true)
}.getOrElse(
visitOperatorPipeAggregate(ctx, left)
- ))))))))))))))
+ )))))))))))))))
}
- private def visitOperatorPipeSet(
- ctx: OperatorPipeRightSideContext, left: LogicalPlan): LogicalPlan = {
+ /**
+ * Builds the pipe operator SET or EVAL, named by `clause`. Both replace an
assigned column that
+ * exists in place. EVAL appends a column that does not exist, as EXTEND
would, where SET fails.
+ */
+ private def visitOperatorPipeSetOrEval(
+ ctx: OperatorPipeRightSideContext, left: LogicalPlan, clause: String):
LogicalPlan = {
+ val assignments = ctx.operatorPipeSetAssignmentSeq()
+ withOrigin(assignments) {
+ if (!assignments.DOT.isEmpty) {
+ operationNotAllowed(
+ s"SQL pipe syntax |> $clause operator with multi-part assignment key
" +
+ s"(only single-part keys are allowed)", assignments)
+ }
+ }
val (setIdentifiers: Seq[String], setTargets: Seq[Expression]) =
- visitOperatorPipeSetAssignmentSeq(ctx.operatorPipeSetAssignmentSeq())
+ visitOperatorPipeSetAssignmentSeq(assignments)
+ val appendMissingColumns = clause == PipeOperators.evalClause
var plan = left
setIdentifiers.zip(setTargets).foreach {
case (_, _: Alias) =>
operationNotAllowed(
- "SQL pipe syntax |> SET operator with an alias assigned with [AS]
aliasName", ctx)
+ s"SQL pipe syntax |> $clause operator with an alias assigned with
[AS] aliasName", ctx)
case (ident, target) =>
- // Add an UnresolvedStarExceptOrReplace to exclude the SET expression
name from the relation
- // and add the new SET expression to the projection list.
+ // Add an UnresolvedStarExceptOrReplace to exclude the assigned column
from the relation
+ // and add the new expression to the projection list.
// Use a PipeSelect expression to make sure it does not contain any
aggregate functions.
Review Comment:
Fixed.
##########
docs/sql-pipe-syntax.md:
##########
@@ -299,6 +303,37 @@ VALUES (0), (1) tab(col)
+---+
```
+#### EVAL
+
+```sql
+|> EVAL <column> = <expression>, ...
+```
+
+Sets columns of the input table to the result of evaluating the provided
expressions. If a column
+appears in the input table, `EVAL` replaces it in place, as `SET` does.
Otherwise, `EVAL` appends it
+after the existing columns, as `EXTEND` does. Each such column reference may
appear in the input
Review Comment:
Done, with your wording.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]