Enhanced nested column pruning

Updated at:

Overview

Enhanced nested column pruning is a performance optimization feature in MaxCompute for querying complex types.

When a table contains complex types like STRUCT, ARRAY<STRUCT>, MAP<STRING, STRUCT>, or other multi-level nested combinations, queries often access only a few subfields. By enabling enhanced nested column pruning, MaxCompute scans only the required subfields when reading data. This avoids reading unused nested fields, reducing storage I/O, lowering memory overhead, and improving query performance.

For example, if a table has a complex type column info STRUCT<a:STRING, b:BIGINT, c:DOUBLE>, and a query uses only info.b, the system reads only info.b and skips info.a and info.c.

How to enable

To enable enhanced nested column pruning, set the following parameter in your SQL session:

SET odps.sql.nested.type.enable.enhanced.cp=true;

Use cases

This feature is ideal for the following scenarios:

  • Querying tables with wide complex type columns, such as a STRUCT with dozens of fields.

  • Accessing only a subset of subfields within a complex type.

  • Working with data structures that have multiple levels of nesting, such as a STRUCT containing an ARRAY<STRUCT> or a MAP<STRING, STRUCT>.

  • Executing queries that involve complex SQL logic, such as joins, aggregation, window functions, common table expressions (CTEs), UNNEST, or LATERAL VIEW EXPLODE.

  • Improving query performance on complex types without modifying table schemas or migrating data.

Supported data types

Enhanced nested column pruning supports the following complex types and their combinations:

Type

Example

STRUCT

STRUCT<a:STRING, b:BIGINT>

ARRAY<STRUCT>

ARRAY<STRUCT<name:STRING, price:BIGINT>>

MAP<STRING, STRUCT>

MAP<STRING, STRUCT<a:STRING, b:BIGINT>>

Multi-level nested combinations

ARRAY<MAP<STRING, STRUCT<a:STRING, b:BIGINT>>>

Benefits

Enabling enhanced nested column pruning provides the following benefits:

  • Reduced data reads: Scans only the nested subfields required by the query.

  • Lower memory usage: Avoids loading unused fields into the execution plan.

  • Shorter query latency: Delivers significant performance gains for queries on wide or deeply nested columns.

  • Expanded optimization scope: Covers more complex SQL scenarios compared to the legacy version.

If a table's complex type column contains 50 fields and a query uses only two or three of them, enhanced nested column pruning can significantly reduce scanning and deserialization overhead.

Comparison with the legacy pruning capability

Legacy nested column pruning mainly applies to simple Project -> TableScan query patterns, such as directly querying s.a. Enhanced nested column pruning can analyze the complete execution plan and therefore has broader coverage.

Capability

Legacy version

Enhanced version

Simple STRUCT field pruning

Supported

Supported

Deeply nested STRUCT

Not supported

Supported

Complex combinations like ARRAY<STRUCT> and MAP<STRUCT>

Not supported

Supported

Using nested fields in a JOIN

Not supported

Supported

Using nested fields in a window function

Not supported

Supported

CTE or common subexpression reuse

Not supported

Supported

aggregation, ROLLUP, and GROUPING SETS

Not supported

Supported

UNNEST subquery

Not supported

Supported

LATERAL VIEW EXPLODE

Not supported

Supported

Pruning the source table in INSERT INTO / CTAS

Not supported

Supported

Data masking scenarios

Not supported

Supported

Examples

STRUCT field pruning

In the most basic scenario, when you query only some fields of a STRUCT, the unused fields are pruned.

-- Table definition: c3 struct<a:string, b:bigint, c:double>
-- Only c3.a is read, while fields b and c are pruned.
SELECT c3.a FROM test;
-- TableScan schema: c3:struct<a:STRING>

Multi-field pruning

When multiple fields from the same STRUCT are accessed, only the required fields are scanned.

-- Table definition: c3 struct<a:string, b:bigint, c:double>
SELECT c3.a, c3.b FROM test;
-- TableScan schema: c3:struct<a:STRING, b:BIGINT>
-- Field c is pruned.

Deeply nested STRUCT pruning

Field pruning is supported for STRUCTs nested at any depth.

-- Table definition: c3 struct<a:string, b:bigint, c:struct<c_a:string, c_b:bigint>>

-- Access a deeply nested field.
SELECT c3.c.c_a FROM test;
-- TableScan schema: c3:struct<c:struct<c_a:STRING>>

-- Access fields at different nesting levels. The schema is automatically merged.
SELECT c3.c.c_a, c3.c FROM test;
-- TableScan schema: c3:struct<c:struct<c_a:STRING, c_b:BIGINT>>

ARRAY index access pruning

When you access a specific field in an ARRAY by its index, unused STRUCT fields can be pruned.

-- Table definition: c2 array<struct<a:string, b:bigint>>,
--          c3 array<struct<a:string, b:struct<a:string, b:bigint>>>
SELECT c2[0].b, c3[0].b.b FROM test;
-- TableScan schema:
--   c2:array<struct<b:BIGINT>>
--   c3:array<struct<b:struct<b:BIGINT>>>
-- Field a is pruned.

Multi-level nested arrays are also supported:

-- Table definition: c2 array<array<struct<a:string, b:struct<a:string, b:bigint>>>>
SELECT (c2[0])[0].b.b FROM test;
-- TableScan schema: c2:array<array<struct<b:struct<b:BIGINT>>>>

MAP<STRING, STRUCT> pruning

When you access STRUCT fields within the values of a MAP by using map_values(), unused fields can be pruned.

-- Table definition: c2 map<string, struct<a:string, b:bigint>>,
--          c3 map<string, struct<a:string, b:struct<a:string, b:bigint>>>
SELECT MAP_VALUES(c2)[0].b, MAP_VALUES(c3)[0].b.b FROM test;
-- TableScan schema:
--   c2:map<STRING, struct<b:BIGINT>>
--   c3:map<STRING, struct<b:struct<b:BIGINT>>>

Pruning complex nested types

Pruning is supported for any combination of nested ARRAY, MAP, and STRUCT types.

-- array<map<string, struct<...>>>
SELECT MAP_VALUES(c2[0])[0].b.b FROM test;
-- TableScan schema: c2:array<map<STRING, struct<b:struct<b:BIGINT>>>>

-- array<struct<..., b:map<string, struct<...>>>>
SELECT MAP_VALUES(c2[0].b.b)[0].b FROM test;
-- TableScan schema: c2:array<struct<b:struct<b:map<STRING, struct<b:STRING>>>>>

-- struct<a:string, b:array<struct<...>>>
SELECT (c2.b)[0].b FROM test;
-- TableScan schema: c2:struct<b:array<struct<b:BIGINT>>>

Pruning with WHERE filter conditions

If the SELECT and WHERE clauses use different nested fields, the system reads the union of both sets of fields.

-- Table definition: c3 struct<a:string, b:bigint, c:double>
SELECT c3.a FROM test WHERE c1 > 'xx';
-- TableScan schema: c1:STRING, c3:struct<a:STRING>

-- Nested field in the WHERE clause
SELECT c3.a FROM test WHERE c3.b > 100;
-- TableScan schema: c3:struct<a:STRING, b:BIGINT>

Pruning with filter pushdown

The optimizer can push down filter conditions on nested fields to the storage layer while still benefiting from column pruning.

-- Table definition: extension struct<col1:string, col2:struct<c21:string, c22:string, c23:string>, col3:bigint>
SELECT id, extension.col1 FROM test WHERE extension.col2.c22 = '1';
-- TableScan schema: id:BIGINT, extension:struct<col1:STRING, col2:struct<c22:STRING>>
-- The filter condition is pushed down to the storage layer, and only the required nested fields are read.

JOIN pruning

In a JOIN query, the optimizer prunes nested fields separately for each table involved in the join.

SELECT t2.id, meta.m2, extension.e2
FROM t_row_policy t1 JOIN nested_src t2 ON t1.id = t2.id AND t1.extension.e3 = t2.key;
-- t_row_policy TableScan: id:BIGINT, extension:struct<e2:BIGINT, e3:STRING>, meta:struct<m2:BIGINT>

-- LEFT SEMI JOIN
SELECT meta.m2, extension.e2
FROM t_row_policy t1 LEFT SEMI JOIN nested_src t2 ON t1.id = t2.id AND t1.extension.e3 = t2.key;
-- TableScan: id:BIGINT, extension:struct<e2:BIGINT, e3:STRING>, meta:struct<m2:BIGINT>

-- MAP JOIN
SELECT /*+mapjoin(t2) */ t2.id, meta.m2, extension.e2
FROM t_row_policy t1 JOIN nested_src t2 ON t1.id = t2.id AND t1.extension.e3 = t2.key;
-- TableScan: id:BIGINT, extension:struct<e2:BIGINT, e3:STRING>, meta:struct<m2:BIGINT>

Aggregation (GROUP BY / ROLLUP / GROUPING SETS) pruning

-- GROUP BY
SELECT extension.e2, MAX(meta.m2) AS meta FROM t_row_policy GROUP BY extension.e2;
-- TableScan: extension:struct<e2:BIGINT>, meta:struct<m2:BIGINT>

-- ROLLUP
SELECT extension.e1, COUNT(1) FROM t_row_policy GROUP BY ROLLUP(extension.e1, extension.e3);
-- TableScan: extension:struct<e1:STRING, e3:STRING>

-- GROUPING SETS
SELECT extension.e1, COUNT(1) FROM t_row_policy GROUP BY GROUPING SETS (extension.e1, extension.e3);
-- TableScan: extension:struct<e1:STRING, e3:STRING>

Window function pruning

-- Basic window function
SELECT extension.e1,
       ROW_NUMBER() OVER (PARTITION BY id ORDER BY extension.e2) AS rn
FROM t_row_policy WHERE extension.e3 = 'test';
-- TableScan: id:BIGINT, extension:struct<e1:STRING, e2:BIGINT, e3:STRING>

-- Aggregation with a window function
SELECT extension.e1,
       SUM(extension.e2) OVER (PARTITION BY meta.m1) AS total
FROM t_row_policy;
-- TableScan: extension:struct<e1:STRING, e2:BIGINT>, meta:struct<m1:STRING>

-- Deeply nested fields
SELECT extension.e1,
       MAX(extension.e2.e22.e221) OVER (PARTITION BY extension.e3) AS max_val
FROM t_row_policy;
-- TableScan: extension:struct<e1:STRING, e2:struct<e22:struct<e221:STRING>>, e3:STRING>

ORDER BY / SORT BY / DISTRIBUTE BY pruning

-- ORDER BY
SELECT c1.c2.c3 FROM test ORDER BY c1.c2.c2 LIMIT 10;
-- TableScan: c1:struct<c2:struct<c2:STRING, c3:STRING>>

-- SORT BY
SELECT extension.e3 FROM t_row_policy SORT BY extension.e2;
-- TableScan: extension:struct<e2:BIGINT, e3:STRING>

-- DISTRIBUTE BY
SELECT extension.e3 FROM t_row_policy DISTRIBUTE BY extension.e2;
-- TableScan: extension:struct<e2:BIGINT, e3:STRING>

CTE (WITH clause) pruning

WITH t AS (SELECT extension FROM t_row_policy WHERE id = 1)
SELECT extension.col1 FROM t
UNION ALL
SELECT extension.col3 FROM t;
-- TableScan: id:BIGINT, extension:struct<col1:STRING, col3:STRING>

UNNEST / LATERAL VIEW pruning

-- LATERAL VIEW EXPLODE
SELECT t.extension.e1, lv.a1
FROM t_row_policy t
LATERAL VIEW EXPLODE(t.name) lv AS a1
WHERE t.extension.e2 > 10;
-- TableScan: name:array<struct<a1:STRING, a2:STRING>>, extension:struct<e1:STRING, e2:BIGINT>

-- LEFT JOIN UNNEST
SELECT extension.e2 FROM t_row_policy LEFT JOIN UNNEST(name) WHERE a1 = '12';
-- TableScan: name:array<struct<a1:STRING>>, extension:struct<e2:BIGINT>

-- UNNEST with a subquery
SELECT extension.e3 AS c1,
       (SELECT MAX(r2) FROM UNNEST(extension.e2)) AS c2,
       (SELECT MAX(r3) FROM UNNEST(extension.e2)) AS c3,
       id
FROM t_row_policy;
-- TableScan: id:BIGINT, extension:struct<e2:array<struct<r2:STRING, r3:STRING>>, e3:STRING>

-- Multi-level UNNEST
SELECT extension2.e3, rr2.r22
FROM t_row_policy
JOIN UNNEST(extension2.e2) AS r
JOIN UNNEST(r.r2) AS rr2
WHERE rr2.r21 > '1';
-- TableScan: extension2:struct<e2:array<struct<r2:array<struct<r21:STRING, r22:BIGINT>>>>, e3:STRING>

Function expression pruning

When you use a nested field as a function argument, the system reads only the required fields.

SELECT CONCAT(extension.e3, '-'), id FROM t_row_policy;
-- TableScan: id:BIGINT, extension:struct<e3:STRING>

SELECT CONCAT(SUBSTR(extension.col2.c22, 1, 2), '-'), id FROM t_row_policy;
-- TableScan: id:BIGINT, extension:struct<col2:struct<c22:STRING>>

SELECT CONCAT(extension.e1, CAST(extension.e2 AS STRING)) AS combined
FROM t_row_policy WHERE extension.e2 > 10;
-- TableScan: extension:struct<e1:STRING, e2:BIGINT>

Data masking pruning

Enhanced nested column pruning remains effective even when you configure a data masking policy on a column.

-- A masking policy is configured on the extension column.
SELECT COUNT(1) FROM t_row_policy WHERE extension.col2 > 20L;
-- TableScan schema: extension:struct<col2:BIGINT>
-- The masking policy works as expected, and only the col2 field is read.

-- Deeply nested masking
SELECT COUNT(1) FROM t_row_policy WHERE extension.col2.c22 > 20L;
-- TableScan schema: extension:struct<col2:struct<c22:BIGINT>>

Pruning limitations

In the following scenarios, the system must read the entire complex type column, so nested field pruning is not applied:

Scenario

Reason

SELECT * FROM t

The query requires all columns and fields.

SELECT extension FROM t

The query requires the entire STRUCT.

SELECT to_json(extension) FROM t

Serialization functions require the full structure.

The query references the entire complex type column.

The system cannot retain only a subset of subfields.

UNNEST outputs all elements of an array.

Full array elements are part of the query result.

Using an entire STRUCT in a GROUPING SETS clause.

The system cannot determine a prunable subset of fields.

Using SELECT * inside a window function or expression.

The optimizer considers all fields necessary.

These limitations are expected behavior. The optimizer prunes subfields only when it can definitively determine that the query does not use them.