-
Notifications
You must be signed in to change notification settings - Fork 13k
/
CatalogMaterializedTable.java
280 lines (239 loc) · 9.83 KB
/
CatalogMaterializedTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* 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.catalog;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.table.api.Schema;
import org.apache.flink.util.Preconditions;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Represents the unresolved metadata of a materialized table in a {@link Catalog}.
*
* <p>Materialized Table definition: In the context of streaming-batch unified storage, it provides
* full history data and incremental changelog. By defining the data's production business logic and
* freshness, data refresh is achieved through continuous or full refresh pipeline, while also
* possessing the capability for both batch and incremental consumption.
*
* <p>The metadata for {@link CatalogMaterializedTable} includes the following four main parts:
*
* <ul>
* <li>Schema, comments, options and partition keys.
* <li>Data freshness, which determines when the data is generated and becomes visible for user.
* <li>Data production business logic, also known as the definition query.
* <li>Background refresh pipeline, either through a flink streaming or periodic flink batch job,
* it is initialized after materialized table is created.
* </ul>
*/
@PublicEvolving
public interface CatalogMaterializedTable extends CatalogBaseTable {
/** Builder for configuring and creating instances of {@link CatalogMaterializedTable}. */
@PublicEvolving
static CatalogMaterializedTable.Builder newBuilder() {
return new CatalogMaterializedTable.Builder();
}
@Override
default TableKind getTableKind() {
return TableKind.MATERIALIZED_TABLE;
}
/**
* Check if the table is partitioned or not.
*
* @return true if the table is partitioned; otherwise, false
*/
boolean isPartitioned();
/**
* Get the partition keys of the table. This will be an empty set if the table is not
* partitioned.
*
* @return partition keys of the table
*/
List<String> getPartitionKeys();
/**
* Returns a copy of this {@code CatalogMaterializedTable} with given table options {@code
* options}.
*
* @return a new copy of this table with replaced table options
*/
CatalogMaterializedTable copy(Map<String, String> options);
/**
* Returns a copy of this {@code CatalogDynamicTable} with given refresh info.
*
* @return a new copy of this table with replaced refresh info
*/
CatalogMaterializedTable copy(
RefreshStatus refreshStatus,
String refreshHandlerDescription,
byte[] serializedRefreshHandler);
/** Return the snapshot specified for the table. Return Optional.empty() if not specified. */
Optional<Long> getSnapshot();
/**
* The definition query text of materialized table, text is expanded in contrast to the original
* SQL. This is needed because the context such as current DB is lost after the session, in
* which view is defined, is gone. Expanded query text takes care of this, as an example.
*
* <p>For example, for a materialized table that is defined in the context of "default" database
* with a query {@code select * from test1}, the expanded query text might become {@code select
* `test1`.`name`, `test1`.`value` from `default`.`test1`}, where table test1 resides in
* database "default" and has two columns ("name" and "value").
*
* @return the materialized table definition in expanded text.
*/
String getDefinitionQuery();
/**
* Get the freshness of materialized table which is used to determine the physical refresh mode.
*/
Duration getFreshness();
/** Get the logical refresh mode of materialized table. */
LogicalRefreshMode getLogicalRefreshMode();
/** Get the physical refresh mode of materialized table. */
RefreshMode getRefreshMode();
/** Get the refresh status of materialized table. */
RefreshStatus getRefreshStatus();
/** Return summary description of refresh handler. */
Optional<String> getRefreshHandlerDescription();
/**
* Return the serialized refresh handler of materialized table. This will not be used for
* describe table.
*/
@Nullable
byte[] getSerializedRefreshHandler();
/** The logical refresh mode of materialized table. */
@PublicEvolving
enum LogicalRefreshMode {
/**
* The refresh pipeline will be executed in continuous mode, corresponding to {@link
* RefreshMode#CONTINUOUS}.
*/
CONTINUOUS,
/**
* The refresh pipeline will be executed in full mode, corresponding to {@link
* RefreshMode#FULL}.
*/
FULL,
/**
* The refresh pipeline mode is determined by freshness of materialized table, either {@link
* RefreshMode#FULL} or {@link RefreshMode#CONTINUOUS}.
*/
AUTOMATIC
}
/** The physical refresh mode of materialized table. */
@PublicEvolving
enum RefreshMode {
CONTINUOUS,
FULL
}
/** Background refresh pipeline status of materialized table. */
@PublicEvolving
enum RefreshStatus {
INITIALIZING,
ACTIVATED,
SUSPENDED
}
// --------------------------------------------------------------------------------------------
/** Builder for configuring and creating instances of {@link CatalogMaterializedTable}. */
@PublicEvolving
class Builder {
private Schema schema;
private String comment;
private List<String> partitionKeys = Collections.emptyList();
private Map<String, String> options = Collections.emptyMap();
private @Nullable Long snapshot;
private String definitionQuery;
private Duration freshness;
private LogicalRefreshMode logicalRefreshMode;
private RefreshMode refreshMode;
private RefreshStatus refreshStatus;
private @Nullable String refreshHandlerDescription;
private @Nullable byte[] serializedRefreshHandler;
private Builder() {}
public Builder schema(Schema schema) {
this.schema = Preconditions.checkNotNull(schema, "Schema must not be null.");
return this;
}
public Builder comment(@Nullable String comment) {
this.comment = comment;
return this;
}
public Builder partitionKeys(List<String> partitionKeys) {
this.partitionKeys =
Preconditions.checkNotNull(partitionKeys, "Partition keys must not be null.");
return this;
}
public Builder options(Map<String, String> options) {
this.options = Preconditions.checkNotNull(options, "Options must not be null.");
return this;
}
public Builder snapshot(@Nullable Long snapshot) {
this.snapshot = snapshot;
return this;
}
public Builder definitionQuery(String definitionQuery) {
this.definitionQuery =
Preconditions.checkNotNull(
definitionQuery, "Definition query must not be null.");
return this;
}
public Builder freshness(Duration freshness) {
this.freshness = Preconditions.checkNotNull(freshness, "Freshness must not be null.");
return this;
}
public Builder logicalRefreshMode(LogicalRefreshMode logicalRefreshMode) {
this.logicalRefreshMode =
Preconditions.checkNotNull(
logicalRefreshMode, "Logical refresh mode must not be null.");
return this;
}
public Builder refreshMode(RefreshMode refreshMode) {
this.refreshMode =
Preconditions.checkNotNull(refreshMode, "Refresh mode must not be null.");
return this;
}
public Builder refreshStatus(RefreshStatus refreshStatus) {
this.refreshStatus =
Preconditions.checkNotNull(refreshStatus, "Refresh status must not be null.");
return this;
}
public Builder refreshHandlerDescription(@Nullable String refreshHandlerDescription) {
this.refreshHandlerDescription = refreshHandlerDescription;
return this;
}
public Builder serializedRefreshHandler(@Nullable byte[] serializedRefreshHandler) {
this.serializedRefreshHandler = serializedRefreshHandler;
return this;
}
public CatalogMaterializedTable build() {
return new DefaultCatalogMaterializedTable(
schema,
comment,
partitionKeys,
options,
snapshot,
definitionQuery,
freshness,
logicalRefreshMode,
refreshMode,
refreshStatus,
refreshHandlerDescription,
serializedRefreshHandler);
}
}
}