-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathmaps.scala
More file actions
158 lines (137 loc) · 5.72 KB
/
maps.scala
File metadata and controls
158 lines (137 loc) · 5.72 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
/*
* 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.serde
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types._
import org.apache.comet.serde.QueryPlanSerde.{createBinaryExpr, exprToProtoInternal, optExprWithInfo, scalarFunctionExprToProto}
object CometMapKeys extends CometExpressionSerde[MapKeys] {
override def convert(
expr: MapKeys,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val childExpr = exprToProtoInternal(expr.child, inputs, binding)
val mapKeysScalarExpr = scalarFunctionExprToProto("map_keys", childExpr)
optExprWithInfo(mapKeysScalarExpr, expr, expr.children: _*)
}
}
object CometMapEntries extends CometExpressionSerde[MapEntries] {
override def convert(
expr: MapEntries,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val childExpr = exprToProtoInternal(expr.child, inputs, binding)
val mapEntriesScalarExpr = scalarFunctionExprToProto("map_entries", childExpr)
optExprWithInfo(mapEntriesScalarExpr, expr, expr.children: _*)
}
}
object CometMapValues extends CometExpressionSerde[MapValues] {
override def convert(
expr: MapValues,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val childExpr = exprToProtoInternal(expr.child, inputs, binding)
val mapValuesScalarExpr = scalarFunctionExprToProto("map_values", childExpr)
optExprWithInfo(mapValuesScalarExpr, expr, expr.children: _*)
}
}
object CometMapExtract extends CometExpressionSerde[GetMapValue] {
override def convert(
expr: GetMapValue,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val mapExpr = exprToProtoInternal(expr.child, inputs, binding)
val keyExpr = exprToProtoInternal(expr.key, inputs, binding)
val mapExtractExpr = scalarFunctionExprToProto("map_extract", mapExpr, keyExpr)
optExprWithInfo(mapExtractExpr, expr, expr.children: _*)
}
}
object CometMapFromArrays extends CometExpressionSerde[MapFromArrays] {
override def convert(
expr: MapFromArrays,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val keysExpr = exprToProtoInternal(expr.left, inputs, binding)
val valuesExpr = exprToProtoInternal(expr.right, inputs, binding)
val keyType = expr.left.dataType.asInstanceOf[ArrayType].elementType
val valueType = expr.right.dataType.asInstanceOf[ArrayType].elementType
val returnType = MapType(keyType = keyType, valueType = valueType)
for {
andBinaryExprProto <- createAndBinaryExpr(expr, inputs, binding)
mapFromArraysExprProto <- scalarFunctionExprToProto("map", keysExpr, valuesExpr)
nullLiteralExprProto <- exprToProtoInternal(Literal(null, returnType), inputs, binding)
} yield {
val caseWhenExprProto = ExprOuterClass.CaseWhen
.newBuilder()
.addWhen(andBinaryExprProto)
.addThen(mapFromArraysExprProto)
.setElseExpr(nullLiteralExprProto)
.build()
ExprOuterClass.Expr
.newBuilder()
.setCaseWhen(caseWhenExprProto)
.build()
}
}
private def createAndBinaryExpr(
expr: MapFromArrays,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
createBinaryExpr(
expr,
IsNotNull(expr.left),
IsNotNull(expr.right),
inputs,
binding,
(builder, binaryExpr) => builder.setAnd(binaryExpr))
}
}
object CometMapContainsKey extends CometExpressionSerde[MapContainsKey] {
override def convert(
expr: MapContainsKey,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
// Replace with array_has(map_keys(map), key)
val mapExpr = exprToProtoInternal(expr.left, inputs, binding)
val keyExpr = exprToProtoInternal(expr.right, inputs, binding)
val mapKeysExpr = scalarFunctionExprToProto("map_keys", mapExpr)
val mapContainsKeyExpr = scalarFunctionExprToProto("array_has", mapKeysExpr, keyExpr)
optExprWithInfo(mapContainsKeyExpr, expr, expr.children: _*)
}
}
object CometMapFromEntries extends CometScalarFunction[MapFromEntries]("map_from_entries") {
val keyUnsupportedReason = "Using BinaryType as Map keys is not allowed in map_from_entries"
val valueUnsupportedReason = "Using BinaryType as Map values is not allowed in map_from_entries"
private def containsBinary(dataType: DataType): Boolean = {
dataType match {
case BinaryType => true
case StructType(fields) => fields.exists(field => containsBinary(field.dataType))
case ArrayType(elementType, _) => containsBinary(elementType)
case _ => false
}
}
override def getSupportLevel(expr: MapFromEntries): SupportLevel = {
if (containsBinary(expr.dataType.keyType)) {
return Incompatible(Some(keyUnsupportedReason))
}
if (containsBinary(expr.dataType.valueType)) {
return Incompatible(Some(valueUnsupportedReason))
}
Compatible(None)
}
}