Bug 107459 - MATCH with third parameter MatchType=-1 fails if the SearchVector is passed directly as the result of an array-formula.
Summary: MATCH with third parameter MatchType=-1 fails if the SearchVector is passed d...
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Eike Rathke
URL:
Whiteboard: target:7.1.0 target:7.0.2 target:7.2.0
Keywords:
Depends on:
Blocks: Calc-Function
  Show dependency treegraph
 
Reported: 2017-04-26 21:13 UTC by Wolfgang Jäger
Modified: 2021-03-07 20:21 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
Demonstration announced in the original bug report (13.15 KB, application/vnd.oasis.opendocument.spreadsheet)
2017-04-26 21:13 UTC, Wolfgang Jäger
Details
Sample file (15.25 KB, application/vnd.oasis.opendocument.spreadsheet)
2017-04-30 14:27 UTC, m_a_riosv
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Jäger 2017-04-26 21:13:29 UTC
Created attachment 132876 [details]
Demonstration announced in the original bug report

{=MATCH($E3;$C$3:$C$11;-1)} e.g. is returning the correct result even if it is unnecessarily entered for array evaluation. 

{=MATCH($E3;0+$B$3:$B$11;-1)} returns the position of the wrong end if the SearchVector is containing a group of equal values determining the match.  

In the attached example this is demonstrated for the case that C3:C11 is calculated by exactly the same trivial array formula provoking the error if directly passed to MATCH. 

The bug may have sparse gray hair, and the demonstrated case is not realistic. There ARE applications of some interest, and it's a bug anyway.
Comment 1 m_a_riosv 2017-04-28 22:55:36 UTC
The issue with your second formula is the use of '0+' as array modification, with a modification different than '0', or with any other modification or a clean array works fine for me.

So it can be a trick way to get the reverse behavior.
Comment 2 Wolfgang Jäger 2017-04-29 23:22:01 UTC
(In reply to m.a.riosv from comment #1)
> The issue with your second formula is the use of '0+' as array modification,
> with a modification different than '0', or with any other modification or a
> clean array works fine for me.
> 
> So it can be a trick way to get the reverse behavior.

Who should have decided to establish that "trick way"? Where is it specified?

Of course, the "0+" is only the extreme simplification of what I had originally when I discovered the bug. There are also relevant formulas resulting in arrays to pass to MATCH in the second place. 
In fact I discovered the bug when I tried to get a REVERSE MATCH against an ascending column-array by something like 
{=MATCH(Value;INDEX($B$1:$B$10;ROW($B$10)-ROW($B$1:$B$10)+1;1);-1)}
I did not find too easily the way back to the root of the bug: making a difference between an array referenced directly and one with the identical elements calculated in the parameter position where it's used. 
The bug is from ancient days and seemingly was not reported all the time. It's a bug nonetheless. And it may e.g. throw light on a flaw in the design of the code concerned with array evaluation generally. It may also be an isolated problem with MATCH.
Comment 3 m_a_riosv 2017-04-30 14:27:20 UTC
Created attachment 132971 [details]
Sample file

N(In reply to Wolfgang Jäger from comment #2)
> (In reply to m.a.riosv from comment #1)
> > So it can be a trick way to get the reverse behavior.
> 
> Who should have decided to establish that "trick way"? Where is it specified?
I didn't say that.

I have done a new sample, where to seems the issue is the -1 for third MATCH parameter.
But it happens with a modified 'Reference|Array SearchRegion', what I am not sure it is allowed in the specification, at least explicitly.

http://docs.oasis-open.org/office/v1.2/csd06/OpenDocument-v1.2-csd06-part2.pdf
page 133.
Syntax: MATCH(Scalar Search ;Reference|Array SearchRegion [;Integer MatchType = 1 ]

Hi @Eike, @Winfried some light.
Comment 4 Wolfgang Jäger 2017-05-01 11:07:58 UTC
(In reply to m.a.riosv from comment #3)
> I have done a new sample, where to seems the issue is the -1 for third MATCH
> parameter.

Ack. See subject. However, I wouldn't see the -1 as the issue but as a condition for its incidence.

> But it happens with a modified 'Reference|Array SearchRegion', what I am not
> sure it is allowed in the specification, at least explicitly. 

What do you think is the rationale for the alternative "Reference|Array" given in the type position for the parameter SearchRange. We shouldn't get deceived by the slightly misleading name. Parameter names are arbitrary basically. However, type names used somewhere should be defined in advance. 
In fact there is an issue with  subchapter 4.10 of the document you mentioned. It reads as if "Array" is synonym with something like "Range" or "RangeReference". The implemented functions concerned accept Calculated Arrays anyway), and I assume this to be the intention of OpenDocument V1.2 part 2, too. A different interpretation would make powerless toys of some functions. 

Is there a draft for a next version of odf (recalc) concerning this?
Comment 5 m_a_riosv 2017-05-01 15:46:55 UTC
(In reply to Wolfgang Jäger from comment #4)
>..............
> 
For me, if it is not explicit, it's not explicit, and I can't interpreter as I like.
Even I think I understand your interpretation, that sounds reasonable.

> Is there a draft for a next version of odf (recalc) concerning this?
https://www.oasis-open.org/committees/document.php?document_id=51469&wg_abbrev=office-collab

In any case testing with a inner array gives what seems an erroneous result

=MATCH(5;{20;19;18;17;16;15;14;13;12;11;10;6;6;5;5;4;4;3;2;1};-1) = 14
           1  2  3  4  5  6  7  8  9 10  1 2 3 4 5 6 7 8 9 20

If I'm not wrong result should be 15 as happens with an unmodified address array.
Comment 6 QA Administrators 2018-05-02 02:32:37 UTC Comment hidden (obsolete)
Comment 7 Commit Notification 2020-09-11 19:19:42 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6f80e2d37ce11e3ea728b7ce5987f912fdfa60b3

Resolves: tdf#107459 MATCH() in descending sorted array find last equal value

It will be available in 7.1.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 8 Eike Rathke 2020-09-11 19:36:56 UTC
Pending review https://gerrit.libreoffice.org/c/core/+/102428 for 7-0
Comment 9 Commit Notification 2020-09-12 13:26:31 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "libreoffice-7-0":

https://git.libreoffice.org/core/commit/181c737d5f2ed3718612dcafeaf458f5f147ce89

Resolves: tdf#107459 MATCH() in descending sorted array find last equal value

It will be available in 7.0.2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 10 Commit Notification 2020-09-13 07:13:26 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a4a6594b6475ef2e397852862ec120e33350b352

tdf#107459: sc_ucalc: Add unittest

It will be available in 7.1.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 11 Commit Notification 2021-03-07 20:21:01 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/ce507a14c551b9d2ce64c5cf1580431bb04b01a0

tdf#107459: move unittest to function test

It will be available in 7.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.