import sys, json, time, urllib.request TRINO = "http://127.0.0.1:8089" USER = "mo" def run(sql): req = urllib.request.Request( TRINO + "/v1/statement", data=sql.encode(), headers={"X-Trino-User": USER, "X-Trino-Catalog": "iceberg", "Content-Type": "text/plain"}) rows, cols = [], None r = json.loads(urllib.request.urlopen(req).read()) while True: if r.get("columns") and cols is None: cols = [c["name"] for c in r["columns"]] rows.extend(r.get("data", []) or []) nxt = r.get("nextUri") st = r.get("stats", {}).get("state") err = r.get("error") if err: raise RuntimeError(json.dumps(err.get("message", err))) if not nxt: break time.sleep(0.1) r = json.loads(urllib.request.urlopen(urllib.request.Request( nxt, headers={"X-Trino-User": USER})).read()) return cols, rows if __name__ == "__main__": sql = sys.stdin.read() if len(sys.argv) < 2 else sys.argv[1] for stmt in [s for s in sql.split(";\n") if s.strip()]: cols, rows = run(stmt.strip()) print(f"--- {stmt.strip()[:70]} ---") if cols: print("\t".join(cols)) for row in rows[:50]: print("\t".join(str(x) for x in row))