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

Support TRUNCATE TABLE statement #8921

Closed
ebyhr opened this issue Aug 19, 2021 · 6 comments · Fixed by #8932
Closed

Support TRUNCATE TABLE statement #8921

ebyhr opened this issue Aug 19, 2021 · 6 comments · Fixed by #8932
Assignees
Labels
enhancement New feature or request

Comments

@ebyhr
Copy link
Member

ebyhr commented Aug 19, 2021

This statement is DML (not DDL) in ANSI SQL. The keyword TRUNCATE is a part of reserved words.

<truncate table statement> ::=
  TRUNCATE TABLE <target table>  [ <identity column restart option>  ]

<identity column restart option> ::=
    CONTINUE IDENTITY
  | RESTART IDENTITY

Output of other databases:
MySQL

mysql> truncate table t1;
Query OK, 0 rows affected (0.01 sec)

PostgreSQL

postgres=# truncate table test.t1;
TRUNCATE TABLE

MS SQL Server

mssql> truncate table t1;
OK

Executed in 0 ms

Oracle

SQL> truncate table t1;
 
Table truncated.

Cassandra

cqlsh> truncate table test.t1;
cqlsh> 

Relates to https://trinodb.slack.com/archives/CFLB9AMBN/p1629328499266100

@ebyhr ebyhr added the enhancement New feature or request label Aug 19, 2021
@ebyhr ebyhr self-assigned this Aug 19, 2021
@martint
Copy link
Member

martint commented Aug 26, 2021

Here are the syntax rules from the spec:

Uploading Screen Shot 2021-08-26 at 8.15.16 AM.png…

In particular, note that:

  • T shall be a base table and shall not be a system-versioned table.
  • T shall not be identified by the name of the referenced table in any referential constraint descriptor.

@martint
Copy link
Member

martint commented Aug 26, 2021

Also, the spec says:

  1. If no rows are deleted from T, then a completion condition is raised: no data.

@ebyhr
Copy link
Member Author

ebyhr commented Aug 26, 2021

@martint Thank you sharing the details. I will add some relevant tests. Could you upload the screenshot again? I can't see that.

@ebyhr
Copy link
Member Author

ebyhr commented Aug 26, 2021

  1. If no rows are deleted from T, then a completion condition is raised: no data.

We may need to check if the table is empty or not beforehand to achieve this. MySQL, PostgreSQL and Oracle always return 0 in Statement.executeUpdate() method and doesn't throw any exception regardless of deleted rows.

@findepi
Copy link
Member

findepi commented Aug 26, 2021

does "completion condition is raised: no data" mean "query should fail"?

@martint
Copy link
Member

martint commented Aug 27, 2021

does "completion condition is raised: no data" mean "query should fail"?

It looks like the answer is no, it shouldn't fail. Completion/exception conditions are parallel and somewhat orthogonal to query query success or failure. They are just a classification of the resulting SQLSTATE of the query.

If a condition is raised that causes a statement to have no effect other
than that associated with raising the condition (that is, not a completion
condition), then the condition is said to be an exception condition or exception.
If a condition is raised that permits a statement to have an effect other than
that associated with raising the condition (corresponding to an SQLSTATE
class code of successful completion, warning, or no data), then the condition
is said to be a completion condition.

The completion condition warning is broadly defined as completion in which the effects are correct, but there is reason to caution the user about those effects. It is raised for implementation-defined conditions as well as conditions specified in this part of ISO/IEC 9075. The completion condition no data has special significance and is used to indicate an empty result. The completion condition successful completion is defined to indicate a completion condition that does not correspond to warning or no data. This includes conditions in which the SQLSTATE subclass provides implementation-defined information of a non-cautionary nature.

For the purpose of choosing status parameter values to be returned, exception conditions for transaction rollback have precedence over exception conditions for statement failure. Similarly, the completion condition no data has precedence over the completion condition warning, which has precedence over the completion condition successful completion. All exception conditions have precedence over all completion conditions. The values assigned to SQLSTATE shall obey these precedence requirements.

and elsewhere:

  1. Case:
    a) If S executed successfully, then either a completion condition is raised: successful completion, or a
    completion condition is raised: warning, or a completion condition is raised: no data.
    b) If S did not execute successfully, then all changes made to SQL-data or schemas by the execution of
    S are canceled and an exception condition is raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

3 participants