Skip to content

[SPARK-52104][CONNECT][SCALA] Validate column name eagerly in Spark Connect Scala Client #50873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -26,6 +26,7 @@ import scala.util.{Failure, Success}
import org.scalatest.concurrent.Eventually._

import org.apache.spark.SparkException
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.StringEncoder
import org.apache.spark.sql.connect.test.{ConnectFunSuite, RemoteSparkSession}
import org.apache.spark.util.SparkThreadUtils.awaitResult
Expand Down Expand Up @@ -450,4 +451,11 @@ class SparkSessionE2ESuite extends ConnectFunSuite with RemoteSparkSession {
Map("one" -> "1", "two" -> "2"))
assert(df.as(StringEncoder).collect().toSet == Set("one", "two"))
}

test("Non-existent columns throw exception") {
val e = intercept[AnalysisException] {
spark.range(10).col("nonexistent")
}
assert(e.getMessage.contains("UNRESOLVED_COLUMN.WITH_SUGGESTION"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.apache.spark.sql.connect

import java.util

import scala.annotation.tailrec
import scala.collection.mutable
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag
Expand All @@ -36,6 +37,7 @@ import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.catalyst.encoders.AgnosticEncoder
import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders._
import org.apache.spark.sql.catalyst.expressions.OrderUtils
import org.apache.spark.sql.catalyst.util.AttributeNameParser
import org.apache.spark.sql.connect.ColumnNodeToProtoConverter.{toExpr, toLiteral, toTypedExpr}
import org.apache.spark.sql.connect.ConnectConversions._
import org.apache.spark.sql.connect.client.SparkResult
Expand All @@ -45,7 +47,7 @@ import org.apache.spark.sql.execution.QueryExecution
import org.apache.spark.sql.expressions.SparkUserDefinedFunction
import org.apache.spark.sql.functions.{struct, to_json}
import org.apache.spark.sql.internal.{ToScalaUDF, UDFAdaptors, UnresolvedAttribute, UnresolvedRegex}
import org.apache.spark.sql.types.{Metadata, StructType}
import org.apache.spark.sql.types.{ArrayType, DataType, Metadata, StructType}
import org.apache.spark.storage.StorageLevel
import org.apache.spark.util.ArrayImplicits._
import org.apache.spark.util.SparkClassUtils
Expand Down Expand Up @@ -451,7 +453,52 @@ class Dataset[T] private[sql] (
}

/** @inheritdoc */
def col(colName: String): Column = new Column(colName, getPlanId)
def col(colName: String): Column = {
// Validate the column name against the schema.
if (!verifyColName(colName, schema)) {
this.select(colName).isLocal
}
new Column(colName, getPlanId)
}

/**
* Verify whether the input column name can be resolved with the given schema. Note that this
* method can not 100% match the analyzer behavior, it is designed to try the best to eliminate
* unnecessary validation RPCs.
*/
private def verifyColName(name: String, schema: StructType): Boolean = {
val partsOpt = AttributeNameParser.parseAttributeName(name)
if (partsOpt == null || partsOpt.isEmpty) return false

@tailrec
def quickVerify(parts: Seq[String], schema: DataType): Boolean = {
if (parts.isEmpty) return true

val part = parts.head
val rest = parts.tail

if (part == "*") {
true
} else {
val structSchema = schema match {
case s: StructType => Some(s)
case a: ArrayType if a.elementType.isInstanceOf[StructType] =>
Some(a.elementType.asInstanceOf[StructType])
case _ => None
}

structSchema match {
case Some(s) =>
s.fields.find(_.name == part) match {
case Some(field) => quickVerify(rest, field.dataType)
case None => false
}
case None => false
}
}
}
quickVerify(partsOpt, schema)
}

/** @inheritdoc */
def metadataColumn(colName: String): Column = {
Expand Down