Skip to content

Commit

Permalink
Merge pull request #22 from Snowflake-Labs/jhilgart/update-readme-and…
Browse files Browse the repository at this point in the history
…-save-functionality

Jhilgart/update readme and save functionality
  • Loading branch information
sfc-gh-jhilgart authored May 1, 2024
2 parents 9a42a83 + fda5dfc commit 8eba113
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ os.environ['SNOWFLAKE_HOST'] = '<your-snowflake-host>'

You may generate a semantic model for a given list of fully qualified tables following the `{database}.{schema}.{table}` format. Each table in this list should be a physical table or a view present in your database.

All generated semantic models by default are saved under `semantic_model_generator/output_models`.
All generated semantic models by default are saved either under `semantic_model_generator/output_models` if running from the root of this project or the current directory you're in.

### Generation - Python

Expand All @@ -69,7 +69,7 @@ pip install dist/semantic_model_generator-0.1.9-py3-none-any.whl
```bash
python
```
3. Generate a semantic model
3. Generate a semantic model.
```python
from semantic_model_generator.generate_model import generate_base_semantic_model_from_snowflake

Expand Down
14 changes: 9 additions & 5 deletions semantic_model_generator/generate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ def generate_base_semantic_model_from_snowflake(
Returns:
None. Writes the semantic context to a YAML file.
"""
formatted_datetime = datetime.now().strftime("%Y%m%d%H%M%S")
if not output_yaml_path:
output_yaml_path = f"semantic_model_generator/output_models/{formatted_datetime}_{_to_snake_case(semantic_model_name)}.yaml"
context = raw_schema_to_semantic_context(
physical_tables,
snowflake_account=snowflake_account,
Expand All @@ -278,16 +281,17 @@ def generate_base_semantic_model_from_snowflake(
yaml_str = proto_utils.proto_to_yaml(context)
# Once we have the yaml, update to include to # <FILL-OUT> tokens.
yaml_str = append_comment_to_placeholders(yaml_str)
if output_yaml_path:

if output_yaml_path: # Assume user gives us correct path.
write_path = output_yaml_path
else:
current_datetime = datetime.now()
# If the output path is not specified or the directory does not exist,
# save in the current directory with a formatted name.
write_path = f"{formatted_datetime}_{_to_snake_case(semantic_model_name)}.yaml"

# Format the current date and time as "YYYY-MM-DD"
formatted_datetime = current_datetime.strftime("%Y%m%d%H%M%S")
write_path = f"semantic_model_generator/output_models/{formatted_datetime}_{_to_snake_case(semantic_model_name)}.yaml"
with open(write_path, "w") as f:
f.write(yaml_str)
logger.info(f"Semantic model saved to {write_path}")
return None


Expand Down
2 changes: 1 addition & 1 deletion semantic_model_generator/tests/validate_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_invalid_yaml_missing_quote(
with pytest.raises(ValueError) as exc_info:
validate(temp_invalid_yaml_unmatched_quote_file, account_name)

expected_error_fragment = "Unable to execute query with your logical table against physical tables on Snowflake. Query = SELECT ALIAS, ZIP_CODE FROM AUTOSQL_DATASET_BIRD_V2.ADDRESS.ALIAS LIMIT 100. Error = Invalid column name 'ZIP_CODE\"'. Mismatched quotes detected."
expected_error_fragment = "Unable to execute query with your logical table against physical tables on Snowflake. Error = Invalid column name 'ZIP_CODE\"'. Mismatched quotes detected."

assert expected_error_fragment in str(exc_info.value), "Unexpected error message"

Expand Down

0 comments on commit 8eba113

Please sign in to comment.