Unverified Commit 7eaf8bb3 authored by Licho's avatar Licho Committed by GitHub

feat: add temporalTableFunction (#1841)

* feat:add CreateTemporalTableFunctionOperation * refactor: splotless apply * chore: fix unit test Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix: function name dot and upper problem * Spotless Apply * refactor: move customer operation execute into self. * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent c9c03617
......@@ -42,7 +42,7 @@ public class ExtendedOperationExecutorWrapper implements ExtendedOperationExecut
public Optional<TableResultInternal> executeOperation(Operation operation) {
Optional<TableResultInternal> customResult =
(Optional<TableResultInternal>) customOperationExecutor.executeOperation(operation);
if (customResult != null) {
if (customResult.isPresent()) {
return customResult;
}
......
......@@ -133,7 +133,7 @@ public class Explainer {
trans.add(
new StatementParam(
watchStatementExplainer.getCreateStatement(tableName),
SqlType.CTASS));
SqlType.CTAS));
} else {
UDF udf = UDFUtil.toUDF(statement);
if (Asserts.isNotNull(udf)) {
......
......@@ -449,7 +449,7 @@ public class JobManager {
List<String> inserts = new ArrayList<>();
for (StatementParam item : jobParam.getTrans()) {
if (item.getType().equals(SqlType.INSERT)
|| item.getType().equals(SqlType.CTASS)) {
|| item.getType().equals(SqlType.CTAS)) {
inserts.add(item.getValue());
}
}
......
......@@ -19,16 +19,11 @@
package org.dinky.executor;
import static org.apache.flink.table.api.Expressions.$;
import org.dinky.trans.ExtendOperation;
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 {
......@@ -41,31 +36,11 @@ public class CustomExtendedOperationExecutorImpl implements CustomExtendedOperat
@Override
public Optional<? extends TableResult> executeOperation(Operation operation) {
if (operation instanceof NewCreateAggTableOperation) {
return executeCreateAggTableOperationNew((NewCreateAggTableOperation) operation);
if (operation instanceof ExtendOperation) {
ExtendOperation extendOperation = (ExtendOperation) operation;
return extendOperation.execute(executor);
}
// 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;
return Optional.empty();
}
}
......@@ -20,7 +20,7 @@
package org.dinky.parser;
import org.dinky.executor.CustomParser;
import org.dinky.trans.CreateAggTableOperationNewParseStrategy;
import org.dinky.trans.CreateTemporalTableFunctionParseStrategy;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.planner.parse.ExtendedParseStrategy;
......@@ -50,7 +50,7 @@ public class CustomParserImpl implements CustomParser {
public static final DinkyExtendedParser INSTANCE = new DinkyExtendedParser();
private static final List<ExtendedParseStrategy> PARSE_STRATEGIES =
Arrays.asList(CreateAggTableOperationNewParseStrategy.INSTANCE);
Arrays.asList(CreateTemporalTableFunctionParseStrategy.INSTANCE);
@Override
public Optional<Operation> parse(String statement) {
......
......@@ -62,7 +62,7 @@ public enum SqlType {
WATCH("WATCH", "^WATCH.*"),
CTASS("CTAS", "^CREATE\\s.*AS\\sSELECT.*$"),
CTAS("CTAS", "^CREATE\\s.*AS\\sSELECT.*$"),
UNKNOWN("UNKNOWN", "^UNKNOWN.*");
......
/*
*
* 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.CreateTemporalTableFunctionOperation;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.planner.parse.AbstractRegexParseStrategy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* CREATE TEMPORAL [TEMPORARY|TEMPORARY SYSTEM] FUNCTION [IF NOT EXISTS]
* [catalog_name.][db_name.]function_name
*
* <pre><code>
* create temporal temporary function if not exists rates as select update_time, currency from tableName
* </code></pre>
*/
public class CreateTemporalTableFunctionParseStrategy extends AbstractRegexParseStrategy {
private static final String PATTERN_STR =
"^CREATE\\s+TEMPORAL(?:\\s+(TEMPORARY|TEMPORARY SYSTEM))?\\s+FUNCTION"
+ "(?:\\s(IF NOT EXISTS))?\\s+([.\\w]+)\\s+AS\\s+SELECT\\s+(\\w+)\\s*,\\s*(\\w+)\\s+FROM\\s+"
+ "(\\w+)\\s*$";
private static final Pattern PATTERN =
Pattern.compile(PATTERN_STR, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
public static final CreateTemporalTableFunctionParseStrategy INSTANCE =
new CreateTemporalTableFunctionParseStrategy();
protected CreateTemporalTableFunctionParseStrategy() {
super(PATTERN);
}
public static String[] getInfo(String statement) {
Matcher matcher = PATTERN.matcher(statement);
matcher.find();
String functionType = matcher.group(1) == null ? "" : matcher.group(1).trim();
String exist = matcher.group(2) == null ? "" : matcher.group(2).trim();
String functionName = matcher.group(3).trim();
String timeColumn = matcher.group(4).trim();
String targetColumn = matcher.group(5).trim();
String tableName = matcher.group(6).trim();
return new String[] {
functionType, exist, functionName, timeColumn, targetColumn, tableName
};
}
@Override
public Operation convert(String statement) {
return new CreateTemporalTableFunctionOperation(statement);
}
@Override
public String[] getHints() {
return new String[0];
}
}
......@@ -17,37 +17,16 @@
*
*/
package org.dinky.trans.ddl;
package org.dinky.trans;
import org.dinky.executor.Executor;
import org.dinky.trans.AbstractOperation;
import org.dinky.trans.Operation;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.operations.Operation;
public class NewCreateAggTableOperation extends AbstractOperation implements Operation {
private static final String KEY_WORD = "CREATE AGGTABLENEW";
import java.util.Optional;
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() {}
/** */
public interface ExtendOperation extends Operation {
Optional<? extends TableResult> execute(Executor executor);
}
/*
*
* 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.CustomTableEnvironment;
import org.dinky.executor.CustomTableEnvironmentImpl;
import org.dinky.executor.CustomTableResultImpl;
import org.dinky.executor.Executor;
import org.dinky.trans.AbstractOperation;
import org.dinky.trans.CreateTemporalTableFunctionParseStrategy;
import org.dinky.trans.ExtendOperation;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.expressions.Expression;
import org.apache.flink.table.expressions.ValueLiteralExpression;
import org.apache.flink.table.functions.TemporalTableFunction;
import java.util.Optional;
public class CreateTemporalTableFunctionOperation extends AbstractOperation
implements ExtendOperation {
public CreateTemporalTableFunctionOperation() {}
public CreateTemporalTableFunctionOperation(String statement) {
super(statement);
}
@Override
public String asSummaryString() {
return statement;
}
@Override
public Optional<? extends TableResult> execute(Executor executor) {
TemporalTable temporalTable = TemporalTable.build(statement);
CustomTableEnvironment env = executor.getCustomTableEnvironment();
CustomTableEnvironmentImpl customTableEnvironmentImpl = ((CustomTableEnvironmentImpl) env);
Expression timeColumn = new ValueLiteralExpression(temporalTable.getTimeColumn());
Expression targetColumn = new ValueLiteralExpression(temporalTable.getTargetColumn());
TemporalTableFunction ttf =
customTableEnvironmentImpl
.from(temporalTable.getTableName())
.createTemporalTableFunction(timeColumn, targetColumn);
if (temporalTable.getFunctionType().toUpperCase().equals("TEMPORARY SYSTEM")) {
customTableEnvironmentImpl.createTemporarySystemFunction(
temporalTable.getFunctionName(), ttf);
} else {
customTableEnvironmentImpl.createTemporaryFunction(
temporalTable.getFunctionName(), ttf);
}
return Optional.of(CustomTableResultImpl.TABLE_RESULT_OK);
}
public static class TemporalTable {
private String statement;
private String functionType;
private boolean exists;
private String functionName;
private String tableName;
private String timeColumn;
private String targetColumn;
private TemporalTable(
String statement,
String functionType,
String exists,
String functionName,
String timeColumn,
String targetColumn,
String tableName) {
this.functionType = functionType;
this.exists = exists.trim().toUpperCase().equals("IF NOT EXISTS");
this.statement = statement;
this.functionName = functionName;
this.tableName = tableName;
this.timeColumn = timeColumn;
this.targetColumn = targetColumn;
}
public static TemporalTable build(String statement) {
String[] info = CreateTemporalTableFunctionParseStrategy.getInfo(statement);
return new TemporalTable(
statement, info[0], info[1], info[2], info[3], info[4], info[5]);
}
public String getStatement() {
return statement;
}
public void setStatement(String statement) {
this.statement = statement;
}
public String getFunctionName() {
return functionName;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTimeColumn() {
return timeColumn;
}
public void setTimeColumn(String timeColumn) {
this.timeColumn = timeColumn;
}
public String getTargetColumn() {
return targetColumn;
}
public void setTargetColumn(String targetColumn) {
this.targetColumn = targetColumn;
}
public String getFunctionType() {
return functionType;
}
public void setFunctionType(String functionType) {
this.functionType = functionType;
}
public boolean isExists() {
return exists;
}
public void setExists(boolean exists) {
this.exists = exists;
}
}
}
......@@ -59,7 +59,7 @@ class SqlTypeTest {
"CREATE TABLE print_OrdersView WITH ('connector' = 'printnet', 'port'='7125', 'hostName' = '172"
+ ".26.16.1', 'sink.parallelism'='1')\n"
+ "AS SELECT * FROM OrdersView; ";
test(sql, SqlType.CTASS, true);
test(sql, SqlType.CTAS, true);
test(sql, SqlType.CREATE, false);
}
......
......@@ -19,33 +19,23 @@
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"};
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class CreateTemporalTableFunctionParseStrategyTest {
@Test
void getInfo() {
String statement =
"CREATE temporal temporary function func as select price, buyer from orders";
String[] result = CreateTemporalTableFunctionParseStrategy.getInfo(statement);
Assertions.assertArrayEquals(
new String[] {"temporary", "", "func", "price", "buyer", "orders"}, result);
statement =
"CREATE temporal temporary function abc.def.func as select price, buyer from orders";
result = CreateTemporalTableFunctionParseStrategy.getInfo(statement);
Assertions.assertArrayEquals(
new String[] {"temporary", "", "abc.def.func", "price", "buyer", "orders"}, result);
}
}
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