summaryrefslogtreecommitdiff
path: root/storage/mroonga/udf/mrn_udf_query_expand.cpp
blob: 03bef3215c74f30fdebdd46a03434eb8d52bf5c1 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
  Copyright(C) 2017 Kouhei Sutou <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

#include <mrn_mysql.h>
#include <mrn_mysql_compat.h>
#include <mrn_path_mapper.hpp>
#include <mrn_windows.hpp>
#include <mrn_macro.hpp>
#include <mrn_variables.hpp>
#include <mrn_database_manager.hpp>
#include <mrn_context_pool.hpp>
#include <mrn_current_thread.hpp>
#include <mrn_query_parser.hpp>

MRN_BEGIN_DECLS

extern mrn::DatabaseManager *mrn_db_manager;
extern mrn::ContextPool *mrn_context_pool;

namespace mrn {
  struct QueryExpandInfo {
    grn_ctx *ctx;
    grn_obj expanded_query;
    grn_obj *term_column;
    grn_obj *expanded_term_column;
  };
}

static void mrn_query_expand_info_free(mrn::QueryExpandInfo *info)
{
  MRN_DBUG_ENTER_FUNCTION();

  if (!info) {
    DBUG_VOID_RETURN;
  }

  if (info->ctx) {
    GRN_OBJ_FIN(info->ctx, &(info->expanded_query));
    if (grn_obj_is_accessor(info->ctx, info->expanded_term_column)) {
      grn_obj_unlink(info->ctx, info->expanded_term_column);
    }
    if (grn_obj_is_accessor(info->ctx, info->term_column)) {
      grn_obj_unlink(info->ctx, info->term_column);
    }
    mrn_context_pool->release(info->ctx);
  }
  my_free(info);

  DBUG_VOID_RETURN;
}

MRN_API my_bool mroonga_query_expand_init(UDF_INIT *init,
                                          UDF_ARGS *args,
                                          char *message)
{
  mrn::QueryExpandInfo *info = NULL;

  MRN_DBUG_ENTER_FUNCTION();

  init->ptr = NULL;
  if (args->arg_count != 4) {
    sprintf(message,
            "mroonga_query_expand(): wrong number of arguments: %u for 4",
            args->arg_count);
    goto error;
  }
  if (args->arg_type[0] != STRING_RESULT) {
    strcpy(message,
           "mroonga_query_expand(): "
           "the 1st argument must be table name as string");
    goto error;
  }
  if (args->arg_type[1] != STRING_RESULT) {
    strcpy(message,
           "mroonga_query_expand(): "
           "the 2nd argument must be term column name as string");
    goto error;
  }
  if (args->arg_type[2] != STRING_RESULT) {
    strcpy(message,
           "mroonga_query_expand(): "
           "the 3nd argument must be expanded term column name as string");
    goto error;
  }
  if (args->arg_type[3] != STRING_RESULT) {
    strcpy(message,
           "mroonga_query_expand(): "
           "the 4th argument must be query as string");
    goto error;
  }

  init->maybe_null = 1;

  info = static_cast<mrn::QueryExpandInfo *>(
    mrn_my_malloc(sizeof(mrn::QueryExpandInfo),
                  MYF(MY_WME | MY_ZEROFILL)));
  if (!info) {
    snprintf(message, MYSQL_ERRMSG_SIZE,
             "mroonga_query_expand(): failed to allocate memory");
    goto error;
  }

  {
    const char *current_db_path = MRN_THD_DB_PATH(current_thd);
    if (!current_db_path) {
      snprintf(message, MYSQL_ERRMSG_SIZE,
               "mroonga_query_expand(): no current database");
      goto error;
    }

    mrn::Database *db;
    int error = mrn_db_manager->open(current_db_path, &db);
    if (error != 0) {
      snprintf(message, MYSQL_ERRMSG_SIZE,
               "mroonga_query_expand(): failed to open database: %s",
               mrn_db_manager->error_message());
      goto error;
    }
    info->ctx = mrn_context_pool->pull();
    grn_ctx_use(info->ctx, db->get());
  }

  GRN_TEXT_INIT(&(info->expanded_query), 0);

  {
    const char *table_name = args->args[0];
    unsigned int table_name_length = args->lengths[0];
    grn_obj *table = grn_ctx_get(info->ctx,
                                 table_name,
                                 table_name_length);
    if (!table) {
      snprintf(message, MYSQL_ERRMSG_SIZE,
               "mroonga_query_expand(): table doesn't exist: <%.*s>",
               static_cast<int>(table_name_length),
               table_name);
      goto error;
    }

    const char *term_column_name = args->args[1];
    unsigned int term_column_name_length = args->lengths[1];
    info->term_column = grn_obj_column(info->ctx,
                                       table,
                                       term_column_name,
                                       term_column_name_length);
    if (!info->term_column) {
      snprintf(message, MYSQL_ERRMSG_SIZE,
               "mroonga_query_expand(): term column doesn't exist: <%.*s.%.*s>",
               static_cast<int>(table_name_length),
               table_name,
               static_cast<int>(term_column_name_length),
               term_column_name);
      goto error;
    }

    const char *expanded_term_column_name = args->args[2];
    unsigned int expanded_term_column_name_length = args->lengths[2];
    info->expanded_term_column = grn_obj_column(info->ctx,
                                                table,
                                                expanded_term_column_name,
                                                expanded_term_column_name_length);
    if (!info->expanded_term_column) {
      snprintf(message, MYSQL_ERRMSG_SIZE,
               "mroonga_query_expand(): "
               "expanded term column doesn't exist: <%.*s.%.*s>",
               static_cast<int>(table_name_length),
               table_name,
               static_cast<int>(expanded_term_column_name_length),
               expanded_term_column_name);
      goto error;
    }
  }

  init->ptr = reinterpret_cast<char *>(info);

  DBUG_RETURN(FALSE);

error:
  mrn_query_expand_info_free(info);
  DBUG_RETURN(TRUE);
}

static void query_expand(mrn::QueryExpandInfo *info, UDF_ARGS *args)
{
  grn_ctx *ctx = info->ctx;
  const char *query = args->args[3];
  unsigned int query_length = args->lengths[3];

  mrn::QueryParser query_parser(info->ctx,
                                current_thd,
                                NULL,
                                NULL,
                                0,
                                NULL);
  const char *raw_query;
  size_t raw_query_length;
  grn_operator default_operator;
  grn_expr_flags flags;
  query_parser.parse_pragma(query,
                            query_length,
                            &raw_query,
                            &raw_query_length,
                            &default_operator,
                            &flags);
  GRN_TEXT_SET(info->ctx,
               &(info->expanded_query),
               query,
               raw_query - query);
  grn_expr_syntax_expand_query_by_table(ctx,
                                        raw_query,
                                        raw_query_length,
                                        flags,
                                        info->term_column,
                                        info->expanded_term_column,
                                        &(info->expanded_query));
}

MRN_API char *mroonga_query_expand(UDF_INIT *init,
                                   UDF_ARGS *args,
                                   char *result,
                                   unsigned long *length,
                                   char *is_null,
                                   char *error)
{
  MRN_DBUG_ENTER_FUNCTION();

  mrn::QueryExpandInfo *info =
    reinterpret_cast<mrn::QueryExpandInfo *>(init->ptr);
  grn_ctx *ctx = info->ctx;

  if (!args->args[3]) {
    *is_null = 1;
    DBUG_RETURN(NULL);
  }

  *is_null = 0;

  query_expand(info, args);

  if (ctx->rc) {
    char message[MYSQL_ERRMSG_SIZE];
    snprintf(message, MYSQL_ERRMSG_SIZE,
             "mroonga_query_expand(): "
             "failed to expand: %s",
             ctx->errbuf);
    my_message(ER_ERROR_ON_WRITE, message, MYF(0));
    goto error;
  }

  *length = GRN_TEXT_LEN(&(info->expanded_query));
  DBUG_RETURN(GRN_TEXT_VALUE(&(info->expanded_query)));

error:
  *error = 1;
  DBUG_RETURN(NULL);
}

MRN_API void mroonga_query_expand_deinit(UDF_INIT *init)
{
  MRN_DBUG_ENTER_FUNCTION();
  mrn::QueryExpandInfo *info =
    reinterpret_cast<mrn::QueryExpandInfo *>(init->ptr);
  mrn_query_expand_info_free(info);
  DBUG_VOID_RETURN;
}

MRN_END_DECLS