/
rules.scala
85 lines (78 loc) · 3.58 KB
/
rules.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright 2018 PingCAP, Inc.
*
* Licensed 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,
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.extensions
import com.pingcap.tispark.statistics.StatisticsManager
import org.apache.spark.sql.{AnalysisException, _}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SubqueryAlias}
import org.apache.spark.sql.catalyst.rules.Rule
import com.pingcap.tispark.{MetaManager, TiDBRelation, TiTableReference}
import org.apache.spark.sql.catalyst.catalog.TiSessionCatalog
import org.apache.spark.sql.execution.command._
import org.apache.spark.sql.execution.datasources.LogicalRelation
case class TiResolutionRule(getOrCreateTiContext: SparkSession => TiContext)(
sparkSession: SparkSession
) extends Rule[LogicalPlan] {
protected val tiContext: TiContext = getOrCreateTiContext(sparkSession)
private lazy val autoLoad = tiContext.autoLoad
protected lazy val meta: MetaManager = tiContext.meta
private lazy val tiCatalog = tiContext.tiCatalog
private lazy val tiSession = tiContext.tiSession
private lazy val sqlContext = tiContext.sqlContext
private def getDatabaseFromIdentifier(tableIdentifier: TableIdentifier): String =
tableIdentifier.database.getOrElse(tiCatalog.getCurrentDatabase)
protected val resolveTiDBRelation: TableIdentifier => LogicalPlan =
(tableIdentifier: TableIdentifier) => {
val dbName = getDatabaseFromIdentifier(tableIdentifier)
val tableName = tableIdentifier.table
val table = meta.getTable(dbName, tableName)
if (table.isEmpty) {
throw new AnalysisException(s"Table or view '$tableName' not found in database '$dbName'")
}
if (autoLoad) {
StatisticsManager.loadStatisticsInfo(table.get)
}
val sizeInBytes = StatisticsManager.estimateTableSize(table.get)
val tiDBRelation = TiDBRelation(
tiSession,
TiTableReference(dbName, tableName, sizeInBytes),
meta
)(sqlContext)
// Use SubqueryAlias so that projects and joins can correctly resolve
// UnresolvedAttributes in JoinConditions, Projects, Filters, etc.
SubqueryAlias(tableName, LogicalRelation(tiDBRelation))
}
override def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
case UnresolvedRelation(tableIdentifier)
if tiCatalog.catalogOf(tableIdentifier.database).exists(_.isInstanceOf[TiSessionCatalog]) =>
resolveTiDBRelation(tableIdentifier)
}
}
case class TiDDLRule(getOrCreateTiContext: SparkSession => TiContext)(sparkSession: SparkSession)
extends Rule[LogicalPlan] {
protected lazy val tiContext: TiContext = getOrCreateTiContext(sparkSession)
override def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
// TODO: support other commands that may concern TiSpark catalog.
case sd: ShowDatabasesCommand =>
TiShowDatabasesCommand(tiContext, sd)
case sd: SetDatabaseCommand =>
TiSetDatabaseCommand(tiContext, sd)
case st: ShowTablesCommand =>
TiShowTablesCommand(tiContext, st)
case dt: DescribeTableCommand =>
TiDescribeTablesCommand(tiContext, dt)
}
}