Skip to content
Closed
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
82 changes: 82 additions & 0 deletions parquet-variant/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Copy link
Contributor

@aihuaxu aihuaxu Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @gene-db to drive the reference implementation.

I have a general question on the requirement: we implement mostly Parse_Json() in this PR. Are we required to construct variant with richer type - date, timestamp, etc.? May be out of scope for this PR. I have the implementation in Iceberg (apache/iceberg#11857 to add the full support. As I talked to @rdblue, that may not be required for Iceberg but I can include such implementation in Parquet after this PR if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think parse_json should be trying to determine what type a particular JSON string is supposed to be. The JSON spec doesn't have the richer types, so parse_json will not try to guess what the strings might be. It might be error-prone and would be costly in terms of performance. Therefore, parse_json will only use a subset of the variant types.

This PR also supports the variant builder, which supports creating variant values with all of the variant types.

<parent>
<groupId>org.apache.parquet</groupId>
<artifactId>parquet</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.16.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>parquet-variant</artifactId>
<packaging>jar</packaging>

<name>Apache Parquet Variant</name>
<url>https://parquet.apache.org</url>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.parquet</groupId>
<artifactId>parquet-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.parquet</groupId>
<artifactId>parquet-jackson</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>${jackson.groupId}</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>${jackson.groupId}</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
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.variant;

import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.Locale;
import java.util.UUID;

/**
* This converts Variant scalar values to JSON.
*/
public class DefaultScalarToJson implements Variant.ScalarToJson {
/** The format for a timestamp without time zone. */
private static final DateTimeFormatter TIMESTAMP_NTZ_FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendPattern("HH:mm:ss")
.appendFraction(ChronoField.MICRO_OF_SECOND, 6, 6, true)
.toFormatter(Locale.US);

/** The format for a timestamp without time zone, with nanosecond precision. */
private static final DateTimeFormatter TIMESTAMP_NANOS_NTZ_FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendPattern("HH:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 9, 9, true)
.toFormatter(Locale.US);

/** The format for a timestamp with time zone. */
private static final DateTimeFormatter TIMESTAMP_FORMATTER = new DateTimeFormatterBuilder()
.append(TIMESTAMP_NTZ_FORMATTER)
.appendOffset("+HH:MM", "+00:00")
.toFormatter(Locale.US);

/** The format for a timestamp with time zone, with nanosecond precision. */
private static final DateTimeFormatter TIMESTAMP_NANOS_FORMATTER = new DateTimeFormatterBuilder()
.append(TIMESTAMP_NANOS_NTZ_FORMATTER)
.appendOffset("+HH:MM", "+00:00")
.toFormatter(Locale.US);

/** The format for a time. */
private static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder()
.appendPattern("HH:mm:ss")
.appendFraction(ChronoField.MICRO_OF_SECOND, 6, 6, true)
.toFormatter(Locale.US);

public void writeNull(JsonGenerator gen) throws IOException {
gen.writeNull();
}

public void writeBoolean(JsonGenerator gen, boolean value) throws IOException {
gen.writeBoolean(value);
}

public void writeByte(JsonGenerator gen, byte value) throws IOException {
gen.writeNumber(value);
}

public void writeShort(JsonGenerator gen, short value) throws IOException {
gen.writeNumber(value);
}

public void writeInt(JsonGenerator gen, int value) throws IOException {
gen.writeNumber(value);
}

public void writeLong(JsonGenerator gen, long value) throws IOException {
gen.writeNumber(value);
}

public void writeFloat(JsonGenerator gen, float value) throws IOException {
gen.writeNumber(value);
}

public void writeDouble(JsonGenerator gen, double value) throws IOException {
gen.writeNumber(value);
}

public void writeString(JsonGenerator gen, String value) throws IOException {
gen.writeString(value);
}

public void writeBinary(JsonGenerator gen, byte[] value) throws IOException {
gen.writeString(Base64.getEncoder().encodeToString(value));
}

public void writeDecimal(JsonGenerator gen, BigDecimal value) throws IOException {
gen.writeNumber(value.toPlainString());
}

public void writeUUID(JsonGenerator gen, UUID value) throws IOException {
gen.writeString(value.toString());
}

public void writeDate(JsonGenerator gen, int value) throws IOException {
gen.writeString(LocalDate.ofEpochDay(value).toString());
}

public void writeTime(JsonGenerator gen, long microsSinceMidnight) throws IOException {
gen.writeString(TIME_FORMATTER.format(LocalTime.ofNanoOfDay(microsSinceMidnight * 1_000)));
}

public void writeTimestamp(JsonGenerator gen, long microsSinceEpoch) throws IOException {
gen.writeString(
TIMESTAMP_FORMATTER.format(microsToInstant(microsSinceEpoch).atZone(ZoneOffset.UTC)));
}

public void writeTimestampNtz(JsonGenerator gen, long microsSinceEpoch) throws IOException {
gen.writeString(
TIMESTAMP_NTZ_FORMATTER.format(microsToInstant(microsSinceEpoch).atZone(ZoneOffset.UTC)));
}

public void writeTimestampNanos(JsonGenerator gen, long nanosSinceEpoch) throws IOException {
gen.writeString(
TIMESTAMP_NANOS_FORMATTER.format(nanosToInstant(nanosSinceEpoch).atZone(ZoneOffset.UTC)));
}

public void writeTimestampNanosNtz(JsonGenerator gen, long nanosSinceEpoch) throws IOException {
gen.writeString(TIMESTAMP_NANOS_NTZ_FORMATTER.format(
nanosToInstant(nanosSinceEpoch).atZone(ZoneOffset.UTC)));
}

protected Instant microsToInstant(long microsSinceEpoch) {
return Instant.EPOCH.plus(microsSinceEpoch, ChronoUnit.MICROS);
}

protected Instant nanosToInstant(long timestampNanos) {
return Instant.EPOCH.plus(timestampNanos, ChronoUnit.NANOS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.variant;

/**
* An exception indicating that the Variant is malformed.
*/
public class MalformedVariantException extends RuntimeException {
public MalformedVariantException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.variant;

/**
* An exception indicating that the Variant contains an unknown type.
*/
public class UnknownVariantTypeException extends RuntimeException {
public final int typeId;

/**
* @param typeId the type id that was unknown
*/
public UnknownVariantTypeException(int typeId) {
super("Unknown type in Variant. id: " + typeId);
this.typeId = typeId;
}

/**
* @return the type id that was unknown
*/
public int typeId() {
return typeId;
}
}
Loading