23
23
import org .apache .drill .exec .expr .DrillSimpleFunc ;
24
24
import org .apache .drill .exec .expr .annotations .FunctionTemplate ;
25
25
import org .apache .drill .exec .expr .annotations .FunctionTemplate .FunctionScope ;
26
+ import org .apache .drill .exec .expr .annotations .FunctionTemplate .NullHandling ;
26
27
import org .apache .drill .exec .expr .annotations .Output ;
27
28
import org .apache .drill .exec .expr .annotations .Param ;
28
29
import org .apache .drill .exec .expr .annotations .Workspace ;
29
30
import org .apache .drill .exec .expr .holders .NullableVarBinaryHolder ;
30
- import org .apache .drill .exec .expr .holders .NullableVarCharHolder ;
31
- import org .apache .drill .exec .expr .holders .VarBinaryHolder ;
32
31
import org .apache .drill .exec .expr .holders .VarCharHolder ;
33
32
import org .apache .drill .exec .physical .resultSet .ResultSetLoader ;
34
33
import org .apache .drill .exec .server .options .OptionManager ;
34
+ import org .apache .drill .exec .vector .complex .writer .BaseWriter ;
35
35
import org .apache .drill .exec .vector .complex .writer .BaseWriter .ComplexWriter ;
36
36
37
37
public class JsonConvertFrom {
38
38
39
- private JsonConvertFrom () {
40
- }
41
-
42
- @ FunctionTemplate (name = "convert_fromJSON" , scope = FunctionScope .SIMPLE , isRandom = true )
43
- public static class ConvertFromJson implements DrillSimpleFunc {
44
-
45
- @ Param VarBinaryHolder in ;
46
- @ Inject
47
- ResultSetLoader loader ;
48
- @ Workspace
49
- org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
50
-
51
- @ Inject
52
- OptionManager options ;
53
-
54
- @ Output ComplexWriter writer ;
55
-
56
- @ Override
57
- public void setup () {
58
- jsonLoaderBuilder = new org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder ()
59
- .resultSetLoader (loader )
60
- .standardOptions (options );
61
- }
62
-
63
- @ Override
64
- public void eval () {
65
- try {
66
- jsonLoaderBuilder .fromStream (in .start , in .end , in .buffer );
67
- org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
68
- loader .startBatch ();
69
- jsonLoader .readBatch ();
70
- loader .close ();
71
-
72
- } catch (Exception e ) {
73
- throw new org .apache .drill .common .exceptions .DrillRuntimeException ("Error while converting from JSON. " , e );
74
- }
75
- }
76
- }
77
-
78
- @ FunctionTemplate (name = "convert_fromJSON" , scope = FunctionScope .SIMPLE , isRandom = true )
79
- public static class ConvertFromJsonVarchar implements DrillSimpleFunc {
80
-
81
- @ Param VarCharHolder in ;
82
- @ Workspace
83
- org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
84
-
85
- @ Inject
86
- OptionManager options ;
87
-
88
- @ Inject
89
- ResultSetLoader loader ;
90
-
91
- @ Output ComplexWriter writer ;
92
-
93
- @ Override
94
- public void setup () {
95
- jsonLoaderBuilder = new org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder ()
96
- .resultSetLoader (loader )
97
- .standardOptions (options );
98
- }
99
-
100
- @ Override
101
- public void eval () {
102
- try {
103
- jsonLoaderBuilder .fromStream (in .start , in .end , in .buffer );
104
- org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
105
- loader .startBatch ();
106
- jsonLoader .readBatch ();
107
- loader .close ();
108
-
109
- } catch (Exception e ) {
110
- throw new org .apache .drill .common .exceptions .DrillRuntimeException ("Error while converting from JSON. " , e );
111
- }
112
- }
113
- }
39
+ private JsonConvertFrom () {}
114
40
115
- @ FunctionTemplate (name = "convert_fromJSON" , scope = FunctionScope .SIMPLE , isRandom = true )
41
+ @ FunctionTemplate (names = {"convert_fromJSON" , "convertFromJson" , "convert_from_json" },
42
+ scope = FunctionScope .SIMPLE , nulls = NullHandling .INTERNAL )
116
43
public static class ConvertFromJsonNullableInput implements DrillSimpleFunc {
117
44
118
- @ Param NullableVarBinaryHolder in ;
45
+ @ Param
46
+ NullableVarBinaryHolder in ;
119
47
120
48
@ Workspace
121
49
org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
@@ -126,7 +54,8 @@ public static class ConvertFromJsonNullableInput implements DrillSimpleFunc {
126
54
@ Inject
127
55
ResultSetLoader loader ;
128
56
129
- @ Output ComplexWriter writer ;
57
+ @ Output
58
+ BaseWriter .ComplexWriter writer ;
130
59
131
60
@ Override
132
61
public void setup () {
@@ -137,7 +66,7 @@ public void setup() {
137
66
138
67
@ Override
139
68
public void eval () {
140
- if (in .isSet == 0 ) {
69
+ if (in .end == 0 ) {
141
70
// Return empty map
142
71
org .apache .drill .exec .vector .complex .writer .BaseWriter .MapWriter mapWriter = writer .rootAsMap ();
143
72
mapWriter .start ();
@@ -157,10 +86,15 @@ public void eval() {
157
86
}
158
87
}
159
88
160
- @ FunctionTemplate (name = "convert_fromJSON" , scope = FunctionScope .SIMPLE , isRandom = true )
161
- public static class ConvertFromJsonVarcharNullableInput implements DrillSimpleFunc {
89
+ @ FunctionTemplate (names = {"convert_fromJSON" , "convertFromJson" , "convert_from_json" },
90
+ scope = FunctionScope .SIMPLE )
91
+ public static class ConvertFromJsonVarcharInput implements DrillSimpleFunc {
162
92
163
- @ Param NullableVarCharHolder in ;
93
+ @ Param
94
+ VarCharHolder in ;
95
+
96
+ @ Output
97
+ ComplexWriter writer ;
164
98
165
99
@ Workspace
166
100
org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
@@ -171,27 +105,27 @@ public static class ConvertFromJsonVarcharNullableInput implements DrillSimpleFu
171
105
@ Inject
172
106
ResultSetLoader loader ;
173
107
174
- @ Output ComplexWriter writer ;
175
-
176
108
@ Override
177
109
public void setup () {
178
- jsonLoaderBuilder = new org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder ()
110
+ jsonLoaderBuilder = new org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder ()
179
111
.resultSetLoader (loader )
180
112
.standardOptions (options );
181
113
}
182
114
183
115
@ Override
184
116
public void eval () {
185
- if (in .isSet == 0 ) {
186
- // Return empty map
117
+ String jsonString = org .apache .drill .exec .expr .fn .impl .StringFunctionHelpers .toStringFromUTF8 (in .start , in .end , in .buffer );
118
+
119
+ // If the input is null or empty, return an empty map
120
+ if (jsonString .length () == 0 ) {
187
121
org .apache .drill .exec .vector .complex .writer .BaseWriter .MapWriter mapWriter = writer .rootAsMap ();
188
122
mapWriter .start ();
189
123
mapWriter .end ();
190
124
return ;
191
125
}
192
126
193
127
try {
194
- jsonLoaderBuilder .fromStream ( in . start , in . end , in . buffer );
128
+ jsonLoaderBuilder .fromString ( jsonString );
195
129
org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
196
130
loader .startBatch ();
197
131
jsonLoader .readBatch ();
0 commit comments