Skip to content

Advanced MSSQL query workaround for limit, offset and for sql with aggregate functions#13262

Merged
wilsonge merged 4 commits intojoomla:stagingfrom
csthomas:fixmssql
Feb 25, 2017
Merged

Advanced MSSQL query workaround for limit, offset and for sql with aggregate functions#13262
wilsonge merged 4 commits intojoomla:stagingfrom
csthomas:fixmssql

Conversation

@csthomas
Copy link
Copy Markdown
Contributor

@csthomas csthomas commented Dec 18, 2016

Competition for #11938

Summary of Changes

Fix various problems with sqlsrv queries:

  • Add alias to column (or expression) which does not have it, but it is required if query will use OFFSET, which means use wrapped query to emulate offset. Every column in SELECT statement required alias if it is placed in subquery.
  • If SELECT contains a wild column x.* then load and put all columns from x table to GROUP BY
  • If GROUP BY contains alias to (column or expression) then replace it with expression.
    Ex:
    SELECT LOWER(name) AS name, catid AS cc, count(*) ... GROUP BY name, cc
    will be replaced to
    SELECT LOWER(name) AS name, count(*) ... GROUP BY LOWER(name), catid
  • Add column from ORDER BY to GROUP BY if not yet exists. If this is an alias then replace it with expression as mentioned earlier.
  • Add columns from SELECT to GROUP BY if missing (related to mysql ONLY_FULL_GROUP_BY). Probably it would be better to fix joomla queries than create such workaround.
    Example of fixed query:
    SELECT id, name, COUNT(*) FROM #__x GROUP BY id
    will be replaced by:
    SELECT id, name, COUNT(*) AS columnAlias0 FROM #__x GROUP BY id, name

Fixed issue: #11279

Removes notices, warnings and errors from:

  • /administrator/index.php?option=com_banners
  • /administrator/index.php?option=com_categories&extension=com_banners
  • /administrator/index.php?option=com_banners&view=clients
  • /administrator/index.php?option=com_finder&view=maps
  • /administrator/index.php?option=com_categories&extension=com_newsfeeds
  • /administrator/index.php?option=com_categories&extension=com_contact
  • /administrator/index.php?option=com_postinstall

Fix issue with Popular Tags module on the front end.

Testing Instructions

Be sure that you can see PHP Notices.
Test above links and module Popular Tags before and after patch.

  • Install a new staging version of joomla (without sample data because currently joomla on mssql is broken - menu assets)
  • Create a category and two articles with a few tags.
  • Be sure that you have module Popular Tags enabled (ex. position-7)
  • Go to the front page
  • I should see some similar sql error as on image but with less details

joomla_win_3 6 6

Documentation Changes Required

No

@csthomas csthomas force-pushed the fixmssql branch 2 times, most recently from 7318025 to 4e6663c Compare December 19, 2016 20:14
@csthomas csthomas changed the title [WIP] More advanced MSSQL query workaround for limit, offset and for sql with aggregate functions Advanced MSSQL query workaround for limit, offset and for sql with aggregate functions Dec 19, 2016
@csthomas csthomas force-pushed the fixmssql branch 2 times, most recently from f48b75d to c5d2340 Compare December 21, 2016 20:29
@csthomas csthomas force-pushed the fixmssql branch 2 times, most recently from 349f10b to 238413e Compare January 6, 2017 00:07
@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Jan 7, 2017

@waader Which URL page or sql query did not work?

@waader
Copy link
Copy Markdown
Contributor

waader commented Jan 7, 2017

With current staging, testing data and your patch applied just go to the frontend. There are a lot of notices (something in the module helper). I have already shutdown my test pc, so I could give you the exact messages tomorrow.

@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Jan 8, 2017

I have found a few errors and I fixed it.
Please inform me if you find some errors.

@alikon
Copy link
Copy Markdown
Contributor

alikon commented Jan 8, 2017

i'm testing on backend and seems ok, unfortunately even if is not caused by this pr i see issues on frontend too as reported by @waader
can you have a look at frontend with current staging and testing data
thanks in advance

@waader
Copy link
Copy Markdown
Contributor

waader commented Jan 8, 2017

With testing data installed frontend gives an "404 Component not found" error. Starting with index.php?option=com_content the page loads. The module helper notices are also present and no modul content is displayed.

In backend I get an error when deleting Banner Tracks:
An expression of non-boolean type specified in a context where a condition is expected, near ')'.

@waader
Copy link
Copy Markdown
Contributor

waader commented Jan 8, 2017

Can you have a look at Smart Search > Content Maps:

[Microsoft][SQL Server Native Client 11.0][SQL Server]Column "d.branch_title" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Jan 8, 2017

I will take a look. Are you sure do you use the last commit in this PR, I added it today morning (night)?

@waader
Copy link
Copy Markdown
Contributor

waader commented Jan 8, 2017

I have several patches for mssql installed now and also this patch and now the problem with content map has gone away.

Frontend is broken nonetheless.

@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Jan 8, 2017

On test database I have found two 404 links because mssql table does not have 2 articles for:

  • Popular Tags (Alias: popular-tags)
  • Similar Tags (Alias: similar-tags)
  • one in news-feed - ??
  • and search component has some error. ??

but home page work for me ok.

@wilsonge
Copy link
Copy Markdown
Contributor

Frontend issue is a separate issue with a fix here: #14057

@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Feb 14, 2017

Warning: ksort() expects parameter 2 to be long

I assume this is php 5.3 issue, I have created an issue.

@waader
Copy link
Copy Markdown
Contributor

waader commented Feb 14, 2017

@WilsonG #14057 resolves the problem - thanks
@csthomas You are right. My mssql-test system run´s on 5.3.29

By the way: with the redirect plugin enabled I get 500 - PLG_SYSTEM_REDIRECT_ERROR_UPDATING_DATABASE

@wilsonge
Copy link
Copy Markdown
Contributor

@csthomas that redirect plugin error might be related to this pr :)

@csthomas
Copy link
Copy Markdown
Contributor Author

By the way: with the redirect plugin enabled I get 500 - PLG_SYSTEM_REDIRECT_ERROR_UPDATING_DATABASE

[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot insert the value NULL into column 'comment', table 'joomla.dbo.j37_redirect_links'; column does not allow nulls. INSERT fails.
This is unrelated.

I have found a big problem with sqlsrv updates in joomla (administrator/components/com_admin/sql/updates/sqlazure/*.sql).
A lots of that ALTER TABLE ALTER COLUMN has invalid syntax.

https://msdn.microsoft.com/en-us/library/ms190273.aspx

They are not executed to database at all. At least for my environment.
I need a little time to create an issue.

@waader
Copy link
Copy Markdown
Contributor

waader commented Feb 22, 2017

@csthomas: Can you sychronize this patch with the changes made in other yet commited sqlserver-patches.

@csthomas
Copy link
Copy Markdown
Contributor Author

OK. Please wait a moment

Tomasz Narloch added 2 commits February 22, 2017 21:56
* Fix offset, limit - intruduce SELECT TOP
* Add multi table update for mssql query
* Add more advanced workaround for mssql GROUP BY.
* Add required aliases to complex columns (ex. function) in subqueries.
* Add workaround for use alias in GROUP BY statement
* Add to GROUP BY statement columns from ORDER BY
* Remove column with static string, NULL or numeric from GROUP BY statement
* Add unit tests
@csthomas
Copy link
Copy Markdown
Contributor Author

I have rebased it. It should work but you can not use sample data, because it is broken, I'm working on that now (#13981).

But you can get sample data from #7680

@csthomas
Copy link
Copy Markdown
Contributor Author

csthomas commented Feb 22, 2017

Please ignore errors like:

Cannot insert the value NULL into column 'term_id', table 'joomla.dbo.#__finder_tokens_aggregate'; column does not allow nulls. INSERT fails.

It is unrelated, and it is waiting for a new PR.

@csthomas
Copy link
Copy Markdown
Contributor Author

There is not need to combine this PR for tests with #14102 in the same time.

But if someone want to test 2 PRs at once then:

@waader
Copy link
Copy Markdown
Contributor

waader commented Feb 24, 2017

I have tested this item ✅ successfully on 00fd4a9

Fixes these problems:
/administrator/index.php?option=com_banners
/administrator/index.php?option=com_categories&extension=com_banners
/administrator/index.php?option=com_banners&view=clients
/administrator/index.php?option=com_finder&view=maps
/administrator/index.php?option=com_categories&extension=com_newsfeeds
/administrator/index.php?option=com_categories&extension=com_contact
/administrator/index.php?option=com_postinstall

Thanks!


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/13262.

@csthomas
Copy link
Copy Markdown
Contributor Author

Last commit fix error on "sample testing" for menu items:

Compact tagged: should show one record
Tagged items: should show one record

@csthomas
Copy link
Copy Markdown
Contributor Author

For All tags the is other error which has to be resolved by separate PR.

DB function failed with error number 169
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]A column has been specified more than once in the order by list. Columns in the order by list must be unique.
SQL =

SELECT TOP 20 a.*
FROM [#__tags] AS a
WHERE [a].[access] IN (1,5,1) AND [a].[parent_id] <> 0 AND [a].[title] LIKE N'%[gl]%' AND . [a].[published] = 1
ORDER BY [title] ASC, a.title ASC

[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]A column has been specified more than once in the order by list. Columns in the order by list must be unique.

@wilsonge wilsonge merged commit aeebb2b into joomla:staging Feb 25, 2017
@wilsonge wilsonge added this to the Joomla 3.7.0 milestone Feb 25, 2017
@wilsonge
Copy link
Copy Markdown
Contributor

Merged with waaders test given this is only affecting mssql

@csthomas
Copy link
Copy Markdown
Contributor Author

Thanks

@csthomas csthomas deleted the fixmssql branch February 25, 2017 12:42
@photodude
Copy link
Copy Markdown
Contributor

@csthomas Will you consider making these changes to the Framework also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants