def apply(plan: LogicalPlan): LogicalPlan = {
plan transform {
case unresolvedRelation: UnresolvedRelation =>
val tblSchemaName: Array[String] = unresolvedRelation.tableName.split("\.")
if (tblSchemaName.length == 1) return plan
val schema = tblSchemaName.apply(0)
val tblName = tblSchemaName.apply(1)
for (ref <- this.refs) {
if (tblName == ref.nqName) {
return unresolvedRelation.copy(multipartIdentifier = Seq(schema.toUpperCase, tblName.toUpperCase), unresolvedRelation.options, unresolvedRelation.isStreaming)
}
}
unresolvedRelation
case unresolvedWith: UnresolvedWith =>
val newCteRelations = unresolvedWith.cteRelations.map {
case (aliasName, subqueryAlias) =>
val newSubqueryAlias = apply(subqueryAlias).asInstanceOf[SubqueryAlias]
(aliasName, newSubqueryAlias)
}
val modifiedCTEUnresolvedWith = unresolvedWith.copy(cteRelations = newCteRelations)
modifiedCTEUnresolvedWith
case otherPlan: LogicalPlan =>
val newChildren = otherPlan.children.map(child => apply(child))
val modified_plan = otherPlan.withNewChildren(newChildren)
modified_plan
}
}
}
rules += UpperCaseTableRule(refs)
val optimizer = new RuleExecutor[LogicalPlan] {
val batches = Seq(
Batch("Rewrite", Once, rules.toList: _*)
)
}
val logicalPlanRewrite = optimizer.execute(logicalPlan)
with the code above, what i want to achieve is to uppercase the names of the table if they are in the list.
the code can work with select statement, but when it comes to CTE, the unresolvedWith can return the correct CTE, but logicalPlanRewrite after the optimizer execute is still lower case.
can anyone help me understand why?