Skip to content

Commit 27f82b6

Browse files
Make index hinting work with CockroachDB (#178)
This includes support for asyncpg and testing. Signed-off-by: Jonathan Dieter <[email protected]> Co-authored-by: Gord Thompson <[email protected]>
1 parent 7af4073 commit 27f82b6

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Unreleased
33

44
- Added `include_hidden` option to `get_columns()` to enable reflection of columns like "rowid". (#173)
5+
- Added support for `.with_hint()` (patch courtesy of Jonathan Dieter)
56

67
# Version 1.4.3
7-
Released Devember 10, 2021
8+
Released December 10, 2021
89

910
- Added preliminary support for asyncpg. See instructions in the README.
1011

sqlalchemy_cockroachdb/asyncpg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from sqlalchemy.dialects.postgresql.asyncpg import PGDialect_asyncpg
22
from .base import CockroachDBDialect
33
from .ddl_compiler import CockroachDDLCompiler
4+
from .stmt_compiler import CockroachCompiler
45
from .stmt_compiler import CockroachIdentifierPreparer
56

67

78
class CockroachDBDialect_asyncpg(PGDialect_asyncpg, CockroachDBDialect):
89
driver = "asyncpg" # driver name
910
preparer = CockroachIdentifierPreparer
1011
ddl_compiler = CockroachDDLCompiler
12+
statement_compiler = CockroachCompiler
1113

1214
supports_statement_cache = True
1315

sqlalchemy_cockroachdb/psycopg2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
22
from .base import CockroachDBDialect
33
from .ddl_compiler import CockroachDDLCompiler
4+
from .stmt_compiler import CockroachCompiler
45
from .stmt_compiler import CockroachIdentifierPreparer
56

67

78
class CockroachDBDialect_psycopg2(PGDialect_psycopg2, CockroachDBDialect):
89
driver = "psycopg2" # driver name
910
preparer = CockroachIdentifierPreparer
1011
ddl_compiler = CockroachDDLCompiler
12+
statement_compiler = CockroachCompiler
1113

1214
supports_statement_cache = True

sqlalchemy_cockroachdb/stmt_compiler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from sqlalchemy.dialects.postgresql.base import PGCompiler
12
from sqlalchemy.dialects.postgresql.base import PGIdentifierPreparer
2-
from sqlalchemy.dialects.postgresql.psycopg2 import PGCompiler_psycopg2
33

44
# This is extracted from CockroachDB's `sql.y`. Add keywords here if *NEW* reserved keywords
55
# are added to sql.y. DO NOT DELETE keywords here, even if they are deleted from sql.y:
@@ -97,5 +97,6 @@ class CockroachIdentifierPreparer(PGIdentifierPreparer):
9797
reserved_words = CRDB_RESERVED_WORDS
9898

9999

100-
class CockroachCompiler(PGCompiler_psycopg2):
101-
pass
100+
class CockroachCompiler(PGCompiler):
101+
def format_from_hint_text(self, sqltext, table, hint, iscrud):
102+
return f"{sqltext}@{hint}"

test/test_with_hint.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from sqlalchemy import Column
2+
from sqlalchemy import Index
3+
from sqlalchemy import Integer
4+
from sqlalchemy import select
5+
from sqlalchemy import String
6+
from sqlalchemy import Table
7+
from sqlalchemy.testing import AssertsCompiledSQL
8+
from sqlalchemy.testing import fixtures
9+
from sqlalchemy.testing import provide_metadata
10+
11+
12+
class WithHintTest(fixtures.TestBase, AssertsCompiledSQL):
13+
# __requires__ = ("sync_driver",)
14+
15+
@provide_metadata
16+
def test_with_hint(self):
17+
meta = self.metadata
18+
t = Table(
19+
"t",
20+
meta,
21+
Column("id", Integer),
22+
Column("txt", String(50)),
23+
Index("ix_t_txt", "txt"),
24+
)
25+
self.assert_compile(
26+
select(t).with_hint(t, "ix_t_txt"),
27+
"SELECT t.id, t.txt FROM t@ix_t_txt",
28+
)
29+
self.assert_compile(
30+
select(t).with_hint(t, "ix_t_txt").where(t.c.id < 3),
31+
"SELECT t.id, t.txt FROM t@ix_t_txt WHERE t.id < %(id_1)s",
32+
)

0 commit comments

Comments
 (0)