Skip to content

Commit

Permalink
feat: Add support for inserting Range values (#3246)
Browse files Browse the repository at this point in the history
* feat: Add support for inserting Range values

* fix: IT assert to use size instead of const
  • Loading branch information
PhongChuong committed Apr 19, 2024
1 parent ee94a6e commit ff1ebc6
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.auto.value.AutoValue;
import com.google.cloud.bigquery.FieldValue.Attribute;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import javax.annotation.Nullable;

Expand All @@ -44,6 +45,18 @@ public FieldValue getEnd() {
@Nullable
abstract String getEndInner();

/** Returns the start and end values of this range. */
public ImmutableMap<String, String> getValues() {
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
if (!getStart().isNull()) {
result.put("start", getStart().getStringValue());
}
if (!getEnd().isNull()) {
result.put("end", getEnd().getStringValue());
}
return result.build();
}

/** Returns the type of the range. */
@Nullable
public abstract FieldElementType getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

public class RangeTest {
Expand Down Expand Up @@ -77,6 +78,17 @@ public void testToBuilder() {
compareRange(RANGE_TIMESTAMP, RANGE_TIMESTAMP.toBuilder().build());
}

@Test
public void testGetValues() {
compareRange(null, null, Range.of("[null, NULL)").getValues());
compareRange(null, null, Range.of("[unbounded, UNBOUNDED)").getValues());
compareRange(null, null, Range.of("[nUlL, uNbOuNdEd)").getValues());

compareRange(null, "2020-12-31", Range.of("[null, 2020-12-31)").getValues());
compareRange("2020-01-01", null, Range.of("[2020-01-01, null)").getValues());
compareRange("2020-01-01", "2020-12-31", Range.of("[2020-01-01, 2020-12-31)").getValues());
}

private static void compareRange(Range expected, Range value) {
assertEquals(expected.getStart(), value.getStart());
assertEquals(expected.getEnd(), value.getEnd());
Expand All @@ -97,4 +109,10 @@ private static void compareRange(String expectedStart, String expectedEnd, Range
assertEquals(expectedEnd, range.getEnd().getStringValue());
}
}

private static void compareRange(
String expectedStart, String expectedEnd, ImmutableMap<String, String> values) {
assertEquals(expectedStart, values.get("start"));
assertEquals(expectedEnd, values.get("end"));
}
}

0 comments on commit ff1ebc6

Please sign in to comment.