forked from delta-io/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Peng Huo <penghuo@gmail.com>
- Loading branch information
Showing
20 changed files
with
2,367 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
72 changes: 72 additions & 0 deletions
72
core/src/main/scala/org/apache/spark/sql/delta/DeltaColumnMapping.scala
This file contains 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,72 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.delta | ||
|
||
import org.apache.spark.sql.delta.actions.Protocol | ||
import org.apache.spark.sql.delta.metering.DeltaLogging | ||
import org.apache.spark.sql.types.StructField | ||
|
||
import java.util.{Locale, UUID} | ||
|
||
trait DeltaColumnMappingBase extends DeltaLogging { | ||
val MIN_WRITER_VERSION = 5 | ||
val MIN_READER_VERSION = 2 | ||
val MIN_PROTOCOL_VERSION = Protocol(MIN_READER_VERSION, MIN_WRITER_VERSION) | ||
|
||
val PARQUET_FIELD_ID_METADATA_KEY = "parquet.field.id" | ||
val COLUMN_MAPPING_METADATA_PREFIX = "delta.columnMapping." | ||
val COLUMN_MAPPING_METADATA_ID_KEY = COLUMN_MAPPING_METADATA_PREFIX + "id" | ||
val COLUMN_MAPPING_PHYSICAL_NAME_KEY = COLUMN_MAPPING_METADATA_PREFIX + "physicalName" | ||
|
||
def generatePhysicalName: String = "col-" + UUID.randomUUID() | ||
|
||
def getPhysicalName(field: StructField): String = { | ||
if (field.metadata.contains(COLUMN_MAPPING_PHYSICAL_NAME_KEY)) { | ||
field.metadata.getString(COLUMN_MAPPING_PHYSICAL_NAME_KEY) | ||
} else { | ||
field.name | ||
} | ||
} | ||
} | ||
|
||
object DeltaColumnMapping extends DeltaColumnMappingBase | ||
|
||
/** | ||
* A trait for Delta column mapping modes. | ||
*/ | ||
sealed trait DeltaColumnMappingMode { | ||
def name: String | ||
} | ||
|
||
/** | ||
* No mapping mode uses a column's display name as its true identifier to | ||
* read and write data. | ||
* | ||
* This is the default mode and is the same mode as Delta always has been. | ||
*/ | ||
case object NoMapping extends DeltaColumnMappingMode { | ||
val name = "none" | ||
} | ||
|
||
object DeltaColumnMappingMode { | ||
def apply(name: String): DeltaColumnMappingMode = { | ||
name.toLowerCase(Locale.ROOT) match { | ||
case NoMapping.name => NoMapping | ||
case mode => throw DeltaErrors.unsupportedColumnMappingMode(mode) | ||
} | ||
} | ||
} |
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
core/src/main/scala/org/apache/spark/sql/delta/DeltaUDF.scala
This file contains 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,44 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.delta | ||
|
||
import org.apache.spark.sql.expressions.{SparkUserDefinedFunction, UserDefinedFunction} | ||
import org.apache.spark.sql.functions.udf | ||
|
||
object DeltaUDF { | ||
|
||
/** | ||
* A template for String => String udfs. It's used to create `SparkUserDefinedFunction` for | ||
* String => String udfs without touching Scala Reflection to reduce the log contention. | ||
*/ | ||
private lazy val stringStringUdfTemplate = | ||
udf[String, String]((x: String) => x).asInstanceOf[SparkUserDefinedFunction] | ||
|
||
private def createUdfFromTemplate[R, T]( | ||
template: SparkUserDefinedFunction, | ||
f: T => R): UserDefinedFunction = { | ||
template.copy( | ||
f = f, | ||
inputEncoders = template.inputEncoders.map(_.map(_.copy())), | ||
outputEncoder = template.outputEncoder.map(_.copy()) | ||
) | ||
} | ||
|
||
def stringStringUdf(f: String => String): UserDefinedFunction = { | ||
udf[String, String](f) | ||
} | ||
} |
This file contains 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 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.