|
| 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.optimizer |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 21 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 22 | +import org.apache.spark.sql.catalyst.expressions.{Concat, GetJsonObject, JsonTuple} |
| 23 | +import org.apache.spark.sql.catalyst.plans.PlanTest |
| 24 | +import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} |
| 25 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 26 | +import org.apache.spark.sql.internal.SQLConf |
| 27 | + |
| 28 | +class RewriteGetJsonObjectSuite extends PlanTest { |
| 29 | + |
| 30 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 31 | + val batches = |
| 32 | + Batch("Rewrite GetJsonObject", Once, |
| 33 | + RewriteGetJsonObject) :: Nil |
| 34 | + } |
| 35 | + |
| 36 | + private val testRelation = LocalRelation('a.string, 'b.string, 'c.string) |
| 37 | + |
| 38 | + test("Rewrite to a single JsonTuple") { |
| 39 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 40 | + val query = testRelation |
| 41 | + .select( |
| 42 | + GetJsonObject($"a", stringToLiteral("$.c1")).as("c1"), |
| 43 | + GetJsonObject($"a", stringToLiteral("$.c2")).as("c2"), |
| 44 | + GetJsonObject($"a", stringToLiteral("$.c3")).as("c3")) |
| 45 | + |
| 46 | + val correctAnswer = testRelation |
| 47 | + .generate( |
| 48 | + JsonTuple(Seq($"a", stringToLiteral("c1"), stringToLiteral("c2"), stringToLiteral("c3"))), |
| 49 | + Nil, |
| 50 | + alias = Some("`a`"), |
| 51 | + outputNames = Seq("c1", "c2", "c3")) |
| 52 | + .select($"c1".as("c1"), $"c2".as("c2"), $"c3".as("c3")) |
| 53 | + |
| 54 | + val getJsonObjects = query.expressions.flatMap { |
| 55 | + _.collect { |
| 56 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 57 | + } |
| 58 | + } |
| 59 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.nonEmpty)) |
| 60 | + comparePlans(Optimize.execute(query.analyze), correctAnswer.analyze) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + test("Rewrite to multiple JsonTuples") { |
| 65 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 66 | + val query = testRelation |
| 67 | + .select( |
| 68 | + GetJsonObject($"a", stringToLiteral("$.c1")).as("c1"), |
| 69 | + GetJsonObject($"a", stringToLiteral("$.c2")).as("c2"), |
| 70 | + GetJsonObject($"b", stringToLiteral("$.c1")).as("c3"), |
| 71 | + GetJsonObject($"b", stringToLiteral("$.c2")).as("c4"), |
| 72 | + GetJsonObject($"c", stringToLiteral("$.c1")).as("c5")) |
| 73 | + |
| 74 | + val correctAnswer = testRelation |
| 75 | + .generate( |
| 76 | + JsonTuple(Seq($"a", stringToLiteral("c1"), stringToLiteral("c2"))), |
| 77 | + Nil, |
| 78 | + alias = Some("`a`"), |
| 79 | + outputNames = Seq("c1", "c2")) |
| 80 | + .generate( |
| 81 | + JsonTuple(Seq($"b", stringToLiteral("c1"), stringToLiteral("c2"))), |
| 82 | + Nil, |
| 83 | + alias = Some("`b`"), |
| 84 | + outputNames = Seq("c1", "c2")) |
| 85 | + .select( |
| 86 | + $"`a`.c1".as("c1"), $"`a`.c2".as("c2"), |
| 87 | + $"`b`.c1".as("c3"), $"`b`.c2".as("c4"), |
| 88 | + GetJsonObject($"c", stringToLiteral("$.c1")).as("c5")) |
| 89 | + |
| 90 | + val getJsonObjects = query.expressions.flatMap { |
| 91 | + _.collect { |
| 92 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 93 | + } |
| 94 | + } |
| 95 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.nonEmpty)) |
| 96 | + comparePlans(Optimize.execute(query.analyze), correctAnswer.analyze) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + test("Rewrite GetJsonObject with other UDFs") { |
| 101 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 102 | + val query = testRelation |
| 103 | + .select( |
| 104 | + Concat(Seq(GetJsonObject($"a", stringToLiteral("$.c1")), |
| 105 | + GetJsonObject($"a", stringToLiteral("$.c2")), |
| 106 | + GetJsonObject($"a", stringToLiteral("$.c3")))).as("c")) |
| 107 | + |
| 108 | + val correctAnswer = testRelation |
| 109 | + .generate( |
| 110 | + JsonTuple(Seq($"a", stringToLiteral("c1"), stringToLiteral("c2"), stringToLiteral("c3"))), |
| 111 | + Nil, |
| 112 | + alias = Some("`a`"), |
| 113 | + outputNames = Seq("c1", "c2", "c3")) |
| 114 | + .select(Concat(Seq($"c1", $"c2", $"c3")).as("c")) |
| 115 | + |
| 116 | + val getJsonObjects = query.expressions.flatMap { |
| 117 | + _.collect { |
| 118 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 119 | + } |
| 120 | + } |
| 121 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.nonEmpty)) |
| 122 | + comparePlans(Optimize.execute(query.analyze), correctAnswer.analyze) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + test("Do not rewrite if parsed path is empty") { |
| 127 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 128 | + val query = testRelation |
| 129 | + .select( |
| 130 | + GetJsonObject($"a", stringToLiteral("c1")).as("c1"), |
| 131 | + GetJsonObject($"a", stringToLiteral("c2")).as("c2"), |
| 132 | + GetJsonObject($"a", stringToLiteral("c3")).as("c3")) |
| 133 | + |
| 134 | + val getJsonObjects = query.expressions.flatMap { |
| 135 | + _.collect { |
| 136 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 137 | + } |
| 138 | + } |
| 139 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.isEmpty)) |
| 140 | + comparePlans(Optimize.execute(query.analyze), query.analyze) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + test("Do not rewrite if parsed path is not Key and Named") { |
| 145 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 146 | + val query = testRelation |
| 147 | + .select( |
| 148 | + GetJsonObject($"a", stringToLiteral("$[0]")).as("c1"), |
| 149 | + GetJsonObject($"a", stringToLiteral("$[1]")).as("c2")) |
| 150 | + |
| 151 | + val getJsonObjects = query.expressions.flatMap { |
| 152 | + _.collect { |
| 153 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 154 | + } |
| 155 | + } |
| 156 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.isEmpty)) |
| 157 | + comparePlans(Optimize.execute(query.analyze), query.analyze) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + test("Do not rewrite if parsed path contains multiple Named") { |
| 162 | + withSQLConf(SQLConf.REWRITE_TO_JSONTUPLE.key -> "true") { |
| 163 | + val query = testRelation |
| 164 | + .select( |
| 165 | + GetJsonObject($"a", stringToLiteral("$.a.b")).as("c1"), |
| 166 | + GetJsonObject($"a", stringToLiteral("$.c.d")).as("c2")) |
| 167 | + |
| 168 | + val getJsonObjects = query.expressions.flatMap { |
| 169 | + _.collect { |
| 170 | + case gjo: GetJsonObject if gjo.rewriteToJsonTuplePath.nonEmpty => gjo |
| 171 | + } |
| 172 | + } |
| 173 | + assert(getJsonObjects.forall(_.rewriteToJsonTuplePath.isEmpty)) |
| 174 | + comparePlans(Optimize.execute(query.analyze), query.analyze) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments