Skip to content

Commit

Permalink
Merge branch 'master' into fix/relax-packaging-dep
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar authored Jan 26, 2024
2 parents 435b68c + 0bce440 commit 7448c90
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Change log

### 0.2.10

- 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import find_packages, setup

VERSION = "0.2.9"
VERSION = "0.2.10"
BASE_DIR = os.path.abspath(os.path.dirname(__file__))

with io.open("README.md", "r", encoding="utf-8") as f:
Expand Down

0 comments on commit 7448c90

Please sign in to comment.