AshenH commited on
Commit
e3b4d13
·
verified ·
1 Parent(s): 63e3ae4

Update tools/sql_tool.py

Browse files
Files changed (1) hide show
  1. tools/sql_tool.py +19 -5
tools/sql_tool.py CHANGED
@@ -20,11 +20,25 @@ class SQLTool:
20
  eval(key_json) if key_json.strip().startswith("{") else {}
21
  )
22
  self.client = bigquery.Client(credentials=creds, project=cfg.gcp_project)
23
- elif self.backend == "motherduck":
24
- import duckdb
25
- token = self.cfg.motherduck_token or os.getenv("MOTHERDUCK_TOKEN")
26
- db_name = self.cfg.motherduck_db or "default"
27
- self.client = duckdb.connect(f"md:/{db_name}?motherduck_token={token}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  else:
29
  raise RuntimeError("Unknown SQL backend")
30
 
 
20
  eval(key_json) if key_json.strip().startswith("{") else {}
21
  )
22
  self.client = bigquery.Client(credentials=creds, project=cfg.gcp_project)
23
+ elif self.backend == "motherduck":
24
+ import duckdb
25
+ token = self.cfg.motherduck_token or os.getenv("MOTHERDUCK_TOKEN")
26
+ db_name = self.cfg.motherduck_db or "default"
27
+
28
+ # Start a plain DuckDB connection
29
+ self.client = duckdb.connect()
30
+
31
+ # Ensure the MotherDuck extension is available and loaded
32
+ # (DuckDB will download it automatically in this environment)
33
+ self.client.execute("INSTALL motherduck;")
34
+ self.client.execute("LOAD motherduck;")
35
+
36
+ # Provide token and attach the remote MotherDuck database as 'md'
37
+ if not token:
38
+ raise RuntimeError("Missing MOTHERDUCK_TOKEN")
39
+ self.client.execute(f"SET motherduck_token='{token}';")
40
+ self.client.execute(f"ATTACH 'md:/{db_name}' AS md;")
41
+ self.client.execute("USE md;") # subsequent queries run against 'md' by default
42
  else:
43
  raise RuntimeError("Unknown SQL backend")
44