|
| 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 | +####### |
| 19 | +# Setup test data table |
| 20 | +####### |
| 21 | +statement ok |
| 22 | +CREATE EXTERNAL TABLE multiple_ordered_table ( |
| 23 | + a0 INTEGER, |
| 24 | + a INTEGER, |
| 25 | + b INTEGER, |
| 26 | + c INTEGER, |
| 27 | + d INTEGER |
| 28 | +) |
| 29 | +STORED AS CSV |
| 30 | +WITH HEADER ROW |
| 31 | +WITH ORDER (a ASC, b ASC) |
| 32 | +WITH ORDER (c ASC) |
| 33 | +LOCATION '../../datafusion/core/tests/data/window_2.csv'; |
| 34 | + |
| 35 | + |
| 36 | +query TT |
| 37 | +EXPLAIN SELECT a, ARRAY_AGG(c ORDER BY c)[1] as result |
| 38 | + FROM multiple_ordered_table |
| 39 | + GROUP BY a; |
| 40 | +---- |
| 41 | +logical_plan |
| 42 | +Projection: multiple_ordered_table.a, NTH_VALUE(multiple_ordered_table.c,Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST] AS result |
| 43 | +--Aggregate: groupBy=[[multiple_ordered_table.a]], aggr=[[NTH_VALUE(multiple_ordered_table.c, Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]]] |
| 44 | +----TableScan: multiple_ordered_table projection=[a, c] |
| 45 | +physical_plan |
| 46 | +ProjectionExec: expr=[a@0 as a, NTH_VALUE(multiple_ordered_table.c,Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]@1 as result] |
| 47 | +--AggregateExec: mode=FinalPartitioned, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1))], ordering_mode=Sorted |
| 48 | +----SortExec: expr=[a@0 ASC NULLS LAST] |
| 49 | +------CoalesceBatchesExec: target_batch_size=8192 |
| 50 | +--------RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4 |
| 51 | +----------AggregateExec: mode=Partial, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1))], ordering_mode=Sorted |
| 52 | +------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 |
| 53 | +--------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, c], output_orderings=[[a@0 ASC NULLS LAST], [c@1 ASC NULLS LAST]], has_header=true |
| 54 | + |
| 55 | + |
| 56 | +query TT |
| 57 | +EXPLAIN SELECT a, NTH_VALUE(c, 1 ORDER BY c) as result |
| 58 | + FROM multiple_ordered_table |
| 59 | + GROUP BY a; |
| 60 | +---- |
| 61 | +logical_plan |
| 62 | +Projection: multiple_ordered_table.a, NTH_VALUE(multiple_ordered_table.c,Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST] AS result |
| 63 | +--Aggregate: groupBy=[[multiple_ordered_table.a]], aggr=[[NTH_VALUE(multiple_ordered_table.c, Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]]] |
| 64 | +----TableScan: multiple_ordered_table projection=[a, c] |
| 65 | +physical_plan |
| 66 | +ProjectionExec: expr=[a@0 as a, NTH_VALUE(multiple_ordered_table.c,Int64(1)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]@1 as result] |
| 67 | +--AggregateExec: mode=FinalPartitioned, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1))], ordering_mode=Sorted |
| 68 | +----SortExec: expr=[a@0 ASC NULLS LAST] |
| 69 | +------CoalesceBatchesExec: target_batch_size=8192 |
| 70 | +--------RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4 |
| 71 | +----------AggregateExec: mode=Partial, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1))], ordering_mode=Sorted |
| 72 | +------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 |
| 73 | +--------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, c], output_orderings=[[a@0 ASC NULLS LAST], [c@1 ASC NULLS LAST]], has_header=true |
| 74 | + |
| 75 | +query TT |
| 76 | +EXPLAIN SELECT a, ARRAY_AGG(c ORDER BY c)[1 + 100] as result |
| 77 | + FROM multiple_ordered_table |
| 78 | + GROUP BY a; |
| 79 | +---- |
| 80 | +logical_plan |
| 81 | +Projection: multiple_ordered_table.a, NTH_VALUE(multiple_ordered_table.c,Int64(1) + Int64(100)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST] AS result |
| 82 | +--Aggregate: groupBy=[[multiple_ordered_table.a]], aggr=[[NTH_VALUE(multiple_ordered_table.c, Int64(101)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST] AS NTH_VALUE(multiple_ordered_table.c,Int64(1) + Int64(100)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]]] |
| 83 | +----TableScan: multiple_ordered_table projection=[a, c] |
| 84 | +physical_plan |
| 85 | +ProjectionExec: expr=[a@0 as a, NTH_VALUE(multiple_ordered_table.c,Int64(1) + Int64(100)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]@1 as result] |
| 86 | +--AggregateExec: mode=FinalPartitioned, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1) + Int64(100)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]], ordering_mode=Sorted |
| 87 | +----SortExec: expr=[a@0 ASC NULLS LAST] |
| 88 | +------CoalesceBatchesExec: target_batch_size=8192 |
| 89 | +--------RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4 |
| 90 | +----------AggregateExec: mode=Partial, gby=[a@0 as a], aggr=[NTH_VALUE(multiple_ordered_table.c,Int64(1) + Int64(100)) ORDER BY [multiple_ordered_table.c ASC NULLS LAST]], ordering_mode=Sorted |
| 91 | +------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 |
| 92 | +--------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, c], output_orderings=[[a@0 ASC NULLS LAST], [c@1 ASC NULLS LAST]], has_header=true |
| 93 | + |
| 94 | +query II |
| 95 | +SELECT a, ARRAY_AGG(c ORDER BY c)[1] as result |
| 96 | + FROM multiple_ordered_table |
| 97 | + GROUP BY a; |
| 98 | +---- |
| 99 | +0 0 |
| 100 | +1 50 |
0 commit comments