cswork / libraryOpen practice workspace

Coding questions · TikTok

Debug a Hive insert query

This question evaluates a candidate's ability to debug Hive INSERT/SELECT operations, focusing on schema and column alignment, data types and casts, partition handling, file formats/SerDe, ACID/transactional constraints, and syntax or reserved keyword issues.

Given a Hive table schema and an incoming table plus an INSERT/SELECT statement meant to inject data, identify why the query fails and provide step-by-step fixes. Check column alignment and names, data types and casts, partition columns and values, dynamic partition settings, file formats/SerDe, ACID/transactional requirements, and any reserved keywords or syntax errors.

Debug a failing Hive INSERT into a partitioned transactional table

You are given a transactional Hive fact table and a staging table that receives raw CSV data.

Target table (already created):

CREATE TABLE fact_orders (
    order_id         BIGINT,
    customer_id      BIGINT,
    order_timestamp  TIMESTAMP,
    total_amount     DECIMAL(10,2),
    status           STRING,
    country          STRING
)
PARTITIONED BY (order_date STRING)
STORED AS ORC
TBLPROPERTIES (
    'transactional' = 'true'
);

Incoming staging table (text-based, non-transactional):

CREATE TABLE staging_orders ( staging_order_id BIGINT, staging_customer_id BIGINT, order_ts STRING, -- e.g. '2025-05-30 10:15:00' order_total STRING, -- numeric value as text status STRING, country STRING, order_date STRING -- e.g. '2025-05-30' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

An engineer wrote the following INSERT query to load data from staging_orders into fact_orders and enable dynamic partitioning, but it fails with errors about column mismatches and types:

SET hive.exec.dynamic.partition = true; SET hive.exec.dynamic.partition.mode = nonstrict;

INSERT INTO TABLE fact_orders PARTITION (order_date)
SELECT
  staging_order_id,
  staging_customer_id,
  order_ts,
  order_total,
  country,
  status,
  order_date
FROM staging_orders;

Your tasks:

  1. Identify the issues in the failing INSERT, including column order and alignment with the partitioned table, data types and necessary casts, and any potential reserved keyword or syntax problems.
  2. Consider that fact_orders is an ORC, transactional table and staging_orders is a text/SerDe table. Ensure your solution remains compatible with these settings and with dynamic partitions.
  3. Write a corrected, working Hive INSERT ... SELECT statement (including any necessary SET statements) that successfully loads all rows from staging_orders into fact_orders, with correct column mapping, data types, and dynamic partitioning on order_date.

Assume there are no existing rows in fact_orders before your INSERT is executed. For the PostgreSQL validation harness, express the corrected load as an INSERT ... SELECT ... RETURNING equivalent over the provided tables.

Table fact_orders

columntypeconstraints
order_idBIGINT
customer_idBIGINT
order_timestampTIMESTAMP
total_amountDECIMAL(10,2)
statusTEXT
countryTEXT
order_dateTEXT

Table staging_orders

columntypeconstraints
staging_order_idBIGINT
staging_customer_idBIGINT
order_tsTEXT
order_totalTEXT
statusTEXT
countryTEXT
order_dateTEXT

Sample data:

countryorder_dateorder_totalorder_tsstaging_customer_idstaging_order_idstatus
US2025-05-3059.992025-05-30 10:15:001101PAID
CA2025-05-31120.502025-05-31 09:00:002102PENDING
US2025-06-0115.002025-06-01 12:30:001103CANCELLED

Expected output

Contents of fact_orders after executing the corrected INSERT ... SELECT once, starting from an empty fact_orders table. The rows are partitioned by order_date, but shown here as a single logical result set.

countrycustomer_idorder_dateorder_idorder_timestampstatustotal_amount
US12025-05-301012025-05-30 10:15:00PAID59.99
CA22025-05-311022025-05-31 09:00:00PENDING120.5
US12025-06-011032025-06-01 12:30:00CANCELLED15

Hints

  1. In Hive, the SELECT list in an INSERT must match the target table: first all non-partition columns in order, then the partition columns, in the same order as in PARTITION(...).
  2. You must CAST string-based timestamp and numeric fields in staging_orders to TIMESTAMP and DECIMAL(10,2) to match fact_orders, and enable dynamic partitions with the appropriate SET parameters.