Skip to content
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

Sorting by map-like values #1064

Open
MMcM opened this issue Oct 29, 2020 · 0 comments
Open

Sorting by map-like values #1064

MMcM opened this issue Oct 29, 2020 · 0 comments
Labels
planner Related to the query planner

Comments

@MMcM
Copy link
Contributor

MMcM commented Oct 29, 2020

message Item {
  optional int64 rec_no = 1 [(field).primary_key = true];
  optional string color = 2;
  message PriceMap {
    message Entry {
      optional string key = 1;
      optional int32 value = 2;
    }
    repeated Entry map_value = 1;
  }
  optional PriceMap prices = 3;
}

...
        final RecordMetaDataHook hook = md -> {
            md.addIndex("Item", "color_price",  Key.Expressions.concat(
                    Key.Expressions.field("color"),
                    Key.Expressions.field("prices").nest(Key.Expressions.mapKeyValues("map_value"))));
        };
...
        RecordQuery query = RecordQuery.newBuilder()
                .setRecordType("Item")
                .setFilter(Query.and(
                        Query.field("color").equalsValue("red"),
                        Query.field("prices").matches(Query.field("map_value")
                                .mapMatches(k -> k.equalsValue("large"), null))))
                .setSort(Key.Expressions.field("prices").nest(Key.Expressions.mapValues("map_value")))
                .build();

The query is not very robustly expressed, but the idea is something like

SELECT * FROM Item WHERE color = 'red' ORDER BY prices['large']

The immediate problem is that RecordQueryPlanner.AndWithThenPlanner.planNestedFieldChild is not able to advanceCurrentSort when it is only a part of a nested concat that just passed the conditional check.

But, in general, all of currentSortMatches has problems with multi-column keys.

For instance, adding another condition

                                .mapMatches(k -> k.equalsValue("large"), v -> v.greaterThan(100)))))

further restricts where the sort must match because it is an inequality.

In this more complex case, RecordQueryPlanner.AndWithThenPlanner.plan is called recursively due to the nested conjunction. The inner one does advance the sort, but fails to successfully convey this to the outer one.

@MMcM MMcM added the planner Related to the query planner label Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
planner Related to the query planner
Projects
None yet
Development

No branches or pull requests

1 participant