Skip to content

Commit cf76178

Browse files
feat:distinct order by (#11)
1 parent 8caa738 commit cf76178

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

__tests/testing_web/test_data_column.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def test_distinct_from_data_view(context: Context, memory_db: MemoryDb):
1212
@context.register_page
1313
def index():
1414
dv1 = dataset["df"]
15-
display.list_box(dv1["Name"].distinct())
15+
display.list_box(dv1["Name"].distinct(order_by="ASC"))
1616

1717
context.open()
18-
ListBox(context).should_have_text(["foo", "bar"])
18+
ListBox(context).should_have_text(["bar", "foo"])
1919

2020

2121
def test_distinct_from_query(context: Context, memory_db: MemoryDb):
@@ -26,10 +26,10 @@ def test_distinct_from_query(context: Context, memory_db: MemoryDb):
2626
def index():
2727
dv1 = dataset["df"]
2828
query = pybi.query(f"SELECT Name FROM {dv1}")
29-
display.list_box(query["Name"].distinct())
29+
display.list_box(query["Name"].distinct(order_by="ASC"))
3030

3131
context.open()
32-
ListBox(context).should_have_text(["foo", "bar"])
32+
ListBox(context).should_have_text(["bar", "foo"])
3333

3434

3535
def test_flat_values_from_data_view(context: Context, memory_db: MemoryDb):

src/pybi/link_sql/_mixin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Any,
66
Dict,
77
List,
8+
Literal,
89
Optional,
910
Union,
1011
TypedDict,
@@ -105,7 +106,7 @@ def get_source_type(self) -> _const.TSourceType:
105106
pass
106107

107108
@abstractmethod
108-
def distinct(self) -> List:
109+
def distinct(self, *, order_by: Optional[Literal["ASC", "DESC"]] = None) -> List:
109110
pass
110111

111112
@abstractmethod

src/pybi/link_sql/data_column.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Dict
2+
from typing import Dict, Literal, Optional
33
from instaui.vars.mixin_types.element_binding import ElementBindingMixin
44
from instaui.vars.mixin_types.observable import ObservableMixin
55

@@ -40,10 +40,9 @@ def flat_values(
4040
):
4141
return self.__source.flat_values()
4242

43-
def distinct(self):
44-
query_name = get_store().gen_query(
45-
f"SELECT DISTINCT {self.field} FROM {self.source_name}"
46-
)
43+
def distinct(self, *, order_by: Optional[Literal["ASC", "DESC"]] = None):
44+
sql = f"SELECT DISTINCT {self.field} FROM {self.source_name}{f' ORDER BY {self.field} {order_by}' if order_by else ''}"
45+
query_name = get_store().gen_query(sql)
4746
return _server_query.create_source(query_name).flat_values()
4847

4948

@@ -80,8 +79,7 @@ def source_name(self) -> str:
8079
def get_source_type(self):
8180
return "query"
8281

83-
def distinct(self):
84-
query_name = get_store().gen_query(
85-
f"SELECT DISTINCT {self.field} FROM {self.source_name}"
86-
)
82+
def distinct(self, *, order_by: Optional[Literal["ASC", "DESC"]] = None):
83+
sql = f"SELECT DISTINCT {self.field} FROM {self.source_name}{f' ORDER BY {self.field} {order_by}' if order_by else ''}"
84+
query_name = get_store().gen_query(sql)
8785
return _server_query.create_source(query_name).flat_values()

0 commit comments

Comments
 (0)