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.prestosql.tests.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.airlift.tpch.TpchTable;
import io.prestosql.plugin.tpch.TpchMetadata;
import io.prestosql.plugin.tpch.TpchRecordSetProvider;
import io.prestosql.plugin.tpch.TpchTableHandle;
import io.prestosql.spi.connector.ColumnHandle;
import io.prestosql.spi.connector.RecordCursor;
import io.prestosql.spi.connector.RecordSet;
import io.prestosql.spi.connector.SchemaTableName;
import io.prestosql.spi.predicate.TupleDomain;
import io.prestosql.spi.type.Type;
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();
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);
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.build();
}
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(final List values, List positions)
{
return Lists.transform(positions, position -> {
checkPositionIndex(position, values.size());
return values.get(position);
});
}
private static IndexedTable indexTable(RecordSet recordSet, final 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