Skip to content

Commit

Permalink
Merge branch 'main' of github.com:datafuselabs/databend into hash_joi…
Browse files Browse the repository at this point in the history
…n_early_filter
  • Loading branch information
Dousir9 committed Nov 4, 2023
2 parents 0d76e75 + 59077f1 commit ca0b577
Show file tree
Hide file tree
Showing 20 changed files with 1,317 additions and 549 deletions.
76 changes: 41 additions & 35 deletions docs/doc/14-sql-commands/10-dml/dml-delete-from.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: DELETE

import FunctionDescription from '@site/src/components/FunctionDescription';

<FunctionDescription description="Introduced: v1.1.60"/>
<FunctionDescription description="Introduced or updated: v1.2.174"/>

Removes one or more rows from a table.

Expand All @@ -15,43 +15,36 @@ Databend ensures data integrity with atomic operations. Inserts, updates, replac
## Syntax

```sql
DELETE FROM <table_name> [[AS] alias]
DELETE FROM <table_name> [AS <table_alias>]
[WHERE <condition>]
```
- `AS <table_alias>`: Allows you to set an alias for a table, making it easier to reference the table within a query. This helps simplify and shorten the SQL code, especially when dealing with complex queries involving multiple tables. See an example in [Deleting with subquery using EXISTS / NOT EXISTS clause](#deleting-with-subquery-using-exists--not-exists-clause).

DELETE FROM does not support the USING clause yet. If you need to use a subquery to identify the rows to be removed, include it within the WHERE clause directly. See examples in [Subquery-Based Deletions](#subquery-based-deletions).
- DELETE does not support the USING clause yet. If you need to use a subquery to identify the rows to be removed, include it within the WHERE clause directly. See examples in [Subquery-Based Deletions](#subquery-based-deletions).

## Examples

### Direct Row Deletion
### Example 1: Direct Row Deletion

This example illustrates the use of the DELETE command to directly remove a book record with an ID of 103 from a "bookstore" table.

```sql
-- create a table
-- Create a table and insert 5 book records
CREATE TABLE bookstore (
book_id INT,
book_name VARCHAR
);

-- insert values
INSERT INTO bookstore VALUES (101, 'After the death of Don Juan');
INSERT INTO bookstore VALUES (102, 'Grown ups');
INSERT INTO bookstore VALUES (103, 'The long answer');
INSERT INTO bookstore VALUES (104, 'Wartime friends');
INSERT INTO bookstore VALUES (105, 'Deconstructed');

-- show the table before deletion
SELECT * FROM bookstore;

101|After the death of Don Juan
102|Grown ups
103|The long answer
104|Wartime friends
105|Deconstructed

-- delete a book (Id: 103)
-- Delete a book (Id: 103)
DELETE FROM bookstore WHERE book_id = 103;

-- show the table again after deletion
-- Show all records after deletion
SELECT * FROM bookstore;

101|After the death of Don Juan
Expand All @@ -60,30 +53,35 @@ SELECT * FROM bookstore;
105|Deconstructed
```

### Subquery-Based Deletions
### Example 2: Subquery-Based Deletions

When using a subquery to identify the rows to be deleted, [Subquery Operators](../30-query-operators/subquery.md) and [Comparison Operators](../30-query-operators/comparisons/index.md) can be utilized to achieve the desired deletion.

The examples in this section are based on the following two tables:

```sql
-- Table: employees
+----+----------+------------+
| id | name | department |
+----+----------+------------+
| 1 | John | HR |
| 2 | Mary | Sales |
| 3 | David | IT |
| 4 | Jessica | Finance |
+----+----------+------------+

-- Table: departments
+----+------------+
| id | department |
+----+------------+
| 1 | Sales |
| 2 | IT |
+----+------------+
-- Create the 'employees' table
CREATE TABLE employees (
id INT,
name VARCHAR,
department VARCHAR
);

-- Insert values into the 'employees' table
INSERT INTO employees VALUES (1, 'John', 'HR');
INSERT INTO employees VALUES (2, 'Mary', 'Sales');
INSERT INTO employees VALUES (3, 'David', 'IT');
INSERT INTO employees VALUES (4, 'Jessica', 'Finance');

-- Create the 'departments' table
CREATE TABLE departments (
id INT,
department VARCHAR
);

-- Insert values into the 'departments' table
INSERT INTO departments VALUES (1, 'Sales');
INSERT INTO departments VALUES (2, 'IT');
```

#### Deleting with subquery using IN / NOT IN clause
Expand All @@ -106,6 +104,14 @@ WHERE EXISTS (
FROM DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT = DEPARTMENTS.DEPARTMENT
);

-- Alternatively, you can delete employees using the alias 'e' for the 'EMPLOYEES' table and 'd' for the 'DEPARTMENTS' table when their department matches.
DELETE FROM EMPLOYEES AS e
WHERE EXISTS (
SELECT *
FROM DEPARTMENTS AS d
WHERE e.DEPARTMENT = d.DEPARTMENT
);
```
This deletes employees who belong to a department that exists in the departments table. In this case, it would delete employees with IDs 2 and 3.

Expand Down
4 changes: 2 additions & 2 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3220,7 +3220,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
let if_then = vec![txn_op_put_with_expire(
&key,
serialize_struct(&lock_meta)?,
req.expire_at,
SeqV::<()>::now_ms() / 1000 + req.expire_secs,
)];

let txn_req = TxnRequest {
Expand Down Expand Up @@ -3284,7 +3284,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
let if_then = vec![txn_op_put_with_expire(
&key,
serialize_struct(&lock_meta)?,
req.expire_at,
SeqV::<()>::now_ms() / 1000 + req.expire_secs,
)];

let txn_req = TxnRequest {
Expand Down
6 changes: 3 additions & 3 deletions src/meta/api/src/schema_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5237,7 +5237,7 @@ impl SchemaApiTestSuite {
info!("--- create table lock revision 1");
let req1 = CreateLockRevReq {
lock_key: LockKey::Table { table_id },
expire_at: (Utc::now().timestamp() + 2) as u64,
expire_secs: 2,
user: "root".to_string(),
node: "node1".to_string(),
query_id: "query1".to_string(),
Expand All @@ -5247,7 +5247,7 @@ impl SchemaApiTestSuite {
info!("--- create table lock revision 2");
let req2 = CreateLockRevReq {
lock_key: LockKey::Table { table_id },
expire_at: (Utc::now().timestamp() + 2) as u64,
expire_secs: 2,
user: "root".to_string(),
node: "node1".to_string(),
query_id: "query2".to_string(),
Expand All @@ -5267,7 +5267,7 @@ impl SchemaApiTestSuite {
info!("--- extend table lock revision 2 expire");
let req4 = ExtendLockRevReq {
lock_key: LockKey::Table { table_id },
expire_at: (Utc::now().timestamp() + 4) as u64,
expire_secs: 4,
revision: res2.revision,
acquire_lock: true,
};
Expand Down
4 changes: 2 additions & 2 deletions src/meta/app/src/schema/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct ListLockRevReq {
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CreateLockRevReq {
pub lock_key: LockKey,
pub expire_at: u64,
pub expire_secs: u64,
pub user: String,
pub node: String,
pub query_id: String,
Expand All @@ -123,7 +123,7 @@ pub struct CreateLockRevReply {
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ExtendLockRevReq {
pub lock_key: LockKey,
pub expire_at: u64,
pub expire_secs: u64,
pub revision: u64,
pub acquire_lock: bool,
}
Expand Down
5 changes: 2 additions & 3 deletions src/query/catalog/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use std::sync::Arc;

use chrono::Utc;
use common_exception::Result;
use common_meta_app::schema::CreateLockRevReq;
use common_meta_app::schema::DeleteLockRevReq;
Expand Down Expand Up @@ -65,7 +64,7 @@ pub trait LockExt: Lock {
user,
node,
query_id,
expire_at: Utc::now().timestamp() as u64 + expire_secs,
expire_secs,
}
}

Expand All @@ -92,7 +91,7 @@ pub trait LockExt: Lock {
lock_key: self.gen_lock_key(),
revision,
acquire_lock,
expire_at: Utc::now().timestamp() as u64 + expire_secs,
expire_secs,
}
}
}
Expand Down
Loading

0 comments on commit ca0b577

Please sign in to comment.