forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCometCast.scala
More file actions
415 lines (388 loc) · 16.2 KB
/
Copy pathCometCast.scala
File metadata and controls
415 lines (388 loc) · 16.2 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.comet.expressions
import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, Expression, Literal}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{ArrayType, DataType, DataTypes, DecimalType, NullType, StructType, TimestampNTZType, TimestampType}
import org.apache.comet.CometConf
import org.apache.comet.CometSparkSessionExtensions.{isSpark40Plus, withFallbackReason}
import org.apache.comet.serde.{CometExpressionSerde, Compatible, ExprOuterClass, Incompatible, SupportLevel, Unsupported}
import org.apache.comet.serde.ExprOuterClass.Expr
import org.apache.comet.serde.QueryPlanSerde.{evalModeToProto, exprToProtoInternal, serializeDataType}
import org.apache.comet.shims.CometExprShim
object CometCast extends CometExpressionSerde[Cast] with CometExprShim {
def supportedTypes: Seq[DataType] =
Seq(
DataTypes.BooleanType,
DataTypes.ByteType,
DataTypes.ShortType,
DataTypes.IntegerType,
DataTypes.LongType,
DataTypes.FloatType,
DataTypes.DoubleType,
DataTypes.createDecimalType(10, 2),
DataTypes.StringType,
DataTypes.BinaryType,
DataTypes.DateType,
DataTypes.TimestampType,
DataTypes.TimestampNTZType)
override def getIncompatibleReasons(): Seq[String] = Seq(
"Some cast operations between specific type pairs may produce different results than Spark." +
" Refer to the compatibility guide for the full matrix of supported cast operations.")
override def getUnsupportedReasons(): Seq[String] = Seq(
"Not all cast type combinations are supported. Unsupported casts fall back to Spark.")
override def getSupportLevel(cast: Cast): SupportLevel = {
if (cast.child.isInstanceOf[Literal]) {
// casting from literal is compatible because we delegate to Spark
// further data type checks will be performed by CometLiteral
Compatible()
} else {
isSupported(cast.child.dataType, cast.dataType, cast.timeZoneId, evalMode(cast))
}
}
override def convert(
cast: Cast,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val cometEvalMode = evalMode(cast)
cast.child match {
case _: Literal =>
exprToProtoInternal(Literal.create(cast.eval(), cast.dataType), inputs, binding)
case _ =>
if (isAlwaysCastToNull(cast.child.dataType, cast.dataType, cometEvalMode)) {
exprToProtoInternal(Literal.create(null, cast.dataType), inputs, binding)
} else {
val childExpr = exprToProtoInternal(cast.child, inputs, binding)
if (childExpr.isDefined) {
castToProto(cast, cast.timeZoneId, cast.dataType, childExpr.get, cometEvalMode)
} else {
withFallbackReason(cast, cast.child)
None
}
}
}
}
// Some casts like date -> int/byte / long are always null. Terminate early in planning
private def isAlwaysCastToNull(
fromType: DataType,
toType: DataType,
evalMode: CometEvalMode.Value): Boolean = {
(fromType, toType) match {
case (
DataTypes.DateType,
DataTypes.BooleanType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType | DataTypes.LongType | DataTypes.FloatType |
DataTypes.DoubleType | _: DecimalType) if evalMode == CometEvalMode.LEGACY =>
true
case _ => false
}
}
/**
* Wrap an already serialized expression in a cast.
*/
def castToProto(
expr: Expression,
timeZoneId: Option[String],
dt: DataType,
childExpr: Expr,
evalMode: CometEvalMode.Value): Option[Expr] = {
serializeDataType(dt) match {
case Some(dataType) =>
val castBuilder = ExprOuterClass.Cast.newBuilder()
castBuilder.setChild(childExpr)
castBuilder.setDatatype(dataType)
castBuilder.setEvalMode(evalModeToProto(evalMode))
castBuilder.setAllowIncompat(
SQLConf.get
.getConfString(CometConf.getExprAllowIncompatConfigKey(classOf[Cast]), "false")
.toBoolean)
castBuilder.setTimezone(timeZoneId.getOrElse("UTC"))
castBuilder.setIsSpark4Plus(isSpark40Plus)
Some(
ExprOuterClass.Expr
.newBuilder()
.setCast(castBuilder)
.build())
case _ =>
withFallbackReason(expr, s"Unsupported datatype in castToProto: $dt")
None
}
}
def isSupported(
fromType: DataType,
toType: DataType,
timeZoneId: Option[String],
evalMode: CometEvalMode.Value): SupportLevel = {
if (fromType == toType) {
return Compatible()
}
(fromType, toType) match {
case (dt: ArrayType, _: ArrayType) if dt.elementType == NullType => Compatible()
case (ArrayType(DataTypes.DateType, _), ArrayType(toElementType, _))
if toElementType != DataTypes.IntegerType && toElementType != DataTypes.StringType =>
unsupported(fromType, toType)
case (dt: ArrayType, DataTypes.StringType) =>
isSupported(dt.elementType, DataTypes.StringType, timeZoneId, evalMode)
case (dt: ArrayType, dt1: ArrayType) =>
isSupported(dt.elementType, dt1.elementType, timeZoneId, evalMode)
case (dt: DataType, _) if dt.typeName == "timestamp_ntz" =>
toType match {
case DataTypes.StringType => Compatible()
case DataTypes.DateType => Compatible()
case DataTypes.TimestampType => Compatible()
case _ => unsupported(fromType, toType)
}
case (_: DecimalType, _: DecimalType) =>
Compatible()
case (DataTypes.StringType, _) =>
canCastFromString(toType, timeZoneId, evalMode)
case (_, DataTypes.StringType) =>
canCastToString(fromType, timeZoneId, evalMode)
case (DataTypes.TimestampType, _) =>
canCastFromTimestamp(toType)
case (_: DecimalType, _) =>
canCastFromDecimal(toType)
case (DataTypes.BooleanType, _) =>
canCastFromBoolean(toType, evalMode)
case (DataTypes.ByteType, _) =>
canCastFromByte(toType, evalMode)
case (DataTypes.ShortType, _) =>
canCastFromShort(toType, evalMode)
case (DataTypes.IntegerType, _) =>
canCastFromInt(toType, evalMode)
case (DataTypes.LongType, _) =>
canCastFromLong(toType, evalMode)
case (DataTypes.FloatType, _) =>
canCastFromFloat(toType)
case (DataTypes.DoubleType, _) =>
canCastFromDouble(toType)
case (from_struct: StructType, to_struct: StructType) =>
from_struct.fields.zip(to_struct.fields).foreach { case (a, b) =>
isSupported(a.dataType, b.dataType, timeZoneId, evalMode) match {
case Compatible(_) =>
// all good
case other =>
return other
}
}
Compatible()
case (DataTypes.DateType, toType) => canCastFromDate(toType, evalMode)
case _ => unsupported(fromType, toType)
}
}
private def canCastFromString(
toType: DataType,
timeZoneId: Option[String],
evalMode: CometEvalMode.Value): SupportLevel = {
toType match {
case DataTypes.BooleanType =>
Compatible()
case DataTypes.ByteType | DataTypes.ShortType | DataTypes.IntegerType |
DataTypes.LongType =>
Compatible()
case DataTypes.BinaryType =>
Compatible()
case DataTypes.FloatType | DataTypes.DoubleType =>
Compatible()
case _: DecimalType =>
Compatible()
case DataTypes.DateType =>
// https://github.com/apache/datafusion-comet/issues/327
Compatible(Some("Only supports years between 262143 BC and 262142 AD"))
case DataTypes.TimestampType =>
Compatible()
case _: TimestampNTZType =>
Compatible()
case _ =>
unsupported(DataTypes.StringType, toType)
}
}
private def canCastToString(
fromType: DataType,
timeZoneId: Option[String],
evalMode: CometEvalMode.Value): SupportLevel = {
fromType match {
case DataTypes.BooleanType => Compatible()
case DataTypes.ByteType | DataTypes.ShortType | DataTypes.IntegerType |
DataTypes.LongType =>
Compatible()
case DataTypes.DateType => Compatible()
case DataTypes.TimestampType => Compatible()
case DataTypes.FloatType | DataTypes.DoubleType =>
Compatible(Some("There can be differences in precision"))
case d: DecimalType if d.scale < 0 =>
// Negative-scale decimals require spark.sql.legacy.allowNegativeScaleOfDecimal=true.
// When that config is enabled, Spark formats them using Java BigDecimal.toString()
// which produces scientific notation (e.g. "1.23E+4"). Comet matches this behavior.
// When the config is disabled, negative-scale decimals cannot be created in Spark,
// so we mark this as incompatible to avoid native execution on unexpected inputs.
val allowNegativeScale = SQLConf.get
.getConfString("spark.sql.legacy.allowNegativeScaleOfDecimal", "false")
.toBoolean
if (allowNegativeScale) Compatible() else Incompatible()
case _: DecimalType =>
// Compatible across all eval modes: LEGACY uses cast_decimal128_to_utf8 which
// replicates Java BigDecimal.toString() (scientific notation when adj_exp < -6);
// TRY and ANSI fall through to DataFusion's plain-notation cast, which matches Spark.
Compatible()
case DataTypes.BinaryType =>
Compatible()
case StructType(fields) =>
for (field <- fields) {
isSupported(field.dataType, DataTypes.StringType, timeZoneId, evalMode) match {
case s: Incompatible =>
return s
case u: Unsupported =>
return u
case _ =>
}
}
Compatible()
case _ => unsupported(fromType, DataTypes.StringType)
}
}
private def canCastFromTimestamp(toType: DataType): SupportLevel = {
toType match {
case DataTypes.BooleanType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType =>
// https://github.com/apache/datafusion-comet/issues/352
// this seems like an edge case that isn't important for us to support
unsupported(DataTypes.TimestampType, toType)
case DataTypes.LongType =>
// https://github.com/apache/datafusion-comet/issues/352
Compatible()
case DataTypes.StringType => Compatible()
case DataTypes.DateType => Compatible()
case _: TimestampNTZType => Compatible()
case _ => unsupported(DataTypes.TimestampType, toType)
}
}
private def canCastFromBoolean(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.ByteType | DataTypes.ShortType | DataTypes.IntegerType | DataTypes.LongType |
DataTypes.FloatType | DataTypes.DoubleType | _: DecimalType =>
Compatible()
case _: TimestampType if evalMode == CometEvalMode.LEGACY =>
Compatible()
case _ => unsupported(DataTypes.BooleanType, toType)
}
private def canCastFromByte(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.BooleanType =>
Compatible()
case DataTypes.ShortType | DataTypes.IntegerType | DataTypes.LongType =>
Compatible()
case DataTypes.FloatType | DataTypes.DoubleType | _: DecimalType =>
Compatible()
case DataTypes.BinaryType if (evalMode == CometEvalMode.LEGACY) =>
Compatible()
case DataTypes.TimestampType =>
Compatible()
case _ =>
unsupported(DataTypes.ByteType, toType)
}
private def canCastFromShort(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.BooleanType =>
Compatible()
case DataTypes.ByteType | DataTypes.IntegerType | DataTypes.LongType =>
Compatible()
case DataTypes.FloatType | DataTypes.DoubleType | _: DecimalType =>
Compatible()
case DataTypes.BinaryType if (evalMode == CometEvalMode.LEGACY) =>
Compatible()
case DataTypes.TimestampType =>
Compatible()
case _ =>
unsupported(DataTypes.ShortType, toType)
}
private def canCastFromInt(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.BooleanType =>
Compatible()
case DataTypes.ByteType | DataTypes.ShortType | DataTypes.LongType =>
Compatible()
case DataTypes.FloatType | DataTypes.DoubleType =>
Compatible()
case _: DecimalType =>
Compatible()
case DataTypes.BinaryType if (evalMode == CometEvalMode.LEGACY) => Compatible()
case DataTypes.TimestampType =>
Compatible()
case _ =>
unsupported(DataTypes.IntegerType, toType)
}
private def canCastFromLong(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.BooleanType =>
Compatible()
case DataTypes.ByteType | DataTypes.ShortType | DataTypes.IntegerType =>
Compatible()
case DataTypes.FloatType | DataTypes.DoubleType =>
Compatible()
case _: DecimalType =>
Compatible()
case DataTypes.BinaryType if (evalMode == CometEvalMode.LEGACY) => Compatible()
case DataTypes.TimestampType =>
Compatible()
case _ =>
unsupported(DataTypes.LongType, toType)
}
private def canCastFromFloat(toType: DataType): SupportLevel = toType match {
case DataTypes.BooleanType | DataTypes.DoubleType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType | DataTypes.LongType | DataTypes.TimestampType =>
Compatible()
case _: DecimalType =>
// https://github.com/apache/datafusion-comet/issues/1371
Incompatible(Some("There can be rounding differences"))
case _ =>
unsupported(DataTypes.FloatType, toType)
}
private def canCastFromDouble(toType: DataType): SupportLevel = toType match {
case DataTypes.BooleanType | DataTypes.FloatType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType | DataTypes.LongType | DataTypes.TimestampType =>
Compatible()
case _: DecimalType =>
// https://github.com/apache/datafusion-comet/issues/1371
Incompatible(Some("There can be rounding differences"))
case _ => unsupported(DataTypes.DoubleType, toType)
}
private def canCastFromDecimal(toType: DataType): SupportLevel = toType match {
case DataTypes.FloatType | DataTypes.DoubleType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType | DataTypes.LongType | DataTypes.BooleanType |
DataTypes.TimestampType =>
Compatible()
case _ => Unsupported(Some(s"Cast from DecimalType to $toType is not supported"))
}
private def canCastFromDate(toType: DataType, evalMode: CometEvalMode.Value): SupportLevel =
toType match {
case DataTypes.TimestampType =>
Compatible()
case _: TimestampNTZType =>
Compatible()
case DataTypes.BooleanType | DataTypes.ByteType | DataTypes.ShortType |
DataTypes.IntegerType | DataTypes.LongType | DataTypes.FloatType |
DataTypes.DoubleType | _: DecimalType if evalMode == CometEvalMode.LEGACY =>
Compatible()
case _ => Unsupported(Some(s"Cast from DateType to $toType is not supported"))
}
private def unsupported(fromType: DataType, toType: DataType): Unsupported = {
Unsupported(Some(s"Cast from $fromType to $toType is not supported"))
}
}