-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-21912][SQL] ORC/Parquet table should not create invalid column names #19124
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
Changes from all commits
808dfe0
a738943
cd539fe
66aff54
aa78eaf
0bf3b43
79929e9
368b242
c70c03c
8ee87dd
c6e9ab6
46847f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * 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.datasources.orc | ||
|
|
||
| import org.apache.orc.TypeDescription | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| private[sql] object OrcFileFormat { | ||
| private def checkFieldName(name: String): Unit = { | ||
| try { | ||
| TypeDescription.fromString(s"struct<$name:int>") | ||
|
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. This seems being equal to call
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.
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. Oh, right, I forgot that is java...
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. Yep. I agree that it's a little urgly now. |
||
| } catch { | ||
| case _: IllegalArgumentException => | ||
| throw new AnalysisException( | ||
| s"""Column name "$name" contains invalid character(s). | ||
| |Please use alias to rename it. | ||
| """.stripMargin.split("\n").mkString(" ").trim) | ||
| } | ||
| } | ||
|
|
||
| def checkFieldNames(schema: StructType): StructType = { | ||
| schema.fieldNames.foreach(checkFieldName) | ||
| schema | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ struct<> | |
|
|
||
|
|
||
| -- !query 2 | ||
| CREATE TABLE showcolumn1 (col1 int, `col 2` int) USING parquet | ||
| CREATE TABLE showcolumn1 (col1 int, `col 2` int) USING json | ||
| -- !query 2 schema | ||
|
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. Please change it to JSON
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. Sure, thank you for the guide! |
||
| struct<> | ||
| -- !query 2 output | ||
|
|
@@ -35,7 +35,7 @@ struct<> | |
|
|
||
|
|
||
| -- !query 4 | ||
| CREATE TEMPORARY VIEW showColumn3 (col3 int, `col 4` int) USING parquet | ||
| CREATE TEMPORARY VIEW showColumn3 (col3 int, `col 4` int) USING json | ||
| -- !query 4 schema | ||
| struct<> | ||
| -- !query 4 output | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2000,4 +2000,38 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton { | |
| assert(setOfPath.size() == pathSizeToDeleteOnExit) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-21912 ORC/Parquet table should not create invalid column names") { | ||
| Seq(" ", ",", ";", "{", "}", "(", ")", "\n", "\t", "=").foreach { name => | ||
| withTable("t21912") { | ||
| Seq("ORC", "PARQUET").foreach { source => | ||
| val m = intercept[AnalysisException] { | ||
| sql(s"CREATE TABLE t21912(`col$name` INT) USING $source") | ||
| }.getMessage | ||
| assert(m.contains(s"contains invalid character(s)")) | ||
|
|
||
| val m2 = intercept[AnalysisException] { | ||
| sql(s"CREATE TABLE t21912 USING $source AS SELECT 1 `col$name`") | ||
| }.getMessage | ||
| assert(m2.contains(s"contains invalid character(s)")) | ||
|
|
||
| withSQLConf(HiveUtils.CONVERT_METASTORE_PARQUET.key -> "false") { | ||
| val m3 = intercept[AnalysisException] { | ||
| sql(s"CREATE TABLE t21912(`col$name` INT) USING hive OPTIONS (fileFormat '$source')") | ||
| }.getMessage | ||
| assert(m3.contains(s"contains invalid character(s)")) | ||
| } | ||
| } | ||
|
|
||
| // TODO: After SPARK-21929, we need to check ORC, too. | ||
| Seq("PARQUET").foreach { source => | ||
|
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. I added only |
||
| sql(s"CREATE TABLE t21912(`col` INT) USING $source") | ||
| val m = intercept[AnalysisException] { | ||
| sql(s"ALTER TABLE t21912 ADD COLUMNS(`col$name` INT)") | ||
| }.getMessage | ||
| assert(m.contains(s"contains invalid character(s)")) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
newSchemaalso containspartition schema. How about partition schema? Do we have the same limits on it?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.
It's okay. Inside
checkDataSchemaFieldNames, we only usestable.dataSchemalike the following.For the partition columns, we have been allowing the special characters.
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.
Could you add test cases and ensure the partitioning columns with special characters work?
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.
DDLSuite and HiveDDLSuite have them here.
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 think this PR passes the above two test cases, too.