Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 org.apache.spark.sql.catalyst.xml.XmlInferSchema
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{ArrayType, DataType, StructType}
import org.apache.spark.unsafe.types.UTF8String

object XmlExpressionEvalUtils {

def schemaOfXml(xmlInferSchema: XmlInferSchema, xml: UTF8String): UTF8String = {
val dataType = xmlInferSchema.infer(xml.toString).get match {
case st: StructType =>
xmlInferSchema.canonicalizeType(st).getOrElse(StructType(Nil))
case at: ArrayType if at.elementType.isInstanceOf[StructType] =>
xmlInferSchema
.canonicalizeType(at.elementType)
.map(ArrayType(_, containsNull = at.containsNull))
.getOrElse(ArrayType(StructType(Nil), containsNull = at.containsNull))
case other: DataType =>
xmlInferSchema.canonicalizeType(other).getOrElse(SQLConf.get.defaultStringType)
}

UTF8String.fromString(dataType.sql)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import java.io.CharArrayWriter
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{DataTypeMismatch, TypeCheckSuccess}
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodegenFallback, ExprCode}
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke
import org.apache.spark.sql.catalyst.expressions.xml.XmlExpressionEvalUtils
import org.apache.spark.sql.catalyst.util.{DropMalformedMode, FailFastMode, FailureSafeParser, PermissiveMode}
import org.apache.spark.sql.catalyst.util.TypeUtils._
import org.apache.spark.sql.catalyst.xml.{StaxXmlGenerator, StaxXmlParser, ValidatorUtil, XmlInferSchema, XmlOptions}
Expand Down Expand Up @@ -149,7 +151,9 @@ case class XmlToStructs(
case class SchemaOfXml(
child: Expression,
options: Map[String, String])
extends UnaryExpression with CodegenFallback with QueryErrorsBase {
extends UnaryExpression
with RuntimeReplaceable
with QueryErrorsBase {

def this(child: Expression) = this(child, Map.empty[String, String])

Expand Down Expand Up @@ -192,26 +196,20 @@ case class SchemaOfXml(
}
}

override def eval(v: InternalRow): Any = {
val dataType = xmlInferSchema.infer(xml.toString).get match {
case st: StructType =>
xmlInferSchema.canonicalizeType(st).getOrElse(StructType(Nil))
case at: ArrayType if at.elementType.isInstanceOf[StructType] =>
xmlInferSchema
.canonicalizeType(at.elementType)
.map(ArrayType(_, containsNull = at.containsNull))
.getOrElse(ArrayType(StructType(Nil), containsNull = at.containsNull))
case other: DataType =>
xmlInferSchema.canonicalizeType(other).getOrElse(SQLConf.get.defaultStringType)
}

UTF8String.fromString(dataType.sql)
}

override def prettyName: String = "schema_of_xml"

override protected def withNewChildInternal(newChild: Expression): SchemaOfXml =
copy(child = newChild)

@transient private lazy val xmlInferSchemaObjectType = ObjectType(classOf[XmlInferSchema])

override def replacement: Expression = StaticInvoke(
XmlExpressionEvalUtils.getClass,
dataType,
"schemaOfXml",
Seq(Literal(xmlInferSchema, xmlInferSchemaObjectType), child),
Seq(xmlInferSchemaObjectType, child.dataType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should set returnNullable to false, otherwise it's a regression. SchemaOfXml#nullable always return false.

cc @panbingkun

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I have fixed this issue with followup #48987. I think scheme_of_xml, schema_of_csv and schema_of_json has similar issues, so I will fix them all at once.
Thanks!

)
}

/**
Expand Down