Skip to content

Commit

Permalink
chore(ibis): implement mdl validation scripts (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmedal authored Oct 30, 2024
1 parent 3ed5e2a commit 8835456
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions ibis-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ markers = [
[tool.ruff]
line-length = 88
target-version = "py311"
exclude = ["tools/"]

[tool.ruff.lint]
select = [
Expand Down
10 changes: 10 additions & 0 deletions ibis-server/tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Description

This folder contains useful tools and scripts for debugging and validation.

# Tools
- `mdl_validation.py`: Used to validate a Wren MDL. This script attempts to select all columns in all models.
- Requires the `wren_core` library. Run `just install-core` and `just install` before using it.
```
poetry run python tools/mdl_validation.py mdl.json function_list/bigquery.csv
```
47 changes: 47 additions & 0 deletions ibis-server/tools/mdl_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# This script is used to validate the MDL file by transforming the SQL queries
# in the MDL file to the SQL queries. This script only validate if it can be transformed
# by Wren core. It does not validate the SQL can be executed by the data source.
#
# Argements:
# - manifest_json_path: Path to the JSON file
# - function_list_path: Path to the function list CSV file
#

import argparse
import base64
import json

from wren_core import SessionContext

# Set up argument parsing
parser = argparse.ArgumentParser(description="Validate the MDL file")
parser.add_argument("manifest_json_path", help="Path to the JSON file")
parser.add_argument("function_list_path", help="Path to the function list CSV file")

args = parser.parse_args()

# Read and encode the JSON data
with open(args.manifest_json_path) as file:
mdl = json.load(file)
# Convert to JSON string
json_str = json.dumps(mdl)
# Encode to base64
encoded_str = base64.b64encode(json_str.encode("utf-8")).decode("utf-8")

session_context = SessionContext(encoded_str, args.function_list_path)
error_cases = []
for model in mdl["models"]:
for column in model["columns"]:
# ignore hidden columns
if column.get("is_hidden"):
continue
sql = f"select \"{column['name']}\" from \"{model['name']}\""
try:
planned_sql = session_context.transform_sql(sql)
except Exception:
error_cases.append((model, column))
print(f"Error transforming {model['name']} {column['name']}")

if len(error_cases) > 0:
raise Exception(f"Error transforming {len(error_cases)} columns")

0 comments on commit 8835456

Please sign in to comment.