File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ import logging
2+ from typing import Optional , Iterable
3+
4+ from countess import VERSION
5+ from countess .core .parameters import (
6+ ArrayParam ,
7+ ColumnChoiceParam ,
8+ NumericColumnChoiceParam ,
9+ )
10+ from countess .core .plugins import DuckdbSqlPlugin
11+ from countess .utils .duckdb import duckdb_escape_identifier
12+
13+ logger = logging .getLogger (__name__ )
14+
15+
16+ class RankingPlugin (DuckdbSqlPlugin ):
17+ name = "Ranking"
18+ description = "Rank rows by column value(s)"
19+ version = VERSION
20+
21+ order_by = ArrayParam ("Order By" , NumericColumnChoiceParam ("Column" ))
22+ partition = ArrayParam ("Partition By" , ColumnChoiceParam ("Column" ))
23+
24+ def sql (self , table_name : str , columns : Iterable [str ]) -> Optional [str ]:
25+
26+ order_by = ', ' .join (
27+ duckdb_escape_identifier (p .value )
28+ for p in self .order_by .params
29+ )
30+ if order_by :
31+ order_by = f"ORDER BY { order_by } "
32+
33+ partition = ', ' .join (
34+ duckdb_escape_identifier (p .value )
35+ for p in self .partition .params
36+ )
37+ if partition :
38+ partition = f"PARTITION BY { partition } "
39+
40+ return f'SELECT *, percent_rank({ order_by } ) over ({ partition } ) as rank FROM { table_name } '
Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ variant_classifier = "countess.plugins.variant:VariantClassifier"
7575variant_converter = " countess.plugins.variant:VariantConverter"
7676frequency = " countess.plugins.frequency:FrequencyPlugin"
7777columns = " countess.plugins.columns:ColumnsPlugin"
78+ ranking = " countess.plugins.ranking:RankingPlugin"
7879
7980[project .entry-points .gui_scripts ]
8081countess_gui = " countess.gui.main:main"
You can’t perform that action at this time.
0 commit comments