Workload
Impala UDFs & procedural utilities to BigQuery
Re-home reusable logic—from Hive/Impala UDFs and script-driven “macro” utilities to BigQuery UDFs and procedures—so behavior stays stable under reruns, backfills, and changing upstream schemas.
Quick answer
Re-home reusable logic—from Hive/Impala UDFs and script-driven “macro” utilities to BigQuery UDFs and procedures—so behavior stays stable under reruns, backfills, and changing upstream schemas.
Back to pair pageContext
Why this breaks
Impala environments rarely have “stored procedures” in the Teradata/Oracle sense, but they do have procedural behavior: Hive/Impala UDFs (Java/Scala/Python), script-driven SQL chains, and macro-like utilities embedded in Oozie/Airflow jobs. These assets often encode business rules, typing assumptions, and operational side effects (control tables, audit logs). When migrated naïvely, BigQuery compiles the SQL but the _logic_ drifts because UDF semantics, typing, and error handling differ. Common symptoms after migration:
- UDF outputs drift due to type coercion and NULL handling differences - Regex/string behavior changes (dialect differences) and breaks downstream expectations - Time conversion logic drifts (epoch handling, timezone assumptions) - Script-driven dynamic SQL behaves differently under parameter substitution - Side effects (control/audit tables) aren’t modeled, so reruns/backfills double-apply or skip A successful migration turns these scattered utilities into explicit BigQuery routines with a behavior contract and a replayable test harness.
Approach
How conversion works
- Inventory & classify reusable logic: Hive/Impala UDFs, script-driven SQL utilities, and call sites across ETL/BI. - Extract the behavior contract: inputs/outputs, typing, null rules, error semantics, side effects, and performance constraints. - Choose BigQuery target form per asset:
- BigQuery SQL UDF for pure expressions - BigQuery JavaScript UDF for complex logic or regex-heavy transforms - BigQuery stored procedure (SQL scripting) for multi-statement control flow and dynamic SQL - Set-based refactor where procedural loops exist in orchestration scripts - Translate and normalize: explicit casts, null-safe comparisons, timezone intent, and deterministic ordering where logic depends on ranking/dedup. - Validate with a harness: golden inputs/outputs, branch coverage, failure-mode tests, and side-effect assertions—then integrate into representative pipelines.
Coverage
Supported constructs
Representative Impala/Hive procedural constructs we commonly migrate to BigQuery routines (exact coverage depends on your estate).
| Source | Target | Notes |
|---|---|---|
| Hive/Impala UDFs (Java/Scala/Python) | BigQuery SQL UDFs / JavaScript UDFs | Choose target form based on complexity and semantics; validate edge cases. |
| Regex-heavy string transforms | BigQuery REGEXP_* functions or JS UDFs | Regex dialect differences validated with golden cohorts. |
| Script-driven macro utilities | Reusable views/UDFs/procedures | Consolidate reusable patterns into testable BigQuery assets. |
| Dynamic SQL via templating | EXECUTE IMMEDIATE with parameter binding | Normalize identifier rules; reduce drift and injection risk. |
| Control tables for restartability | Applied-window tracking + idempotency markers | Reruns/backfills become safe and auditable. |
| Epoch/time conversion helpers | TIMESTAMP_SECONDS/MILLIS and explicit timezone handling | Prevents boundary-day drift in reporting. |
Compare
How workload changes
| Topic | Impala / Hadoop | BigQuery | Notes |
|---|---|---|---|
| Where logic lives | UDF JARs + script-driven SQL utilities in orchestration | Centralized routines (UDFs/procedures) with explicit contracts | Migration consolidates and stabilizes reusable logic. |
| Typing and coercion | Hive/Impala implicit casts often tolerated | Explicit casts recommended for stable outputs | Validation focuses on mixed-type branches and join keys. |
| Operational behavior | Reruns/retries often encoded in job scripts | Idempotency and side effects must be explicit | Harness proves behavior under reruns/backfills. |
Examples
Examples
Illustrative patterns for moving Impala-era UDF and macro utilities into BigQuery routines. Adjust datasets and types to match your environment.
-- BigQuery SQL UDF example
CREATE OR REPLACE FUNCTION `proj.util.safe_div`(n NUMERIC, d NUMERIC) AS (
IF(d IS NULL OR d = 0, NULL, n / d)
); -- BigQuery JavaScript UDF example (regex/complex logic)
CREATE OR REPLACE FUNCTION `proj.util.normalize_code`(x STRING)
RETURNS STRING
LANGUAGE js AS """
if (x === null) return null;
return x.trim().toUpperCase();
"""; -- BigQuery stored procedure with dynamic SQL + parameter binding
CREATE OR REPLACE PROCEDURE `proj.util.refresh_window`(start_d DATE, end_d DATE)
BEGIN
DECLARE sql STRING;
SET sql = '''
DELETE FROM `proj.mart.fact_orders`
WHERE event_date BETWEEN @s AND @e;
INSERT INTO `proj.mart.fact_orders`
SELECT * FROM `proj.stg.fact_orders`
WHERE event_date BETWEEN @s AND @e;
''';
EXECUTE IMMEDIATE sql USING start_d AS s, end_d AS e;
END; -- Minimal harness pattern: compare routine outputs to golden expectations
CREATE TEMP TABLE golden AS
SELECT 10 AS n, 2 AS d, 5 AS expected UNION ALL
SELECT 10, 0, NULL;
SELECT
n, d,
`proj.util.safe_div`(n, d) AS got,
expected,
IF(`proj.util.safe_div`(n, d) IS NOT DISTINCT FROM expected, 'PASS', 'FAIL') AS verdict
FROM golden; Workload Assessment
Migrate UDFs and utilities with a test harness
We inventory your Impala UDFs and macro utilities, migrate a representative subset into BigQuery routines, and deliver a harness that proves parity—including side effects and rerun behavior.
Book assessmentAvoid
Common pitfalls
- Hidden dependencies: UDFs rely on external JARs/configs or implicit Hive settings not captured in migration.
- Mixed-type branches: CASE/IF returns mixed types; BigQuery requires explicit casts to preserve intent.
- NULL semantics drift: comparisons and string functions behave differently unless made explicit.
- Regex dialect differences: pattern syntax and escaping change outputs for edge inputs.
- Dynamic SQL via templating: string substitution behaves differently; identifier quoting breaks.
- Side effects ignored: control-table/audit updates not recreated; reruns/backfills become unsafe.
- Row-by-row script logic: loops in shell/Oozie scripts should become set-based SQL or bounded windows.
Proof
Validation approach
- Compile + interface checks: each routine deploys; signatures match the contract (args/return types).
- Golden tests: curated input sets validate outputs, including NULL-heavy and boundary cases.
- Branch + failure-mode coverage: expected failures are tested (invalid inputs, missing rows).
- Side-effect verification: assert expected writes to control/log/audit tables and idempotency under reruns.
- Integration replay: run routines inside representative pipelines and compare downstream KPIs/aggregates.
- Performance gate: confirm no hidden row-by-row scans; refactors validated with bytes scanned/runtime baselines.
Execution
Migration steps
A sequence that keeps behavior explicit, testable, and safe to cut over.
-
01
Inventory reusable logic and call sites
Collect Hive/Impala UDFs, script-driven utilities, and their call sites across pipelines and BI queries. Identify dependencies and side effects (control/audit tables).
-
02
Define the behavior contract
Specify inputs/outputs, typing, NULL rules, regex expectations, error semantics, side effects, and performance constraints. Decide target form (SQL UDF, JS UDF, procedure, or refactor).
-
03
Convert with safety patterns
Make casts explicit, normalize timezone intent, implement null-safe comparisons, and migrate dynamic SQL using EXECUTE IMMEDIATE with bindings and explicit identifier rules.
-
04
Build a replayable harness
Create golden input sets, boundary cases, and expected failures. Validate outputs and side effects deterministically so parity isn’t debated at cutover.
-
05
Integrate and cut over behind gates
Run routines in representative pipelines, compare downstream KPIs, validate reruns/backfills, and cut over with rollback-ready criteria.
FAQ
Frequently asked questions
Impala doesn’t really have stored procedures—what are we migrating? +
Usually UDFs (often Java/Scala/Python) and script-driven procedural behavior embedded in orchestration. We migrate that logic into BigQuery UDFs/procedures with explicit contracts and a validation harness.
Do we have to rewrite all UDFs in JavaScript? +
No. Many can become BigQuery SQL UDFs. We use JS UDFs only when logic is complex or regex/object handling requires it. The target form is chosen per asset to reduce risk and cost.
How do you prove parity for UDFs and utilities? +
We build a replayable harness with golden inputs/outputs, branch and failure-mode coverage, and side-effect assertions. Integration replay validates downstream KPIs before cutover.
What’s the biggest drift risk? +
Implicit typing/NULL behavior and regex dialect differences, plus side effects used for restartability. We make these explicit and validate under reruns/backfills so behavior remains stable.
Migration Acceleration
Cut over utilities with proof-backed sign-off
Get a conversion plan, review markers for ambiguous intent, and validation artifacts so procedural logic cutover is gated by evidence and rollback-ready criteria.
Next reads
Related pages
- Read more
End-to-end approach: what breaks, validation gates, and cutover plan.
- Read more
Migrate Impala pipelines to BigQuery with explicit late-data and restartability semantics.
- Read more
Convert Impala SQL to BigQuery with pruning-safe rewrites and golden-query validation.
- Read more
How we define parity contracts, thresholds, and evidence for sign-off.