Skip to content

Commit

Permalink
fix(opendistro): quote with backtick
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Feb 6, 2023
1 parent 812a6bf commit 590eded
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Change log

### Next

- fix: OpenDistro dialect quotes properly with backticks now (#99) [Beto Dealmeida]

### 0.2.9

- fix: remove six dependency (#84) [Daniel Vaz Gaspar]
Expand Down
9 changes: 9 additions & 0 deletions es/opendistro/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from es import basesqlalchemy
import es.opendistro
from sqlalchemy.engine import Connection
from sqlalchemy.sql import compiler

logger = logging.getLogger(__name__)

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


class ESTypeIdentifierPreparer(compiler.IdentifierPreparer):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)

self.initial_quote = self.final_quote = "`"


class ESDialect(basesqlalchemy.BaseESDialect):

name = "odelasticsearch"
scheme = "http"
driver = "rest"
statement_compiler = ESCompiler
type_compiler = ESTypeCompiler
preparer = ESTypeIdentifierPreparer

@classmethod
def dbapi(cls) -> ModuleType:
Expand Down
20 changes: 20 additions & 0 deletions es/tests/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import unittest
from unittest.mock import Mock, patch

from es.elastic.sqlalchemy import ESDialect as ElasticDialect
from es.exceptions import DatabaseError
from es.opendistro.sqlalchemy import ESDialect as OpenDistroDialect
from es.tests.fixtures.fixtures import data1_columns, flights_columns
from sqlalchemy import func, inspect, select
from sqlalchemy.engine import create_engine
Expand Down Expand Up @@ -325,3 +327,21 @@ def test_opendistro_ping_failed(self):
conn = self.engine.raw_connection()
with self.assertRaises(DatabaseError):
self.engine.dialect.do_ping(conn)


class TestQuote(unittest.TestCase):
"""
Test quoting identifiers in ES and OD.
"""

def test_elastic(self) -> None:
assert (
ElasticDialect.preparer(dialect=ElasticDialect()).quote("DATE(123)")
== '"DATE(123)"'
)

def test_opendistro(self) -> None:
assert (
OpenDistroDialect.preparer(dialect=OpenDistroDialect()).quote("DATE(123)")
== "`DATE(123)`"
)
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ readme_renderer==24.0
mypy==0.790
requests-aws4auth==1.0.1
boto3==1.16.63
pytest==7.2.1

0 comments on commit 590eded

Please sign in to comment.