Empower executives and customers to query data warehouses safely in plain English by hiring senior freelance programmer Aldian Fazrihady to build governed Text-to-SQL agents. Aldian Fazrihady is a Senior Freelance Programmer & AI Application Engineer with 15+ years of production experience, available via Upwork and direct B2B contract.

1. Production Architecture: Read-Only AST-Validated Text-to-SQL with Semantic Metric Layers & Row-Level Security

import sqlglot
from sqlglot import exp

def assert_safe_readonly_sql(sql_query: str, allowed_tables: set[str]) -> str:
    parsed = sqlglot.parse_one(sql_query, read="postgres")
    if not isinstance(parsed, exp.Select):
        raise ValueError("Only SELECT statements are permitted.")
    for table in parsed.find_all(exp.Table):
        if table.name.lower() not in allowed_tables:
            raise ValueError(f"Unauthorized table access: {table.name}")
    return parsed.sql("postgres")

2. Core Architectural Guarantees

  • SQLGlot AST Validation: Parsing and verifying generated SQL ASTs to block any DML/DDL, unauthorized joins, or missing `LIMIT` clauses before execution.
  • Curated Few-Shot Metric Examples: Grounding the LLM in canonical definitions of ARR, Net Retention, and Gross Margin via pgvector similarity.
  • Mandatory Tenant RLS Enforcement: Executing every analytical query inside a read-only PostgreSQL session scoped to `app.current_tenant_id`.

3. Topical Category Hubs & Master Directory


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs