2323import org .apache .drill .exec .expr .DrillSimpleFunc ;
2424import org .apache .drill .exec .expr .annotations .FunctionTemplate ;
2525import org .apache .drill .exec .expr .annotations .FunctionTemplate .FunctionScope ;
26+ import org .apache .drill .exec .expr .annotations .FunctionTemplate .NullHandling ;
2627import org .apache .drill .exec .expr .annotations .Output ;
2728import org .apache .drill .exec .expr .annotations .Param ;
2829import org .apache .drill .exec .expr .annotations .Workspace ;
2930import 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 ;
3231import org .apache .drill .exec .expr .holders .VarCharHolder ;
3332import org .apache .drill .exec .physical .resultSet .ResultSetLoader ;
3433import org .apache .drill .exec .server .options .OptionManager ;
34+ import org .apache .drill .exec .vector .complex .writer .BaseWriter ;
3535import org .apache .drill .exec .vector .complex .writer .BaseWriter .ComplexWriter ;
3636
3737public class JsonConvertFrom {
3838
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 () {}
11440
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 )
11643 public static class ConvertFromJsonNullableInput implements DrillSimpleFunc {
11744
118- @ Param NullableVarBinaryHolder in ;
45+ @ Param
46+ NullableVarBinaryHolder in ;
11947
12048 @ Workspace
12149 org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
@@ -126,7 +54,8 @@ public static class ConvertFromJsonNullableInput implements DrillSimpleFunc {
12654 @ Inject
12755 ResultSetLoader loader ;
12856
129- @ Output ComplexWriter writer ;
57+ @ Output
58+ BaseWriter .ComplexWriter writer ;
13059
13160 @ Override
13261 public void setup () {
@@ -137,7 +66,7 @@ public void setup() {
13766
13867 @ Override
13968 public void eval () {
140- if (in .isSet == 0 ) {
69+ if (in .end == 0 ) {
14170 // Return empty map
14271 org .apache .drill .exec .vector .complex .writer .BaseWriter .MapWriter mapWriter = writer .rootAsMap ();
14372 mapWriter .start ();
@@ -157,10 +86,15 @@ public void eval() {
15786 }
15887 }
15988
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 {
16292
163- @ Param NullableVarCharHolder in ;
93+ @ Param
94+ VarCharHolder in ;
95+
96+ @ Output
97+ ComplexWriter writer ;
16498
16599 @ Workspace
166100 org .apache .drill .exec .store .easy .json .loader .JsonLoaderImpl .JsonLoaderBuilder jsonLoaderBuilder ;
@@ -171,27 +105,27 @@ public static class ConvertFromJsonVarcharNullableInput implements DrillSimpleFu
171105 @ Inject
172106 ResultSetLoader loader ;
173107
174- @ Output ComplexWriter writer ;
175-
176108 @ Override
177109 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 ()
179111 .resultSetLoader (loader )
180112 .standardOptions (options );
181113 }
182114
183115 @ Override
184116 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 ) {
187121 org .apache .drill .exec .vector .complex .writer .BaseWriter .MapWriter mapWriter = writer .rootAsMap ();
188122 mapWriter .start ();
189123 mapWriter .end ();
190124 return ;
191125 }
192126
193127 try {
194- jsonLoaderBuilder .fromStream ( in . start , in . end , in . buffer );
128+ jsonLoaderBuilder .fromString ( jsonString );
195129 org .apache .drill .exec .store .easy .json .loader .JsonLoader jsonLoader = jsonLoaderBuilder .build ();
196130 loader .startBatch ();
197131 jsonLoader .readBatch ();
0 commit comments