cloud-fan commented on code in PR #51714:
URL: https://github.com/apache/spark/pull/51714#discussion_r2242103020


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -445,6 +449,42 @@ case class CreateSQLFunctionCommand(
     }
   }
 
+  /**
+   * Derive the SQL data access routine of the function and check if the SQL 
function matches
+   * its data access routine. If the data access is CONTAINS SQL, the 
expression should not
+   * access operators and expressions that read SQL data.
+   *
+   * Returns true is SQL data access routine is READS SQL DATA, otherwise 
returns false.
+   */
+  private def deriveSQLDataAccess(plan: LogicalPlan): Boolean = {
+    // Find logical plan nodes that read SQL data.
+    val readsSQLData = plan.find {
+      case _: View => true
+      case l: LeafNode => l match {
+        case _: OneRowRelation | _: LocalRelation | _: Range => false
+        case _ => true
+      }
+      case f: SQLFunctionNode => f.function.containsSQL.contains(false)
+      case p: LogicalPlan =>
+        lazy val sub = p.subqueries.exists(deriveSQLDataAccess)
+        // If the SQL function contains another SQL function that has SQL data 
access routine
+        // to be READS SQL DATA, then this SQL function will also be READS SQL 
DATA.
+        p.expressions.exists(expr => expr.find {
+          case f: SQLScalarFunction => f.function.containsSQL.contains(false)
+          case _ => false
+        }.isDefined) || sub

Review Comment:
   ```suggestion
             case f: SQLScalarFunction => f.function.containsSQL.contains(false)
             case sub: SubqueryExpression => deriveSQLDataAccess(sub.plan)
             case _ => false
           }.isDefined)
   ```



-- 
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]

Reply via email to