-- ============================================================================
-- Table: A_PARALLEL_EXPORT_CHUNKS
-- ============================================================================
-- Purpose: Permanent table for storing partition export chunks for DBMS_PARALLEL_EXECUTE
--          CRITICAL: Must be permanent (not global temporary) because DBMS_PARALLEL_EXECUTE
--          runs callbacks in separate sessions that cannot access GTT data from parent session
-- Schema: CT_MRDS
-- Author: Grzegorz Michalski
-- Created: 2025-12-20
-- Modified: 2025-12-21 - Changed from GTT to permanent table
-- Related: MARS-835-PREHOOK
-- ============================================================================

CREATE TABLE CT_MRDS.A_PARALLEL_EXPORT_CHUNKS (
    CHUNK_ID          NUMBER          PRIMARY KEY,
    TASK_NAME         VARCHAR2(100)   NOT NULL,
    YEAR_VALUE        VARCHAR2(4)     NOT NULL,
    MONTH_VALUE       VARCHAR2(2)     NOT NULL,
    SCHEMA_NAME       VARCHAR2(128)   NOT NULL,
    TABLE_NAME        VARCHAR2(128)   NOT NULL,
    KEY_COLUMN_NAME   VARCHAR2(128)   NOT NULL,
    BUCKET_URI        VARCHAR2(4000)  NOT NULL,
    FOLDER_NAME       VARCHAR2(1000)  NOT NULL,
    PROCESSED_COLUMNS VARCHAR2(32767),
    MIN_DATE          DATE            NOT NULL,
    MAX_DATE          DATE            NOT NULL,
    CREDENTIAL_NAME   VARCHAR2(200)   NOT NULL,
    FORMAT_TYPE       VARCHAR2(20)    NOT NULL,
    FILE_BASE_NAME    VARCHAR2(1000),
    CREATED_DATE      TIMESTAMP       DEFAULT SYSTIMESTAMP NOT NULL
);

CREATE INDEX IX_PARALLEL_CHUNKS_TASK ON CT_MRDS.A_PARALLEL_EXPORT_CHUNKS(TASK_NAME);

COMMENT ON TABLE CT_MRDS.A_PARALLEL_EXPORT_CHUNKS IS 'Permanent table for parallel export chunk processing (DBMS_PARALLEL_EXECUTE) - permanent because GTT data not visible in parallel callback sessions';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.CHUNK_ID IS 'Unique chunk identifier (partition number)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.TASK_NAME IS 'DBMS_PARALLEL_EXECUTE task name for cleanup';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.YEAR_VALUE IS 'Partition year (YYYY)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.MONTH_VALUE IS 'Partition month (MM)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.SCHEMA_NAME IS 'Schema owning the source table';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.TABLE_NAME IS 'Source table name for export';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.KEY_COLUMN_NAME IS 'Key column for load history join';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.BUCKET_URI IS 'OCI bucket URI for export destination';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.FOLDER_NAME IS 'Folder name within bucket';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.PROCESSED_COLUMNS IS 'Comma-separated list of columns to export';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.MIN_DATE IS 'Minimum date filter for partition';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.MAX_DATE IS 'Maximum date filter for partition';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.CREDENTIAL_NAME IS 'OCI credential name for authentication';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.FORMAT_TYPE IS 'Export format: PARQUET or CSV';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.FILE_BASE_NAME IS 'Base filename for CSV exports (NULL for Parquet)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.CREATED_DATE IS 'Timestamp when chunk was created';
