dawidwys commented on a change in pull request #8006: [FLINK-11068][table] Convert the API classes of *Table to interfaces URL: https://github.com/apache/flink/pull/8006#discussion_r266771310
########## File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Table.java ########## @@ -0,0 +1,922 @@ +/* + * 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.expressions.Expression; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.functions.TemporalTableFunction; +import org.apache.flink.table.sinks.TableSink; + +/** + * A Table is the core component of the Table API. + * Similar to how the batch and streaming APIs have DataSet and DataStream, + * the Table API is built around {@link Table}. + * + * <p>Use the methods of {@link Table} to transform data. Use {@code TableEnvironment} to convert a + * {@link Table} back to a {@code DataSet} or {@code DataStream}. + * + * <p>When using Scala a {@link Table} can also be converted using implicit conversions. + * + * <p>Java Example: + * + * <pre> + * {@code + * ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); + * BatchTableEnvironment tEnv = BatchTableEnvironment.create(env); + * + * DataSet<Tuple2<String, Integer>> set = ... + * tEnv.registerTable("MyTable", set, "a, b"); + * + * Table table = tEnv.scan("MyTable").select(...); + * ... + * Table table2 = ... + * DataSet<MyType> set2 = tEnv.toDataSet(table2, MyType.class); + * } + * </pre> + * + * <p>Scala Example: + * + * <pre> + * {@code + * val env = ExecutionEnvironment.getExecutionEnvironment + * val tEnv = BatchTableEnvironment.create(env) + * + * val set: DataSet[(String, Int)] = ... + * val table = set.toTable(tEnv, 'a, 'b) + * ... + * val table2 = ... + * val set2: DataSet[MyType] = table2.toDataSet[MyType] + * } + * </pre> + * + * <p>Operations such as {@code join}, {@code select}, {@code where} and {@code groupBy} either + * take arguments in a Scala DSL or as an expression String. Please refer to the documentation for + * the expression syntax. + * + */ +@PublicEvolving +public interface Table { + + /** + * Returns the schema of this table. + */ + TableSchema getSchema(); + + /** + * Prints the schema of this table to the console in a tree format. + */ + void printSchema(); + + /** + * Performs a selection operation. Similar to an SQL SELECT statement. The field expressions + * can contain complex expressions and aggregations. + * + * <p>Example: + * + * <pre> + * {@code + * tab.select("key, value.avg + ' The average' as average") + * } + * </pre> + */ + Table select(String fields); + + /** + * Performs a selection operation. Similar to an SQL SELECT statement. The field expressions + * can contain complex expressions and aggregations. + * + * <p>Example: + * + * <pre> + * {@code + * tab.select('key, 'value.avg + " The average" as 'average) + * } + * </pre> + */ + Table select(Expression... fields); + + /** + * Creates {@link TemporalTableFunction} backed up by this table as a history table. + * Temporal Tables represent a concept of a table that changes over time and for which + * Flink keeps track of those changes. {@link TemporalTableFunction} provides a way how to + * access those data. + * + * <p>For more information please check Flink's documentation on Temporal Tables. + * + * <p>Currently {@link TemporalTableFunction}s are only supported in streaming. + * + * @param timeAttribute Must points to a time attribute. Provides a way to compare which + * records are a newer or older version. + * @param primaryKey Defines the primary key. With primary key it is possible to update + * a row or to delete it. + * @return {@link TemporalTableFunction} which is an instance of {@link TableFunction}. + * It takes one single argument, the {@code timeAttribute}, for which it returns + * matching version of the {@link Table}, from which {@link TemporalTableFunction} + * was created. + */ + TemporalTableFunction createTemporalTableFunction(String timeAttribute, String primaryKey); + + /** + * Creates {@link TemporalTableFunction} backed up by this table as a history table. + * Temporal Tables represent a concept of a table that changes over time and for which + * Flink keeps track of those changes. {@link TemporalTableFunction} provides a way how to + * access those data. + * + * <p>For more information please check Flink's documentation on Temporal Tables. + * + * <p>Currently {@link TemporalTableFunction}s are only supported in streaming. + * + * @param timeAttribute Must points to a time indicator. Provides a way to compare which + * records are a newer or older version. + * @param primaryKey Defines the primary key. With primary key it is possible to update + * a row or to delete it. + * @return {@link TemporalTableFunction} which is an instance of {@link TableFunction}. + * It takes one single argument, the {@code timeAttribute}, for which it returns + * matching version of the {@link Table}, from which {@link TemporalTableFunction} + * was created. + */ + TemporalTableFunction createTemporalTableFunction(Expression timeAttribute, Expression primaryKey); + + /** + * Renames the fields of the expression result. Use this to disambiguate fields before + * joining to operations. + * + * <p>Example: + * + * <pre> + * {@code + * tab.as("a, b") + * } + * </pre> + */ + Table as(String fields); + + /** + * Renames the fields of the expression result. Use this to disambiguate fields before + * joining to operations. + * + * <p>Example: + * + * <pre> + * {@code + * tab.as('a, 'b) + * } + * </pre> + */ + Table as(Expression... fields); + + /** + * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE + * clause. + * + * <p>Example: + * + * <pre> + * {@code + * tab.filter("name = 'Fred'") + * } + * </pre> + */ + Table filter(String predicate); + + /** + * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE + * clause. + * + * <p>Example: + * + * <pre> + * {@code + * tab.filter('name === "Fred") + * } + * </pre> + */ + Table filter(Expression predicate); + + /** + * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE + * clause. + * + * <p>Example: + * + * <pre> + * {@code + * tab.where("name = 'Fred'") + * } + * </pre> + */ + Table where(String predicate); + + /** + * Filters out elements that don't pass the filter predicate. Similar to a SQL WHERE + * clause. + * + * <p>Example: + * + * <pre> + * {@code + * tab.where('name === "Fred") + * } + * </pre> + */ + Table where(Expression predicate); + + /** + * Groups the elements on some grouping keys. Use this before a selection with aggregations + * to perform the aggregation on a per-group basis. Similar to a SQL GROUP BY statement. + * + * <p>Example: + * + * <pre> + * {@code + * tab.groupBy("key").select("key, value.avg") + * } + * </pre> + */ + GroupedTable groupBy(String fields); + + /** + * Groups the elements on some grouping keys. Use this before a selection with aggregations + * to perform the aggregation on a per-group basis. Similar to a SQL GROUP BY statement. + * + * <p>Example: + * + * <pre> + * {@code + * tab.groupBy('key).select('key, 'value.avg) + * } + * </pre> + */ + GroupedTable groupBy(Expression... fields); + + /** + * Removes duplicate values and returns only distinct (different) values. + * + * <p>Example: + * + * <pre> + * {@code + * tab.select("key, value").distinct() + * } + * </pre> + */ + Table distinct(); + + /** + * Joins two {@link Table}s. Similar to an SQL join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. You can use + * where and select clauses after a join to further specify the behaviour of the join. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} . + * + * <p>Example: + * + * <pre> + * {@code + * left.join(right).where('a === 'b && 'c > 3).select('a, 'b, 'd) + * } + * </pre> + */ + Table join(Table right); + + /** + * Joins two {@link Table}s. Similar to an SQL join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} . + * + * <p>Example: + * + * <pre> + * {@code + * left.join(right, "a = b") + * } + * </pre> + */ + Table join(Table right, String joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} . + * + * <p>Example: + * + * <pre> + * {@code + * left.join(right, 'a === 'b).select('a, 'b, 'd) + * } + * </pre> + */ + Table join(Table right, Expression joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL left outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.leftOuterJoin(right).select('a, 'b, 'd) + * } + * </pre> + */ + Table leftOuterJoin(Table right); + + /** + * Joins two {@link Table}s. Similar to an SQL left outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.leftOuterJoin(right, "a = b").select("a, b, d") + * } + * </pre> + */ + Table leftOuterJoin(Table right, String joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL left outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.leftOuterJoin(right, 'a === 'b).select('a, 'b, 'd) + * } + * </pre> + */ + Table leftOuterJoin(Table right, Expression joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL right outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.rightOuterJoin(right, "a = b").select('a, 'b, 'd) + * } + * </pre> + */ + Table rightOuterJoin(Table right, String joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL right outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.rightOuterJoin(right, 'a === 'b).select('a, 'b, 'd) + * } + * </pre> + */ + Table rightOuterJoin(Table right, Expression joinPredicate); + + /** + * Joins two {@link Table}s. Similar to an SQL full outer join. The fields of the two joined + * operations must not overlap, use {@code as} to rename fields if necessary. + * + * <p>Note: Both tables must be bound to the same {@code TableEnvironment} and its + * {@code TableConfig} must have null check enabled (default). + * + * <p>Example: + * + * <pre> + * {@code + * left.fullOuterJoin(right, "a = b").select('a, 'b, 'd) Review comment: Let's try to stay in the same API approach. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
