-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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-2456] support 'show partitions' sql #3693
Changes from 2 commits
6f1ded9
cb86fea
a2025a0
9ad6e66
ca20de6
5f539d6
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 |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
*/ | ||
public class PartitionPathEncodeUtils { | ||
|
||
public static final String HUDI_DEFAULT_PARTITION_PATH = "default"; | ||
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. actually lets just do 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. ok 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. Done |
||
|
||
static BitSet charToEscape = new BitSet(128); | ||
static { | ||
for (char c = 0; c < ' '; c++) { | ||
|
@@ -71,7 +73,7 @@ public static String escapePathName(String path, String defaultPath) { | |
if (defaultPath == null) { | ||
//previously, when path is empty or null and no default path is specified, | ||
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. the description should be updated accordingly? 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. done |
||
// __HIVE_DEFAULT_PARTITION__ was the return value for escapePathName | ||
return "__HIVE_DEFAULT_PARTITION__"; | ||
return HUDI_DEFAULT_PARTITION_PATH; | ||
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. why is this change needed 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. +1 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. When the partition's value is null, hudi use "default" as the part of path, rather than "HIVE_DEFAULT_PARTITION". |
||
} else { | ||
return defaultPath; | ||
} | ||
|
@@ -111,4 +113,12 @@ public static String unescapePathName(String path) { | |
} | ||
return sb.toString(); | ||
} | ||
|
||
public static String escapePartitionValue(String value) { | ||
if (value == null || value.isEmpty()) { | ||
return HUDI_DEFAULT_PARTITION_PATH; | ||
} else { | ||
return escapePathName(value); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.hudi.command | ||
|
||
import java.util.Properties | ||
|
||
import org.apache.hudi.client.common.HoodieSparkEngineContext | ||
import org.apache.hudi.common.config.HoodieMetadataConfig | ||
import org.apache.hudi.common.fs.FSUtils | ||
import org.apache.spark.api.java.JavaSparkContext | ||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.catalyst.catalog.CatalogTable | ||
import org.apache.spark.sql.hudi.HoodieSqlUtils | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* Helper trait for all hoodie commands. | ||
*/ | ||
trait HoodieCommand { | ||
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. in the interest of keeping class hierarchies tenable, can this become a helper method? 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 is already 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. OK, i'll amend it. 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. Done |
||
|
||
protected def getAllPartitionPaths(spark: SparkSession, table: CatalogTable): Seq[String] = { | ||
val sparkEngine = new HoodieSparkEngineContext(new JavaSparkContext(spark.sparkContext)) | ||
val metadataConfig = { | ||
val properties = new Properties() | ||
properties.putAll((spark.sessionState.conf.getAllConfs ++ table.storage.properties).asJava) | ||
HoodieMetadataConfig.newBuilder.fromProperties(properties).build() | ||
} | ||
FSUtils.getAllPartitionPaths(sparkEngine, metadataConfig, HoodieSqlUtils.getTableLocation(table, spark)).asScala | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.hudi.command | ||
|
||
import org.apache.hudi.common.table.HoodieTableMetaClient | ||
import org.apache.hudi.common.util.PartitionPathEncodeUtils | ||
import org.apache.spark.sql.{Row, SparkSession} | ||
import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier} | ||
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec | ||
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} | ||
import org.apache.spark.sql.execution.command.RunnableCommand | ||
import org.apache.spark.sql.execution.datasources.PartitioningUtils | ||
import org.apache.spark.sql.hudi.HoodieSqlUtils | ||
import org.apache.spark.sql.types.StringType | ||
|
||
/** | ||
* Command for show hudi table's partitions. | ||
*/ | ||
case class ShowHoodieTablePartitionsCommand( | ||
tableName: TableIdentifier, | ||
specOpt: Option[TablePartitionSpec]) | ||
extends RunnableCommand with HoodieCommand { | ||
|
||
override val output: Seq[Attribute] = { | ||
AttributeReference("partition", StringType, nullable = false)() :: Nil | ||
} | ||
|
||
override def run(sparkSession: SparkSession): Seq[Row] = { | ||
val catalog = sparkSession.sessionState.catalog | ||
val resolver = sparkSession.sessionState.conf.resolver | ||
val catalogTable = catalog.getTableMetadata(tableName) | ||
val tablePath = HoodieSqlUtils.getTableLocation(catalogTable, sparkSession) | ||
|
||
val hadoopConf = sparkSession.sessionState.newHadoopConf() | ||
val metaClient = HoodieTableMetaClient.builder().setBasePath(tablePath) | ||
.setConf(hadoopConf).build() | ||
val schemaOpt = HoodieSqlUtils.getTableSqlSchema(metaClient) | ||
val partitionColumnNamesOpt = metaClient.getTableConfig.getPartitionFields | ||
if (partitionColumnNamesOpt.isPresent && partitionColumnNamesOpt.get.nonEmpty | ||
&& schemaOpt.isDefined && schemaOpt.nonEmpty) { | ||
|
||
val partitionColumnNames = partitionColumnNamesOpt.get | ||
val schema = schemaOpt.get | ||
val allPartitionPaths: Seq[String] = getAllPartitionPaths(sparkSession, catalogTable) | ||
|
||
if (specOpt.isEmpty) { | ||
allPartitionPaths.map(Row(_)) | ||
} else { | ||
val spec = specOpt.get | ||
allPartitionPaths.filter { partitionPath => | ||
val part = PartitioningUtils.parsePathFragment(partitionPath) | ||
spec.forall { case (col, value) => | ||
PartitionPathEncodeUtils.escapePartitionValue(value) == part.getOrElse(col, null) | ||
} | ||
}.map(Row(_)) | ||
} | ||
} else { | ||
Seq.empty[Row] | ||
} | ||
} | ||
} |
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.
rename:
HOODIE_DEFAULT_PARTITION_PATH
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.
OK
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.
Done