-
Notifications
You must be signed in to change notification settings - Fork 82
Add methods to create data generation specs from files #310
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
Draft
ghanse
wants to merge
16
commits into
databrickslabs:master
Choose a base branch
from
ghanse:feature_file_specs
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d749476
Add methods to create data generation specs from files
ghanse c9c4e93
Update tests and documentation
ghanse cb2f355
Add toDict and fromDict methods for constraints
ghanse b1b1f1a
Add toDict method for column generation specs
ghanse b9e8ea9
Add methods to create data generators from config
ghanse 3c9b851
Clean-up messages from linter
ghanse 1dc8b13
Fix tests for creating data generators from config
ghanse 039b768
Show the Java version during build steps
ghanse b011c19
Test git action
ghanse 72e1b2c
Test git action
ghanse 900121f
Clean-up formatting
ghanse 07f4b7f
Clean-up formatting
ghanse adcc9b2
Test
ghanse 83ce7ba
Test
ghanse 607c17f
Test
ghanse 0f585b3
Try actions on ubuntu-22.04
ghanse 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
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
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
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
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
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
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 |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
This file defines the `DataGenError` and `DataGenerator` classes | ||
""" | ||
import copy | ||
import json | ||
import logging | ||
import re | ||
|
||
import yaml | ||
from pyspark.sql.types import LongType, IntegerType, StringType, StructType, StructField, DataType | ||
|
||
from ._version import _get_spark_version | ||
|
@@ -869,6 +871,17 @@ def withColumn(self, colName, colType=StringType(), minValue=None, maxValue=None | |
self._inferredSchemaFields.append(StructField(colName, newColumn.datatype, nullable)) | ||
return self | ||
|
||
def withColumnDefinitions(self, columns): | ||
""" Adds a set of columns to the synthetic generation specification. | ||
|
||
:param columns: A list of column generation specifications as dictionaries | ||
:returns: A modified in-place instance of a data generator allowing for chaining of calls | ||
following a builder pattern | ||
""" | ||
for column in columns: | ||
self.withColumn(**column) | ||
return self | ||
|
||
def _mkSqlStructFromList(self, fields): | ||
""" | ||
Create a SQL struct expression from a list of fields | ||
|
@@ -1206,6 +1219,12 @@ def _getColumnDataTypes(self, columns): | |
""" | ||
return [self._columnSpecsByName[colspec].datatype for colspec in columns] | ||
|
||
def getColumnGenerationSpecs(self): | ||
return self._allColumnSpecs | ||
|
||
def getConstraints(self): | ||
return self._constraints | ||
|
||
def withConstraint(self, constraint): | ||
"""Add a constraint to control the data generation | ||
|
||
|
@@ -1255,6 +1274,17 @@ def withSqlConstraint(self, sqlExpression: str): | |
self.withConstraint(SqlExpr(sqlExpression)) | ||
return self | ||
|
||
def withConstraintDefinitions(self, constraints): | ||
""" Adds a set of constraints to the synthetic generation specification. | ||
|
||
:param constraints: A list of constraints as dictionaries | ||
:returns: A modified in-place instance of a data generator allowing for chaining of calls | ||
following a builder pattern | ||
""" | ||
for c in constraints: | ||
self.withConstraint(Constraint.fromDict(c)) | ||
return self | ||
|
||
def computeBuildPlan(self): | ||
""" prepare for building by computing a pseudo build plan | ||
|
||
|
@@ -1604,3 +1634,66 @@ def scriptMerge(self, tgtName=None, srcName=None, updateExpr=None, delExpr=None, | |
result = HtmlUtils.formatCodeAsHtml(results) | ||
|
||
return result | ||
|
||
@staticmethod | ||
def fromDict(options): | ||
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. Make sure to have explicit tests for this covering the following use cases: 1 - with simple options See the examples on the following page for object valued options - i.e DateRange, Distribution objects |
||
""" Creates a data generator from a dictionary of options. | ||
:param options: Dictionary with data generator options (e.g. "name", "rows") | ||
:return: A data generator with the specified options | ||
""" | ||
generator = options["generator"] | ||
columns = options.get("columns", []) | ||
constraints = options.get("constraints", []) | ||
return ( | ||
DataGenerator(**generator) | ||
.withColumnDefinitions(columns) | ||
.withConstraintDefinitions(constraints) | ||
) | ||
|
||
def toDict(self): | ||
""" Creates a dictionary from a DataGenerator. | ||
:return: A dictionary representation of the DataGenerator | ||
""" | ||
generator = { | ||
"name": self.name, | ||
"rows": self.rowCount, | ||
"partitions": self.partitions, | ||
"random": self.random, | ||
"randomSeed": self.randomSeed, | ||
"startingId": self.starting_id, | ||
} | ||
return { | ||
"generator": generator, | ||
"columns": [column.toDict() for column in self.getColumnGenerationSpecs()], | ||
"constraints": [constraint.toDict() for constraint in self.getConstraints()] | ||
} | ||
|
||
@staticmethod | ||
def fromJson(options): | ||
""" Creates a data generator from a JSON string. | ||
:param options: A JSON string containing data generation options | ||
:return: A data generator with the specified options | ||
""" | ||
options = json.loads(options) | ||
return DataGenerator.fromDict(options) | ||
|
||
def toJson(self): | ||
""" Returns the JSON string representation of a data generator. | ||
:return: A JSON string representation of the DataGenerator | ||
""" | ||
return json.dumps(self.toDict()) | ||
|
||
@staticmethod | ||
def fromYaml(options): | ||
""" Creates a data generator from a YAML string. | ||
:param options: A YAML string containing data generation options | ||
:return: A data generator with the specified options | ||
""" | ||
options = yaml.safe_load(options) | ||
return DataGenerator.fromDict(options) | ||
|
||
def toYaml(self): | ||
""" Returns the YAML string representation of a data generator. | ||
:return: A YAML string representation of the DataGenerator | ||
""" | ||
return yaml.dump(self.toDict()) |
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
Oops, something went wrong.
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.