Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status

Current state: Under DiscussionAccepted

Discussion thread: https://lists.apache.org/thread/m4r3wrd7p96wdst3nz3ncqzog6kf51cf
JIRA:

 here (<- link to Vote thread: https://issueslists.apache.org/thread/jira/browse/FLINK-XXXX)fosvz0zcyfn6bp6vz2oxl45vq9qhkn2v

JIRA:

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyFLINK-31874

Released: <Flink Version>1.18.0

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

Motivation

The TRUNCATE TABLE statement is a SQL command that allows users to quickly and efficiently delete all rows from a table without dropping the table itself.  This statement is commonly used in data warehouse, where large data sets are frequently loaded and unloaded from tables. It can improve performance by reducing the amount of time and resources required to delete large amounts of data.

...

Considering the TRUNCATE TABLE statment is useful and widely-used, this FLIP is aimed to support TRUNCATE TABLE  statment in Flink. This FLIP will bring Flink the TRUNCATE TABLE  syntax and an interface with which the coresponding connectors can implement their own  logic for truncating table.

Note:  Considering the use cases of truncate table are mainly for batch scenario and the semantic in stream scenario should be discussed separately, this FLIP to to support truncate table in batch only.

Public Interfaces

Syntax

We propose add the following syntax for TRUNCATE TABLE statement.

Code Block
languagesql
TRUNCATE TABLE table_name

It supports trucate permanent or temporary table,  if it's a view table, it should throw TableException with message Truncate a view table is not support .

...

Public interfaces Changes

We propose add a inerface for TRUNCATE TABLE statment. The connectors can implement this interface to custom their logic for truncating table.

Code Block
languagejava
/**
 * Enables to delete all existing data in a {@link DynamicTableSink} table using {@code TRUNCATE
 * TABLE} statement.
 *
 * <p>For {@code TRUNCATE TABLE} statement, if the corresponding {@link DynamicTableSink} have
 * implemented this interface, then the method {@link #truncateTable()} will be invoked in execution
 * phase. Otherwise, Flink will throw an exception directly.
 */
@PublicEvolving
public interface SupportsTruncate {

    /**
     * Execute truncating table.
     *
     * @return trueNote: please remember to throw exception if truncate the table successfully otherwise falsethe truncation hasn't been executed successfully,
     * otherwise it'll be still considered to haven been executed successfully by Flink.
     */
    booleanvoid executeTruncation();
}


Proposed Changes

1: Add a parse rule in parserImpls.ftl to match TRUNCATE TABLE  statement and convert it to SqlTruncateTable 

...

3: TruncateTableOperation  should implement ExecutableOperation, and in method #execute(Context ctx), it will  get the DynamicTableSink  of the table which should implement SupportsTruncate   and then call method SupportsTruncate#executeTruncation  directly to truncate the table. If the DynamicTableSink  doesn't implemente this interface, it should throw TableException('The truncate statement for the table is not supported as it hasn't implemented the interface SupportsTruncate') . For some sinks that doesn't support deleting data, it can also implements this interface but throw more concrete exception like "xxx donesn't support to truncate a table as delete is impossible for xxx".

Compatibility, Deprecation, and Migration Plan

No any compatibility issue.

Test Plan

It'll be covered by UT & IT.

Rejected Alternatives

If there are alternative ways of accomplishing the same thing, what were they? The purpose of this section is to motivate why the design is the way it is and not some other way.