YannByron commented on code in PR #8414:
URL: https://github.com/apache/paimon/pull/8414#discussion_r3511191778
##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/catalyst/analysis/PaimonAnalysis.scala:
##########
@@ -102,12 +99,201 @@ class PaimonAnalysis(session: SparkSession) extends
Rule[LogicalPlan] {
}
}
+ private def resolvePaimonWrite(
+ v2WriteCommand: V2WriteCommand,
+ table: DataSourceV2Relation,
+ options: Options,
+ mergeSchemaEnabled: Boolean): LogicalPlan = {
+ val query = stripHiveDynamicPartitionMarker(v2WriteCommand.query)
+ hiveDynamicPartitionColumns(v2WriteCommand.query) match {
+ case Some(dynamicPartitionColumns) if !v2WriteCommand.isByName =>
+ resolveHiveDynamicPartitionWrite(
+ query,
+ table,
+ dynamicPartitionColumns,
+ options,
+ mergeSchemaEnabled)
+ case _ =>
+ v2WriteCommand match {
+ case o: OverwritePartitionsDynamic if !o.isByName =>
+ resolveDynamicPartitionOverwrite(
+ o.copy(query = query),
+ table,
+ options,
+ mergeSchemaEnabled)
+ case _ =>
+ val expected =
+ expectedAttrsForWrite(query, table, options,
v2WriteCommand.isByName)
+ resolveWriteOutput(
+ query,
+ table.name,
+ expected,
+ v2WriteCommand.isByName,
+ mergeSchemaEnabled)
+ }
+ }
+ }
+
+ private def hiveDynamicPartitionColumns(query: LogicalPlan):
Option[Seq[String]] = {
+ query.collectFirst {
+ case PaimonHiveDynamicPartitionQuery(dynamicPartitionColumns, _) =>
+ dynamicPartitionColumns
+ }
+ }
+
+ private def stripHiveDynamicPartitionMarker(query: LogicalPlan): LogicalPlan
= {
+ query.transformDown { case PaimonHiveDynamicPartitionQuery(_, child) =>
child }
+ }
+
+ private def resolveHiveDynamicPartitionWrite(
+ query: LogicalPlan,
+ table: DataSourceV2Relation,
+ dynamicPartitionColumns: Seq[String],
+ options: Options,
+ mergeSchemaEnabled: Boolean): LogicalPlan = {
+ if (sameOutputNames(query.output, table.output)) {
+ resolveWriteOutput(
+ query,
+ table.name,
+ expectedAttrsForWrite(query, table, options, byName = false),
+ byName = false,
+ mergeSchemaEnabled)
+ } else {
+ hiveStyleDynamicPartitionOutput(table, dynamicPartitionColumns) match {
+ case Some(hiveStyleOutput) if !sameOutputNames(hiveStyleOutput,
table.output) =>
+ val hiveStyleQuery =
+ resolveWriteOutput(
+ query,
+ table.name,
+ hiveStyleOutput,
+ byName = false,
+ mergeSchemaEnabled)
+ resolveWriteOutput(
+ hiveStyleQuery,
+ table.name,
+ expectedAttrsForWrite(hiveStyleQuery, table, options, byName =
true),
+ byName = true,
+ mergeSchemaEnabled)
+ case _ =>
+ resolveWriteOutput(
+ query,
+ table.name,
+ expectedAttrsForWrite(query, table, options, byName = false),
+ byName = false,
+ mergeSchemaEnabled)
+ }
+ }
+ }
+
+ private def resolveDynamicPartitionOverwrite(
+ overwrite: OverwritePartitionsDynamic,
+ table: DataSourceV2Relation,
+ options: Options,
+ mergeSchemaEnabled: Boolean): LogicalPlan = {
+ hiveStyleDynamicPartitionOutput(overwrite.query, table) match {
+ case Some(hiveStyleOutput) if !sameOutputNames(hiveStyleOutput,
table.output) =>
+ val hiveStyleQuery =
+ resolveWriteOutput(
+ overwrite.query,
+ table.name,
+ hiveStyleOutput,
+ byName = false,
+ mergeSchemaEnabled)
+ resolveWriteOutput(
+ hiveStyleQuery,
+ table.name,
+ expectedAttrsForWrite(hiveStyleQuery, table, options, byName = true),
+ byName = true,
+ mergeSchemaEnabled)
+ case _ =>
+ resolveWriteOutput(
+ overwrite.query,
+ table.name,
+ expectedAttrsForWrite(overwrite.query, table, options, byName =
false),
+ byName = false,
+ mergeSchemaEnabled)
+ }
+ }
+
+ private def expectedAttrsForWrite(
+ query: LogicalPlan,
+ table: DataSourceV2Relation,
+ options: Options,
+ byName: Boolean): Seq[Attribute] = {
+ SchemaEvolutionHelper.expectedAttrsForCatalogWrite(
+ table,
+ query.schema,
+ options,
+ byName,
+ session)
+ }
+
+ private def resolveWriteOutput(
+ query: LogicalPlan,
+ tableName: String,
+ expectedOutput: Seq[Attribute],
+ byName: Boolean,
+ mergeSchemaEnabled: Boolean): LogicalPlan = {
+ if (paimonWriteResolved(query, expectedOutput)) {
+ query
+ } else {
+ PaimonOutputResolver.resolveOutputColumns(
+ tableName,
+ expectedOutput,
+ query,
+ byName,
+ mergeSchemaEnabled)
+ }
+ }
+
+ private def hiveStyleDynamicPartitionOutput(
+ query: LogicalPlan,
+ table: DataSourceV2Relation): Option[Seq[Attribute]] = {
+ val dynamicPartitionColumns =
+
table.table.asInstanceOf[SparkTable].getTable.partitionKeys().asScala.toSeq
+ hiveStyleDynamicPartitionOutput(table, dynamicPartitionColumns).filter {
+ hiveStyleOutput => sameOutputNames(query.output, hiveStyleOutput)
+ }
+ }
+
+ private def hiveStyleDynamicPartitionOutput(
+ table: DataSourceV2Relation,
+ dynamicPartitionColumns: Seq[String]): Option[Seq[Attribute]] = {
+ val partitionKeys =
table.table.asInstanceOf[SparkTable].getTable.partitionKeys().asScala.toSeq
+ val dynamicPartitionSet = dynamicPartitionColumns
Review Comment:
It is a misleading no-op alias.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]