Skip to content

FEAT: dj.Top orders the preview with order_by #1242

Open
@CBroz1

Description

@CBroz1

Feature Request

Problem

I'm using dj.Top to monitor the top results of a given computation. Intuitively, running MyTable & dj.Top(order_by='field DESC', limit=n) should show the top n values of this field, in this order. Instead, I need to scan the result ordered by primary key, or fetch it.

Requirements

The script below outlines expected vs actual outcome.

Demo
import datajoint as dj

schema = dj.schema("cbroz_temp")


@schema
class TopDemo(dj.Lookup):
    definition = """
    id : int
    ---
    my_var: varchar(64)
    my_float: float
    """

    contents = [(1, "C", 2.0), (2, "D", 4.0), (3, "A", 1.0), (4, "B", 3.0)]


if __name__ == "__main__":
    print("TopDemo contents:\n", TopDemo())
    print(
        "Order by my_float ascending:\n",
        "Expected IDs 3, 4, 1. Got IDs: 1, 3, 4\n",
        TopDemo & dj.Top(order_by="my_float ASC", limit=3),
    )
    print(
        "Order by my_var descending:\n",
        "Expected IDs 2, 1, 4. Got IDs: 1, 2, 4\n",
        TopDemo & dj.Top(order_by="my_var DESC", limit=3),
    )

Justification

This would cut down on redundant use of order_by

Alternative Considerations

Currently, I pass the same order_by arg to a fetch with format='frame'

(MyTable & dj.Top(order_by="field DESC", limit=3)).fetch(order_by"field DESC", format=Frame)

Related Errors

n/a

Please include steps to reproduce provided errors as follows:

  • OS: Linux
  • Python Version: 3.9.6
  • MySQL Version: 8 latest
  • MySQL Deployment Strategy: local docker
  • DataJoint Version: 0.14.3
  • Minimum number of steps to reliably reproduce the issue: see above
  • Complete error stack as a result of evaluating the above steps: n/a

Screenshots

n/a

Additional Research and Context

preview is already running the alternative step of fetching as a frame

def preview(query_expression, limit, width):
heading = query_expression.heading
rel = query_expression.proj(*heading.non_blobs)
if limit is None:
limit = config["display.limit"]
if width is None:
width = config["display.width"]
tuples = rel.fetch(limit=limit + 1, format="array")

If query expressions were assigned an order_by attr by Top, QueryExpression.preview could pass this as a kwarg to preview.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIndicates new improvementstriageIndicates issues, pull requests, or discussions need to be reviewed for the first time

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions