-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23486]cache the function name from the external catalog for lookupFunctions #20795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
5c8648c
caf6b6d
b8871e2
14c62e4
5c6687c
e030bd0
6358b92
74b01a5
27246fb
8dceda9
0db2826
26f2f54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| import scala.collection.mutable | ||
| import scala.collection.mutable.ArrayBuffer | ||
| import scala.util.Random | ||
|
|
||
|
|
@@ -1204,16 +1207,32 @@ class Analyzer( | |
| * only performs simple existence check according to the function identifier to quickly identify | ||
| * undefined functions without triggering relation resolution, which may incur potentially | ||
| * expensive partition/schema discovery process in some cases. | ||
| * | ||
| * In order to avoid duplicate external functions lookup, the external function identifier will | ||
| * store in the local hash set externalFunctionNameSet. | ||
| * @see [[ResolveFunctions]] | ||
| * @see https://issues.apache.org/jira/browse/SPARK-19737 | ||
| */ | ||
| object LookupFunctions extends Rule[LogicalPlan] { | ||
| override def apply(plan: LogicalPlan): LogicalPlan = plan.transformAllExpressions { | ||
| case f: UnresolvedFunction if !catalog.functionExists(f.name) => | ||
| withPosition(f) { | ||
| throw new NoSuchFunctionException(f.name.database.getOrElse("default"), f.name.funcName) | ||
| } | ||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| val externalFunctionNameSet = new mutable.HashSet[FunctionIdentifier]() | ||
| plan.transformAllExpressions { | ||
| case f: UnresolvedFunction | ||
| if externalFunctionNameSet.contains(normalizeFuncName(f.name)) => f | ||
| case f: UnresolvedFunction if catalog.isRegisteredFunction(f.name) => f | ||
| case f: UnresolvedFunction if catalog.isPersistentFunction(f.name) => | ||
| externalFunctionNameSet.add(normalizeFuncName(f.name)) | ||
| f | ||
| case f: UnresolvedFunction => | ||
| withPosition(f) { | ||
| throw new NoSuchFunctionException(f.name.database.getOrElse(catalog.getCurrentDatabase), | ||
| f.name.funcName) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def normalizeFuncName(name: FunctionIdentifier): FunctionIdentifier = { | ||
| FunctionIdentifier(name.funcName.toLowerCase(Locale.ROOT), | ||
| name.database.orElse(Some(catalog.getCurrentDatabase))) | ||
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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.analysis | ||
|
|
||
| import java.net.URI | ||
|
|
||
| import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier} | ||
| import org.apache.spark.sql.catalyst.catalog.{CatalogDatabase, InMemoryCatalog, SessionCatalog} | ||
| import org.apache.spark.sql.catalyst.expressions.Alias | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| class LookupFunctionsSuite extends PlanTest { | ||
|
|
||
| test("SPARK-23486: LookupFunctions should not check the same function name more than once") { | ||
| val externalCatalog = new CustomInMemoryCatalog | ||
| val conf = new SQLConf() | ||
| val catalog = new SessionCatalog(externalCatalog, FunctionRegistry.builtin, conf) | ||
| val analyzer = { | ||
| catalog.createDatabase( | ||
| CatalogDatabase("default", "", new URI("loc"), Map.empty), | ||
| ignoreIfExists = false) | ||
| new Analyzer(catalog, conf) | ||
| } | ||
|
|
||
| def table(ref: String): LogicalPlan = UnresolvedRelation(TableIdentifier(ref)) | ||
| val unresolvedFunc = UnresolvedFunction("func", Seq.empty, false) | ||
|
||
| val plan = Project( | ||
| Seq(Alias(unresolvedFunc, "call1")(), Alias(unresolvedFunc, "call2")(), | ||
| Alias(unresolvedFunc, "call1")()), | ||
|
||
| table("TaBlE")) | ||
| analyzer.LookupFunctions.apply(plan) | ||
| assert(externalCatalog.getFunctionExistsCalledTimes == 1) | ||
|
|
||
| assert(analyzer.LookupFunctions.normalizeFuncName | ||
| (unresolvedFunc.name).database == Some("default")) | ||
| assert(catalog.isRegisteredFunction(unresolvedFunc.name) == false) | ||
| assert(catalog.isRegisteredFunction(FunctionIdentifier("max")) == true) | ||
|
||
|
|
||
| } | ||
| } | ||
|
|
||
| class CustomInMemoryCatalog extends InMemoryCatalog { | ||
|
|
||
| private var functionExistsCalledTimes: Int = 0 | ||
|
|
||
| override def functionExists(db: String, funcName: String): Boolean = synchronized { | ||
| functionExistsCalledTimes = functionExistsCalledTimes + 1 | ||
| true | ||
| } | ||
|
|
||
| def getFunctionExistsCalledTimes: Int = functionExistsCalledTimes | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a common utility function. We can refactor the code later.