Status
Current state: Under Discussion
Discussion thread: https://lists.apache.org/thread/66g79w5zlod2ylyv8k065j57pjjmv1jo
Vote thread:
JIRA: [Umbrella] Pluggable dialect and decouple Hive connector
Released: 1.16
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
Currently, with FLIP-123, FLIP-152, we have supported Hive dialect. But it's much dependent on Flink planner. Also the interfaces involved is more like to be internal, which is not convenient and available to implement other dialects.
So, this Flip is to Introduce pluggable dialect with some public interfaces to that make convenient to support other dialects. Also, at the same time, it's intend to slove the legacy problem brought by supporting Hive dialect that the Hive connector is coupled to Flink planner, which brings much complexity andmaintenance burden.
Proposed Changes
The idea
Introuce a slim module may called flink-table-planer-spi containing the interface ParserFactory. Then, to support new dialect, it's need include the module to and implement the ParserFactory. All the existing dialects are to follow this way.
Introduce flink-table-planner-spi module
ParserFactory
/** * Factory that creates {@link Parser}. * / @Public public interface ParserFactory extends Factory { /** Creates a new parser. */ Parser create(Context context); /** Context provided when a parser is created. */ interface Context { // ... } }
Then, for example, if you would like to support MySQL dialect, you need to provide an implementation for ParserFactory may called MySQLParserFactory. The factory is responsible for creating the corresponding MySQLParser. The interface Parser has existed in Flink, it look like as follows:
/** Provides methods for parsing SQL objects from a SQL string. */ public interface Parser { List<Operation> parse(String statement); }
For the MySQLParser, you should provide the method that converts SQL statement to the Operaton that Flink expects.
Finally, specific the class path of MySQLParserFactory in the reosurce file org.apache.flink.table.factories.Factory to make it can be discovered by Java SPI mechanism.
After that, you can switch to MySQL dialect by setting table.sql-dialect whiling excuting the sql.
Decouple Hive connector
When talk about decopling Hive connector, it's a little of complex for the current implementation is converting the sql to Calcite’s RelNode which is consistent to Hive’s implementation when using CBO in Hive. It requires flink-table-planner to creat Calcite RelNode.
The better way is to convert Hive AST to Operation tree, but it'll take much efforts for we need to rewrite the codebase about Hive dialect totally for it's totally different from the current implementation. It's hard to migrate to Operation tree at one shot.
So the tempory way is to provide the calcite dependency in flink-table-planner-spi module, along with the ability to create RelNode, which invoves accessing the RelOptCluster, RelBuilder, etc, provided by PlannerContext. But it's internal and only used by Hive connector.
Anyway, at the end, the calcite dependency should be removed and the Hive dialect should be migrate to Operation tree.
About the ability to create RelNode, it may need a context to create RelNode, the context looks like as follows:
// Context for creating RelNode public interface RelNodeContext { CalciteCatalogReader createCatalogReader( boolean lenientCaseSensitivity, String currentCatalog, String currentDatabase); RelOptCluster getCluster(); FrameworkConfig createFrameworkConfig(); RelDataTypeFactory getTypeFactory(); RelBuilder createRelBuilder(String currentCatalog, String currentDatabase); }
The interfaces have been implemented in PlannerContext, but we need to expose them to enable Hive connector to use.
Then, the Hive connector will work without the dependency on flink-table-planner in pom.
New or Changed Public Interfaces
With decoupling Hive connector, the public interfaces looks like:
@Internal public interface ParserFactory extends Factory { /** Creates a new parser. */ Parser create(Context context); /** Context provided when a parser is created. */ interface Context { CatalogManager getCatalogManager(); RelNodeContext getPlannerContext(); } }
Compatibility, Deprecation, and Migration Plan
N/A
Test Plan
It's just refactor work, which can be tested by existing tests.