-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16628][SQL] Translate file-based relation schema when file schema is inconsistent with catalog schema #14365
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9fab59
Fix the possible inconsistency between file schema and catalog schema…
viirya cc953a3
Fix test with correct require condition.
viirya 0c17748
Add document.
viirya 5cfdff1
Merge remote-tracking branch 'upstream/master' into fix-file-ds-incon…
viirya 4125e6f
Do schema translation in OrcFileFormat.
viirya 9cbb406
Merge remote-tracking branch 'upstream/master' into fix-file-ds-incon…
viirya 2bb8368
Refine code comment.
viirya 141cb1d
Add more test.
viirya 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
80 changes: 80 additions & 0 deletions
80
sql/core/src/main/scala/org/apache/spark/sql/execution/SchemaMapping.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,80 @@ | ||
| /* | ||
| * 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.execution | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression} | ||
| import org.apache.spark.sql.types.{StructField, StructType} | ||
|
|
||
| /** | ||
| * An interface for mapping two different schemas. For the relations that have are backed by files, | ||
| * the inferred schema from the files might be different with the schema stored in the catalog. In | ||
| * such case, the interface helps mapping inconsistent schemas. | ||
| */ | ||
| private[sql] trait SchemaMapping { | ||
| /** The schema inferred from the files. */ | ||
| val dataSchema: StructType | ||
|
|
||
| /** The schema used in partition. */ | ||
| val partitionSchema: StructType | ||
|
|
||
| /** The schema fetched from the catalog. */ | ||
| val catalogSchema: StructType | ||
|
|
||
| require(catalogSchema.length == 0 || | ||
| dataSchema.merge(partitionSchema).length == catalogSchema.length, | ||
| s"The data schema in files: $dataSchema plus the partition schema: $partitionSchema " + | ||
| s"should have the same number of fields with the schema in catalog: $catalogSchema.") | ||
|
|
||
| /** Returns the correspond catalog field for the given data field. */ | ||
| def lookForFieldFromDataField(field: StructField): Option[StructField] = { | ||
| if (catalogSchema.fields.length == 0) { | ||
| None | ||
| } else { | ||
| dataSchema.getFieldIndex(field.name).map { idx => | ||
| catalogSchema.fields(idx) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Returns the correspond data field for the given catalog field. */ | ||
| def lookForFieldFromCatalogField(field: StructField): Option[StructField] = { | ||
| catalogSchema.getFieldIndex(field.name).map { idx => | ||
| dataSchema.fields(idx) | ||
| } | ||
| } | ||
|
|
||
| /** Returns the correspond data field for the given catalog field. */ | ||
| def lookForFieldFromCatalogField(fieldName: String): Option[StructField] = { | ||
| catalogSchema.getFieldIndex(fieldName).map { idx => | ||
| dataSchema.fields(idx) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Transforms the attributes in the given expression which is based on the catalog schema | ||
| * to corresponding attributes in the schema in the files. | ||
| */ | ||
| def transformExpressionToUseDataSchema(expr: Expression): Expression = { | ||
| expr transform { | ||
| case a: AttributeReference => | ||
| lookForFieldFromCatalogField(a.name).map { field => | ||
| a.withName(field.name) | ||
| }.getOrElse(a) | ||
| } | ||
| } | ||
| } | ||
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
80 changes: 80 additions & 0 deletions
80
sql/core/src/test/scala/org/apache/spark/sql/execution/SchemaMappingSuite.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,80 @@ | ||
| /* | ||
| * 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.execution | ||
|
|
||
| import org.apache.spark.{SparkException, SparkFunSuite} | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, EqualTo, Literal} | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| case class SchemaMappingRelation( | ||
| val dataSchema: StructType, | ||
| val partitionSchema: StructType, | ||
| val catalogSchema: StructType) extends SchemaMapping | ||
|
|
||
| class SchemaMappingSuite extends SparkFunSuite { | ||
|
|
||
| val dataSchema = StructType( | ||
| StructField("_col1", IntegerType) :: | ||
| StructField("_col2", LongType) :: | ||
| StructField("_col3", BooleanType) :: Nil) | ||
|
|
||
| val partitionSchema = StructType( | ||
| StructField("part", IntegerType) :: Nil) | ||
|
|
||
| val catalogSchema = StructType( | ||
| StructField("f1", IntegerType) :: | ||
| StructField("f2", LongType) :: | ||
| StructField("f3", BooleanType) :: | ||
| StructField("part", IntegerType) :: Nil) | ||
|
|
||
| val relation = SchemaMappingRelation(dataSchema, partitionSchema, catalogSchema) | ||
|
|
||
| test("looking for data schema field with given catalog field name") { | ||
| val col1 = relation.lookForFieldFromCatalogField("f1").get | ||
| assert(col1.name == "_col1" && col1.dataType == IntegerType) | ||
|
|
||
| val col2 = relation.lookForFieldFromCatalogField("f2").get | ||
| assert(col2.name == "_col2" && col2.dataType == LongType) | ||
|
|
||
| val col3 = relation.lookForFieldFromCatalogField("f3").get | ||
| assert(col3.name == "_col3" && col3.dataType == BooleanType) | ||
|
|
||
| assert(relation.lookForFieldFromCatalogField("f4").isEmpty) | ||
| } | ||
|
|
||
| test("relation with empty catalog schema") { | ||
| val relationWithoutCatalogSchema = SchemaMappingRelation(dataSchema, | ||
| partitionSchema, new StructType()) | ||
| assert(relationWithoutCatalogSchema.lookForFieldFromCatalogField("f1").isEmpty) | ||
| } | ||
|
|
||
| test("data schema must match catalog schema in length if catalog schema is not empty") { | ||
| val catalogSchema = StructType(StructField("f1", IntegerType) :: Nil) | ||
| val e = intercept[RuntimeException] { | ||
| SchemaMappingRelation(dataSchema, partitionSchema, catalogSchema) | ||
| } | ||
| assert(e.getMessage.contains("should have the same number of fields")) | ||
| } | ||
|
|
||
| test("transform expression of catalog schema fields to use data schema fields") { | ||
| val attr = AttributeReference("f1", IntegerType)() | ||
| val expr = EqualTo(attr, Literal(1)) | ||
| val expected = EqualTo(attr.withName("_col1"), Literal(1)) | ||
| assert(relation.transformExpressionToUseDataSchema(expr) == expected) | ||
| } | ||
| } |
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
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.
Can you put the detailed description of this mapping in the doc here? thanks.
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.
I've added more detailed document. Please take a look. Thanks.