|
| 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 | +# Tests for duplicate column names/aliases in projections. |
| 19 | +# DataFusion auto-suffixes duplicates with :{count} (e.g. cov, cov:1). |
| 20 | + |
| 21 | +# Setup |
| 22 | +statement ok |
| 23 | +CREATE TABLE t(x INT, y INT) AS VALUES (1, 2), (3, 4); |
| 24 | + |
| 25 | +# |
| 26 | +# Basic duplicate alias |
| 27 | +# |
| 28 | +query II |
| 29 | +SELECT x AS c1, y AS c1 FROM t; |
| 30 | +---- |
| 31 | +1 2 |
| 32 | +3 4 |
| 33 | + |
| 34 | +# |
| 35 | +# Duplicate literal expressions |
| 36 | +# |
| 37 | +query II |
| 38 | +SELECT 1, 1; |
| 39 | +---- |
| 40 | +1 1 |
| 41 | + |
| 42 | +# |
| 43 | +# Same column selected twice |
| 44 | +# |
| 45 | +query II |
| 46 | +SELECT x, x FROM t; |
| 47 | +---- |
| 48 | +1 1 |
| 49 | +3 3 |
| 50 | + |
| 51 | +# |
| 52 | +# Subquery with duplicate column names |
| 53 | +# |
| 54 | +query II |
| 55 | +SELECT * FROM (SELECT x AS c1, y AS c1 FROM t); |
| 56 | +---- |
| 57 | +1 2 |
| 58 | +3 4 |
| 59 | + |
| 60 | +# |
| 61 | +# ORDER BY referencing a duplicated alias resolves to first occurrence |
| 62 | +# |
| 63 | +query II |
| 64 | +SELECT x AS c1, y AS c1 FROM t ORDER BY c1; |
| 65 | +---- |
| 66 | +1 2 |
| 67 | +3 4 |
| 68 | + |
| 69 | +# |
| 70 | +# CTE join producing duplicate column names (TPC-DS Q39 pattern) |
| 71 | +# |
| 72 | +statement ok |
| 73 | +CREATE TABLE inv(warehouse_sk INT, item_sk INT, moy INT, cov DOUBLE) AS VALUES |
| 74 | + (1, 10, 1, 1.5), |
| 75 | + (1, 10, 2, 2.0), |
| 76 | + (2, 20, 1, 0.8), |
| 77 | + (2, 20, 2, 1.2); |
| 78 | + |
| 79 | +query IIIRIIIR |
| 80 | +WITH inv1 AS ( |
| 81 | + SELECT warehouse_sk, item_sk, moy, cov FROM inv WHERE moy = 1 |
| 82 | +), |
| 83 | +inv2 AS ( |
| 84 | + SELECT warehouse_sk, item_sk, moy, cov FROM inv WHERE moy = 2 |
| 85 | +) |
| 86 | +SELECT inv1.warehouse_sk, inv1.item_sk, inv1.moy, inv1.cov, |
| 87 | + inv2.warehouse_sk, inv2.item_sk, inv2.moy, inv2.cov |
| 88 | +FROM inv1 JOIN inv2 |
| 89 | + ON inv1.item_sk = inv2.item_sk AND inv1.warehouse_sk = inv2.warehouse_sk |
| 90 | +ORDER BY inv1.warehouse_sk, inv1.item_sk; |
| 91 | +---- |
| 92 | +1 10 1 1.5 1 10 2 2 |
| 93 | +2 20 1 0.8 2 20 2 1.2 |
| 94 | + |
| 95 | +# |
| 96 | +# Three-way duplicate |
| 97 | +# |
| 98 | +query III |
| 99 | +SELECT x AS a, y AS a, x + y AS a FROM t; |
| 100 | +---- |
| 101 | +1 2 3 |
| 102 | +3 4 7 |
| 103 | + |
| 104 | +# |
| 105 | +# CAST produces same schema_name as original column (TPC-DS Q39 pattern). |
| 106 | +# CAST is transparent to schema_name, so CAST(x AS DOUBLE) and x |
| 107 | +# both have schema_name "x" — this must be deduped. |
| 108 | +# |
| 109 | +query RI |
| 110 | +SELECT CAST(x AS DOUBLE), x FROM t; |
| 111 | +---- |
| 112 | +1 1 |
| 113 | +3 3 |
| 114 | + |
| 115 | +# |
| 116 | +# GROUP BY with duplicate expressions in SELECT |
| 117 | +# |
| 118 | +query II |
| 119 | +SELECT x, x FROM t GROUP BY x; |
| 120 | +---- |
| 121 | +1 1 |
| 122 | +3 3 |
| 123 | + |
| 124 | +# |
| 125 | +# Aggregate with GROUP BY producing duplicate column names |
| 126 | +# |
| 127 | +query III |
| 128 | +SELECT x, SUM(y) AS total, SUM(y) AS total FROM t GROUP BY x ORDER BY x; |
| 129 | +---- |
| 130 | +1 2 2 |
| 131 | +3 4 4 |
| 132 | + |
| 133 | +# |
| 134 | +# ORDER BY referencing the second (renamed) column by position |
| 135 | +# |
| 136 | +query II |
| 137 | +SELECT y AS c1, x AS c1 FROM t ORDER BY 2; |
| 138 | +---- |
| 139 | +2 1 |
| 140 | +4 3 |
| 141 | + |
| 142 | +# |
| 143 | +# Function calls that produce the same schema_name after argument |
| 144 | +# normalization (reported in issue #6543 for iszero). |
| 145 | +# |
| 146 | +query BB |
| 147 | +SELECT iszero(0.0), iszero(-0.0); |
| 148 | +---- |
| 149 | +true true |
| 150 | + |
| 151 | +# |
| 152 | +# Duplicate expressions inside a UNION subquery |
| 153 | +# |
| 154 | +query II |
| 155 | +SELECT * FROM (SELECT x AS a, y AS a FROM t UNION ALL SELECT y AS a, x AS a FROM t) ORDER BY 1, 2; |
| 156 | +---- |
| 157 | +1 2 |
| 158 | +2 1 |
| 159 | +3 4 |
| 160 | +4 3 |
| 161 | + |
| 162 | +# |
| 163 | +# Known limitation: wildcard expansion happens after dedup, so |
| 164 | +# SELECT *, col FROM t still errors when col overlaps with *. |
| 165 | +# This will be addressed in a follow-up PR. |
| 166 | +# |
| 167 | +query error DataFusion error: Error during planning: Projections require unique expression names but the expression "t\.x" at position 0 and "t\.x" at position 2 have the same name\. Consider aliasing \("AS"\) one of them\. |
| 168 | +SELECT *, x FROM t; |
| 169 | + |
| 170 | +# Cleanup |
| 171 | +statement ok |
| 172 | +DROP TABLE t; |
| 173 | + |
| 174 | +statement ok |
| 175 | +DROP TABLE inv; |
0 commit comments