|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// Regression tests for the string-length offset-only optimization. |
| 19 | +// |
| 20 | +// When length() is the *only* use of a string column (or a string field inside a |
| 21 | +// struct), the FE should emit a DATA access path with an extra "offset" component so |
| 22 | +// that the BE can satisfy the query by reading only the offset array instead of the |
| 23 | +// full chars data. The EXPLAIN plan should show: |
| 24 | +// nested columns: <col>:[DATA(<col>.offset)] |
| 25 | +// |
| 26 | +// Crucially, the slot type must remain varchar (not bigint), and any predicate |
| 27 | +// using length() must be preserved as-is (e.g. "length(str_col) > 1"), never |
| 28 | +// rewritten to "CAST(str_col AS int) > 1". |
| 29 | +// |
| 30 | +// When the same string column is also read directly (e.g. projected, passed to |
| 31 | +// substr(), …) the optimization must be suppressed: no nested-columns entry for |
| 32 | +// the plain string column should appear. |
| 33 | + |
| 34 | +suite("string_length_column_pruning") { |
| 35 | + sql "set disable_nereids_rules=PRUNE_EMPTY_PARTITION" |
| 36 | + |
| 37 | + sql """ DROP TABLE IF EXISTS slcp_str_tbl """ |
| 38 | + sql """ |
| 39 | + CREATE TABLE slcp_str_tbl ( |
| 40 | + id INT, |
| 41 | + str_col STRING, |
| 42 | + c_struct STRUCT<f1: INT, f3: STRING> |
| 43 | + ) ENGINE = OLAP |
| 44 | + DUPLICATE KEY(id) |
| 45 | + DISTRIBUTED BY HASH(id) BUCKETS 1 |
| 46 | + PROPERTIES ("replication_allocation" = "tag.location.default: 1") |
| 47 | + """ |
| 48 | + |
| 49 | + // ─── Optimizable cases ────────────────────────────────────────────────────── |
| 50 | + |
| 51 | + // Plain string column: length() is the only use → offset access path emitted, |
| 52 | + // slot type stays varchar (not bigint). |
| 53 | + explain { |
| 54 | + sql "select length(str_col) from slcp_str_tbl" |
| 55 | + contains "nested columns" |
| 56 | + contains "offset" |
| 57 | + notContains "type=bigint" |
| 58 | + } |
| 59 | + |
| 60 | + // Struct string field: length(struct_element) is the only use |
| 61 | + explain { |
| 62 | + sql "select length(struct_element(c_struct, 'f3')) from slcp_str_tbl" |
| 63 | + contains "nested columns" |
| 64 | + contains "offset" |
| 65 | + notContains "type=bigint" |
| 66 | + } |
| 67 | + |
| 68 | + // length() in both SELECT and WHERE: predicate must remain length(str_col) > 1, |
| 69 | + // never be rewritten to CAST(str_col AS int) > 1. Slot type must stay varchar. |
| 70 | + explain { |
| 71 | + sql "select length(str_col) from slcp_str_tbl where length(str_col) > 1" |
| 72 | + contains "nested columns" |
| 73 | + contains "offset" |
| 74 | + contains "length(str_col" |
| 75 | + notContains "CAST(str_col" |
| 76 | + notContains "type=bigint" |
| 77 | + } |
| 78 | + |
| 79 | + // ─── Non-optimizable cases ────────────────────────────────────────────────── |
| 80 | + |
| 81 | + // str_col also projected directly → full chars data needed, offset path suppressed. |
| 82 | + // No nested-columns entry for str_col, slot type stays varchar. |
| 83 | + explain { |
| 84 | + sql "select length(str_col), str_col from slcp_str_tbl" |
| 85 | + notContains "type=bigint" |
| 86 | + notContains "CAST(str_col" |
| 87 | + } |
| 88 | + |
| 89 | + // str_col also used in substr() → full chars data needed |
| 90 | + explain { |
| 91 | + sql "select length(str_col), substr(str_col, 2) from slcp_str_tbl" |
| 92 | + notContains "type=bigint" |
| 93 | + notContains "CAST(str_col" |
| 94 | + } |
| 95 | + |
| 96 | + // Struct field also projected directly → field access is full, not offset-only |
| 97 | + // The struct's nested-columns entry still appears (partial struct pruning), |
| 98 | + // but the pruned field type must remain text (not bigint). |
| 99 | + explain { |
| 100 | + sql "select length(struct_element(c_struct, 'f3')), struct_element(c_struct, 'f3') from slcp_str_tbl" |
| 101 | + contains "nested columns" |
| 102 | + notContains "bigint" |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments