Skip to content

Commit 83fe8e5

Browse files
committed
Add index hints
1 parent 3308020 commit 83fe8e5

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

test/test_core.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,88 @@ def test_indexes_reflection(self, connection: sa.Connection, metadata: sa.MetaDa
823823
"test_cover_index": {"index_col1"},
824824
"test_async_cover_index": {"index_col1"},
825825
}
826+
827+
def test_index_simple_usage(self, connection: sa.Connection, metadata: sa.MetaData):
828+
persons = Table(
829+
"test_index_simple_usage/persons",
830+
metadata,
831+
sa.Column("id", sa.Integer(), primary_key=True),
832+
sa.Column("tax_number", sa.Integer()),
833+
sa.Column("full_name", sa.Unicode()),
834+
sa.Index("ix_tax_number_cover_full_name", "tax_number", ydb_cover=["full_name"]),
835+
)
836+
persons.create(connection)
837+
connection.execute(
838+
sa.insert(persons).values(
839+
[
840+
{"id": 1, "tax_number": 333333, "full_name": "John Connor"},
841+
{"id": 2, "tax_number": 444444, "full_name": "Sarah Connor"},
842+
]
843+
)
844+
)
845+
846+
# Because of this bug https://github.com/ydb-platform/ydb/issues/3510,
847+
# it is not possible to use full qualified name of columns with VIEW clause
848+
select_stmt = (
849+
sa.select(sa.column(persons.c.full_name.name))
850+
.select_from(persons)
851+
.with_hint(persons, "VIEW `ix_tax_number_cover_full_name`")
852+
.where(sa.column(persons.c.tax_number.name) == 444444)
853+
)
854+
855+
cursor = connection.execute(select_stmt)
856+
assert cursor.scalar_one() == "Sarah Connor"
857+
858+
def test_index_with_join_usage(self, connection: sa.Connection, metadata: sa.MetaData):
859+
persons = Table(
860+
"test_index_with_join_usage/persons",
861+
metadata,
862+
sa.Column("id", sa.Integer(), primary_key=True),
863+
sa.Column("tax_number", sa.Integer()),
864+
sa.Column("full_name", sa.Unicode()),
865+
sa.Index("ix_tax_number_cover_full_name", "tax_number", ydb_cover=["full_name"]),
866+
)
867+
persons.create(connection)
868+
connection.execute(
869+
sa.insert(persons).values(
870+
[
871+
{"id": 1, "tax_number": 333333, "full_name": "John Connor"},
872+
{"id": 2, "tax_number": 444444, "full_name": "Sarah Connor"},
873+
]
874+
)
875+
)
876+
person_status = Table(
877+
"test_index_with_join_usage/person_status",
878+
metadata,
879+
sa.Column("id", sa.Integer(), primary_key=True),
880+
sa.Column("status", sa.Unicode()),
881+
)
882+
person_status.create(connection)
883+
connection.execute(
884+
sa.insert(person_status).values(
885+
[
886+
{"id": 1, "status": "unknown"},
887+
{"id": 2, "status": "wanted"},
888+
]
889+
)
890+
)
891+
892+
# Because of this bug https://github.com/ydb-platform/ydb/issues/3510,
893+
# it is not possible to use full qualified name of columns with VIEW clause
894+
persons_indexed = (
895+
sa.select(
896+
sa.column(persons.c.id.name),
897+
sa.column(persons.c.full_name.name),
898+
sa.column(persons.c.tax_number.name),
899+
)
900+
.select_from(persons)
901+
.with_hint(persons, "VIEW `ix_tax_number_cover_full_name`")
902+
)
903+
select_stmt = (
904+
sa.select(persons_indexed.c.full_name, person_status.c.status)
905+
.select_from(person_status.join(persons_indexed, persons_indexed.c.id == person_status.c.id))
906+
.where(persons_indexed.c.tax_number == 444444)
907+
)
908+
909+
cursor = connection.execute(select_stmt)
910+
assert cursor.one() == ("Sarah Connor", "wanted")

ydb_sqlalchemy/sqlalchemy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ def __init__(self, name, params, *args, **kwargs):
235235
class YqlCompiler(StrSQLCompiler):
236236
compound_keywords = COMPOUND_KEYWORDS
237237

238+
def get_from_hint_text(self, table, text):
239+
return text
240+
238241
def render_bind_cast(self, type_, dbapi_type, sqltext):
239242
pass
240243

0 commit comments

Comments
 (0)