-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[HUDI-5000] Support schema evolution for Hive/presto #6989
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
07da7c6
[HUDI-5000] Support schema evolution for Hive
8faa0e3
[HUDI-5000] Support schema evolution for Hive/presto
8ef5a3d
Merge remote-tracking branch 'upstream/master'
6287b2c
[HUDI-5000] Support schema evolution for Hive/presto
11d8108
[HUDI-5000] Support schema evolution for Hive/presto
e47fda7
[HUDI-5000] Support schema evolution for Hive/presto
edb0ca1
[HUDI-5000] Support schema evolution for Hive/presto
c65ba71
Merge branch 'master' of https://github.com/apache/hudi
ce351e5
[HUDI-5000] Support schema evolution for Hive/presto
d76cb8d
[HUDI-5000] Support schema evolution for Hive/presto
552ad3d
[HUDI-5000] Support schema evolution for Hive/presto
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
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
353 changes: 353 additions & 0 deletions
353
hudi-hadoop-mr/src/main/java/org/apache/hudi/hadoop/SchemaEvolutionContext.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
155 changes: 155 additions & 0 deletions
155
...rce/hudi-spark/src/test/java/org/apache/hudi/functional/TestHiveTableSchemaEvolution.java
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,155 @@ | ||
/* | ||
* 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.hudi.functional; | ||
|
||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat; | ||
import org.apache.hadoop.hive.serde.serdeConstants; | ||
import org.apache.hadoop.hive.serde2.ColumnProjectionUtils; | ||
import org.apache.hadoop.mapred.FileInputFormat; | ||
import org.apache.hadoop.mapred.InputSplit; | ||
import org.apache.hadoop.mapred.JobConf; | ||
import org.apache.hadoop.mapred.RecordReader; | ||
import org.apache.hudi.HoodieSparkUtils; | ||
import org.apache.hudi.common.fs.FSUtils; | ||
import org.apache.hudi.hadoop.HoodieParquetInputFormat; | ||
import org.apache.hudi.hadoop.SchemaEvolutionContext; | ||
import org.apache.hudi.hadoop.realtime.HoodieEmptyRecordReader; | ||
import org.apache.hudi.hadoop.realtime.HoodieRealtimeRecordReader; | ||
import org.apache.hudi.hadoop.realtime.RealtimeCompactedRecordReader; | ||
import org.apache.hudi.hadoop.realtime.RealtimeSplit; | ||
import org.apache.spark.SparkConf; | ||
import org.apache.spark.sql.SparkSession; | ||
import org.apache.spark.sql.hudi.HoodieSparkSessionExtension; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.uber.hoodie.hadoop.realtime.HoodieRealtimeInputFormat; | ||
|
||
import java.io.File; | ||
import java.util.Date; | ||
|
||
@Tag("functional") | ||
public class TestHiveTableSchemaEvolution { | ||
|
||
private SparkSession sparkSession = null; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
initSparkContexts("HiveSchemaEvolution"); | ||
} | ||
|
||
private void initSparkContexts(String appName) { | ||
SparkConf sparkConf = new SparkConf(); | ||
if (HoodieSparkUtils.gteqSpark3_2()) { | ||
sparkConf.set("spark.sql.catalog.spark_catalog", | ||
"org.apache.spark.sql.hudi.catalog.HoodieCatalog"); | ||
} | ||
sparkSession = SparkSession.builder().appName(appName) | ||
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") | ||
.withExtensions(new HoodieSparkSessionExtension()) | ||
.config("hoodie.insert.shuffle.parallelism", "4") | ||
.config("hoodie.upsert.shuffle.parallelism", "4") | ||
.config("hoodie.delete.shuffle.parallelism", "4") | ||
.config("hoodie.support.write.lock", "false") | ||
.config("spark.sql.session.timeZone", "CTT") | ||
.config("spark.sql.hive.convertMetastoreParquet", "false") | ||
.config(sparkConf) | ||
.master("local[1]").getOrCreate(); | ||
sparkSession.sparkContext().setLogLevel("ERROR"); | ||
} | ||
|
||
@Test | ||
public void testCopyOnWriteTableForHive() throws Exception { | ||
String tableName = "huditest" + new Date().getTime(); | ||
File file = new File(System.getProperty("java.io.tmpdir") + tableName); | ||
if (HoodieSparkUtils.gteqSpark3_1()) { | ||
sparkSession.sql("set hoodie.schema.on.read.enable=true"); | ||
String path = new Path(file.getCanonicalPath()).toUri().toString(); | ||
sparkSession.sql("create table " + tableName + "(col0 int, col1 float, col2 string) using hudi options(type='cow', primaryKey='col0', preCombineField='col1') location '" + path + "'"); | ||
sparkSession.sql("insert into " + tableName + " values(1, 1.1, 'text')"); | ||
sparkSession.sql("alter table " + tableName + " alter column col1 type double"); | ||
sparkSession.sql("alter table " + tableName + " rename column col2 to aaa"); | ||
|
||
HoodieParquetInputFormat inputFormat = new HoodieParquetInputFormat(); | ||
JobConf jobConf = new JobConf(); | ||
inputFormat.setConf(jobConf); | ||
FileInputFormat.setInputPaths(jobConf, path); | ||
InputSplit[] splits = inputFormat.getSplits(jobConf, 1); | ||
assertEvolutionResult("cow", splits[0], jobConf); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMergeOnReadTableForHive() throws Exception { | ||
String tableName = "huditest" + new Date().getTime(); | ||
File file = new File(System.getProperty("java.io.tmpdir") + tableName); | ||
if (HoodieSparkUtils.gteqSpark3_1()) { | ||
sparkSession.sql("set hoodie.schema.on.read.enable=true"); | ||
String path = new Path(file.getCanonicalPath()).toUri().toString(); | ||
sparkSession.sql("create table " + tableName + "(col0 int, col1 float, col2 string) using hudi options(type='cow', primaryKey='col0', preCombineField='col1') location '" + path + "'"); | ||
sparkSession.sql("insert into " + tableName + " values(1, 1.1, 'text')"); | ||
sparkSession.sql("insert into " + tableName + " values(2, 1.2, 'text2')"); | ||
sparkSession.sql("alter table " + tableName + " alter column col1 type double"); | ||
sparkSession.sql("alter table " + tableName + " rename column col2 to aaa"); | ||
|
||
HoodieRealtimeInputFormat inputFormat = new HoodieRealtimeInputFormat(); | ||
JobConf jobConf = new JobConf(); | ||
inputFormat.setConf(jobConf); | ||
FileInputFormat.setInputPaths(jobConf, path); | ||
InputSplit[] splits = inputFormat.getSplits(jobConf, 1); | ||
assertEvolutionResult("mor", splits[0], jobConf); | ||
} | ||
} | ||
|
||
private void assertEvolutionResult(String tableType, InputSplit split, JobConf jobConf) throws Exception { | ||
jobConf.set(ColumnProjectionUtils.READ_COLUMN_NAMES_CONF_STR, "col1,aaa"); | ||
jobConf.set(ColumnProjectionUtils.READ_COLUMN_IDS_CONF_STR, "6,7"); | ||
jobConf.set(serdeConstants.LIST_COLUMNS, "_hoodie_commit_time,_hoodie_commit_seqno," | ||
+ "_hoodie_record_key,_hoodie_partition_path,_hoodie_file_name,col0,col1,aaa"); | ||
jobConf.set(serdeConstants.LIST_COLUMN_TYPES, "string,string,string,string,string,int,double,string"); | ||
|
||
SchemaEvolutionContext schemaEvolutionContext = new SchemaEvolutionContext(split, jobConf); | ||
if ("cow".equals(tableType)) { | ||
schemaEvolutionContext.doEvolutionForParquetFormat(); | ||
} else { | ||
// mot table | ||
RealtimeSplit realtimeSplit = (RealtimeSplit) split; | ||
RecordReader recordReader; | ||
// for log only split, set the parquet reader as empty. | ||
if (FSUtils.isLogFile(realtimeSplit.getPath())) { | ||
recordReader = new HoodieRealtimeRecordReader(realtimeSplit, jobConf, new HoodieEmptyRecordReader(realtimeSplit, jobConf)); | ||
} else { | ||
// create a RecordReader to be used by HoodieRealtimeRecordReader | ||
recordReader = new MapredParquetInputFormat().getRecordReader(realtimeSplit, jobConf, null); | ||
} | ||
RealtimeCompactedRecordReader realtimeCompactedRecordReader = new RealtimeCompactedRecordReader(realtimeSplit, jobConf, recordReader); | ||
// mor table also run with doEvolutionForParquetFormat in HoodieParquetInputFormat | ||
schemaEvolutionContext.doEvolutionForParquetFormat(); | ||
schemaEvolutionContext.doEvolutionForRealtimeInputFormat(realtimeCompactedRecordReader); | ||
} | ||
|
||
assertEquals(jobConf.get(ColumnProjectionUtils.READ_COLUMN_NAMES_CONF_STR), "col1,col2"); | ||
assertEquals(jobConf.get(serdeConstants.LIST_COLUMNS), "_hoodie_commit_time,_hoodie_commit_seqno," | ||
+ "_hoodie_record_key,_hoodie_partition_path,_hoodie_file_name,col0,col1,col2"); | ||
assertEquals(jobConf.get(serdeConstants.LIST_COLUMN_TYPES), "string,string,string,string,string,int,double,string"); | ||
} | ||
} |
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
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.
Do we need to fallback, if schemEvolution failed
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 don't think it's worth paying attention, because here only a runtime exception is thrown when schema evolution is needed and the evolution fails.