|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.analysis |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.analysis.TestRelations.testRelation2 |
| 21 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 22 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 23 | +import org.apache.spark.sql.catalyst.expressions.Literal |
| 24 | +import org.apache.spark.sql.catalyst.SimpleCatalystConf |
| 25 | + |
| 26 | +class UnresolvedOrdinalSubstitutionSuite extends AnalysisTest { |
| 27 | + |
| 28 | + test("test rule UnresolvedOrdinalSubstitution, replaces ordinal in order by or group by") { |
| 29 | + val a = testRelation2.output(0) |
| 30 | + val b = testRelation2.output(1) |
| 31 | + val conf = new SimpleCatalystConf(caseSensitiveAnalysis = true) |
| 32 | + |
| 33 | + // Expression OrderByOrdinal is unresolved. |
| 34 | + assert(!UnresolvedOrdinal(0).resolved) |
| 35 | + |
| 36 | + // Tests order by ordinal, apply single rule. |
| 37 | + val plan = testRelation2.orderBy(Literal(1).asc, Literal(2).asc) |
| 38 | + comparePlans( |
| 39 | + new UnresolvedOrdinalSubstitution(conf).apply(plan), |
| 40 | + testRelation2.orderBy(UnresolvedOrdinal(1).asc, UnresolvedOrdinal(2).asc)) |
| 41 | + |
| 42 | + // Tests order by ordinal, do full analysis |
| 43 | + checkAnalysis(plan, testRelation2.orderBy(a.asc, b.asc)) |
| 44 | + |
| 45 | + // order by ordinal can be turned off by config |
| 46 | + comparePlans( |
| 47 | + new UnresolvedOrdinalSubstitution(conf.copy(orderByOrdinal = false)).apply(plan), |
| 48 | + testRelation2.orderBy(Literal(1).asc, Literal(2).asc)) |
| 49 | + |
| 50 | + |
| 51 | + // Tests group by ordinal, apply single rule. |
| 52 | + val plan2 = testRelation2.groupBy(Literal(1), Literal(2))('a, 'b) |
| 53 | + comparePlans( |
| 54 | + new UnresolvedOrdinalSubstitution(conf).apply(plan2), |
| 55 | + testRelation2.groupBy(UnresolvedOrdinal(1), UnresolvedOrdinal(2))('a, 'b)) |
| 56 | + |
| 57 | + // Tests group by ordinal, do full analysis |
| 58 | + checkAnalysis(plan2, testRelation2.groupBy(a, b)(a, b)) |
| 59 | + |
| 60 | + // group by ordinal can be turned off by config |
| 61 | + comparePlans( |
| 62 | + new UnresolvedOrdinalSubstitution(conf.copy(groupByOrdinal = false)).apply(plan2), |
| 63 | + testRelation2.groupBy(Literal(1), Literal(2))('a, 'b)) |
| 64 | + } |
| 65 | +} |
0 commit comments