|
| 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.xml |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult |
| 21 | +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure |
| 22 | +import org.apache.spark.sql.catalyst.expressions._ |
| 23 | +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback |
| 24 | +import org.apache.spark.sql.catalyst.util.GenericArrayData |
| 25 | +import org.apache.spark.sql.types._ |
| 26 | +import org.apache.spark.unsafe.types.UTF8String |
| 27 | + |
| 28 | +/** |
| 29 | + * Base class for xpath_boolean, xpath_double, xpath_int, etc. |
| 30 | + * |
| 31 | + * This is not the world's most efficient implementation due to type conversion, but works. |
| 32 | + */ |
| 33 | +abstract class XPathExtract extends BinaryExpression with ExpectsInputTypes with CodegenFallback { |
| 34 | + override def left: Expression = xml |
| 35 | + override def right: Expression = path |
| 36 | + |
| 37 | + /** XPath expressions are always nullable, e.g. if the xml string is empty. */ |
| 38 | + override def nullable: Boolean = true |
| 39 | + |
| 40 | + override def inputTypes: Seq[AbstractDataType] = Seq(StringType, StringType) |
| 41 | + |
| 42 | + override def checkInputDataTypes(): TypeCheckResult = { |
| 43 | + if (!path.foldable) { |
| 44 | + TypeCheckFailure("path should be a string literal") |
| 45 | + } else { |
| 46 | + super.checkInputDataTypes() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @transient protected lazy val xpathUtil = new UDFXPathUtil |
| 51 | + @transient protected lazy val pathString: String = path.eval().asInstanceOf[UTF8String].toString |
| 52 | + |
| 53 | + /** Concrete implementations need to override the following three methods. */ |
| 54 | + def xml: Expression |
| 55 | + def path: Expression |
| 56 | +} |
| 57 | + |
| 58 | +@ExpressionDescription( |
| 59 | + usage = "_FUNC_(xml, xpath) - Evaluates a boolean xpath expression.", |
| 60 | + extended = "> SELECT _FUNC_('<a><b>1</b></a>','a/b');\ntrue") |
| 61 | +case class XPathBoolean(xml: Expression, path: Expression) extends XPathExtract { |
| 62 | + |
| 63 | + override def prettyName: String = "xpath_boolean" |
| 64 | + override def dataType: DataType = BooleanType |
| 65 | + |
| 66 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 67 | + xpathUtil.evalBoolean(xml.asInstanceOf[UTF8String].toString, pathString) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@ExpressionDescription( |
| 72 | + usage = "_FUNC_(xml, xpath) - Returns a short value that matches the xpath expression", |
| 73 | + extended = "> SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)');\n3") |
| 74 | +case class XPathShort(xml: Expression, path: Expression) extends XPathExtract { |
| 75 | + override def prettyName: String = "xpath_int" |
| 76 | + override def dataType: DataType = ShortType |
| 77 | + |
| 78 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 79 | + val ret = xpathUtil.evalNumber(xml.asInstanceOf[UTF8String].toString, pathString) |
| 80 | + if (ret eq null) null else ret.shortValue() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@ExpressionDescription( |
| 85 | + usage = "_FUNC_(xml, xpath) - Returns an integer value that matches the xpath expression", |
| 86 | + extended = "> SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)');\n3") |
| 87 | +case class XPathInt(xml: Expression, path: Expression) extends XPathExtract { |
| 88 | + override def prettyName: String = "xpath_int" |
| 89 | + override def dataType: DataType = IntegerType |
| 90 | + |
| 91 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 92 | + val ret = xpathUtil.evalNumber(xml.asInstanceOf[UTF8String].toString, pathString) |
| 93 | + if (ret eq null) null else ret.intValue() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@ExpressionDescription( |
| 98 | + usage = "_FUNC_(xml, xpath) - Returns a long value that matches the xpath expression", |
| 99 | + extended = "> SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)');\n3") |
| 100 | +case class XPathLong(xml: Expression, path: Expression) extends XPathExtract { |
| 101 | + override def prettyName: String = "xpath_long" |
| 102 | + override def dataType: DataType = LongType |
| 103 | + |
| 104 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 105 | + val ret = xpathUtil.evalNumber(xml.asInstanceOf[UTF8String].toString, pathString) |
| 106 | + if (ret eq null) null else ret.longValue() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@ExpressionDescription( |
| 111 | + usage = "_FUNC_(xml, xpath) - Returns a float value that matches the xpath expression", |
| 112 | + extended = "> SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)');\n3.0") |
| 113 | +case class XPathFloat(xml: Expression, path: Expression) extends XPathExtract { |
| 114 | + override def prettyName: String = "xpath_float" |
| 115 | + override def dataType: DataType = FloatType |
| 116 | + |
| 117 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 118 | + val ret = xpathUtil.evalNumber(xml.asInstanceOf[UTF8String].toString, pathString) |
| 119 | + if (ret eq null) null else ret.floatValue() |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +@ExpressionDescription( |
| 124 | + usage = "_FUNC_(xml, xpath) - Returns a double value that matches the xpath expression", |
| 125 | + extended = "> SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)');\n3.0") |
| 126 | +case class XPathDouble(xml: Expression, path: Expression) extends XPathExtract { |
| 127 | + override def prettyName: String = "xpath_float" |
| 128 | + override def dataType: DataType = DoubleType |
| 129 | + |
| 130 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 131 | + val ret = xpathUtil.evalNumber(xml.asInstanceOf[UTF8String].toString, pathString) |
| 132 | + if (ret eq null) null else ret.doubleValue() |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// scalastyle:off line.size.limit |
| 137 | +@ExpressionDescription( |
| 138 | + usage = "_FUNC_(xml, xpath) - Returns the text contents of the first xml node that matches the xpath expression", |
| 139 | + extended = "> SELECT _FUNC_('<a><b>b</b><c>cc</c></a>','a/c');\ncc") |
| 140 | +// scalastyle:on line.size.limit |
| 141 | +case class XPathString(xml: Expression, path: Expression) extends XPathExtract { |
| 142 | + override def prettyName: String = "xpath_string" |
| 143 | + override def dataType: DataType = StringType |
| 144 | + |
| 145 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 146 | + val ret = xpathUtil.evalString(xml.asInstanceOf[UTF8String].toString, pathString) |
| 147 | + UTF8String.fromString(ret) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// scalastyle:off line.size.limit |
| 152 | +@ExpressionDescription( |
| 153 | + usage = "_FUNC_(xml, xpath) - Returns a string array of values within xml nodes that match the xpath expression", |
| 154 | + extended = "> SELECT _FUNC_('<a><b>b1</b><b>b2</b><b>b3</b><c>c1</c><c>c2</c></a>','a/b/text()');\n['b1','b2','b3']") |
| 155 | +// scalastyle:on line.size.limit |
| 156 | +case class XPathList(xml: Expression, path: Expression) extends XPathExtract { |
| 157 | + override def prettyName: String = "xpath" |
| 158 | + override def dataType: DataType = ArrayType(StringType, containsNull = false) |
| 159 | + |
| 160 | + override def nullSafeEval(xml: Any, path: Any): Any = { |
| 161 | + val nodeList = xpathUtil.evalNodeList(xml.asInstanceOf[UTF8String].toString, pathString) |
| 162 | + if (nodeList ne null) { |
| 163 | + val ret = new Array[UTF8String](nodeList.getLength) |
| 164 | + var i = 0 |
| 165 | + while (i < nodeList.getLength) { |
| 166 | + ret(i) = UTF8String.fromString(nodeList.item(i).getNodeValue) |
| 167 | + i += 1 |
| 168 | + } |
| 169 | + new GenericArrayData(ret) |
| 170 | + } else { |
| 171 | + null |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments