Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong queries created by builder #118

Open
mgrechanik opened this issue Aug 18, 2020 · 11 comments
Open

Wrong queries created by builder #118

mgrechanik opened this issue Aug 18, 2020 · 11 comments
Labels

Comments

@mgrechanik
Copy link

What steps will reproduce the problem?

        $query = new \yii\sphinx\Query();
        $query->select('id, name, entity_id, entity_type')->
                from('all');
//...
$query->match(new Expression(':match', ['match' => $str]));

What's expected?

SQL like:

SELECT `id` , `name` , `entity_id` , `entity_type`  FROM `all` WHERE MATCH('dddddddd* ')

What do you get instead?

SELECT `id` AS `id`, `name` AS `name`, `entity_id` AS `entity_id`, `entity_type` AS `entity_type` FROM `all` WHERE MATCH('dddddddd* ')

And such sql - with aliases which are the same with fields they are for - does not work for sphinx 2.3.2 and two other earlier versions.
One can try such sphinx sql in console, the one with such aliases does not work.
The error is this one:

ERROR 1064 (42000): sphinxql: syntax error, unexpected ID near '`id`, `name` AS `name`, `entity_id` AS `entity_id`, `entity_type` AS `entity_type` FROM `all` WHERE MATCH('sss')'

Additional info

Q A
Yii version 2.0.32
Yii Sphinx version 2.0.11
Sphinx server version 2.3.2
PHP version 7.3.
Operating system Ubuntu 16
@mgrechanik
Copy link
Author

It seems that the problem is with aliasing the id : id AS id
Without it it will be all right.

@samdark
Copy link
Member

samdark commented Aug 18, 2020

Am I correct that these versions you've mentioned do not support aliases at all? Or is the problem with using same name for alias?

@samdark samdark added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Aug 18, 2020
@mgrechanik
Copy link
Author

mgrechanik commented Aug 19, 2020

@samdark , only id field does not suppot the same alias to itself.
This works:

SELECT `id` , `name` AS `name`, `entity_id` AS `entity_id`, `entity_type` AS `entity_type` FROM `all` WHERE MATCH('dddddddd* ')

p.s. there is no such problem in the modern versions of sphinx (tested on 3.1.1)

@samdark
Copy link
Member

samdark commented Aug 19, 2020

@mgrechanik does it make sense to fix for old versions?

@mgrechanik
Copy link
Author

@samdark they are old versions but many think they are more stable.
I solved this problem in the project by manually using my own alias:

$query->select('id as doc_id, name, entity_id, entity_type')

, so yii2-sphinx does not try to add it's own

@samdark
Copy link
Member

samdark commented Aug 19, 2020

OK. How would you fix it?

@samdark samdark removed the status:to be verified Needs to be reproduced and validated. label Aug 19, 2020
@mgrechanik
Copy link
Author

If this line :

$columns[$i] = "$column AS " . $this->db->quoteColumnName($i);

change to:

$columns[$i] = ($i == 'id') ? $column : "$column AS " . $this->db->quoteColumnName($i);

error disapears.

@klimov-paul , what would you say?

@samdark samdark added this to the 2.0.14 milestone Aug 21, 2020
@samdark
Copy link
Member

samdark commented Aug 21, 2020

Looks OK. Do you want to prepare a pull request to speed things up?

@mgrechanik
Copy link
Author

On the second thought this quick guess is not perfect

@samdark
Copy link
Member

samdark commented Aug 24, 2020

Why?

@mgrechanik
Copy link
Author

Because In Sphinx 3 it is possible to use such alias and someone might have used it for some other field.

@samdark samdark removed this from the 2.0.14 milestone Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants