Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve conversion to domain #7754

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static EquatableValueSet of(Type type, Object first, Object... rest)
return new EquatableValueSet(type, true, set);
}

static EquatableValueSet copyOf(Type type, Collection<Object> values)
static EquatableValueSet copyOf(Type type, Collection<?> values)
{
return new EquatableValueSet(type, true, values.stream()
.map(value -> ValueEntry.create(type, value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static ValueSet of(Type type, Object first, Object... rest)
throw new IllegalArgumentException("Cannot create discrete ValueSet with non-comparable type: " + type);
}

static ValueSet copyOf(Type type, Collection<Object> values)
static ValueSet copyOf(Type type, Collection<?> values)
{
if (type.isOrderable()) {
return SortedRangeSet.of(type, values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.VerboseMode;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -244,6 +245,33 @@ private SortedRangeSet generateRangeSet(int size)
}
}

@Test
public void test()
{
Data data = new Data();
data.init();

benchmarkBuilder(data);

equalsSmall(data);
equalsLarge(data);

unionSmall(data);
unionLarge(data);

overlapsSmall(data);
overlapsLarge(data);

containsValueSmall(data);
containsValueLarge(data);

complementSmall(data);
complementLarge(data);

getOrderedRangesSmall(data);
getOrderedRangesLarge(data);
}

public static void main(String[] args)
throws RunnerException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,56 +308,51 @@ public static Domain getDomain(Type type, DictionaryDescriptor dictionaryDescrip

int dictionarySize = dictionaryPage.get().getDictionarySize();
if (type.equals(BIGINT) && columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == PrimitiveTypeName.INT64) {
List<Domain> domains = new ArrayList<>();
List<Long> values = new ArrayList<>(dictionarySize);
for (int i = 0; i < dictionarySize; i++) {
domains.add(Domain.singleValue(type, dictionary.decodeToLong(i)));
values.add(dictionary.decodeToLong(i));
}
domains.add(Domain.onlyNull(type));
return Domain.union(domains);
return Domain.create(ValueSet.copyOf(type, values), true);
}

if ((type.equals(BIGINT) || type.equals(DATE)) && columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == PrimitiveTypeName.INT32) {
List<Domain> domains = new ArrayList<>();
List<Long> values = new ArrayList<>(dictionarySize);
for (int i = 0; i < dictionarySize; i++) {
domains.add(Domain.singleValue(type, (long) dictionary.decodeToInt(i)));
values.add((long) dictionary.decodeToInt(i));
}
domains.add(Domain.onlyNull(type));
return Domain.union(domains);
return Domain.create(ValueSet.copyOf(type, values), true);
}

if (type.equals(DOUBLE) && columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == PrimitiveTypeName.DOUBLE) {
List<Domain> domains = new ArrayList<>();
List<Double> values = new ArrayList<>(dictionarySize);
for (int i = 0; i < dictionarySize; i++) {
double value = dictionary.decodeToDouble(i);
if (Double.isNaN(value)) {
return Domain.all(type);
}
domains.add(Domain.singleValue(type, value));
values.add(value);
}
domains.add(Domain.onlyNull(type));
return Domain.union(domains);
return Domain.create(ValueSet.copyOf(type, values), true);
}

if (type.equals(DOUBLE) && columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == PrimitiveTypeName.FLOAT) {
List<Domain> domains = new ArrayList<>();
List<Double> values = new ArrayList<>(dictionarySize);
for (int i = 0; i < dictionarySize; i++) {
float value = dictionary.decodeToFloat(i);
if (Float.isNaN(value)) {
return Domain.all(type);
}
domains.add(Domain.singleValue(type, (double) value));
values.add((double) value);
}
domains.add(Domain.onlyNull(type));
return Domain.union(domains);
return Domain.create(ValueSet.copyOf(type, values), true);
}

if (type instanceof VarcharType && columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == PrimitiveTypeName.BINARY) {
List<Domain> domains = new ArrayList<>();
List<Slice> values = new ArrayList<>(dictionarySize);
for (int i = 0; i < dictionarySize; i++) {
domains.add(Domain.singleValue(type, Slices.wrappedBuffer(dictionary.decodeToBinary(i).getBytes())));
values.add(Slices.wrappedBuffer(dictionary.decodeToBinary(i).getBytes()));
}
domains.add(Domain.onlyNull(type));
return Domain.union(domains);
return Domain.create(ValueSet.copyOf(type, values), true);
}

return Domain.all(type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.parquet.predicate;

import io.airlift.slice.DynamicSliceOutput;
import io.airlift.slice.Slice;
import io.trino.parquet.DictionaryPage;
import io.trino.parquet.ParquetEncoding;
import io.trino.spi.predicate.Domain;
import org.apache.parquet.column.ColumnDescriptor;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.VerboseMode;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

import static io.trino.spi.type.BigintType.BIGINT;
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64;

@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
public class BenchmarkTupleDomainParquetPredicate
{
@Benchmark
public List<?> domainFromDictionary(Data data)
{
List<Domain> result = new ArrayList<>(data.bigintDictionaries.size());
for (DictionaryDescriptor dictionary : data.bigintDictionaries) {
result.add(TupleDomainParquetPredicate.getDomain(BIGINT, dictionary));
}
return result;
}

@State(Scope.Thread)
public static class Data
{
public List<DictionaryDescriptor> bigintDictionaries;

@Setup(Level.Iteration)
public void init()
{
bigintDictionaries = new ArrayList<>();

for (int i = 0; i < 1_000; i++) {
bigintDictionaries.add(createBigintDictionary());
}
}

private DictionaryDescriptor createBigintDictionary()
{
int size = 1_000;
Slice slice;
try (DynamicSliceOutput sliceOutput = new DynamicSliceOutput(0)) {
for (int i = 0; i < size; i++) {
sliceOutput.appendLong(ThreadLocalRandom.current().nextLong());
}
slice = sliceOutput.slice();
}
catch (IOException e) {
throw new RuntimeException(e);
}

return new DictionaryDescriptor(
new ColumnDescriptor(new String[] {"path"}, INT64, 0, 0),
Optional.of(
new DictionaryPage(
slice,
slice.length(),
size,
ParquetEncoding.PLAIN)));
}
}

@Test
public void test()
{
Data data = new Data();
data.init();

domainFromDictionary(data);
}

public static void main(String[] args)
throws RunnerException
{
Options options = new OptionsBuilder()
.verbosity(VerboseMode.NORMAL)
.include(".*" + BenchmarkTupleDomainParquetPredicate.class.getSimpleName() + ".*")
.build();

new Runner(options).run();
}
}