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.
/*
* 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.testing.tpch;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import io.airlift.slice.Slice;
import io.trino.plugin.tpch.DecimalTypeMapping;
import io.trino.plugin.tpch.TpchMetadata;
import io.trino.plugin.tpch.TpchRecordSetProvider;
import io.trino.plugin.tpch.TpchTableHandle;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.RecordCursor;
import io.trino.spi.connector.RecordSet;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.predicate.TupleDomain;
import io.trino.spi.type.Type;
import io.trino.tpch.TpchTable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkPositionIndex;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Objects.requireNonNull;
public class TpchIndexedData
{
private final Map, IndexedTable> indexedTables;
public TpchIndexedData(TpchIndexSpec tpchIndexSpec)
{
requireNonNull(tpchIndexSpec, "tpchIndexSpec is null");
TpchMetadata tpchMetadata = new TpchMetadata();
TpchRecordSetProvider tpchRecordSetProvider = new TpchRecordSetProvider(DecimalTypeMapping.DOUBLE);
ImmutableMap.Builder, IndexedTable> indexedTablesBuilder = ImmutableMap.builder();
Set tables = tpchIndexSpec.listIndexedTables();
for (TpchScaledTable table : tables) {
SchemaTableName tableName = new SchemaTableName("sf" + table.getScaleFactor(), table.getTableName());
TpchTableHandle tableHandle = tpchMetadata.getTableHandle(null, tableName, Optional.empty(), Optional.empty());
Map columnHandles = new LinkedHashMap<>(tpchMetadata.getColumnHandles(null, tableHandle));
for (Set columnNames : tpchIndexSpec.getColumnIndexes(table)) {
List keyColumnNames = ImmutableList.copyOf(columnNames); // Finalize the key order
Set keyColumns = keyColumnNames.stream()
.map(name -> new TpchScaledColumn(table, name))
.collect(toImmutableSet());
TpchTable> tpchTable = TpchTable.getTable(table.getTableName());
RecordSet recordSet = tpchRecordSetProvider.getRecordSet(tpchTable, ImmutableList.copyOf(columnHandles.values()), table.getScaleFactor(), 0, 1, TupleDomain.all());
IndexedTable indexedTable = indexTable(recordSet, ImmutableList.copyOf(columnHandles.keySet()), keyColumnNames);
indexedTablesBuilder.put(keyColumns, indexedTable);
}
}
indexedTables = indexedTablesBuilder.buildOrThrow();
}
public Optional getIndexedTable(String tableName, double scaleFactor, Set indexColumnNames)
{
TpchScaledTable table = new TpchScaledTable(tableName, scaleFactor);
Set indexColumns = indexColumnNames.stream()
.map(name -> new TpchScaledColumn(table, name))
.collect(toImmutableSet());
return Optional.ofNullable(indexedTables.get(indexColumns));
}
private static List extractPositionValues(List values, List positions)
{
return Lists.transform(positions, position -> {
checkPositionIndex(position, values.size());
return values.get(position);
});
}
private static IndexedTable indexTable(RecordSet recordSet, List outputColumns, List keyColumns)
{
List keyPositions = keyColumns.stream()
.map(columnName -> {
int position = outputColumns.indexOf(columnName);
checkState(position != -1);
return position;
})
.collect(toImmutableList());
ImmutableListMultimap.Builder indexedValuesBuilder = ImmutableListMultimap.builder();
List outputTypes = recordSet.getColumnTypes();
List keyTypes = extractPositionValues(outputTypes, keyPositions);
RecordCursor cursor = recordSet.cursor();
while (cursor.advanceNextPosition()) {
List