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

Merged
merged 2 commits into from
Dec 9, 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
12 changes: 12 additions & 0 deletions docs/src/main/sphinx/connector/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ catalog named `sales` using the configured connector.
```{include} non-transactional-insert.fragment
```

### Non-transactional MERGE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mosabua Would you mind to have a look?


The connector supports adding rows using {doc}`MERGE statements </sql/merge>`.
However, the connector only support merge modifying directly to the target
table at current, to use merge you need to set the `merge.non-transactional-merge.enabled`
catalog property or the corresponding `non_transactional_merge_enabled` catalog session property to
`true`.

Note that with this property enabled, data can be corrupted in rare cases where
exceptions occur during the merge operation. With transactions disabled, no
rollback can be performed.

(postgresql-fte-support)=
### Fault-tolerant execution support

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,12 @@ public boolean isLimitGuaranteed(ConnectorSession session)
throw new TrinoException(JDBC_ERROR, "limitFunction() is implemented without isLimitGuaranteed()");
}

@Override
public boolean supportsMerge()
{
return false;
}

@Override
public String quoted(String name)
{
Expand Down
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 @@ -352,6 +354,12 @@ public boolean isLimitGuaranteed(ConnectorSession session)
return delegate.isLimitGuaranteed(session);
}

@Override
public boolean supportsMerge()
{
return delegate.supportsMerge();
}

@Override
public Optional<JdbcTableHandle> getTableHandle(ConnectorSession session, SchemaTableName schemaTableName)
{
Expand Down Expand Up @@ -608,6 +616,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 @@ -112,6 +114,7 @@
import static io.trino.plugin.jdbc.JdbcMetadataSessionProperties.isJoinPushdownEnabled;
import static io.trino.plugin.jdbc.JdbcMetadataSessionProperties.isTopNPushdownEnabled;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.isNonTransactionalInsert;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.isNonTransactionalMerge;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.connector.RetryMode.NO_RETRIES;
import static io.trino.spi.connector.RowChangeParadigm.CHANGE_ONLY_UPDATED_COLUMNS;
Expand All @@ -123,9 +126,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 +1277,72 @@ 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, Map<Integer, Collection<ColumnHandle>> updateColumnHandles, RetryMode retryMode)
{
if (retryMode != NO_RETRIES) {
throw new TrinoException(NOT_SUPPORTED, "This connector does not support MERGE with fault-tolerant execution");
}

if (!jdbcClient.supportsMerge()) {
throw new TrinoException(NOT_SUPPORTED, MODIFYING_ROWS_MESSAGE);
}

if (!isNonTransactionalMerge(session)) {
throw new TrinoException(NOT_SUPPORTED, "Non-transactional MERGE is disabled");
}

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, "The connector can not perform merge on the target table without primary keys");
}

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, columns, updateColumnHandles);
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
}

@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 @@ -353,6 +353,12 @@ public boolean isLimitGuaranteed(ConnectorSession session)
return delegate().isLimitGuaranteed(session);
}

@Override
public boolean supportsMerge()
{
return delegate().supportsMerge();
}

@Override
public Optional<String> getTableComment(ResultSet resultSet)
throws SQLException
Expand Down Expand Up @@ -497,4 +503,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 @@ -166,6 +166,8 @@ Optional<PreparedQuery> legacyImplementJoin(

boolean isLimitGuaranteed(ConnectorSession session);

boolean supportsMerge();
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved

default Optional<String> getTableComment(ResultSet resultSet)
throws SQLException
{
Expand Down Expand Up @@ -262,4 +264,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
Loading