-- ============================================================================
-- 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
-- ============================================================================

-- Drop if exists (in case of re-run)
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE CT_MRDS.A_PARALLEL_EXPORT_CHUNKS';
   DBMS_OUTPUT.PUT_LINE('Dropped existing A_PARALLEL_EXPORT_CHUNKS table');
EXCEPTION
   WHEN OTHERS THEN
      IF SQLCODE != -942 THEN -- ORA-00942: table or view does not exist
         RAISE;
      ELSE
         DBMS_OUTPUT.PUT_LINE('Table A_PARALLEL_EXPORT_CHUNKS does not exist - will create new');
      END IF;
END;
/

CREATE TABLE CT_MRDS.A_PARALLEL_EXPORT_CHUNKS (
    CHUNK_ID          NUMBER          NOT NULL,
    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),
    TEMPLATE_TABLE_NAME VARCHAR2(200),
    MAX_FILE_SIZE       NUMBER          DEFAULT 104857600 NOT NULL,
    JOB_CLASS           VARCHAR2(128),
    STATUS              VARCHAR2(30)    DEFAULT 'PENDING' NOT NULL,
    ERROR_MESSAGE       VARCHAR2(4000),
    EXPORT_TIMESTAMP    TIMESTAMP,
    CREATED_DATE        TIMESTAMP       DEFAULT SYSTIMESTAMP NOT NULL,
    CONSTRAINT PK_PARALLEL_EXPORT_CHUNKS PRIMARY KEY (TASK_NAME, CHUNK_ID)
);

-- Index for status-based queries (e.g., WHERE STATUS = 'FAILED' AND TASK_NAME = ?)
CREATE INDEX IX_PARALLEL_CHUNKS_STATUS_TASK ON CT_MRDS.A_PARALLEL_EXPORT_CHUNKS(STATUS, 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. PK: (TASK_NAME, CHUNK_ID) ensures session isolation for concurrent exports.';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.CHUNK_ID IS 'Chunk identifier within task (partition number) - unique per TASK_NAME, not globally';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.TASK_NAME IS 'DBMS_PARALLEL_EXECUTE task name - session isolation key, part of composite PK with CHUNK_ID';
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.TEMPLATE_TABLE_NAME IS 'Template table name for per-column date format configuration (e.g., CT_ET_TEMPLATES.TABLE_NAME)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.MAX_FILE_SIZE IS 'Maximum file size in bytes for CSV exports only (e.g., 104857600 = 100MB, 1073741824 = 1GB) - default 100MB (104857600). NOTE: Not applicable for PARQUET format (Oracle limitation)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.JOB_CLASS IS 'Oracle Scheduler job class name for resource management (e.g., ''high'', ''DEFAULT_JOB_CLASS'') - NULL uses default scheduler priority';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.STATUS IS 'Chunk processing status: PENDING (not started), PROCESSING (in progress), COMPLETED (success), FAILED (error) - allows retry of failed partitions only';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.ERROR_MESSAGE IS 'Error message if chunk processing failed (STATUS = FAILED)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.EXPORT_TIMESTAMP IS 'Timestamp when chunk export was completed (STATUS = COMPLETED)';
COMMENT ON COLUMN CT_MRDS.A_PARALLEL_EXPORT_CHUNKS.CREATED_DATE IS 'Timestamp when chunk was created';
