-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b47de8
commit efe5d34
Showing
9 changed files
with
204 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
parquet-hadoop/src/test/java/org/apache/parquet/statistics/TestFloat16Statistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.parquet.statistics; | ||
|
||
import org.apache.parquet.Preconditions; | ||
import org.apache.parquet.example.data.Group; | ||
import org.apache.parquet.example.data.GroupFactory; | ||
import org.apache.parquet.example.data.simple.SimpleGroupFactory; | ||
import org.apache.parquet.hadoop.ParquetFileReader; | ||
import org.apache.parquet.hadoop.ParquetWriter; | ||
import org.apache.parquet.internal.column.columnindex.ColumnIndex; | ||
import org.apache.parquet.io.api.Binary; | ||
import org.apache.parquet.schema.MessageType; | ||
import org.apache.parquet.schema.Types; | ||
import org.apache.parquet.type.Float16; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.parquet.column.statistics.Statistics; | ||
import org.apache.parquet.hadoop.example.ExampleParquetWriter; | ||
import org.apache.parquet.hadoop.example.GroupWriteSupport; | ||
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; | ||
import org.apache.parquet.hadoop.util.HadoopInputFile; | ||
|
||
import static org.apache.parquet.schema.LogicalTypeAnnotation.float16Type; | ||
import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TestFloat16Statistics { | ||
|
||
@Rule | ||
public TemporaryFolder temp = new TemporaryFolder(); | ||
|
||
private short[] valuesInAscendingOrder = { | ||
(short) 0xfc00, // -Infinity | ||
(short) 0xc000, // -2.0 | ||
-Float16.MAX_VALUE, // -6.109476E-5 | ||
Float16.NEGATIVE_ZERO, // -0 | ||
Float16.POSITIVE_ZERO, // +0 | ||
Float16.MIN_VALUE, // 5.9604645E-8 | ||
Float16.MAX_VALUE, // 65504.0 | ||
(short) 0x7c00}; // Infinity | ||
|
||
@Test | ||
public void testFloat16ColumnIndex() throws IOException | ||
{ | ||
MessageType schema = Types.buildMessage(). | ||
required(FIXED_LEN_BYTE_ARRAY).as(float16Type()).length(2).named("col_float16").named("msg"); | ||
|
||
Configuration conf = new Configuration(); | ||
GroupWriteSupport.setSchema(schema, conf); | ||
|
||
GroupFactory factory = new SimpleGroupFactory(schema); | ||
Path path = newTempPath(); | ||
try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(path) | ||
.withConf(conf) | ||
.withDictionaryEncoding(false) | ||
.build()) { | ||
|
||
for (short value : valuesInAscendingOrder) { | ||
writer.write(factory.newGroup().append("col_float16", Binary.fromConstantByteArray(Float16.toBytesLittleEndian(value)))); | ||
} | ||
} | ||
|
||
try (ParquetFileReader reader = ParquetFileReader.open(HadoopInputFile.fromPath(path, new Configuration()))) { | ||
|
||
ColumnChunkMetaData column = reader.getFooter().getBlocks().get(0).getColumns().get(0); | ||
ColumnIndex index = reader.readColumnIndex(column); | ||
assertEquals(Collections.singletonList((short) 0xfc00), toFloat16List(index.getMinValues())); | ||
assertEquals(Collections.singletonList((short) 0x7c00), toFloat16List(index.getMaxValues())); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFloat16Statistics() throws IOException { | ||
for (int i = 0; i < valuesInAscendingOrder.length; ++i) { | ||
for (int j = 0; j < valuesInAscendingOrder.length; ++j) { | ||
int minIndex = i; | ||
int maxIndex = j; | ||
|
||
if (Float16.compare(valuesInAscendingOrder[i], valuesInAscendingOrder[j]) > 0) { | ||
minIndex = j; | ||
maxIndex = i; | ||
} | ||
|
||
// Refer to Float16Builder class | ||
if (valuesInAscendingOrder[minIndex] == Float16.POSITIVE_ZERO) { | ||
minIndex = 3; | ||
} | ||
if (valuesInAscendingOrder[maxIndex] == Float16.NEGATIVE_ZERO) { | ||
maxIndex = 4; | ||
} | ||
|
||
MessageType schema = Types.buildMessage(). | ||
required(FIXED_LEN_BYTE_ARRAY).as(float16Type()).length(2).named("col_float16").named("msg"); | ||
|
||
Configuration conf = new Configuration(); | ||
GroupWriteSupport.setSchema(schema, conf); | ||
|
||
GroupFactory factory = new SimpleGroupFactory(schema); | ||
Path path = newTempPath(); | ||
try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(path) | ||
.withConf(conf) | ||
.withDictionaryEncoding(false) | ||
.build()) { | ||
writer.write(factory.newGroup().append("col_float16", Binary.fromConstantByteArray(Float16.toBytesLittleEndian(valuesInAscendingOrder[i])))); | ||
writer.write(factory.newGroup().append("col_float16", Binary.fromConstantByteArray(Float16.toBytesLittleEndian(valuesInAscendingOrder[j])))); | ||
} | ||
|
||
try (ParquetFileReader reader = ParquetFileReader.open(HadoopInputFile.fromPath(path, new Configuration()))) { | ||
ColumnChunkMetaData column = reader.getFooter().getBlocks().get(0).getColumns().get(0); | ||
Statistics<?> statistics = column.getStatistics(); | ||
|
||
assertEquals(valuesInAscendingOrder[minIndex], Float16.fromBytesLittleEndian(statistics.getMinBytes())); | ||
assertEquals(valuesInAscendingOrder[maxIndex], Float16.fromBytesLittleEndian(statistics.getMaxBytes())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Path newTempPath() throws IOException { | ||
File file = temp.newFile(); | ||
Preconditions.checkArgument(file.delete(), "Could not remove temp file"); | ||
return new Path(file.getAbsolutePath()); | ||
} | ||
|
||
private static List<Short> toFloat16List(List<ByteBuffer> buffers) { | ||
return buffers.stream() | ||
.map(buffer -> Float16.fromBytesLittleEndian(buffer.array())) | ||
.collect(Collectors.toList()); | ||
} | ||
} |