ydb-platform / ydb-sqlalchemy

YQL Dialect for SQLAlchemy
Apache License 2.0
18 stars 5 forks source link

Feature: Secondary indexes #37

Closed LuckySting closed 3 months ago

LuckySting commented 3 months ago

Description

YDB supports secondary indexes over the table, which may be used to optimize the selection of data.

In this PR

This PR contains the dialect features to declare and usage of YDB Secondary Indexes within SQLAlchemy Index and hints.

Example

Declaration

sa.Index("my_index", table.c.c2, ydb_async=True, ydb_cover=["c3"])

is rendered as

ALTER TABLE table ADD INDEX my_index GLOBAL ASYNC ON (c2) COVER (c3);

Usage

sa.select(
    sa.column(table.c.c1)
)
.select_from(table)
.where(sa.column(table.c.c2) == 1)
.with_hint(table, "VIEW `my_index`")

is rendered as

SELECT c1
FROM table VIEW `my_index`
WHERE c2 == 1