-
-
Notifications
You must be signed in to change notification settings - Fork 702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
?sort=colname~numeric to sort by by column cast to real #894
Comments
This needs querystring parameter design. Some options:
|
I could even let plugins define new sort types. Imagine a plugin that enables this:
|
Sorting by date when the column has a junk date format in it is such a column need it should maybe ship in Datasette by default - though I've been trying to avoid adding heavy dependencies like |
This does feel like a weird plugin hook just because there aren't really THAT many different use-cases that plugins could solve. The ones I can think of are:
Could this work if I just allow One possible solution for the no-arbitrary-SQL case: users can define sort orders in databases:
mydb:
tables:
museums:
sort_clause: bm25(fts) |
There's something interesting about figuring out which sort options should be offered in the column actions menu. Two options:
|
The simplest solution would be for Python code to scan all of the visible values on the current page and show the column action for "sort by this numeric" based purely on that. I already do that in the JavaScript for "are there any blank values in the first page?" here: datasette/datasette/static/table.js Lines 106 to 118 in 4f7c0eb
|
I think the first version of this feature involves implementing One question: how to reflect that this is happening in the current sort UI. This menu here for example: And this interface: how should it indicate that a text is currently sorted numerically v.s. sorted alphabetically, and allow the user to switch from one to the other? |
For the "Sort by X" select menu case... I could automatically expand that menu to contain extra options for "Sort numerically by X" for each TEXT column in the table. That's a pretty good option. For the action cog menu, I can add the extra options to the cog menu - and rely on the fact that the title of the page will say "Sorted numerically by colname descending". |
This is enough of a design to build a working prototype. |
The Sort by datasette/datasette/templates/table.html Lines 80 to 87 in 4f7c0eb
If it's going to include sort by numeric options it needs a different format - since Can I come up with a value syntax for this that is guaranteed not to clash with a weirdly named existing column? I think so. I could use |
Simpler option:
|
Even better solution: use URL encoding in the parameter details. This is consistent with how So the format can be:
For most columns this will look like: For columns with a Problem: both
The other unreserved non-alphanumeric characters are Of these,
|
Urgh this isn't going to work. I could use double-percentage-encoding here instead. I feel like there's a simpler solution that I'm missing (and that may well be in use within Datasette already, I'm not doing great thinking this morning). |
Much easier solution: if the suffix is Using |
Relevant code: datasette/datasette/views/table.py Lines 485 to 510 in 4f7c0eb
|
Something to watch out for:
|
|
Also need to rethink this template logic that decides if to show a column as sorted or not: datasette/datasette/templates/_table.html Lines 10 to 14 in 4f7c0eb
|
Prototype so far: diff --git a/datasette/views/table.py b/datasette/views/table.py
index ea11a51..d61f8bd 100644
--- a/datasette/views/table.py
+++ b/datasette/views/table.py
@@ -497,17 +497,32 @@ class TableView(RowTableShared):
if sort and sort_desc:
raise DatasetteError("Cannot use _sort and _sort_desc at the same time")
+ def parse_sort(sort):
+ if "~" in sort:
+ if sort.endswith("~default"):
+ col = sort.rsplit("~", 1)[0]
+ return col, escape_sqlite(col)
+ elif sort.endswith("~numeric"):
+ col = sort.rsplit("~", 1)[0]
+ return col, "cast(nullif({}, '') as real)".format(escape_sqlite(col))
+ else:
+ return sort, escape_sqlite(sort)
+ else:
+ return sort, escape_sqlite(sort)
+
if sort:
- if sort not in sortable_columns:
- raise DatasetteError("Cannot sort table by {}".format(sort))
+ sort_column, sort_clause = parse_sort(sort)
+ if sort_column not in sortable_columns:
+ raise DatasetteError("Cannot sort table by {}".format(sort_column))
- order_by = escape_sqlite(sort)
+ order_by = sort_clause
if sort_desc:
- if sort_desc not in sortable_columns:
- raise DatasetteError("Cannot sort table by {}".format(sort_desc))
+ sort_column, sort_clause = parse_sort(sort_desc)
+ if sort_column not in sortable_columns:
+ raise DatasetteError("Cannot sort table by {}".format(sort_column))
- order_by = "{} desc".format(escape_sqlite(sort_desc))
+ order_by = "{} desc".format(sort_clause)
from_sql = "from {table_name} {where}".format(
table_name=escape_sqlite(table), |
Maybe |
I could add these sorting links to the cog menu for any |
If a text column actually contains numbers, being able to "sort by column, treated as numeric" would be really useful.
Probably depends on column actions enabled by #690
The text was updated successfully, but these errors were encountered: