All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.trino.plugin.tpcds.TpcdsMetadata Maven / Gradle / Ivy
/*
* 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.plugin.tpcds;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.trino.plugin.tpcds.statistics.TpcdsTableStatisticsFactory;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ConnectorMetadata;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableHandle;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.connector.ConnectorTableVersion;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.connector.SchemaTablePrefix;
import io.trino.spi.statistics.TableStatistics;
import io.trino.spi.type.BigintType;
import io.trino.spi.type.DateType;
import io.trino.spi.type.IntegerType;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.Type;
import io.trino.tpcds.Table;
import io.trino.tpcds.column.Column;
import io.trino.tpcds.column.ColumnType;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.type.CharType.createCharType;
import static io.trino.spi.type.DecimalType.createDecimalType;
import static io.trino.spi.type.VarcharType.createVarcharType;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
public class TpcdsMetadata
implements ConnectorMetadata
{
public static final String TINY_SCHEMA_NAME = "tiny";
public static final double TINY_SCALE_FACTOR = 0.01;
public static final List SCHEMA_NAMES = ImmutableList.of(
TINY_SCHEMA_NAME, "sf1", "sf10", "sf100", "sf300", "sf1000", "sf3000", "sf10000", "sf30000", "sf100000");
private final Set tableNames;
private final TpcdsTableStatisticsFactory tpcdsTableStatisticsFactory = new TpcdsTableStatisticsFactory();
public TpcdsMetadata()
{
ImmutableSet.Builder tableNames = ImmutableSet.builder();
for (Table tpcdsTable : Table.getBaseTables()) {
tableNames.add(tpcdsTable.getName().toLowerCase(ENGLISH));
}
this.tableNames = tableNames.build();
}
@Override
public boolean schemaExists(ConnectorSession session, String schemaName)
{
return schemaNameToScaleFactor(schemaName) > 0;
}
@Override
public List listSchemaNames(ConnectorSession session)
{
return SCHEMA_NAMES;
}
@Override
public ConnectorTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName, Optional startVersion, Optional endVersion)
{
if (startVersion.isPresent() || endVersion.isPresent()) {
throw new TrinoException(NOT_SUPPORTED, "This connector does not support versioned tables");
}
requireNonNull(tableName, "tableName is null");
if (!tableNames.contains(tableName.getTableName())) {
return null;
}
// parse the scale factor
double scaleFactor = schemaNameToScaleFactor(tableName.getSchemaName());
if (scaleFactor <= 0) {
return null;
}
return new TpcdsTableHandle(tableName.getTableName(), scaleFactor);
}
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle)
{
TpcdsTableHandle tpcdsTableHandle = (TpcdsTableHandle) tableHandle;
Table table = Table.getTable(tpcdsTableHandle.tableName());
String schemaName = scaleFactorSchemaName(tpcdsTableHandle.scaleFactor());
return getTableMetadata(schemaName, table);
}
private static ConnectorTableMetadata getTableMetadata(String schemaName, Table tpcdsTable)
{
ImmutableList.Builder columns = ImmutableList.builder();
for (Column column : tpcdsTable.getColumns()) {
columns.add(new ColumnMetadata(column.getName(), getTrinoType(column.getType())));
}
SchemaTableName tableName = new SchemaTableName(schemaName, tpcdsTable.getName());
return new ConnectorTableMetadata(tableName, columns.build());
}
@Override
public TableStatistics getTableStatistics(ConnectorSession session, ConnectorTableHandle tableHandle)
{
TpcdsTableHandle tpcdsTableHandle = (TpcdsTableHandle) tableHandle;
Table table = Table.getTable(tpcdsTableHandle.tableName());
String schemaName = scaleFactorSchemaName(tpcdsTableHandle.scaleFactor());
return tpcdsTableStatisticsFactory.create(schemaName, table, getColumnHandles(session, tableHandle));
}
@Override
public Map getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
{
ImmutableMap.Builder builder = ImmutableMap.builder();
for (ColumnMetadata columnMetadata : getTableMetadata(session, tableHandle).getColumns()) {
builder.put(columnMetadata.getName(), new TpcdsColumnHandle(columnMetadata.getName(), columnMetadata.getType()));
}
return builder.buildOrThrow();
}
@Override
public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle)
{
ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
String columnName = ((TpcdsColumnHandle) columnHandle).columnName();
for (ColumnMetadata column : tableMetadata.getColumns()) {
if (column.getName().equals(columnName)) {
return column;
}
}
throw new IllegalArgumentException(format("Table '%s' does not have column '%s'", tableMetadata.getTable(), columnName));
}
@Override
public Map> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
ImmutableMap.Builder> tableColumns = ImmutableMap.builder();
for (String schemaName : getSchemaNames(session, prefix.getSchema())) {
for (Table tpcdsTable : Table.getBaseTables()) {
if (prefix.getTable().map(tpcdsTable.getName()::equals).orElse(true)) {
ConnectorTableMetadata tableMetadata = getTableMetadata(schemaName, tpcdsTable);
tableColumns.put(new SchemaTableName(schemaName, tpcdsTable.getName()), tableMetadata.getColumns());
}
}
}
return tableColumns.buildOrThrow();
}
@Override
public List listTables(ConnectorSession session, Optional filterSchema)
{
ImmutableList.Builder builder = ImmutableList.builder();
for (String schemaName : getSchemaNames(session, filterSchema)) {
for (Table tpcdsTable : Table.getBaseTables()) {
builder.add(new SchemaTableName(schemaName, tpcdsTable.getName()));
}
}
return builder.build();
}
private List getSchemaNames(ConnectorSession session, Optional schemaName)
{
if (schemaName.isEmpty()) {
return listSchemaNames(session);
}
if (schemaNameToScaleFactor(schemaName.get()) > 0) {
return ImmutableList.of(schemaName.get());
}
return ImmutableList.of();
}
private static String scaleFactorSchemaName(double scaleFactor)
{
return "sf" + scaleFactor;
}
public static double schemaNameToScaleFactor(String schemaName)
{
if (TINY_SCHEMA_NAME.equals(schemaName)) {
return TINY_SCALE_FACTOR;
}
if (!schemaName.startsWith("sf")) {
return -1;
}
try {
return Double.parseDouble(schemaName.substring(2));
}
catch (Exception _) {
return -1;
}
}
public static Type getTrinoType(ColumnType tpcdsType)
{
return switch (tpcdsType.getBase()) {
case IDENTIFIER -> BigintType.BIGINT;
case INTEGER -> IntegerType.INTEGER;
case DATE -> DateType.DATE;
case DECIMAL -> createDecimalType(tpcdsType.getPrecision().get(), tpcdsType.getScale().get());
case CHAR -> createCharType(tpcdsType.getPrecision().get());
case VARCHAR -> createVarcharType(tpcdsType.getPrecision().get());
case TIME -> TimeType.TIME_MILLIS;
};
}
}