|
@@ -164,7 +164,20 @@ def resolve_calculation_formula(formula, calculation_fields, visited=None):
|
|
|
return re.sub(r"\[([^\[\]]+)\]", replace_calculation_field, formula)
|
|
return re.sub(r"\[([^\[\]]+)\]", replace_calculation_field, formula)
|
|
|
|
|
|
|
|
def build_with_part(new_date_fields, new_dimension_fields, dataset_fid_name_map, added_fields_info, dataset_id):
|
|
def build_with_part(new_date_fields, new_dimension_fields, dataset_fid_name_map, added_fields_info, dataset_id):
|
|
|
- sql_part = 'WITH tmp as (\nSELECT *,\n'
|
|
|
|
|
|
|
+ override_field_names = set()
|
|
|
|
|
+ for _, new_name in new_date_fields:
|
|
|
|
|
+ override_field_names.add(new_name)
|
|
|
|
|
+ for fid, _ in new_dimension_fields:
|
|
|
|
|
+ override_field_names.add(added_fields_info[fid]["field_name"])
|
|
|
|
|
+
|
|
|
|
|
+ base_columns = []
|
|
|
|
|
+ seen_columns = set()
|
|
|
|
|
+ for field_name in dataset_fid_name_map.values():
|
|
|
|
|
+ if field_name in override_field_names or field_name in seen_columns:
|
|
|
|
|
+ continue
|
|
|
|
|
+ seen_columns.add(field_name)
|
|
|
|
|
+ base_columns.append(quote_identifier(field_name))
|
|
|
|
|
+
|
|
|
with_expressions = []
|
|
with_expressions = []
|
|
|
for fid, new_name in new_date_fields:
|
|
for fid, new_name in new_date_fields:
|
|
|
old_fid, partial_date = fid.split('_')
|
|
old_fid, partial_date = fid.split('_')
|
|
@@ -193,8 +206,10 @@ def build_with_part(new_date_fields, new_dimension_fields, dataset_fid_name_map,
|
|
|
formula = resolve_calculation_formula(formula, added_fields_info, {fid})
|
|
formula = resolve_calculation_formula(formula, added_fields_info, {fid})
|
|
|
tmp_part = quote_identifier(formula, formula=True) + f" AS `{new_name}`"
|
|
tmp_part = quote_identifier(formula, formula=True) + f" AS `{new_name}`"
|
|
|
with_expressions.append(tmp_part)
|
|
with_expressions.append(tmp_part)
|
|
|
- sql_part += ',\n'.join(with_expressions)
|
|
|
|
|
- sql_part += f"\nFROM `{dataset_id}\n`"
|
|
|
|
|
|
|
+
|
|
|
|
|
+ select_parts = base_columns + with_expressions
|
|
|
|
|
+ sql_part = "WITH tmp as (\nSELECT " + ",\n".join(select_parts)
|
|
|
|
|
+ sql_part += f"\nFROM {quote_identifier(str(dataset_id))}\n)"
|
|
|
return sql_part
|
|
return sql_part
|
|
|
|
|
|
|
|
# 处理计算字段
|
|
# 处理计算字段
|
|
@@ -603,6 +618,8 @@ def build_sql_query(card_data, added_fields_info, dataset_fid_name_map):
|
|
|
select_parts.append(f"{agg_expression} AS {quote_identifier(alias)}")
|
|
select_parts.append(f"{agg_expression} AS {quote_identifier(alias)}")
|
|
|
selected_fid_alias_map[fid] = alias
|
|
selected_fid_alias_map[fid] = alias
|
|
|
|
|
|
|
|
|
|
+ # BI 卡片配置里可能存在重复字段。这里只对完全相同的 SELECT 表达式去重,保留表达式不同但别名相同的情况。
|
|
|
|
|
+ select_parts = dedupe_sql_parts(select_parts)
|
|
|
if not select_parts:
|
|
if not select_parts:
|
|
|
print(f"错误: {card_id} {card_name} 没有select字段。")
|
|
print(f"错误: {card_id} {card_name} 没有select字段。")
|
|
|
return '', '', '', ''
|
|
return '', '', '', ''
|