|
| 1 | +/* |
| 2 | + * Copyright (2020) The Hyperspace Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.microsoft.hyperspace.index.rules |
| 18 | + |
| 19 | +import scala.util.Try |
| 20 | + |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, GetStructField} |
| 22 | +import org.apache.spark.sql.types.{DataType, StructType} |
| 23 | + |
| 24 | +import com.microsoft.hyperspace.util.SchemaUtils |
| 25 | + |
| 26 | +object PlanUtils { |
| 27 | + |
| 28 | + /** |
| 29 | + * The method extract field names from a Spark Catalyst [[Expression]]. |
| 30 | + * |
| 31 | + * @param exp The Spark Catalyst expression from which to extract names. |
| 32 | + * @return A set of distinct field names. |
| 33 | + */ |
| 34 | + def extractNamesFromExpression(exp: Expression): Set[String] = { |
| 35 | + exp match { |
| 36 | + case AttributeReference(name, _, _, _) => |
| 37 | + Set(s"$name") |
| 38 | + case otherExp => |
| 39 | + otherExp.containsChild.map { |
| 40 | + case g: GetStructField => |
| 41 | + s"${getChildNameFromStruct(g)}" |
| 42 | + case e: Expression => |
| 43 | + extractNamesFromExpression(e).filter(_.nonEmpty).mkString(".") |
| 44 | + case _ => "" |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Given a [[GetStructField]] expression for a nested field (aka a struct) |
| 51 | + * the method will extract the full field `.` (dot) separated name. |
| 52 | + * |
| 53 | + * @param field The [[GetStructField]] field from which we want to extract |
| 54 | + * the name. |
| 55 | + * @return A field name `.` (dot) separated if nested. |
| 56 | + */ |
| 57 | + def getChildNameFromStruct(field: GetStructField): String = { |
| 58 | + field.child match { |
| 59 | + case f: GetStructField => |
| 60 | + s"${getChildNameFromStruct(f)}.${field.name.get}" |
| 61 | + case a: AttributeReference => |
| 62 | + s"${a.name}.${field.name.get}" |
| 63 | + case _ => |
| 64 | + s"${field.name.get}" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Given an Spark Catalyst [[Expression]] and a field name the method extracts |
| 70 | + * the parent search expression and the expression that contains the field name |
| 71 | + * @param exp The Spark Catalyst [[Expression]] to extract from. |
| 72 | + * @param name The field name to search for. |
| 73 | + * @return A tuple with the parent expression and the leaf expression that |
| 74 | + * contains the given name. |
| 75 | + */ |
| 76 | + def extractSearchQuery(exp: Expression, name: String): (Expression, Expression) = { |
| 77 | + val splits = name.split(".") |
| 78 | + val expFound = exp.find { |
| 79 | + case a: AttributeReference if splits.forall(s => a.name.contains(s)) => true |
| 80 | + case f: GetStructField if splits.forall(s => f.toString().contains(s)) => true |
| 81 | + case _ => false |
| 82 | + }.get |
| 83 | + val parent = exp.find { |
| 84 | + case e: Expression if e.containsChild.contains(expFound) => true |
| 85 | + case _ => false |
| 86 | + }.get |
| 87 | + (parent, expFound) |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Given an Spark Catalyst [[Expression]], a needle [[Expression]] and a replace |
| 92 | + * [[Expression]] the method will replace the needle with the replacement into |
| 93 | + * the parent expression. |
| 94 | + * |
| 95 | + * @param parent The parent Spark Catalyst [[Expression]] into which to replace. |
| 96 | + * @param needle The Spark Catalyst [[Expression]] needle to search for. |
| 97 | + * @param repl The replacement Spark Catalyst [[Expression]]. |
| 98 | + * @return A new Spark Catalyst [[Expression]]. |
| 99 | + */ |
| 100 | + def replaceInSearchQuery( |
| 101 | + parent: Expression, |
| 102 | + needle: Expression, |
| 103 | + repl: Expression): Expression = { |
| 104 | + parent.mapChildren { c => |
| 105 | + if (c == needle) { |
| 106 | + repl |
| 107 | + } else { |
| 108 | + c |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Given an Spark Catalyst [[Expression]] and a field name the method |
| 115 | + * extracts the [[AttributeReference]] for that field name. |
| 116 | + * |
| 117 | + * @param exp The Spark Catalyst [[Expression]] to extract from. |
| 118 | + * @param name The field name for which to extract the attribute reference. |
| 119 | + * @return A Spark Catalyst [[AttributeReference]] pointing to the field name. |
| 120 | + */ |
| 121 | + def extractAttributeRef(exp: Expression, name: String): AttributeReference = { |
| 122 | + val splits = name.split(".") |
| 123 | + val elem = exp.find { |
| 124 | + case a: AttributeReference if splits.contains(a.name) => true |
| 125 | + case _ => false |
| 126 | + } |
| 127 | + elem.get.asInstanceOf[AttributeReference] |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Given a Spark Catalyst [[Expression]] and a field name the method |
| 132 | + * extracts the type of the field as a Spark SQL [[DataType]]. |
| 133 | + * |
| 134 | + * @param exp The Spark Catalyst [[Expression]] from which to extract the type. |
| 135 | + * @param name The field name for which we need to get the type. |
| 136 | + * @return A Spark SQL [[DataType]] of the given field name. |
| 137 | + */ |
| 138 | + def extractTypeFromExpression(exp: Expression, name: String): DataType = { |
| 139 | + val splits = name.split(".") |
| 140 | + val elem = exp.flatMap { |
| 141 | + case a: AttributeReference => |
| 142 | + if (splits.forall(s => a.name == s)) { |
| 143 | + Some((name, a.dataType)) |
| 144 | + } else { |
| 145 | + Try({ |
| 146 | + val h :: t = splits.toList |
| 147 | + if (a.name == h && a.dataType.isInstanceOf[StructType]) { |
| 148 | + val currentDataType = a.dataType.asInstanceOf[StructType] |
| 149 | + val foldedFields = t.foldLeft(Seq.empty[(String, DataType)]) { (acc, i) => |
| 150 | + val idx = currentDataType.indexWhere(_.name.equalsIgnoreCase(i)) |
| 151 | + acc :+ (i, currentDataType(idx).dataType) |
| 152 | + } |
| 153 | + Some(foldedFields.last) |
| 154 | + } else { |
| 155 | + None |
| 156 | + } |
| 157 | + }).getOrElse(None) |
| 158 | + } |
| 159 | + case f: GetStructField if splits.forall(s => f.toString().contains(s)) => |
| 160 | + Some((name, f.dataType)) |
| 161 | + case _ => None |
| 162 | + } |
| 163 | + elem.find(e => e._1 == name || e._1 == splits.last).get._2 |
| 164 | + } |
| 165 | +} |
0 commit comments