Skip to content

Commit 529430d

Browse files
committed
refactor: _get_column_info 함수 최적화
1 parent 9c48299 commit 529430d

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

llm_utils/tools.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
T = TypeVar("T")
1111
R = TypeVar("R")
1212

13-
1413
def parallel_process(
1514
items: Iterable[T],
1615
process_fn: Callable[[T], R],
@@ -93,30 +92,27 @@ def _get_table_info(max_workers: int = 8) -> Dict[str, str]:
9392
return table_info
9493

9594

96-
def _get_column_info(table_name: str, max_workers: int = 8) -> List[Dict[str, str]]:
97-
"""table_name에 해당하는 컬럼 이름과 설명을 가져오는 함수
95+
def _get_column_info(table_name: str, urn_table_mapping: Dict[str, str], max_workers: int = 8) -> List[Dict[str, str]]:
96+
"""table_name에 해당하는 컬럼 이름과 설명을 가져오는 함수 (최적화됨)
9897
9998
Args:
10099
table_name (str): 테이블 이름
100+
urn_table_mapping (Dict[str, str]): URN-테이블명 매핑 딕셔너리
101101
max_workers (int, optional): 병렬 처리에 사용할 최대 쓰레드 수. Defaults to 8.
102102
103103
Returns:
104104
List[Dict[str, str]]: 컬럼 정보 리스트
105105
"""
106+
# 해당 테이블의 URN 직접 찾기
107+
target_urn = urn_table_mapping.get(table_name)
108+
if not target_urn:
109+
return []
110+
111+
# Fetcher 생성 및 컬럼 정보 가져오기
106112
fetcher = _get_fetcher()
107-
urns = fetcher.get_urns()
108-
109-
results = parallel_process(
110-
urns,
111-
lambda urn: _process_column_info(urn, table_name, fetcher),
112-
max_workers=max_workers,
113-
show_progress=False,
114-
)
115-
116-
for result in results:
117-
if result:
118-
return result
119-
return []
113+
column_info = fetcher.get_column_names_and_descriptions(target_urn)
114+
115+
return column_info
120116

121117

122118
def get_info_from_db(max_workers: int = 8) -> List[Document]:
@@ -129,10 +125,19 @@ def get_info_from_db(max_workers: int = 8) -> List[Document]:
129125
List[Document]: 테이블과 컬럼 정보를 담은 Document 객체 리스트
130126
"""
131127
table_info = _get_table_info(max_workers=max_workers)
128+
129+
# URN-테이블명 매핑을 한 번만 생성
130+
fetcher = _get_fetcher()
131+
urns = list(fetcher.get_urns())
132+
urn_table_mapping = {}
133+
for urn in urns:
134+
table_name = fetcher.get_table_name(urn)
135+
if table_name:
136+
urn_table_mapping[table_name] = urn
132137

133138
def process_table_info(item: tuple[str, str]) -> str:
134139
table_name, table_description = item
135-
column_info = _get_column_info(table_name, max_workers=max_workers)
140+
column_info = _get_column_info(table_name, urn_table_mapping, max_workers=max_workers)
136141
column_info_str = "\n".join(
137142
[
138143
f"{col['column_name']}: {col['column_description']}"
@@ -147,7 +152,7 @@ def process_table_info(item: tuple[str, str]) -> str:
147152
max_workers=max_workers,
148153
desc="컬럼 정보 수집 중",
149154
)
150-
155+
151156
return [Document(page_content=info) for info in table_info_str_list]
152157

153158

0 commit comments

Comments
 (0)