Skip to content

sql: support VIRTUAL computed columns #57608

@RaduBerinde

Description

@RaduBerinde

This issue tracks implementing virtual computed columns (using VIRTUAL instead of STORED). This is closely related to #9682 - the expression-based index columns will be implemented as virtual columns internally. It is also related to hash shared indexes where we would like the bucket column to be virtual.

Running checklist:

  • parser support
  • test catalog support
  • descriptors and catalog support
  • support querying tables with virtual columns
  • mutations of tables with virtual columns (with or without indexes on virtual columns)
    • INSERT
    • DELETE
    • UPDATE
    • UPSERT
      • Add rules for lookup joins with projections to fix plans for UPSERT
  • disallow PK on virtual columns
  • disallow indexes STORING virtual columns
  • disallow FKs on virtual columns
  • disallow computed/virtual columns depending on other virtual columns (already disallowed for all computed columns)
  • support virtual columns in partial index predicates
  • support adding / removing virtual columns
  • support adding / removing indexes on virtual columns
  • disallow or support virtual columns in check constraints
  • support or disallow inverted indexes on virtual columns
  • support unique (and partial unique) indexes on virtual columns
  • support virtual column as leading column on a multi-column inverted index
  • support partial indexes on virtual columns
  • support virtual columns in unique constraints without index
  • teach SQLsmith about virtual columns
  • inline virtual column projections into filters (allowing use of indexes on virtual columns more cases)
  • add backup/restore test with table with virtual columns

Non-critical items:

  • figure out how to prove implication for cases like:
CREATE TABLE t (
  k INT PRIMARY KEY,
  a INT,
  b INT,
  c INT AS (b + 10) VIRTUAL,
  INDEX (a) WHERE c IN (10, 20, 30)
)

SELECT k FROM t WHERE c = 20

(filed separate issue #61565).

  • handle numeric references case where we specify a virtual column but not a dependent one (applies to stored computed columns as well); filed separate issue opt: better validation of column numeric references #61563.
  • teach indexConstraintCtx.simplifyFilter to remove the redundant filters like the b:2 = 3 below:
exec-ddl
CREATE TABLE tmp (
  a INT,
  b INT,
  v INT AS (b + 10) VIRTUAL,
  INDEX idx (v)
)
----

opt
SELECT * FROM tmp WHERE v = 13
----
  project
   ├── columns: a:1 b:2!null v:3!null
   ├── immutable
   ├── fd: ()-->(2,3)
   ├── select
   │    ├── columns: a:1 b:2!null
   │    ├── fd: ()-->(2)
   │    ├── index-join tmp
   │    │    ├── columns: a:1 b:2
   │    │    └── scan tmp@idx
   │    │         ├── columns: rowid:4!null
   │    │         ├── constraint: /3/4: [/13 - /13]
   │    │         └── key: (4)
   │    └── filters
   │         └── b:2 = 3 [outer=(2), constraints=(/2: [/3 - /3]; tight), fd=()-->(2)]
   └── projections
        └── b:2 + 10 [as=v:3, outer=(2), immutable]

(filed separate issue #61566).

21.2 items:

Metadata

Metadata

Assignees

Labels

C-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions