costas-db commented on code in PR #47428: URL: https://github.com/apache/spark/pull/47428#discussion_r1705979913
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/EvaluateUnresolvedInlineTable.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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 + +import org.apache.spark.sql.catalyst.analysis._ +import org.apache.spark.sql.catalyst.expressions.{AliasHelper, EvalHelper, Expression} +import org.apache.spark.sql.catalyst.optimizer.EvalInlineTables +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.trees.AlwaysProcess +import org.apache.spark.sql.catalyst.trees.TreePattern.CURRENT_LIKE +import org.apache.spark.sql.catalyst.types.DataTypeUtils +import org.apache.spark.sql.catalyst.util.TypeUtils.{toSQLExpr, toSQLId} +import org.apache.spark.sql.types.{StructField, StructType} + +/** + * Utility object used to replace [[UnresolvedInlineTable]] with [[ResolvedInlineTable]] or + * (whenever possible) with [[LocalRelation]]. Typically, [[UnresolvedInlineTable]] is + * a child of [[InsertIntoStatement]]. + * Use the method: [[EvaluateUnresolvedInlineTable.evaluate]] as the entry point for this + * transformation. + */ +object EvaluateUnresolvedInlineTable extends SQLConfHelper + with AliasHelper with EvalHelper with CastSupport with UnresolvedInlineTableUtils { + + def evaluate(plan: LogicalPlan): LogicalPlan = { + plan.resolveOperatorsWithPruning(AlwaysProcess.fn) { + case table: UnresolvedInlineTable if table.expressionsResolved => + validateInputDimension(table) + validateInputEvaluable(table) + val resolvedTable = findCommonTypesAndCast(table) + earlyEvalIfPossible(resolvedTable) + } + } + + /** + * This function attempts to early evaluate rows in inline table. + * If evaluation doesn't rely on non-deterministic expressions (e.g. current_like) + * expressions will be evaluated and inlined as [[LocalRelation]] + * This is package visible for unit testing. + */ + private def earlyEvalIfPossible(table: ResolvedInlineTable): LogicalPlan = { + val earlyEvalPossible = table.rows.flatten.forall(!_.containsPattern(CURRENT_LIKE)) + if (earlyEvalPossible) EvalInlineTables(table) else table + } +} + +trait UnresolvedInlineTableUtils extends SQLConfHelper Review Comment: So this PR went through a few iterations and in previous versions these methods were included in this trait because they were invoked from `ResolveInlineTables`. In the current state, these functions are just called from the `ResolveInlineTablesSuite`, but I suppose we don't have to use `ResolveInlineTables`, we can just call `EvaluateUnresolvedInlineTable` from that test. I made these changes. Let me know if that works for you. I guess one downside of the current state is that it can feel a bit counter-intuitive that we need to import an extra object in order to perform analyzer related validations/assertions instead of using the analyzer rule itself. -- 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