Blob Blame History Raw
From 9cd5a1b11f2f5f69d674f0b0e44352d96518013e Mon Sep 17 00:00:00 2001
From: Michael Stahl <mstahl@redhat.com>
Date: Thu, 19 Jan 2012 15:25:07 +0100
Subject: [PATCH 4/8] connectivity: fdo#43479: fix crash on DISTINCT:

Since commit f89f2b8bf506de0cc547ad596c75cbe1a0cf1ef1,
OResultSet::sortRows() works on the rows after SELECT, not on full rows.
So OResultSet::OpenImpl() has to be adapted to not use the mapping from
selected columns to entries rows in m_aColMapping any more; instead,
use the given ORDER BY clause for sorting.
But first extend the sort order to cover all columns, so it is no longer
necessary to call sortRows twice (this should be legal, because SQL says
the order of rows that are equal in ORDER BY columns is unspecified).

Signed-off-by: Lionel Elie Mamane <lionel@mamane.lu>
---
 connectivity/source/drivers/file/FResultSet.cxx |   48 ++++++++---------------
 1 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/connectivity/source/drivers/file/FResultSet.cxx b/connectivity/source/drivers/file/FResultSet.cxx
index 71d2f3e..26baffe 100644
--- a/connectivity/source/drivers/file/FResultSet.cxx
+++ b/connectivity/source/drivers/file/FResultSet.cxx
@@ -1441,26 +1441,29 @@ sal_Bool OResultSet::OpenImpl()
             else
             {
                 sal_Bool bDistinct = sal_False;
-                sal_Bool bWasSorted = sal_False;
                 OSQLParseNode *pDistinct = m_pParseTree->getChild(1);
-                ::std::vector<sal_Int32>				aOrderbyColumnNumberSave;
-                ::std::vector<TAscendingOrder>          aOrderbyAscendingSave;
                 
+                assert(m_aOrderbyColumnNumber.size() ==
+                       m_aOrderbyAscending.size());
                 if (pDistinct && pDistinct->getTokenID() == SQL_TOKEN_DISTINCT )
                 {
-                    // Sort on all columns, saving original order for later
-                    if(IsSorted())
+                    // To eliminate duplicates we need to sort on all columns.
+                    // This is not a problem because the SQL spec says that the
+                    // order of columns that are not specified in ORDER BY
+                    // clause is undefined, so it doesn't hurt to sort on
+                    // these; pad the vectors to include them.
+                    for (sal_Int32 i = 1; // 0: bookmark (see setBoundedColumns)
+                         static_cast<size_t>(i) < m_aColMapping.size(); ++i)
                     {
-                        aOrderbyColumnNumberSave = m_aOrderbyColumnNumber;
-                        m_aOrderbyColumnNumber.clear();
-                        aOrderbyAscendingSave.assign(m_aOrderbyAscending.begin(), m_aOrderbyAscending.end());
-                        bWasSorted = sal_True;
+                        if (::std::find(m_aOrderbyColumnNumber.begin(),
+                                        m_aOrderbyColumnNumber.end(), i)
+                                == m_aOrderbyColumnNumber.end())
+                        {
+                            m_aOrderbyColumnNumber.push_back(i);
+                            // ASC or DESC doesn't matter
+                            m_aOrderbyAscending.push_back(SQL_ASC);
+                        }
                     }
-                    
-                    // the first column is the bookmark column
-                    ::std::vector<sal_Int32>::iterator aColStart = (m_aColMapping.begin()+1);
-                    ::std::copy(aColStart, m_aColMapping.end(),::std::back_inserter(m_aOrderbyColumnNumber));
-                    m_aOrderbyAscending.assign(m_aColMapping.size()-1, SQL_ASC);
                     bDistinct = sal_True;
                 }
 
@@ -1542,23 +1545,6 @@ sal_Bool OResultSet::OpenImpl()
                         m_pFileSet->get().erase(::std::remove_if(m_pFileSet->get().begin(),m_pFileSet->get().end(),
                                                             ::std::bind2nd(::std::equal_to<sal_Int32>(),0))
                                           ,m_pFileSet->get().end());
-
-                        if (bWasSorted)
-                        {
-                            // Re-sort on original requested order
-                            m_aOrderbyColumnNumber = aOrderbyColumnNumberSave;
-                            m_aOrderbyAscending.assign(aOrderbyAscendingSave.begin(), aOrderbyAscendingSave.end());
-                            
-                            TIntVector aEvaluationKeySet(m_pFileSet->get());
-                            m_pEvaluationKeySet = &aEvaluationKeySet;
-                            sortRows();
-                        }
-                        else
-                        {
-                            m_aOrderbyColumnNumber.clear();
-                            m_aOrderbyAscending.clear();
-                            ::std::sort(m_pFileSet->get().begin(),m_pFileSet->get().end());
-                        }
                     }
                 }
             }
-- 
1.7.7.6