cubes icon indicating copy to clipboard operation
cubes copied to clipboard

Natural ordering isn't working

Open anton-kaidalov opened this issue 6 years ago • 1 comments

Hi guys,

I'm only starting digging into Cubes, so please pardon if I misunderstand something. I really couldn't find any existing explanation to what I'm experiencing, and the code really seems to be somewhat wrong, so I've decided to create this issue.

In my experience, when I'm querying aggregates without explicit ordering, the order_query method in utils.py never applies the natural order, although it has it.

I performed some debugging and found out that:

  • natural_order comes to order_query from Drilldown.natural_order;
  • order_query expects natural_order to be the dictionary of names->directions;
  • Drilldown.natural_order returns a list of tuples instead;
  • this causes if name in natural_order to be syntactically correct but always return false, so ORDER BY clause is not generated.

Another surprising thing is the next condition in the same if: name not in order_by. What is order_by? I don't even see it declared anywhere. Looks like without those types problems with natural_order it would actually get to the second condition and crash completely.

Sorry again if I'm misunderstanding some logic and/or design intentions. I'm really looking forward to your comments.

And some details about my environment, although I don't really think they matter.

My database is PostgreSQL.

I use Cubes version 1.1, installed with pipenv.

My model:

{
    "dimensions": [
        {
            "name": "ts",
            "role": "time",
            "levels": [
                {
                    "name": "year",
                    "label": "Y"
                },
                {
                    "name": "month",
                    "label": "M"
                },
                {
                    "name": "day",
                    "label": "D"
                }
            ]
        },
        {"name": "cat1"},
        {"name": "currency"}
    ],
    "cubes": [
        {
            "name": "transactions",
            "dimensions": ["ts", "cat1", "currency"],
            "measures": [
                {"name": "amount"}
            ],
            "aggregates": [
                {
                    "name": "amount_sum",
                    "measure": "amount",
                    "function": "sum"
                }
            ],
            "mappings": {
              "ts.year": {"column":"ts", "extract":"year"},
              "ts.month": {"column":"ts", "extract":"month"},
              "ts.day": {"column":"ts", "extract":"day"}
            }
        }
    ]
}

anton-kaidalov avatar Mar 10 '19 01:03 anton-kaidalov

Hi @anton-kaidalov, yeah there was already an issue on natural ordering. That's some great debugging info, thank you very much. Will have a look at this.

KhaledTo avatar May 11 '19 22:05 KhaledTo