forked from GoogleCloudDataproc/spark-spanner-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Write struct support #15
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
Open
stevelordbq
wants to merge
6
commits into
main
Choose a base branch
from
write-struct-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8b62c4
Write struct initial version
stevelordbq 69765bf
Write struct add unit test for struct array
stevelordbq d15abed
Write struct extend unit test for struct array
stevelordbq 547383f
Spanner Write - assume Spark stores strings at UTF8String instead of …
stevelordbq 0e7d5b8
Spanner Write - add types to struct test
stevelordbq e893f55
Spanner Write - PR comments
stevelordbq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| import com.google.cloud.Date; | ||
| import com.google.cloud.Timestamp; | ||
| import com.google.cloud.spanner.Mutation; | ||
| import com.google.cloud.spanner.Struct; | ||
| import com.google.cloud.spanner.Type; | ||
| import com.google.cloud.spanner.Value; | ||
| import java.math.MathContext; | ||
| import java.math.RoundingMode; | ||
|
|
@@ -29,6 +31,7 @@ | |
| import java.util.Collections; | ||
| import java.util.function.BiConsumer; | ||
| import org.apache.spark.sql.catalyst.InternalRow; | ||
| import org.apache.spark.sql.catalyst.expressions.GenericInternalRow; | ||
| import org.apache.spark.sql.catalyst.util.ArrayData; | ||
| import org.apache.spark.sql.types.DataType; | ||
| import org.apache.spark.sql.types.DataTypes; | ||
|
|
@@ -56,14 +59,25 @@ public class SpannerWriterUtilsTest { | |
| @RunWith(Parameterized.class) | ||
| public static class ScalarTests { | ||
| private static final byte[] BYTE_DATA = {95, -10, 127}; | ||
| private static final Object[] structValues = | ||
| new Object[] { | ||
| 100L, | ||
| UTF8String.fromString("str_value"), | ||
| true, | ||
| 95.5, | ||
| BYTE_DATA, | ||
| 1704067200000000L, | ||
| 19723, | ||
| decimal | ||
| }; | ||
|
|
||
| // Parameters for each test case: [ColumnName, DataType, MockValue, ExpectedSpannerValue] | ||
| @Parameters(name = "Testing {0}") | ||
| public static Collection<Object[]> data() { | ||
| return Arrays.asList( | ||
| new Object[][] { | ||
| {"long", DataTypes.LongType, 100L, Value.int64(100L)}, | ||
| {"string", DataTypes.StringType, "Hello", Value.string("Hello")}, | ||
| {"string", DataTypes.StringType, UTF8String.fromString("Hello"), Value.string("Hello")}, | ||
| {"boolean", DataTypes.BooleanType, true, Value.bool(true)}, | ||
| {"double", DataTypes.DoubleType, 95.5, Value.float64(95.5)}, | ||
| {"binary", DataTypes.BinaryType, BYTE_DATA, Value.bytes(ByteArray.copyFrom(BYTE_DATA))}, | ||
|
|
@@ -74,7 +88,48 @@ public static Collection<Object[]> data() { | |
| Value.timestamp(Timestamp.ofTimeMicroseconds(1704067200000000L)) | ||
| }, | ||
| {"dt", DataTypes.DateType, 19723, Value.date(Date.fromYearMonthDay(2024, 1, 1))}, | ||
| {"decimal", new DecimalType(38, 9), decimal, Value.numeric(jbd)} | ||
| {"decimal", new DecimalType(38, 9), decimal, Value.numeric(jbd)}, | ||
| { | ||
| "struct", | ||
| new StructType() | ||
| .add("long_field", DataTypes.LongType) | ||
| .add("str_field", DataTypes.StringType) | ||
| .add("bool_field", DataTypes.BooleanType) | ||
| .add("double_field", DataTypes.DoubleType) | ||
| .add("binary_field", DataTypes.BinaryType) | ||
| .add("ts_field", DataTypes.TimestampType) | ||
| .add("dt_field", DataTypes.DateType) | ||
| .add("decimal_field", new DecimalType(38, 9)), // TODO: Add Struct | ||
|
stevelordbq marked this conversation as resolved.
Outdated
|
||
| new GenericInternalRow(structValues), | ||
| Value.struct( | ||
| Type.struct( | ||
| Type.StructField.of("long_field", Type.int64()), | ||
| Type.StructField.of("str_field", Type.string()), | ||
| Type.StructField.of("bool_field", Type.bool()), | ||
| Type.StructField.of("double_field", Type.float64()), | ||
| Type.StructField.of("binary_field", Type.bytes()), | ||
| Type.StructField.of("ts_field", Type.timestamp()), | ||
| Type.StructField.of("dt_field", Type.date()), | ||
| Type.StructField.of("decimal_field", Type.numeric())), | ||
| Struct.newBuilder() | ||
| .set("long_field") | ||
| .to(100L) | ||
| .set("str_field") | ||
| .to("str_value") | ||
| .set("bool_field") | ||
| .to(true) | ||
| .set("double_field") | ||
| .to(95.5) | ||
| .set("binary_field") | ||
| .to(ByteArray.copyFrom(BYTE_DATA)) | ||
| .set("ts_field") | ||
| .to(Timestamp.ofTimeMicroseconds(1704067200000000L)) | ||
| .set("dt_field") | ||
| .to(Date.fromYearMonthDay(2024, 1, 1)) | ||
| .set("decimal_field") | ||
| .to(jbd) | ||
| .build()) | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -102,7 +157,7 @@ public void testScalarConversion() { | |
| // Setup specific getter based on type | ||
| if (sparkType == DataTypes.LongType) when(row.getLong(0)).thenReturn((Long) mockValue); | ||
| else if (sparkType == DataTypes.StringType) | ||
| when(row.getString(0)).thenReturn((String) mockValue); | ||
| when(row.getUTF8String(0)).thenReturn((UTF8String) mockValue); | ||
| else if (sparkType == DataTypes.BooleanType) | ||
| when(row.getBoolean(0)).thenReturn((Boolean) mockValue); | ||
| else if (sparkType == DataTypes.DoubleType) | ||
|
|
@@ -114,6 +169,8 @@ else if (sparkType == DataTypes.TimestampType) | |
| else if (sparkType == DataTypes.DateType) when(row.getInt(0)).thenReturn((int) mockValue); | ||
| else if (sparkType instanceof DecimalType) | ||
| when(row.getDecimal(0, 38, 9)).thenReturn((Decimal) mockValue); | ||
| else if (sparkType instanceof StructType) | ||
| when(row.getStruct(0, 8)).thenReturn((InternalRow) mockValue); | ||
|
stevelordbq marked this conversation as resolved.
Outdated
|
||
|
|
||
| // 3. Execute | ||
| com.google.cloud.spanner.Mutation mutation = | ||
|
|
@@ -143,6 +200,7 @@ public static Collection<Object[]> data() { | |
| {"timestamp", DataTypes.TimestampType, Value.timestamp(null)}, | ||
| {"date", DataTypes.DateType, Value.date(null)}, | ||
| {"decimal", new DecimalType(38, 9), Value.numeric(null)}, | ||
| {"struct", new StructType(), Value.struct(Struct.newBuilder().build())}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| "long_array", | ||
| DataTypes.createArrayType(DataTypes.LongType), | ||
|
|
@@ -174,6 +232,11 @@ public static Collection<Object[]> data() { | |
| "decimal_array", | ||
| DataTypes.createArrayType(new DecimalType(38, 9)), | ||
| Value.numericArray(null) | ||
| }, | ||
| { | ||
| "struct_array", | ||
| DataTypes.createArrayType(new StructType()), | ||
| Value.structArray(Type.struct(), null) | ||
| } | ||
| }); | ||
| } | ||
|
|
@@ -341,6 +404,39 @@ public static Collection<Object[]> data() { | |
| ad, | ||
| (Decimal[]) d, | ||
| (i, val) -> when(ad.getDecimal(i, 38, 9)).thenReturn((Decimal) val)) | ||
| }, | ||
|
|
||
| // 9. Struct Array | ||
| { | ||
| "struct_array", | ||
| new StructType() | ||
| .add("str_field", DataTypes.StringType) | ||
| .add("bool_field", DataTypes.BooleanType), | ||
| new InternalRow[] { | ||
| mock(InternalRow.class) | ||
| }, // Use InternalRow instead of Spanner Struct | ||
| Value.structArray( | ||
| Type.struct( | ||
| Type.StructField.of("str_field", Type.string()), | ||
| Type.StructField.of("bool_field", Type.bool())), | ||
| Collections.singletonList( | ||
| Struct.newBuilder() | ||
| .set("str_field") | ||
| .to("str_value") | ||
| .set("bool_field") | ||
| .to(true) | ||
| .build())), | ||
| (BiConsumer<ArrayData, Object>) | ||
| (ad, d) -> | ||
| setupArrayMock( | ||
| ad, | ||
| (InternalRow[]) d, // Cast to InternalRow array | ||
| (i, val) -> { | ||
| when(ad.getStruct(i, 2)).thenReturn((InternalRow) val); | ||
|
stevelordbq marked this conversation as resolved.
Outdated
|
||
| when(((InternalRow) val).getUTF8String(0)) | ||
| .thenReturn(UTF8String.fromString("str_value")); | ||
| when(((InternalRow) val).getBoolean(1)).thenReturn(true); | ||
| }) | ||
| } | ||
| }); | ||
| } | ||
|
|
@@ -368,7 +464,7 @@ public void testArrayConversion() { | |
| when(row.isNullAt(0)).thenReturn(false); | ||
| when(row.getArray(0)).thenReturn(arrayData); | ||
|
|
||
| // Executes the specific stubbing (e.g., ad.toLongArray() or ad.toIntArray()) | ||
| // Executes the specific stubbing (e.g., ad.toLongArray(), ad.toIntArray(), ad.to) | ||
| mockSetup.accept(arrayData, inputData); | ||
|
|
||
| Mutation mutation = SpannerWriterUtils.internalRowToMutation(TABLE_NAME, row, schema); | ||
|
|
@@ -377,4 +473,9 @@ public void testArrayConversion() { | |
| "Failure on type: " + colName, expectedValue, mutation.asMap().get(colName)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testStructConversion() { | ||
| // TODO: add test | ||
| } | ||
|
stevelordbq marked this conversation as resolved.
Outdated
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.