Discussion threadhttps://lists.apache.org/thread/zon967w7synby8z6m1s7dj71dhkh9ccy
Vote threadhttps://lists.apache.org/thread/lc2h6xpk25bmvm4c5pgtfggm920j2ckr
JIRA

Unable to render Jira issues macro, execution error.

Release1.19

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

Motivation

Many SQL vendors expose additional metadata via so-called "pseudo columns" or "system columns" next to the physical columns.

Some examples can be found here:

  • BigQuery exposes _PARTITIONTIME: Ingestion-time partitioned tables contain a pseudo-column named _PARTITIONTIME, which is the partitioning column.
  • Oracle exposes ROWID: For each row in the database, the ROWID pseudocolumn returns a row’s address. 
  • DB2 exposes LEVEL: A pseudo column is an identifier that shares the same namespace as columns and variables. After regular name resolution for a column has failed, Db2® attempts to match an unqualified identifier to a pseudo column name. 
  • According to the SQL standard, system-versioned tables contain some system columns: Columns of a system-versioned table include a system-time period start column and a system-time period end column.
  • Some parts of the Calcite stack already consider system columns (e.g. org.apache.calcite.sql.validate.SqlValidatorNamespace#getRowTypeSansSystemColumns)

Esp. for platform teams and connector/catalog implementers, it would be beneficial to offer additional columns for commonly used properties such as the row's timestamp or row number.

However, those columns should not be selected by default when expaning SELECT *.  Also for the sake of backward compatibility.

Flink SQL already offers pseudo columns next to the physical columns exposed as metadata columns.

This proposal suggests to evolve the existing column design slightly to be more useful for platform providers.

Public Interfaces

  • SELECT *

Proposed Changes

Instead of introducing a new concept around system columns, we propose to leverage virtual metadata column concept and adjust their expanding behavior.

A new ConfigOption configures the expanding behavior:

    @Documentation.TableOption(execMode = Documentation.ExecMode.BATCH_STREAMING)
    public static final ConfigOption<List<ColumnExpansionStrategy>>
            TABLE_COLUMN_EXPANSION_STRATEGY =
                    key("table.column-expansion-strategy")
                            .enumType(ColumnExpansionStrategy.class)
                            .asList()
                            .defaultValues()
                            .withDescription(
                                    "Configures the default expansion behavior of 'SELECT *'. "
                                            + "By default, all top-level columns of the table's "
                                            + "schema are selected and nested fields are retained.");

    enum ColumnExpansionStrategy {
        // Excludes `c METADATA VIRTUAL FROM k` from SELECT *
        EXCLUDE_ALIASED_VIRTUAL_METADATA_COLUMNS,
        
        // Excludes `c METADATA VIRTUAL` from SELECT *
        EXCLUDE_DEFAULT_VIRTUAL_METADATA_COLUMNS
    }

Notes:

  • The ConfigOption is prepared for future extensions (e.g. excluding computed columns, system columns, or default columns)
  • Since the ConfigOption is a list, multiple strategies can be defined.


Excluded columns are not expanded in a SELECT * FROM t or tableEnv.from("t").select(Expressions.$("*")).

The change can be applied to SqlValidatorImpl and the corresponding class in Table API.


Users need to select them explicitly. This allows for adding more columns in the future without breaking existing SQL queries.

DESCRIBE works as before and will list the column. 

SQL Example: SELECT / INSERT INTO

-- Given a table t with schema
-- (
--   i INT,
--   s STRING,
--   rt1 TIMESTAMP_LTZ(3) METADATA VIRTUAL,
--   rt2 TIMESTAMP_LTZ(3) METADATA VIRTUAL FROM 'timestamp',
--   rt3 TIMESTAMP_LTZ(3) METADATA,
--   rt4 TIMESTAMP_LTZ(3) METADATA FROM 'timestamp'
-- )


SELECT * FROM t;
-- returns (i, s, rt1, rt2, rt3, rt4)


-- TableEnvironment is configured as
-- table.column-expansion-strategy = EXCLUDE_DEFAULT_VIRTUAL_METADATA_COLUMNS

SELECT * FROM t;
-- returns (i, s, rt2, rt3, rt4)

SELECT rt1, * FROM t;
-- rt1 must be selected explicitly and returns (rt1, i, s, rt2, rt3, rt4)


-- TableEnvironment is configured as
-- table.column-expansion-strategy = EXCLUDE_ALIASED_VIRTUAL_METADATA_COLUMNS

SELECT * FROM t;
-- returns (i, s, rt1, rt3, rt4)

INSERT INTO t SELECT * FROM t;
-- always works


Compatibility, Deprecation, and Migration Plan

Major compatibility issues should not arise as the whole behavior depends on the ConfigOption.

Test Plan

Unit tests on table environment and maybe one ITCase should be sufficient.

Rejected Alternatives

Completely new concept of System Columns

New concept of system columns with a reserved prefix such as `$`.

→ Metadata columns fit nicely and require no additional interfaces for connectors and/or changes within planner rules.

→ Concept of system columns including columns that can be expose per operation are future work.