...
There are two aspects of the motivation:
- Enpower users to write code and do customized developement for Flink table core
- Enable users to integrate Flink with cores and built-in objects of other systems, so users can reuse whatever they are familiar with in other SQL systems seamlessly as core and built-ins of Flink SQL and Table
- Enpower users to write code and do customized developement for Flink table core
Plugins define a set of metadata, including functions, user defined types, operators, rules, etc. Prebuilt plugins will be added and provided, or users may choose write their own. Flink will take metadata from plugins as extensions of its core built-in system that users can take advantages of. For example, users can define their own geo functions and geo data types and plug them into Flink table as built-in objects. Another example is users can use an out-of-shelf Hive plugin to use Hive built-in functions as part of Flink built-in functions.
...
- HivePlugin, supporting Hive built-in functions and numerous Hive versions
Overall Design
All plugins modules will implement the Plugin module interface. The Plugin module interface defines a set of APIs to provide metadata such as functions, user defined types, operators, rules, etc. Each plugin module can choose to provide all or only a subset of the metadata. All plugins modules are managed by a PluginManagermoduleManager, and all pluggable metadata are loaded on demand in object lookup.
Flink’s existing core metadata will also be a plugin module named as “CorePlugin”“CoreModule”. Since we want to focus on supporting functions thru Pluginsmodules,we’ll only migrate Flink’s existing built-in functions into the CorePlugin CoreModule at this moment as the first step.
All plugin module metadata will be seen as a part of Flink table core, and won’t have namespaces.
Objects in modules are loaded on demand instead of eagerly, so there won't be inconsistency.
Users have to be fully aware of the consequences of resetting modules as that might cause that some objects can not be referenced anymore or resolution order of some objects changes. E.g. “CAST” and “AS” cannot be overriden in CoreModule and users should be fully aware of that.
How to Load Plugins
To load plugins, users have to make sure relevant classes are already in classpath.
Java/Scala:
// new APIs to TableEnvironment // unload a module instance from module list and other modules remain the same relative positions // list all the modules' names according to order in module list // note the following plugins modules will be of the order they are specified |
Yaml file:
Pluginsmodules: # note the following plugins modules will be of the order they are specified |
Since it doesn’t make sense to load the same plugin multiple times, plugins don’t need to have names.
Based on the plugins module type defined in yaml file, SQL CLI will invoke factory service to search the factory class that provides the given plugin module name, and then set them in TableEnvironment.
If users don’t set plugins, the CorePlugin will be used by default.
All plugin metadata are case-insensitive.
...
A few clarifications
- By default, yaml file doesn’t have the “modules” section in effect, and core module will be loaded by default.
- If users specify the “modules” section in yaml file, modules will be strictly loaded according to that, if CoreModule is not specified there, it won’t be loaded.
In case users forgot to specify core module, “modules” section will be commented out in yaml file as following
#modules: # note the following modules will be of the order they are specified |
SQL:
- SHOW MODULES: show module names in the existing module list in order
- LOAD MODULE 'name' [WITH (‘type’=’xxx’, 'prop'='myProp', ...)] : load a module with given name and append to end of the module list
- UNLOAD MODULE 'name’ : unload a module by name from module list and other modules remain the same relative positions
Resolution Order
Object will be resolved to plugins modules in the order they are defined either in program or in yaml configs. When there are objects sharing the same name, resolution logic will go thru plugins modules in order and return whatever the first one is found, the other ones sitting in the back in the order will be ignored. E.g. if plugins modules are set as “xxx, yyy” where xxx and yyy modules both have a function named “f”, then “f” will always be resolved as that in xxx module.
...
Besides, users may want to define different resolution orders for different metadata, e.g. “xxx, yyy” for functions, but “yyy, xxx” for data types. They will not be taken in this FLIP too. We can tackle that problem incrementally when there’s a real need from users.
Classes
The following is a generic design with functions as a specific example.
...
Module Interface
Plugin Module interface defines a set of metadata that a plugin module can provide to Flink. It provides default implementations for all the APIs thus an implementation can implement only what it’s able to supply.
interface Plugin Module { default Optional<FunctionDefinition> getFunctionDefinition(String name) { return Optional.empty() }; } |
...
ModuleFactory interface
PluginFactory ModuleFactory defines a factory that is used for descriptors to uniquely identify a Plugin module in service discovery, and create an instance of the pluginmodule.
interface PluginFactory ModuleFactory extends TableFactory { |
...
CoreModule and
...
CoreModuleFactory
CorePlugin CoreModule is a pre-defined singleton plugin module that contains should contain all built-in metadata of Flink core.
We currently only move built-in functions into CorePluginCoremodule.
public class CorePlugin CoreModule implements Plugin Module { |
class CorePluginFactory CoreModuleFactory { |
...
ModuleManager
PluginManager ModuleManager is responsible for loading all the Plugins, managing their life cycles, and resolve plugin module objects.
public class PluginManager ModuleManager { public public void setPlugins(List<Plugin> pluginsloadModule(String name, Module module) { ... } this.plugins = plugins; public Set<Set<String>> listFunctions() {
|
...
class FunctionCatalog implements FunctionLookup { public Optional<FunctionLookup.Result> lookupFunction(String name) { // search built-in functions in PluginManagerModuleManager, rather than BuiltInFunctionsDefinitions // Resolution order depends on FLIP-57: Rework FunctionCatalog } } |
...
There was some proposals of merging FunctionCatalog with CatalogManager. It will not be considered in this FLIP.
How to Write and Use a Self-Defined
...
Module - Using
...
HiveModule as an Example
To support numerous Hive versions, we will use the shim approach, which is similar to that of existing HiveCatalog. Letting users explicitly specifying Hive versions is necessary since there are differences in Flink-Hive data conversions among different Hive versions.
public class HivePlugin HiveModule implements Plugin Module { private static final Logger LOGGER = LoggerFactory.getLogger(HivePlugin.class)final String hiveVersion; public HivePluginHiveModule(String hiveVersion) { @Override @Override |
public abstract class HivePluginFactory HiveModuleFactory implements PluginFactory ModuleFactory { |
Java/Scala:
tableEnv.usePluginsloadModule(CorePluginCoreModule.INSTANCE, new HivePluginHiveModule("2_.2_.1")); |
Yaml file:
pluginsmodules: - type: core name: core - type: hive name: hive hive-version: 2.2.1 |
...