-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[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
Draft
davidm-db
wants to merge
8
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.
+937
−114
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd6eeaf
supporting various clients thorugh types framework
davidm-db 6a17769
minor refactors + scalafmt
davidm-db 453e22f
scala style
davidm-db 3e6546f
styling
davidm-db a7ad71d
more style
davidm-db 775d5a3
more style
davidm-db 9fdf5c6
more style
davidm-db 98668b1
minor last style hopefully
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
193 changes: 193 additions & 0 deletions
193
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,193 @@ | ||
| /* | ||
| * 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), JDBC mapping (JdbcUtils), 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 | ||
|
|
||
| // ==================== JDBC Mapping ==================== | ||
|
|
||
| /** | ||
| * Returns the java.sql.Types constant for this type. | ||
| * | ||
| * Used by JdbcUtils.getCommonJDBCType for JDBC write path. | ||
| * | ||
| * @return | ||
| * java.sql.Types constant (e.g., java.sql.Types.TIME) | ||
| */ | ||
| def getJdbcType: Int | ||
|
|
||
| /** | ||
| * Returns the DDL type name string for this type. | ||
| * | ||
| * Used by JdbcUtils for CREATE TABLE DDL generation. | ||
| * | ||
| * @return | ||
| * DDL type string (e.g., "TIME") | ||
| */ | ||
| def jdbcTypeName: String | ||
|
|
||
| // ==================== 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. | ||
| * | ||
| * @param value | ||
| * the external-type value to format | ||
| * @return | ||
| * formatted string representation | ||
| */ | ||
| def formatExternal(value: Any): String | ||
|
|
||
| // ==================== 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.
Uh oh!
There was an error while loading. Please reload this page.