You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Status

Current state["Under Discussion"]

Discussion threadhere (<- link to https://mail-archives.apache.org/mod_mbox/flink-dev/)

JIRAhere (<- link to https://issues.apache.org/jira/browse/FLINK-XXXX)

Released: <Flink Version>

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

Table API is a unified, relational API for streaming and batch processing which shares the same underlying query optimization and query execution stack with SQL. As more and more features and functionalities are added in SQL, Table API is also becoming more and more powerful as most optimizations and functionalities are shared between them. 

For the Table API itself, the community is also consistently improving it, including but not limited to the following:

  • We have supported several row-based operations, e.g. map/flatMap/aggregate/flatAggregate in FLIP-29 and column-based operations, e.g. addColumns/renameColumns/dropColumns/addOrReplaceColumns in Flink 1.9
  • Expression DSL has been introduced in the Table API in FLIP-55 which improves the usability a lot.
  • In FLIP-129, it has proposed to refactor the existing Descriptor API to fill the functionality gap between Descriptor API and SQL DDL.
  • Python Table API is also supported since Flink 1.9 which allows users to write Table API programs in Python language.


In this FLIP, we’d like to continuously improve the Table API by introducing a few new methods as currently there are a few problems for the Table API: some tasks are not easy to express with Table API e.g. deduplication, topn, etc, or not easy to express when there are hundreds of columns in a table, e.g. null data handling, etc

The proposed changes could be summarized as the following categories:

  • Make Table API users to easily leverage the powerful features in SQL, e.g. deduplication, topn, etc
  • Provide convenient frequently used functionalities, e.g. introducing a series of methods for null data handling (it will become a problem if there are hundreds of columns) and data sampling and splitting (it is very common in ML to split a data set into multiple splits separately for training and validation)

Public Interfaces

Table Operations

dropDuplicates


Specification:

Table dropDuplicates(Expression[] dedupFields, Expression orderField, boolean keepFirst)


Description:
Deduplication according to the specified deduplication fields.

Example:

table.dropDuplicates(new Expression[] {$("a"), $("b")}, $("proctime"), true)


It’s equivalent to the following SQL:

SELECT a, b, c

FROM (

   SELECT a, b, c,

     ROW_NUMBER() OVER (PARTITION BY a, b ORDER BY proctime asc) AS rownum

   FROM table_name
) WHERE rownum = 1

topn

Specification:

Table topn(int n, Expression[] partitionFields, Expression[] orderFields)


Description:
Returns the largest/smallest n elements ordered by the specified orderFields for each group specified by the partitionFields.

Example:

Expression[] partitionFields = new Expression[] {$("a"), $("b")};
Expression[] orderFields = new Expression[] { $("c"), ($("d").desc() };
table.topn(partitionFields, orderFields, 3)


It’s equivalent to the following SQL:

SELECT a, b, c, d, e

FROM (

   SELECT a, b, c, d, e

     ROW_NUMBER() OVER (PARTITION BY a, b ORDER BY c asc, d desc) AS rownum

   FROM table_name
) WHERE rownum = 3

describe

Specification:

TableResult describe()


Description:
Describe the schema of a table or a view.

Example:

table.describe().print()


It’s equivalent to the following SQL:

DESCRIBE orders

Null Data Handling

fillna

API Specification:

Table fillna(Object value)

Table fillna(Object value, String[] fields)

Table fillna(Map<String, Object> values)


Description:
Replace the specified fields with the specified value if they are null. 

- param value: the value to fill with
- param fields: it will only replace the specified fields.
- param values: the map key is the name of the field to replace and the map value is the value to replace with.

Example 1:

table.fillna(1, new String[] {"a", "b"})


It’s equivalent to the following SQL:

SELECT

    (case when a is null then 1 else a end) as a,
    (case when b is null then 1 else b end) as b,
    c
FROM T


Example 2:

table.fillna(ImmutableMap.of("a", 1, "b", 3))


It’s equivalent to the following SQL:

SELECT

    (case when a is null then 1 else a end) as a,
    (case when b is null then 3 else b end) as b,
    c
FROM T

dropna

API Specification:

Table dropna(int thresh)

Table dropna(int thresh, String[] fields)


Description:
Remove one row if the number of null fields exceeds the specified threshold.
- param thresh: remove a row if the number of non-null columns is less than the threshold. If it is      

                          -1, a row is removed only when all of the columns are null.
- param fields: it will only consider the specified fields


Example 1:

table.dropna(2)


It’s equivalent to the following SQL:

SELECT a, b, c
FROM (
    SELECT

      a,

      b,
      c,
      (case when a is not null then 1 else 0 end) + (case when b is not null then 1 else 0 end) + (case when c is not null then 1 else 0 end) as cnt
    FROM T
)
WHERE cnt < 2


Example 2:

table.dropna(1, new String[] {"a", "b"})


It’s equivalent to the following SQL:

SELECT a, b, c
FROM (
    SELECT

      a,

      b,
      c,
      (case when a is not null then 1 else 0 end) + (case when b is not null then 1 else 0 end) as cnt
    FROM T
)
WHERE cnt < 1

replace

API Specification:

Table replace(Map<T, T> replacement, String[] fields)


Description:
For the given fields, replace the values which are among the key of the replacement with the corresponding value.
- param replacement: the map key is the value to replace and map value is the value to replace with.
- param fields: the columns to consider

Example:

table.replace(ImmutableMap.of(1, 3, 2, 4), new String[] {"a", "b"})


It’s equivalent to the following SQL:

SELECT
    (case when a = 1 then 3
            when a = 2 then 4
      else a end) as a,
    (case when b = 1 then 3
            when b = 2 then 4
      else b end) as b,
    c
FROM T

Sampling

sample

API Specification:

Table sample(double fraction)

Table sample(double fraction, long seed)


Description:
Take a sample of the table according to the given fraction([0.0, 1.0]). 

Example:

table.sample(0.1)


It’s equivalent to the following SQL:

SELECT
  a, b, c
FROM T
WHERE RAND() < 0.1

split

API Specification:

Table[] split(double[] weights)

Table[] split(double[] weights, long seed)


Description:
Splits the table into multiple sub-tables according to the given weights.

Example:

table.split(new double[] { 0.1, 0.2, 0.3 })


It’s logically equivalent to the following SQL:

CREATE VIEW TT AS
SELECT
  a, b, c, RAND(100) as d
FROM T

CREATE VIEW TT1 AS
SELECT
  a, b, c
FROM TT
WHERE d < 0.1/(0.1 + 0.2 + 0.3)

CREATE VIEW TT2 AS
SELECT
  a, b, c
FROM TT
WHERE d >= 0.1/(0.1 + 0.2 + 0.3) and d < 0.2/(0.1 + 0.2 + 0.3)

CREATE VIEW TT3 AS
SELECT
  a, b, c
FROM TT
WHERE d >= 0.2/(0.1 + 0.2 + 0.3)

NOTE: The seed for all the RAND should be the same to make sure that the random value is the same for one element. This is to make sure that one element belongs to only one sub-table(e.g. TT1, TT2, TT3).

Compatibility, Deprecation, and Migration Plan

N/A: As all the methods are newly added, there are no compatibility issues.

Test Plan

N/A

Rejected Alternatives

N/A

  • No labels