spark git commit: [SPARK-16271][SQL] Implement Hive's UDFXPathUtil
Repository: spark Updated Branches: refs/heads/branch-2.0 a04975457 -> 144aa84ce [SPARK-16271][SQL] Implement Hive's UDFXPathUtil This patch ports Hive's UDFXPathUtil over to Spark, which can be used to implement xpath functionality in Spark in the near future. Added two new test suites UDFXPathUtilSuite and ReusableStringReaderSuite. They have been ported over from Hive (but rewritten in Scala in order to leverage ScalaTest). Author: petermaxleeCloses #13961 from petermaxlee/xpath. (cherry picked from commit 153c2f9ac12846367a09684fd875c496d350a603) Signed-off-by: Reynold Xin Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/144aa84c Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/144aa84c Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/144aa84c Branch: refs/heads/branch-2.0 Commit: 144aa84ce0f3463d95c06c78df6e9996ad42240a Parents: a049754 Author: petermaxlee Authored: Tue Jun 28 21:07:52 2016 -0700 Committer: Reynold Xin Committed: Thu Jul 7 21:07:03 2016 -0700 -- .../catalyst/expressions/xml/UDFXPathUtil.java | 192 +++ .../xml/ReusableStringReaderSuite.scala | 103 ++ .../expressions/xml/UDFXPathUtilSuite.scala | 99 ++ 3 files changed, 394 insertions(+) -- http://git-wip-us.apache.org/repos/asf/spark/blob/144aa84c/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java -- diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java new file mode 100644 index 000..01a11f9 --- /dev/null +++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.expressions.xml; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +import javax.xml.namespace.QName; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * Utility class for all XPath UDFs. Each UDF instance should keep an instance of this class. + * + * This is based on Hive's UDFXPathUtil implementation. + */ +public class UDFXPathUtil { + private XPath xpath = XPathFactory.newInstance().newXPath(); + private ReusableStringReader reader = new ReusableStringReader(); + private InputSource inputSource = new InputSource(reader); + private XPathExpression expression = null; + private String oldPath = null; + + public Object eval(String xml, String path, QName qname) { +if (xml == null || path == null || qname == null) { + return null; +} + +if (xml.length() == 0 || path.length() == 0) { + return null; +} + +if (!path.equals(oldPath)) { + try { +expression = xpath.compile(path); + } catch (XPathExpressionException e) { +expression = null; + } + oldPath = path; +} + +if (expression == null) { + return null; +} + +reader.set(xml); + +try { + return expression.evaluate(inputSource, qname); +} catch (XPathExpressionException e) { + throw new RuntimeException ("Invalid expression '" + oldPath + "'", e); +} + } + + public Boolean evalBoolean(String xml, String path) { +return (Boolean) eval(xml, path, XPathConstants.BOOLEAN); + } + + public String evalString(String xml, String path) { +return (String) eval(xml, path, XPathConstants.STRING); + } + + public Double evalNumber(String xml, String path) { +return (Double) eval(xml, path,
spark git commit: [SPARK-16271][SQL] Implement Hive's UDFXPathUtil
Repository: spark Updated Branches: refs/heads/master 0df5ce1bc -> 153c2f9ac [SPARK-16271][SQL] Implement Hive's UDFXPathUtil ## What changes were proposed in this pull request? This patch ports Hive's UDFXPathUtil over to Spark, which can be used to implement xpath functionality in Spark in the near future. ## How was this patch tested? Added two new test suites UDFXPathUtilSuite and ReusableStringReaderSuite. They have been ported over from Hive (but rewritten in Scala in order to leverage ScalaTest). Author: petermaxleeCloses #13961 from petermaxlee/xpath. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/153c2f9a Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/153c2f9a Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/153c2f9a Branch: refs/heads/master Commit: 153c2f9ac12846367a09684fd875c496d350a603 Parents: 0df5ce1 Author: petermaxlee Authored: Tue Jun 28 21:07:52 2016 -0700 Committer: Reynold Xin Committed: Tue Jun 28 21:07:52 2016 -0700 -- .../catalyst/expressions/xml/UDFXPathUtil.java | 192 +++ .../xml/ReusableStringReaderSuite.scala | 103 ++ .../expressions/xml/UDFXPathUtilSuite.scala | 99 ++ 3 files changed, 394 insertions(+) -- http://git-wip-us.apache.org/repos/asf/spark/blob/153c2f9a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java -- diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java new file mode 100644 index 000..01a11f9 --- /dev/null +++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/xml/UDFXPathUtil.java @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.expressions.xml; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +import javax.xml.namespace.QName; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +/** + * Utility class for all XPath UDFs. Each UDF instance should keep an instance of this class. + * + * This is based on Hive's UDFXPathUtil implementation. + */ +public class UDFXPathUtil { + private XPath xpath = XPathFactory.newInstance().newXPath(); + private ReusableStringReader reader = new ReusableStringReader(); + private InputSource inputSource = new InputSource(reader); + private XPathExpression expression = null; + private String oldPath = null; + + public Object eval(String xml, String path, QName qname) { +if (xml == null || path == null || qname == null) { + return null; +} + +if (xml.length() == 0 || path.length() == 0) { + return null; +} + +if (!path.equals(oldPath)) { + try { +expression = xpath.compile(path); + } catch (XPathExpressionException e) { +expression = null; + } + oldPath = path; +} + +if (expression == null) { + return null; +} + +reader.set(xml); + +try { + return expression.evaluate(inputSource, qname); +} catch (XPathExpressionException e) { + throw new RuntimeException ("Invalid expression '" + oldPath + "'", e); +} + } + + public Boolean evalBoolean(String xml, String path) { +return (Boolean) eval(xml, path, XPathConstants.BOOLEAN); + } + + public String evalString(String xml, String path) { +return (String) eval(xml, path, XPathConstants.STRING); + } + + public Double evalNumber(String xml, String path) { +return (Double) eval(xml, path, XPathConstants.NUMBER); + } + + public Node evalNode(String