Skip to content

Commit b6f5777

Browse files
Adding alloydb omni as a separate db_type
1 parent aa47454 commit b6f5777

5 files changed

Lines changed: 57 additions & 78 deletions

File tree

datasets/bat/db_configs/alloydb.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ dialect: postgres
33
database_name: <your-db-name>
44
database_path: <your-db-path> # should be in the following format: 'projects/{PROJECT_ID}/locations/{REGION}/clusters/{CLUSTER_NAME}/instances/{INSTANCE_NAME}'
55
max_executions_per_minute: 180
6-
host: <your-host> # usef for connecting to omni
7-
port: <your-port> # usef for connecting to omni, e.g. 5432
86
user_name: <your-username>
97
password: <your-password>
10-
nl_config: <nl-config-name> # name of the nl config that will be used for query generation. This should be equal to the name used in the database setup.
11-
omni: false # set to true if you want to use omni for the database connection
8+
nl_config: <nl-config-name> # name of the nl config that will be used for query generation. This should be equal to the name used in the database setup.

datasets/bat/run_config.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

evalbench/databases/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from .db import DB
66
from .bigquery import BQDB
77
from .alloydb import AlloyDB
8+
from .alloydb_omni import AlloyDBOmni
9+
810

911

1012
def get_database(db_config, db_name) -> DB:
@@ -26,4 +28,6 @@ def get_database(db_config, db_name) -> DB:
2628
return BQDB(db_config)
2729
if db_config["db_type"] == "alloydb":
2830
return AlloyDB(db_config)
31+
if db_config["db_type"] == "alloydb_omni":
32+
return AlloyDBOmni(db_config)
2933
raise ValueError("DB Type not Supported")

evalbench/databases/alloydb.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,19 @@ def __init__(self, db_config):
1616
"""
1717
super().__init__(db_config)
1818
self.nl_config = db_config['nl_config']
19-
self.omni = db_config.get("omni", False)
20-
self.host = db_config.get("host", "localhost")
21-
self.port = db_config.get("port", 5432)
2219

2320
self.connector = AlloyDBConnector()
2421

2522
def get_conn_alloydb():
26-
if not self.omni:
27-
return self.connector.connect(
28-
self.db_path,
29-
"pg8000",
30-
user=self.username,
31-
password=self.password,
32-
db=self.db_name,
33-
enable_iam_auth=False,
34-
ip_type=AlloyDBIPTypes.PUBLIC,
35-
)
36-
else:
37-
conn_str = f"user={self.username} password={self.password} host={self.host} port={self.port} dbname={self.db_name}"
38-
return pg8000.connect(
39-
user=self.username,
40-
password=self.password,
41-
host=self.host,
42-
port=self.port,
43-
database=self.db_name
44-
)
23+
return self.connector.connect(
24+
self.db_path,
25+
"pg8000",
26+
user=self.username,
27+
password=self.password,
28+
db=self.db_name,
29+
enable_iam_auth=False,
30+
ip_type=AlloyDBIPTypes.PUBLIC,
31+
)
4532

4633
def get_engine_args_alloydb():
4734
common_args = {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
from .db import DB
3+
from .postgres import PGDB
4+
import sqlalchemy
5+
import pg8000
6+
from sqlalchemy.pool import NullPool
7+
8+
9+
class AlloyDBOmni(PGDB):
10+
def __init__(self, db_config):
11+
"""
12+
Initializes the AlloyDB connection, overriding the PGDB's
13+
default Google Cloud SQL connection mechanism.
14+
"""
15+
super().__init__(db_config)
16+
self.nl_config = db_config['nl_config']
17+
self.host = db_config.get("host", "localhost")
18+
self.port = db_config.get("port", 5432)
19+
20+
def get_conn_alloydb():
21+
return pg8000.connect(
22+
user=self.username,
23+
password=self.password,
24+
host=self.host,
25+
port=self.port,
26+
database=self.db_name
27+
)
28+
29+
def get_engine_args_alloydb():
30+
common_args = {
31+
"creator": get_conn_alloydb,
32+
"connect_args": {"command_timeout": 60},
33+
}
34+
if "is_tmp_db" in db_config:
35+
common_args["poolclass"] = NullPool
36+
else:
37+
common_args["pool_size"] = 50
38+
common_args["pool_recycle"] = 300
39+
return common_args
40+
41+
self.engine = sqlalchemy.create_engine(
42+
"postgresql+pg8000://", **get_engine_args_alloydb()
43+
)

0 commit comments

Comments
 (0)