Skip to content

Commit 590eded

Browse files
committed
fix(opendistro): quote with backtick
1 parent 812a6bf commit 590eded

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change log
22

3+
### Next
4+
5+
- fix: OpenDistro dialect quotes properly with backticks now (#99) [Beto Dealmeida]
6+
37
### 0.2.9
48

59
- fix: remove six dependency (#84) [Daniel Vaz Gaspar]

es/opendistro/sqlalchemy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from es import basesqlalchemy
66
import es.opendistro
77
from sqlalchemy.engine import Connection
8+
from sqlalchemy.sql import compiler
89

910
logger = logging.getLogger(__name__)
1011

@@ -17,13 +18,21 @@ class ESTypeCompiler(basesqlalchemy.BaseESTypeCompiler): # pragma: no cover
1718
pass
1819

1920

21+
class ESTypeIdentifierPreparer(compiler.IdentifierPreparer):
22+
def __init__(self, *args: Any, **kwargs: Any):
23+
super().__init__(*args, **kwargs)
24+
25+
self.initial_quote = self.final_quote = "`"
26+
27+
2028
class ESDialect(basesqlalchemy.BaseESDialect):
2129

2230
name = "odelasticsearch"
2331
scheme = "http"
2432
driver = "rest"
2533
statement_compiler = ESCompiler
2634
type_compiler = ESTypeCompiler
35+
preparer = ESTypeIdentifierPreparer
2736

2837
@classmethod
2938
def dbapi(cls) -> ModuleType:

es/tests/test_sqlalchemy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import unittest
44
from unittest.mock import Mock, patch
55

6+
from es.elastic.sqlalchemy import ESDialect as ElasticDialect
67
from es.exceptions import DatabaseError
8+
from es.opendistro.sqlalchemy import ESDialect as OpenDistroDialect
79
from es.tests.fixtures.fixtures import data1_columns, flights_columns
810
from sqlalchemy import func, inspect, select
911
from sqlalchemy.engine import create_engine
@@ -325,3 +327,21 @@ def test_opendistro_ping_failed(self):
325327
conn = self.engine.raw_connection()
326328
with self.assertRaises(DatabaseError):
327329
self.engine.dialect.do_ping(conn)
330+
331+
332+
class TestQuote(unittest.TestCase):
333+
"""
334+
Test quoting identifiers in ES and OD.
335+
"""
336+
337+
def test_elastic(self) -> None:
338+
assert (
339+
ElasticDialect.preparer(dialect=ElasticDialect()).quote("DATE(123)")
340+
== '"DATE(123)"'
341+
)
342+
343+
def test_opendistro(self) -> None:
344+
assert (
345+
OpenDistroDialect.preparer(dialect=OpenDistroDialect()).quote("DATE(123)")
346+
== "`DATE(123)`"
347+
)

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ readme_renderer==24.0
1111
mypy==0.790
1212
requests-aws4auth==1.0.1
1313
boto3==1.16.63
14+
pytest==7.2.1

0 commit comments

Comments
 (0)