From 7f0c1f3ddfe5e80453b0e34f7938824106a7e25e Mon Sep 17 00:00:00 2001 From: Jin Xing Date: Wed, 11 May 2022 22:28:58 +0800 Subject: [PATCH] [HUDI-4079] Supports showing table comment for hudi with spark3 (#5546) --- .../spark/sql/hudi/TestCreateTable.scala | 23 +++++++++++++++ .../sql/hudi/catalog/HoodieCatalog.scala | 28 ++++++++++--------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestCreateTable.scala b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestCreateTable.scala index e7910fa11585..69147272dabe 100644 --- a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestCreateTable.scala +++ b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestCreateTable.scala @@ -18,6 +18,7 @@ package org.apache.spark.sql.hudi import org.apache.hudi.DataSourceWriteOptions._ +import org.apache.hudi.HoodieSparkUtils import org.apache.hudi.common.model.HoodieRecord import org.apache.hudi.common.table.{HoodieTableConfig, HoodieTableMetaClient} import org.apache.hudi.config.HoodieWriteConfig @@ -641,4 +642,26 @@ class TestCreateTable extends HoodieSparkSqlTestBase { |""".stripMargin ) } + + if (HoodieSparkUtils.gteqSpark3_2) { + test("Test create table with comment") { + val tableName = generateTableName + spark.sql( + s""" + | create table $tableName ( + | id int, + | name string, + | price double, + | ts long + | ) using hudi + | comment "This is a simple hudi table" + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + val shown = spark.sql(s"show create table $tableName").head.getString(0) + assertResult(true)(shown.contains("COMMENT 'This is a simple hudi table'")) + } + } } diff --git a/hudi-spark-datasource/hudi-spark3/src/main/scala/org/apache/spark/sql/hudi/catalog/HoodieCatalog.scala b/hudi-spark-datasource/hudi-spark3/src/main/scala/org/apache/spark/sql/hudi/catalog/HoodieCatalog.scala index 82ea356215ca..5f4572dcc938 100644 --- a/hudi-spark-datasource/hudi-spark3/src/main/scala/org/apache/spark/sql/hudi/catalog/HoodieCatalog.scala +++ b/hudi-spark-datasource/hudi-spark3/src/main/scala/org/apache/spark/sql/hudi/catalog/HoodieCatalog.scala @@ -89,19 +89,21 @@ class HoodieCatalog extends DelegatingCatalogExtension } override def loadTable(ident: Identifier): Table = { - try { - super.loadTable(ident) match { - case v1: V1Table if sparkAdapter.isHoodieTable(v1.catalogTable) => - HoodieInternalV2Table( - spark, - v1.catalogTable.location.toString, - catalogTable = Some(v1.catalogTable), - tableIdentifier = Some(ident.toString)) - case o => o - } - } catch { - case e: Exception => - throw e + super.loadTable(ident) match { + case V1Table(catalogTable0) if sparkAdapter.isHoodieTable(catalogTable0) => + val catalogTable = catalogTable0.comment match { + case Some(v) => + val newProps = catalogTable0.properties + (TableCatalog.PROP_COMMENT -> v) + catalogTable0.copy(properties = newProps) + case _ => + catalogTable0 + } + HoodieInternalV2Table( + spark = spark, + path = catalogTable.location.toString, + catalogTable = Some(catalogTable), + tableIdentifier = Some(ident.toString)) + case o => o } }