Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-27964][SQL] Move v2 catalog update methods to CatalogV2Util#24813
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * 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.catalog.v2.utils | ||
| import java.util | ||
| import java.util.Collections | ||
| import scala.collection.JavaConverters._ | ||
| import org.apache.spark.sql.catalog.v2.TableChange | ||
| import org.apache.spark.sql.catalog.v2.TableChange.{AddColumn, DeleteColumn, RemoveProperty, RenameColumn, SetProperty, UpdateColumnComment, UpdateColumnType} | ||
| import org.apache.spark.sql.types.{StructField, StructType} | ||
| object CatalogV2Util { | ||
| /** | ||
| * Apply properties changes to a map and return the result. | ||
| */ | ||
| def applyPropertiesChanges( | ||
| properties: Map[String, String], | ||
| changes: Seq[TableChange]): Map[String, String] = { | ||
| applyPropertiesChanges(properties.asJava, changes).asScala.toMap | ||
| } | ||
| /** | ||
| * Apply properties changes to a Java map and return the result. | ||
| */ | ||
| def applyPropertiesChanges( | ||
| properties: util.Map[String, String], | ||
| changes: Seq[TableChange]): util.Map[String, String] = { | ||
| val newProperties = new util.HashMap[String, String](properties) | ||
| changes.foreach { | ||
| case set: SetProperty => | ||
| newProperties.put(set.property, set.value) | ||
| case unset: RemoveProperty => | ||
| newProperties.remove(unset.property) | ||
| case _ => | ||
| // ignore non-property changes | ||
| } | ||
| Collections.unmodifiableMap(newProperties) | ||
| } | ||
| /** | ||
| * Apply schema changes to a schema and return the result. | ||
| */ | ||
| def applySchemaChanges(schema: StructType, changes: Seq[TableChange]): StructType = { | ||
| changes.foldLeft(schema) { (schema, change) => | ||
| change match { | ||
| case add: AddColumn => | ||
| add.fieldNames match { | ||
| case Array(name) => | ||
| val newField = StructField(name, add.dataType, nullable = add.isNullable) | ||
| Option(add.comment) match { | ||
| case Some(comment) => | ||
| schema.add(newField.withComment(comment)) | ||
| case _ => | ||
| schema.add(newField) | ||
| } | ||
| case names => | ||
| replace(schema, names.init, parent => parent.dataType match { | ||
| case parentType: StructType => | ||
| val field = StructField(names.last, add.dataType, nullable = add.isNullable) | ||
| val newParentType = Option(add.comment) match { | ||
| case Some(comment) => | ||
| parentType.add(field.withComment(comment)) | ||
| case None => | ||
| parentType.add(field) | ||
| } | ||
| Some(StructField(parent.name, newParentType, parent.nullable, parent.metadata)) | ||
| case _ => | ||
| throw new IllegalArgumentException(s"Not a struct: ${names.init.last}") | ||
| }) | ||
| } | ||
| case rename: RenameColumn => | ||
| replace(schema, rename.fieldNames, field => | ||
| Some(StructField(rename.newName, field.dataType, field.nullable, field.metadata))) | ||
| case update: UpdateColumnType => | ||
| replace(schema, update.fieldNames, field => { | ||
| if (!update.isNullable && field.nullable) { | ||
| throw new IllegalArgumentException( | ||
| s"Cannot change optional column to required: $field.name") | ||
| } | ||
| Some(StructField(field.name, update.newDataType, update.isNullable, field.metadata)) | ||
| }) | ||
| case update: UpdateColumnComment => | ||
| replace(schema, update.fieldNames, field => | ||
| Some(field.withComment(update.newComment))) | ||
| case delete: DeleteColumn => | ||
| replace(schema, delete.fieldNames, _ => None) | ||
| case _ => | ||
| // ignore non-schema changes | ||
| schema | ||
| } | ||
| } | ||
| } | ||
| private def replace( | ||
| struct: StructType, | ||
| fieldNames: Seq[String], | ||
Member 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. Thank you for the update. (This field name is modified based on the recent comment.) | ||
| update: StructField => Option[StructField]): StructType = { | ||
| val pos = struct.getFieldIndex(fieldNames.head) | ||
| .getOrElse(throw new IllegalArgumentException(s"Cannot find field: ${fieldNames.head}")) | ||
| val field = struct.fields(pos) | ||
| val replacement: Option[StructField] = if (fieldNames.tail.isEmpty) { | ||
| update(field) | ||
| } else { | ||
| field.dataType match { | ||
| case nestedStruct: StructType => | ||
| val updatedType: StructType = replace(nestedStruct, fieldNames.tail, update) | ||
| Some(StructField(field.name, updatedType, field.nullable, field.metadata)) | ||
| case _ => | ||
| throw new IllegalArgumentException(s"Not a struct: ${fieldNames.head}") | ||
| } | ||
| } | ||
| val newFields = struct.fields.zipWithIndex.flatMap { | ||
| case (_, index) if pos == index => | ||
| replacement | ||
| case (other, _) => | ||
| Some(other) | ||
| } | ||
| new StructType(newFields) | ||
| } | ||
| } | ||
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.
This is a newly added function during migration.