Peng-Lei commented on a change in pull request #32931: URL: https://github.com/apache/spark/pull/32931#discussion_r653997704
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") Review comment: @yaooqinn ,If the view is temp view, it will throw AnalysisException. Becauce when call visitShowCreateTable, temp view is not allowed. `override def visitShowCreateTable(ctx: ShowCreateTableContext): LogicalPlan = withOrigin(ctx) { ShowCreateTable( UnresolvedTableOrView( visitMultipartIdentifier(ctx.multipartIdentifier()), allowTempView = false), ctx.SERDE != null) }` I'm not sure how to tell the view is permanent view ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") Review comment: @yaooqinn ,If the view is temp view, it will throw AnalysisException. Becauce when call visitShowCreateTable, temp view is not allowed. `override def visitShowCreateTable(ctx: ShowCreateTableContext): LogicalPlan = withOrigin(ctx) { ShowCreateTable( UnresolvedTableOrView( visitMultipartIdentifier(ctx.multipartIdentifier()), allowTempView = false), ctx.SERDE != null) }` I'm not sure how to tell the view is permanent view ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") Review comment: @yaooqinn ,If the view is temp view, it will throw AnalysisException. Becauce when call visitShowCreateTable, temp view is not allowed. ` UnresolvedTableOrView( visitMultipartIdentifier(ctx.multipartIdentifier()), allowTempView = false), ctx.SERDE != null) ` I'm not sure how to tell the view is permanent view ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") + } else { + if (asSerde) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for v2 tables of" + + " Hive Serde table to generate the Hive DDL.") + } else { + // it is used to generate Spark DDL for given table. include Hive Serde table + showCreateTable(table, builder) + } + } + + Seq(InternalRow(UTF8String.fromString(builder.toString))) + } + + private def showCreateTable(table: Table, builder: StringBuilder): Unit = { + builder ++= s"CREATE TABLE ${table.name()}" + val isHive = DDLUtils.isHiveTable(Option(table.properties.get(TableCatalog.PROP_PROVIDER))) + if (isHive) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for Hive serde table" + + " to generate the Spark DDL.") + } + showTableDataColumns(table, builder) + showTableUsing(table, builder) + showTableOptions(table, builder) + showTableNonDataColumns(table, builder) + showTableComment(table, builder) + showTableLocation(table, builder) + showTableProperties(table, builder) + } + + private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = { + val columns = table.schema().fields.map(_.toDDL) + builder ++= concatByMultiLines(columns) + } + + private def showTableUsing(table: Table, builder: StringBuilder): Unit = { + if (table.properties.containsKey(TableCatalog.PROP_PROVIDER)) { + builder ++= + s"USING ${escapeSingleQuotedString(table.properties().get(TableCatalog.PROP_PROVIDER))}\n" + } + } + + private def showTableOptions(table: Table, builder: StringBuilder): Unit = { + import scala.collection.JavaConverters._ + val dataSourceOptions = table.properties.asScala + .filterKeys(_.startsWith(TableCatalog.OPTION_PREFIX)) + if (dataSourceOptions.nonEmpty) { + val props = dataSourceOptions.map { case (key, value) => + s"'${escapeSingleQuotedString(key)}' = '${escapeSingleQuotedString(value)}'" + } + + builder ++= "OPTIONS " + builder ++= concatByMultiLines(props) + } + } + + private def showTableNonDataColumns(table: Table, builder: StringBuilder): Unit = { + if (!table.partitioning.isEmpty) { + val identityCols = new mutable.ArrayBuffer[String] + var bucketSpec = Option.empty[BucketSpec] + + table.partitioning.map { + case IdentityTransform(FieldReference(Seq(col))) => + identityCols += col + case t : BucketTransform => + bucketSpec = Some(BucketSpec(t.numBuckets.value, t.columns.map(col => col.toString), Nil)) + case transform => Review comment: @sunchao when we create table using sql like ` CREATE TABLE t (a INT, b STRING, ts TIMESTAMP) USING parquet PARTITIONED BY ( a, bucket(16, b), years(ts), months(ts), days(ts), hours(ts) ` `bucket(16, b)` can parse to `BucketTransform` , we should show the ddl like `PARTITION BY ( bucket(16,b)) ` or `CLUSTERED BY (b) INTO 16 BUCKETS` ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") + } else { + if (asSerde) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for v2 tables of" + + " Hive Serde table to generate the Hive DDL.") + } else { + // it is used to generate Spark DDL for given table. include Hive Serde table + showCreateTable(table, builder) + } + } + + Seq(InternalRow(UTF8String.fromString(builder.toString))) + } + + private def showCreateTable(table: Table, builder: StringBuilder): Unit = { + builder ++= s"CREATE TABLE ${table.name()}" + val isHive = DDLUtils.isHiveTable(Option(table.properties.get(TableCatalog.PROP_PROVIDER))) + if (isHive) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for Hive serde table" + + " to generate the Spark DDL.") + } + showTableDataColumns(table, builder) + showTableUsing(table, builder) + showTableOptions(table, builder) + showTableNonDataColumns(table, builder) + showTableComment(table, builder) + showTableLocation(table, builder) + showTableProperties(table, builder) + } + + private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = { + val columns = table.schema().fields.map(_.toDDL) + builder ++= concatByMultiLines(columns) + } + + private def showTableUsing(table: Table, builder: StringBuilder): Unit = { + if (table.properties.containsKey(TableCatalog.PROP_PROVIDER)) { + builder ++= + s"USING ${escapeSingleQuotedString(table.properties().get(TableCatalog.PROP_PROVIDER))}\n" + } + } + + private def showTableOptions(table: Table, builder: StringBuilder): Unit = { + import scala.collection.JavaConverters._ + val dataSourceOptions = table.properties.asScala + .filterKeys(_.startsWith(TableCatalog.OPTION_PREFIX)) + if (dataSourceOptions.nonEmpty) { + val props = dataSourceOptions.map { case (key, value) => + s"'${escapeSingleQuotedString(key)}' = '${escapeSingleQuotedString(value)}'" + } + + builder ++= "OPTIONS " + builder ++= concatByMultiLines(props) + } + } + + private def showTableNonDataColumns(table: Table, builder: StringBuilder): Unit = { + if (!table.partitioning.isEmpty) { + val identityCols = new mutable.ArrayBuffer[String] + var bucketSpec = Option.empty[BucketSpec] + + table.partitioning.map { + case IdentityTransform(FieldReference(Seq(col))) => + identityCols += col + case t : BucketTransform => + bucketSpec = Some(BucketSpec(t.numBuckets.value, t.columns.map(col => col.toString), Nil)) + case transform => Review comment: @sunchao when we create table using sql like `CREATE TABLE t (a INT, b STRING, ts TIMESTAMP) USING parquet` ` PARTITIONED BY ( ` ` a, ` ` bucket(16, b), ` ` years(ts), ` ` months(ts), ` ` days(ts), ` ` hours(ts) ` `bucket(16, b)` can parse to `BucketTransform` , we should show the ddl like `PARTITION BY ( bucket(16,b)) ` or `CLUSTERED BY (b) INTO 16 BUCKETS` ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") + } else { + if (asSerde) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for v2 tables of" + + " Hive Serde table to generate the Hive DDL.") + } else { + // it is used to generate Spark DDL for given table. include Hive Serde table + showCreateTable(table, builder) + } + } + + Seq(InternalRow(UTF8String.fromString(builder.toString))) + } + + private def showCreateTable(table: Table, builder: StringBuilder): Unit = { + builder ++= s"CREATE TABLE ${table.name()}" + val isHive = DDLUtils.isHiveTable(Option(table.properties.get(TableCatalog.PROP_PROVIDER))) + if (isHive) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for Hive serde table" + + " to generate the Spark DDL.") + } + showTableDataColumns(table, builder) + showTableUsing(table, builder) + showTableOptions(table, builder) + showTableNonDataColumns(table, builder) + showTableComment(table, builder) + showTableLocation(table, builder) + showTableProperties(table, builder) + } + + private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = { + val columns = table.schema().fields.map(_.toDDL) + builder ++= concatByMultiLines(columns) + } + + private def showTableUsing(table: Table, builder: StringBuilder): Unit = { + if (table.properties.containsKey(TableCatalog.PROP_PROVIDER)) { + builder ++= + s"USING ${escapeSingleQuotedString(table.properties().get(TableCatalog.PROP_PROVIDER))}\n" + } + } + + private def showTableOptions(table: Table, builder: StringBuilder): Unit = { + import scala.collection.JavaConverters._ + val dataSourceOptions = table.properties.asScala + .filterKeys(_.startsWith(TableCatalog.OPTION_PREFIX)) + if (dataSourceOptions.nonEmpty) { + val props = dataSourceOptions.map { case (key, value) => + s"'${escapeSingleQuotedString(key)}' = '${escapeSingleQuotedString(value)}'" + } + + builder ++= "OPTIONS " + builder ++= concatByMultiLines(props) + } + } + + private def showTableNonDataColumns(table: Table, builder: StringBuilder): Unit = { + if (!table.partitioning.isEmpty) { + val identityCols = new mutable.ArrayBuffer[String] + var bucketSpec = Option.empty[BucketSpec] + + table.partitioning.map { + case IdentityTransform(FieldReference(Seq(col))) => + identityCols += col + case t : BucketTransform => + bucketSpec = Some(BucketSpec(t.numBuckets.value, t.columns.map(col => col.toString), Nil)) + case transform => Review comment: @sunchao when we create table using sql like `CREATE TABLE t (a INT, b STRING, ts TIMESTAMP) USING parquet` ` PARTITIONED BY ( ` ` a, ` ` bucket(16, b), ` ` years(ts), ` ` months(ts), ` ` days(ts), ` ` hours(ts) ) ` `bucket(16, b)` can parse to `BucketTransform` , we should show the ddl like `PARTITION BY ( bucket(16,b)) ` or `CLUSTERED BY (b) INTO 16 BUCKETS` ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") + } else { + if (asSerde) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for v2 tables of" + + " Hive Serde table to generate the Hive DDL.") + } else { + // it is used to generate Spark DDL for given table. include Hive Serde table + showCreateTable(table, builder) + } + } + + Seq(InternalRow(UTF8String.fromString(builder.toString))) + } + + private def showCreateTable(table: Table, builder: StringBuilder): Unit = { + builder ++= s"CREATE TABLE ${table.name()}" + val isHive = DDLUtils.isHiveTable(Option(table.properties.get(TableCatalog.PROP_PROVIDER))) + if (isHive) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for Hive serde table" + + " to generate the Spark DDL.") + } + showTableDataColumns(table, builder) + showTableUsing(table, builder) + showTableOptions(table, builder) + showTableNonDataColumns(table, builder) + showTableComment(table, builder) + showTableLocation(table, builder) + showTableProperties(table, builder) + } + + private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = { + val columns = table.schema().fields.map(_.toDDL) + builder ++= concatByMultiLines(columns) + } + + private def showTableUsing(table: Table, builder: StringBuilder): Unit = { + if (table.properties.containsKey(TableCatalog.PROP_PROVIDER)) { + builder ++= + s"USING ${escapeSingleQuotedString(table.properties().get(TableCatalog.PROP_PROVIDER))}\n" + } + } + + private def showTableOptions(table: Table, builder: StringBuilder): Unit = { + import scala.collection.JavaConverters._ + val dataSourceOptions = table.properties.asScala + .filterKeys(_.startsWith(TableCatalog.OPTION_PREFIX)) + if (dataSourceOptions.nonEmpty) { + val props = dataSourceOptions.map { case (key, value) => + s"'${escapeSingleQuotedString(key)}' = '${escapeSingleQuotedString(value)}'" + } + + builder ++= "OPTIONS " + builder ++= concatByMultiLines(props) + } + } + + private def showTableNonDataColumns(table: Table, builder: StringBuilder): Unit = { + if (!table.partitioning.isEmpty) { + val identityCols = new mutable.ArrayBuffer[String] + var bucketSpec = Option.empty[BucketSpec] + + table.partitioning.map { + case IdentityTransform(FieldReference(Seq(col))) => + identityCols += col + case t : BucketTransform => + bucketSpec = Some(BucketSpec(t.numBuckets.value, t.columns.map(col => col.toString), Nil)) + case transform => Review comment: @sunchao when we create table using sql like `CREATE TABLE t (a INT, b STRING, ts TIMESTAMP) USING parquet` ` PARTITIONED BY ( ` ` a, ` ` bucket(16, b), ` ` years(ts), ` ` months(ts), ` ` days(ts), ` ` hours(ts) ) ` `bucket(16, b)` can parse to `BucketTransform` , we should show the ddl like `PARTITION BY ( bucket(16,b)) ` or `CLUSTERED BY (b) INTO 16 BUCKETS` ? ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ShowCreateTableExec.scala ########## @@ -0,0 +1,172 @@ +/* + * 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.v2 + +import scala.collection.mutable + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.catalog.BucketSpec +import org.apache.spark.sql.catalyst.expressions.Attribute +import org.apache.spark.sql.catalyst.util.escapeSingleQuotedString +import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Table, TableCatalog} +import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform} +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.LeafExecNode +import org.apache.spark.sql.execution.command.DDLUtils +import org.apache.spark.unsafe.types.UTF8String + +/** + * Physical plan node for show create table. + */ +case class ShowCreateTableExec( + output: Seq[Attribute], + table: Table, + asSerde: Boolean = false) extends V2CommandExec with LeafExecNode { + override protected def run(): Seq[InternalRow] = { + val builder = StringBuilder.newBuilder + if (isView(table)) { + // It is better to use `ShowCreateViewExec` independent from `ShowCreateTableExec` + throw new AnalysisException("SHOW CREATE TABLE is not supported for V2 Views.") + } else { + if (asSerde) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for v2 tables of" + + " Hive Serde table to generate the Hive DDL.") + } else { + // it is used to generate Spark DDL for given table. include Hive Serde table + showCreateTable(table, builder) + } + } + + Seq(InternalRow(UTF8String.fromString(builder.toString))) + } + + private def showCreateTable(table: Table, builder: StringBuilder): Unit = { + builder ++= s"CREATE TABLE ${table.name()}" + val isHive = DDLUtils.isHiveTable(Option(table.properties.get(TableCatalog.PROP_PROVIDER))) + if (isHive) { + throw new AnalysisException("SHOW CREATE TABLE is not supported for Hive serde table" + + " to generate the Spark DDL.") + } + showTableDataColumns(table, builder) + showTableUsing(table, builder) + showTableOptions(table, builder) + showTableNonDataColumns(table, builder) + showTableComment(table, builder) + showTableLocation(table, builder) + showTableProperties(table, builder) + } + + private def showTableDataColumns(table: Table, builder: StringBuilder): Unit = { + val columns = table.schema().fields.map(_.toDDL) + builder ++= concatByMultiLines(columns) + } + + private def showTableUsing(table: Table, builder: StringBuilder): Unit = { + if (table.properties.containsKey(TableCatalog.PROP_PROVIDER)) { + builder ++= + s"USING ${escapeSingleQuotedString(table.properties().get(TableCatalog.PROP_PROVIDER))}\n" + } + } + + private def showTableOptions(table: Table, builder: StringBuilder): Unit = { + import scala.collection.JavaConverters._ + val dataSourceOptions = table.properties.asScala + .filterKeys(_.startsWith(TableCatalog.OPTION_PREFIX)) + if (dataSourceOptions.nonEmpty) { + val props = dataSourceOptions.map { case (key, value) => + s"'${escapeSingleQuotedString(key)}' = '${escapeSingleQuotedString(value)}'" + } + + builder ++= "OPTIONS " + builder ++= concatByMultiLines(props) + } + } + + private def showTableNonDataColumns(table: Table, builder: StringBuilder): Unit = { + if (!table.partitioning.isEmpty) { + val identityCols = new mutable.ArrayBuffer[String] + var bucketSpec = Option.empty[BucketSpec] + + table.partitioning.map { + case IdentityTransform(FieldReference(Seq(col))) => + identityCols += col + case t : BucketTransform => + bucketSpec = Some(BucketSpec(t.numBuckets.value, t.columns.map(col => col.toString), Nil)) + case transform => + throw QueryExecutionErrors.unsupportedPartitionTransformError(transform) + } + if (identityCols.nonEmpty) { + builder ++= s"PARTITIONED BY ${identityCols.mkString("(", ", ", ")")}\n" Review comment: @sunchao When we create table ,we can use follow ddl: `CREATE TABLE t (a INT, b STRING, ts TIMESTAMP) USING parquet` `PARTITIONED BY (` > `a,` > `bucket(16, b),` > `years(ts),` > `months(ts),` > `days(ts),` > `hours(ts),` > `foo(a, "bar", 34))` But, `bucket(16, b),` parse to `BucketSpec` first, then convert to `BucketTransform` and add to table.partitioning when resolve plan. What's confusing is that we should show ddl `Partitioned BY (bucket(16, b))` or `CLUSTERED BY (B) [SORTED BY ()]INTO 4 BUCKETS`. I don't really understand this place. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
