Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-8940

group.sort broken, can through AIOOBE if clause length differs from sort param, or cast exception if datatypes are incompatible with sort clause types

    Details

      Description

      We get an ArrayIndexOutOfBoundsException when searching after upgrading to solr 5.5.

      Here's the query:

      "params":{
            "q":"*:*",
            "group.sort":"priceAmount asc,rnd desc",
            "indent":"on",
            "fl":"priceAmount,flightTripId,brand,slob,cabinType,tripDuration",
            "group.limit":"100",
            "fq":["searchId:e31a0c58-9056-4297-8d70-049017ba4906",
              "doctype:offer",
              "flightTripId:(DY6020421-SK2360519 OR DY6020421-SK2600519 OR DY6020421-SK2620519 OR DY6020421-SK2740519 OR DY6020421-SK2900519 OR DY6020421-SK2860519 OR DY6040421-SK2380519 OR DY6040421-SK2440519 OR DY6040421-SK2480519 OR DY6040421-SK2520519 OR DY6040421-SK2600519 OR DY6040421-SK2620519 OR DY6040421-SK2720519 OR DY6040421-SK2740519 OR DY6040421-SK2800519 OR DY6040421-SK2840519 OR DY6040421-SK2820519 OR DY6060421-SK2480519 OR DY6060421-SK2740519 OR DY6060421-SK2800519 OR DY6060421-SK2840519 OR DY6060421-SK2900519 OR DY6060421-SK2860519 OR DY6060421-SK2820519 OR DY6080421-SK2440519)",
              "maximumLegDuration:[* TO 180]",
              "departureAirportLeg1:(OSL)",
              "(arrivalAirportLeg2:(OSL) OR (* NOT arrivalAirportLeg2:*))",
              "arrivalAirportLeg1:(BGO)",
              "(departureAirportLeg2:(BGO) OR (* NOT departureAirportLeg2:*))"],
            "group.ngroups":"true",
            "wt":"json",
            "group.field":"flightTripId",
            "group":"true"}}
      

      And here's the exception:

      ERROR [20160404T104846,333] qtp315138752-3037 org.apache.solr.servlet.HttpSolrCall - null:java.lang.ArrayIndexOutOfBoundsException: 1
              at org.apache.solr.search.grouping.distributed.shardresultserializer.TopGroupsResultTransformer.transformToNativeShardDoc(TopGroupsResultTransformer.java:175)
              at org.apache.solr.search.grouping.distributed.shardresultserializer.TopGroupsResultTransformer.transformToNative(TopGroupsResultTransformer.java:137)
              at org.apache.solr.search.grouping.distributed.responseprocessor.TopGroupsShardResponseProcessor.process(TopGroupsShardResponseProcessor.java:129)
              at org.apache.solr.handler.component.QueryComponent.handleGroupedResponses(QueryComponent.java:750)
              at org.apache.solr.handler.component.QueryComponent.handleResponses(QueryComponent.java:733)
              at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:405)
              at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:155)
              at org.apache.solr.core.SolrCore.execute(SolrCore.java:2082)
      

      The exception is thrown at the last line here (TopGroupsResultTransformer.java line 175):

        protected ScoreDoc[] transformToNativeShardDoc(List<NamedList<Object>> documents, Sort groupSort, String shard,
                                                       IndexSchema schema) {
          [...]
          for (NamedList<Object> document : documents) {
            [...]
            Object sortValuesVal = document.get("sortValues");
            if (sortValuesVal != null) {
              sortValues = ((List) sortValuesVal).toArray();
              for (int k = 0; k < sortValues.length; k++) {
                SchemaField field = groupSort.getSort()[k].getField() != null
                    ? schema.getFieldOrNull(groupSort.getSort()[k].getField()) : null;
      

      It's not obvious to me that sortValues.length == groupSort.getSort().length, but I guess there's some logic behind it

      I have attached the schema and json result.

      The problem disappears when rolling back to 5.4.0.

      1. 0001-SOLR-8940-Avoid-ArrayIndexOutOfBoundsException-in-To.patch
        2 kB
        Henrik
      2. schema.xml
        5 kB
        Henrik
      3. schema-types.xml
        0.6 kB
        Henrik
      4. SOLR-8940.patch
        4 kB
        Hoss Man
      5. solrconfig.xml
        5 kB
        Henrik
      6. solr-query-exception.txt
        4 kB
        Henrik

        Issue Links

          Activity

          Hide
          stephlag Stephan Lagraulet added a comment -

          Can you remove this issue from version 5.5.1 as this version is packaged without a fix for this issue?

          Show
          stephlag Stephan Lagraulet added a comment - Can you remove this issue from version 5.5.1 as this version is packaged without a fix for this issue?
          Hide
          monti Henrik added a comment -

          Still a problem in 5.5.1

          Show
          monti Henrik added a comment - Still a problem in 5.5.1
          Hide
          monti Henrik added a comment - - edited

          Here's a patch. I don't know if it just hides the original issue, but at least it doesn't make assumptions regarding sortValues.length == groupSort.getSort().length

          Ref. https://issues.apache.org/jira/secure/attachment/12804889/0001-SOLR-8940-Avoid-ArrayIndexOutOfBoundsException-in-To.patch

          Show
          monti Henrik added a comment - - edited Here's a patch. I don't know if it just hides the original issue, but at least it doesn't make assumptions regarding sortValues.length == groupSort.getSort().length Ref. https://issues.apache.org/jira/secure/attachment/12804889/0001-SOLR-8940-Avoid-ArrayIndexOutOfBoundsException-in-To.patch
          Hide
          hossman Hoss Man added a comment -

          Henrik: your patch would definitely just mask the underlying problem.

          The crux of the issue seems to be that the code is convoluting variables/properties of "sorting within groups" and "sorting the groups" – the AIOOBE happens anytime the (effective) value of group.sort (how docs in a group are sorted) has more clauses then the (effective) value of the sort param (how the groups are sorted).

          Henrik: wih your suggested patch, the AIOOBE may goe away, in your sample query but i think that's only because it's ignroning any group.sort clauses after the first one (since hte default effective value of the "sort" param is one clause: "score desc").

          But even when sort & group.sort have the exact same number of clauses, there are still bugs. For example using the techproducs sample data (in a 2 shard cloud collection) this query throws an error about not being able to fast a Float to a String because of how the sort metadata is getting confused between the sort=id vs the group.sort=price...

          http://localhost:8983/solr/techproducts/select?wt=json&indent=true&fl=id,name,price&q=solr+memory&group=true&sort=id+asc&group.field=manu_exact&group.limit=2&group.sort=price+desc

          ...in a single node collection (bin/solr -e techproducts) that query works just fine.

          I suspect this bug was introduced by a mistake in some of the refactoring in LUCENE-6900, but it doesn't immediately jump out at me when skimming the diff ... i'll review more throughly a bit later today.

          Show
          hossman Hoss Man added a comment - Henrik: your patch would definitely just mask the underlying problem. The crux of the issue seems to be that the code is convoluting variables/properties of "sorting within groups" and "sorting the groups" – the AIOOBE happens anytime the (effective) value of group.sort (how docs in a group are sorted) has more clauses then the (effective) value of the sort param (how the groups are sorted). Henrik: wih your suggested patch, the AIOOBE may goe away, in your sample query but i think that's only because it's ignroning any group.sort clauses after the first one (since hte default effective value of the "sort" param is one clause: "score desc"). But even when sort & group.sort have the exact same number of clauses, there are still bugs. For example using the techproducs sample data (in a 2 shard cloud collection) this query throws an error about not being able to fast a Float to a String because of how the sort metadata is getting confused between the sort=id vs the group.sort=price... http://localhost:8983/solr/techproducts/select?wt=json&indent=true&fl=id,name,price&q=solr+memory&group=true&sort=id+asc&group.field=manu_exact&group.limit=2&group.sort=price+desc ...in a single node collection (bin/solr -e techproducts) that query works just fine. I suspect this bug was introduced by a mistake in some of the refactoring in LUCENE-6900 , but it doesn't immediately jump out at me when skimming the diff ... i'll review more throughly a bit later today.
          Hide
          monti Henrik added a comment -

          Thanks for diving into this Hoss Man

          Show
          monti Henrik added a comment - Thanks for diving into this Hoss Man
          Hide
          hossman Hoss Man added a comment -

          I suspect this bug was introduced by a mistake in some of the refactoring in LUCENE-6900, ...

          confirmed. some near identical code was refactored into the (new in 5.5) transformToNativeShardDoc method, but the two places where the code was refactored were updated to include identical calls to this method with identical arguments including groupSort – in one of those cases sortWithinGroup should have been used as the method arg instead.


          Attaching a patch with fix and some new non trivial group.sort tests that demonstrate the bug w/o the fix applied

          Show
          hossman Hoss Man added a comment - I suspect this bug was introduced by a mistake in some of the refactoring in LUCENE-6900 , ... confirmed. some near identical code was refactored into the (new in 5.5) transformToNativeShardDoc method, but the two places where the code was refactored were updated to include identical calls to this method with identical arguments including groupSort – in one of those cases sortWithinGroup should have been used as the method arg instead. Attaching a patch with fix and some new non trivial group.sort tests that demonstrate the bug w/o the fix applied
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 22faa09882f818ce5f91d0230d84f7dc8cc1c084 in lucene-solr's branch refs/heads/branch_6x from Chris Hostetter
          [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=22faa09 ]

          SOLR-8940: Fix group.sort option

          (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)

          Show
          jira-bot ASF subversion and git services added a comment - Commit 22faa09882f818ce5f91d0230d84f7dc8cc1c084 in lucene-solr's branch refs/heads/branch_6x from Chris Hostetter [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=22faa09 ] SOLR-8940 : Fix group.sort option (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 18256fc2873f198e8e577c6eb0f337df1d1cda24 in lucene-solr's branch refs/heads/master from Chris Hostetter
          [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=18256fc ]

          SOLR-8940: Fix group.sort option

          Show
          jira-bot ASF subversion and git services added a comment - Commit 18256fc2873f198e8e577c6eb0f337df1d1cda24 in lucene-solr's branch refs/heads/master from Chris Hostetter [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=18256fc ] SOLR-8940 : Fix group.sort option
          Hide
          hossman Hoss Man added a comment -

          thanks for reporting this Henrik!

          Show
          hossman Hoss Man added a comment - thanks for reporting this Henrik!
          Hide
          steve_rowe Steve Rowe added a comment -

          Reopening to backport for 6.0.2/5.6/5.5.2

          Show
          steve_rowe Steve Rowe added a comment - Reopening to backport for 6.0.2/5.6/5.5.2
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit bdab648a4063bbdeda7877353fa25eb49871dbe9 in lucene-solr's branch refs/heads/branch_5_5 from Chris Hostetter
          [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=bdab648 ]

          SOLR-8940: Fix group.sort option

          (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)

          Show
          jira-bot ASF subversion and git services added a comment - Commit bdab648a4063bbdeda7877353fa25eb49871dbe9 in lucene-solr's branch refs/heads/branch_5_5 from Chris Hostetter [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=bdab648 ] SOLR-8940 : Fix group.sort option (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit d3a9d03c261907e27c5559affbc4a6d2138add65 in lucene-solr's branch refs/heads/branch_5x from Chris Hostetter
          [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=d3a9d03 ]

          SOLR-8940: Fix group.sort option

          (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)

          Show
          jira-bot ASF subversion and git services added a comment - Commit d3a9d03c261907e27c5559affbc4a6d2138add65 in lucene-solr's branch refs/heads/branch_5x from Chris Hostetter [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=d3a9d03 ] SOLR-8940 : Fix group.sort option (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 240140da0fc833a80eab2130ea117ae4f21e77aa in lucene-solr's branch refs/heads/branch_6_0 from Chris Hostetter
          [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=240140d ]

          SOLR-8940: Fix group.sort option

          (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)

          Show
          jira-bot ASF subversion and git services added a comment - Commit 240140da0fc833a80eab2130ea117ae4f21e77aa in lucene-solr's branch refs/heads/branch_6_0 from Chris Hostetter [ https://git-wip-us.apache.org/repos/asf?p=lucene-solr.git;h=240140d ] SOLR-8940 : Fix group.sort option (cherry picked from commit 18256fc2873f198e8e577c6eb0f337df1d1cda24)

            People

            • Assignee:
              hossman Hoss Man
              Reporter:
              monti Henrik
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development