Skip to content

Commit

Permalink
Refactoring to use VariantUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
aihuaxu committed Feb 2, 2025
1 parent 5ba965a commit b2290a0
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@

/** An variant array value. */
public interface VariantArray extends VariantValue {
default int numElements() {
throw new UnsupportedOperationException();
}

/** Returns the {@link VariantValue} at {@code index} in this array. */
VariantValue get(int index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
public class VariantArrayBuilder extends VariantBuilderBase {
private final List<Integer> offsets;

public VariantArrayBuilder(ByteBufferWrapper buffer, Dictionary dict) {
super(buffer, dict);
public VariantArrayBuilder(ByteBufferWrapper valueBuffer, Dictionary dict) {
super(valueBuffer, dict);
offsets = Lists.newArrayList();
}

public VariantObjectBuilder startObject() {
addOffset();
return new VariantObjectBuilder(getBuffer(), getDict());
return new VariantObjectBuilder(valueBuffer, dict);
}

public VariantArrayBuilder startArray() {
addOffset();
return new VariantArrayBuilder(getBuffer(), getDict());
return new VariantArrayBuilder(valueBuffer, dict);
}

public VariantArrayBuilder writeNull() {
Expand Down Expand Up @@ -111,10 +111,10 @@ public VariantArrayBuilder writeString(String str) {
}

private void addOffset() {
offsets.add(getBuffer().getPos() - getStartPos());
offsets.add(valueBuffer.pos() - startPos);
}

public void endArray() {
super.endArray(getStartPos(), offsets);
super.endArray(startPos, offsets);
}
}
39 changes: 18 additions & 21 deletions core/src/main/java/org/apache/iceberg/variants/VariantBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@
/** A builder class to build a primitive/array/object variant. */
public class VariantBuilder extends VariantBuilderBase {
public VariantBuilder() {
super(new VariantBuilderBase.ByteBufferWrapper(), new VariantBuilderBase.Dictionary());
super(new ByteBufferWrapper(), new Dictionary());
}

public VariantPrimitiveBuilder createPrimitive() {
VariantPrimitiveBuilder primitiveBuilder = new VariantPrimitiveBuilder(getBuffer(), getDict());
return primitiveBuilder;
return new VariantPrimitiveBuilder(valueBuffer, dict);
}

public VariantObjectBuilder startObject() {
return new VariantObjectBuilder(getBuffer(), getDict());
return new VariantObjectBuilder(valueBuffer, dict);
}

public VariantArrayBuilder startArray() {
return new VariantArrayBuilder(getBuffer(), getDict());
return new VariantArrayBuilder(valueBuffer, dict);
}

/**
Expand All @@ -63,13 +62,13 @@ public static Variant parseJson(String json) throws IOException {
parser.nextToken();

VariantBuilder builder = new VariantBuilder();
builder.buildJson(parser);
builder.parseJson(parser);

return builder.build();
}
}

private void buildJson(JsonParser parser) throws IOException {
private void parseJson(JsonParser parser) throws IOException {
JsonToken token = parser.currentToken();

if (token == null) {
Expand Down Expand Up @@ -108,28 +107,28 @@ private void buildJson(JsonParser parser) throws IOException {

private void writeObject(JsonParser parser) throws IOException {
List<VariantBuilderBase.FieldEntry> fields = Lists.newArrayList();
int startPos = getBuffer().getPos();
int startPos = valueBuffer.pos();

// Store object keys to dictionary of metadata
while (parser.nextToken() != JsonToken.END_OBJECT) {
String key = parser.currentName();
parser.nextToken(); // Move to the value

int id = getDict().add(key);
fields.add(new VariantBuilderBase.FieldEntry(key, id, getBuffer().getPos() - startPos));
buildJson(parser);
int id = dict.add(key);
fields.add(new VariantBuilderBase.FieldEntry(key, id, valueBuffer.pos() - startPos));
parseJson(parser);
}

endObject(startPos, fields);
}

private void writeArray(JsonParser parser) throws IOException {
List<Integer> offsets = Lists.newArrayList();
int startPos = getBuffer().getPos();
int startPos = valueBuffer.pos();

while (parser.nextToken() != JsonToken.END_ARRAY) {
offsets.add(getBuffer().getPos() - startPos);
buildJson(parser);
offsets.add(valueBuffer.pos() - startPos);
parseJson(parser);
}

endArray(startPos, offsets);
Expand All @@ -150,12 +149,10 @@ private void writeFloat(JsonParser parser) throws IOException {
}

/**
* Attempts to parse a JSON number as a decimal and write it. The input must meet the following
* criteria: - Be in a valid decimal format (integer with an optional '.'). - Not in scientific
* notation. - Fit within the precision and scale limits of decimal types.
* This function attempts to parse a JSON number and write it as a decimal value.
*
* @param input the input string representing the JSON number
* @return true if the decimal is valid and written successfully; false otherwise
* @param input the input string expecting to be in decimal format, not in scientific notation.
* @return true if the decimal is valid and written successfully; false otherwise.
*/
private boolean tryWriteDecimal(String input) {
// Validate that the input matches a decimal format and is not in scientific notation.
Expand All @@ -167,8 +164,8 @@ private boolean tryWriteDecimal(String input) {
BigDecimal decimalValue = new BigDecimal(input);

// Ensure the decimal value meets precision and scale limits.
if (decimalValue.scale() <= VariantConstants.MAX_DECIMAL16_PRECISION
&& decimalValue.precision() <= VariantConstants.MAX_DECIMAL16_PRECISION) {
if (decimalValue.scale() <= MAX_DECIMAL16_PRECISION
&& decimalValue.precision() <= MAX_DECIMAL16_PRECISION) {
writeDecimalInternal(decimalValue);
return true;
}
Expand Down
Loading

0 comments on commit b2290a0

Please sign in to comment.