Skip to content

Commit d749108

Browse files
committed
Improve query plan rendering for memory connector
Use names instead of internal IDs in the table and column handles.
1 parent f77b2b0 commit d749108

File tree

5 files changed

+54
-42
lines changed

5 files changed

+54
-42
lines changed

plugin/trino-memory/src/main/java/io/trino/plugin/memory/ColumnInfo.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,27 @@
1414
package io.trino.plugin.memory;
1515

1616
import com.fasterxml.jackson.annotation.JsonIgnore;
17-
import io.trino.spi.connector.ColumnHandle;
1817
import io.trino.spi.connector.ColumnMetadata;
19-
import io.trino.spi.type.Type;
2018

2119
import java.util.Optional;
2220

2321
import static com.google.common.base.MoreObjects.toStringHelper;
2422
import static java.util.Objects.requireNonNull;
2523

26-
public record ColumnInfo(ColumnHandle handle, String name, Type type, boolean nullable, Optional<String> comment)
24+
public record ColumnInfo(MemoryColumnHandle handle, boolean nullable, Optional<String> comment)
2725
{
2826
public ColumnInfo
2927
{
3028
requireNonNull(handle, "handle is null");
31-
requireNonNull(name, "name is null");
32-
requireNonNull(type, "type is null");
3329
requireNonNull(comment, "comment is null");
3430
}
3531

3632
@JsonIgnore
3733
public ColumnMetadata getMetadata()
3834
{
3935
return ColumnMetadata.builder()
40-
.setName(name)
41-
.setType(type)
36+
.setName(handle.name())
37+
.setType(handle.type())
4238
.setNullable(nullable)
4339
.setComment(comment)
4440
.build();
@@ -48,8 +44,8 @@ public ColumnMetadata getMetadata()
4844
public String toString()
4945
{
5046
return toStringHelper(this)
51-
.add("name", name)
52-
.add("type", type)
47+
.add("name", handle.name())
48+
.add("type", handle.type())
5349
.add("nullable", nullable)
5450
.add("comment", comment)
5551
.toString();

plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryColumnHandle.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616
import io.trino.spi.connector.ColumnHandle;
1717
import io.trino.spi.type.Type;
1818

19-
public record MemoryColumnHandle(int columnIndex, Type type)
19+
import static java.util.Objects.requireNonNull;
20+
21+
public record MemoryColumnHandle(int columnIndex, String name, Type type)
2022
implements ColumnHandle
2123
{
24+
public MemoryColumnHandle
25+
{
26+
requireNonNull(name, "name is null");
27+
requireNonNull(type, "type is null");
28+
}
29+
2230
@Override
2331
public String toString()
2432
{
25-
return Integer.toString(columnIndex);
33+
return name + ":" + type;
2634
}
2735
}

plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryMetadata.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public synchronized ConnectorTableHandle getTableHandle(ConnectorSession session
212212
return null;
213213
}
214214

215-
return new MemoryTableHandle(id, OptionalLong.empty(), OptionalDouble.empty());
215+
return new MemoryTableHandle(id, schemaTableName, OptionalLong.empty(), OptionalDouble.empty());
216216
}
217217

218218
@Override
@@ -243,9 +243,8 @@ public synchronized List<SchemaTableName> listTables(ConnectorSession session, O
243243
public synchronized Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
244244
{
245245
MemoryTableHandle handle = (MemoryTableHandle) tableHandle;
246-
return tables.get(handle.id())
247-
.columns().stream()
248-
.collect(toImmutableMap(ColumnInfo::name, ColumnInfo::handle));
246+
return tables.get(handle.id()).columns().stream()
247+
.collect(toImmutableMap(column -> column.handle().name(), ColumnInfo::handle));
249248
}
250249

251250
@Override
@@ -348,7 +347,8 @@ public synchronized MemoryOutputTableHandle beginCreateTable(ConnectorSession se
348347
ImmutableList.Builder<ColumnInfo> columns = ImmutableList.builder();
349348
for (int i = 0; i < tableMetadata.getColumns().size(); i++) {
350349
ColumnMetadata column = tableMetadata.getColumns().get(i);
351-
columns.add(new ColumnInfo(new MemoryColumnHandle(i, column.getType()), column.getName(), column.getType(), column.isNullable(), Optional.ofNullable(column.getComment())));
350+
MemoryColumnHandle handle = new MemoryColumnHandle(i, column.getName(), column.getType());
351+
columns.add(new ColumnInfo(handle, column.isNullable(), Optional.ofNullable(column.getComment())));
352352
}
353353

354354
tableIds.put(tableMetadata.getTable(), tableId);
@@ -446,9 +446,11 @@ public synchronized void addColumn(ConnectorSession session, ConnectorTableHandl
446446
throw new TrinoException(NOT_SUPPORTED, format("Unable to add NOT NULL column '%s' for non-empty table: %s", column.getName(), table.getSchemaTableName()));
447447
}
448448

449+
MemoryColumnHandle newColumn = new MemoryColumnHandle(table.columns().size(), column.getName(), column.getType());
450+
449451
List<ColumnInfo> columns = ImmutableList.<ColumnInfo>builderWithExpectedSize(table.columns().size() + 1)
450452
.addAll(table.columns())
451-
.add(new ColumnInfo(new MemoryColumnHandle(table.columns().size(), column.getType()), column.getName(), column.getType(), column.isNullable(), Optional.ofNullable(column.getComment())))
453+
.add(new ColumnInfo(newColumn, column.isNullable(), Optional.ofNullable(column.getComment())))
452454
.build();
453455

454456
tables.put(tableId, new TableInfo(tableId, table.schemaName(), table.tableName(), columns, table.truncated(), table.dataFragments(), table.comment()));
@@ -462,9 +464,11 @@ public synchronized void renameColumn(ConnectorSession session, ConnectorTableHa
462464
long tableId = handle.id();
463465
TableInfo table = tables.get(handle.id());
464466

467+
MemoryColumnHandle newColumn = new MemoryColumnHandle(column.columnIndex(), target, column.type());
468+
465469
List<ColumnInfo> columns = new ArrayList<>(table.columns());
466470
ColumnInfo columnInfo = columns.get(column.columnIndex());
467-
columns.set(column.columnIndex(), new ColumnInfo(columnInfo.handle(), target, columnInfo.type(), columnInfo.nullable(), columnInfo.comment()));
471+
columns.set(column.columnIndex(), new ColumnInfo(newColumn, columnInfo.nullable(), columnInfo.comment()));
468472

469473
tables.put(tableId, new TableInfo(tableId, table.schemaName(), table.tableName(), ImmutableList.copyOf(columns), table.truncated(), table.dataFragments(), table.comment()));
470474
}
@@ -479,7 +483,7 @@ public synchronized void dropNotNullConstraint(ConnectorSession session, Connect
479483

480484
List<ColumnInfo> columns = new ArrayList<>(table.columns());
481485
ColumnInfo columnInfo = columns.get(column.columnIndex());
482-
columns.set(column.columnIndex(), new ColumnInfo(columnInfo.handle(), columnInfo.name(), columnInfo.type(), true, columnInfo.comment()));
486+
columns.set(column.columnIndex(), new ColumnInfo(columnInfo.handle(), true, columnInfo.comment()));
483487

484488
tables.put(tableId, new TableInfo(tableId, table.schemaName(), table.tableName(), ImmutableList.copyOf(columns), table.truncated(), table.dataFragments(), table.comment()));
485489
}
@@ -626,10 +630,7 @@ public Optional<LimitApplicationResult<ConnectorTableHandle>> applyLimit(Connect
626630
return Optional.empty();
627631
}
628632

629-
return Optional.of(new LimitApplicationResult<>(
630-
new MemoryTableHandle(table.id(), OptionalLong.of(limit), OptionalDouble.empty()),
631-
true,
632-
true));
633+
return Optional.of(new LimitApplicationResult<>(table.withLimit(limit), true, true));
633634
}
634635

635636
@Override
@@ -641,9 +642,8 @@ public Optional<SampleApplicationResult<ConnectorTableHandle>> applySample(Conne
641642
return Optional.empty();
642643
}
643644

644-
return Optional.of(new SampleApplicationResult<>(
645-
new MemoryTableHandle(table.id(), table.limit(), OptionalDouble.of(table.sampleRatio().orElse(1) * sampleRatio)),
646-
true));
645+
double newRatio = table.sampleRatio().orElse(1) * sampleRatio;
646+
return Optional.of(new SampleApplicationResult<>(table.withSampleRatio(newRatio), true));
647647
}
648648

649649
@Override
@@ -667,18 +667,12 @@ public synchronized void setColumnComment(ConnectorSession session, ConnectorTab
667667
MemoryTableHandle table = (MemoryTableHandle) tableHandle;
668668
TableInfo info = tables.get(table.id());
669669
checkArgument(info != null, "Table not found");
670-
tables.put(
671-
table.id(),
672-
new TableInfo(
673-
table.id(),
674-
info.schemaName(),
675-
info.tableName(),
676-
info.columns().stream()
677-
.map(tableColumn -> Objects.equals(tableColumn.handle(), columnHandle) ? new ColumnInfo(tableColumn.handle(), tableColumn.name(), tableColumn.getMetadata().getType(), tableColumn.nullable(), comment) : tableColumn)
678-
.collect(toImmutableList()),
679-
info.truncated(),
680-
info.dataFragments(),
681-
info.comment()));
670+
List<ColumnInfo> newColumns = info.columns().stream()
671+
.map(column -> column.handle().equals(columnHandle)
672+
? new ColumnInfo(column.handle(), column.nullable(), comment)
673+
: column)
674+
.collect(toImmutableList());
675+
tables.put(table.id(), new TableInfo(table.id(), info.schemaName(), info.tableName(), newColumns, info.truncated(), info.dataFragments(), info.comment()));
682676
}
683677

684678
@Override

plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryTableHandle.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.trino.plugin.memory;
1515

1616
import io.trino.spi.connector.ConnectorTableHandle;
17+
import io.trino.spi.connector.SchemaTableName;
1718

1819
import java.util.OptionalDouble;
1920
import java.util.OptionalLong;
@@ -22,12 +23,14 @@
2223

2324
public record MemoryTableHandle(
2425
long id,
26+
SchemaTableName name,
2527
OptionalLong limit,
2628
OptionalDouble sampleRatio)
2729
implements ConnectorTableHandle
2830
{
2931
public MemoryTableHandle
3032
{
33+
requireNonNull(name, "name is null");
3134
requireNonNull(limit, "limit is null");
3235
requireNonNull(sampleRatio, "sampleRatio is null");
3336
}
@@ -36,9 +39,19 @@ public record MemoryTableHandle(
3639
public String toString()
3740
{
3841
StringBuilder builder = new StringBuilder();
39-
builder.append(id);
40-
limit.ifPresent(value -> builder.append("(limit:" + value + ")"));
41-
sampleRatio.ifPresent(value -> builder.append("(sampleRatio:" + value + ")"));
42+
builder.append(name);
43+
limit.ifPresent(value -> builder.append(" limit=").append(value));
44+
sampleRatio.ifPresent(value -> builder.append(" sampleRatio=").append(value));
4245
return builder.toString();
4346
}
47+
48+
public MemoryTableHandle withLimit(long limit)
49+
{
50+
return new MemoryTableHandle(id, name, OptionalLong.of(limit), sampleRatio);
51+
}
52+
53+
public MemoryTableHandle withSampleRatio(double sampleRatio)
54+
{
55+
return new MemoryTableHandle(id, name, limit, OptionalDouble.of(sampleRatio));
56+
}
4457
}

plugin/trino-memory/src/main/java/io/trino/plugin/memory/TableInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public ConnectorTableMetadata getMetadata()
6969
@JsonIgnore
7070
public ColumnInfo getColumn(ColumnHandle handle)
7171
{
72+
int columnIndex = ((MemoryColumnHandle) handle).columnIndex();
7273
return columns.stream()
73-
.filter(column -> column.handle().equals(handle))
74+
.filter(column -> column.handle().columnIndex() == columnIndex)
7475
.collect(onlyElement());
7576
}
7677
}

0 commit comments

Comments
 (0)