Skip to content

Commit

Permalink
Add Indexes and Stats format reader and writer
Browse files Browse the repository at this point in the history
Add reader and writer for the Index and Statistics File Format.
  • Loading branch information
findepi committed Apr 22, 2022
1 parent 454e246 commit bcdd2f1
Show file tree
Hide file tree
Showing 20 changed files with 1,233 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ project(':iceberg-core') {
exclude group: 'org.tukaani' // xz compression is not supported
}

implementation 'io.airlift:aircompressor'
implementation 'org.apache.httpcomponents.client5:httpclient5'
implementation "com.fasterxml.jackson.core:jackson-databind"
implementation "com.fasterxml.jackson.core:jackson-core"
Expand Down
77 changes: 77 additions & 0 deletions core/src/main/java/org/apache/iceberg/stats/BlobMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.iceberg.stats;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;

public class BlobMetadata {
private final String type;
private final Set<Integer> columns;
private final long offset;
private final long length;
private final String compressionCodec;

@JsonCreator
public BlobMetadata(
@JsonProperty("type") String type,
@JsonProperty("columns") Set<Integer> columns,
@JsonProperty("offset") long offset,
@JsonProperty("length") long length,
@JsonProperty("compression_codec") @Nullable String compressionCodec) {
this.type = Preconditions.checkNotNull(type, "type is null");
this.columns = ImmutableSet.copyOf(Preconditions.checkNotNull(columns, "columns is null"));
this.offset = offset;
this.length = length;
this.compressionCodec = compressionCodec;
}

public String type() {
return type;
}

public Set<Integer> columns() {
return columns;
}

/**
* Offset in the file
*/
public long offset() {
return offset;
}

/**
* Length in the file
*/
public long length() {
return length;
}

@Nullable
public String compressionCodec() {
return compressionCodec;
}
}
49 changes: 49 additions & 0 deletions core/src/main/java/org/apache/iceberg/stats/FileMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.iceberg.stats;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;

public class FileMetadata {
private final List<BlobMetadata> blobs;
private final Map<String, String> properties;

@JsonCreator
public FileMetadata(
@JsonProperty("blobs") List<BlobMetadata> blobs,
@JsonProperty("properties") Map<String, String> properties) {
this.blobs = ImmutableList.copyOf(Preconditions.checkNotNull(blobs, "blobs is null"));
this.properties = ImmutableMap.copyOf(Preconditions.checkNotNull(properties, "properties is null"));
}

public List<BlobMetadata> blobs() {
return blobs;
}

public Map<String, String> properties() {
return properties;
}
}
147 changes: 147 additions & 0 deletions core/src/main/java/org/apache/iceberg/stats/FileMetadataParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.iceberg.stats;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Set;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.util.JsonUtil;

public final class FileMetadataParser {

private FileMetadataParser() {
}

private static final String BLOBS = "blobs";
private static final String PROPERTIES = "properties";

private static final String TYPE = "type";
private static final String COLUMNS = "columns";
private static final String OFFSET = "offset";
private static final String LENGTH = "length";
private static final String COMPRESSION_CODEC = "compression_codec";

public static String toJson(FileMetadata fileMetadata) {
try {
StringWriter writer = new StringWriter();
JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
generator.useDefaultPrettyPrinter();
toJson(fileMetadata, generator);
generator.flush();
return writer.toString();
} catch (IOException e) {
throw new UncheckedIOException("Failed to write json for: " + fileMetadata, e);
}
}

public static FileMetadata fromJson(String json) {
try {
return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static FileMetadata fromJson(JsonNode json) {
return fileMetadataFromJson(json);
}

static void toJson(FileMetadata fileMetadata, JsonGenerator generator) throws IOException {
generator.writeStartObject();

generator.writeArrayFieldStart(BLOBS);
for (BlobMetadata blobMetadata : fileMetadata.blobs()) {
toJson(blobMetadata, generator);
}
generator.writeEndArray();

generator.writeObjectFieldStart(PROPERTIES);
for (Map.Entry<String, String> entry : fileMetadata.properties().entrySet()) {
generator.writeStringField(entry.getKey(), entry.getValue());
}
generator.writeEndObject();

generator.writeEndObject();
}

static FileMetadata fileMetadataFromJson(JsonNode json) {

ImmutableList.Builder<BlobMetadata> blobs = ImmutableList.builder();
JsonNode blobsJson = json.get(BLOBS);
Preconditions.checkArgument(blobsJson != null && blobsJson.isArray(),
"Cannot parse blobs from non-array: %s", blobsJson);
for (JsonNode blobJson : blobsJson) {
blobs.add(blobMetadataFromJson(blobJson));
}

Map<String, String> properties = ImmutableMap.of();
JsonNode propertiesJson = json.get(PROPERTIES);
if (propertiesJson != null) {
properties = JsonUtil.getStringMap(PROPERTIES, json);
}

return new FileMetadata(
blobs.build(),
properties);
}

static void toJson(BlobMetadata blobMetadata, JsonGenerator generator) throws IOException {
generator.writeStartObject();

generator.writeStringField(TYPE, blobMetadata.type());

generator.writeArrayFieldStart(COLUMNS);
for (int column : blobMetadata.columns()) {
generator.writeNumber(column);
}
generator.writeEndArray();

generator.writeNumberField(OFFSET, blobMetadata.offset());
generator.writeNumberField(LENGTH, blobMetadata.length());

if (blobMetadata.compressionCodec() != null) {
generator.writeStringField(COMPRESSION_CODEC, blobMetadata.compressionCodec());
}

generator.writeEndObject();
}

static BlobMetadata blobMetadataFromJson(JsonNode json) {
String type = JsonUtil.getString(TYPE, json);
Set<Integer> columns = JsonUtil.getIntegerSet(COLUMNS, json);
long offset = JsonUtil.getLong(OFFSET, json);
long length = JsonUtil.getLong(LENGTH, json);
String compressionCodec = JsonUtil.getStringOrNull(COMPRESSION_CODEC, json);

return new BlobMetadata(
type,
columns,
offset,
length,
compressionCodec);
}
}
35 changes: 35 additions & 0 deletions core/src/main/java/org/apache/iceberg/stats/StandardBlobTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.iceberg.stats;

public final class StandardBlobTypes {
private StandardBlobTypes() {
}

/**
* 8-bytes integer stored little-endian and representing number of distinct values
*/
public static final String NDV_LONG_LITTLE_ENDIAN = "ndv-long-little-endian";

/**
* A serialized form of a "compact" Theta sketch produced by the <a href="https://datasketches.apache.org/">Apache DataSketches</a> library
*/
public static final String APACHE_DATASKETCHES_THETA_V1 = "apache-datasketches-theta-v1";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.iceberg.stats;

import org.apache.iceberg.relocated.com.google.common.base.Preconditions;

public enum StatsCompressionCodec {
/**
* LZ4 single compression frame with content size present
*/
LZ4("lz4"),

/**
* Zstandard single compression frame with content size present
*/
ZSTD("zstd"),
/**/;

private final String codecName;

StatsCompressionCodec(String codecName) {
this.codecName = codecName;
}

public String getCodecName() {
return codecName;
}

public static StatsCompressionCodec forName(String codecName) {
Preconditions.checkNotNull(codecName, "codecName is null");
for (StatsCompressionCodec value : values()) {
if (value.getCodecName().equals(codecName)) {
return value;
}
}
throw new IllegalArgumentException("Unrecognized codec name " + codecName);
}
}
Loading

0 comments on commit bcdd2f1

Please sign in to comment.