|
| 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.expressions |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 22 | +import org.apache.spark.sql.catalyst.plans.PlanTest |
| 23 | +import org.apache.spark.sql.internal.SQLConf |
| 24 | +import org.apache.spark.sql.types.BooleanType |
| 25 | + |
| 26 | +class ConjunctiveNormalFormPredicateSuite extends SparkFunSuite with PredicateHelper with PlanTest { |
| 27 | + private val a = AttributeReference("A", BooleanType)(exprId = ExprId(1)).withQualifier(Seq("ta")) |
| 28 | + private val b = AttributeReference("B", BooleanType)(exprId = ExprId(2)).withQualifier(Seq("tb")) |
| 29 | + private val c = AttributeReference("C", BooleanType)(exprId = ExprId(3)).withQualifier(Seq("tc")) |
| 30 | + private val d = AttributeReference("D", BooleanType)(exprId = ExprId(4)).withQualifier(Seq("td")) |
| 31 | + private val e = AttributeReference("E", BooleanType)(exprId = ExprId(5)).withQualifier(Seq("te")) |
| 32 | + private val f = AttributeReference("F", BooleanType)(exprId = ExprId(6)).withQualifier(Seq("tf")) |
| 33 | + private val g = AttributeReference("C", BooleanType)(exprId = ExprId(7)).withQualifier(Seq("tg")) |
| 34 | + private val h = AttributeReference("D", BooleanType)(exprId = ExprId(8)).withQualifier(Seq("th")) |
| 35 | + private val i = AttributeReference("E", BooleanType)(exprId = ExprId(9)).withQualifier(Seq("ti")) |
| 36 | + private val j = AttributeReference("F", BooleanType)(exprId = ExprId(10)).withQualifier(Seq("tj")) |
| 37 | + private val a1 = |
| 38 | + AttributeReference("a1", BooleanType)(exprId = ExprId(11)).withQualifier(Seq("ta")) |
| 39 | + private val a2 = |
| 40 | + AttributeReference("a2", BooleanType)(exprId = ExprId(12)).withQualifier(Seq("ta")) |
| 41 | + private val b1 = |
| 42 | + AttributeReference("b1", BooleanType)(exprId = ExprId(12)).withQualifier(Seq("tb")) |
| 43 | + |
| 44 | + // Check CNF conversion with expected expression, assuming the input has non-empty result. |
| 45 | + private def checkCondition(input: Expression, expected: Expression): Unit = { |
| 46 | + val cnf = conjunctiveNormalForm(input) |
| 47 | + assert(cnf.nonEmpty) |
| 48 | + val result = cnf.reduceLeft(And) |
| 49 | + assert(result.semanticEquals(expected)) |
| 50 | + } |
| 51 | + |
| 52 | + test("Keep non-predicated expressions") { |
| 53 | + checkCondition(a, a) |
| 54 | + checkCondition(Literal(1), Literal(1)) |
| 55 | + } |
| 56 | + |
| 57 | + test("Conversion of Not") { |
| 58 | + checkCondition(!a, !a) |
| 59 | + checkCondition(!(!a), a) |
| 60 | + checkCondition(!(!(a && b)), a && b) |
| 61 | + checkCondition(!(!(a || b)), a || b) |
| 62 | + checkCondition(!(a || b), !a && !b) |
| 63 | + checkCondition(!(a && b), !a || !b) |
| 64 | + } |
| 65 | + |
| 66 | + test("Conversion of And") { |
| 67 | + checkCondition(a && b, a && b) |
| 68 | + checkCondition(a && b && c, a && b && c) |
| 69 | + checkCondition(a && (b || c), a && (b || c)) |
| 70 | + checkCondition((a || b) && c, (a || b) && c) |
| 71 | + checkCondition(a && b && c && d, a && b && c && d) |
| 72 | + } |
| 73 | + |
| 74 | + test("Conversion of Or") { |
| 75 | + checkCondition(a || b, a || b) |
| 76 | + checkCondition(a || b || c, a || b || c) |
| 77 | + checkCondition(a || b || c || d, a || b || c || d) |
| 78 | + checkCondition((a && b) || c, (a || c) && (b || c)) |
| 79 | + checkCondition((a && b) || (c && d), (a || c) && (a || d) && (b || c) && (b || d)) |
| 80 | + } |
| 81 | + |
| 82 | + test("More complex cases") { |
| 83 | + checkCondition(a && !(b || c), a && !b && !c) |
| 84 | + checkCondition((a && b) || !(c && d), (a || !c || !d) && (b || !c || !d)) |
| 85 | + checkCondition(a || b || c && d, (a || b || c) && (a || b || d)) |
| 86 | + checkCondition(a || (b && c || d), (a || b || d) && (a || c || d)) |
| 87 | + checkCondition(a && !(b && c || d && e), a && (!b || !c) && (!d || !e)) |
| 88 | + checkCondition(((a && b) || c) || (d || e), (a || c || d || e) && (b || c || d || e)) |
| 89 | + |
| 90 | + checkCondition( |
| 91 | + (a && b && c) || (d && e && f), |
| 92 | + (a || d) && (a || e) && (a || f) && (b || d) && (b || e) && (b || f) && |
| 93 | + (c || d) && (c || e) && (c || f) |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + test("Aggregate predicate of same qualifiers to avoid expanding") { |
| 98 | + checkCondition(((a && b && a1) || c), ((a && a1) || c) && (b ||c)) |
| 99 | + checkCondition(((a && a1 && b) || c), ((a && a1) || c) && (b ||c)) |
| 100 | + checkCondition(((b && d && a && a1) || c), ((a && a1) || c) && (b ||c) && (d || c)) |
| 101 | + checkCondition(((b && a2 && d && a && a1) || c), ((a2 && a && a1) || c) && (b ||c) && (d || c)) |
| 102 | + checkCondition(((b && d && a && a1 && b1) || c), |
| 103 | + ((a && a1) || c) && ((b && b1) ||c) && (d || c)) |
| 104 | + checkCondition((a && a1) || (b && b1), (a && a1) || (b && b1)) |
| 105 | + checkCondition((a && a1 && c) || (b && b1), ((a && a1) || (b && b1)) && (c || (b && b1))) |
| 106 | + } |
| 107 | + |
| 108 | + test("Return None when exceeding MAX_CNF_NODE_COUNT") { |
| 109 | + // The following expression contains 36 conjunctive sub-expressions in CNF |
| 110 | + val input = (a && b && c) || (d && e && f) || (g && h && i && j) |
| 111 | + // The following expression contains 9 conjunctive sub-expressions in CNF |
| 112 | + val input2 = (a && b && c) || (d && e && f) |
| 113 | + Seq(8, 9, 10, 35, 36, 37).foreach { maxCount => |
| 114 | + withSQLConf(SQLConf.MAX_CNF_NODE_COUNT.key -> maxCount.toString) { |
| 115 | + if (maxCount < 36) { |
| 116 | + assert(conjunctiveNormalForm(input).isEmpty) |
| 117 | + } else { |
| 118 | + assert(conjunctiveNormalForm(input).nonEmpty) |
| 119 | + } |
| 120 | + if (maxCount < 9) { |
| 121 | + assert(conjunctiveNormalForm(input2).isEmpty) |
| 122 | + } else { |
| 123 | + assert(conjunctiveNormalForm(input2).nonEmpty) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments