holdenk commented on code in PR #43471: URL: https://github.com/apache/spark/pull/43471#discussion_r1369374345
########## sql/core/src/main/scala/org/apache/spark/sql/execution/ExecuteUncorrelatedScalarSubquery.scala: ########## @@ -0,0 +1,89 @@ +/* + * 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.execution + +import org.apache.spark.sql.{AnalysisException, SparkSession} +import org.apache.spark.sql.catalyst.expressions +import org.apache.spark.sql.catalyst.expressions.Literal +import org.apache.spark.sql.catalyst.expressions.SubExprUtils.hasOuterReferences +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.rules.Rule + +class ExecuteUncorrelatedScalarSubquery(spark: SparkSession) extends Rule[LogicalPlan] { + override def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions { + case subquery: expressions.ScalarSubquery if !hasOuterReferences(subquery.plan) => + val result = SubqueryEvaluation.ifEnabled(spark) { + evaluate(subquery) + } + result.getOrElse(subquery) + } + + private def evaluate(subquery: expressions.ScalarSubquery): Literal = { + val qe = new QueryExecution(spark, subquery.plan) + val (resultType, rows) = SQLExecution.withNewExecutionId(qe) { + val physicalPlan = qe.executedPlan + (physicalPlan.schema.fields.head.dataType, physicalPlan.executeCollect()) + } + + if (rows.length > 1) { + throw new AnalysisException( + s"More than one row returned by a subquery used as an expression:\n${subquery.plan}") + } + + if (rows.length == 1) { + assert(rows(0).numFields == 1, + s"Expects 1 field, but got ${rows(0).numFields}; something went wrong in analysis") Review Comment: Should we through an analysis exception here too instead of the assert? ########## sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala: ########## @@ -31,12 +32,14 @@ import org.apache.spark.sql.execution.python.{ExtractGroupingPythonUDFFromAggreg class SparkOptimizer( catalogManager: CatalogManager, catalog: SessionCatalog, - experimentalMethods: ExperimentalMethods) + experimentalMethods: ExperimentalMethods, + spark: SparkSession) extends Optimizer(catalogManager) { override def earlyScanPushDownRules: Seq[Rule[LogicalPlan]] = // TODO: move SchemaPruning into catalyst Seq(SchemaPruning) :+ + new ExecuteUncorrelatedScalarSubquery(spark) :+ Review Comment: If there is a filter to push down inside of the sub query would it still get pushed down? I'm thinking of a limit 1 case might be especially common. I'm thinking it will since we launch another query but I just want to make sure. -- 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]
