Added quantiling UDAFs
[com/gs-lite.git] / src / ftacmp / gen_tuple_access.cc
1 /* ------------------------------------------------\r
2 Copyright 2014 AT&T Intellectual Property\r
3    Licensed under the Apache License, Version 2.0 (the "License");\r
4    you may not use this file except in compliance with the License.\r
5    You may obtain a copy of the License at\r
6 \r
7      http://www.apache.org/licenses/LICENSE-2.0\r
8 \r
9    Unless required by applicable law or agreed to in writing, software\r
10    distributed under the License is distributed on an "AS IS" BASIS,\r
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12    See the License for the specific language governing permissions and\r
13    limitations under the License.\r
14  ------------------------------------------- */\r
15 \r
16 #include "parse_fta.h"\r
17 #include "parse_schema.h"\r
18 #include"generate_utils.h"\r
19 \r
20 #include <string>\r
21 #include<set>\r
22 \r
23 #include <stdlib.h>\r
24 #include <stdio.h>\r
25 #include <unistd.h>\r
26 \r
27 #include<errno.h>\r
28 \r
29 #define TMPSTRLEN 1000\r
30 \r
31 #ifndef PATH_DELIM\r
32   #define PATH_DELIM '/'\r
33 #endif\r
34 \r
35 char tmp_schema_str[10000];\r
36 \r
37 //              Interface to FTA definition lexer and parser ...\r
38 \r
39 extern int FtaParserparse(void);\r
40 extern FILE *FtaParserin;\r
41 extern int FtaParserdebug;\r
42 \r
43 fta_parse_t *fta_parse_result;\r
44 var_defs_t *fta_parse_defines;\r
45 \r
46 \r
47 \r
48 using namespace std;\r
49 \r
50 \r
51 \r
52 \r
53 int main(int argc, char **argv){\r
54         int s,t,f;\r
55   char tmpstr[TMPSTRLEN];\r
56 \r
57 \r
58 \r
59 //                              set these to 1 to debug the parser\r
60   FtaParserdebug = 0;\r
61 \r
62   FILE *table_schemas_in;               // source tables definition file\r
63 \r
64   // -------------------------------\r
65   // Handling of Input Arguments\r
66   // -------------------------------\r
67     const char *optstr = "C:";\r
68         const char *usage_str = "Usage: %s [-C <config directory>] schema_file [operator name ...]\n"\r
69         "\t[-C] : use <config directory> for definition files\n";\r
70 \r
71 \r
72 //              parameters gathered from command line processing\r
73         string config_dir_path;\r
74         set<string> operator_names;\r
75         string schema_file_name;\r
76 \r
77    char chopt;\r
78    while((chopt = getopt(argc,argv,optstr)) != -1){\r
79                 switch(chopt){\r
80                 case 'C':\r
81                                 if(optarg != NULL)\r
82                                  config_dir_path = string(optarg) + string("/");\r
83                         break;\r
84                 case '?':\r
85                         fprintf(stderr,"Error, argument %c not recognized.\n",optopt);\r
86                         fprintf(stderr,"%s\n", usage_str);\r
87                         exit(1);\r
88                 default:\r
89                         fprintf(stderr,"Invalid arguments\n");\r
90                         fprintf(stderr,"%s\n", usage_str);\r
91                         exit(1);\r
92                 }\r
93         }\r
94         argc -= optind;\r
95         argv += optind;\r
96         for (int i = 0; i < argc; ++i) {\r
97                 if(schema_file_name == ""){\r
98                         schema_file_name = argv[i];\r
99                 }else{\r
100                         operator_names.insert(argv[i]);\r
101                 }\r
102         }\r
103 \r
104         if(schema_file_name == ""){\r
105                 fprintf(stderr,"%s\n", usage_str);\r
106                 exit(1);\r
107         }\r
108 \r
109 \r
110 //                      Open globally used file names.\r
111 \r
112         // prepend config directory to schema file\r
113         schema_file_name = config_dir_path + schema_file_name;\r
114 \r
115         if((table_schemas_in = fopen(schema_file_name.c_str(), "r")) == NULL) {\r
116                 fprintf(stderr,"Can't open schema file %s\n%s\n",schema_file_name.c_str(),strerror(errno));\r
117                 exit(1);\r
118         }\r
119 \r
120 \r
121 \r
122 \r
123 //                      Get an initial Schema\r
124         table_list *Schema;\r
125 //                      Parse the table schema definitions.\r
126         fta_parse_result = new fta_parse_t();\r
127         FtaParser_setfileinput(table_schemas_in);\r
128         if(FtaParserparse()){\r
129                 fprintf(stderr,"Table schema parse failed.\n");\r
130                 exit(1);\r
131         }\r
132         if(fta_parse_result->parse_type != TABLE_PARSE){\r
133                 fprintf(stderr,"ERROR, file %s is not a table definition file.\n",schema_file_name.c_str());\r
134                 exit(1);\r
135         }\r
136         Schema = fta_parse_result->tables;\r
137 \r
138 //                      Process schema field inheritance\r
139         string err_str;\r
140         int retval;\r
141         retval = Schema->unroll_tables(err_str);\r
142         if(retval){\r
143                 fprintf(stderr,"Error processing schema filed inheritance:\n %s\n", err_str.c_str() );\r
144                 exit(1);\r
145         }\r
146 \r
147 //                      Open header,  code files\r
148 \r
149         FILE *hdr_fl = fopen("operator_tuple_pack.h", "w");\r
150         if(hdr_fl == NULL){\r
151                 fprintf(stderr,"Can't open output header file operator_tuple_pack.h\n%s\n",strerror(errno));\r
152                 exit(1);\r
153         }\r
154         FILE *code_fl = fopen("operator_tuple_pack.cc", "w");\r
155         if(code_fl == NULL){\r
156                 fprintf(stderr,"Can't open output code file operator_tuple_pack.c\n%s\n",strerror(errno));\r
157                 exit(1);\r
158         }\r
159 \r
160 \r
161 \r
162 \r
163 \r
164 //                      Generate code for the accessing the operator tuples.\r
165 \r
166         string header =\r
167 "#ifndef __OPERATOR_TUPLE_PACK__\n"\r
168 "#define __OPERATOR_TUPLE_PACK__\n"\r
169 "\n"\r
170 "#include \"host_tuple.h\"\n"\r
171 "#include \"vstring.h\"\n"\r
172 "#include \"byteswap.h\""\r
173 "\n"\r
174 "void *unpack_operator_params(void *buf, int buflen, char **param_list, int plist_len, int *params_found);\n"\r
175 "\n"\r
176 "\n";\r
177 \r
178         string code =\r
179 "#include \"operator_tuple_pack.h\"\n"\r
180 "#include <sys/param.h>\n"\r
181 "#include \"fta.h\"\n"\r
182 "#include \"lapp.h\"\n"\r
183 "#include \"hfta_runtime_library.h\"\n"\r
184 "\n"\r
185 "void *unpack_operator_params(void *buf, int buflen, char **param_list, int plist_len, int *params_found){\n"\r
186 "       char *c=(char *)buf;\n"\r
187 "    int pno=0, pos=0;\n"\r
188 "       *params_found = 0;\n"\r
189 "\n"\r
190 "       for(pos=0;pos<buflen;++pos){\n"\r
191 "               if(c[pos]=='\\n' || c[pos]=='\\0') return buf;\n"\r
192 "               if(c[pos]==':') break;\n"\r
193 "       }\n"\r
194 "       if(pos == buflen) return buf;\n"\r
195 "\n"\r
196 "       pos++; \n"\r
197 "       param_list[pno]=c+pos;\n"\r
198 "       pno++;\n"\r
199 "       \n"\r
200 "       for(;pos<buflen;++pos){\n"\r
201 "               if(c[pos]==','){\n"\r
202 "                       c[pos]='\\0';\n"\r
203 "                       pos++;\n"\r
204 "                       param_list[pno]=c+pos;\n"\r
205 "                       pno++;\n"\r
206 "               }\n"\r
207 "               if(c[pos]=='\\n'){\n"\r
208 "                       c[pos]='\\0';\n"\r
209 "                       pos++;\n"\r
210 "                       *params_found = pno;\n"\r
211 "                       return c+pos;\n"\r
212 "               }\n"\r
213 "       }\n"\r
214 "       return buf;\n"\r
215 "}\n"\r
216 "\n"\r
217 "\n";\r
218 \r
219         set<string> ufcns;\r
220         vector<string> tbl_names = Schema->get_table_names();\r
221         for(t=0;t<tbl_names.size();++t){\r
222                 if(operator_names.size()==0 || operator_names.count(tbl_names[t])>0){\r
223                         int tid = Schema->find_tbl(tbl_names[t]);\r
224                         if(Schema->get_schema_type(tid) == OPERATOR_VIEW_SCHEMA){\r
225                                 table_def *optbl = Schema->get_table(tid);\r
226                                 header += generate_host_name_tuple_struct(optbl);\r
227                                 header += "int pack_"+tbl_names[t]+"_tuple(host_tuple *tup, char *buf, int len, struct "+generate_tuple_name(tbl_names[t])+" *s, unsigned char tuple_type);\n\n";\r
228                                 code += generate_host_tuple_pack(optbl);\r
229 \r
230                                 header += "extern char *"+tbl_names[t]+"_Schema_def_;\n\n";\r
231                                 code += "\tchar *"+tbl_names[t]+"_Schema_def_ = \n";\r
232                                 string schema_str = "FTA{\n\n";\r
233                                 schema_str += optbl->to_stream_string();\r
234                                 schema_str +=\r
235 "DEFINE{\n"\r
236 "\tquery_name '"+tbl_names[t]+"';\n"\r
237 "}\n"\r
238 "\n"\r
239 "Select";\r
240                                 vector<field_entry *> flds = optbl->get_fields();\r
241                                 for(f=0;f<flds.size();++f){\r
242                                         if(f>0) schema_str+=",";\r
243                                         schema_str += " " + flds[f]->get_name();\r
244                                 }\r
245                                 schema_str += "\nFrom "+tbl_names[t]+"\n}\n\n";\r
246                                 code += make_C_embedded_string(schema_str)+";\n\n";\r
247 \r
248 \r
249                                 vector<subquery_spec *> subq = Schema->get_subqueryspecs(tid);\r
250                                 for(s=0;s<subq.size();++s){\r
251                                         subquery_spec *sqs = subq[s];\r
252                                         field_entry_list fel;\r
253                                         for(f=0;f<sqs->types.size();++f){\r
254                                                 fel.append_field(new field_entry(sqs->types[f].c_str(), sqs->names[f].c_str(), "", sqs->modifiers[f],ufcns));\r
255                                         }\r
256                                         string subq_name = tbl_names[t]+"_subq_"+sqs->name;\r
257                                         table_def std(subq_name.c_str(),NULL,NULL,&fel, STREAM_SCHEMA);\r
258                                         header += generate_host_name_tuple_struct(&std);\r
259                                         header += "struct "+subq_name+"_tuple *unpack_"+subq_name+"_tuple(host_tuple *tup);\n\n";\r
260                                         code += generate_host_tuple_unpack(&std);\r
261                                 }\r
262                         }else{\r
263                                 if(operator_names.size()>0)\r
264                                         fprintf(stderr,"WARNING: %s is not an operator, skipping.\n",tbl_names[t].c_str());\r
265                         }\r
266                 }\r
267         }\r
268 \r
269         header += "\n#endif\n";\r
270 \r
271 \r
272         fprintf(hdr_fl,"%s\n",header.c_str());\r
273         fprintf(code_fl,"%s\n",code.c_str());\r
274 \r
275 ///////////////////////////////////////////////////////////////////////\r
276 \r
277         return(0);\r
278 }\r
279 \r