Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-14016][python][flink-table-planner] Introduce DataStreamPython…
…Calc for Python function execution
- Loading branch information
Showing
19 changed files
with
944 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...e/flink-table-common/src/main/java/org/apache/flink/table/functions/FunctionLanguage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.functions; | ||
|
||
import org.apache.flink.annotation.PublicEvolving; | ||
|
||
/** | ||
* Categorizes the language of a {@link FunctionDefinition}. | ||
*/ | ||
@PublicEvolving | ||
public enum FunctionLanguage { | ||
|
||
JVM, | ||
|
||
PYTHON | ||
} |
56 changes: 56 additions & 0 deletions
56
...le-common/src/main/java/org/apache/flink/table/functions/python/SimplePythonFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.functions.python; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.util.Preconditions; | ||
|
||
/** | ||
* A simple implementation of {@link PythonFunction}. | ||
*/ | ||
@Internal | ||
public final class SimplePythonFunction implements PythonFunction { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Serialized representation of the user-defined python function. | ||
*/ | ||
private final byte[] serializedPythonFunction; | ||
|
||
/** | ||
* Python execution environment. | ||
*/ | ||
private final PythonEnv pythonEnv; | ||
|
||
public SimplePythonFunction(byte[] serializedPythonFunction, PythonEnv pythonEnv) { | ||
this.serializedPythonFunction = Preconditions.checkNotNull(serializedPythonFunction); | ||
this.pythonEnv = Preconditions.checkNotNull(pythonEnv); | ||
} | ||
|
||
@Override | ||
public byte[] getSerializedPythonFunction() { | ||
return serializedPythonFunction; | ||
} | ||
|
||
@Override | ||
public PythonEnv getPythonEnv() { | ||
return pythonEnv; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ink-table-planner/src/main/scala/org/apache/flink/table/plan/nodes/CommonPythonCalc.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.plan.nodes | ||
|
||
import org.apache.calcite.rex.{RexCall, RexInputRef, RexNode} | ||
import org.apache.flink.table.functions.FunctionLanguage | ||
import org.apache.flink.table.functions.python.{PythonFunction, PythonFunctionInfo, SimplePythonFunction} | ||
import org.apache.flink.table.functions.utils.ScalarSqlFunction | ||
|
||
import scala.collection.JavaConversions._ | ||
import scala.collection.mutable | ||
|
||
trait CommonPythonCalc { | ||
|
||
private[flink] def extractPythonScalarFunctionInfos( | ||
rexCalls: Array[RexCall]): (Array[Int], Array[PythonFunctionInfo]) = { | ||
// using LinkedHashMap to keep the insert order | ||
val inputNodes = new mutable.LinkedHashMap[RexNode, Integer]() | ||
val pythonFunctionInfos = rexCalls.map(createPythonScalarFunctionInfo(_, inputNodes)) | ||
|
||
val udfInputOffsets = inputNodes.toArray.sortBy(_._2).map(_._1).map { | ||
case inputRef: RexInputRef => inputRef.getIndex | ||
} | ||
(udfInputOffsets, pythonFunctionInfos) | ||
} | ||
|
||
private[flink] def createPythonScalarFunctionInfo( | ||
rexCall: RexCall, | ||
inputNodes: mutable.Map[RexNode, Integer]): PythonFunctionInfo = rexCall.getOperator match { | ||
case sfc: ScalarSqlFunction if sfc.getScalarFunction.getLanguage == FunctionLanguage.PYTHON => | ||
val inputs = new mutable.ArrayBuffer[AnyRef]() | ||
rexCall.getOperands.foreach { | ||
case pythonRexCall: RexCall if pythonRexCall.getOperator.asInstanceOf[ScalarSqlFunction] | ||
.getScalarFunction.getLanguage == FunctionLanguage.PYTHON => | ||
// Continuous Python UDFs can be chained together | ||
val argPythonInfo = createPythonScalarFunctionInfo(pythonRexCall, inputNodes) | ||
inputs.append(argPythonInfo) | ||
|
||
case argNode: RexNode => | ||
// For input arguments of RexInputRef, it's replaced with an offset into the input row; | ||
inputNodes.get(argNode) match { | ||
case Some(existing) => inputs.append(existing) | ||
case None => | ||
val inputOffset = Integer.valueOf(inputNodes.size) | ||
inputs.append(inputOffset) | ||
inputNodes.put(argNode, inputOffset) | ||
} | ||
} | ||
|
||
// Extracts the necessary information for Python function execution, such as | ||
// the serialized Python function, the Python env, etc | ||
val pythonFunction = new SimplePythonFunction( | ||
sfc.getScalarFunction.asInstanceOf[PythonFunction].getSerializedPythonFunction, | ||
sfc.getScalarFunction.asInstanceOf[PythonFunction].getPythonEnv) | ||
new PythonFunctionInfo(pythonFunction, inputs.toArray) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.