-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21769] [SQL] Add a table-specific option for always respecting schemas inferred/controlled by Spark SQL #19003
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 1 commit
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,50 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap | ||
|
|
||
| /** | ||
| * Options for the Parquet data source. | ||
| */ | ||
| class SourceOptions( | ||
| @transient private val parameters: CaseInsensitiveMap[String]) | ||
| extends Serializable { | ||
| import SourceOptions._ | ||
|
|
||
| def this(parameters: Map[String, String]) = this(CaseInsensitiveMap(parameters)) | ||
|
|
||
| // A flag to disable saving a data source table's metadata in hive compatible way. | ||
| val skipHiveMetadata: Boolean = parameters | ||
| .get(SKIP_HIVE_METADATA).map(_.toBoolean).getOrElse(DEFAULT_SKIP_HIVE_METADATA) | ||
|
|
||
| // A flag to always respect the Spark schema restored from the table properties | ||
| val respectSparkSchema: Boolean = parameters | ||
| .get(RESPECT_SPARK_SCHEMA).map(_.toBoolean).getOrElse(DEFAULT_RESPECT_SPARK_SCHEMA) | ||
| } | ||
|
|
||
|
|
||
| object SourceOptions { | ||
|
|
||
| val SKIP_HIVE_METADATA = "skipHiveMetadata" | ||
| val DEFAULT_SKIP_HIVE_METADATA = false | ||
|
|
||
| val RESPECT_SPARK_SCHEMA = "respectSparkSchema" | ||
| val DEFAULT_RESPECT_SPARK_SCHEMA = false | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -763,6 +763,47 @@ class VersionsSuite extends SparkFunSuite with Logging { | |
| } | ||
| } | ||
|
|
||
| test(s"$version: read avro file containing decimal") { | ||
| val url = Thread.currentThread().getContextClassLoader.getResource("avroDecimal") | ||
| val location = new File(url.getFile) | ||
|
|
||
| val tableName = "tab1" | ||
| val avroSchema = | ||
| """{ | ||
| | "name": "test_record", | ||
| | "type": "record", | ||
| | "fields": [ { | ||
| | "name": "f0", | ||
| | "type": [ | ||
| | "null", | ||
| | { | ||
| | "precision": 38, | ||
| | "scale": 2, | ||
| | "type": "bytes", | ||
| | "logicalType": "decimal" | ||
| | } | ||
| | ] | ||
| | } ] | ||
| |} | ||
| """.stripMargin | ||
| withTable(tableName) { | ||
| versionSpark.sql( | ||
| s""" | ||
| |CREATE TABLE $tableName | ||
| |ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' | ||
| |WITH SERDEPROPERTIES ('respectSparkSchema' = 'true') | ||
| |STORED AS | ||
| | INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' | ||
| | OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat' | ||
| |LOCATION '$location' | ||
| |TBLPROPERTIES ('avro.schema.literal' = '$avroSchema') | ||
|
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. For such an example that requires users setting
Contributor
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. There was an argument about whether we should add |
||
| """.stripMargin | ||
| ) | ||
| assert(versionSpark.table(tableName).collect() === | ||
| versionSpark.sql("SELECT 1.30").collect()) | ||
| } | ||
| } | ||
|
|
||
| // TODO: add more tests. | ||
| } | ||
| } | ||
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: update docs