Skip to content

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
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

ghanse
Copy link
Collaborator

@ghanse ghanse commented Feb 7, 2025

Proposed changes

Added several methods to support creating DataGenerator and ColumnGenerationSpec objects from Python dictionaries and JSON/YAML files.

Types of changes

What types of changes does your code introduce to dbldatagen?
Put an x in the boxes that apply

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Change to tutorials, tests or examples
  • Non code change (readme, images or other non-code assets)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR.
If you're unsure about any of them, don't hesitate to ask. We're here to help!
This is simply a reminder of what we are going to look for before merging your code.

  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules
  • Submission does not reduce code coverage numbers
  • Submission does not increase alerts or messages from prospector / lint

Further comments

I added several methods:

  • withColumns adds ColumnGenerationSpec objects via a list of dictionaries; It iteratively passes the dictionary values as arguments to withColumn
  • fromDict creates a DataGenerator from a dictionary by passing the values as arguments to the constructor
  • fromJson allows users to create a DataGenerator and add ColumnGenerationSpecs from a JSON file
  • fromYaml allows users to create a DataGenerator and add ColumnGenerationSpecs` from a YAML file
  • fromFile wraps both fromJson and fromYaml into a single API

@@ -1604,3 +1617,51 @@ def scriptMerge(self, tgtName=None, srcName=None, updateExpr=None, delExpr=None,
result = HtmlUtils.formatCodeAsHtml(results)

return result

@staticmethod
def fromDict(options):
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
2 - with composite (object valued options)

See the examples on the following page for object valued options - i.e DateRange, Distribution objects

return DataGenerator(**options)

@staticmethod
def fromFile(path):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont add fromFile as method as open does not support reading file from a Databricks workspace or dbfs

raise ValueError("File type must be '.json' or '.yml'")

@staticmethod
def fromJson(path):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than taking a path, pass a string containing the definition to method

Calling code should be responsible for loading string

it could be from dbfs, from a database, from unity catalog

return DataGenerator.fromDict(generator).withColumns(columns)

@staticmethod
def fromYaml(path):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than taking a path, pass a string containing the definition to method

Calling code should be responsible for loading string

it could be from dbfs, from a database, from unity catalog

@@ -182,3 +182,48 @@ This has several implications:
SQL expression.
To enforce the dependency, you must use the `baseColumn` attribute to indicate the dependency.

Creating data generation specs from files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be creating data specs from string based YAML or JSON

Also we should have capability to write to JSON and YAML

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ronanstokes-db the code is done. I will update the docs.

assert gen_from_dict.randomSeed == dg_spec.get("randomSeed")

def test_generation_from_file(self):
path = "tests/files/test_generator_spec.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use string based APIs, they'll be more general - also you can simply define the definitions as multi-line strings rather than requiring separate data files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants