snowpark-checkpoints-validators 0.1.0rc2__py3-none-any.whl → 0.1.0rc3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,313 @@
1
+ Metadata-Version: 2.4
2
+ Name: snowpark-checkpoints-validators
3
+ Version: 0.1.0rc3
4
+ Summary: Migration tools for Snowpark
5
+ Project-URL: Bug Tracker, https://github.com/snowflakedb/snowpark-checkpoints/issues
6
+ Project-URL: Source code, https://github.com/snowflakedb/snowpark-checkpoints/
7
+ Author-email: "Snowflake, Inc." <snowflake-python-libraries-dl@snowflake.com>
8
+ License: Apache License, Version 2.0
9
+ License-File: LICENSE
10
+ Keywords: Snowflake,Snowpark,analytics,cloud,database,db
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Console
13
+ Classifier: Environment :: Other Environment
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Education
16
+ Classifier: Intended Audience :: Information Technology
17
+ Classifier: Intended Audience :: System Administrators
18
+ Classifier: License :: OSI Approved :: Apache Software License
19
+ Classifier: Operating System :: OS Independent
20
+ Classifier: Programming Language :: Python :: 3 :: Only
21
+ Classifier: Programming Language :: SQL
22
+ Classifier: Topic :: Database
23
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
24
+ Classifier: Topic :: Software Development
25
+ Classifier: Topic :: Software Development :: Libraries
26
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
27
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
+ Requires-Python: <3.12,>=3.9
29
+ Requires-Dist: pandera-report==0.1.2
30
+ Requires-Dist: pandera[io]==0.20.4
31
+ Requires-Dist: pyspark
32
+ Requires-Dist: snowflake-connector-python
33
+ Requires-Dist: snowflake-snowpark-python
34
+ Provides-Extra: development
35
+ Requires-Dist: coverage>=7.6.7; extra == 'development'
36
+ Requires-Dist: deepdiff>=8.0.0; extra == 'development'
37
+ Requires-Dist: hatchling==1.25.0; extra == 'development'
38
+ Requires-Dist: pre-commit>=4.0.1; extra == 'development'
39
+ Requires-Dist: pyarrow>=18.0.0; extra == 'development'
40
+ Requires-Dist: pytest-cov>=6.0.0; extra == 'development'
41
+ Requires-Dist: pytest>=8.3.3; extra == 'development'
42
+ Requires-Dist: setuptools>=70.0.0; extra == 'development'
43
+ Requires-Dist: twine==5.1.1; extra == 'development'
44
+ Description-Content-Type: text/markdown
45
+
46
+ # snowpark-checkpoints-validators
47
+
48
+ ---
49
+ **NOTE**
50
+
51
+ This package is on Private Preview.
52
+
53
+ ---
54
+
55
+ **snowpark-checkpoints-validators** is a package designed to validate Snowpark DataFrames against predefined schemas and checkpoints. This package ensures data integrity and consistency by performing schema and data validation checks at various stages of a Snowpark pipeline.
56
+
57
+ ## Features
58
+
59
+ - Validate Snowpark DataFrames against predefined Pandera schemas.
60
+ - Perform custom checks and skip specific checks as needed.
61
+ - Generate validation results and log them for further analysis.
62
+ - Support for sampling strategies to validate large datasets efficiently.
63
+ - Integration with PySpark for cross-validation between Snowpark and PySpark DataFrames.
64
+
65
+ ## Functionalities
66
+
67
+ ### Validate DataFrame Schema from File
68
+
69
+ The `validate_dataframe_checkpoint` function validates a Snowpark DataFrame against a checkpoint schema file or dataframe.
70
+
71
+ ```python
72
+ from snowflake.snowpark import DataFrame as SnowparkDataFrame
73
+ from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
74
+ from snowflake.snowpark_checkpoints.utils.constant import (
75
+ CheckpointMode,
76
+ )
77
+ from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
78
+ from typing import Any, Optional
79
+
80
+ # Signature of the function
81
+ def validate_dataframe_checkpoint(
82
+ df: SnowparkDataFrame,
83
+ checkpoint_name: str,
84
+ job_context: Optional[SnowparkJobContext] = None,
85
+ mode: Optional[CheckpointMode] = CheckpointMode.SCHEMA,
86
+ custom_checks: Optional[dict[Any, Any]] = None,
87
+ skip_checks: Optional[dict[Any, Any]] = None,
88
+ sample_frac: Optional[float] = 1.0,
89
+ sample_number: Optional[int] = None,
90
+ sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
91
+ output_path: Optional[str] = None,
92
+ ):
93
+ ...
94
+ ```
95
+
96
+ - `df`: Snowpark dataframe to validate.
97
+ - `checkpoint_name`: Name of the checkpoint schema file or dataframe.
98
+ - `job_context`: Snowpark job context.
99
+ - `mode`: Checkpoint mode (schema or data).
100
+ - `custom_checks`: Custom checks to perform.
101
+ - `skip_checks`: Checks to skip.
102
+ - `sample_frac`: Fraction of the dataframe to sample.
103
+ - `sample_number`: Number of rows to sample.
104
+ - `sampling_strategy`: Sampling strategy to use.
105
+ - `output_path`: Output path for the checkpoint report.
106
+
107
+ ### Usage Example
108
+
109
+ ```python
110
+ from snowflake.snowpark import Session
111
+ from snowflake.snowpark_checkpoints.utils.constant import (
112
+ CheckpointMode,
113
+ )
114
+ from snowflake.snowpark_checkpoints.checkpoint import validate_dataframe_checkpoint
115
+ from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
116
+ from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
117
+ from pyspark.sql import SparkSession
118
+
119
+ session = Session.builder.getOrCreate()
120
+ job_context = SnowparkJobContext(
121
+ session, SparkSession.builder.getOrCreate(), "job_context", True
122
+ )
123
+ df = session.read.format("csv").load("data.csv")
124
+
125
+ validate_dataframe_checkpoint(
126
+ df,
127
+ "schema_checkpoint",
128
+ job_context=job_context,
129
+ mode=CheckpointMode.SCHEMA,
130
+ sample_frac=0.1,
131
+ sampling_strategy=SamplingStrategy.RANDOM_SAMPLE
132
+ )
133
+ ```
134
+
135
+ ### Check with Spark Decorator
136
+
137
+ The `check_with_spark` decorator converts any Snowpark dataframe arguments to a function, samples them, and converts them to PySpark dataframe. It then executes a provided Spark function and compares the outputs between the two implementations.
138
+
139
+ ```python
140
+ from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
141
+ from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
142
+ from typing import Callable, Optional, TypeVar
143
+
144
+ fn = TypeVar("F", bound=Callable)
145
+
146
+ # Signature of the decorator
147
+ def check_with_spark(
148
+ job_context: Optional[SnowparkJobContext],
149
+ spark_function: fn,
150
+ checkpoint_name: str,
151
+ sample_number: Optional[int] = 100,
152
+ sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
153
+ output_path: Optional[str] = None,
154
+ ) -> Callable[[fn], fn]:
155
+ ...
156
+ ```
157
+
158
+ - `job_context`: Snowpark job context.
159
+ - `spark_function`: PySpark function to execute.
160
+ - `checkpoint_name`: Name of the check.
161
+ - `sample_number`: Number of rows to sample.
162
+ - `sampling_strategy`: Sampling strategy to use.
163
+ - `output_path`: Output path for the checkpoint report.
164
+
165
+ ### Usage Example
166
+
167
+ ```python
168
+ from snowflake.snowpark import Session
169
+ from snowflake.snowpark import DataFrame as SnowparkDataFrame
170
+ from snowflake.snowpark_checkpoints.spark_migration import check_with_spark
171
+ from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
172
+ from pyspark.sql import DataFrame as SparkDataFrame, SparkSession
173
+
174
+ session = Session.builder.getOrCreate()
175
+ job_context = SnowparkJobContext(
176
+ session, SparkSession.builder.getOrCreate(), "job_context", True
177
+ )
178
+
179
+ def my_spark_scalar_fn(df: SparkDataFrame):
180
+ return df.count()
181
+
182
+ @check_with_spark(
183
+ job_context=job_context,
184
+ spark_function=my_spark_scalar_fn,
185
+ checkpoint_name="count_checkpoint",
186
+ )
187
+ def my_snowpark_scalar_fn(df: SnowparkDataFrame):
188
+ return df.count()
189
+
190
+ df = job_context.snowpark_session.create_dataframe(
191
+ [[1, 2], [3, 4]], schema=["a", "b"]
192
+ )
193
+ count = my_snowpark_scalar_fn(df)
194
+ ```
195
+
196
+ ### Pandera Snowpark Decorators
197
+
198
+ The decorators `@check_input_schema` and `@check_output_schema` allow for sampled schema validation of Snowpark dataframes in the input arguments or in the return value.
199
+
200
+ ```python
201
+ from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
202
+ from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
203
+ from pandera import DataFrameSchema
204
+ from typing import Optional
205
+
206
+ # Signature of the decorator
207
+ def check_input_schema(
208
+ pandera_schema: DataFrameSchema,
209
+ checkpoint_name: str,
210
+ sample_frac: Optional[float] = 1.0,
211
+ sample_number: Optional[int] = None,
212
+ sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
213
+ job_context: Optional[SnowparkJobContext] = None,
214
+ output_path: Optional[str] = None,
215
+ ):
216
+ ...
217
+
218
+ # Signature of the decorator
219
+ def check_output_schema(
220
+ pandera_schema: DataFrameSchema,
221
+ checkpoint_name: str,
222
+ sample_frac: Optional[float] = 1.0,
223
+ sample_number: Optional[int] = None,
224
+ sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
225
+ job_context: Optional[SnowparkJobContext] = None,
226
+ output_path: Optional[str] = None,
227
+ ):
228
+ ...
229
+ ```
230
+
231
+ - `pandera_schema`: Pandera schema to validate.
232
+ - `checkpoint_name`: Name of the checkpoint schema file or DataFrame.
233
+ - `sample_frac`: Fraction of the DataFrame to sample.
234
+ - `sample_number`: Number of rows to sample.
235
+ - `sampling_strategy`: Sampling strategy to use.
236
+ - `job_context`: Snowpark job context.
237
+ - `output_path`: Output path for the checkpoint report.
238
+
239
+ ### Usage Example
240
+
241
+ #### Check Input Schema Example
242
+ ```python
243
+ from pandas import DataFrame as PandasDataFrame
244
+ from pandera import DataFrameSchema, Column, Check
245
+ from snowflake.snowpark import Session
246
+ from snowflake.snowpark import DataFrame as SnowparkDataFrame
247
+ from snowflake.snowpark_checkpoints.checkpoint import check_input_schema
248
+ from numpy import int8
249
+
250
+ df = PandasDataFrame(
251
+ {
252
+ "COLUMN1": [1, 4, 0, 10, 9],
253
+ "COLUMN2": [-1.3, -1.4, -2.9, -10.1, -20.4],
254
+ }
255
+ )
256
+
257
+ in_schema = DataFrameSchema(
258
+ {
259
+ "COLUMN1": Column(int8, Check(lambda x: 0 <= x <= 10, element_wise=True)),
260
+ "COLUMN2": Column(float, Check(lambda x: x < -1.2, element_wise=True)),
261
+ }
262
+ )
263
+
264
+ @check_input_schema(in_schema, "input_schema_checkpoint")
265
+ def preprocessor(dataframe: SnowparkDataFrame):
266
+ dataframe = dataframe.withColumn(
267
+ "COLUMN3", dataframe["COLUMN1"] + dataframe["COLUMN2"]
268
+ )
269
+ return dataframe
270
+
271
+ session = Session.builder.getOrCreate()
272
+ sp_dataframe = session.create_dataframe(df)
273
+
274
+ preprocessed_dataframe = preprocessor(sp_dataframe)
275
+ ```
276
+
277
+ #### Check Input Schema Example
278
+ ```python
279
+ from pandas import DataFrame as PandasDataFrame
280
+ from pandera import DataFrameSchema, Column, Check
281
+ from snowflake.snowpark import Session
282
+ from snowflake.snowpark import DataFrame as SnowparkDataFrame
283
+ from snowflake.snowpark_checkpoints.checkpoint import check_output_schema
284
+ from numpy import int8
285
+
286
+ df = PandasDataFrame(
287
+ {
288
+ "COLUMN1": [1, 4, 0, 10, 9],
289
+ "COLUMN2": [-1.3, -1.4, -2.9, -10.1, -20.4],
290
+ }
291
+ )
292
+
293
+ out_schema = DataFrameSchema(
294
+ {
295
+ "COLUMN1": Column(int8, Check.between(0, 10, include_max=True, include_min=True)),
296
+ "COLUMN2": Column(float, Check.less_than_or_equal_to(-1.2)),
297
+ "COLUMN3": Column(float, Check.less_than(10)),
298
+ }
299
+ )
300
+
301
+ @check_output_schema(out_schema, "output_schema_checkpoint")
302
+ def preprocessor(dataframe: SnowparkDataFrame):
303
+ return dataframe.with_column(
304
+ "COLUMN3", dataframe["COLUMN1"] + dataframe["COLUMN2"]
305
+ )
306
+
307
+ session = Session.builder.getOrCreate()
308
+ sp_dataframe = session.create_dataframe(df)
309
+
310
+ preprocessed_dataframe = preprocessor(sp_dataframe)
311
+ ```
312
+
313
+ ------
@@ -0,0 +1,4 @@
1
+ snowpark_checkpoints_validators-0.1.0rc3.dist-info/METADATA,sha256=tjA56AFxmFIDPB3jx0ySyf2sHNAdEFYUu9RuqwySQ4M,11002
2
+ snowpark_checkpoints_validators-0.1.0rc3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
3
+ snowpark_checkpoints_validators-0.1.0rc3.dist-info/licenses/LICENSE,sha256=pmjhbh6uVhV5MBXOlou_UZgFP7CYVQITkCCdvfcS5lY,11340
4
+ snowpark_checkpoints_validators-0.1.0rc3.dist-info/RECORD,,
@@ -1,514 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: snowpark-checkpoints-validators
3
- Version: 0.1.0rc2
4
- Summary: Migration tools for Snowpark
5
- Project-URL: Bug Tracker, https://github.com/snowflakedb/snowpark-checkpoints/issues
6
- Project-URL: Source code, https://github.com/snowflakedb/snowpark-checkpoints/
7
- Author: Snowflake Inc.
8
- License:
9
- Apache License
10
- Version 2.0, January 2004
11
- http://www.apache.org/licenses/
12
-
13
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
14
-
15
- 1. Definitions.
16
-
17
- "License" shall mean the terms and conditions for use, reproduction,
18
- and distribution as defined by Sections 1 through 9 of this document.
19
-
20
- "Licensor" shall mean the copyright owner or entity authorized by
21
- the copyright owner that is granting the License.
22
-
23
- "Legal Entity" shall mean the union of the acting entity and all
24
- other entities that control, are controlled by, or are under common
25
- control with that entity. For the purposes of this definition,
26
- "control" means (i) the power, direct or indirect, to cause the
27
- direction or management of such entity, whether by contract or
28
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
29
- outstanding shares, or (iii) beneficial ownership of such entity.
30
-
31
- "You" (or "Your") shall mean an individual or Legal Entity
32
- exercising permissions granted by this License.
33
-
34
- "Source" form shall mean the preferred form for making modifications,
35
- including but not limited to software source code, documentation
36
- source, and configuration files.
37
-
38
- "Object" form shall mean any form resulting from mechanical
39
- transformation or translation of a Source form, including but
40
- not limited to compiled object code, generated documentation,
41
- and conversions to other media types.
42
-
43
- "Work" shall mean the work of authorship, whether in Source or
44
- Object form, made available under the License, as indicated by a
45
- copyright notice that is included in or attached to the work
46
- (an example is provided in the Appendix below).
47
-
48
- "Derivative Works" shall mean any work, whether in Source or Object
49
- form, that is based on (or derived from) the Work and for which the
50
- editorial revisions, annotations, elaborations, or other modifications
51
- represent, as a whole, an original work of authorship. For the purposes
52
- of this License, Derivative Works shall not include works that remain
53
- separable from, or merely link (or bind by name) to the interfaces of,
54
- the Work and Derivative Works thereof.
55
-
56
- "Contribution" shall mean any work of authorship, including
57
- the original version of the Work and any modifications or additions
58
- to that Work or Derivative Works thereof, that is intentionally
59
- submitted to Licensor for inclusion in the Work by the copyright owner
60
- or by an individual or Legal Entity authorized to submit on behalf of
61
- the copyright owner. For the purposes of this definition, "submitted"
62
- means any form of electronic, verbal, or written communication sent
63
- to the Licensor or its representatives, including but not limited to
64
- communication on electronic mailing lists, source code control systems,
65
- and issue tracking systems that are managed by, or on behalf of, the
66
- Licensor for the purpose of discussing and improving the Work, but
67
- excluding communication that is conspicuously marked or otherwise
68
- designated in writing by the copyright owner as "Not a Contribution."
69
-
70
- "Contributor" shall mean Licensor and any individual or Legal Entity
71
- on behalf of whom a Contribution has been received by Licensor and
72
- subsequently incorporated within the Work.
73
-
74
- 2. Grant of Copyright License. Subject to the terms and conditions of
75
- this License, each Contributor hereby grants to You a perpetual,
76
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
- copyright license to reproduce, prepare Derivative Works of,
78
- publicly display, publicly perform, sublicense, and distribute the
79
- Work and such Derivative Works in Source or Object form.
80
-
81
- 3. Grant of Patent License. Subject to the terms and conditions of
82
- this License, each Contributor hereby grants to You a perpetual,
83
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
84
- (except as stated in this section) patent license to make, have made,
85
- use, offer to sell, sell, import, and otherwise transfer the Work,
86
- where such license applies only to those patent claims licensable
87
- by such Contributor that are necessarily infringed by their
88
- Contribution(s) alone or by combination of their Contribution(s)
89
- with the Work to which such Contribution(s) was submitted. If You
90
- institute patent litigation against any entity (including a
91
- cross-claim or counterclaim in a lawsuit) alleging that the Work
92
- or a Contribution incorporated within the Work constitutes direct
93
- or contributory patent infringement, then any patent licenses
94
- granted to You under this License for that Work shall terminate
95
- as of the date such litigation is filed.
96
-
97
- 4. Redistribution. You may reproduce and distribute copies of the
98
- Work or Derivative Works thereof in any medium, with or without
99
- modifications, and in Source or Object form, provided that You
100
- meet the following conditions:
101
-
102
- (a) You must give any other recipients of the Work or
103
- Derivative Works a copy of this License; and
104
-
105
- (b) You must cause any modified files to carry prominent notices
106
- stating that You changed the files; and
107
-
108
- (c) You must retain, in the Source form of any Derivative Works
109
- that You distribute, all copyright, patent, trademark, and
110
- attribution notices from the Source form of the Work,
111
- excluding those notices that do not pertain to any part of
112
- the Derivative Works; and
113
-
114
- (d) If the Work includes a "NOTICE" text file as part of its
115
- distribution, then any Derivative Works that You distribute must
116
- include a readable copy of the attribution notices contained
117
- within such NOTICE file, excluding those notices that do not
118
- pertain to any part of the Derivative Works, in at least one
119
- of the following places: within a NOTICE text file distributed
120
- as part of the Derivative Works; within the Source form or
121
- documentation, if provided along with the Derivative Works; or,
122
- within a display generated by the Derivative Works, if and
123
- wherever such third-party notices normally appear. The contents
124
- of the NOTICE file are for informational purposes only and
125
- do not modify the License. You may add Your own attribution
126
- notices within Derivative Works that You distribute, alongside
127
- or as an addendum to the NOTICE text from the Work, provided
128
- that such additional attribution notices cannot be construed
129
- as modifying the License.
130
-
131
- You may add Your own copyright statement to Your modifications and
132
- may provide additional or different license terms and conditions
133
- for use, reproduction, or distribution of Your modifications, or
134
- for any such Derivative Works as a whole, provided Your use,
135
- reproduction, and distribution of the Work otherwise complies with
136
- the conditions stated in this License.
137
-
138
- 5. Submission of Contributions. Unless You explicitly state otherwise,
139
- any Contribution intentionally submitted for inclusion in the Work
140
- by You to the Licensor shall be under the terms and conditions of
141
- this License, without any additional terms or conditions.
142
- Notwithstanding the above, nothing herein shall supersede or modify
143
- the terms of any separate license agreement you may have executed
144
- with Licensor regarding such Contributions.
145
-
146
- 6. Trademarks. This License does not grant permission to use the trade
147
- names, trademarks, service marks, or product names of the Licensor,
148
- except as required for reasonable and customary use in describing the
149
- origin of the Work and reproducing the content of the NOTICE file.
150
-
151
- 7. Disclaimer of Warranty. Unless required by applicable law or
152
- agreed to in writing, Licensor provides the Work (and each
153
- Contributor provides its Contributions) on an "AS IS" BASIS,
154
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
155
- implied, including, without limitation, any warranties or conditions
156
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
157
- PARTICULAR PURPOSE. You are solely responsible for determining the
158
- appropriateness of using or redistributing the Work and assume any
159
- risks associated with Your exercise of permissions under this License.
160
-
161
- 8. Limitation of Liability. In no event and under no legal theory,
162
- whether in tort (including negligence), contract, or otherwise,
163
- unless required by applicable law (such as deliberate and grossly
164
- negligent acts) or agreed to in writing, shall any Contributor be
165
- liable to You for damages, including any direct, indirect, special,
166
- incidental, or consequential damages of any character arising as a
167
- result of this License or out of the use or inability to use the
168
- Work (including but not limited to damages for loss of goodwill,
169
- work stoppage, computer failure or malfunction, or any and all
170
- other commercial damages or losses), even if such Contributor
171
- has been advised of the possibility of such damages.
172
-
173
- 9. Accepting Warranty or Additional Liability. While redistributing
174
- the Work or Derivative Works thereof, You may choose to offer,
175
- and charge a fee for, acceptance of support, warranty, indemnity,
176
- or other liability obligations and/or rights consistent with this
177
- License. However, in accepting such obligations, You may act only
178
- on Your own behalf and on Your sole responsibility, not on behalf
179
- of any other Contributor, and only if You agree to indemnify,
180
- defend, and hold each Contributor harmless for any liability
181
- incurred by, or claims asserted against, such Contributor by reason
182
- of your accepting any such warranty or additional liability.
183
-
184
- END OF TERMS AND CONDITIONS
185
-
186
- APPENDIX: How to apply the Apache License to your work.
187
-
188
- To apply the Apache License to your work, attach the following
189
- boilerplate notice, with the fields enclosed by brackets "[]"
190
- replaced with your own identifying information. (Don't include
191
- the brackets!) The text should be enclosed in the appropriate
192
- comment syntax for the file format. We also recommend that a
193
- file or class name and description of purpose be included on the
194
- same "printed page" as the copyright notice for easier
195
- identification within third-party archives.
196
-
197
- Copyright 2025 Snowflake
198
-
199
- Licensed under the Apache License, Version 2.0 (the "License");
200
- you may not use this file except in compliance with the License.
201
- You may obtain a copy of the License at
202
-
203
- http://www.apache.org/licenses/LICENSE-2.0
204
-
205
- Unless required by applicable law or agreed to in writing, software
206
- distributed under the License is distributed on an "AS IS" BASIS,
207
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208
- See the License for the specific language governing permissions and
209
- limitations under the License.
210
- License-File: LICENSE
211
- Keywords: Snowflake,Snowpark,analytics,cloud,database,db
212
- Classifier: Development Status :: 4 - Beta
213
- Classifier: Environment :: Console
214
- Classifier: Environment :: Other Environment
215
- Classifier: Intended Audience :: Developers
216
- Classifier: Intended Audience :: Education
217
- Classifier: Intended Audience :: Information Technology
218
- Classifier: Intended Audience :: System Administrators
219
- Classifier: License :: OSI Approved :: Apache Software License
220
- Classifier: Operating System :: OS Independent
221
- Classifier: Programming Language :: Python :: 3 :: Only
222
- Classifier: Programming Language :: SQL
223
- Classifier: Topic :: Database
224
- Classifier: Topic :: Scientific/Engineering :: Information Analysis
225
- Classifier: Topic :: Software Development
226
- Classifier: Topic :: Software Development :: Libraries
227
- Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
228
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
229
- Requires-Python: <3.12,>=3.9
230
- Requires-Dist: pandera-report==0.1.2
231
- Requires-Dist: pandera[io]==0.20.4
232
- Requires-Dist: pyspark
233
- Requires-Dist: snowflake-connector-python
234
- Requires-Dist: snowflake-snowpark-python
235
- Provides-Extra: development
236
- Requires-Dist: coverage>=7.6.7; extra == 'development'
237
- Requires-Dist: deepdiff>=8.0.0; extra == 'development'
238
- Requires-Dist: hatchling==1.25.0; extra == 'development'
239
- Requires-Dist: pre-commit>=4.0.1; extra == 'development'
240
- Requires-Dist: pyarrow>=18.0.0; extra == 'development'
241
- Requires-Dist: pytest-cov>=6.0.0; extra == 'development'
242
- Requires-Dist: pytest>=8.3.3; extra == 'development'
243
- Requires-Dist: setuptools>=70.0.0; extra == 'development'
244
- Requires-Dist: twine==5.1.1; extra == 'development'
245
- Description-Content-Type: text/markdown
246
-
247
- # snowpark-checkpoints-validators
248
-
249
- ---
250
- **NOTE**
251
-
252
- This package is on Private Preview.
253
-
254
- ---
255
-
256
- **snowpark-checkpoints-validators** is a package designed to validate Snowpark DataFrames against predefined schemas and checkpoints. This package ensures data integrity and consistency by performing schema and data validation checks at various stages of a Snowpark pipeline.
257
-
258
- ## Features
259
-
260
- - Validate Snowpark DataFrames against predefined Pandera schemas.
261
- - Perform custom checks and skip specific checks as needed.
262
- - Generate validation results and log them for further analysis.
263
- - Support for sampling strategies to validate large datasets efficiently.
264
- - Integration with PySpark for cross-validation between Snowpark and PySpark DataFrames.
265
-
266
- ## Functionalities
267
-
268
- ### Validate DataFrame Schema from File
269
-
270
- The `validate_dataframe_checkpoint` function validates a Snowpark DataFrame against a checkpoint schema file or dataframe.
271
-
272
- ```python
273
- from snowflake.snowpark import DataFrame as SnowparkDataFrame
274
- from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
275
- from snowflake.snowpark_checkpoints.utils.constant import (
276
- CheckpointMode,
277
- )
278
- from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
279
- from typing import Any, Optional
280
-
281
- # Signature of the function
282
- def validate_dataframe_checkpoint(
283
- df: SnowparkDataFrame,
284
- checkpoint_name: str,
285
- job_context: Optional[SnowparkJobContext] = None,
286
- mode: Optional[CheckpointMode] = CheckpointMode.SCHEMA,
287
- custom_checks: Optional[dict[Any, Any]] = None,
288
- skip_checks: Optional[dict[Any, Any]] = None,
289
- sample_frac: Optional[float] = 1.0,
290
- sample_number: Optional[int] = None,
291
- sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
292
- output_path: Optional[str] = None,
293
- ):
294
- ...
295
- ```
296
-
297
- - `df`: Snowpark dataframe to validate.
298
- - `checkpoint_name`: Name of the checkpoint schema file or dataframe.
299
- - `job_context`: Snowpark job context.
300
- - `mode`: Checkpoint mode (schema or data).
301
- - `custom_checks`: Custom checks to perform.
302
- - `skip_checks`: Checks to skip.
303
- - `sample_frac`: Fraction of the dataframe to sample.
304
- - `sample_number`: Number of rows to sample.
305
- - `sampling_strategy`: Sampling strategy to use.
306
- - `output_path`: Output path for the checkpoint report.
307
-
308
- ### Usage Example
309
-
310
- ```python
311
- from snowflake.snowpark import Session
312
- from snowflake.snowpark_checkpoints.utils.constant import (
313
- CheckpointMode,
314
- )
315
- from snowflake.snowpark_checkpoints.checkpoint import validate_dataframe_checkpoint
316
- from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
317
- from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
318
- from pyspark.sql import SparkSession
319
-
320
- session = Session.builder.getOrCreate()
321
- job_context = SnowparkJobContext(
322
- session, SparkSession.builder.getOrCreate(), "job_context", True
323
- )
324
- df = session.read.format("csv").load("data.csv")
325
-
326
- validate_dataframe_checkpoint(
327
- df,
328
- "schema_checkpoint",
329
- job_context=job_context,
330
- mode=CheckpointMode.SCHEMA,
331
- sample_frac=0.1,
332
- sampling_strategy=SamplingStrategy.RANDOM_SAMPLE
333
- )
334
- ```
335
-
336
- ### Check with Spark Decorator
337
-
338
- The `check_with_spark` decorator converts any Snowpark dataframe arguments to a function, samples them, and converts them to PySpark dataframe. It then executes a provided Spark function and compares the outputs between the two implementations.
339
-
340
- ```python
341
- from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
342
- from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
343
- from typing import Callable, Optional, TypeVar
344
-
345
- fn = TypeVar("F", bound=Callable)
346
-
347
- # Signature of the decorator
348
- def check_with_spark(
349
- job_context: Optional[SnowparkJobContext],
350
- spark_function: fn,
351
- checkpoint_name: str,
352
- sample_number: Optional[int] = 100,
353
- sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
354
- output_path: Optional[str] = None,
355
- ) -> Callable[[fn], fn]:
356
- ...
357
- ```
358
-
359
- - `job_context`: Snowpark job context.
360
- - `spark_function`: PySpark function to execute.
361
- - `checkpoint_name`: Name of the check.
362
- - `sample_number`: Number of rows to sample.
363
- - `sampling_strategy`: Sampling strategy to use.
364
- - `output_path`: Output path for the checkpoint report.
365
-
366
- ### Usage Example
367
-
368
- ```python
369
- from snowflake.snowpark import Session
370
- from snowflake.snowpark import DataFrame as SnowparkDataFrame
371
- from snowflake.snowpark_checkpoints.spark_migration import check_with_spark
372
- from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
373
- from pyspark.sql import DataFrame as SparkDataFrame, SparkSession
374
-
375
- session = Session.builder.getOrCreate()
376
- job_context = SnowparkJobContext(
377
- session, SparkSession.builder.getOrCreate(), "job_context", True
378
- )
379
-
380
- def my_spark_scalar_fn(df: SparkDataFrame):
381
- return df.count()
382
-
383
- @check_with_spark(
384
- job_context=job_context,
385
- spark_function=my_spark_scalar_fn,
386
- checkpoint_name="count_checkpoint",
387
- )
388
- def my_snowpark_scalar_fn(df: SnowparkDataFrame):
389
- return df.count()
390
-
391
- df = job_context.snowpark_session.create_dataframe(
392
- [[1, 2], [3, 4]], schema=["a", "b"]
393
- )
394
- count = my_snowpark_scalar_fn(df)
395
- ```
396
-
397
- ### Pandera Snowpark Decorators
398
-
399
- The decorators `@check_input_schema` and `@check_output_schema` allow for sampled schema validation of Snowpark dataframes in the input arguments or in the return value.
400
-
401
- ```python
402
- from snowflake.snowpark_checkpoints.spark_migration import SamplingStrategy
403
- from snowflake.snowpark_checkpoints.job_context import SnowparkJobContext
404
- from pandera import DataFrameSchema
405
- from typing import Optional
406
-
407
- # Signature of the decorator
408
- def check_input_schema(
409
- pandera_schema: DataFrameSchema,
410
- checkpoint_name: str,
411
- sample_frac: Optional[float] = 1.0,
412
- sample_number: Optional[int] = None,
413
- sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
414
- job_context: Optional[SnowparkJobContext] = None,
415
- output_path: Optional[str] = None,
416
- ):
417
- ...
418
-
419
- # Signature of the decorator
420
- def check_output_schema(
421
- pandera_schema: DataFrameSchema,
422
- checkpoint_name: str,
423
- sample_frac: Optional[float] = 1.0,
424
- sample_number: Optional[int] = None,
425
- sampling_strategy: Optional[SamplingStrategy] = SamplingStrategy.RANDOM_SAMPLE,
426
- job_context: Optional[SnowparkJobContext] = None,
427
- output_path: Optional[str] = None,
428
- ):
429
- ...
430
- ```
431
-
432
- - `pandera_schema`: Pandera schema to validate.
433
- - `checkpoint_name`: Name of the checkpoint schema file or DataFrame.
434
- - `sample_frac`: Fraction of the DataFrame to sample.
435
- - `sample_number`: Number of rows to sample.
436
- - `sampling_strategy`: Sampling strategy to use.
437
- - `job_context`: Snowpark job context.
438
- - `output_path`: Output path for the checkpoint report.
439
-
440
- ### Usage Example
441
-
442
- #### Check Input Schema Example
443
- ```python
444
- from pandas import DataFrame as PandasDataFrame
445
- from pandera import DataFrameSchema, Column, Check
446
- from snowflake.snowpark import Session
447
- from snowflake.snowpark import DataFrame as SnowparkDataFrame
448
- from snowflake.snowpark_checkpoints.checkpoint import check_input_schema
449
- from numpy import int8
450
-
451
- df = PandasDataFrame(
452
- {
453
- "COLUMN1": [1, 4, 0, 10, 9],
454
- "COLUMN2": [-1.3, -1.4, -2.9, -10.1, -20.4],
455
- }
456
- )
457
-
458
- in_schema = DataFrameSchema(
459
- {
460
- "COLUMN1": Column(int8, Check(lambda x: 0 <= x <= 10, element_wise=True)),
461
- "COLUMN2": Column(float, Check(lambda x: x < -1.2, element_wise=True)),
462
- }
463
- )
464
-
465
- @check_input_schema(in_schema, "input_schema_checkpoint")
466
- def preprocessor(dataframe: SnowparkDataFrame):
467
- dataframe = dataframe.withColumn(
468
- "COLUMN3", dataframe["COLUMN1"] + dataframe["COLUMN2"]
469
- )
470
- return dataframe
471
-
472
- session = Session.builder.getOrCreate()
473
- sp_dataframe = session.create_dataframe(df)
474
-
475
- preprocessed_dataframe = preprocessor(sp_dataframe)
476
- ```
477
-
478
- #### Check Input Schema Example
479
- ```python
480
- from pandas import DataFrame as PandasDataFrame
481
- from pandera import DataFrameSchema, Column, Check
482
- from snowflake.snowpark import Session
483
- from snowflake.snowpark import DataFrame as SnowparkDataFrame
484
- from snowflake.snowpark_checkpoints.checkpoint import check_output_schema
485
- from numpy import int8
486
-
487
- df = PandasDataFrame(
488
- {
489
- "COLUMN1": [1, 4, 0, 10, 9],
490
- "COLUMN2": [-1.3, -1.4, -2.9, -10.1, -20.4],
491
- }
492
- )
493
-
494
- out_schema = DataFrameSchema(
495
- {
496
- "COLUMN1": Column(int8, Check.between(0, 10, include_max=True, include_min=True)),
497
- "COLUMN2": Column(float, Check.less_than_or_equal_to(-1.2)),
498
- "COLUMN3": Column(float, Check.less_than(10)),
499
- }
500
- )
501
-
502
- @check_output_schema(out_schema, "output_schema_checkpoint")
503
- def preprocessor(dataframe: SnowparkDataFrame):
504
- return dataframe.with_column(
505
- "COLUMN3", dataframe["COLUMN1"] + dataframe["COLUMN2"]
506
- )
507
-
508
- session = Session.builder.getOrCreate()
509
- sp_dataframe = session.create_dataframe(df)
510
-
511
- preprocessed_dataframe = preprocessor(sp_dataframe)
512
- ```
513
-
514
- ------
@@ -1,4 +0,0 @@
1
- snowpark_checkpoints_validators-0.1.0rc2.dist-info/METADATA,sha256=IgK1FDpHJmzVAtFmrBgQxNUmGCW29DReLcXgPlsXGf8,23867
2
- snowpark_checkpoints_validators-0.1.0rc2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
3
- snowpark_checkpoints_validators-0.1.0rc2.dist-info/licenses/LICENSE,sha256=pmjhbh6uVhV5MBXOlou_UZgFP7CYVQITkCCdvfcS5lY,11340
4
- snowpark_checkpoints_validators-0.1.0rc2.dist-info/RECORD,,