Skip to content

Commit

Permalink
Run mvn spotless:apply
Browse files Browse the repository at this point in the history
  • Loading branch information
gene-db committed Jan 21, 2025
1 parent 5997732 commit de96bac
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
/**
* An exception indicating that the Variant is malformed.
*/
public class MalformedVariantException extends RuntimeException {
}
public class MalformedVariantException extends RuntimeException {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package org.apache.parquet.variant;

import static java.time.temporal.ChronoField.*;
import static org.apache.parquet.variant.VariantUtil.*;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -35,10 +37,6 @@
import java.util.Base64;
import java.util.Locale;

import static java.time.temporal.ChronoField.*;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static org.apache.parquet.variant.VariantUtil.*;

/**
* This Variant class holds the Variant-encoded value and metadata binary values.
*/
Expand Down Expand Up @@ -143,8 +141,7 @@ public Type getType() {
* @return the number of object fields in the variant. `getType()` must be `Type.OBJECT`.
*/
public int objectSize() {
return handleObject(value, pos,
(size, idSize, offsetSize, idStart, offsetStart, dataStart) -> size);
return handleObject(value, pos, (size, idSize, offsetSize, idStart, offsetStart, dataStart) -> size);
}

// Find the field value whose key is equal to `key`. Return null if the key is not found.
Expand Down Expand Up @@ -292,7 +289,7 @@ public String toJson(ZoneId zoneId, boolean truncateTrailingZeros) {
*/
private static String escapeJson(String str) {
try (CharArrayWriter writer = new CharArrayWriter();
JsonGenerator gen = new JsonFactory().createGenerator(writer)) {
JsonGenerator gen = new JsonFactory().createGenerator(writer)) {
gen.writeString(str);
gen.flush();
return writer.toString();
Expand Down Expand Up @@ -329,15 +326,14 @@ private static void appendQuoted(StringBuilder sb, String str) {
.toFormatter(Locale.US);

/** The format for a timestamp without time zone, truncating trailing microsecond zeros. */
private static final DateTimeFormatter TIMESTAMP_NTZ_TRUNC_FORMATTER =
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendPattern("HH:mm:ss")
.optionalStart()
.appendFraction(MICRO_OF_SECOND, 0, 6, true)
.optionalEnd()
.toFormatter(Locale.US);
private static final DateTimeFormatter TIMESTAMP_NTZ_TRUNC_FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendPattern("HH:mm:ss")
.optionalStart()
.appendFraction(MICRO_OF_SECOND, 0, 6, true)
.optionalEnd()
.toFormatter(Locale.US);

/** The format for a timestamp with time zone, truncating trailing microsecond zeros. */
private static final DateTimeFormatter TIMESTAMP_TRUNC_FORMATTER = new DateTimeFormatterBuilder()
Expand All @@ -349,8 +345,8 @@ private static Instant microsToInstant(long microsSinceEpoch) {
return Instant.EPOCH.plus(microsSinceEpoch, ChronoUnit.MICROS);
}

private static void toJsonImpl(byte[] value, byte[] metadata, int pos, StringBuilder sb,
ZoneId zoneId, boolean truncateTrailingZeros) {
private static void toJsonImpl(
byte[] value, byte[] metadata, int pos, StringBuilder sb, ZoneId zoneId, boolean truncateTrailingZeros) {
switch (VariantUtil.getType(value, pos)) {
case OBJECT:
handleObject(value, pos, (size, idSize, offsetSize, idStart, offsetStart, dataStart) -> {
Expand Down Expand Up @@ -398,30 +394,43 @@ private static void toJsonImpl(byte[] value, byte[] metadata, int pos, StringBui
break;
case DECIMAL:
if (truncateTrailingZeros) {
sb.append(VariantUtil.getDecimal(value, pos).stripTrailingZeros().toPlainString());
sb.append(VariantUtil.getDecimal(value, pos)
.stripTrailingZeros()
.toPlainString());
} else {
sb.append(VariantUtil.getDecimal(value, pos).toPlainString());
}
break;
case DATE:
appendQuoted(sb, LocalDate.ofEpochDay((int) VariantUtil.getLong(value, pos)).toString());
appendQuoted(
sb,
LocalDate.ofEpochDay((int) VariantUtil.getLong(value, pos))
.toString());
break;
case TIMESTAMP:
if (truncateTrailingZeros) {
appendQuoted(sb, TIMESTAMP_TRUNC_FORMATTER.format(
microsToInstant(VariantUtil.getLong(value, pos)).atZone(zoneId)));
appendQuoted(
sb,
TIMESTAMP_TRUNC_FORMATTER.format(microsToInstant(VariantUtil.getLong(value, pos))
.atZone(zoneId)));
} else {
appendQuoted(sb, TIMESTAMP_FORMATTER.format(
microsToInstant(VariantUtil.getLong(value, pos)).atZone(zoneId)));
appendQuoted(
sb,
TIMESTAMP_FORMATTER.format(microsToInstant(VariantUtil.getLong(value, pos))
.atZone(zoneId)));
}
break;
case TIMESTAMP_NTZ:
if (truncateTrailingZeros) {
appendQuoted(sb, TIMESTAMP_NTZ_TRUNC_FORMATTER.format(
microsToInstant(VariantUtil.getLong(value, pos)).atZone(ZoneOffset.UTC)));
appendQuoted(
sb,
TIMESTAMP_NTZ_TRUNC_FORMATTER.format(microsToInstant(VariantUtil.getLong(value, pos))
.atZone(ZoneOffset.UTC)));
} else {
appendQuoted(sb, TIMESTAMP_NTZ_FORMATTER.format(
microsToInstant(VariantUtil.getLong(value, pos)).atZone(ZoneOffset.UTC)));
appendQuoted(
sb,
TIMESTAMP_NTZ_FORMATTER.format(microsToInstant(VariantUtil.getLong(value, pos))
.atZone(ZoneOffset.UTC)));
}
break;
case FLOAT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
*/
package org.apache.parquet.variant;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static org.apache.parquet.variant.VariantUtil.*;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.exc.InputCoercionException;

import static org.apache.parquet.variant.VariantUtil.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
* Builder for creating Variant value and metadata.
Expand Down Expand Up @@ -80,8 +79,7 @@ public static Variant parseJson(String json, VariantBuilder builder) throws IOEx
* @throws VariantSizeLimitException if the resulting variant value or metadata would exceed
* the size limit
*/
public static Variant parseJson(JsonParser parser, VariantBuilder builder)
throws IOException {
public static Variant parseJson(JsonParser parser, VariantBuilder builder) throws IOException {
builder.buildFromJsonParser(parser);
return builder.result();
}
Expand All @@ -106,7 +104,7 @@ public Variant result() {
if (maxSize > sizeLimitBytes) {
throw new VariantSizeLimitException();
}
int offsetSize = getMinIntegerSize((int)maxSize);
int offsetSize = getMinIntegerSize((int) maxSize);

int offsetStart = 1 + offsetSize;
int stringStart = offsetStart + (numKeys + 1) * offsetSize;
Expand Down Expand Up @@ -346,8 +344,7 @@ public void finishWritingObject(int start, ArrayList<FieldEntry> fields) {
for (int i = 0; i < size; ++i) {
int oldOffset = fields.get(i).offset;
int fieldSize = VariantUtil.valueSize(writeBuffer, start + oldOffset);
System.arraycopy(writeBuffer, start + oldOffset,
writeBuffer, start + currentOffset, fieldSize);
System.arraycopy(writeBuffer, start + oldOffset, writeBuffer, start + currentOffset, fieldSize);
fields.set(i, fields.get(i).withNewOffset(currentOffset));
currentOffset += fieldSize;
}
Expand Down Expand Up @@ -620,6 +617,7 @@ private boolean tryParseDecimal(String input) {

/** The buffer for building the Variant value. The first `writePos` bytes have been written. */
private byte[] writeBuffer = new byte[128];

private int writePos = 0;
/** The dictionary for mapping keys to monotonically increasing ids. */
private final HashMap<String, Integer> dictionary = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@
* An exception indicating that the metadata or data size of the Variant exceeds the
* configured size limit.
*/
public class VariantSizeLimitException extends RuntimeException {
}
public class VariantSizeLimitException extends RuntimeException {}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
/**
* This class defines constants related to the Variant format and provides functions for
* manipulating Variant binaries.
*
* A Variant is made up of 2 binaries: value and metadata. A Variant value consists of a one-byte
* header and a number of content bytes (can be zero). The header byte is divided into upper 6 bits
* (called "type info") and lower 2 bits (called "basic type"). The content format is explained in
* the below constants for all possible basic type and type info values.
*
* The Variant metadata includes a version id and a dictionary of distinct strings (case-sensitive).
* Its binary format is:
* - Version: 1-byte unsigned integer. The only acceptable value is 1 currently.
Expand Down Expand Up @@ -184,14 +184,14 @@ public static byte shortStrHeader(int size) {
}

public static byte objectHeader(boolean largeSize, int idSize, int offsetSize) {
return (byte) (((largeSize ? 1 : 0) << (BASIC_TYPE_BITS + 4)) |
((idSize - 1) << (BASIC_TYPE_BITS + 2)) |
((offsetSize - 1) << BASIC_TYPE_BITS) | OBJECT);
return (byte) (((largeSize ? 1 : 0) << (BASIC_TYPE_BITS + 4))
| ((idSize - 1) << (BASIC_TYPE_BITS + 2))
| ((offsetSize - 1) << BASIC_TYPE_BITS)
| OBJECT);
}

public static byte arrayHeader(boolean largeSize, int offsetSize) {
return (byte) (((largeSize ? 1 : 0) << (BASIC_TYPE_BITS + 2)) |
((offsetSize - 1) << BASIC_TYPE_BITS) | ARRAY);
return (byte) (((largeSize ? 1 : 0) << (BASIC_TYPE_BITS + 2)) | ((offsetSize - 1) << BASIC_TYPE_BITS) | ARRAY);
}

public static MalformedVariantException malformedVariant() {
Expand Down Expand Up @@ -348,12 +348,17 @@ public static int valueSize(byte[] value, int pos) {
case SHORT_STR:
return 1 + typeInfo;
case OBJECT:
return handleObject(value, pos,
return handleObject(
value,
pos,
(size, idSize, offsetSize, idStart, offsetStart, dataStart) ->
dataStart - pos + readUnsigned(value, offsetStart + size * offsetSize, offsetSize));
case ARRAY:
return handleArray(value, pos, (size, offsetSize, offsetStart, dataStart) ->
dataStart - pos + readUnsigned(value, offsetStart + size * offsetSize, offsetSize));
return handleArray(
value,
pos,
(size, offsetSize, offsetStart, dataStart) ->
dataStart - pos + readUnsigned(value, offsetStart + size * offsetSize, offsetSize));
default:
switch (typeInfo) {
case NULL:
Expand Down
Loading

0 comments on commit de96bac

Please sign in to comment.