feat(factors): 添加 SchemaCache 实现数据库表结构自动扫描
This commit is contained in:
@@ -16,6 +16,7 @@ from src.factors.dsl import (
|
||||
from src.factors.compiler import DependencyExtractor
|
||||
from src.factors.translator import PolarsTranslator
|
||||
from src.factors.engine.data_spec import DataSpec, ExecutionPlan
|
||||
from src.factors.engine.schema_cache import get_schema_cache
|
||||
|
||||
|
||||
class ExecutionPlanner:
|
||||
@@ -73,9 +74,8 @@ class ExecutionPlanner:
|
||||
) -> List[DataSpec]:
|
||||
"""从依赖推导数据规格。
|
||||
|
||||
基础行情字段(open, high, low, close, vol, amount, pre_close, change, pct_chg)
|
||||
默认从 pro_bar 表获取。
|
||||
每日指标字段(total_mv, circ_mv, pe, pb 等)从 daily_basic 表获取。
|
||||
使用 SchemaCache 动态扫描数据库表结构,自动匹配字段到对应的表。
|
||||
表结构只扫描一次并缓存在内存中。
|
||||
|
||||
Args:
|
||||
dependencies: 依赖的字段集合
|
||||
@@ -84,69 +84,16 @@ class ExecutionPlanner:
|
||||
Returns:
|
||||
数据规格列表
|
||||
"""
|
||||
# 基础行情字段集合(这些字段从 pro_bar 表获取)
|
||||
pro_bar_fields = {
|
||||
"open",
|
||||
"high",
|
||||
"low",
|
||||
"close",
|
||||
"vol",
|
||||
"amount",
|
||||
"pre_close",
|
||||
"change",
|
||||
"pct_chg",
|
||||
"turnover_rate",
|
||||
"volume_ratio",
|
||||
}
|
||||
|
||||
# 每日指标字段集合(这些字段从 daily_basic 表获取)
|
||||
daily_basic_fields = {
|
||||
"turnover_rate_f",
|
||||
"pe",
|
||||
"pe_ttm",
|
||||
"pb",
|
||||
"ps",
|
||||
"ps_ttm",
|
||||
"dv_ratio",
|
||||
"dv_ttm",
|
||||
"total_share",
|
||||
"float_share",
|
||||
"free_share",
|
||||
"total_mv",
|
||||
"circ_mv",
|
||||
}
|
||||
|
||||
# 将依赖分为不同表的字段
|
||||
pro_bar_deps = dependencies & pro_bar_fields
|
||||
daily_basic_deps = dependencies & daily_basic_fields
|
||||
other_deps = dependencies - pro_bar_fields - daily_basic_fields
|
||||
# 使用 SchemaCache 自动匹配字段到表
|
||||
schema_cache = get_schema_cache()
|
||||
table_to_fields = schema_cache.match_fields_to_tables(dependencies)
|
||||
|
||||
data_specs = []
|
||||
|
||||
# pro_bar 表的数据规格
|
||||
if pro_bar_deps:
|
||||
for table_name, columns in table_to_fields.items():
|
||||
data_specs.append(
|
||||
DataSpec(
|
||||
table="pro_bar",
|
||||
columns=sorted(pro_bar_deps),
|
||||
)
|
||||
)
|
||||
|
||||
# daily_basic 表的数据规格
|
||||
if daily_basic_deps:
|
||||
data_specs.append(
|
||||
DataSpec(
|
||||
table="daily_basic",
|
||||
columns=sorted(daily_basic_deps),
|
||||
)
|
||||
)
|
||||
|
||||
# 其他字段从 daily 表获取
|
||||
if other_deps:
|
||||
data_specs.append(
|
||||
DataSpec(
|
||||
table="daily",
|
||||
columns=sorted(other_deps),
|
||||
table=table_name,
|
||||
columns=columns,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user