|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.analysis |
| 19 | + |
| 20 | +import scala.collection.mutable |
| 21 | + |
| 22 | +import org.apache.spark.SparkException |
| 23 | +import org.apache.spark.sql.catalyst.SQLConfHelper |
| 24 | +import org.apache.spark.sql.catalyst.analysis.TableReference.Context |
| 25 | +import org.apache.spark.sql.catalyst.analysis.TableReference.TableInfo |
| 26 | +import org.apache.spark.sql.catalyst.analysis.TableReference.TemporaryViewContext |
| 27 | +import org.apache.spark.sql.catalyst.expressions.AttributeReference |
| 28 | +import org.apache.spark.sql.catalyst.plans.logical.LeafNode |
| 29 | +import org.apache.spark.sql.catalyst.plans.logical.Statistics |
| 30 | +import org.apache.spark.sql.catalyst.util.{truncatedString, MetadataColumnHelper} |
| 31 | +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.CatalogHelper |
| 32 | +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.IdentifierHelper |
| 33 | +import org.apache.spark.sql.connector.catalog.Column |
| 34 | +import org.apache.spark.sql.connector.catalog.Identifier |
| 35 | +import org.apache.spark.sql.connector.catalog.MetadataColumn |
| 36 | +import org.apache.spark.sql.connector.catalog.SupportsMetadataColumns |
| 37 | +import org.apache.spark.sql.connector.catalog.Table |
| 38 | +import org.apache.spark.sql.connector.catalog.TableCatalog |
| 39 | +import org.apache.spark.sql.errors.QueryCompilationErrors |
| 40 | +import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation |
| 41 | +import org.apache.spark.sql.types.DataType |
| 42 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap |
| 43 | +import org.apache.spark.util.ArrayImplicits._ |
| 44 | + |
| 45 | +case class TableReference private ( |
| 46 | + catalog: TableCatalog, |
| 47 | + identifier: Identifier, |
| 48 | + options: CaseInsensitiveStringMap, |
| 49 | + info: TableInfo, |
| 50 | + output: Seq[AttributeReference], |
| 51 | + context: Context) |
| 52 | + extends LeafNode with MultiInstanceRelation with NamedRelation { |
| 53 | + |
| 54 | + override def name: String = { |
| 55 | + s"${catalog.name()}.${identifier.quoted}" |
| 56 | + } |
| 57 | + |
| 58 | + override def newInstance(): TableReference = { |
| 59 | + copy(output = output.map(_.newInstance())) |
| 60 | + } |
| 61 | + |
| 62 | + override def computeStats(): Statistics = Statistics.DUMMY |
| 63 | + |
| 64 | + override def simpleString(maxFields: Int): String = { |
| 65 | + val outputString = truncatedString(output, "[", ", ", "]", maxFields) |
| 66 | + s"TableReference$outputString $name" |
| 67 | + } |
| 68 | + |
| 69 | + def toRelation(table: Table): DataSourceV2Relation = { |
| 70 | + TableReferenceUtils.validateLoadedTable(table, this) |
| 71 | + DataSourceV2Relation(table, output, Some(catalog), Some(identifier), options) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +object TableReference { |
| 76 | + |
| 77 | + case class TableInfo(columns: Seq[Column]) |
| 78 | + |
| 79 | + sealed trait Context |
| 80 | + case class TemporaryViewContext(viewName: Seq[String]) extends Context |
| 81 | + |
| 82 | + def createForTempView(relation: DataSourceV2Relation, viewName: Seq[String]): TableReference = { |
| 83 | + create(relation, TemporaryViewContext(viewName)) |
| 84 | + } |
| 85 | + |
| 86 | + private def create(relation: DataSourceV2Relation, context: Context): TableReference = { |
| 87 | + val ref = TableReference( |
| 88 | + relation.catalog.get.asTableCatalog, |
| 89 | + relation.identifier.get, |
| 90 | + relation.options, |
| 91 | + TableInfo(relation.table.columns.toImmutableArraySeq), |
| 92 | + relation.output, |
| 93 | + context) |
| 94 | + ref.copyTagsFrom(relation) |
| 95 | + ref |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +object TableReferenceUtils extends SQLConfHelper { |
| 100 | + |
| 101 | + def validateLoadedTable(table: Table, ref: TableReference): Unit = { |
| 102 | + ref.context match { |
| 103 | + case ctx: TemporaryViewContext => |
| 104 | + validateLoadedTableInTempView(table, ref, ctx) |
| 105 | + case ctx => |
| 106 | + throw SparkException.internalError(s"Unknown table ref context: ${ctx.getClass.getName}") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private def validateLoadedTableInTempView( |
| 111 | + table: Table, |
| 112 | + ref: TableReference, |
| 113 | + ctx: TemporaryViewContext): Unit = { |
| 114 | + val dataColMismatches = validateDataColumns(table, ref.info.columns) |
| 115 | + if (dataColMismatches.nonEmpty) { |
| 116 | + throw QueryCompilationErrors.tableReferenceTempViewColumnMismatchError( |
| 117 | + ctx.viewName, |
| 118 | + ref.identifier.toQualifiedNameParts(ref.catalog), |
| 119 | + dataColMismatches.mkString("\n")) |
| 120 | + } |
| 121 | + |
| 122 | + val metaOutput = ref.output.filter(_.isMetadataCol) |
| 123 | + if (metaOutput.nonEmpty) { |
| 124 | + val metaColMismatches = validateMetadataColumns(table, metaOutput) |
| 125 | + if (metaColMismatches.nonEmpty) { |
| 126 | + throw QueryCompilationErrors.tableReferenceTempViewMetadataColumnMismatchError( |
| 127 | + ctx.viewName, |
| 128 | + ref.identifier.toQualifiedNameParts(ref.catalog), |
| 129 | + metaColMismatches.mkString("\n")) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private def validateDataColumns(table: Table, refDataCols: Seq[Column]): Seq[String] = { |
| 135 | + val mismatches = mutable.ArrayBuffer[String]() |
| 136 | + |
| 137 | + refDataCols.foreach { refCol => |
| 138 | + table.columns.find(c => conf.resolver(c.name, refCol.name)) match { |
| 139 | + case Some(col) => |
| 140 | + if (refCol.dataType != col.dataType || refCol.nullable != col.nullable) { |
| 141 | + val refType = formatType(refCol.dataType, refCol.nullable) |
| 142 | + val newType = formatType(col.dataType, col.nullable) |
| 143 | + mismatches += s"- ${refCol.name} has evolved from $refType to $newType" |
| 144 | + } |
| 145 | + case None => |
| 146 | + mismatches += s"- ${refCol.name} is missing" |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + table.columns.foreach { col => |
| 151 | + if (!refDataCols.exists(refCol => conf.resolver(col.name, refCol.name))) { |
| 152 | + mismatches += s"- ${col.name} has been added" |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + mismatches.toSeq |
| 157 | + } |
| 158 | + |
| 159 | + private def validateMetadataColumns( |
| 160 | + table: Table, |
| 161 | + metaOutput: Seq[AttributeReference]): Seq[String] = { |
| 162 | + val mismatches = mutable.ArrayBuffer[String]() |
| 163 | + val metaCols = metadataColumns(table) |
| 164 | + |
| 165 | + metaOutput.foreach { metaAttr => |
| 166 | + metaCols.find(c => conf.resolver(c.name, metaAttr.name)) match { |
| 167 | + case Some(col) => |
| 168 | + if (metaAttr.dataType != col.dataType || metaAttr.nullable != col.isNullable) { |
| 169 | + val refType = formatType(metaAttr.dataType, metaAttr.nullable) |
| 170 | + val newType = formatType(col.dataType, col.isNullable) |
| 171 | + mismatches += s"- ${metaAttr.name} has evolved from $refType to $newType" |
| 172 | + } |
| 173 | + case None => |
| 174 | + mismatches += s"- ${metaAttr.name} is missing (metadata column)" |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + mismatches.toSeq |
| 179 | + } |
| 180 | + |
| 181 | + private def metadataColumns(table: Table): Array[MetadataColumn] = table match { |
| 182 | + case hasMeta: SupportsMetadataColumns => hasMeta.metadataColumns |
| 183 | + case _ => Array.empty |
| 184 | + } |
| 185 | + |
| 186 | + private def formatType(dataType: DataType, nullable: Boolean): String = { |
| 187 | + val nullableDDL = if (nullable) "" else " NOT NULL" |
| 188 | + s"${dataType.sql}$nullableDDL" |
| 189 | + } |
| 190 | +} |
0 commit comments