Unverified Commit afe08b2c authored by Licho's avatar Licho Committed by GitHub

feat: inject parser and extendedExecutor wrapper into flink (#1735)

* feat: make org.dinky.*.Operation extends org.flink.*.Operation, * feat: reflect for setting parser and ExtendedOperationExecutor * feat: replace parser and extendedExecutor, could run * feat: example for remove jar * reflector: change to reflectUtils * reflector: change to reflectUtils * feat: inject move to client * fix: regrex * Spotless Apply * feat: desperate custom parser and extendedOperationExecutor * Spotless Apply * fix: not type Optional<TableResultInternal> Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * fix: Optional<TableResultInternal> Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * feat: github workflow remove 1.12 Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix: not run jdk 11 Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com> Co-authored-by: 's avataraiwenmo <32723967+aiwenmo@users.noreply.github.com>
parent 8358a14f
...@@ -89,10 +89,7 @@ jobs: ...@@ -89,10 +89,7 @@ jobs:
fail-fast: true fail-fast: true
matrix: matrix:
jdk: [8, 11] jdk: [8, 11]
flink: [ 1.12, 1.13, 1.14, 1.15, 1.16 ] flink: [1.13, 1.14, 1.15, 1.16]
exclude:
- jdk: 11
flink: 1.12
timeout-minutes: 30 timeout-minutes: 30
env: env:
......
...@@ -23,7 +23,11 @@ import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; ...@@ -23,7 +23,11 @@ import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableEnvironment; import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl; import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl;
import org.apache.flink.table.delegation.ExtendedOperationExecutor;
import org.apache.flink.table.delegation.Planner; import org.apache.flink.table.delegation.Planner;
import org.apache.flink.table.planner.delegation.PlannerBase;
import cn.hutool.core.util.ReflectUtil;
/** */ /** */
public abstract class AbstractCustomTableEnvironment public abstract class AbstractCustomTableEnvironment
...@@ -51,4 +55,21 @@ public abstract class AbstractCustomTableEnvironment ...@@ -51,4 +55,21 @@ public abstract class AbstractCustomTableEnvironment
public Planner getPlanner() { public Planner getPlanner() {
return ((StreamTableEnvironmentImpl) streamTableEnvironment).getPlanner(); return ((StreamTableEnvironmentImpl) streamTableEnvironment).getPlanner();
} }
@Override
public void injectParser(CustomParser parser) {
ReflectUtil.setFieldValue(
getPlanner(), "parser", new ParserWrapper(getPlanner().getParser(), parser));
}
@Override
public void injectExtendedExecutor(CustomExtendedOperationExecutor extendedExecutor) {
PlannerBase plannerBase = (PlannerBase) getPlanner();
ExtendedOperationExecutor extendedOperationExecutor =
new ExtendedOperationExecutorWrapper(
plannerBase.getExtendedOperationExecutor(), extendedExecutor);
ReflectUtil.setFieldValue(
getPlanner(), "extendedOperationExecutor", extendedOperationExecutor);
}
} }
...@@ -101,6 +101,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -101,6 +101,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
StreamExecutionEnvironment executionEnvironment, EnvironmentSettings settings) { StreamExecutionEnvironment executionEnvironment, EnvironmentSettings settings) {
StreamTableEnvironment streamTableEnvironment = StreamTableEnvironment streamTableEnvironment =
StreamTableEnvironment.create(executionEnvironment, settings); StreamTableEnvironment.create(executionEnvironment, settings);
return new CustomTableEnvironmentImpl(streamTableEnvironment); return new CustomTableEnvironmentImpl(streamTableEnvironment);
} }
......
...@@ -44,50 +44,62 @@ public interface DefaultTableEnvironmentInternal ...@@ -44,50 +44,62 @@ public interface DefaultTableEnvironmentInternal
} }
// region TableEnvironmentInternal interface // region TableEnvironmentInternal interface
@Override
default Parser getParser() { default Parser getParser() {
return getTableEnvironmentInternal().getParser(); return getTableEnvironmentInternal().getParser();
} }
@Override
default CatalogManager getCatalogManager() { default CatalogManager getCatalogManager() {
return getTableEnvironmentInternal().getCatalogManager(); return getTableEnvironmentInternal().getCatalogManager();
} }
@Override
default OperationTreeBuilder getOperationTreeBuilder() { default OperationTreeBuilder getOperationTreeBuilder() {
return getTableEnvironmentInternal().getOperationTreeBuilder(); return getTableEnvironmentInternal().getOperationTreeBuilder();
} }
@Override
default Table fromTableSource(TableSource<?> tableSource) { default Table fromTableSource(TableSource<?> tableSource) {
return getTableEnvironmentInternal().fromTableSource(tableSource); return getTableEnvironmentInternal().fromTableSource(tableSource);
} }
@Override
default TableResultInternal executeInternal(List<ModifyOperation> list) { default TableResultInternal executeInternal(List<ModifyOperation> list) {
return getTableEnvironmentInternal().executeInternal(list); return getTableEnvironmentInternal().executeInternal(list);
} }
@Override
default TableResultInternal executeInternal(Operation operation) { default TableResultInternal executeInternal(Operation operation) {
return getTableEnvironmentInternal().executeInternal(operation); return getTableEnvironmentInternal().executeInternal(operation);
} }
@Override
default String explainInternal(List<Operation> list, ExplainDetail... explainDetails) { default String explainInternal(List<Operation> list, ExplainDetail... explainDetails) {
return getTableEnvironmentInternal().explainInternal(list, explainDetails); return getTableEnvironmentInternal().explainInternal(list, explainDetails);
} }
@Override
default void registerTableSourceInternal(String s, TableSource<?> tableSource) { default void registerTableSourceInternal(String s, TableSource<?> tableSource) {
getTableEnvironmentInternal().registerTableSourceInternal(s, tableSource); getTableEnvironmentInternal().registerTableSourceInternal(s, tableSource);
} }
@Override
default void registerTableSinkInternal(String s, TableSink<?> tableSink) { default void registerTableSinkInternal(String s, TableSink<?> tableSink) {
getTableEnvironmentInternal().registerTableSinkInternal(s, tableSink); getTableEnvironmentInternal().registerTableSinkInternal(s, tableSink);
} }
@Override
default CompiledPlan compilePlan(List<ModifyOperation> list) { default CompiledPlan compilePlan(List<ModifyOperation> list) {
return getTableEnvironmentInternal().compilePlan(list); return getTableEnvironmentInternal().compilePlan(list);
} }
@Override
default TableResultInternal executePlan(InternalPlan internalPlan) { default TableResultInternal executePlan(InternalPlan internalPlan) {
return getTableEnvironmentInternal().executePlan(internalPlan); return getTableEnvironmentInternal().executePlan(internalPlan);
} }
@Override
default String explainPlan(InternalPlan internalPlan, ExplainDetail... explainDetails) { default String explainPlan(InternalPlan internalPlan, ExplainDetail... explainDetails) {
return getTableEnvironmentInternal().explainPlan(internalPlan, explainDetails); return getTableEnvironmentInternal().explainPlan(internalPlan, explainDetails);
} }
......
/*
*
* 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.dinky.executor;
import org.apache.flink.table.api.internal.TableResultInternal;
import org.apache.flink.table.delegation.ExtendedOperationExecutor;
import org.apache.flink.table.operations.Operation;
import java.util.Optional;
public class ExtendedOperationExecutorWrapper implements ExtendedOperationExecutor {
private ExtendedOperationExecutor extendedOperationExecutor;
private final CustomExtendedOperationExecutor customOperationExecutor;
public ExtendedOperationExecutorWrapper(
ExtendedOperationExecutor extendedOperationExecutor,
CustomExtendedOperationExecutor customOperationExecutor) {
this.extendedOperationExecutor = extendedOperationExecutor;
this.customOperationExecutor = customOperationExecutor;
}
@Override
public Optional<TableResultInternal> executeOperation(Operation operation) {
Optional<TableResultInternal> customResult =
(Optional<TableResultInternal>) customOperationExecutor.executeOperation(operation);
if (customResult != null) {
return customResult;
}
return extendedOperationExecutor.executeOperation(operation);
}
}
/*
*
* 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.dinky.executor;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import org.apache.flink.table.delegation.Parser;
import org.apache.flink.table.expressions.ResolvedExpression;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.RowType;
import java.util.List;
import javax.annotation.Nullable;
public class ParserWrapper implements Parser {
private Parser parser;
private CustomParser customParser;
public ParserWrapper(Parser parser, CustomParser customParser) {
this.parser = parser;
this.customParser = customParser;
}
@Override
public List<Operation> parse(String statement) {
List<Operation> result = customParser.parse(statement);
if (result != null) {
return result;
}
return parser.parse(statement);
}
@Override
public UnresolvedIdentifier parseIdentifier(String identifier) {
return parser.parseIdentifier(identifier);
}
@Override
public ResolvedExpression parseSqlExpression(
String sqlExpression, RowType inputRowType, @Nullable LogicalType outputType) {
return parser.parseSqlExpression(sqlExpression, inputRowType, outputType);
}
@Override
public String[] getCompletionHints(String statement, int position) {
return parser.getCompletionHints(statement, position);
}
}
/*
*
* 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.dinky.executor;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.operations.Operation;
import java.util.Optional;
public interface CustomExtendedOperationExecutor {
Optional<? extends TableResult> executeOperation(Operation operation);
}
/*
*
* 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.dinky.executor;
import org.apache.flink.table.operations.Operation;
import java.util.List;
public interface CustomParser {
List<Operation> parse(String statement);
}
...@@ -25,4 +25,8 @@ import org.apache.flink.table.api.TableEnvironment; ...@@ -25,4 +25,8 @@ import org.apache.flink.table.api.TableEnvironment;
public interface TableEnvironmentInstance { public interface TableEnvironmentInstance {
TableEnvironment getTableEnvironment(); TableEnvironment getTableEnvironment();
default void injectParser(CustomParser parser) {};
default void injectExtendedExecutor(CustomExtendedOperationExecutor extendedExecutor) {};
} }
...@@ -33,6 +33,9 @@ import java.util.HashMap; ...@@ -33,6 +33,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* FlinkBaseUtil * FlinkBaseUtil
* *
...@@ -40,6 +43,7 @@ import java.util.Map; ...@@ -40,6 +43,7 @@ import java.util.Map;
* @since 2022/3/9 19:15 * @since 2022/3/9 19:15
*/ */
public class FlinkBaseUtil { public class FlinkBaseUtil {
private static final Logger logger = LoggerFactory.getLogger(FlinkBaseUtil.class);
public static Map<String, String> getParamsFromArgs(String[] args) { public static Map<String, String> getParamsFromArgs(String[] args) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
......
/*
*
* 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.dinky.executor;
import static org.apache.flink.table.api.Expressions.$;
import org.dinky.trans.ddl.AggTable;
import org.dinky.trans.ddl.NewCreateAggTableOperation;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.operations.Operation;
import java.util.List;
import java.util.Optional;
public class CustomExtendedOperationExecutorImpl implements CustomExtendedOperationExecutor {
private Executor executor;
public CustomExtendedOperationExecutorImpl(Executor executor) {
this.executor = executor;
}
@Override
public Optional<? extends TableResult> executeOperation(Operation operation) {
if (operation instanceof NewCreateAggTableOperation) {
return executeCreateAggTableOperationNew((NewCreateAggTableOperation) operation);
}
// note: null result represent not in custom operator,
return null;
}
public Optional<? extends TableResult> executeCreateAggTableOperationNew(
NewCreateAggTableOperation option) {
AggTable aggTable = AggTable.build(option.getStatement());
Table source =
executor.getCustomTableEnvironment()
.sqlQuery("select * from " + aggTable.getTable());
List<String> wheres = aggTable.getWheres();
if (wheres != null && wheres.size() > 0) {
for (String s : wheres) {
source = source.filter($(s));
}
}
Table sink =
source.groupBy($(aggTable.getGroupBy()))
.flatAggregate($(aggTable.getAggBy()))
.select($(aggTable.getColumns()));
executor.getCustomTableEnvironment().registerTable(aggTable.getName(), sink);
return null;
}
}
...@@ -24,6 +24,7 @@ import org.dinky.context.DinkyClassLoaderContextHolder; ...@@ -24,6 +24,7 @@ import org.dinky.context.DinkyClassLoaderContextHolder;
import org.dinky.interceptor.FlinkInterceptor; import org.dinky.interceptor.FlinkInterceptor;
import org.dinky.interceptor.FlinkInterceptorResult; import org.dinky.interceptor.FlinkInterceptorResult;
import org.dinky.model.LineageRel; import org.dinky.model.LineageRel;
import org.dinky.parser.CustomParserImpl;
import org.dinky.result.SqlExplainResult; import org.dinky.result.SqlExplainResult;
import org.apache.flink.api.common.ExecutionConfig; import org.apache.flink.api.common.ExecutionConfig;
...@@ -183,8 +184,9 @@ public abstract class Executor { ...@@ -183,8 +184,9 @@ public abstract class Executor {
private void initExecutionEnvironment() { private void initExecutionEnvironment() {
useSqlFragment = executorSetting.isUseSqlFragment(); useSqlFragment = executorSetting.isUseSqlFragment();
tableEnvironment = createCustomTableEnvironment(); tableEnvironment = createCustomTableEnvironment();
tableEnvironment.injectParser(new CustomParserImpl());
tableEnvironment.injectExtendedExecutor(new CustomExtendedOperationExecutorImpl(this));
Configuration configuration = tableEnvironment.getConfig().getConfiguration(); Configuration configuration = tableEnvironment.getConfig().getConfiguration();
if (executorSetting.isValidJobName()) { if (executorSetting.isValidJobName()) {
configuration.setString(PipelineOptions.NAME.key(), executorSetting.getJobName()); configuration.setString(PipelineOptions.NAME.key(), executorSetting.getJobName());
......
/*
*
* 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.dinky.parser;
import org.dinky.executor.CustomParser;
import org.dinky.trans.CreateAggTableOperationNewParseStrategy;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.planner.parse.ExtendedParseStrategy;
import org.apache.flink.table.planner.parse.ExtendedParser;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class CustomParserImpl implements CustomParser {
private static final DinkyExtendedParser DINKY_EXTENDED_PARSER = DinkyExtendedParser.INSTANCE;
@Override
public List<Operation> parse(String statement) {
Optional<Operation> command = DINKY_EXTENDED_PARSER.parse(statement);
if (command.isPresent()) {
return Collections.singletonList(command.get());
}
// note: null represent not custom parser;
return null;
}
public static class DinkyExtendedParser extends ExtendedParser {
public static final DinkyExtendedParser INSTANCE = new DinkyExtendedParser();
private static final List<ExtendedParseStrategy> PARSE_STRATEGIES =
Arrays.asList(CreateAggTableOperationNewParseStrategy.INSTANCE);
@Override
public Optional<Operation> parse(String statement) {
for (ExtendedParseStrategy strategy : PARSE_STRATEGIES) {
if (strategy.match(statement)) {
return Optional.of(strategy.convert(statement));
}
}
return Optional.empty();
}
}
}
/*
*
* 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.dinky.trans;
import org.dinky.trans.ddl.NewCreateAggTableOperation;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.planner.parse.AbstractRegexParseStrategy;
import java.util.regex.Pattern;
public class CreateAggTableOperationNewParseStrategy extends AbstractRegexParseStrategy {
private static final String PATTERN_STR = "(CREATE NEWAGGTABLE)\\s+(.*)";
private static final Pattern ADD_JAR_PATTERN =
Pattern.compile(PATTERN_STR, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
public static final CreateAggTableOperationNewParseStrategy INSTANCE =
new CreateAggTableOperationNewParseStrategy();
protected CreateAggTableOperationNewParseStrategy() {
super(ADD_JAR_PATTERN);
}
@Override
public Operation convert(String statement) {
return new NewCreateAggTableOperation(statement);
}
@Override
public String[] getHints() {
return new String[] {"CREATE AGGTABLENEW"};
}
}
...@@ -29,7 +29,7 @@ import org.apache.flink.table.api.TableResult; ...@@ -29,7 +29,7 @@ import org.apache.flink.table.api.TableResult;
* @author wenmo * @author wenmo
* @since 2021/6/13 19:24 * @since 2021/6/13 19:24
*/ */
public interface Operation { public interface Operation extends org.apache.flink.table.operations.Operation {
String getHandle(); String getHandle();
...@@ -38,4 +38,9 @@ public interface Operation { ...@@ -38,4 +38,9 @@ public interface Operation {
TableResult build(Executor executor); TableResult build(Executor executor);
boolean noExecute(); boolean noExecute();
@Override
default String asSummaryString() {
return getHandle();
}
} }
/*
*
* 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.dinky.trans.ddl;
import org.dinky.executor.Executor;
import org.dinky.trans.AbstractOperation;
import org.dinky.trans.Operation;
import org.apache.flink.table.api.TableResult;
public class NewCreateAggTableOperation extends AbstractOperation implements Operation {
private static final String KEY_WORD = "CREATE AGGTABLENEW";
public NewCreateAggTableOperation(String statement) {
super(statement);
}
public NewCreateAggTableOperation() {}
@Override
public String getHandle() {
return KEY_WORD;
}
@Override
public Operation create(String statement) {
return null;
}
@Override
public TableResult build(Executor executor) {
return null;
}
public void init() {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment