Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for inserting Range values #3246

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
}
}