Skip to content

Support vector index for Peewee #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 11, 2024

Conversation

breezewish
Copy link
Member

@breezewish breezewish commented Nov 7, 2024

A VectorAdaptor is introduced similar to how we did for SQLAlchemy.

It is possible to utilize Peewee's native index, when we only need one SQL statement to add index. Currently we must execute two SQL statements, so that an adaptor is needed.

An example of being native Peewee index:

class VectorIndex(peewee.SQL):

    table: peewee.Table
    field: peewee.Field
    distance_metric: tidb_vector.DistanceMetric

    def __init__(
        self,
        table: peewee.Model,
        field: peewee.Field,
        distance_metric: tidb_vector.DistanceMetric,
    ):
        self.table = table
        self.field = field
        self.distance_metric = distance_metric
        super(VectorIndex, self).__init__("")

    def __sql__(self, ctx: peewee.Context):
        ctx.literal("ALTER TABLE ")
        ctx.sql(self.table)
        ctx.literal(" ADD VECTOR INDEX ")
        with ctx(parentheses=True):
            with ctx(parentheses=True):
                ctx.literal(self.distance_metric.to_sql_func())
                with ctx(parentheses=True):
                    ctx.sql(self.field)
        return ctx

Signed-off-by: Wish <breezewish@outlook.com>
Signed-off-by: Wish <breezewish@outlook.com>
Signed-off-by: Wish <breezewish@outlook.com>
Signed-off-by: Wish <breezewish@outlook.com>
Signed-off-by: Wish <breezewish@outlook.com>
Signed-off-by: Wish <breezewish@outlook.com>
@wd0517 wd0517 merged commit d9b8cee into pingcap:main Nov 11, 2024
2 checks passed
@breezewish breezewish deleted the wenxuan/peewee_readme branch November 11, 2024 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants