forked from GoogleCloudDataproc/spark-spanner-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Write acceptance tests #25
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
5
commits into
main
Choose a base branch
from
acceptance-write
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a8ff65
Write acceptance test initial version
stevelordbq c23e006
Write acceptance test
stevelordbq fe88324
Write acceptance test with column compare
stevelordbq 63915e4
Write acceptance test PR updates
stevelordbq dd2693a
Write acceptance test add serverless tests
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
120 changes: 120 additions & 0 deletions
120
spark-3.1-spanner-lib/src/test/resources/acceptance/write_test_table.py
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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2023 Google Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import sys | ||
| from datetime import datetime, date | ||
| from decimal import Decimal | ||
| from pyspark.sql import SparkSession, Row | ||
| from pyspark.sql.types import StructType, StructField, StringType, LongType, BinaryType, TimestampType, DecimalType, BooleanType, DoubleType, DateType | ||
|
|
||
| def main(): | ||
|
|
||
| # Initialize Spark Session | ||
| spark = SparkSession.builder.appName('Write Acceptance Test on Spark').getOrCreate() | ||
|
|
||
| # 1. Define the Schema (Column Name, Type, Nullable) | ||
| schema = StructType([ | ||
| StructField("A", LongType(), False), | ||
| StructField("B", StringType(), True), | ||
| StructField("C", BinaryType(), True), | ||
| StructField("D", TimestampType(), True), | ||
| StructField("E", DecimalType(38, 9), True), | ||
| StructField("F", BooleanType(), True), | ||
| StructField("G", DoubleType(), True), | ||
| StructField("H", DateType(), True) | ||
| ]) | ||
|
|
||
| # 2. Prepare Data as a list of tuples | ||
| data = [ | ||
| (1, "2", None, datetime.fromisoformat("2023-08-22T12:22:00"), Decimal("1000.282111401"), True, 123.456, date(2023, 12, 25)), | ||
| (10, "20", None, datetime.fromisoformat("2023-08-22T12:23:00"), Decimal("10000.282111603"), False, 987.654, date(2023, 12, 24)), | ||
| (30, "30", None, datetime.fromisoformat("2023-08-22T12:24:00"), Decimal("30000.282111805"), True, -2121.1212, date(2023, 12, 23)) | ||
| ] | ||
|
|
||
| # 3. Create the DataFrame | ||
| dfw = spark.createDataFrame(data, schema) | ||
| dfw.show() | ||
|
|
||
| table = 'AWriteTable' | ||
|
|
||
| # Configure Spanner properties | ||
| spanner_base_options = { | ||
| "instanceId": sys.argv[3], | ||
| "databaseId": sys.argv[4], | ||
| "projectId": sys.argv[2], | ||
| "table": table | ||
| } | ||
|
|
||
| spanner_write_options = { | ||
| **spanner_base_options, | ||
| "mutationType": "insert_or_update", # Use this to avoid ALREADY_EXISTS errors | ||
| "enablePartialRowUpdates": "true" # Required since not all columns are being populated | ||
| } | ||
|
|
||
| spanner_read_options = { | ||
| **spanner_base_options | ||
| } | ||
|
|
||
| dfw.write.format('cloud-spanner') \ | ||
| .options(**spanner_write_options) \ | ||
| .mode("append") \ | ||
| .save() | ||
|
|
||
| # Read the table to verify the write operation | ||
| df = spark.read.format('cloud-spanner') \ | ||
| .options(**spanner_read_options) \ | ||
| .load(table) | ||
|
|
||
| print('The resulting schema is') | ||
| df.printSchema() | ||
| df.show() | ||
|
|
||
| df_result = verify_data_to_df(dfw, df, spark) | ||
| df_result.show() | ||
|
|
||
| # coalesce 1 to ensure results are written in single partition and avoid empty file creation. | ||
| df_result.coalesce(1).write.csv(sys.argv[1]) | ||
|
|
||
|
|
||
| def verify_data_to_df(df_expected, df_actual, spark): | ||
| issues = [] | ||
|
|
||
| # 1. Validation Logic | ||
| if df_expected.schema != df_actual.schema: | ||
| issues.append("Schema mismatch") | ||
|
|
||
| # Cache DFs for performance since we'll perform multiple actions. | ||
| df_expected.cache() | ||
| df_actual.cache() | ||
|
|
||
| missing_rows_count = df_expected.subtract(df_actual).count() | ||
| if missing_rows_count > 0: | ||
| issues.append(f"Missing rows in actual: {missing_rows_count}") | ||
|
|
||
| extra_rows_count = df_actual.subtract(df_expected).count() | ||
| if extra_rows_count > 0: | ||
| issues.append(f"Extra rows in actual: {extra_rows_count}") | ||
|
|
||
| df_expected.unpersist() | ||
| df_actual.unpersist() | ||
|
|
||
| # 2. Determine Final Status | ||
| status_msg = "PASS" if not issues else "FAIL: " + " | ".join(issues) | ||
|
|
||
| # 3. Create a DataFrame from the result string | ||
| return spark.createDataFrame([Row(summary=status_msg)]) | ||
|
stevelordbq marked this conversation as resolved.
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one-time initialization logic has a bug: the
initializedflag is never set totrue. This causes the expensive setup to run before every test, defeating the purpose of the refactoring. Additionally, the check is not thread-safe, which could lead to race conditions if tests are run in parallel. I suggest wrapping the initialization logic in asynchronizedblock and setting the flag upon successful completion.