17
17
package org .apache .spark .sql .delta .catalog
18
18
import org .apache .spark .internal .Logging
19
19
import org .apache .spark .sql .SparkSession
20
- import org .apache .spark .sql .catalyst .catalog .{ BucketSpec , CatalogTable }
20
+ import org .apache .spark .sql .catalyst .catalog .CatalogTable
21
21
import org .apache .spark .sql .catalyst .expressions .{Attribute , Expression }
22
22
import org .apache .spark .sql .connector .read .InputPartition
23
23
import org .apache .spark .sql .connector .write .{LogicalWriteInfo , WriteBuilder }
24
- import org .apache .spark .sql .delta .{ClickhouseSnapshot , DeltaErrors , DeltaLog , DeltaTimeTravelSpec }
24
+ import org .apache .spark .sql .delta .{ClickhouseSnapshot , DeltaErrors , DeltaLog , DeltaTimeTravelSpec , Snapshot }
25
25
import org .apache .spark .sql .delta .actions .Metadata
26
26
import org .apache .spark .sql .delta .catalog .ClickHouseTableV2 .deltaLog2Table
27
27
import org .apache .spark .sql .delta .sources .DeltaDataSource
@@ -54,8 +54,8 @@ class ClickHouseTableV2(
54
54
tableIdentifier,
55
55
timeTravelOpt,
56
56
options,
57
- cdcOptions) {
58
- protected def getMetadata : Metadata = if (snapshot == null ) Metadata () else snapshot.metadata
57
+ cdcOptions)
58
+ with ClickHouseTableV2Base {
59
59
60
60
lazy val (rootPath, partitionFilters, timeTravelByPath) = {
61
61
if (catalogTable.isDefined) {
@@ -93,126 +93,6 @@ class ClickHouseTableV2(
93
93
new WriteIntoDeltaBuilder (deltaLog, info.options)
94
94
}
95
95
96
- lazy val dataBaseName = catalogTable
97
- .map(_.identifier.database.getOrElse(" default" ))
98
- .getOrElse(" clickhouse" )
99
-
100
- lazy val tableName = catalogTable
101
- .map(_.identifier.table)
102
- .getOrElse(path.toUri.getPath)
103
-
104
- lazy val bucketOption : Option [BucketSpec ] = {
105
- val tableProperties = properties()
106
- if (tableProperties.containsKey(" numBuckets" )) {
107
- val numBuckets = tableProperties.get(" numBuckets" ).trim.toInt
108
- val bucketColumnNames : Seq [String ] =
109
- tableProperties.get(" bucketColumnNames" ).split(" ," ).map(_.trim).toSeq
110
- val sortColumnNames : Seq [String ] = if (tableProperties.containsKey(" orderByKey" )) {
111
- tableProperties.get(" orderByKey" ).split(" ," ).map(_.trim).toSeq
112
- } else Seq .empty[String ]
113
- Some (BucketSpec (numBuckets, bucketColumnNames, sortColumnNames))
114
- } else {
115
- None
116
- }
117
- }
118
-
119
- lazy val lowCardKeyOption : Option [Seq [String ]] = {
120
- getCommaSeparatedColumns(" lowCardKey" )
121
- }
122
-
123
- lazy val minmaxIndexKeyOption : Option [Seq [String ]] = {
124
- getCommaSeparatedColumns(" minmaxIndexKey" )
125
- }
126
-
127
- lazy val bfIndexKeyOption : Option [Seq [String ]] = {
128
- getCommaSeparatedColumns(" bloomfilterIndexKey" )
129
- }
130
-
131
- lazy val setIndexKeyOption : Option [Seq [String ]] = {
132
- getCommaSeparatedColumns(" setIndexKey" )
133
- }
134
-
135
- private def getCommaSeparatedColumns (keyName : String ) = {
136
- val tableProperties = properties()
137
- if (tableProperties.containsKey(keyName)) {
138
- if (tableProperties.get(keyName).nonEmpty) {
139
- val keys = tableProperties.get(keyName).split(" ," ).map(_.trim).toSeq
140
- keys.foreach(
141
- s => {
142
- if (s.contains(" ." )) {
143
- throw new IllegalStateException (
144
- s " $keyName $s can not contain '.' (not support nested column yet) " )
145
- }
146
- })
147
- Some (keys.map(s => s.toLowerCase()))
148
- } else {
149
- None
150
- }
151
- } else {
152
- None
153
- }
154
- }
155
-
156
- lazy val orderByKeyOption : Option [Seq [String ]] = {
157
- if (bucketOption.isDefined && bucketOption.get.sortColumnNames.nonEmpty) {
158
- val orderByKes = bucketOption.get.sortColumnNames
159
- val invalidKeys = orderByKes.intersect(partitionColumns)
160
- if (invalidKeys.nonEmpty) {
161
- throw new IllegalStateException (
162
- s " partition cols $invalidKeys can not be in the order by keys. " )
163
- }
164
- Some (orderByKes)
165
- } else {
166
- val tableProperties = properties()
167
- if (tableProperties.containsKey(" orderByKey" )) {
168
- if (tableProperties.get(" orderByKey" ).nonEmpty) {
169
- val orderByKes = tableProperties.get(" orderByKey" ).split(" ," ).map(_.trim).toSeq
170
- val invalidKeys = orderByKes.intersect(partitionColumns)
171
- if (invalidKeys.nonEmpty) {
172
- throw new IllegalStateException (
173
- s " partition cols $invalidKeys can not be in the order by keys. " )
174
- }
175
- Some (orderByKes)
176
- } else {
177
- None
178
- }
179
- } else {
180
- None
181
- }
182
- }
183
- }
184
-
185
- lazy val primaryKeyOption : Option [Seq [String ]] = {
186
- if (orderByKeyOption.isDefined) {
187
- val tableProperties = properties()
188
- if (tableProperties.containsKey(" primaryKey" )) {
189
- if (tableProperties.get(" primaryKey" ).nonEmpty) {
190
- val primaryKeys = tableProperties.get(" primaryKey" ).split(" ," ).map(_.trim).toSeq
191
- if (! orderByKeyOption.get.mkString(" ," ).startsWith(primaryKeys.mkString(" ," ))) {
192
- throw new IllegalStateException (
193
- s " Primary key $primaryKeys must be a prefix of the sorting key " )
194
- }
195
- Some (primaryKeys)
196
- } else {
197
- None
198
- }
199
- } else {
200
- None
201
- }
202
- } else {
203
- None
204
- }
205
- }
206
-
207
- lazy val partitionColumns = snapshot.metadata.partitionColumns
208
-
209
- lazy val clickhouseTableConfigs : Map [String , String ] = {
210
- val tableProperties = properties()
211
- val configs = scala.collection.mutable.Map [String , String ]()
212
- configs += (" storage_policy" -> tableProperties.getOrDefault(" storage_policy" , " default" ))
213
- configs.toMap
214
- }
215
-
216
96
def getFileFormat (meta : Metadata ): DeltaMergeTreeFileFormat = {
217
97
new DeltaMergeTreeFileFormat (
218
98
meta,
@@ -230,41 +110,19 @@ class ClickHouseTableV2(
230
110
)
231
111
}
232
112
233
- def cacheThis (): Unit = {
234
- deltaLog2Table.put(deltaLog, this )
235
- }
113
+ override def deltaProperties (): ju.Map [String , String ] = properties()
236
114
237
- cacheThis()
115
+ override def deltaCatalog () : Option [ CatalogTable ] = catalogTable
238
116
239
- def primaryKey (): String = primaryKeyOption match {
240
- case Some (keys) => keys.mkString(" ," )
241
- case None => " "
242
- }
243
-
244
- def orderByKey (): String = orderByKeyOption match {
245
- case Some (keys) => keys.mkString(" ," )
246
- case None => " tuple()"
247
- }
248
-
249
- def lowCardKey (): String = lowCardKeyOption match {
250
- case Some (keys) => keys.mkString(" ," )
251
- case None => " "
252
- }
117
+ override def deltaPath (): Path = path
253
118
254
- def minmaxIndexKey (): String = minmaxIndexKeyOption match {
255
- case Some (keys) => keys.mkString(" ," )
256
- case None => " "
257
- }
119
+ override def deltaSnapshot (): Snapshot = snapshot
258
120
259
- def bfIndexKey (): String = bfIndexKeyOption match {
260
- case Some (keys) => keys.mkString(" ," )
261
- case None => " "
121
+ def cacheThis (): Unit = {
122
+ deltaLog2Table.put(deltaLog, this )
262
123
}
263
124
264
- def setIndexKey (): String = setIndexKeyOption match {
265
- case Some (keys) => keys.mkString(" ," )
266
- case None => " "
267
- }
125
+ cacheThis()
268
126
}
269
127
270
128
@ SuppressWarnings (Array (" io.github.zhztheplayer.scalawarts.InheritFromCaseClass" ))
0 commit comments