summaryrefslogtreecommitdiffstats
path: root/src/s3select/example/s3select_example.cpp
blob: 840b62c6afe872d2a04b1b4876f9370e574f42ce (plain)
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
#include "s3select.h"
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace s3selectEngine;
using namespace BOOST_SPIRIT_CLASSIC_NS;

int cli_get_schema(const char* input_schema, actionQ& x)
{
  g_push_column.set_action_q(&x);

  rule<> column_name_rule = lexeme_d[(+alpha_p >> *digit_p)];

  //TODO an issue to resolve with trailing space
  parse_info<> info = parse(input_schema, ((column_name_rule)[BOOST_BIND_ACTION(push_column)] >> *(',' >> (column_name_rule)[BOOST_BIND_ACTION(push_column)])), space_p);

  if (!info.full)
  {
    std::cout << "failure in schema description " << input_schema << std::endl;
    return -1;
  }

  return 0;
}

int main(int argc, char** argv)
{

  //purpose: demostrate the s3select functionalities
  s3select s3select_syntax;

  char*  input_query = 0;

  for (int i = 0; i < argc; i++)
  {

    if (!strcmp(argv[i], "-q"))
    {
      input_query = argv[i + 1];
    }
  }


  if (!input_query)
  {
    std::cout << "type -q 'select ... from ...  '" << std::endl;
    return -1;
  }


  bool to_aggregate = false;

  int status = s3select_syntax.parse_query(input_query);
  if (status != 0)
  {
    std::cout << "failed to parse query " << s3select_syntax.get_error_description() << std::endl;
    return -1;
  }

  std::string object_name = s3select_syntax.get_from_clause(); //TODO stdin

  FILE* fp;

  if (object_name.compare("stdin")==0)
  {
    fp = stdin;
  }
  else
  {
    fp  = fopen(object_name.c_str(), "r");
  }


  if(!fp)
  {
    std::cout << " input stream is not valid, abort;" << std::endl;
    return -1;
  }

  struct stat statbuf;

  lstat(object_name.c_str(), &statbuf);

  std::string s3select_result;
  s3selectEngine::csv_object::csv_defintions csv;
  csv.use_header_info = false;
  //csv.column_delimiter='|';
  //csv.row_delimiter='\t';


  s3selectEngine::csv_object  s3_csv_object(&s3select_syntax, csv);
  //s3selectEngine::csv_object  s3_csv_object(&s3select_syntax);

#define BUFF_SIZE 1024*1024*4
  char* buff = (char*)malloc( BUFF_SIZE );
  while(1)
  {
    //char buff[4096];

    //char * in = fgets(buff,sizeof(buff),fp);
    size_t input_sz = fread(buff, 1, BUFF_SIZE, fp);
    char* in=buff;
    //input_sz = strlen(buff);
    //size_t input_sz = in == 0 ? 0 : strlen(in);

    //if (!input_sz) to_aggregate = true;


    //int status = s3_csv_object.run_s3select_on_object(s3select_result,in,input_sz,false,false,to_aggregate);
    int status = s3_csv_object.run_s3select_on_stream(s3select_result, in, input_sz, statbuf.st_size);
    if(status<0)
    {
      std::cout << "failure on execution " << std::endl;
      break;
    }

    if(s3select_result.size()>1)
    {
      std::cout << s3select_result;
    }

    s3select_result = "";
    if(!input_sz || feof(fp))
    {
      break;
    }

  }

  free(buff);
  fclose(fp);


}