2323import org .apache .hudi .cli .utils .InputStreamConsumer ;
2424import org .apache .hudi .cli .utils .SparkUtil ;
2525import org .apache .hudi .common .table .HoodieTableMetaClient ;
26+ import org .apache .hudi .common .table .HoodieTableVersion ;
27+ import org .apache .hudi .common .util .StringUtils ;
2628
2729import org .apache .spark .launcher .SparkLauncher ;
2830import org .springframework .shell .core .CommandMarker ;
@@ -48,15 +50,16 @@ public String upgradeHoodieTable(
4850 HoodieTableMetaClient metaClient = HoodieCLI .getTableMetaClient ();
4951
5052 SparkLauncher sparkLauncher = SparkUtil .initLauncher (sparkPropertiesPath );
51- sparkLauncher .addAppArgs (SparkCommand .UPGRADE .toString (), master , sparkMemory , metaClient .getBasePath (), toVersion );
53+ String toVersionName = getHoodieTableVersionName (toVersion , true );
54+ sparkLauncher .addAppArgs (SparkCommand .UPGRADE .toString (), master , sparkMemory , metaClient .getBasePath (), toVersionName );
5255 Process process = sparkLauncher .launch ();
5356 InputStreamConsumer .captureOutput (process );
5457 int exitCode = process .waitFor ();
5558 HoodieCLI .refreshTableMetadata ();
5659 if (exitCode != 0 ) {
57- return String .format ("Failed: Could not Upgrade/Downgrade Hoodie table to \" %s\" ." , toVersion );
60+ return String .format ("Failed: Could not Upgrade/Downgrade Hoodie table to \" %s\" ." , toVersionName );
5861 }
59- return String .format ("Hoodie table upgraded/downgraded to %s" , toVersion );
62+ return String .format ("Hoodie table upgraded/downgraded to %s" , toVersionName );
6063 }
6164
6265 @ CliCommand (value = "downgrade table" , help = "Downgrades a table" )
@@ -70,14 +73,29 @@ public String downgradeHoodieTable(
7073
7174 HoodieTableMetaClient metaClient = HoodieCLI .getTableMetaClient ();
7275 SparkLauncher sparkLauncher = SparkUtil .initLauncher (sparkPropertiesPath );
73- sparkLauncher .addAppArgs (SparkCommand .DOWNGRADE .toString (), master , sparkMemory , metaClient .getBasePath (), toVersion );
76+ String toVersionName = getHoodieTableVersionName (toVersion , false );
77+ sparkLauncher .addAppArgs (SparkCommand .DOWNGRADE .toString (), master , sparkMemory , metaClient .getBasePath (), toVersionName );
7478 Process process = sparkLauncher .launch ();
7579 InputStreamConsumer .captureOutput (process );
7680 int exitCode = process .waitFor ();
7781 HoodieCLI .refreshTableMetadata ();
7882 if (exitCode != 0 ) {
79- return String .format ("Failed: Could not Upgrade/Downgrade Hoodie table to \" %s\" ." , toVersion );
83+ return String .format ("Failed: Could not Upgrade/Downgrade Hoodie table to \" %s\" ." , toVersionName );
84+ }
85+ return String .format ("Hoodie table upgraded/downgraded to %s" , toVersionName );
86+ }
87+
88+ static String getHoodieTableVersionName (String versionOption , boolean overrideWithDefault ) {
89+ if (StringUtils .isNullOrEmpty (versionOption ) && overrideWithDefault ) {
90+ return HoodieTableVersion .current ().name ();
91+ }
92+
93+ try {
94+ int versionCode = Integer .parseInt (versionOption );
95+ return HoodieTableVersion .versionFromCode (versionCode ).name ();
96+ } catch (NumberFormatException e ) {
97+ // The version option from the CLI is not a number, returns the original String
98+ return versionOption ;
8099 }
81- return String .format ("Hoodie table upgraded/downgraded to %s" , toVersion );
82100 }
83101}
0 commit comments