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

Jdbc merge support #23034

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -90,6 +90,7 @@ public class CachingJdbcClient
private final Cache<ColumnsCacheKey, List<JdbcColumnHandle>> columnsCache;
private final Cache<TableListingCacheKey, List<RelationCommentMetadata>> tableCommentsCache;
private final Cache<JdbcTableHandle, TableStatistics> statisticsCache;
private final Cache<RemoteTableName, List<JdbcColumnHandle>> tablePrimaryKeysCache;

@Inject
public CachingJdbcClient(
Expand Down Expand Up @@ -138,6 +139,7 @@ public CachingJdbcClient(
columnsCache = buildCache(ticker, cacheMaximumSize, metadataCachingTtl);
tableCommentsCache = buildCache(ticker, cacheMaximumSize, metadataCachingTtl);
statisticsCache = buildCache(ticker, cacheMaximumSize, statisticsCachingTtl);
tablePrimaryKeysCache = buildCache(ticker, cacheMaximumSize, statisticsCachingTtl);
}

private static <K, V> Cache<K, V> buildCache(Ticker ticker, long cacheSize, Duration cachingTtl)
Expand Down Expand Up @@ -608,6 +610,12 @@ public OptionalInt getMaxColumnNameLength(ConnectorSession session)
return delegate.getMaxColumnNameLength(session);
}

@Override
public List<JdbcColumnHandle> getPrimaryKeys(ConnectorSession session, RemoteTableName remoteTableName)
{
return get(tablePrimaryKeysCache, remoteTableName, () -> delegate.getPrimaryKeys(session, remoteTableName));
}

public void onDataChanged(SchemaTableName table)
{
invalidateAllIf(statisticsCache, key -> key.mayReference(table));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ConnectorInsertTableHandle;
import io.trino.spi.connector.ConnectorMergeTableHandle;
import io.trino.spi.connector.ConnectorOutputMetadata;
import io.trino.spi.connector.ConnectorOutputTableHandle;
import io.trino.spi.connector.ConnectorSession;
Expand Down Expand Up @@ -71,6 +72,7 @@
import io.trino.spi.security.TrinoPrincipal;
import io.trino.spi.statistics.ComputedStatistics;
import io.trino.spi.statistics.TableStatistics;
import io.trino.spi.type.RowType;
import io.trino.spi.type.Type;

import java.sql.Types;
Expand Down Expand Up @@ -123,9 +125,9 @@
public class DefaultJdbcMetadata
implements JdbcMetadata
{
public static final String MERGE_ROW_ID = "$merge_row_id";
private static final String SYNTHETIC_COLUMN_NAME_PREFIX = "_pfgnrtd_";
private static final String DELETE_ROW_ID = "_trino_artificial_column_handle_for_delete_row_id_";
private static final String MERGE_ROW_ID = "$merge_row_id";

private final JdbcClient jdbcClient;
private final TimestampTimeZoneDomain timestampTimeZoneDomain;
Expand Down Expand Up @@ -1274,13 +1276,62 @@ public Optional<ConnectorOutputMetadata> finishInsert(
public ColumnHandle getMergeRowIdColumnHandle(ConnectorSession session, ConnectorTableHandle tableHandle)
{
verify(!isTableHandleForProcedure(tableHandle), "Not a table reference: %s", tableHandle);
// The column is used for row-level merge, which is not supported, but it's required during analysis anyway.
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
JdbcTableHandle handle = (JdbcTableHandle) tableHandle;

List<RowType.Field> primaryKeyFields = jdbcClient.getPrimaryKeys(session, handle.getRequiredNamedRelation().getRemoteTableName()).stream()
.map(columnHandle -> new RowType.Field(Optional.of(columnHandle.getColumnName()), columnHandle.getColumnType()))
.collect(toImmutableList());
if (!primaryKeyFields.isEmpty()) {
return new JdbcColumnHandle(
MERGE_ROW_ID,
new JdbcTypeHandle(Types.ROWID, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()),
RowType.from(primaryKeyFields));
}
return new JdbcColumnHandle(
// The column is used for row-level merge, which is not supported, but it's required during analysis anyway.
MERGE_ROW_ID,
new JdbcTypeHandle(Types.BIGINT, Optional.of("bigint"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()),
BIGINT);
}

@Override
public ConnectorMergeTableHandle beginMerge(ConnectorSession session, ConnectorTableHandle tableHandle, RetryMode retryMode)
{
if (retryMode != NO_RETRIES) {
throw new TrinoException(NOT_SUPPORTED, "This connector does not support MERGE with fault-tolerant execution");
}
JdbcTableHandle handle = (JdbcTableHandle) tableHandle;
checkArgument(handle.isNamedRelation(), "Merge target must be named relation table");
List<JdbcColumnHandle> primaryKeys = jdbcClient.getPrimaryKeys(session, handle.getRequiredNamedRelation().getRemoteTableName());
if (primaryKeys.isEmpty()) {
throw new TrinoException(NOT_SUPPORTED, MODIFYING_ROWS_MESSAGE);
}

SchemaTableName schemaTableName = handle.getRequiredNamedRelation().getSchemaTableName();
RemoteTableName remoteTableName = handle.getRequiredNamedRelation().getRemoteTableName();

List<JdbcColumnHandle> columns = jdbcClient.getColumns(session, schemaTableName, remoteTableName);
JdbcOutputTableHandle jdbcOutputTableHandle = (JdbcOutputTableHandle) beginInsert(
session,
new JdbcTableHandle(schemaTableName, remoteTableName, Optional.empty()),
ImmutableList.copyOf(columns),
retryMode);

return new JdbcMergeTableHandle(handle, jdbcOutputTableHandle, primaryKeys);
}

@Override
public void finishMerge(
ConnectorSession session,
ConnectorMergeTableHandle tableHandle,
List<ConnectorTableHandle> sourceTableHandles,
Collection<Slice> fragments,
Collection<ComputedStatistics> computedStatistics)
{
JdbcMergeTableHandle handle = (JdbcMergeTableHandle) tableHandle;
finishInsert(session, handle.getOutputTableHandle(), ImmutableList.of(), fragments, computedStatistics);
}

@Override
public Optional<ConnectorTableHandle> applyDelete(ConnectorSession session, ConnectorTableHandle handle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,10 @@ public OptionalInt getMaxColumnNameLength(ConnectorSession session)
{
return delegate().getMaxColumnNameLength(session);
}

@Override
public List<JdbcColumnHandle> getPrimaryKeys(ConnectorSession session, RemoteTableName remoteTableName)
{
return delegate().getPrimaryKeys(session, remoteTableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,13 @@ default OptionalInt getMaxColumnNameLength(ConnectorSession session)
{
return OptionalInt.empty();
}

/**
* Retrieves primary keys for remote table used in the merge process.
* These primary keys are unique identifiers of each row in table, commonly mapping to primary or unique keys in the database.
*/
default List<JdbcColumnHandle> getPrimaryKeys(ConnectorSession session, RemoteTableName remoteTableName)
{
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.trino.spi.connector.ConnectorCapabilities;
import io.trino.spi.connector.ConnectorMetadata;
import io.trino.spi.connector.ConnectorPageSinkProvider;
import io.trino.spi.connector.ConnectorRecordSetProvider;
import io.trino.spi.connector.ConnectorPageSourceProvider;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorSplitManager;
import io.trino.spi.connector.ConnectorTransactionHandle;
Expand All @@ -46,7 +46,7 @@ public class JdbcConnector
{
private final LifeCycleManager lifeCycleManager;
private final ConnectorSplitManager jdbcSplitManager;
private final ConnectorRecordSetProvider jdbcRecordSetProvider;
private final ConnectorPageSourceProvider jdbcPageSourceProvider;
private final ConnectorPageSinkProvider jdbcPageSinkProvider;
private final Optional<ConnectorAccessControl> accessControl;
private final Set<Procedure> procedures;
Expand All @@ -59,7 +59,7 @@ public class JdbcConnector
public JdbcConnector(
LifeCycleManager lifeCycleManager,
ConnectorSplitManager jdbcSplitManager,
ConnectorRecordSetProvider jdbcRecordSetProvider,
ConnectorPageSourceProvider jdbcPageSourceProvider,
ConnectorPageSinkProvider jdbcPageSinkProvider,
Optional<ConnectorAccessControl> accessControl,
Set<Procedure> procedures,
Expand All @@ -70,7 +70,7 @@ public JdbcConnector(
{
this.lifeCycleManager = requireNonNull(lifeCycleManager, "lifeCycleManager is null");
this.jdbcSplitManager = requireNonNull(jdbcSplitManager, "jdbcSplitManager is null");
this.jdbcRecordSetProvider = requireNonNull(jdbcRecordSetProvider, "jdbcRecordSetProvider is null");
this.jdbcPageSourceProvider = requireNonNull(jdbcPageSourceProvider, "jdbcRecordSetProvider is null");
this.jdbcPageSinkProvider = requireNonNull(jdbcPageSinkProvider, "jdbcPageSinkProvider is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.procedures = ImmutableSet.copyOf(requireNonNull(procedures, "procedures is null"));
Expand Down Expand Up @@ -115,9 +115,9 @@ public ConnectorSplitManager getSplitManager()
}

@Override
public ConnectorRecordSetProvider getRecordSetProvider()
public ConnectorPageSourceProvider getPageSourceProvider()
{
return jdbcRecordSetProvider;
return jdbcPageSourceProvider;
}

@Override
Expand Down
Loading