Skip to content
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

[WIP][PYTHON] Optimize Py4J call for DDL parse method #49320

Open
wants to merge 1 commit 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
31 changes: 3 additions & 28 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,34 +1916,9 @@ def _parse_datatype_string(s: str) -> DataType:
from py4j.java_gateway import JVMView

sc = get_active_spark_context()

def from_ddl_schema(type_str: str) -> DataType:
return _parse_datatype_json_string(
cast(JVMView, sc._jvm)
.org.apache.spark.sql.types.StructType.fromDDL(type_str)
.json()
)

def from_ddl_datatype(type_str: str) -> DataType:
return _parse_datatype_json_string(
cast(JVMView, sc._jvm)
.org.apache.spark.sql.api.python.PythonSQLUtils.parseDataType(type_str)
.json()
)

try:
# DDL format, "fieldname datatype, fieldname datatype".
return from_ddl_schema(s)
except Exception as e:
try:
# For backwards compatibility, "integer", "struct<fieldname: datatype>" and etc.
return from_ddl_datatype(s)
except BaseException:
try:
# For backwards compatibility, "fieldname: datatype, fieldname: datatype" case.
return from_ddl_datatype("struct<%s>" % s.strip())
except BaseException:
raise e
return _parse_datatype_json_string(
cast(JVMView, sc._jvm).org.apache.spark.sql.api.python.PythonSQLUtils.ddlToJson(s)
)


def _parse_datatype_json_string(json_string: str) -> DataType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ private[sql] object PythonSQLUtils extends Logging {
DataType.fromJson(json).asInstanceOf[StructType].toDDL
}

def ddlToJson(ddl: String): String = {
val dataType = try {
// DDL format, "fieldname datatype, fieldname datatype".
StructType.fromDDL(ddl)
} catch {
case e: Throwable =>
try {
// For backwards compatibility, "integer", "struct<fieldname: datatype>" and etc.
parseDataType(ddl)
} catch {
case _: Throwable =>
try {
// For backwards compatibility, "fieldname: datatype, fieldname: datatype" case.
parseDataType(s"struct<${ddl.trim}>")
} catch {
case _: Throwable =>
throw e
}
}
}
dataType.json
}

def unresolvedNamedLambdaVariable(name: String): Column =
Column(internal.UnresolvedNamedLambdaVariable.apply(name))

Expand Down
Loading