forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-16367][table] Introduce TableEnvironment#createStatementSet api
This closes apache#12042
- Loading branch information
Showing
16 changed files
with
897 additions
and
86 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
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,93 @@ | ||
################################################################################ | ||
# 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. | ||
################################################################################ | ||
|
||
from pyflink.util.utils import to_j_explain_detail_arr | ||
|
||
__all__ = ['StatementSet'] | ||
|
||
|
||
class StatementSet(object): | ||
""" | ||
A StatementSet accepts DML statements or Tables, | ||
the planner can optimize all added statements and Tables together | ||
and then submit as one job. | ||
.. note:: | ||
The added statements and Tables will be cleared | ||
when calling the `execute` method. | ||
""" | ||
|
||
def __init__(self, _j_statement_set): | ||
self._j_statement_set = _j_statement_set | ||
|
||
def add_insert_sql(self, stmt): | ||
""" | ||
add insert statement to the set. | ||
:param stmt: The statement to be added. | ||
:type stmt: str | ||
:return: current StatementSet instance. | ||
:rtype: pyflink.table.StatementSet | ||
""" | ||
self._j_statement_set.addInsertSql(stmt) | ||
return self | ||
|
||
def add_insert(self, target_path, table, overwrite=False): | ||
""" | ||
add Table with the given sink table name to the set. | ||
:param target_path: The path of the registered :class:`~pyflink.table.TableSink` to which | ||
the :class:`~pyflink.table.Table` is written. | ||
:type target_path: str | ||
:param table: The Table to add. | ||
:type table: pyflink.table.Table | ||
:param overwrite: The flag that indicates whether the insert | ||
should overwrite existing data or not. | ||
:type overwrite: bool | ||
:return: current StatementSet instance. | ||
:rtype: pyflink.table.StatementSet | ||
""" | ||
self._j_statement_set.addInsert(target_path, table._j_table, overwrite) | ||
return self | ||
|
||
def explain(self, *extra_details): | ||
""" | ||
returns the AST and the execution plan of all statements and Tables. | ||
:param extra_details: The extra explain details which the explain result should include, | ||
e.g. estimated cost, changelog mode for streaming | ||
:type extra_details: tuple[ExplainDetail] (variable-length arguments of ExplainDetail) | ||
:return: All statements and Tables for which the AST and execution plan will be returned. | ||
:rtype: str | ||
""" | ||
j_extra_details = to_j_explain_detail_arr(extra_details) | ||
return self._j_statement_set.explain(j_extra_details) | ||
|
||
def execute(self): | ||
""" | ||
execute all statements and Tables as a batch. | ||
.. note:: | ||
The added statements and Tables will be cleared when executing this method. | ||
:return: execution result. | ||
""" | ||
# TODO convert java TableResult to python TableResult once FLINK-17303 is finished | ||
return self._j_statement_set.execute() |
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
65 changes: 65 additions & 0 deletions
65
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/StatementSet.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,65 @@ | ||
/* | ||
* 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.api; | ||
|
||
import org.apache.flink.annotation.PublicEvolving; | ||
|
||
/** | ||
* A {@link StatementSet} accepts DML statements or {@link Table}s, | ||
* the planner can optimize all added statements and Tables together | ||
* and then submit as one job. | ||
* | ||
* <p>The added statements and Tables will be cleared | ||
* when calling the `execute` method. | ||
*/ | ||
@PublicEvolving | ||
public interface StatementSet { | ||
|
||
/** | ||
* add insert statement to the set. | ||
*/ | ||
StatementSet addInsertSql(String statement); | ||
|
||
/** | ||
* add Table with the given sink table name to the set. | ||
*/ | ||
StatementSet addInsert(String targetPath, Table table); | ||
|
||
/** | ||
* add {@link Table} with the given sink table name to the set. | ||
*/ | ||
StatementSet addInsert(String targetPath, Table table, boolean overwrite); | ||
|
||
/** | ||
* returns the AST and the execution plan to compute the result of the | ||
* all statements and Tables. | ||
* | ||
* @param extraDetails The extra explain details which the explain result should include, | ||
* e.g. estimated cost, changelog mode for streaming | ||
* @return AST and the execution plan. | ||
*/ | ||
String explain(ExplainDetail... extraDetails); | ||
|
||
/** | ||
* execute all statements and Tables as a batch. | ||
* | ||
* <p>The added statements and Tables will be cleared when executing this method. | ||
*/ | ||
TableResult execute(); | ||
} |
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
102 changes: 102 additions & 0 deletions
102
...nk-table-api-java/src/main/java/org/apache/flink/table/api/internal/StatementSetImpl.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,102 @@ | ||
/* | ||
* 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.api.internal; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.table.api.ExplainDetail; | ||
import org.apache.flink.table.api.StatementSet; | ||
import org.apache.flink.table.api.Table; | ||
import org.apache.flink.table.api.TableException; | ||
import org.apache.flink.table.api.TableResult; | ||
import org.apache.flink.table.catalog.ObjectIdentifier; | ||
import org.apache.flink.table.catalog.UnresolvedIdentifier; | ||
import org.apache.flink.table.operations.CatalogSinkModifyOperation; | ||
import org.apache.flink.table.operations.ModifyOperation; | ||
import org.apache.flink.table.operations.Operation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Implementation for {@link StatementSet}. | ||
*/ | ||
@Internal | ||
class StatementSetImpl implements StatementSet { | ||
private final TableEnvironmentInternal tableEnvironment; | ||
private List<ModifyOperation> operations = new ArrayList<>(); | ||
|
||
protected StatementSetImpl(TableEnvironmentInternal tableEnvironment) { | ||
this.tableEnvironment = tableEnvironment; | ||
} | ||
|
||
@Override | ||
public StatementSet addInsertSql(String statement) { | ||
List<Operation> operations = tableEnvironment.getParser().parse(statement); | ||
|
||
if (operations.size() != 1) { | ||
throw new TableException("Only single statement is supported."); | ||
} | ||
|
||
Operation operation = operations.get(0); | ||
if (operation instanceof ModifyOperation) { | ||
this.operations.add((ModifyOperation) operation); | ||
} else { | ||
throw new TableException("Only insert statement is supported now."); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public StatementSet addInsert(String targetPath, Table table) { | ||
return addInsert(targetPath, table, false); | ||
} | ||
|
||
@Override | ||
public StatementSet addInsert(String targetPath, Table table, boolean overwrite) { | ||
UnresolvedIdentifier unresolvedIdentifier = tableEnvironment.getParser().parseIdentifier(targetPath); | ||
ObjectIdentifier objectIdentifier = tableEnvironment.getCatalogManager() | ||
.qualifyIdentifier(unresolvedIdentifier); | ||
|
||
operations.add(new CatalogSinkModifyOperation( | ||
objectIdentifier, | ||
table.getQueryOperation(), | ||
Collections.emptyMap(), | ||
overwrite, | ||
Collections.emptyMap())); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public String explain(ExplainDetail... extraDetails) { | ||
List<Operation> operationList = operations.stream().map(o -> (Operation) o).collect(Collectors.toList()); | ||
return tableEnvironment.explainInternal(operationList, extraDetails); | ||
} | ||
|
||
@Override | ||
public TableResult execute() { | ||
try { | ||
return tableEnvironment.executeInternal(operations); | ||
} finally { | ||
operations.clear(); | ||
} | ||
} | ||
} |
Oops, something went wrong.