cloud-fan commented on code in PR #45397: URL: https://github.com/apache/spark/pull/45397#discussion_r1515557219
########## sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/ConvertCommandResultToLocalRelation.scala: ########## @@ -0,0 +1,52 @@ +/* + * 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.optimizer + +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, IntegerLiteral, InterpretedMutableProjection, Predicate, Unevaluable} +import org.apache.spark.sql.catalyst.plans.logical.{CommandResult, Filter, Limit, LocalRelation, LogicalPlan, Project} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND_RESULT + +/** + * Converts local operations (i.e. ones that don't require data exchange) on `CommandResult` + * to `LocalRelation`. + */ +object ConvertCommandResultToLocalRelation extends Rule[LogicalPlan] { + + override def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning( + _.containsPattern(COMMAND_RESULT)) { + case Project(projectList, CommandResult(output, _, _, rows)) + if !projectList.exists(hasUnevaluableExpr) => + val projection = new InterpretedMutableProjection(projectList, output) + projection.initialize(0) + LocalRelation(projectList.map(_.toAttribute), rows.map(projection(_).copy())) + + case Limit(IntegerLiteral(limit), CommandResult(output, _, _, rows)) => + LocalRelation(output, rows.take(limit)) + + case Filter(condition, CommandResult(output, _, _, rows)) Review Comment: By looking at this rule, I'm on the fence now. The original target of `CommandResult` is for better UI support: https://github.com/apache/spark/commit/8013f985a4d07a948b0c22638314162819bfb2be e.g. , if you do `sql("show tables").filter(...)`, we do want to see a command result node under a filter node in the UI, even if it means extra jobs. I think certain DataFrame operations such as `df.show()`, `df.isEmpty` should just be exceptions. It looks like a single operation to users and we should not have extra jobs. But this should not be general to all operations on `CommandResult` cc @HyukjinKwon -- 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]
