Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fred3m committed Jul 5, 2024
1 parent 6ee00db commit 5f19a93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
19 changes: 13 additions & 6 deletions python/lsst/rubintv/analysis/service/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def __call__(self, database: ConsDbSchema):
table_model1 = database.tables[table1]
table_model2 = database.tables[table2]
joins = []
print("matches is", self.matches)
print("table1 is", table1)
print("table2 is", table2)
for index in range(self.n_columns):
joins.append(
table_model1.columns[self.matches[table1][index]]
Expand Down Expand Up @@ -289,12 +292,14 @@ def get_join(self, table1: str, table2: str) -> sqlalchemy.ColumnElement:

def build_join(self, table_names: set[str]) -> sqlalchemy.Table | sqlalchemy.Join:
tables = list(table_names)
last_table = tables[0]
select_from = self.tables[last_table]
if len(table_names) > 1:
for table_name in tables[1:]:
join = self.get_join(last_table, table_name)
select_from = sqlalchemy.join(select_from, self.tables[table_name], join)
select_from = self.tables[tables[0]]
print("tables are", tables)
for i in range(1, len(tables)):
current_table = tables[i]
previous_table = tables[i-1]
print("current:", current_table, "previous:", previous_table)
join = self.get_join(previous_table, current_table)
select_from = sqlalchemy.join(select_from, self.tables[current_table], join)
return select_from

def fetch_data(self, query_model: sqlalchemy.Select) -> dict[str, list]:
Expand All @@ -307,6 +312,7 @@ def fetch_data(self, query_model: sqlalchemy.Select) -> dict[str, list]:
connection = self.engine.connect()
result = connection.execute(query_model)
data = result.fetchall()
connection.close()

# Convert the unnamed row data into columns
return {str(col): [row[i] for row in data] for i, col in enumerate(result.keys())}
Expand Down Expand Up @@ -383,6 +389,7 @@ def query(
if query is not None:
query_result = query(self)
query_model = sqlalchemy.and_(query_model, query_result.result)
table_names.add(*query_result.tables)
if data_ids is not None:
data_id_select = sqlalchemy.tuple_(day_obs_column, seq_num_column).in_(data_ids)
query_model = sqlalchemy.and_(query_model, data_id_select)
Expand Down
48 changes: 32 additions & 16 deletions scripts/joins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ joins:
ccdvisit1:
- visit_id

# exposure and ccdvisit1_quicklook (through ccdexposure and ccdvisit1)
- type: inner
matches:
exposure:
- exposure_id
ccdvisit1_quicklook:
- visit_id

# exposure and visit1_quicklook
- type: inner
matches:
exposure:
- exposure_id
visit1_quicklook:
- visit_id

# ccdexposure and ccdexposure_camera
- type: inner
matches:
Expand Down Expand Up @@ -72,6 +88,22 @@ joins:
visit1_quicklook:
- visit_id

# visit1 and ccdvisit1
- type: inner
matches:
visit1:
- visit_id
ccdvisit1:
- visit_id

# visit1 and ccdvisit1_quicklook
- type: inner
matches:
visit1:
- visit_id
ccdvisit1_quicklook:
- visit_id

# ccdvisit1 and ccdvisit1_quicklook
- type: inner
matches:
Expand All @@ -96,22 +128,6 @@ joins:
ccdexposure_flexdata_schema:
- key

# exposure and ccdvisit1_quicklook (through ccdexposure and ccdvisit1)
- type: inner
matches:
exposure:
- exposure_id
ccdvisit1_quicklook:
- visit_id

# exposure and visit1_quicklook
- type: inner
matches:
exposure:
- exposure_id
visit1_quicklook:
- visit_id

# ccdexposure and visit1
- type: inner
matches:
Expand Down

0 comments on commit 5f19a93

Please sign in to comment.