question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deleting records with selection on `_id` is broken

See original GitHub issue

CrateDB version

4.7.0 / 5.0.1 / 5.10

CrateDB setup information

single / multi-node

Steps to Reproduce

cr> CREATE TABLE del_test (val int);                                                                                                                                                                          
CREATE OK, 1 row affected  (0.415 sec)

cr> INSERT INTO del_test VALUES  (1),(1),(2);                                                                                                                                                                 
INSERT OK, 3 rows affected  (0.032 sec)

cr> SELECT arbitrary(_id)  
    FROM del_test  
    WHERE val = 1  
    GROUP BY val  
    HAVING count(*) > 1;                                                                                                                                                                                      
+----------------------+
| arbitrary(_id)       |
+----------------------+
| 6wCcQoQBNMIiChzeUweD |
+----------------------+
SELECT 1 row in set (0.019 sec)
-- here is the problem:
cr> DELETE FROM del_test WHERE _id IN (SELECT arbitrary(_id)  
    FROM del_test  
    WHERE val = 1  
    GROUP BY val  
    HAVING count(*) > 1);                                                                                                                                                                                     
DELETE OK, 0 rows affected  (0.022 sec)

cr> DELETE FROM del_test WHERE _id IN (SELECT '6wCcQoQBNMIiChzeUweD');                                                                                                                                        
DELETE OK, 0 rows affected  (0.029 sec)
-- that works
cr> DELETE FROM del_test WHERE _id IN ('6wCcQoQBNMIiChzeUweD');                                                                                                                                               
DELETE OK, 1 row affected  (0.031 sec)

Expected Result

DELETE OK, 1 row affected  (0.031 sec)

Actual Result

DELETE OK, 0 rows affected  (0.029 sec)

edit:

EXPLAIN
SELECT *
FROM del_test
WHERE _id IN (SELECT _id FROM del_test)
-- SELECT OK, 0 records returned (0.033 seconds)

MultiPhase
  └ Collect[doc.del_test | [val] | (_id = ANY((SELECT _id FROM (doc.del_test))))]
  └ OrderBy[_id ASC]
    └ Collect[doc.del_test | [_id] | true]

------------

EXPLAIN
SELECT *
FROM del_test
WHERE _id::CHAR(20) IN (SELECT _id FROM del_test)
-- SELECT OK, 3 records returned (0.041 seconds)

MultiPhase
  └ Collect[doc.del_test | [val] | (_cast(cast(_id AS character), 'text') = ANY((SELECT _id FROM (doc.del_test))))]
  └ OrderBy[_id ASC]
    └ Collect[doc.del_test | [_id] | true]

------------
EXPLAIN
SELECT *
FROM del_test
WHERE _id IN ('58puQ4QBQmJ97UbuHnXp','jxcRQ4QB4KuX8tRunOAg')

Get[doc.del_test | val | DocKeys{'58puQ4QBQmJ97UbuHnXp'; 'jxcRQ4QB4KuX8tRunOAg'} | (_id = ANY(['58puQ4QBQmJ97UbuHnXp', 'jxcRQ4QB4KuX8tRunOAg']))]

Seems like the bug first occurred with CrateDB 4.7 (4.6.8 tested to be working)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matrivcommented, Nov 7, 2022

FYI: That’s not a problem of the DELETE but a problem with _id IN/ANY and subquery.

1reaction
hlcianfagnacommented, Nov 7, 2022

Hi, I am the original reporter on this issue, today I tried to add casting to the _id field as suggested. I am on version 4.8.4 and unfortunately the cast does not work here, I get the message The 'char' type doesn't support type parameters when I add ::CHAR(20).

Just logging that on a separate conversation it has been confirmed that on versions of CrateDB < 5.0.0 the CHAR data type is not available, but the issue can still be worked around using VARCHAR instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Delete query with select query is not working as expected
DELETE FROM B_786 WHERE EXISTS (SELECT * FROM I_786);. But it is deleting all the data from B_786 table. Here the where condition...
Read more >
CTE SQL Deletes; Considerations when Deleting Data with ...
Since we want to removed the ordered data that show a duplicate id (in this case, 2), we'll create a query with the...
Read more >
I get the error message "Could not delete from the specified ...
To fix this problem, set the query's Unique Records property to Yes. Open the delete query in Design view.
Read more >
Delete class not working - apex - Salesforce Stack Exchange
SELECT Id FROM Account_List_vod__c WHERE Name = 'HO_RADY\'S CHILDREN\'S UCSD' ... deleteAccounts}" rerender="pmsg, pbAccounts" value="Delete ...
Read more >
Mass Delete Records tool fails without error message
While working in the tool, users may report that the deletion page refreshes as expected but selected records continue to appear in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found