Skip to content

Commit c6537d8

Browse files
unit tests
1 parent 7f6dc00 commit c6537d8

File tree

4 files changed

+704
-651
lines changed

4 files changed

+704
-651
lines changed

elasticsearch/dsl/document_base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ def __mod__(self, value: Any) -> "InstrumentedExpression":
123123
def __rmod__(self, value: Any) -> "InstrumentedExpression":
124124
return InstrumentedExpression(f"{json.dumps(value)} % {self._expr}")
125125

126+
def where(self, expr: "InstrumentedExpression") -> "InstrumentedExpression":
127+
"""Add a condition to be met for the row to be included.
128+
129+
Use only in expressions given in the ``STATS`` command.
130+
"""
131+
return InstrumentedExpression(f"{self._expr} WHERE {json.dumps(expr)}")
132+
126133

127134
class InstrumentedField(InstrumentedExpression):
128135
"""Proxy object for a mapped document field.
@@ -170,15 +177,31 @@ def __neg__(self) -> str: # type: ignore[override]
170177
return f"-{self._expr}"
171178

172179
def asc(self) -> "InstrumentedField":
180+
"""Return the field name representation for ascending sort order.
181+
182+
For use in ES|QL queries only.
183+
"""
173184
return InstrumentedField(f"{self._expr} ASC", None)
174185

175186
def desc(self) -> "InstrumentedField":
187+
"""Return the field name representation for descending sort order.
188+
189+
For use in ES|QL queries only.
190+
"""
176191
return InstrumentedField(f"{self._expr} DESC", None)
177192

178193
def nulls_first(self) -> "InstrumentedField":
194+
"""Return the field name representation for nulls first sort order.
195+
196+
For use in ES|QL queries only.
197+
"""
179198
return InstrumentedField(f"{self._expr} NULLS FIRST", None)
180199

181200
def nulls_last(self) -> "InstrumentedField":
201+
"""Return the field name representation for nulls last sort order.
202+
203+
For use in ES|QL queries only.
204+
"""
182205
return InstrumentedField(f"{self._expr} NULLS LAST", None)
183206

184207
def __str__(self) -> str:

elasticsearch/esql/esql.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import json
1919
from abc import ABC, abstractmethod
20-
from typing import Any, Dict, Optional, Tuple, Type
20+
from typing import Any, Dict, Optional, Tuple, Type, Union
2121

22-
from ..dsl.document_base import DocumentBase, InstrumentedField
22+
from ..dsl.document_base import DocumentBase, InstrumentedExpression, InstrumentedField
2323

24-
FieldType = InstrumentedField | str
25-
IndexType = Type[DocumentBase] | str
24+
FieldType = Union[InstrumentedField, str]
25+
IndexType = Union[Type[DocumentBase], str]
2626
ExpressionType = Any
2727

2828

@@ -537,12 +537,13 @@ class Row(ESQLBase):
537537

538538
def __init__(self, **params: ExpressionType):
539539
super().__init__()
540-
self._params = params
540+
self._params = {
541+
k: json.dumps(v) if not isinstance(v, InstrumentedExpression) else v
542+
for k, v in params.items()
543+
}
541544

542545
def _render_internal(self) -> str:
543-
return "ROW " + ", ".join(
544-
[f"{k} = {json.dumps(v)}" for k, v in self._params.items()]
545-
)
546+
return "ROW " + ", ".join([f"{k} = {v}" for k, v in self._params.items()])
546547

547548

548549
class Show(ESQLBase):

0 commit comments

Comments
 (0)