manticoresoftware / manticoresearch

Easy to use open source fast database for search | Good alternative to Elasticsearch now | Drop-in replacement for E in the ELK soon
https://manticoresearch.com
GNU General Public License v3.0
8.87k stars 490 forks source link

Allow usage of COUNT(1) #1741

Open cappadaan opened 8 months ago

cappadaan commented 8 months ago

Feature Request to allow:

COUNT(1)

which should be an alias of

COUNT(*)

Mysql and Elastic Search allow this, so would be nice to have this as well.

239yash commented 5 months ago

@sanikolaev I am checking this out, Taking a look at file - /manticoresearch-buddy-main/src/Plugin/Select/Payload.php Please help me, whether I am going right or not. I guess, we need to make changes in this file and the Handler file of this module to allow COUNT(1) right?

sanikolaev commented 5 months ago

@donhardman pls help @239yash

donhardman commented 5 months ago

@239yash, yes, this is correct. You need to add a handler for this type of query in the Payload (using the hasMatch method) and also add execution logic that will simply replace it with COUNT(*) and proxy the request to the Manticore search, then return the results to the Handler class.

239yash commented 5 months ago

Hi @donhardman I have this snippet of code from the function handleSelectCountOnField present in the Handler.php file from the Select module.

<?php
$pattern = '/COUNT\((?! *\* *\))(\w+)\)/ius';
$replacement = 'COUNT(*)';
$query = preg_replace($pattern, $replacement, 'COUNT(1)');
echo $query;
?>

If you pass COUNT(1) inside the function, This will result in COUNT(*) only. Is this case already handled, or is there anything else I missed?

donhardman commented 5 months ago

Hi @donhardman I have this snippet of code from the function handleSelectCountOnField present in the Handler.php file from the Select module.

<?php
$pattern = '/COUNT\((?! *\* *\))(\w+)\)/ius';
$replacement = 'COUNT(*)';
$query = preg_replace($pattern, $replacement, 'COUNT(1)');
echo $query;
?>

If you pass COUNT(1) inside the function, This will result in COUNT(*) only. Is this case already handled, or is there anything else I missed?

This method handles complex queries. By default, the COUNT(1) pattern doesn't go through this method.

To make it work, you need to add a rule in the Payload to handle such queries. Once the queries reach the Payload, you can rewrite them and pass them to the internal Manticoresearch client. Then, you can return the final results to the user

sanikolaev commented 4 months ago

@239yash Hi. How are things going? Do you need any assistance to complete the PR?

239yash commented 4 months ago

@sanikolaev hi, I am checking this. I will surely ask if any help is required. Thanks!