vladimirg-db commented on code in PR #50699: URL: https://github.com/apache/spark/pull/50699#discussion_r2063080040
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala: ########## @@ -442,14 +442,35 @@ package object dsl extends SQLConfHelper { otherPlan) } - def orderBy(sortExprs: SortOrder*): LogicalPlan = Sort(sortExprs, true, logicalPlan) + def orderBy(sortExprs: SortOrder*): LogicalPlan = { + val sortExpressionsWithOrdinals = sortExprs.map(replaceOrdinalsInSortOrder) + Sort(sortExpressionsWithOrdinals, true, logicalPlan) + } + + def sortBy(sortExprs: SortOrder*): LogicalPlan = { + val sortExpressionsWithOrdinals = sortExprs.map(replaceOrdinalsInSortOrder) + Sort(sortExpressionsWithOrdinals, false, logicalPlan) + } - def sortBy(sortExprs: SortOrder*): LogicalPlan = Sort(sortExprs, false, logicalPlan) + /** + * Replaces top-level integer literals from [[SortOrder]] with [[UnresolvedOrdinal]], if + * `orderByOrdinal` is enabled. + */ + private def replaceOrdinalsInSortOrder(sortOrder: SortOrder): SortOrder = sortOrder match { + case sortOrderByOrdinal @ SortOrder(literal @ Literal(value: Int, IntegerType), _, _, _) + if conf.orderByOrdinal => + val ordinal = CurrentOrigin.withOrigin(literal.origin) { UnresolvedOrdinal(value) } Review Comment: Do we need to copy tags here as well? ########## sql/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala: ########## @@ -4108,7 +4108,8 @@ class SparkConnectPlanner( */ private def replaceIntegerLiteralWithOrdinal(groupingExpression: Expression) = groupingExpression match { - case Literal(value: Int, IntegerType) => UnresolvedOrdinal(value) + case literal @ Literal(value: Int, IntegerType) => + CurrentOrigin.withOrigin(literal.origin) { UnresolvedOrdinal(value) } Review Comment: I wonder why we need that change? ########## sql/core/src/test/scala/org/apache/spark/sql/ReplaceIntegerLiteralsWithOrdinalsDataframeSuite.scala: ########## @@ -0,0 +1,127 @@ +/* + * 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 + +import org.apache.spark.sql.catalyst.analysis.UnresolvedOrdinal +import org.apache.spark.sql.catalyst.expressions.SortOrder +import org.apache.spark.sql.functions.lit +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.test.SharedSparkSession + +class ReplaceIntegerLiteralsWithOrdinalsDataframeSuite extends QueryTest with SharedSparkSession { Review Comment: We can also add the following tests: - `groupBy(-1)` - `groupBy(UnaryMinus(-1))` - `groupBy("ALL").groupBy(100)` -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org