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

Compare with Current View Page History

« Previous Version 3 Next »

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

deduplicate

Specification:

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


Description:
Deduplication according to the specified deduplication fields.
- param dedupFields: the fields to perform deduplication against
- param orderField: the time attribute field, it defines the order of duplicate rows
- param keepFirst: Determines which row to keep. If true, keep the first occurrence and drop the later ones, otherwise, keep the last occurrence. 

Example:

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

Python:
table.deduplicate([table.a, table.b], table.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.
- param n: the number of elements to return for each partition
- param partitionFields: the partition fields, each partition will have a top-n result
- param orderFields: the order fields, the ordering direction could be different for different columns

Example:

Java:
Expression[] partitionFields = new Expression[] {$("a"), $("b")};

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

Python:
table.topn(3, [table.a, table.b], [table.c, table.d.desc])


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:

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

Python:
table.fillna(1, ['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:

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

Python:
table.fillna({"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:

Java:
table.dropna(2)

Python:
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:

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

Python:
table.dropna(1, ['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:

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

Python:
table.replace({1: 3, 2: 4}, ['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

  • No labels