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

DDL for Table Constraints #20384

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.presto.common.resourceGroups.QueryType;
import com.facebook.presto.sql.tree.AddColumn;
import com.facebook.presto.sql.tree.AddConstraint;
import com.facebook.presto.sql.tree.AlterFunction;
import com.facebook.presto.sql.tree.Analyze;
import com.facebook.presto.sql.tree.Call;
Expand All @@ -32,6 +33,7 @@
import com.facebook.presto.sql.tree.DescribeInput;
import com.facebook.presto.sql.tree.DescribeOutput;
import com.facebook.presto.sql.tree.DropColumn;
import com.facebook.presto.sql.tree.DropConstraint;
import com.facebook.presto.sql.tree.DropFunction;
import com.facebook.presto.sql.tree.DropMaterializedView;
import com.facebook.presto.sql.tree.DropRole;
Expand Down Expand Up @@ -126,6 +128,8 @@ private StatementUtils() {}
builder.put(RenameColumn.class, QueryType.DATA_DEFINITION);
builder.put(DropColumn.class, QueryType.DATA_DEFINITION);
builder.put(DropTable.class, QueryType.DATA_DEFINITION);
builder.put(DropConstraint.class, QueryType.DATA_DEFINITION);
builder.put(AddConstraint.class, QueryType.DATA_DEFINITION);
builder.put(CreateView.class, QueryType.DATA_DEFINITION);
builder.put(TruncateTable.class, QueryType.DATA_DEFINITION);
builder.put(DropView.class, QueryType.DATA_DEFINITION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public void createTable(ConnectorSession session, ConnectorTableMetadata tableMe
metastore.createTable(
metastoreContext(session),
table,
principalPrivileges);
principalPrivileges,
emptyList());
}

@Override
Expand Down
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/language/reserved.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Keyword SQL:2016 SQL-92
``ORDER`` reserved reserved
``OUTER`` reserved reserved
``PREPARE`` reserved reserved
``PRIMARY`` reserved reserved
``RECURSIVE`` reserved
``RIGHT`` reserved reserved
``ROLLUP`` reserved
Expand Down
18 changes: 18 additions & 0 deletions presto-docs/src/main/sphinx/sql/alter-table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Synopsis
ALTER TABLE [ IF EXISTS ] name ADD COLUMN [ IF NOT EXISTS ] column_name data_type [ COMMENT comment ] [ WITH ( property_name = expression [, ...] ) ]
ALTER TABLE [ IF EXISTS ] name DROP COLUMN column_name
ALTER TABLE [ IF EXISTS ] name RENAME COLUMN [ IF EXISTS ] column_name TO new_column_name
ALTER TABLE [ IF EXISTS ] name ADD [ CONSTRAINT constraint_name ] { PRIMARY KEY | UNIQUE } ( { column_name [, ...] } ) [ { ENABLED | DISABLED } ] [ [ NOT ] RELY ] [ [ NOT ] ENFORCED } ]
ALTER TABLE [ IF EXISTS ] name DROP CONSTRAINT [ IF EXISTS ] constraint_name

Description
-----------
Expand Down Expand Up @@ -58,6 +60,22 @@ Rename column ``id`` to ``user_id`` in the ``users`` table if table ``users`` an

ALTER TABLE IF EXISTS users RENAME column IF EXISTS id to user_id;

Add constraint ``pk`` on column ``user_id`` in the ``users`` table::

ALTER TABLE users ADD CONSTRAINT pk PRIMARY KEY (user_id);

Add unnamed, disabled unique constraint on columns ``first_name`` and ``last_name`` in the ``users`` table if table ``users`` exists::

ALTER TABLE IF EXISTS users ADD UNIQUE (first_name, last_name) DISABLED;

Drop constraint ``pk`` from the ``users`` table::

ALTER TABLE users DROP CONSTRAINT pk;

Drop constraint ``pk`` from the ``users`` table if table ``users`` exists and constraint ``pk`` exists::

ALTER TABLE IF EXISTS users DROP CONSTRAINT IF EXISTS pk;

See Also
--------

Expand Down
8 changes: 5 additions & 3 deletions presto-docs/src/main/sphinx/sql/create-table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Synopsis
CREATE TABLE [ IF NOT EXISTS ]
table_name (
{ column_name data_type [ COMMENT comment ] [ WITH ( property_name = expression [, ...] ) ]
| LIKE existing_table_name [ { INCLUDING | EXCLUDING } PROPERTIES ] }
| LIKE existing_table_name [ { INCLUDING | EXCLUDING } PROPERTIES ]
| [ CONSTRAINT constraint_name ] { PRIMARY KEY | UNIQUE } ( { column_name [, ...] } ) [ { ENABLED | DISABLED } ] [ [ NOT ] RELY ] [ [ NOT ] ENFORCED ] }
[, ...]
)
[ COMMENT table_comment ]
Expand Down Expand Up @@ -61,13 +62,14 @@ Create a new table ``orders``::
WITH (format = 'ORC')

Create the table ``orders`` if it does not already exist, adding a table comment
and a column comment::
and a column comment and a primary key constraint on column ``orderkey``::

CREATE TABLE IF NOT EXISTS orders (
orderkey bigint,
orderstatus varchar,
totalprice double COMMENT 'Price in cents.',
orderdate date
orderdate date,
PRIMARY KEY (orderkey)
)
COMMENT 'A table to keep track of orders.'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.hive;

import com.facebook.presto.spi.PrestoException;

import java.util.Optional;

import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS;
import static java.lang.String.format;

public class TableConstraintAlreadyExistsException
extends PrestoException
{
private final Optional<String> constraintName;

public TableConstraintAlreadyExistsException(Optional<String> constraintName)
{
this(constraintName, format("Constraint already exists: '%s'", constraintName.get()));
}

public TableConstraintAlreadyExistsException(Optional<String> constraintName, String message)
{
this(constraintName, message, null);
}

public TableConstraintAlreadyExistsException(Optional<String> constraintName, String message, Throwable cause)
{
super(ALREADY_EXISTS, message, cause);
this.constraintName = constraintName;
}

public Optional<String> getConstraintName()
{
return constraintName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,10 @@ protected void invalidateDatabase(String databaseName)
}

@Override
public MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges)
public MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges, List<TableConstraint<String>> constraints)
{
try {
return delegate.createTable(metastoreContext, table, principalPrivileges);
return delegate.createTable(metastoreContext, table, principalPrivileges, constraints);
}
finally {
invalidateTable(table.getDatabaseName(), table.getTableName());
Expand Down Expand Up @@ -1016,6 +1016,28 @@ public void setPartitionLeases(MetastoreContext metastoreContext, String databas
delegate.setPartitionLeases(metastoreContext, databaseName, tableName, partitionNameToLocation, leaseDuration);
}

@Override
public MetastoreOperationResult dropConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, String constraintName)
{
try {
return delegate.dropConstraint(metastoreContext, databaseName, tableName, constraintName);
}
finally {
invalidateTable(databaseName, tableName);
}
}

@Override
public MetastoreOperationResult addConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, TableConstraint<String> tableConstraint)
{
try {
return delegate.addConstraint(metastoreContext, databaseName, tableName, tableConstraint);
}
finally {
invalidateTable(databaseName, tableName);
}
}

@Override
public Optional<Long> lock(MetastoreContext metastoreContext, String databaseName, String tableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ default Optional<Table> getTable(MetastoreContext metastoreContext, HiveTableHan

void renameDatabase(MetastoreContext metastoreContext, String databaseName, String newDatabaseName);

MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges);
MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges, List<TableConstraint<String>> constraints);

void dropTable(MetastoreContext metastoreContext, String databaseName, String tableName, boolean deleteData);

Expand Down Expand Up @@ -161,4 +161,8 @@ default int getPartitionCommitBatchSize()
{
return 10;
}

MetastoreOperationResult dropConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, String constraintName);

MetastoreOperationResult addConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, TableConstraint<String> tableConstraint);
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ public void renameDatabase(MetastoreContext metastoreContext, String databaseNam
}

@Override
public MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges)
public MetastoreOperationResult createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges, List<TableConstraint<String>> constraints)
{
verifyRecordingMode();
return delegate.createTable(metastoreContext, table, principalPrivileges);
return delegate.createTable(metastoreContext, table, principalPrivileges, constraints);
}

@Override
Expand Down Expand Up @@ -499,6 +499,20 @@ public void setPartitionLeases(MetastoreContext metastoreContext, String databas
throw new UnsupportedOperationException("setPartitionLeases is not supported in RecordingHiveMetastore");
}

@Override
public MetastoreOperationResult dropConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, String constraintName)
{
verifyRecordingMode();
return delegate.dropConstraint(metastoreContext, databaseName, tableName, constraintName);
}

@Override
public MetastoreOperationResult addConstraint(MetastoreContext metastoreContext, String databaseName, String tableName, TableConstraint<String> tableConstraint)
{
verifyRecordingMode();
return delegate.addConstraint(metastoreContext, databaseName, tableName, tableConstraint);
}

private <K, V> V loadValue(Cache<K, V> cache, K key, Supplier<V> valueSupplier)
{
if (replay) {
Expand Down
Loading
Loading