File size: 13,975 Bytes
1a436de
 
 
 
 
 
 
 
bcca921
1a436de
 
d862493
1a436de
 
 
 
d862493
1a436de
 
d862493
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
bcca921
 
 
 
 
1a436de
 
 
 
 
 
 
 
bcca921
1a436de
 
 
bcca921
 
 
 
1a436de
 
 
bcca921
 
 
 
 
 
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcca921
1a436de
 
 
 
 
 
 
 
 
 
bcca921
 
d862493
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
d862493
1a436de
d862493
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
d862493
1a436de
d862493
1a436de
 
 
 
d862493
 
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d862493
 
1a436de
 
 
 
 
 
 
 
 
d862493
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d862493
 
1a436de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d862493
1a436de
 
 
 
 
 
bcca921
 
 
1a436de
 
 
 
 
 
 
 
 
 
d862493
 
1a436de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import io
import logging
import os
import shutil
import sys
import tempfile
import uuid
from pathlib import Path
from typing import List, Tuple

import duckdb
import gradio as gr
import pandas as pd
import pytest
import requests
from dotenv import load_dotenv

from src.client import LLMChain
from src.pipelines import Query2Schema

load_dotenv()


LEVEL = "INFO" if not os.getenv("ENV") == "PROD" else "WARNING"
logging.basicConfig(
    level=getattr(logging, LEVEL, logging.INFO),
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)

if not Path("/tmp").exists():
    os.mkdir("/tmp")


def download_file(url: str, save_path: str):
    if Path(save_path).exists():
        print(f"File already exists at {save_path}. Skipping download.")
        return duckdb.connect(database=save_path)

    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()

        with open(save_path, "wb") as out_file:
            shutil.copyfileobj(response.raw, out_file)
        return duckdb.connect(database=save_path)
    except Exception as e:
        logger.info(f"Error Downloding Chinook DB: {e}")
        raise


conn = download_file(
    url="https://raw.githubusercontent.com/RandomFractals/duckdb-sql-tools/main/data/chinook/duckdb/chinook.duckdb",
    save_path="database/chinook.duckdb",
)
pipe = Query2Schema(duckdb=conn, chain=LLMChain())


def get_test_databases() -> List[str]:
    """Scans the 'tests' directory for subdirectories (representing databases)."""

    return ["All", "chinook", "Northwind"]


def get_tables_names(schema_name):
    tables = conn.execute("SELECT table_name FROM information_schema.tables").fetchall()
    return [table[0] for table in tables]


def update_table_names(schema_name):
    tables = get_tables_names(schema_name)
    return gr.update(choices=tables, value=tables[0] if tables else None)


def update_column_names(table_name):
    columns = conn.execute(
        f"SELECT column_name FROM information_schema.columns WHERE table_name = '{table_name}' "
    ).fetchall()
    columns = [column[0] for column in columns]
    df = pd.DataFrame(columns, columns=["Column Names"])
    # return gr.update(
    #     choices=columns,
    #     value=columns[0] if columns else None
    # )
    return df


def get_ddl(table: str) -> str:
    result = conn.sql(
        f"SELECT sql, database_name, schema_name FROM duckdb_tables() where table_name ='{table}';"
    ).df()
    ddl_create = result.iloc[0, 0]
    parent_database = result.iloc[0, 1]
    schema_name = result.iloc[0, 2]
    full_path = f"{parent_database}.{schema_name}.{table}"
    if schema_name != "main":
        old_path = f"{schema_name}.{table}"
    else:
        old_path = table
    ddl_create = ddl_create.replace(old_path, full_path)
    return ddl_create


def run_pipeline(table: str, query_input: str) -> Tuple[str, pd.DataFrame]:
    try:
        schema = get_ddl(table=table)
    except Exception as e:
        logger.error(f"Failed to fetch DDL for table {table}: {e}")
        raise
    try:
        sql, df = pipe.try_sql_with_retries(
            user_question=query_input,
            context=schema,
        )
        sql = sql.get("sql_query") if isinstance(sql, dict) else sql
        if not sql:
            raise ValueError("SQL generation returned None")
        return sql, df
    except Exception as e:
        logger.error(f"Error generating SQL for table {table}: {e}")
        raise


def create_mesh_model(sql: str, db_name: str = "chinook") -> Tuple[str, str, str]:
    model_name = f"model_{uuid.uuid4().hex[:8]}"

    # Use catalog.schema.model_name format
    full_model_name = f"{db_name}.{model_name}"

    MODEL_HEADER = f"""MODEL (
name {full_model_name},
kind FULL
);
    """
    try:
        model_dir = Path("models/")
        model_dir.mkdir(parents=True, exist_ok=True)

        model_path = model_dir / f"{model_name}.sql"
        model_text = MODEL_HEADER + "\n" + sql.replace("chinook.main.", "")
        model_path.write_text(model_text)

        return model_text, str(model_path), full_model_name
    except Exception as e:
        logger.error(f"Error creating SQL Mesh model: {e}")
        raise


def create_pandera_schema(
    sql: str, user_instruction: str, model_name: str
) -> Tuple[str, str]:
    SCRIPT_HEADER = """
import pandas as pd
import pandera.pandas as pa
from pandera.typing import *

import pytest
from sqlmesh import Context
from datetime import date
from pathlib import Path
import shutil
import duckdb

    """

    MESH_STR = f"""
@pytest.fixture(scope="session")
def mesh_context():

    context = Context(paths=".", gateway="duckdb", load=True)
    yield context

@pytest.fixture
def today_str():
    return date.today().isoformat()

def test_back_fill(mesh_context, today_str):
    mesh_context.plan(skip_backfill=False, auto_apply=True)
    mesh_context.run(start=today_str, end=today_str)

    # df = mesh_context.fetchdf("SELECT * FROM {model_name} LIMIT 10")
    # assert not df.empty
    """
    try:
        schema = pipe.generate_pandera_schema(
            sql_query=sql, user_instruction=user_instruction
        )
        test_schema = f"""
        
def test_schema(mesh_context, today_str):    
    df = mesh_context.evaluate(
        "{model_name}",
        start=today_str,
        end=today_str,
        execution_time=today_str,
    )
    {schema.split()[1].split("(")[0].strip()}.validate(df)
    """
        print(schema)

        with tempfile.NamedTemporaryFile(
            mode="w",
            prefix="test_",
            suffix=".py",
            delete=False,
            encoding="utf-8",
        ) as f:
            f.write(SCRIPT_HEADER)
            f.write("\n\n")
            f.write(schema)
            f.write("\n\n")
            f.write(MESH_STR)
            f.write("\n\n")
            f.write(test_schema)

        file_path = Path(f.name)

        return schema, str(file_path)
    except Exception as e:
        logger.error(f"Error creating Pandera schema: {e}")
        raise


def create_test_file(
    table_name: str, db_name: str, sql_instruction: str, user_instruction: str
) -> Tuple[str, str, pd.DataFrame, str, str]:
    try:
        sql, df = run_pipeline(table=table_name, query_input=sql_instruction)
        model_text, model_file, model_name = create_mesh_model(sql=sql, db_name=db_name)
        schema, test_file = create_pandera_schema(
            sql=sql,
            user_instruction=user_instruction,
            model_name=model_name,
        )
        return test_file, model_file, df, model_text, schema
    except Exception as e:
        logger.error(f"Error creating test file for table {table_name}: {e}")
        raise


def run_tests(
    table_name: str, db_name: str, sql_instruction: str, user_instruction: str
):
    test_file, model_file, df, model_text, schema = create_test_file(
        table_name=table_name,
        db_name=db_name,
        sql_instruction=sql_instruction,
        user_instruction=user_instruction,
    )

    capture_out = io.StringIO()
    capture_err = io.StringIO()

    old_out = sys.stdout
    old_err = sys.stderr

    sys.stdout = capture_out
    sys.stderr = capture_err

    try:
        retcode = pytest.main(
            [
                test_file,
                "-s",
                "--tb=short",
                "--disable-warnings",
                "-o",
                "cache_dir=/tmp",
            ]
        )
    except Exception as e:
        sys.stdout = old_out
        sys.stderr = old_err
        return f"Error running tests: {str(e)}", ""

    sys.stdout = old_out
    sys.stderr = old_err

    output = capture_out.getvalue() + "\n" + capture_err.getvalue()

    for f in [test_file, model_file]:
        try:
            os.remove(f)
        except FileNotFoundError:
            pass

    return output, df, model_text, schema


custom_css = """
/* --- Overall container --- */
.gradio-container {
    background-color: #f0f4f8; /* light background */
    font-family: 'Arial', sans-serif;
}

/* --- Logo --- */
.logo {
    max-width: 200px;
    margin: 20px auto;
    display: block;
}

/* --- Buttons --- */
.gr-button {
    background-color: #4a90e2 !important;  /* primary color */
    font-size: 14px;                        /* fixed font size */
    padding: 6px 12px !important;           /* fixed padding */
    height: 36px !important;                /* fixed height */
    min-width: 120px !important;            /* fixed width */
}
.gr-button:hover {
    background-color: #3a7bc8 !important;
}

/* --- Logs Textbox --- */
#logs textarea {
    overflow-y: scroll;   
    resize: none;         
    height: 400px;        
    width: 100%;          
    font-family: monospace; 
    font-size: 13px;
    line-height: 1.4;
}

/* Optional: small spacing between rows */
.gr-row {
    gap: 10px;
}
"""

with gr.Blocks(
    theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo"), css=custom_css
) as demo:
    gr.Image("logo.png", label=None, show_label=False, container=False, height=100)

    gr.Markdown(
        """
    <div style='text-align: center;'>
    <strong style='font-size: 36px;'>SQL Test Suite</strong>
    <br>
    <span style='font-size: 20px;'>Automated testing and schema validation for SQL models with LLM.</span>
    </div>
    """
    )

    with gr.Row():
        with gr.Column(scale=1):
            schema_dropdown = gr.Dropdown(
                choices=["chinook", "northwind"],
                value="chinook",
                label="Select Schema",
                interactive=True,
            )
            tables_dropdown = gr.Dropdown(
                choices=[], label="Available Tables", value=None, interactive=True
            )
            # columns_dropdown = gr.Dropdown(choices=[], label="Available Columns", value=None, interactive=True)
            columns_df = gr.DataFrame(label="Columns", value=[], interactive=False)
            # with gr.Row():
            #     generate_result = gr.Button("Run Tests", variant="primary")

        with gr.Column(scale=3):
            with gr.Row():
                sql_instruction = gr.Textbox(
                    lines=3,
                    label="Business Metric Query (Plain English)",
                    placeholder=(
                        "Describe the business question you want to answer.\n"
                        "Example: 'Show me the average sales per month.'\n"
                        "Example: 'Total revenue by product category for last year.'"
                    ),
                )
            with gr.Row():
                user_instruction = gr.Textbox(
                    lines=5,
                    label="Define Data Quality Level",
                    placeholder=(
                        "Describe the validation rule and how strict it should be.\n"
                        "Example: Validate that the incident_zip column contains valid 5-digit ZIP codes.\n"
                    ),
                )
            with gr.Row():
                with gr.Column(scale=7):
                    pass
                with gr.Column(scale=1):
                    run_tests_btn = gr.Button("▶ Run Tests", variant="primary")

            with gr.Row():
                with gr.Column():
                    with gr.Tabs():
                        with gr.Tab("Test Logs"):
                            with gr.Row():
                                with gr.Column():
                                    test_logs = gr.Textbox(
                                        label="Test Logs",
                                        lines=20,
                                        max_lines=20,
                                        interactive=False,
                                        elem_id="logs",
                                    )

                        with gr.Tab("SQL Model"):
                            with gr.Row():
                                with gr.Column():
                                    sql_model = gr.Textbox(
                                        label="SQL Model",
                                        lines=20,
                                        max_lines=20,
                                        interactive=False,
                                        elem_id="sql_model",
                                    )

                        with gr.Tab("Schema"):
                            with gr.Row():
                                with gr.Column():
                                    result_schema = gr.Textbox(
                                        label="Validation Schema",
                                        lines=20,
                                        max_lines=20,
                                        interactive=False,
                                    )

                        with gr.Tab("Data"):
                            with gr.Row():
                                with gr.Column():
                                    result_data = gr.DataFrame(
                                        label="Query Result",
                                        value=[],
                                        interactive=False,
                                    )

        schema_dropdown.change(
            update_table_names, inputs=schema_dropdown, outputs=tables_dropdown
        )
        tables_dropdown.change(
            update_column_names, inputs=tables_dropdown, outputs=columns_df
        )
        demo.load(
            fn=update_table_names, inputs=schema_dropdown, outputs=tables_dropdown
        )
        run_tests_btn.click(
            run_tests,
            inputs=[
                tables_dropdown,
                schema_dropdown,
                sql_instruction,
                user_instruction,
            ],
            outputs=[test_logs, result_data, sql_model, result_schema],
        )

if __name__ == "__main__":
    demo.launch(debug=True)