Skip to content

Commit

Permalink
Update TPCH plugin to normalize schema name
Browse files Browse the repository at this point in the history
Before this commit, the TPCH plugin would return schema
names by concatenating a scale factor, which is a double,
to the string sf. This results in schema names like
sf1.0 or sf0.01. Users would expect schema names like
the following, sf1, or tiny.

This commit changes the logic to save the schema name as
a field in TpchTableHandle to preserve the original schema
name.
  • Loading branch information
andrewdibiasio6 authored and findepi committed Nov 24, 2021
1 parent 80b8ebb commit 9c1e32b
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ private TableScanNode tableScan(String id, String... symbols)
assignments.put(symbol, new TpchColumnHandle("orderkey", BIGINT));
}

TpchTableHandle tableHandle = new TpchTableHandle("orders", 1.0);
TpchTableHandle tableHandle = new TpchTableHandle("sf1", "orders", 1.0);
return new TableScanNode(
new PlanNodeId(id),
new TableHandle(new CatalogName("tpch"), tableHandle, INSTANCE, Optional.of(new TpchTableLayoutHandle(tableHandle, TupleDomain.all()))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Optional;

import static io.trino.plugin.tpch.TpchMetadata.TINY_SCALE_FACTOR;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.sql.planner.assertions.PlanMatchPattern.values;
import static io.trino.sql.planner.iterative.rule.test.PlanBuilder.expressions;
Expand Down Expand Up @@ -162,7 +163,7 @@ public void testDoesNotFireOnNestedNonCountAggregate()
p.tableScan(
new TableHandle(
new CatalogName("local"),
new TpchTableHandle("orders", TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, "orders", TINY_SCALE_FACTOR),
TpchTransactionHandle.INSTANCE,
Optional.empty()),
ImmutableList.of(totalPrice),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCALE_FACTOR;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.sql.planner.assertions.PlanMatchPattern.constrainedIndexSource;
Expand Down Expand Up @@ -82,7 +83,7 @@ private static PlanNode buildProjectedIndexSource(PlanBuilder p, Predicate<Symbo
p.indexSource(
new TableHandle(
new CatalogName("local"),
new TpchTableHandle("orders", TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, "orders", TINY_SCALE_FACTOR),
TpchTransactionHandle.INSTANCE,
Optional.empty()),
ImmutableSet.of(orderkey, custkey),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCALE_FACTOR;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.spi.type.DateType.DATE;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.sql.planner.assertions.PlanMatchPattern.strictConstrainedTableScan;
Expand All @@ -74,7 +75,7 @@ public void testNotAllOutputsReferenced()
p.tableScan(
new TableHandle(
new CatalogName("local"),
new TpchTableHandle("orders", TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, "orders", TINY_SCALE_FACTOR),
TpchTransactionHandle.INSTANCE,
Optional.empty()),
ImmutableList.of(orderdate, totalprice),
Expand Down Expand Up @@ -102,7 +103,7 @@ public void testPruneEnforcedConstraint()
p.tableScan(
new TableHandle(
new CatalogName("local"),
new TpchTableHandle("orders", TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, "orders", TINY_SCALE_FACTOR),
TpchTransactionHandle.INSTANCE,
Optional.empty()),
List.of(orderdate, totalprice),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void testExtractDereferencesFromFilterAboveScan()
{
TableHandle testTable = new TableHandle(
new CatalogName(CATALOG_ID),
new TpchTableHandle("orders", 1.0),
new TpchTableHandle("sf1", "orders", 1.0),
TestingTransactionHandle.create(),
Optional.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public void setUpBeforeClass()
CatalogName catalogName = tester().getCurrentConnectorId();
tester().getQueryRunner().createCatalog(MOCK_CATALOG, createMockFactory(), ImmutableMap.of());

TpchTableHandle nation = new TpchTableHandle("nation", 1.0);
TpchTableHandle nation = new TpchTableHandle("sf1", "nation", 1.0);
nationTableHandle = new TableHandle(
catalogName,
nation,
TpchTransactionHandle.INSTANCE,
Optional.of(new TpchTableLayoutHandle(nation, TupleDomain.all())));

TpchTableHandle orders = new TpchTableHandle("orders", 1.0);
TpchTableHandle orders = new TpchTableHandle("sf1", "orders", 1.0);
ordersTableHandle = new TableHandle(
catalogName,
orders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testDoesNotFire(Rule<?> rule)
.on(p -> p.tableDelete(
new SchemaTableName("sch", "tab"),
p.tableScan(
new TableHandle(CONNECTOR_ID, new TpchTableHandle("nation", 1.0), TpchTransactionHandle.INSTANCE, Optional.empty()),
new TableHandle(CONNECTOR_ID, new TpchTableHandle("sf1", "nation", 1.0), TpchTransactionHandle.INSTANCE, Optional.empty()),
ImmutableList.of(),
ImmutableMap.of()),
p.symbol("a", BigintType.BIGINT)))
Expand All @@ -52,7 +52,7 @@ public void testDoesNotFire(Rule<?> rule)
.on(p -> p.tableWithExchangeDelete(
new SchemaTableName("sch", "tab"),
p.tableScan(
new TableHandle(CONNECTOR_ID, new TpchTableHandle("nation", 1.0), TpchTransactionHandle.INSTANCE, Optional.empty()),
new TableHandle(CONNECTOR_ID, new TpchTableHandle("sf1", "nation", 1.0), TpchTransactionHandle.INSTANCE, Optional.empty()),
ImmutableList.of(),
ImmutableMap.of()),
p.symbol("a", BigintType.BIGINT)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public void setUpBeforeClass()
{
removeRedundantTableScanPredicate = new RemoveRedundantTableScanPredicate(tester().getMetadata(), new TypeOperators(), tester().getTypeAnalyzer());
CatalogName catalogName = tester().getCurrentConnectorId();
TpchTableHandle nation = new TpchTableHandle("nation", 1.0);
TpchTableHandle nation = new TpchTableHandle("sf1", "nation", 1.0);
nationTableHandle = new TableHandle(
catalogName,
nation,
TpchTransactionHandle.INSTANCE,
Optional.of(new TpchTableLayoutHandle(nation, TupleDomain.all())));

TpchTableHandle orders = new TpchTableHandle("orders", 1.0);
TpchTableHandle orders = new TpchTableHandle("sf1", "orders", 1.0);
ordersTableHandle = new TableHandle(
catalogName,
orders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Optional;

import static io.trino.plugin.tpch.TpchMetadata.TINY_SCALE_FACTOR;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.sql.planner.assertions.PlanMatchPattern.project;
import static io.trino.sql.planner.assertions.PlanMatchPattern.tableScan;
Expand All @@ -54,7 +55,7 @@ public void testRewrite()
p.tableScan(
new TableHandle(
new CatalogName("local"),
new TpchTableHandle("nation", TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, "nation", TINY_SCALE_FACTOR),
TpchTransactionHandle.INSTANCE,
Optional.empty()),
ImmutableList.of(p.symbol("l_nationkey")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testReportNoFireWithTableScan()
(node, captures, context) -> Result.empty()))
.on(p ->
p.tableScan(
new TableHandle(tester.getCurrentConnectorId(), new TpchTableHandle("nation", 1.0), TestingTransactionHandle.create(), Optional.empty()),
new TableHandle(tester.getCurrentConnectorId(), new TpchTableHandle("sf1", "nation", 1.0), TestingTransactionHandle.create(), Optional.empty()),
List.of(p.symbol("x")),
Map.of(p.symbol("x"), new TestingColumnHandle("column"))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public void setup()
CatalogName catalogName = getCurrentConnectorId();
lineitemTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("lineitem", 1.0),
new TpchTableHandle("sf1", "lineitem", 1.0),
TestingTransactionHandle.create(),
Optional.empty());
lineitemOrderKeySymbol = builder.symbol("LINEITEM_OK", BIGINT);
lineitemTableScanNode = builder.tableScan(lineitemTableHandle, ImmutableList.of(lineitemOrderKeySymbol), ImmutableMap.of(lineitemOrderKeySymbol, new TpchColumnHandle("orderkey", BIGINT)));

TableHandle ordersTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("orders", 1.0),
new TpchTableHandle("sf1", "orders", 1.0),
TestingTransactionHandle.create(),
Optional.empty());
ordersOrderKeySymbol = builder.symbol("ORDERS_OK", BIGINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Optional;

import static io.trino.plugin.tpch.TpchMetadata.TINY_SCALE_FACTOR;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.sql.DynamicFilters.createDynamicFilterExpression;
import static io.trino.sql.planner.assertions.PlanMatchPattern.filter;
Expand Down Expand Up @@ -147,7 +148,7 @@ private TableHandle tableHandle(Session session, String tableName)
{
return new TableHandle(
new CatalogName(session.getCatalog().get()),
new TpchTableHandle(tableName, TINY_SCALE_FACTOR),
new TpchTableHandle(TINY_SCHEMA_NAME, tableName, TINY_SCALE_FACTOR),
TestingTransactionHandle.create(),
Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public void setup()
CatalogName catalogName = getCurrentConnectorId();
TableHandle lineitemTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("lineitem", 1.0),
new TpchTableHandle("sf1", "lineitem", 1.0),
TestingTransactionHandle.create(),
Optional.empty());
lineitemOrderKeySymbol = builder.symbol("LINEITEM_OK", BIGINT);
lineitemTableScanNode = builder.tableScan(lineitemTableHandle, ImmutableList.of(lineitemOrderKeySymbol), ImmutableMap.of(lineitemOrderKeySymbol, new TpchColumnHandle("orderkey", BIGINT)));

TableHandle ordersTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("orders", 1.0),
new TpchTableHandle("sf1", "orders", 1.0),
TestingTransactionHandle.create(),
Optional.empty());
ordersOrderKeySymbol = builder.symbol("ORDERS_OK", BIGINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setup()
CatalogName catalogName = getCurrentConnectorId();
TableHandle nationTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("nation", 1.0),
new TpchTableHandle("sf1", "nation", 1.0),
TestingTransactionHandle.create(),
Optional.empty());
TpchColumnHandle nationkeyColumnHandle = new TpchColumnHandle("nationkey", BIGINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void setup()
CatalogName catalogName = getCurrentConnectorId();
nationTableHandle = new TableHandle(
catalogName,
new TpchTableHandle("nation", 1.0),
new TpchTableHandle("sf1", "nation", 1.0),
TpchTransactionHandle.INSTANCE,
Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public TpchTableHandle getTableHandle(ConnectorSession session, SchemaTableName
return null;
}

return new TpchTableHandle(tableName.getTableName(), scaleFactor);
return new TpchTableHandle(tableName.getSchemaName(), tableName.getTableName(), scaleFactor);
}

@Override
Expand All @@ -220,9 +220,8 @@ public ConnectorTableMetadata getTableMetadata(ConnectorSession session, Connect
TpchTableHandle tpchTableHandle = (TpchTableHandle) tableHandle;

TpchTable<?> tpchTable = TpchTable.getTable(tpchTableHandle.getTableName());
String schemaName = scaleFactorSchemaName(tpchTableHandle.getScaleFactor());

return getTableMetadata(schemaName, tpchTable, columnNaming);
return getTableMetadata(tpchTableHandle.getSchemaName(), tpchTable, columnNaming);
}

private ConnectorTableMetadata getTableMetadata(String schemaName, TpchTable<?> tpchTable, ColumnNaming columnNaming)
Expand Down Expand Up @@ -519,6 +518,7 @@ else if (predicatePushdownEnabled && handle.getTableName().equals(TpchTable.PART

return Optional.of(new ConstraintApplicationResult<>(
new TpchTableHandle(
handle.getSchemaName(),
handle.getTableName(),
handle.getScaleFactor(),
oldDomain.intersect(predicate)),
Expand All @@ -536,7 +536,7 @@ public Optional<TableScanRedirectApplicationResult> applyTableScanRedirect(Conne

CatalogSchemaTableName destinationTable = new CatalogSchemaTableName(
destinationCatalog.get(),
destinationSchema.orElse(scaleFactorSchemaName(handle.getScaleFactor())),
destinationSchema.orElse(handle.getSchemaName()),
handle.getTableName());
return Optional.of(
new TableScanRedirectApplicationResult(
Expand Down Expand Up @@ -570,11 +570,6 @@ private List<String> getSchemaNames(ConnectorSession session, Optional<String> s
return ImmutableList.of();
}

private static String scaleFactorSchemaName(double scaleFactor)
{
return "sf" + scaleFactor;
}

public static double schemaNameToScaleFactor(String schemaName)
{
if (TINY_SCHEMA_NAME.equals(schemaName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,36 @@
public class TpchTableHandle
implements ConnectorTableHandle
{
private final String schemaName;
private final String tableName;
private final double scaleFactor;
private final TupleDomain<ColumnHandle> constraint;

public TpchTableHandle(String tableName, double scaleFactor)
public TpchTableHandle(String schemaName, String tableName, double scaleFactor)
{
this(tableName, scaleFactor, TupleDomain.all());
this(schemaName, tableName, scaleFactor, TupleDomain.all());
}

@JsonCreator
public TpchTableHandle(
@JsonProperty("schemaName") String schemaName,
@JsonProperty("tableName") String tableName,
@JsonProperty("scaleFactor") double scaleFactor,
@JsonProperty("constraint") TupleDomain<ColumnHandle> constraint)
{
this.schemaName = requireNonNull(schemaName, "schemaName is null");
this.tableName = requireNonNull(tableName, "tableName is null");
checkArgument(scaleFactor > 0, "Scale factor must be larger than 0");
this.scaleFactor = scaleFactor;
this.constraint = requireNonNull(constraint, "constraint is null");
}

@JsonProperty
public String getSchemaName()
{
return schemaName;
}

@JsonProperty
public String getTableName()
{
Expand All @@ -69,13 +78,13 @@ public TupleDomain<ColumnHandle> getConstraint()
@Override
public String toString()
{
return tableName + ":sf" + scaleFactor;
return schemaName + ":" + tableName;
}

@Override
public int hashCode()
{
return Objects.hash(tableName, scaleFactor, constraint);
return Objects.hash(schemaName, tableName, scaleFactor, constraint);
}

@Override
Expand All @@ -88,7 +97,8 @@ public boolean equals(Object obj)
return false;
}
TpchTableHandle other = (TpchTableHandle) obj;
return Objects.equals(this.tableName, other.tableName) &&
return Objects.equals(this.schemaName, other.schemaName) &&
Objects.equals(this.tableName, other.tableName) &&
Objects.equals(this.scaleFactor, other.scaleFactor) &&
Objects.equals(this.constraint, other.constraint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableHandle;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.connector.Constraint;
import io.trino.spi.connector.ConstraintApplicationResult;
import io.trino.spi.connector.SchemaTableName;
Expand Down Expand Up @@ -129,6 +130,30 @@ public void testTableStatsWithConstraints()
});
}

@Test
public void testGetTableMetadata()
{
Stream.of("sf0.01", "tiny", "sf1.0", "sf1.000", "sf2.01", "sf3.1", "sf10.0", "sf100.0", "sf30000.0", "sf30000.2").forEach(
schemaName -> {
testGetTableMetadata(schemaName, REGION);
testGetTableMetadata(schemaName, NATION);
testGetTableMetadata(schemaName, SUPPLIER);
testGetTableMetadata(schemaName, CUSTOMER);
testGetTableMetadata(schemaName, PART);
testGetTableMetadata(schemaName, PART_SUPPLIER);
testGetTableMetadata(schemaName, ORDERS);
testGetTableMetadata(schemaName, LINE_ITEM);
});
}

private void testGetTableMetadata(String schema, TpchTable<?> table)
{
TpchTableHandle tableHandle = tpchMetadata.getTableHandle(session, new SchemaTableName(schema, table.getTableName()));
ConnectorTableMetadata tableMetadata = tpchMetadata.getTableMetadata(session, tableHandle);
assertEquals(tableMetadata.getTableSchema().getTable().getTableName(), table.getTableName());
assertEquals(tableMetadata.getTableSchema().getTable().getSchemaName(), schema);
}

@Test
public void testHiddenSchemas()
{
Expand Down
Loading

0 comments on commit 9c1e32b

Please sign in to comment.