Skip to content

Commit

Permalink
Add layout to ConnectorMetadata beginCreateTable
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Feb 3, 2016
1 parent 0f4b85a commit 7c075e9
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void renameTable(ConnectorSession session, ConnectorTableHandle tableHand
@Override
public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
ConnectorOutputTableHandle outputTableHandle = beginCreateTable(session, tableMetadata);
ConnectorOutputTableHandle outputTableHandle = beginCreateTable(session, tableMetadata, Optional.empty());
finishCreateTable(session, outputTableHandle, ImmutableList.of());
}

Expand All @@ -170,7 +170,7 @@ public Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSession conn
}

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
int splitCount = (Integer) tableMetadata.getProperties().get(SPLIT_COUNT_PROPERTY);
int pagesPerSplit = (Integer) tableMetadata.getProperties().get(PAGES_PER_SPLIT_PROPERTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.facebook.presto.testing.TestingConnectorSession.SESSION;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -50,7 +51,8 @@ public void tableIsCreatedAfterCommits()
schemaTableName,
ImmutableList.of(),
tableProperties,
null));
null),
Optional.empty());

assertThatNoTableIsCreated();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.facebook.presto.cassandra.util.CassandraCqlUtils;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorNewTableLayout;
import com.facebook.presto.spi.ConnectorOutputTableHandle;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.ConnectorTableHandle;
Expand Down Expand Up @@ -267,7 +268,7 @@ public void renameTable(ConnectorSession session, ConnectorTableHandle tableHand
}

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
checkArgument(!isNullOrEmpty(tableMetadata.getOwner()), "Table owner is null or empty");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public interface Metadata
/**
* Begin the atomic creation of a table with data.
*/
OutputTableHandle beginCreateTable(Session session, String catalogName, TableMetadata tableMetadata);
OutputTableHandle beginCreateTable(Session session, String catalogName, TableMetadata tableMetadata, Optional<NewTableLayout> layout);

/**
* Finish a table creation with data after the data is written.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,14 @@ public Optional<NewTableLayout> getNewTableLayout(Session session, String catalo
}

@Override
public OutputTableHandle beginCreateTable(Session session, String catalogName, TableMetadata tableMetadata)
public OutputTableHandle beginCreateTable(Session session, String catalogName, TableMetadata tableMetadata, Optional<NewTableLayout> layout)
{
ConnectorEntry entry = connectorsByCatalog.get(catalogName);
checkArgument(entry != null, "Catalog %s does not exist", catalogName);
ConnectorMetadata metadata = entry.getMetadataForWrite(session);
ConnectorTransactionHandle transactionHandle = entry.getTransactionHandle(session);
ConnectorSession connectorSession = session.toConnectorSession(entry.getCatalog());
ConnectorOutputTableHandle handle = metadata.beginCreateTable(connectorSession, tableMetadata.getMetadata());
ConnectorOutputTableHandle handle = metadata.beginCreateTable(connectorSession, tableMetadata.getMetadata(), layout.map(NewTableLayout::getLayout));
return new OutputTableHandle(entry.getConnectorId(), transactionHandle, handle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private RelationPlan createTableCreationPlan(Analysis analysis)
return createTableWriterPlan(
analysis,
plan,
new CreateName(destination.getCatalogName(), tableMetadata),
new CreateName(destination.getCatalogName(), tableMetadata, newTableLayout),
tableMetadata.getVisibleColumnNames(),
newTableLayout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private TableWriterNode.WriterTarget createWriterTarget(TableWriterNode.WriterTa
// TODO: begin these operations in pre-execution step, not here
if (target instanceof TableWriterNode.CreateName) {
TableWriterNode.CreateName create = (TableWriterNode.CreateName) target;
return new TableWriterNode.CreateHandle(metadata.beginCreateTable(session, create.getCatalog(), create.getTableMetadata()));
return new TableWriterNode.CreateHandle(metadata.beginCreateTable(session, create.getCatalog(), create.getTableMetadata(), create.getLayout()));
}
if (target instanceof TableWriterNode.InsertReference) {
TableWriterNode.InsertReference insert = (TableWriterNode.InsertReference) target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.sql.planner.plan;

import com.facebook.presto.metadata.InsertTableHandle;
import com.facebook.presto.metadata.NewTableLayout;
import com.facebook.presto.metadata.OutputTableHandle;
import com.facebook.presto.metadata.TableHandle;
import com.facebook.presto.metadata.TableMetadata;
Expand Down Expand Up @@ -145,11 +146,13 @@ public static class CreateName
{
private final String catalog;
private final TableMetadata tableMetadata;
private final Optional<NewTableLayout> layout;

public CreateName(String catalog, TableMetadata tableMetadata)
public CreateName(String catalog, TableMetadata tableMetadata, Optional<NewTableLayout> layout)
{
this.catalog = requireNonNull(catalog, "catalog is null");
this.tableMetadata = requireNonNull(tableMetadata, "tableMetadata is null");
this.layout = requireNonNull(layout, "layout is null");
}

public String getCatalog()
Expand All @@ -162,6 +165,11 @@ public TableMetadata getTableMetadata()
return tableMetadata;
}

public Optional<NewTableLayout> getLayout()
{
return layout;
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public final Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSessio
}

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
checkState(rollbackAction.get() == null, "Cannot begin a new write while in an existing one");
ConnectorOutputTableHandle outputTableHandle = metadata.beginCreateTable(session, tableMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorInsertTableHandle;
import com.facebook.presto.spi.ConnectorNewTableLayout;
import com.facebook.presto.spi.ConnectorOutputTableHandle;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.ConnectorTableHandle;
Expand Down Expand Up @@ -256,7 +257,7 @@ public ConnectorTableLayout getTableLayout(ConnectorSession session, ConnectorTa
@Override
public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
{
finishCreateTable(session, beginCreateTable(session, tableMetadata), ImmutableList.of());
finishCreateTable(session, beginCreateTable(session, tableMetadata, Optional.empty()), ImmutableList.of());
}

@Override
Expand Down Expand Up @@ -303,7 +304,7 @@ public void renameColumn(ConnectorSession session, ConnectorTableHandle tableHan
}

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
ImmutableList.Builder<RaptorColumnHandle> columnHandles = ImmutableList.builder();
ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -394,7 +395,7 @@ public void testTransactionTableWrite()
{
// start table creation
long transactionId = 1;
ConnectorOutputTableHandle outputHandle = metadata.beginCreateTable(SESSION, getOrdersTable());
ConnectorOutputTableHandle outputHandle = metadata.beginCreateTable(SESSION, getOrdersTable(), Optional.empty());

// transaction is in progress
assertTrue(transactionExists(transactionId));
Expand Down Expand Up @@ -479,7 +480,7 @@ public void testTransactionAbort()
{
// start table creation
long transactionId = 1;
ConnectorOutputTableHandle outputHandle = metadata.beginCreateTable(SESSION, getOrdersTable());
ConnectorOutputTableHandle outputHandle = metadata.beginCreateTable(SESSION, getOrdersTable(), Optional.empty());

// transaction is in progress
assertTrue(transactionExists(transactionId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ default Optional<ConnectorNewTableLayout> getNewTableLayout(ConnectorSession ses
/**
* Begin the atomic creation of a table with data.
*/
default ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata)
default ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout)
{
throw new PrestoException(NOT_SUPPORTED, "This connector does not support creating tables with data");
}
Expand Down

0 comments on commit 7c075e9

Please sign in to comment.