Skip to content

Commit 3b253d1

Browse files
chenjian2664ebyhr
authored andcommitted
Use RemoteTableName in JdbcOutputTableHandle
Use RemoteTableName, which wraps `catalogName` and `schemaName` in `Optional` to handle their potential absence more explicitly
1 parent 784a131 commit 3b253d1

File tree

13 files changed

+59
-95
lines changed

13 files changed

+59
-95
lines changed

plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/BaseJdbcClient.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,7 @@ protected JdbcOutputTableHandle createTable(
931931
}
932932

933933
return new JdbcOutputTableHandle(
934-
catalog,
935-
remoteSchema,
936-
remoteTable,
934+
new RemoteTableName(Optional.ofNullable(catalog), Optional.ofNullable(remoteSchema), remoteTable),
937935
columnNames.build(),
938936
columnTypes.build(),
939937
Optional.empty(),
@@ -1015,9 +1013,7 @@ protected JdbcOutputTableHandle beginInsertTable(
10151013

10161014
if (isNonTransactionalInsert(session)) {
10171015
return new JdbcOutputTableHandle(
1018-
catalog,
1019-
remoteSchema,
1020-
remoteTable,
1016+
new RemoteTableName(Optional.ofNullable(catalog), Optional.ofNullable(remoteSchema), remoteTable),
10211017
columnNames.build(),
10221018
columnTypes.build(),
10231019
Optional.of(jdbcColumnTypes.build()),
@@ -1039,9 +1035,7 @@ protected JdbcOutputTableHandle beginInsertTable(
10391035
}
10401036

10411037
return new JdbcOutputTableHandle(
1042-
catalog,
1043-
remoteSchema,
1044-
remoteTable,
1038+
new RemoteTableName(Optional.ofNullable(catalog), Optional.ofNullable(remoteSchema), remoteTable),
10451039
columnNames.build(),
10461040
columnTypes.build(),
10471041
Optional.of(jdbcColumnTypes.build()),
@@ -1075,10 +1069,10 @@ public void commitCreateTable(ConnectorSession session, JdbcOutputTableHandle ha
10751069
else {
10761070
renameTable(
10771071
session,
1078-
handle.getCatalogName(),
1079-
handle.getSchemaName(),
1072+
handle.getRemoteTableName().getCatalogName().orElse(null),
1073+
handle.getRemoteTableName().getSchemaName().orElse(null),
10801074
handle.getTemporaryTableName().orElseThrow(() -> new IllegalStateException("Temporary table name missing")),
1081-
new SchemaTableName(handle.getSchemaName(), handle.getTableName()));
1075+
handle.getRemoteTableName().getSchemaTableName());
10821076
}
10831077
}
10841078

@@ -1123,8 +1117,8 @@ private RemoteTableName constructPageSinkIdsTable(ConnectorSession session, Conn
11231117
verify(handle.getPageSinkIdColumnName().isPresent(), "Output table handle's pageSinkIdColumn is empty");
11241118

11251119
RemoteTableName pageSinkTable = new RemoteTableName(
1126-
Optional.ofNullable(handle.getCatalogName()),
1127-
Optional.ofNullable(handle.getSchemaName()),
1120+
handle.getRemoteTableName().getCatalogName(),
1121+
handle.getRemoteTableName().getSchemaName(),
11281122
generateTemporaryTableName(session));
11291123

11301124
int maxBatchSize = getWriteBatchSize(session);
@@ -1173,13 +1167,9 @@ public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle ha
11731167
}
11741168

11751169
RemoteTableName temporaryTable = new RemoteTableName(
1176-
Optional.ofNullable(handle.getCatalogName()),
1177-
Optional.ofNullable(handle.getSchemaName()),
1170+
handle.getRemoteTableName().getCatalogName(),
1171+
handle.getRemoteTableName().getSchemaName(),
11781172
handle.getTemporaryTableName().orElseThrow());
1179-
RemoteTableName targetTable = new RemoteTableName(
1180-
Optional.ofNullable(handle.getCatalogName()),
1181-
Optional.ofNullable(handle.getSchemaName()),
1182-
handle.getTableName());
11831173

11841174
// We conditionally create more than the one table, so keep a list of the tables that need to be dropped.
11851175
Closer closer = Closer.create();
@@ -1192,7 +1182,7 @@ public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle ha
11921182
.collect(joining(", "));
11931183

11941184
String insertSql = format("INSERT INTO %s (%s) SELECT %s FROM %s temp_table",
1195-
postProcessInsertTableNameClause(session, quoted(targetTable)),
1185+
postProcessInsertTableNameClause(session, quoted(handle.getRemoteTableName())),
11961186
columns,
11971187
columns,
11981188
quoted(temporaryTable));
@@ -1358,8 +1348,8 @@ public void rollbackCreateTable(ConnectorSession session, JdbcOutputTableHandle
13581348
if (handle.getTemporaryTableName().isPresent()) {
13591349
dropTable(session,
13601350
new RemoteTableName(
1361-
Optional.ofNullable(handle.getCatalogName()),
1362-
Optional.ofNullable(handle.getSchemaName()),
1351+
handle.getRemoteTableName().getCatalogName(),
1352+
handle.getRemoteTableName().getSchemaName(),
13631353
handle.getTemporaryTableName().get()),
13641354
true);
13651355
}
@@ -1383,7 +1373,10 @@ public String buildInsertSql(JdbcOutputTableHandle handle, List<WriteFunction> c
13831373
checkArgument(handle.getColumnNames().size() == columnWriters.size(), "handle and columnWriters mismatch: %s, %s", handle, columnWriters);
13841374
return format(
13851375
"INSERT INTO %s (%s%s) VALUES (%s%s)",
1386-
quoted(handle.getCatalogName(), handle.getSchemaName(), handle.getTemporaryTableName().orElseGet(handle::getTableName)),
1376+
quoted(
1377+
handle.getRemoteTableName().getCatalogName().orElse(null),
1378+
handle.getRemoteTableName().getSchemaName().orElse(null),
1379+
handle.getTemporaryTableName().orElseGet(() -> handle.getRemoteTableName().getTableName())),
13871380
handle.getColumnNames().stream()
13881381
.map(this::quoted)
13891382
.collect(joining(", ")),

plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/CachingJdbcClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public JdbcProcedureHandle getProcedureHandle(ConnectorSession session, Procedur
388388
public void commitCreateTable(ConnectorSession session, JdbcOutputTableHandle handle, Set<Long> pageSinkIds)
389389
{
390390
delegate.commitCreateTable(session, handle, pageSinkIds);
391-
invalidateTableCaches(new SchemaTableName(handle.getSchemaName(), handle.getTableName()));
391+
invalidateTableCaches(handle.getRemoteTableName().getSchemaTableName());
392392
}
393393

394394
@Override
@@ -401,7 +401,7 @@ public JdbcOutputTableHandle beginInsertTable(ConnectorSession session, JdbcTabl
401401
public void finishInsertTable(ConnectorSession session, JdbcOutputTableHandle handle, Set<Long> pageSinkIds)
402402
{
403403
delegate.finishInsertTable(session, handle, pageSinkIds);
404-
onDataChanged(new SchemaTableName(handle.getSchemaName(), handle.getTableName()));
404+
onDataChanged(handle.getRemoteTableName().getSchemaTableName());
405405
}
406406

407407
@Override

plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/JdbcOutputTableHandle.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@
1919
import io.trino.spi.connector.ConnectorInsertTableHandle;
2020
import io.trino.spi.connector.ConnectorOutputTableHandle;
2121
import io.trino.spi.type.Type;
22-
import jakarta.annotation.Nullable;
2322

2423
import java.util.List;
2524
import java.util.Objects;
2625
import java.util.Optional;
2726

2827
import static com.google.common.base.Preconditions.checkArgument;
29-
import static java.lang.String.format;
3028
import static java.util.Objects.requireNonNull;
3129

3230
public class JdbcOutputTableHandle
3331
implements ConnectorOutputTableHandle, ConnectorInsertTableHandle
3432
{
35-
private final String catalogName;
36-
private final String schemaName;
37-
private final String tableName;
33+
private final RemoteTableName remoteTableName;
3834
private final List<String> columnNames;
3935
private final List<Type> columnTypes;
4036
private final Optional<List<JdbcTypeHandle>> jdbcColumnTypes;
@@ -43,18 +39,14 @@ public class JdbcOutputTableHandle
4339

4440
@JsonCreator
4541
public JdbcOutputTableHandle(
46-
@JsonProperty("catalogName") @Nullable String catalogName,
47-
@JsonProperty("schemaName") @Nullable String schemaName,
48-
@JsonProperty("tableName") String tableName,
42+
@JsonProperty("remoteTableName") RemoteTableName remoteTableName,
4943
@JsonProperty("columnNames") List<String> columnNames,
5044
@JsonProperty("columnTypes") List<Type> columnTypes,
5145
@JsonProperty("jdbcColumnTypes") Optional<List<JdbcTypeHandle>> jdbcColumnTypes,
5246
@JsonProperty("temporaryTableName") Optional<String> temporaryTableName,
5347
@JsonProperty("pageSinkIdColumnName") Optional<String> pageSinkIdColumnName)
5448
{
55-
this.catalogName = catalogName;
56-
this.schemaName = schemaName;
57-
this.tableName = requireNonNull(tableName, "tableName is null");
49+
this.remoteTableName = requireNonNull(remoteTableName, "remoteTableName is null");
5850
this.temporaryTableName = requireNonNull(temporaryTableName, "temporaryTableName is null");
5951

6052
requireNonNull(columnNames, "columnNames is null");
@@ -68,23 +60,9 @@ public JdbcOutputTableHandle(
6860
}
6961

7062
@JsonProperty
71-
@Nullable
72-
public String getCatalogName()
63+
public RemoteTableName getRemoteTableName()
7364
{
74-
return catalogName;
75-
}
76-
77-
@JsonProperty
78-
@Nullable
79-
public String getSchemaName()
80-
{
81-
return schemaName;
82-
}
83-
84-
@JsonProperty
85-
public String getTableName()
86-
{
87-
return tableName;
65+
return remoteTableName;
8866
}
8967

9068
@JsonProperty
@@ -120,16 +98,14 @@ public Optional<String> getPageSinkIdColumnName()
12098
@Override
12199
public String toString()
122100
{
123-
return format("jdbc:%s.%s.%s", catalogName, schemaName, tableName);
101+
return "jdbc:%s".formatted(remoteTableName);
124102
}
125103

126104
@Override
127105
public int hashCode()
128106
{
129107
return Objects.hash(
130-
catalogName,
131-
schemaName,
132-
tableName,
108+
remoteTableName,
133109
columnNames,
134110
columnTypes,
135111
jdbcColumnTypes,
@@ -147,9 +123,7 @@ public boolean equals(Object obj)
147123
return false;
148124
}
149125
JdbcOutputTableHandle other = (JdbcOutputTableHandle) obj;
150-
return Objects.equals(this.catalogName, other.catalogName) &&
151-
Objects.equals(this.schemaName, other.schemaName) &&
152-
Objects.equals(this.tableName, other.tableName) &&
126+
return Objects.equals(this.remoteTableName, other.remoteTableName) &&
153127
Objects.equals(this.columnNames, other.columnNames) &&
154128
Objects.equals(this.columnTypes, other.columnTypes) &&
155129
Objects.equals(this.jdbcColumnTypes, other.jdbcColumnTypes) &&

plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/RemoteTableName.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.fasterxml.jackson.annotation.JsonCreator;
1818
import com.fasterxml.jackson.annotation.JsonProperty;
1919
import com.google.common.base.Joiner;
20+
import io.trino.spi.connector.SchemaTableName;
2021

2122
import java.util.Objects;
2223
import java.util.Optional;
@@ -58,6 +59,11 @@ public String getTableName()
5859
return tableName;
5960
}
6061

62+
public SchemaTableName getSchemaTableName()
63+
{
64+
return new SchemaTableName(schemaName.orElseThrow(), tableName);
65+
}
66+
6167
@Override
6268
public boolean equals(Object o)
6369
{

plugin/trino-base-jdbc/src/test/java/io/trino/plugin/jdbc/TestJdbcOutputTableHandle.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ public class TestJdbcOutputTableHandle
2929
public void testJsonRoundTrip()
3030
{
3131
JdbcOutputTableHandle handleForCreate = new JdbcOutputTableHandle(
32-
"catalog",
33-
"schema",
34-
"table",
32+
new RemoteTableName(Optional.of("catalog"), Optional.of("schema"), "table"),
3533
ImmutableList.of("abc", "xyz"),
3634
ImmutableList.of(VARCHAR, VARCHAR),
3735
Optional.empty(),
@@ -41,9 +39,7 @@ public void testJsonRoundTrip()
4139
assertJsonRoundTrip(OUTPUT_TABLE_CODEC, handleForCreate);
4240

4341
JdbcOutputTableHandle handleForInsert = new JdbcOutputTableHandle(
44-
"catalog",
45-
"schema",
46-
"table",
42+
new RemoteTableName(Optional.of("catalog"), Optional.of("schema"), "table"),
4743
ImmutableList.of("abc", "xyz"),
4844
ImmutableList.of(VARCHAR, VARCHAR),
4945
Optional.of(ImmutableList.of(JDBC_VARCHAR, JDBC_VARCHAR)),

plugin/trino-ignite/src/main/java/io/trino/plugin/ignite/IgniteClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import io.trino.plugin.jdbc.LongWriteFunction;
3838
import io.trino.plugin.jdbc.PreparedQuery;
3939
import io.trino.plugin.jdbc.QueryBuilder;
40+
import io.trino.plugin.jdbc.RemoteTableName;
4041
import io.trino.plugin.jdbc.WriteFunction;
4142
import io.trino.plugin.jdbc.WriteMapping;
4243
import io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint;
@@ -415,8 +416,7 @@ public JdbcOutputTableHandle beginCreateTable(ConnectorSession session, Connecto
415416
execute(session, connection, sql);
416417

417418
return new IgniteOutputTableHandle(
418-
schemaTableName.getSchemaName(),
419-
schemaTableName.getTableName(),
419+
new RemoteTableName(Optional.empty(), Optional.of(schemaTableName.getSchemaName()), schemaTableName.getTableName()),
420420
columnNames,
421421
columnTypes.build(),
422422
Optional.empty(),
@@ -568,7 +568,7 @@ public String buildInsertSql(JdbcOutputTableHandle handle, List<WriteFunction> c
568568
}
569569
return format(
570570
"INSERT INTO %s (%s) VALUES (%s)",
571-
quoted(null, handle.getSchemaName(), handle.getTableName()),
571+
quoted(handle.getRemoteTableName()),
572572
columns,
573573
params);
574574
}

plugin/trino-ignite/src/main/java/io/trino/plugin/ignite/IgniteMetadata.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ public ConnectorInsertTableHandle beginInsert(ConnectorSession session, Connecto
104104

105105
RemoteTableName remoteTableName = handle.asPlainTable().getRemoteTableName();
106106
return new IgniteOutputTableHandle(
107-
remoteTableName.getSchemaName().orElse(null),
108-
remoteTableName.getTableName(),
107+
remoteTableName,
109108
columnNames.build(),
110109
columnTypes.build(),
111110
Optional.of(columnJdbcTypeHandles.build()),

plugin/trino-ignite/src/main/java/io/trino/plugin/ignite/IgniteOutputTableHandle.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import com.fasterxml.jackson.annotation.JsonProperty;
1818
import io.trino.plugin.jdbc.JdbcOutputTableHandle;
1919
import io.trino.plugin.jdbc.JdbcTypeHandle;
20+
import io.trino.plugin.jdbc.RemoteTableName;
2021
import io.trino.spi.type.Type;
21-
import jakarta.annotation.Nullable;
2222

2323
import java.util.List;
2424
import java.util.Optional;
@@ -32,14 +32,13 @@ public class IgniteOutputTableHandle
3232

3333
@JsonCreator
3434
public IgniteOutputTableHandle(
35-
@Nullable @JsonProperty("schemaName") String schemaName,
36-
@JsonProperty("tableName") String tableName,
35+
@JsonProperty("remoteTableName") RemoteTableName remoteTableName,
3736
@JsonProperty("columnNames") List<String> columnNames,
3837
@JsonProperty("columnTypes") List<Type> columnTypes,
3938
@JsonProperty("jdbcColumnTypes") Optional<List<JdbcTypeHandle>> jdbcColumnTypes,
4039
@JsonProperty("dummyIdColumn") Optional<String> dummyIdColumn)
4140
{
42-
super("", schemaName, tableName, columnNames, columnTypes, jdbcColumnTypes, Optional.empty(), Optional.empty());
41+
super(remoteTableName, columnNames, columnTypes, jdbcColumnTypes, Optional.empty(), Optional.empty());
4342
this.dummyIdColumn = requireNonNull(dummyIdColumn, "dummyIdColumn is null");
4443
}
4544

plugin/trino-phoenix5/src/main/java/io/trino/plugin/phoenix5/PhoenixClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ public String buildInsertSql(JdbcOutputTableHandle handle, List<WriteFunction> c
410410
if (outputHandle.rowkeyColumn().isPresent()) {
411411
String nextId = format(
412412
"NEXT VALUE FOR %s, ",
413-
quoted(null, handle.getSchemaName(), handle.getTableName() + "_sequence"));
413+
quoted(null, handle.getRemoteTableName().getSchemaName().orElse(null), handle.getRemoteTableName().getTableName() + "_sequence"));
414414
params = nextId + params;
415415
columns = outputHandle.rowkeyColumn().get() + ", " + columns;
416416
}
417417
return format(
418418
"UPSERT INTO %s (%s) VALUES (%s)",
419-
quoted(null, handle.getSchemaName(), handle.getTableName()),
419+
quoted(handle.getRemoteTableName()),
420420
columns,
421421
params);
422422
}
@@ -696,8 +696,7 @@ public JdbcOutputTableHandle beginCreateTable(ConnectorSession session, Connecto
696696
execute(session, sql);
697697

698698
return new PhoenixOutputTableHandle(
699-
schema,
700-
table,
699+
new RemoteTableName(Optional.empty(), Optional.ofNullable(schema), table),
701700
columnNames.build(),
702701
columnTypes.build(),
703702
Optional.empty(),

0 commit comments

Comments
 (0)