Skip to content

Commit

Permalink
DLHELP-10200 Use upper median in case of even number of values (#466)
Browse files Browse the repository at this point in the history
* DLHELP-10200 Use upper median in case of even number of values

* mypy
  • Loading branch information
KonstantAnxiety authored May 30, 2024
1 parent 7f871d3 commit 60a8c27
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# median
base.AggMedian(
variants=[
V(D.ORACLE, lambda expr: within_group(sa.func.PERCENTILE_DISC(0.5), expr)),
V(D.ORACLE, lambda expr: within_group(sa.func.PERCENTILE_DISC(0.5), expr.desc())),
]
),
# min
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _all_concat_pg(expr: ClauseElement, sep: str = ", ") -> ClauseElement:
variants=[
V(
D.and_above(D.POSTGRESQL_9_4) | D.COMPENG,
lambda expr: (within_group(sa.func.PERCENTILE_DISC(0.5), expr)),
lambda expr: (within_group(sa.func.PERCENTILE_DISC(0.5), expr.desc())),
),
]
),
Expand All @@ -82,7 +82,6 @@ def _all_concat_pg(expr: ClauseElement, sep: str = ", ") -> ClauseElement:
V(
D.and_above(D.POSTGRESQL_9_4) | D.COMPENG,
lambda expr, quant: (
# In MSSQLSRV window functions cannot be used as aggregations
within_group(
sa.func.percentile_disc(quantile_value(un_literal(quant))),
expr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AggMedianIntSF(base.AggMedian):
V(
D.SNOWFLAKE,
lambda expr: sa.func.TO_NUMBER(
within_group(sa.func.PERCENTILE_DISC(0.5), expr),
within_group(sa.func.PERCENTILE_DISC(0.5), expr.desc()),
),
),
]
Expand All @@ -37,7 +37,7 @@ class AggMedianFloatSF(base.AggMedian):
variants = [
V(
D.SNOWFLAKE,
lambda expr: within_group(sa.func.PERCENTILE_DISC(0.5), expr),
lambda expr: within_group(sa.func.PERCENTILE_DISC(0.5), expr.desc()),
),
]

Expand All @@ -52,7 +52,7 @@ class AggMedianDateSF(base.AggMedian):
sa.func.TO_TIMESTAMP(
within_group(
sa.func.PERCENTILE_DISC(0.5),
sa.func.DATE_PART("EPOCH_SECOND", expr),
sa.func.DATE_PART("EPOCH_SECOND", expr.desc()),
)
)
),
Expand All @@ -69,7 +69,7 @@ class AggMedianDateTimeSF(base.AggMedian):
lambda expr: sa.func.TO_TIMESTAMP(
within_group(
sa.func.PERCENTILE_DISC(0.5),
sa.func.DATE_PART("EPOCH_SECOND", expr),
sa.func.DATE_PART("EPOCH_SECOND", expr.desc()),
)
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,20 @@ def test_median(self, dbe: DbEvaluator, data_table: sa.Table) -> None:
if not self.supports_median:
pytest.skip()

int_values = data_table.int_values # type: ignore # 2024-05-29 # TODO: "Table" has no attribute "int_values" [attr-defined]
date_values = data_table.date_values # type: ignore # 2024-01-29 # TODO: "Table" has no attribute "date_values" [attr-defined]
datetime_values = data_table.datetime_values # type: ignore # 2024-01-29 # TODO: "Table" has no attribute "datetime_values" [attr-defined]

VALUE_TV = TypeVar("VALUE_TV")

def medians(values: Collection[Union[VALUE_TV]]) -> tuple[VALUE_TV, VALUE_TV]:
def median(values: Collection[Union[VALUE_TV]]) -> VALUE_TV:
values = sorted(values) # type: ignore # 2024-01-30 # TODO: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "VALUE_TV" [type-var]
middle = len(values) // 2 - 1
return (values[middle], values[middle + 1])
upper_middle = len(values) // 2 + 1
return values[upper_middle]

assert dbe.eval("MEDIAN([int_value])", from_=data_table) in (40, 50)
assert dbe.eval("MEDIAN([date_value])", from_=data_table) in medians(date_values)
assert dbe.eval("MEDIAN([datetime_value])", from_=data_table) in medians(datetime_values)
assert dbe.eval("MEDIAN([int_value])", from_=data_table) == median(int_values)
assert dbe.eval("MEDIAN([date_value])", from_=data_table) == median(date_values)
assert dbe.eval("MEDIAN([datetime_value])", from_=data_table) == median(datetime_values)

def test_any(self, dbe: DbEvaluator, data_table: sa.Table) -> None:
if not self.supports_any:
Expand Down

0 comments on commit 60a8c27

Please sign in to comment.