26Y27e1

QK·2026년 7월 27일

이 에러는 step2_pipeline.py 상단에서 첫 번째 파일(first_file = raw_files[0])의 스키마만 기준으로 컬럼 목록을 단 1회 고정(Global) 정의했기 때문에 발생합니다.

  • first_file에는 mem_request 컬럼이 존재했기 때문에 select_fields에 추가되고 missing_fields에서는 제외되었습니다.
  • 그러나 루프를 돌며 특정 일자/시간의 파일(day_pattern)을 pl.scan_parquet(day_pattern).select(select_fields)로 읽을 때, 해당 시점 파일에 mem_request 메트릭이 수집되지 않아 Polars가 ColumnNotFoundError를 발생시킨 것입니다.

🛠️ 해결 방법

일자별 루프(for date_chunk in target_dates:) 내부에서 각 일자 파티션의 실제 스키마를 동적으로 확인하여 누락 컬럼을 보정하도록 수정해주시면 완치됩니다.

step2_pipeline.py 수정 코드

1. 루프 외부 (상단 전역 스키마 추출 로직 제거 및 필드 목록 정의)

기존 first_file = raw_files[0]schema_cols = ... 관련 구문을 지우고 아래와 같이 필드명 명세만 남깁니다.

# [수정 전] first_file 기반 전역 스키마 추출 구문 제거[cite: 2]
# [수정 후] 필수 파싱 대상 필드 명세만 정적으로 선언
base_fields = ["timestamp", "namespace", "node", "pod", "container"]
metric_fields = [
    "cpu_request", "cpu_limit", "cpu_usage", "cpu_throttled", 
    "mem_request", "mem_limit", "mem_usage", "mem_rss", 
    "oom_event", "pv_capacity", "pv_used"
]

2. 루프 내부 (for date_chunk in target_dates: 로직 보완)

day_pattern 스캔 직전에 해당 파일들의 스키마를 동적으로 추출하여 selectmissing_fields를 계산하도록 변경합니다.

    for date_chunk in target_dates:
        day_pattern = str(RAW_DIR / f"prom_raw_{date_chunk}_*.parquet")
        day_files = list(Path(RAW_DIR).glob(f"prom_raw_{date_chunk}_*.parquet"))
        
        if not day_files:
            print(f"\n   ⚠️  [스킵] {date_chunk} 일자의 로컬 Parquet 원부가 없어 다음 날짜로 롤오버합니다.")
            continue
            
        print(f"\n📅 [일자 격리 가동] {date_chunk} 파티션 대용량 연산 스트리밍 로드 시작...")
        
        # 💡 [동적 스키마 추출] 해당 일자 파일들에 실재하는 컬럼 체크
        try:
            day_schema_cols = pl.scan_parquet(day_pattern).collect_schema().names()
        except AttributeError:
            # Polars 구버전 호환용 (0.20 이하)
            day_schema_cols = pl.scan_parquet(day_pattern).schema.keys()

        # 실재하는 컬럼과 누락된 컬럼을 일자별로 동적 계산
        select_fields = [c for c in base_fields + metric_fields if c in day_schema_cols]
        missing_fields = [c for c in metric_fields if c not in day_schema_cols]

        # 🛡️ [OOM 방어]: 존재하는 컬럼만 먼저 프로젝션
        lf_day = pl.scan_parquet(day_pattern).select(select_fields)
        
        # 원본 파일에 누락된 메트릭 열이 있다면 0.0 리터럴로 가상화 보정[cite: 2]
        if missing_fields:
            lf_day = lf_day.with_columns([pl.lit(0.0).alias(c) for c in missing_fields])
            
        # 기존 Categorical 변환 및 파이프라인 계속 진행...

💡 요약

이렇게 수정하면 특정 시간대 파티션에 mem_requestpv_capacity 같은 메트릭이 수집되지 않고 누락되어 있더라도, 해당 일자 파티션 스캔 시 자동으로 감지하여 0.0으로 가상화 컬럼을 채워넣으므로 에러 없이 안정적으로 전체 구간 정산 조회가 진행됩니다.

profile
engineer

0개의 댓글