Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update
  • Loading branch information
panbingkun committed Nov 13, 2024
commit efe2a11fb5596c0205964c2c3412857fefede1b9
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,17 @@
* 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.util.DropMalformedMode
import org.apache.spark.sql.catalyst.xml.{XmlInferSchema, XmlOptions}
import org.apache.spark.sql.errors.QueryCompilationErrors
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

case class SchemaOfXmlEvaluator(options: Map[String, String]) {

@transient
private lazy val xmlOptions = new XmlOptions(options, "UTC")

@transient
private lazy val xmlInferSchema = {
if (xmlOptions.parseMode == DropMalformedMode) {
throw QueryCompilationErrors.parseModeUnsupportedError("schema_of_xml", xmlOptions.parseMode)
}
new XmlInferSchema(xmlOptions, caseSensitive = SQLConf.get.caseSensitiveAnalysis)
}
object XmlExpressionEvalUtils {

final def evaluate(xml: UTF8String): Any = {
def schemaOfXml(xmlInferSchema: XmlInferSchema, xml: UTF8String): UTF8String = {
val dataType = xmlInferSchema.infer(xml.toString).get match {
case st: StructType =>
xmlInferSchema.canonicalizeType(st).getOrElse(StructType(Nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ 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, ExprCode}
import org.apache.spark.sql.catalyst.expressions.objects.Invoke
import org.apache.spark.sql.catalyst.expressions.xml.SchemaOfXmlEvaluator
import org.apache.spark.sql.catalyst.util.{FailFastMode, FailureSafeParser, PermissiveMode}
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, XmlOptions}
import org.apache.spark.sql.catalyst.xml.{StaxXmlGenerator, StaxXmlParser, ValidatorUtil, XmlInferSchema, XmlOptions}
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryErrorsBase}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.types.StringTypeWithCollation
Expand Down Expand Up @@ -191,14 +191,24 @@ case class SchemaOfXml(
copy(child = newChild)

@transient
private lazy val evaluator: SchemaOfXmlEvaluator = SchemaOfXmlEvaluator(options)
private lazy val xmlOptions = new XmlOptions(options, "UTC")

override def replacement: Expression = Invoke(
Literal.create(evaluator, ObjectType(classOf[SchemaOfXmlEvaluator])),
"evaluate",
@transient
private lazy val xmlInferSchema = {
if (xmlOptions.parseMode == DropMalformedMode) {
throw QueryCompilationErrors.parseModeUnsupportedError("schema_of_xml", xmlOptions.parseMode)
}
new XmlInferSchema(xmlOptions, caseSensitive = SQLConf.get.caseSensitiveAnalysis)
}

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

override def replacement: Expression = StaticInvoke(
XmlExpressionEvalUtils.getClass,
dataType,
Seq(child),
Seq(child.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