-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-39094][table-planner] Avoid creating duplicate function instances in code generation #27613
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -129,6 +129,9 @@ class CodeGeneratorContext( | |
| // string_constant -> reused_term | ||
| private val reusableStringConstants: mutable.Map[String, String] = mutable.Map[String, String]() | ||
|
|
||
| // set of function instance term that will be added only once | ||
| private val reusableFunctionTerms: mutable.HashSet[String] = mutable.HashSet[String]() | ||
|
|
||
| // map of type serializer that will be added only once | ||
| // LogicalType -> reused_term | ||
| private val reusableTypeSerializers: mutable.Map[LogicalType, String] = | ||
|
|
@@ -835,10 +838,13 @@ class CodeGeneratorContext( | |
| function: UserDefinedFunction, | ||
| functionContextClass: Class[_ <: FunctionContext] = classOf[FunctionContext], | ||
| contextArgs: Seq[String] = null): String = { | ||
| val classQualifier = function.getClass.getName | ||
| val fieldTerm = CodeGenUtils.udfFieldName(function) | ||
|
|
||
| addReusableObjectInternal(function, fieldTerm, classQualifier) | ||
| // check if function has been added before to avoid duplicate function instances | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know the text of the PR says that existing tests are sufficient. It would be great to add a method level unit test to test out the logic around this specific change.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added unit tests in |
||
| if (!reusableFunctionTerms.contains(fieldTerm)) { | ||
| reusableFunctionTerms += fieldTerm | ||
| val classQualifier = function.getClass.getName | ||
| addReusableObjectInternal(function, fieldTerm, classQualifier) | ||
| } | ||
|
|
||
| val openFunction = if (contextArgs != null) { | ||
| s""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.flink.table.planner.codegen | ||
|
|
||
| import org.apache.flink.configuration.Configuration | ||
| import org.apache.flink.table.functions.ScalarFunction | ||
|
|
||
| import org.junit.jupiter.api.Assertions.{assertEquals, assertNotEquals} | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| /** Tests for [[CodeGeneratorContext]]. */ | ||
| class CodeGeneratorContextTest { | ||
|
|
||
| private val classLoader = Thread.currentThread().getContextClassLoader | ||
|
|
||
| @Test | ||
| def testAddSameStatelessFunctionInstanceDedup(): Unit = { | ||
| val ctx = new CodeGeneratorContext(new Configuration, classLoader) | ||
| val udf = new StatelessTestFunction() | ||
|
|
||
| val term1 = ctx.addReusableFunction(udf) | ||
| val term2 = ctx.addReusableFunction(udf) | ||
|
|
||
| assertEquals(term1, term2) | ||
| assertEquals(1, ctx.references.size) | ||
| } | ||
|
|
||
| @Test | ||
| def testAddSameStatefulFunctionInstancesDedup(): Unit = { | ||
| val ctx = new CodeGeneratorContext(new Configuration, classLoader) | ||
| val udf1 = new StatefulTestFunction(42) | ||
| val udf2 = new StatefulTestFunction(42) | ||
|
|
||
| val term1 = ctx.addReusableFunction(udf1) | ||
| val term2 = ctx.addReusableFunction(udf2) | ||
|
|
||
| assertEquals(term1, term2) | ||
| assertEquals(1, ctx.references.size) | ||
| } | ||
|
|
||
| @Test | ||
| def testAddDifferentStatelessFunctionInstancesDedup(): Unit = { | ||
| val ctx = new CodeGeneratorContext(new Configuration, classLoader) | ||
| val udf1 = new StatelessTestFunction() | ||
| val udf2 = new StatelessTestFunction() | ||
|
|
||
| val term1 = ctx.addReusableFunction(udf1) | ||
| val term2 = ctx.addReusableFunction(udf2) | ||
|
|
||
| assertEquals(term1, term2) | ||
| assertEquals(1, ctx.references.size) | ||
| } | ||
|
|
||
| @Test | ||
| def testAddDifferentStatefulFunctionInstances(): Unit = { | ||
| val ctx = new CodeGeneratorContext(new Configuration, classLoader) | ||
| val udf1 = new StatefulTestFunction(1) | ||
| val udf2 = new StatefulTestFunction(2) | ||
|
|
||
| val term1 = ctx.addReusableFunction(udf1) | ||
| val term2 = ctx.addReusableFunction(udf2) | ||
|
|
||
| assertNotEquals(term1, term2) | ||
| assertEquals(2, ctx.references.size) | ||
| } | ||
| } | ||
|
|
||
| @SerialVersionUID(1L) | ||
| class StatelessTestFunction extends ScalarFunction { | ||
| def eval(a: Long): Long = a | ||
| } | ||
|
|
||
| @SerialVersionUID(1L) | ||
| class StatefulTestFunction(private val mode: Int) extends ScalarFunction { | ||
| def eval(a: Long): Long = a + mode | ||
| } |
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.
how can we be sure in this?
Can we add a test with custom udf and counter inside checking that it was invoked once?
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.
The dedup key (fieldTerm) is derived from
functionIdentifier(), which is the same key used byaddReusableObjectInternalto generate member/init statements — so the dedup is consistent with existing behavior.I added unit tests in
CodeGeneratorContextTestthat directly assertsreferences.sizeafter callingaddReusableFunctionwith the same/different functions, covering stateless dedup, stateful dedup, and stateful non-dedup cases.As for a counter-based test,
addReusableObjectInternalcreates instances viaInstantiationUtil.deserializeObjectthat bypasses constructors, andopen()was already deduplicated by LinkedHashSet before this fix, so neither counter can distinguish the before/after behavior. Let me know if you have better idea.