1 | /* |
2 | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5 | * |
6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
7 | */ |
8 | |
9 | /* |
10 | * This simple module replaces the SQL rendering with a JSON rendering |
11 | * Can be called after dataflow optimizer. The result appears as a string |
12 | * in the calling environment. |
13 | */ |
14 | #include "monetdb_config.h" |
15 | #include "mal_builder.h" |
16 | #include "opt_json.h" |
17 | |
18 | str |
19 | OPTjsonImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
20 | { |
21 | int i, j, limit, slimit; |
22 | int bu = 0, br = 0, bj = 0; |
23 | str nme; |
24 | InstrPtr p,q; |
25 | int actions = 0; |
26 | InstrPtr *old; |
27 | char buf[256]; |
28 | lng usec = GDKusec(); |
29 | str msg = MAL_SUCCEED; |
30 | |
31 | (void) pci; |
32 | (void) cntxt; |
33 | (void) stk; /* to fool compilers */ |
34 | old= mb->stmt; |
35 | limit= mb->stop; |
36 | slimit = mb->ssize; |
37 | if ( newMalBlkStmt(mb,mb->stop) < 0) |
38 | throw(MAL,"optimizer.json" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
39 | |
40 | |
41 | for (i = 0; i < limit; i++) { |
42 | p = old[i]; |
43 | if( getModuleId(p) == sqlRef && getFunctionId(p) == affectedRowsRef) { |
44 | q = newInstruction(0, jsonRef, resultSetRef); |
45 | q = pushArgument(mb, q, bu); |
46 | q = pushArgument(mb, q, br); |
47 | q = pushArgument(mb, q, bj); |
48 | j = getArg(q,0); |
49 | p= getInstrPtr(mb,0); |
50 | setDestVar(q, newTmpVariable(mb, TYPE_str)); |
51 | pushInstruction(mb,p); |
52 | q = newInstruction(0, NULL, NULL); |
53 | q->barrier = RETURNsymbol; |
54 | getArg(q,0)= getArg(p,0); |
55 | pushArgument(mb,q,j); |
56 | pushInstruction(mb,q); |
57 | actions++; |
58 | continue; |
59 | } |
60 | if( getModuleId(p) == sqlRef && getFunctionId(p) == rsColumnRef) { |
61 | nme = getVarConstant(mb,getArg(p,4)).val.sval; |
62 | if (strcmp(nme,"uuid" )==0) |
63 | bu = getArg(p,7); |
64 | if (strcmp(nme,"lng" )==0) |
65 | br = getArg(p,7); |
66 | if (strcmp(nme,"json" )==0) |
67 | bj = getArg(p,7); |
68 | freeInstruction(p); |
69 | actions++; |
70 | continue; |
71 | } |
72 | pushInstruction(mb,p); |
73 | } |
74 | for(; i<slimit; i++) |
75 | if (old[i]) |
76 | freeInstruction(old[i]); |
77 | GDKfree(old); |
78 | /* Defense line against incorrect plans */ |
79 | if( actions > 0){ |
80 | chkTypes(cntxt->usermodule, mb, FALSE); |
81 | chkFlow(mb); |
82 | chkDeclarations(mb); |
83 | } |
84 | /* keep all actions taken as a post block comment */ |
85 | usec = GDKusec()- usec; |
86 | snprintf(buf,256,"%-20s actions=%2d time=" LLFMT " usec" ,"json" ,actions, usec); |
87 | newComment(mb,buf); |
88 | if( actions >= 0) |
89 | addtoMalBlkHistory(mb); |
90 | |
91 | if( OPTdebug & OPTjson){ |
92 | fprintf(stderr, "#JIT optimizer exit\n" ); |
93 | fprintFunction(stderr, mb, 0, LIST_MAL_ALL); |
94 | } |
95 | return msg; |
96 | } |
97 | |