-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-53504][SQL] Type framework #51467
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
MaxGekk
wants to merge
26
commits into
apache:master
Choose a base branch
from
MaxGekk:type-framework-v1
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
26 commits
Select commit
Hold shift + click to select a range
eef5143
Initial implementation
MaxGekk 37e1751
Get Java classes
MaxGekk 579e32e
Modify CodeGenerator
MaxGekk 4215f92
Handle mutable values
MaxGekk b672cc2
Add LiteralTypeOps
MaxGekk 0f3bc55
Improve type checks
MaxGekk b1a4b0b
Merge remote-tracking branch 'origin/master' into type-framework-v1
MaxGekk 97f171e
Add the Encode interface
MaxGekk 8918964
Implement Apis specific interfaces
MaxGekk 7b788a9
Add APIs ops
MaxGekk 393c803
Implement Encode ops in RowEncoder
MaxGekk 55465ae
Fix formatting
MaxGekk fd12b90
Add FormatTypeOps
MaxGekk 5dfe0e6
Fix coding style
MaxGekk b032e6b
toJavaLiteral
MaxGekk 4183e8e
To SQL typed literal
MaxGekk f1e2264
Reuse the Format interface in ToStringBase
MaxGekk d35e2e3
Column type
MaxGekk dbf5ef4
Change HiveResult
MaxGekk d98676e
Make TimeTypeApiOps serializable
MaxGekk 3848b0b
External type ops
MaxGekk cdf01b7
Refactoring
MaxGekk 9d9bedc
Fix an issue
MaxGekk 8e1cdbd
Remove extra empty lines
MaxGekk 42bf232
Merge remote-tracking branch 'origin/master' into type-framework-v1
MaxGekk e03fbe5
Resolve conflicts
MaxGekk 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
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
36 changes: 36 additions & 0 deletions
36
sql/api/src/main/scala/org/apache/spark/sql/types/ops/EncodeTypeOps.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,36 @@ | ||
| /* | ||
| * 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.spark.sql.catalyst.encoders.AgnosticEncoder | ||
| import org.apache.spark.sql.types.{DataType, TimeType} | ||
|
|
||
| // Encode type values to external types, for instance to Java types. | ||
| trait EncodeTypeOps { | ||
| // Gets an agnostic encoder which contains all info needed to convert internal row | ||
| // values of the given type to a specific objects. | ||
| def getEncoder: AgnosticEncoder[_] | ||
| } | ||
|
|
||
| object EncodeTypeOps { | ||
| private val supportedDataTypes: Set[DataType] = | ||
| Set(TimeType.MIN_PRECISION to TimeType.MAX_PRECISION map TimeType.apply: _*) | ||
|
|
||
| def supports(dt: DataType): Boolean = supportedDataTypes.contains(dt) | ||
| def apply(dt: DataType): EncodeTypeOps = TypeApiOps(dt).asInstanceOf[EncodeTypeOps] | ||
| } |
37 changes: 37 additions & 0 deletions
37
sql/api/src/main/scala/org/apache/spark/sql/types/ops/FormatTypeOps.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,37 @@ | ||
| /* | ||
| * 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.spark.sql.types.{DataType, TimeType} | ||
|
|
||
| // Format type values to strings | ||
| trait FormatTypeOps { | ||
| // Formats times according to the pattern `HH:mm:ss.[..fff..]` where `[..fff..]` is a fraction | ||
| // of second up to microsecond resolution. It doesn't output trailing zeros in the fraction. | ||
| def format(v: Any): String | ||
| // Converts given value to a SQL typed literal | ||
| def toSQLValue(v: Any): String | ||
| } | ||
|
|
||
| object FormatTypeOps { | ||
| private val supportedDataTypes: Set[DataType] = | ||
| Set(TimeType.MIN_PRECISION to TimeType.MAX_PRECISION map TimeType.apply: _*) | ||
|
|
||
| def supports(dt: DataType): Boolean = supportedDataTypes.contains(dt) | ||
| def apply(dt: DataType): FormatTypeOps = TypeApiOps(dt).asInstanceOf[FormatTypeOps] | ||
| } |
45 changes: 45 additions & 0 deletions
45
sql/api/src/main/scala/org/apache/spark/sql/types/ops/TimeTypeApiOps.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,45 @@ | ||
| /* | ||
| * 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 java.time.LocalTime | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.catalyst.encoders.AgnosticEncoder | ||
| import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.LocalTimeEncoder | ||
| import org.apache.spark.sql.catalyst.util.TimeFormatter | ||
| import org.apache.spark.sql.types.TimeType | ||
|
|
||
| class TimeTypeApiOps(t: TimeType) | ||
| extends TypeApiOps | ||
| with EncodeTypeOps | ||
| with FormatTypeOps | ||
| with Serializable { | ||
| private lazy val fracFormatter = TimeFormatter.getFractionFormatter() | ||
|
|
||
| override def getEncoder: AgnosticEncoder[_] = LocalTimeEncoder | ||
|
|
||
| override def format(v: Any): String = v match { | ||
| case l: Long => fracFormatter.format(l) | ||
| case lt: LocalTime => fracFormatter.format(lt) | ||
| case other => | ||
| throw SparkException.internalError( | ||
| s"Unsupported external type ${other.getClass.getName} for the type ${t.sql}") | ||
| } | ||
| override def toSQLValue(v: Any): String = s"TIME '${format(v)}'" | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
sql/api/src/main/scala/org/apache/spark/sql/types/ops/TypeApiOps.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,34 @@ | ||
| /* | ||
| * 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.spark.SparkException | ||
| import org.apache.spark.sql.types.{DataType, TimeType} | ||
|
|
||
| // Operations over a data type | ||
| trait TypeApiOps | ||
|
|
||
| // The factory of type operation objects. | ||
| object TypeApiOps { | ||
| def apply(dt: DataType): TypeApiOps = dt match { | ||
| case tt: TimeType => new TimeTypeApiOps(tt) | ||
| case other => | ||
| throw SparkException.internalError( | ||
| s"Cannot create an operation object of the type ${other.sql}") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,8 +23,9 @@ import org.apache.spark.sql.catalyst.InternalRow | |
| import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.{BinaryEncoder, CalendarIntervalEncoder, NullEncoder, PrimitiveBooleanEncoder, PrimitiveByteEncoder, PrimitiveDoubleEncoder, PrimitiveFloatEncoder, PrimitiveIntEncoder, PrimitiveLongEncoder, PrimitiveShortEncoder, SparkDecimalEncoder, VariantEncoder} | ||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| import org.apache.spark.sql.catalyst.types.{PhysicalBinaryType, PhysicalIntegerType, PhysicalLongType} | ||
| import org.apache.spark.sql.catalyst.types.ops.PhyTypeOps | ||
| import org.apache.spark.sql.catalyst.util.{ArrayData, MapData} | ||
| import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteType, CalendarIntervalType, DataType, DateType, DayTimeIntervalType, Decimal, DecimalType, DoubleType, FloatType, GeographyType, GeometryType, IntegerType, LongType, MapType, ObjectType, ShortType, StringType, StructType, TimestampNTZType, TimestampType, TimeType, UserDefinedType, VariantType, YearMonthIntervalType} | ||
| import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteType, CalendarIntervalType, DataType, DateType, DayTimeIntervalType, Decimal, DecimalType, DoubleType, FloatType, GeographyType, GeometryType, IntegerType, LongType, MapType, ObjectType, ShortType, StringType, StructType, TimestampNTZType, TimestampType, UserDefinedType, VariantType, YearMonthIntervalType} | ||
| import org.apache.spark.unsafe.types.{CalendarInterval, GeographyVal, GeometryVal, UTF8String, VariantVal} | ||
|
|
||
| /** | ||
|
|
@@ -99,10 +100,10 @@ object EncoderUtils { | |
|
|
||
| def dataTypeJavaClass(dt: DataType): Class[_] = { | ||
| dt match { | ||
| case _ if PhyTypeOps.supports(dt) => PhyTypeOps(dt).getJavaClass | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future, we will get an Ops object here, and we will match by it instead of |
||
| case _: DecimalType => classOf[Decimal] | ||
| case _: DayTimeIntervalType => classOf[PhysicalLongType.InternalType] | ||
| case _: YearMonthIntervalType => classOf[PhysicalIntegerType.InternalType] | ||
| case _: TimeType => classOf[PhysicalLongType.InternalType] | ||
| case _: StringType => classOf[UTF8String] | ||
| case _: StructType => classOf[InternalRow] | ||
| case _: ArrayType => classOf[ArrayData] | ||
|
|
||
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.
nit. indentation?
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.
@dongjoon-hyun I have to violate the coding style because some GA fails and requires the formatting