Skip to content
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
28 changes: 25 additions & 3 deletions core/trino-main/src/main/java/io/trino/sql/analyzer/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.trino.spi.connector.CatalogHandle.CatalogVersion;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnSchema;
import io.trino.spi.connector.ConnectorTableHandle;
import io.trino.spi.connector.ConnectorTableLocation;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.connector.ConnectorTransactionHandle;
import io.trino.spi.eventlistener.BaseViewReferenceInfo;
Expand Down Expand Up @@ -667,7 +669,18 @@ public void registerTable(
rowFilterScopes.isEmpty() &&
columnMaskScopes.isEmpty(),
viewText,
referenceChain));
referenceChain,
handle
.map(TableHandle::connectorHandle)
.flatMap(Analysis::resolveTableLocation)));
}

private static Optional<String> resolveTableLocation(ConnectorTableHandle catalogHandle)
{
if (catalogHandle instanceof ConnectorTableLocation tableLocation) {
return tableLocation.getTableLocation();
}
return Optional.empty();
}

public Set<ResolvedFunction> getResolvedFunctions()
Expand Down Expand Up @@ -1233,7 +1246,8 @@ public List<TableInfo> getReferencedTables()
columns,
info.isDirectlyReferenced(),
info.getViewText(),
info.getReferenceChain());
info.getReferenceChain(),
info.getLocation());
})
.collect(toImmutableList());
}
Expand Down Expand Up @@ -2134,6 +2148,7 @@ private static class TableEntry
private final boolean directlyReferenced;
private final Optional<String> viewText;
private final List<TableReferenceInfo> referenceChain;
private final Optional<String> location;

public TableEntry(
Optional<TableHandle> handle,
Expand All @@ -2142,7 +2157,8 @@ public TableEntry(
Scope accessControlScope,
boolean directlyReferenced,
Optional<String> viewText,
Iterable<TableReferenceInfo> referenceChain)
Iterable<TableReferenceInfo> referenceChain,
Optional<String> location)
{
this.handle = requireNonNull(handle, "handle is null");
this.name = requireNonNull(name, "name is null");
Expand All @@ -2151,6 +2167,7 @@ public TableEntry(
this.directlyReferenced = directlyReferenced;
this.viewText = requireNonNull(viewText, "viewText is null");
this.referenceChain = ImmutableList.copyOf(requireNonNull(referenceChain, "referenceChain is null"));
this.location = requireNonNull(location, "location is null");
}

public Optional<TableHandle> getHandle()
Expand Down Expand Up @@ -2187,6 +2204,11 @@ public List<TableReferenceInfo> getReferenceChain()
{
return referenceChain;
}

public Optional<String> getLocation()
{
return location;
}
}

public static class SourceColumn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 io.trino.spi.connector;

import java.util.Optional;

/**
* Represents a physical table location (i.e. database coordinates or storage location), if applicable.
* This is used for lineage tracking and other features that need to know the physical location of the dataset.
*/
public interface ConnectorTableLocation
{
default Optional<String> getTableLocation()
{
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class TableInfo
private final boolean directlyReferenced;
private final Optional<String> viewText;
private final List<TableReferenceInfo> referenceChain;
private final Optional<String> location;

@JsonCreator
@Unstable
Expand All @@ -49,7 +50,8 @@ public TableInfo(
List<ColumnInfo> columns,
boolean directlyReferenced,
Optional<String> viewText,
List<TableReferenceInfo> referenceChain)
List<TableReferenceInfo> referenceChain,
Optional<String> location)
{
this.catalog = requireNonNull(catalog, "catalog is null");
this.schema = requireNonNull(schema, "schema is null");
Expand All @@ -60,6 +62,7 @@ public TableInfo(
this.directlyReferenced = directlyReferenced;
this.viewText = requireNonNull(viewText, "viewText is null");
this.referenceChain = List.copyOf(requireNonNull(referenceChain, "referenceChain is null"));
this.location = requireNonNull(location, "location is null");
}

@JsonProperty
Expand Down Expand Up @@ -145,4 +148,10 @@ public List<TableReferenceInfo> getReferenceChain()
{
return referenceChain;
}

@JsonProperty
public Optional<String> getLocation()
{
return location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public Optional<JdbcTableHandle> getTableHandle(ConnectorSession session, Schema
try (ResultSet resultSet = getTables(connection, Optional.of(remoteSchema), Optional.of(remoteTable))) {
List<JdbcTableHandle> tableHandles = new ArrayList<>();
while (resultSet.next()) {
tableHandles.add(new JdbcTableHandle(schemaTableName, getRemoteTable(resultSet), getTableComment(resultSet)));
tableHandles.add(new JdbcTableHandle(schemaTableName, getRemoteTable(resultSet), getTableComment(resultSet), connectionFactory.getConnectionUrl()));
}
if (tableHandles.isEmpty()) {
return Optional.empty();
Expand Down Expand Up @@ -1229,7 +1229,7 @@ public JdbcMergeTableHandle beginMerge(

List<JdbcColumnHandle> columns = getColumns(session, schemaTableName, remoteTableName);

JdbcTableHandle plainTable = new JdbcTableHandle(schemaTableName, remoteTableName, Optional.empty());
JdbcTableHandle plainTable = new JdbcTableHandle(schemaTableName, remoteTableName, Optional.empty(), handle.getTableLocation());

JdbcOutputTableHandle outputTableHandle = beginInsertTable(session, plainTable, columns);
rollbackActions.add(() -> rollbackCreateTable(session, outputTableHandle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;

@FunctionalInterface
public interface ConnectionFactory
Expand All @@ -26,6 +27,11 @@ public interface ConnectionFactory
Connection openConnection(ConnectorSession session)
throws SQLException;

default Optional<String> getConnectionUrl()
{
return Optional.empty();
}

@Override
@PreDestroy
default void close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
handle.getOtherReferencedTables(),
handle.getNextSyntheticColumnId(),
handle.getAuthorization(),
handle.getUpdateAssignments());
handle.getUpdateAssignments(),
handle.getTableLocation());

return Optional.of(new ConstraintApplicationResult<>(handle, remainingFilter, remainingExpression, precalculateStatisticsForPushdown));
}
Expand All @@ -308,7 +309,8 @@ private JdbcTableHandle flushAttributesAsQuery(ConnectorSession session, JdbcTab
handle.getAllReferencedTables(),
handle.getNextSyntheticColumnId(),
handle.getAuthorization(),
handle.getUpdateAssignments());
handle.getUpdateAssignments(),
handle.getTableLocation());
}

@Override
Expand Down Expand Up @@ -367,7 +369,8 @@ public Optional<ProjectionApplicationResult<ConnectorTableHandle>> applyProjecti
handle.getOtherReferencedTables(),
handle.getNextSyntheticColumnId(),
handle.getAuthorization(),
handle.getUpdateAssignments()),
handle.getUpdateAssignments(),
handle.getTableLocation()),
projections,
assignments.entrySet().stream()
.map(assignment -> new Assignment(
Expand Down Expand Up @@ -927,7 +930,8 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
handle.getOtherReferencedTables(),
handle.getNextSyntheticColumnId(),
handle.getAuthorization(),
handle.getUpdateAssignments());
handle.getUpdateAssignments(),
handle.getTableLocation());

return Optional.of(new LimitApplicationResult<>(handle, jdbcClient.isLimitGuaranteed(session), precalculateStatisticsForPushdown));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Properties;

import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -52,6 +53,12 @@ public DriverConnectionFactory(
this.dataSource = new TracingDataSource(requireNonNull(openTelemetry, "openTelemetry is null"), driver, connectionUrl);
}

@Override
public Optional<String> getConnectionUrl()
{
return Optional.of(connectionUrl);
}

@Override
public Connection openConnection(ConnectorSession session)
throws SQLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private MergeJdbcPageSource createMergePageSource(
tableHandle.getOtherReferencedTables(),
tableHandle.getNextSyntheticColumnId(),
tableHandle.getAuthorization(),
tableHandle.getUpdateAssignments());
tableHandle.getUpdateAssignments(),
tableHandle.getTableLocation());
return new MergeJdbcPageSource(
createPageSource(session, jdbcSplit, newTableHandle, scanColumns),
columnAdaptationsBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableSet;
import io.trino.plugin.jdbc.expression.ParameterizedExpression;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorTableLocation;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.expression.Constant;
import io.trino.spi.predicate.TupleDomain;
Expand All @@ -38,6 +39,7 @@

public final class JdbcTableHandle
extends BaseJdbcConnectorTableHandle
implements ConnectorTableLocation
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we have a SPI specific API in ConnectorMetadata which would resolve location based on ConnectorTableHandle ?

{
private final JdbcRelationHandle relationHandle;

Expand All @@ -63,8 +65,14 @@ public final class JdbcTableHandle
private final int nextSyntheticColumnId;
private final Optional<String> authorization;
private final List<JdbcAssignmentItem> updateAssignments;
private final Optional<String> connectionUrl;

public JdbcTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTableName, Optional<String> comment)
{
this(schemaTableName, remoteTableName, comment, Optional.empty());
}

public JdbcTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTableName, Optional<String> comment, Optional<String> connectionUrl)
{
this(
new JdbcNamedRelationHandle(schemaTableName, remoteTableName, comment),
Expand All @@ -76,7 +84,8 @@ public JdbcTableHandle(SchemaTableName schemaTableName, RemoteTableName remoteTa
Optional.of(ImmutableSet.of()),
0,
Optional.empty(),
ImmutableList.of());
ImmutableList.of(),
connectionUrl);
}

@JsonCreator
Expand All @@ -90,7 +99,8 @@ public JdbcTableHandle(
@JsonProperty("otherReferencedTables") Optional<Set<SchemaTableName>> otherReferencedTables,
@JsonProperty("nextSyntheticColumnId") int nextSyntheticColumnId,
@JsonProperty("authorization") Optional<String> authorization,
@JsonProperty("updateAssignments") List<JdbcAssignmentItem> updateAssignments)
@JsonProperty("updateAssignments") List<JdbcAssignmentItem> updateAssignments,
@JsonProperty("connectionUrl") Optional<String> connectionUrl)
{
this.relationHandle = requireNonNull(relationHandle, "relationHandle is null");
this.constraint = requireNonNull(constraint, "constraint is null");
Expand All @@ -103,6 +113,7 @@ public JdbcTableHandle(
this.nextSyntheticColumnId = nextSyntheticColumnId;
this.authorization = requireNonNull(authorization, "authorization is null");
this.updateAssignments = requireNonNull(updateAssignments, "updateAssignments is null");
this.connectionUrl = requireNonNull(connectionUrl, "connectionUrl is null");
}

public JdbcTableHandle intersectedWithConstraint(TupleDomain<ColumnHandle> newConstraint)
Expand All @@ -117,7 +128,8 @@ public JdbcTableHandle intersectedWithConstraint(TupleDomain<ColumnHandle> newCo
otherReferencedTables,
nextSyntheticColumnId,
authorization,
updateAssignments);
updateAssignments,
connectionUrl);
}

public JdbcTableHandle withAssignments(Map<ColumnHandle, Constant> assignments)
Expand All @@ -137,7 +149,8 @@ public JdbcTableHandle withAssignments(Map<ColumnHandle, Constant> assignments)
.map(e -> {
return new JdbcAssignmentItem((JdbcColumnHandle) e.getKey(), new QueryParameter(e.getValue().getType(), Optional.ofNullable(e.getValue().getValue())));
})
.collect(toImmutableList()));
.collect(toImmutableList()),
connectionUrl);
}

public JdbcNamedRelationHandle asPlainTable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ public void testApplyTableScanRedirect()
Optional.of(ImmutableSet.of()),
0,
Optional.empty(),
ImmutableList.of());
ImmutableList.of(),
baseTableHandle.getTableLocation());

// redirection is not applied if constraintExpressions is present
assertThat(metadata.applyTableScanRedirect(session, filterWithConstraintExpressionResult)).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ private ConnectorPageSource getCursor(JdbcTableHandle jdbcTableHandle, List<Colu
jdbcTableHandle.getOtherReferencedTables(),
jdbcTableHandle.getNextSyntheticColumnId(),
Optional.empty(),
ImmutableList.of());
ImmutableList.of(),
jdbcTableHandle.getTableLocation());

ConnectorSplitSource splits = jdbcClient.getSplits(SESSION, jdbcTableHandle);
JdbcSplit split = (JdbcSplit) getOnlyElement(getFutureValue(splits.getNextBatch(1000)).getSplits());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public ConnectorMergeTableHandle beginMerge(ConnectorSession session, ConnectorT
handle.getOtherReferencedTables(),
handle.getNextSyntheticColumnId(),
handle.getAuthorization(),
handle.getUpdateAssignments());
handle.getUpdateAssignments(),
handle.getTableLocation());
}

return new IgniteMergeTableHandle(
Expand Down
Loading