-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-55441][SQL] Types Framework - Phase 1c - Client Integration #54905
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
davidm-db
wants to merge
19
commits into
apache:master
Choose a base branch
from
davidm-db:davidm-db/types_framework_1c
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a7eee13
supporting various clients thorugh types framework
davidm-db ff1263c
minor refactors + scalafmt
davidm-db 0d7cb57
scala style
davidm-db 58a74f9
styling
davidm-db 30b593a
more style
davidm-db 1d49fbd
more style
davidm-db ad7cdfd
more style
davidm-db e81ebf5
minor last style hopefully
davidm-db 6ba67e0
resolve self-review found issues
davidm-db 4d47d26
minor comment to clarify
davidm-db bc89339
scalastyle
davidm-db c694e5e
remove non-ascii leftover characters
davidm-db 510cb91
addressing cloud-fan's comments
davidm-db ca30590
addressing dejankrak's comments
davidm-db 3778dbf
build break fix
davidm-db 4051864
scalafmt
davidm-db bc61b04
merging connect ops into a single trait
davidm-db 9757da5
revert bad merge conflict resolution
davidm-db b7764bb
scalafmt
davidm-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
sql/api/src/main/scala/org/apache/spark/sql/types/ops/ClientTypeOps.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * 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.types.ops | ||
|
|
||
| import org.apache.arrow.vector.types.pojo.ArrowType | ||
|
|
||
| import org.apache.spark.sql.internal.SqlApiConf | ||
| import org.apache.spark.sql.types.{DataType, TimeType} | ||
|
|
||
| /** | ||
| * Optional client-side type operations for the Types Framework. | ||
| * | ||
| * This trait extends TypeApiOps with operations needed by client-facing infrastructure: Arrow | ||
| * conversion (ArrowUtils), Python interop (EvaluatePython), Hive formatting (HiveResult), and | ||
| * Thrift type mapping (SparkExecuteStatementOperation). | ||
| * | ||
| * Lives in sql/api so it's visible from sql/core and sql/hive-thriftserver. | ||
| * | ||
| * USAGE - integration points use ClientTypeOps(dt) which returns Option[ClientTypeOps]: | ||
| * {{{ | ||
| * // Forward lookup (most files): | ||
| * ClientTypeOps(dt).map(_.toArrowType(timeZoneId)).getOrElse { ... } | ||
| * | ||
| * // Reverse lookup (ArrowUtils.fromArrowType): | ||
| * ClientTypeOps.fromArrowType(at).getOrElse { ... } | ||
| * }}} | ||
| * | ||
| * @see | ||
| * TimeTypeApiOps for a reference implementation | ||
| * @since 4.2.0 | ||
| */ | ||
| trait ClientTypeOps { self: TypeApiOps => | ||
|
|
||
| // ==================== Utilities ==================== | ||
|
|
||
| /** | ||
| * Null-safe conversion helper. Returns null for null input, applies the partial function for | ||
| * non-null input, and returns null for unmatched values. | ||
| */ | ||
| protected def nullSafeConvert(input: Any)(f: PartialFunction[Any, Any]): Any = { | ||
| if (input == null) { | ||
| null | ||
| } else { | ||
| f.applyOrElse(input, (_: Any) => null) | ||
| } | ||
| } | ||
|
|
||
| // ==================== Arrow Conversion ==================== | ||
|
|
||
| /** | ||
| * Converts this DataType to its Arrow representation. | ||
| * | ||
| * Used by ArrowUtils.toArrowType. | ||
| * | ||
| * @param timeZoneId | ||
| * the session timezone (needed by some temporal types) | ||
| * @return | ||
| * the corresponding ArrowType | ||
| */ | ||
| def toArrowType(timeZoneId: String): ArrowType | ||
|
|
||
| // ==================== Python Interop ==================== | ||
|
|
||
| /** | ||
| * Returns true if values of this type need conversion when passed to/from Python. | ||
| * | ||
| * Used by EvaluatePython.needConversionInPython. | ||
| */ | ||
| def needConversionInPython: Boolean | ||
|
|
||
| /** | ||
| * Creates a converter function for Python/Py4J interop. | ||
| * | ||
| * Used by EvaluatePython.makeFromJava. The returned function handles null-safe conversion of | ||
| * Java/Py4J values to the internal Catalyst representation. | ||
| * | ||
| * @return | ||
| * a function that converts a Java value to the internal representation | ||
| */ | ||
| def makeFromJava: Any => Any | ||
|
|
||
| // ==================== Hive Formatting ==================== | ||
|
|
||
| /** | ||
| * Formats an external-type value for Hive output. | ||
| * | ||
| * Used by HiveResult.toHiveString. The input is an external-type value (e.g., | ||
| * java.time.LocalTime for TimeType), NOT the internal representation. Most types override this | ||
| * simple version. Types that need different formatting when nested (e.g., quoting) should | ||
| * override the 2-param overload instead. | ||
| */ | ||
| def formatExternal(value: Any): String | ||
|
|
||
| /** | ||
| * Formats an external-type value for Hive output with nesting context. Default delegates to the | ||
| * simple version. Override if nesting affects formatting. | ||
| */ | ||
| def formatExternal(value: Any, nested: Boolean): String = formatExternal(value) | ||
|
|
||
| // ==================== Thrift Mapping ==================== | ||
|
|
||
| /** | ||
| * Returns the Thrift TTypeId name for this type. | ||
| * | ||
| * Used by SparkExecuteStatementOperation.toTTypeId. Returns a String that maps to a TTypeId | ||
| * enum value (e.g., "STRING_TYPE") since TTypeId is only available in the hive-thriftserver | ||
| * module. | ||
| * | ||
| * @return | ||
| * TTypeId enum name (e.g., "STRING_TYPE") | ||
| */ | ||
| def thriftTypeName: String | ||
davidm-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Factory object for ClientTypeOps lookup. | ||
| * | ||
| * Delegates to TypeApiOps and narrows via collect to find implementations that mix in | ||
| * ClientTypeOps. | ||
| */ | ||
| object ClientTypeOps { | ||
|
|
||
| /** | ||
| * Returns a ClientTypeOps instance for the given DataType, if available. | ||
| * | ||
| * @param dt | ||
| * the DataType to get operations for | ||
| * @return | ||
| * Some(ClientTypeOps) if supported, None otherwise | ||
| */ | ||
| // Delegates to TypeApiOps and narrows: a type must implement TypeApiOps AND mix in | ||
| // ClientTypeOps to be found here. No separate registration needed - the collect | ||
| // filter handles incremental trait adoption automatically. | ||
| def apply(dt: DataType): Option[ClientTypeOps] = | ||
| TypeApiOps(dt).collect { case co: ClientTypeOps => co } | ||
davidm-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Reverse lookup: converts an Arrow type to a Spark DataType, if it belongs to a | ||
| * framework-managed type. | ||
| * | ||
| * Used by ArrowUtils.fromArrowType. Returns None if the Arrow type doesn't correspond to any | ||
| * framework-managed type, or the framework is disabled. | ||
| * | ||
| * @param at | ||
| * the ArrowType to convert | ||
| * @return | ||
| * Some(DataType) if recognized, None otherwise | ||
| */ | ||
| def fromArrowType(at: ArrowType): Option[DataType] = { | ||
davidm-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.apache.arrow.vector.types.TimeUnit | ||
| if (!SqlApiConf.get.typesFrameworkEnabled) return None | ||
| at match { | ||
| case t: ArrowType.Time if t.getUnit == TimeUnit.NANOSECOND && t.getBitWidth == 8 * 8 => | ||
| Some(TimeType(TimeType.MICROS_PRECISION)) | ||
| // Add new framework types here | ||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeApiOpsandClientTypeOpsare both for client side? shall we merge them into one? I'm ok to leave additional work for followup PRs, but not commit a draft version and rewrite it in followup.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. It's also true for
CatalystTypeOpsandTypeOpswho are both on the catalyst side. However, in both cases, one is optional and the other is mandatory. I thought of merging it like this to enable functionality, and follow-up with a refactor, where I would introduce the concept of optional functions (returningOptions) in the ops classes, since it's a framework design, rather than enabling functionality.If you think however that we should do it here, I can, I was just explaining my reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to go with optional methods (return None by default), let's do it now and abandon the optional trait approach.