Repository navigation
[FLINK-41214][table] Add Table#materializeInto for materialized tables #29838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Timm0
wants to merge
2
commits into
apache:master
Choose a base branch
from
Timm0:FLINK-41214
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,81 @@ | ||
| --- | ||
| title: Table API | ||
| weight: 5 | ||
| type: docs | ||
| --- | ||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| `Table#materializeInto` declares the query of a `Table` as the definition of a materialized table, | ||
| and `execute()` submits the declaration with the semantics of | ||
| [`CREATE OR ALTER MATERIALIZED TABLE ... AS <query>`]({{< ref "docs/sql/materialized-table/statements" >}}#or-alter). | ||
|
|
||
| # Setup | ||
|
|
||
| Continuous refresh jobs need a session cluster as the execution target of the `TableEnvironment`, | ||
| and redeploying a running materialized table needs a savepoint directory. See | ||
| [Using Table API]({{< ref "docs/sql/materialized-table/deployment" >}}#using-table-api) for the | ||
| supported targets. The catalog must supply the storage of the materialized table. | ||
|
|
||
| # Creating a Materialized Table | ||
|
|
||
| ```java | ||
| MaterializedTable revenue = tEnv.from("orders") | ||
| .groupBy($("region")) | ||
| .select($("region"), $("amount").sum().as("revenue")) | ||
| .materializeInto("revenue", Duration.ofMinutes(1)) | ||
| .execute(); | ||
| ``` | ||
|
|
||
| The schema is derived from the stored SQL text of the query, and the path is resolved against the | ||
| current catalog and database. The freshness is optional and defaults to | ||
| [materialized-table.default-freshness.continuous]({{< ref "docs/dev/table/config" >}}#materialized-table-default-freshness-continuous). | ||
| The refresh mode is derived from the freshness (see [Refresh Mode]({{< ref "docs/sql/materialized-table/overview" >}}#refresh-mode)). | ||
| Full refresh mode needs a workflow scheduler and cannot be used from a plain `TableEnvironment`. | ||
|
|
||
| # Redeploying | ||
|
|
||
| `execute()` on an existing materialized table alters it, and a changed query follows the | ||
| schema-evolution rules of | ||
| [ALTER MATERIALIZED TABLE AS]({{< ref "docs/sql/materialized-table/statements" >}}#as-select_statement-1). | ||
| A running continuous refresh job is stopped with a savepoint and restarted without restoring it, | ||
| even if the declaration is unchanged, so streaming state is reset. | ||
|
|
||
| An existing materialized table keeps its freshness, so a freshness passed to `materializeInto` is | ||
| ignored. As with `CREATE OR ALTER` without `WITH` and `DISTRIBUTED BY` clauses, its options are | ||
| reset and its distribution is dropped. To keep them, redeploy with SQL `CREATE OR ALTER` and | ||
| restate the clauses. | ||
|
|
||
| If the changes are rejected, for example because the new query drops a column, `execute()` fails | ||
| and leaves the materialized table suspended with its previous definition. Resume it with | ||
| `ALTER MATERIALIZED TABLE ... RESUME` before you redeploy, because redeploying a suspended | ||
| materialized table clears its savepoint. | ||
|
|
||
| If the new refresh job starts but the catalog cannot record it, the job is cancelled and the old | ||
| job restarts from its savepoint. If that cancel fails, the materialized table is rolled back and | ||
| left suspended instead of restarting the old job. | ||
|
|
||
| # Limitations | ||
|
|
||
| - The query of a refresh job is stored as SQL text, so it must be expressible in SQL. Inline | ||
| functions (unless a custom `SqlFactory` serializes them), tables created from a `TableDescriptor` | ||
| or a `DataStream`, and operations without a SQL form, such as session group windows, fail at | ||
| `materializeInto`. | ||
| - As in SQL, referring to temporary tables, views, functions or models is not supported. They are | ||
| not rejected, but a refresh job started from another session can't resolve them. |
This file contains hidden or 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,81 @@ | ||
| --- | ||
| title: Table API | ||
| weight: 5 | ||
| type: docs | ||
| --- | ||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| `Table#materializeInto` declares the query of a `Table` as the definition of a materialized table, | ||
| and `execute()` submits the declaration with the semantics of | ||
| [`CREATE OR ALTER MATERIALIZED TABLE ... AS <query>`]({{< ref "docs/sql/materialized-table/statements" >}}#or-alter). | ||
|
|
||
| # Setup | ||
|
|
||
| Continuous refresh jobs need a session cluster as the execution target of the `TableEnvironment`, | ||
| and redeploying a running materialized table needs a savepoint directory. See | ||
| [Using Table API]({{< ref "docs/sql/materialized-table/deployment" >}}#using-table-api) for the | ||
| supported targets. The catalog must supply the storage of the materialized table. | ||
|
|
||
| # Creating a Materialized Table | ||
|
|
||
| ```java | ||
| MaterializedTable revenue = tEnv.from("orders") | ||
| .groupBy($("region")) | ||
| .select($("region"), $("amount").sum().as("revenue")) | ||
| .materializeInto("revenue", Duration.ofMinutes(1)) | ||
| .execute(); | ||
| ``` | ||
|
|
||
| The schema is derived from the stored SQL text of the query, and the path is resolved against the | ||
| current catalog and database. The freshness is optional and defaults to | ||
| [materialized-table.default-freshness.continuous]({{< ref "docs/dev/table/config" >}}#materialized-table-default-freshness-continuous). | ||
| The refresh mode is derived from the freshness (see [Refresh Mode]({{< ref "docs/sql/materialized-table/overview" >}}#refresh-mode)). | ||
| Full refresh mode needs a workflow scheduler and cannot be used from a plain `TableEnvironment`. | ||
|
|
||
| # Redeploying | ||
|
|
||
| `execute()` on an existing materialized table alters it, and a changed query follows the | ||
| schema-evolution rules of | ||
| [ALTER MATERIALIZED TABLE AS]({{< ref "docs/sql/materialized-table/statements" >}}#as-select_statement-1). | ||
| A running continuous refresh job is stopped with a savepoint and restarted without restoring it, | ||
| even if the declaration is unchanged, so streaming state is reset. | ||
|
|
||
| An existing materialized table keeps its freshness, so a freshness passed to `materializeInto` is | ||
| ignored. As with `CREATE OR ALTER` without `WITH` and `DISTRIBUTED BY` clauses, its options are | ||
| reset and its distribution is dropped. To keep them, redeploy with SQL `CREATE OR ALTER` and | ||
| restate the clauses. | ||
|
|
||
| If the changes are rejected, for example because the new query drops a column, `execute()` fails | ||
| and leaves the materialized table suspended with its previous definition. Resume it with | ||
| `ALTER MATERIALIZED TABLE ... RESUME` before you redeploy, because redeploying a suspended | ||
| materialized table clears its savepoint. | ||
|
|
||
| If the new refresh job starts but the catalog cannot record it, the job is cancelled and the old | ||
| job restarts from its savepoint. If that cancel fails, the materialized table is rolled back and | ||
| left suspended instead of restarting the old job. | ||
|
|
||
| # Limitations | ||
|
|
||
| - The query of a refresh job is stored as SQL text, so it must be expressible in SQL. Inline | ||
| functions (unless a custom `SqlFactory` serializes them), tables created from a `TableDescriptor` | ||
| or a `DataStream`, and operations without a SQL form, such as session group windows, fail at | ||
| `materializeInto`. | ||
| - As in SQL, referring to temporary tables, views, functions or models is not supported. They are | ||
| not rejected, but a refresh job started from another session can't resolve them. | ||
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
...e/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedPipeline.java
This file contains hidden or 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,64 @@ | ||
| /* | ||
| * 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; | ||
| import org.apache.flink.table.catalog.ObjectIdentifier; | ||
|
|
||
| /** | ||
| * A pending declaration of one materialized table, created by {@link | ||
| * Table#materializeInto(String)}. | ||
| */ | ||
| @PublicEvolving | ||
| public interface MaterializedPipeline { | ||
|
|
||
| /** | ||
| * The fully qualified identifier this declaration will create or alter. | ||
| * | ||
| * <p>Resolved against the current catalog and database at {@link Table#materializeInto}. | ||
| */ | ||
| ObjectIdentifier getIdentifier(); | ||
|
|
||
| /** | ||
| * Submits this declaration with the semantics of {@code CREATE OR ALTER MATERIALIZED TABLE}. | ||
| * | ||
| * <p>If the materialized table exists and its continuous refresh job is running, the job is | ||
| * stopped with a savepoint and a new refresh job is started, which does not restore that | ||
| * savepoint. This also happens if the declaration is unchanged. The stop requires a savepoint | ||
| * directory ({@code execution.checkpointing.savepoint-dir}); without one, this fails before the | ||
| * stop. If the changes are rejected or cannot be written after the stop, the materialized table | ||
| * is left suspended with its previous definition and the savepoint. If the new refresh job | ||
| * starts but the catalog cannot record it, that job is cancelled and the previous refresh job | ||
| * restarts from the savepoint. If the cancel fails, the materialized table is rolled back and | ||
| * left suspended instead. | ||
| * | ||
| * <p>A suspended materialized table is altered and stays suspended, and its stored savepoint is | ||
| * cleared, so a later {@code RESUME} starts the refresh job without state. A materialized table | ||
| * in full refresh mode is altered without changing its refresh workflow. | ||
|
Timm0 marked this conversation as resolved.
Resolved
|
||
| * | ||
| * <p>An existing materialized table keeps its freshness, comment and partition keys, so a | ||
| * freshness given to {@link Table#materializeInto(String, java.time.Duration)} is ignored. Its | ||
| * options are reset and its distribution is dropped, as by a {@code CREATE OR ALTER | ||
| * MATERIALIZED TABLE} without {@code WITH} and {@code DISTRIBUTED BY} clauses. | ||
| * | ||
| * <p>Returns once the catalog entry is written and, if a continuous refresh job is started, | ||
| * once that job has been submitted. | ||
| */ | ||
| MaterializedTable execute(); | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
...able/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedTable.java
This file contains hidden or 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,32 @@ | ||
| /* | ||
| * 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; | ||
| import org.apache.flink.table.catalog.ObjectIdentifier; | ||
|
|
||
| /** | ||
| * A handle on an existing materialized table, returned by {@link MaterializedPipeline#execute()}. | ||
| */ | ||
| @PublicEvolving | ||
| public interface MaterializedTable { | ||
|
|
||
| /** The fully qualified identifier of the materialized table. */ | ||
| ObjectIdentifier getIdentifier(); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.