zfr9527 commented on code in PR #60260: URL: https://github.com/apache/doris/pull/60260#discussion_r2731550321
########## regression-test/suites/nereids_function_p0/gen_function/unnest_select_list_test.groovy: ########## @@ -0,0 +1,206 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("unnest_select_list_test", "unnest") { + + String prefix_str = "unnest_select_list_" + def tb_name1 = prefix_str + "table1" + + sql """drop table if exists ${tb_name1}""" + sql """ + CREATE TABLE IF NOT EXISTS ${tb_name1} ( + id INT, + user_name VARCHAR(50), + scores ARRAY<INT>, + user_ids BITMAP, + properties MAP<STRING, STRING> + ) + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_num" = "1");""" + + sql """ + INSERT INTO ${tb_name1} VALUES + (1, 'Alice', [90, 85, 95], to_bitmap(101), {'age': '25', 'city': 'Beijing'}), + (2, 'Bob', [70, 80], bitmap_from_string('201,202'), {'level': 'VIP', 'rank': 'A'}), + (3, 'EmptyCase', [], bitmap_empty(), {}), + (4, 'NullCase', NULL, bitmap_empty(), NULL);""" + + // Test using UNNEST on an array in the SELECT list. + qt_select_unnest_array """SELECT id, user_name, UNNEST(scores) as score FROM ${tb_name1} ORDER BY id, user_name, score;""" + + // array + test { + // Test that using WITH ORDINALITY for UNNEST in the SELECT list is not supported. + sql """ + SELECT + id, + user_name, + UNNEST(scores) WITH ORDINALITY AS (score_pos, score_val) + FROM ${tb_name1};""" + exception "mismatched input" + } + + // Test UNNEST on a constant array in the SELECT list. + qt_select_unnest_constant_array """SELECT UNNEST(['a', 'b', 'c']) as col1 ORDER BY col1;""" + + test { + // Test that WITH ORDINALITY is not supported for UNNEST on a constant array in the SELECT list. + sql """SELECT UNNEST(['a', 'b', 'c']) WITH ORDINALITY AS (val, pos);""" + exception "mismatched input" + } + + // Test UNNEST on a modified array (result of a function) in the SELECT list. + qt_select_unnest_on_modified_array """ + SELECT + id, + UNNEST(ARRAY_SORT(scores)) AS sorted_score + FROM ${tb_name1} WHERE id = 1 + ORDER BY id, sorted_score;""" + + test { + // Test that nested UNNEST calls in the SELECT list are not supported. + sql """SELECT UNNEST(t.v) FROM (SELECT UNNEST(ARRAY(ARRAY(1,2))) AS v) t;""" + exception "Could not find function unnest" + } + + + // bitmap + // Test using UNNEST on a bitmap in the SELECT list. + qt_select_unnest_bitmap """ + SELECT + id, + user_name, + UNNEST(user_ids) AS exploded_id + FROM ${tb_name1} + ORDER BY id, user_name, exploded_id;""" + + // Test UNNEST on a constant bitmap in the SELECT list. + qt_select_unnest_constant_bitmap """SELECT UNNEST(bitmap_from_string('10,20,30')) AS uid ORDER BY uid;""" + + // Test aggregating the results of an UNNEST in a subquery's SELECT list. + qt_aggregate_on_select_unnest_in_subquery """ + SELECT SUM(uid) FROM ( + SELECT UNNEST(user_ids) AS uid FROM ${tb_name1} + ) t;""" + + // Test UNNEST on the result of a bitmap function in the SELECT list. + qt_select_unnest_on_bitmap_function_result """SELECT UNNEST(BITMAP_AND(user_ids, to_bitmap(201))) AS intersection FROM ${tb_name1} ORDER BY intersection;""" + // Test using UNNEST directly inside a COUNT aggregate function. + qt_count_of_unnest """SELECT COUNT(UNNEST(scores)) FROM ${tb_name1};""" + + // map + // Test using UNNEST on a map in the SELECT list. + qt_select_unnest_map """ + SELECT + id, + UNNEST(properties) AS map_entry + FROM ${tb_name1} + ORDER BY id, map_entry;""" + + // Test UNNEST on a constant map in the SELECT list. + qt_select_unnest_constant_map """SELECT UNNEST(MAP('k1', 'v1', 'k2', 'v2')) AS entry ORDER BY entry;""" + + // Test filtering the results of an UNNEST in a subquery's SELECT list. + qt_filter_on_select_unnest_in_subquery """ + SELECT * FROM ( + SELECT id, UNNEST(properties) AS entry FROM ${tb_name1} + ) t + WHERE entry LIKE '%VIP%' + ORDER BY t.id, t.entry;""" + + // null + // Test UNNEST on a NULL array column in the SELECT list. + qt_select_unnest_null_array """SELECT UNNEST(scores) as score FROM ${tb_name1} WHERE id = 4 ORDER BY score NULLS FIRST;""" + + // cross join + test { + // Test that multiple UNNEST functions with different types in the SELECT list are invalid. + sql """SELECT UNNEST(scores), UNNEST(user_ids) FROM ${tb_name1};""" + exception "multiple UNNEST functions in same place must have ARRAY argument type" + } + + // nested + test { + // Test that nested UNNEST on a constant array in the SELECT list is invalid. + sql """SELECT UNNEST(UNNEST(ARRAY(ARRAY(1,2), ARRAY(3,4))));""" + exception "Could not find function unnest" + } + + // agg + sql """SELECT COUNT(UNNEST(scores)) FROM ${tb_name1};""" + Review Comment: Convert to qt_* format -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
