CUBRID Engine  latest
arithmetic.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * arithmetic.c - arithmetic functions
21  */
22 
23 #ident "$Id$"
24 
25 #include "arithmetic.h"
26 
27 #include "config.h"
28 #include "crypt_opfunc.h"
29 #include "db_date.h"
30 #include "db_json.hpp"
31 #include "db_json_path.hpp"
32 #include "dbtype.h"
33 #include "error_manager.h"
36 #include "numeric_opfunc.h"
37 #include "object_primitive.h"
38 #include "object_representation.h"
39 #include "string_opfunc.h"
40 #include "tz_support.h"
41 
42 #include <algorithm>
43 #include <assert.h>
44 #include <cctype>
45 #include <float.h>
46 #include <math.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #if defined(SOLARIS)
51 #include <ieeefp.h>
52 #endif
53 
54 
55 #if defined (SUPPRESS_STRLEN_WARNING)
56 #define strlen(s1) ((int) strlen(s1))
57 #endif /* defined (SUPPRESS_STRLEN_WARNING) */
58 
59 static int db_mod_short (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
60 static int db_mod_int (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
61 static int db_mod_bigint (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
62 static int db_mod_float (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
63 static int db_mod_double (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
64 static int db_mod_string (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
65 static int db_mod_numeric (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
66 static int db_mod_monetary (DB_VALUE * value, DB_VALUE * value1, DB_VALUE * value2);
67 static double round_double (double num, double integer);
68 static int move_n_days (int *monthp, int *dayp, int *yearp, const int interval);
69 static int round_date (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2);
70 static double truncate_double (double num, double integer);
71 static DB_BIGINT truncate_bigint (DB_BIGINT num, DB_BIGINT integer);
72 static int truncate_date (DB_DATE * date, const DB_VALUE * format_str);
73 static int get_number_dbval_as_double (double *d, const DB_VALUE * value);
74 static int get_number_dbval_as_long_double (long double *ld, const DB_VALUE * value);
75 static int db_width_bucket_calculate_numeric (double *result, const DB_VALUE * value1, const DB_VALUE * value2,
76  const DB_VALUE * value3, const DB_VALUE * value4);
77 static int is_str_find_all (DB_VALUE * val, bool & find_all);
78 static bool is_any_arg_null (DB_VALUE * const *args, int num_args);
79 
80 /*
81  * db_floor_dbval () - take floor of db_value
82  * return: NO_ERROR
83  * result(out) : resultant db_value
84  * value(in) : input db_value
85  */
86 int
87 db_floor_dbval (DB_VALUE * result, DB_VALUE * value)
88 {
89  DB_TYPE res_type;
90  double dtmp;
91  int er_status = NO_ERROR;
92  DB_VALUE cast_value;
93 
94  res_type = DB_VALUE_DOMAIN_TYPE (value);
95  if (res_type == DB_TYPE_NULL || DB_IS_NULL (value))
96  {
97  return er_status;
98  }
99 
100  switch (res_type)
101  {
102  case DB_TYPE_SHORT:
103  db_make_short (result, db_get_short (value));
104  break;
105  case DB_TYPE_INTEGER:
106  db_make_int (result, db_get_int (value));
107  break;
108  case DB_TYPE_BIGINT:
109  db_make_bigint (result, db_get_bigint (value));
110  break;
111  case DB_TYPE_FLOAT:
112  dtmp = floor (db_get_float (value));
113  db_make_float (result, (float) dtmp);
114  break;
115  case DB_TYPE_CHAR:
116  case DB_TYPE_VARCHAR:
117  case DB_TYPE_NCHAR:
118  case DB_TYPE_VARNCHAR:
119  db_make_null (&cast_value);
120  er_status = tp_value_str_auto_cast_to_number (value, &cast_value, &res_type);
121  if (er_status != NO_ERROR
123  {
124  return er_status;
125  }
126 
127  assert (res_type == DB_TYPE_DOUBLE);
128 
129  value = &cast_value;
130 
131  /* fall through */
132 
133  case DB_TYPE_DOUBLE:
134  dtmp = floor (db_get_double (value));
135  db_make_double (result, (double) dtmp);
136  break;
137  case DB_TYPE_NUMERIC:
138  {
139  int p = DB_VALUE_PRECISION (value), s = DB_VALUE_SCALE (value);
140 
141  if (s)
142  {
143  unsigned char num[DB_NUMERIC_BUF_SIZE];
144  char num_str[DB_MAX_NUMERIC_PRECISION * 4 + 2] = { '\0' };
145  char *num_str_p;
146  int num_str_len;
147  bool decrement = false;
148 
149  num_str_p = num_str + 1;
150  numeric_coerce_num_to_dec_str (db_get_numeric (value), num_str_p);
151  num_str_len = strlen (num_str_p);
152 
153  num_str_p += num_str_len - s;
154 
155  while (*num_str_p)
156  {
157  if (*num_str_p != '0')
158  {
159  *num_str_p = '0';
160  decrement = true;
161  }
162 
163  num_str_p++;
164  }
165 
166  if (decrement && num_str[1] == '-')
167  {
168  /* To decrement a negative value, the absolute value (the digits) actually has to be incremented. */
169 
170  char *num_str_digits = num_str + num_str_len - p;
171  bool carry = true;
172 
173  num_str_p = num_str + num_str_len - s;
174  while (*num_str_p == '9')
175  {
176  *num_str_p-- = '0';
177  }
178 
179  if (*num_str_p == '-')
180  {
181  num_str[0] = '-';
182  *num_str_p = '1';
183  }
184  else
185  {
186  (*num_str_p)++;
187  carry = false;
188  }
189 
190  if (carry || num_str_p <= num_str_digits)
191  {
192  if (p < DB_MAX_NUMERIC_PRECISION)
193  {
194  p++;
195  }
196  else
197  {
198  s--;
199  num_str[num_str_len] = '\0';
200  }
201  }
202 
203  if (num_str[0])
204  {
205  num_str_p = num_str;
206  }
207  else
208  {
209  num_str_p = num_str + 1;
210  }
211 
212  numeric_coerce_dec_str_to_num (num_str_p, num);
213  db_make_numeric (result, num, p, s);
214  }
215  else
216  {
217  /* given numeric is positive or already rounded */
218  numeric_coerce_dec_str_to_num (num_str + 1, num);
219  db_make_numeric (result, num, p, s);
220  }
221  }
222  else
223  {
224  /* given numeric number is already of integral type */
225  db_make_numeric (result, db_get_numeric (value), p, 0);
226  }
227 
228  break;
229  }
230  case DB_TYPE_MONETARY:
231  dtmp = (db_get_monetary (value))->amount;
232  dtmp = floor (dtmp);
233  db_make_monetary (result, (db_get_monetary (value))->type, dtmp);
234  break;
235  default:
237  {
238  er_status = ER_QPROC_INVALID_DATATYPE;
239  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
240  }
241  break;
242  }
243 
244  return er_status;
245 }
246 
247 /*
248  * db_ceil_dbval () - take ceil of db_value
249  * return: NO_ERROR
250  * result(out) : resultant db_value
251  * value(in) : input db_value
252  */
253 int
254 db_ceil_dbval (DB_VALUE * result, DB_VALUE * value)
255 {
256  DB_TYPE res_type;
257  double dtmp;
258  int er_status = NO_ERROR;
259  DB_VALUE cast_value;
260 
261  res_type = DB_VALUE_DOMAIN_TYPE (value);
262  if (res_type == DB_TYPE_NULL || DB_IS_NULL (value))
263  {
264  return er_status;
265  }
266 
267  switch (res_type)
268  {
269  case DB_TYPE_SHORT:
270  db_make_short (result, db_get_short (value));
271  break;
272  case DB_TYPE_INTEGER:
273  db_make_int (result, db_get_int (value));
274  break;
275  case DB_TYPE_BIGINT:
276  db_make_bigint (result, db_get_bigint (value));
277  break;
278  case DB_TYPE_FLOAT:
279  dtmp = ceil (db_get_float (value));
280  db_make_float (result, (float) dtmp);
281  break;
282  case DB_TYPE_CHAR:
283  case DB_TYPE_VARCHAR:
284  case DB_TYPE_NCHAR:
285  case DB_TYPE_VARNCHAR:
286  db_make_null (&cast_value);
287  er_status = tp_value_str_auto_cast_to_number (value, &cast_value, &res_type);
288  if (er_status != NO_ERROR
290  {
291  return er_status;
292  }
293 
294  assert (res_type == DB_TYPE_DOUBLE);
295 
296  value = &cast_value;
297 
298  /* fall through */
299 
300  case DB_TYPE_DOUBLE:
301  dtmp = ceil (db_get_double (value));
302  db_make_double (result, (double) dtmp);
303  break;
304  case DB_TYPE_NUMERIC:
305  {
306  int s = DB_VALUE_SCALE (value), p = DB_VALUE_PRECISION (value);
307 
308  if (s)
309  {
310  char num_str[DB_MAX_NUMERIC_PRECISION * 4 + 2] = { '\0' };
311  char *num_str_p;
312  int num_str_len = 0;
313  bool increment = false;
314 
315  num_str_p = num_str + 1;
317  if (num_str_p[0] == '-')
318  {
319  num_str_p++;
320  }
321 
322  num_str_len = strlen (num_str_p);
323  num_str_p += num_str_len - s;
324 
325  while (*num_str_p)
326  {
327  if (*num_str_p != '0')
328  {
329  increment = true;
330  *num_str_p = '0';
331  }
332 
333  num_str_p++;
334  }
335 
336  if (increment)
337  {
338  unsigned char num[DB_NUMERIC_BUF_SIZE];
339  if (num_str[1] == '-')
340  {
341  /* CEIL(-3.1) is -3.0, as opposed to CEIL(+3.1) which is 4 */
342  numeric_coerce_dec_str_to_num (num_str + 1, num);
343  db_make_numeric (result, num, p, s);
344  }
345  else
346  {
347  bool carry = true;
348  char *num_str_digits = num_str + 1 + num_str_len - p;
349 
350  /* position num_str_p one digit in front of the decimal point */
351  num_str_p = num_str;
352  num_str_p += num_str_len - s;
353 
354  while (*num_str_p == '9')
355  {
356  *num_str_p-- = '0';
357  }
358 
359  if (*num_str_p)
360  {
361  (*num_str_p)++;
362  carry = false;
363  }
364 
365  if (carry || num_str_p < num_str_digits)
366  {
367  if (carry)
368  {
369  *num_str_p = '1';
370  }
371 
372  if (p < DB_MAX_NUMERIC_PRECISION)
373  {
374  p++;
375  }
376  else
377  {
378  num_str[num_str_len] = '\0';
379  s--;
380  }
381  }
382  else
383  {
384  num_str_p = num_str + 1;
385  }
386 
387  numeric_coerce_dec_str_to_num (num_str_p, num);
388  db_make_numeric (result, num, p, s);
389  }
390  }
391  else
392  {
393  /* the given numeric value is already an integer */
394  db_make_numeric (result, db_locate_numeric (value), p, s);
395  }
396  }
397  else
398  {
399  /* the given numeric value has a scale of 0 */
400  db_make_numeric (result, db_locate_numeric (value), p, 0);
401  }
402 
403  break;
404  }
405  case DB_TYPE_MONETARY:
406  dtmp = (db_get_monetary (value))->amount;
407  dtmp = ceil (dtmp);
408  db_make_monetary (result, (db_get_monetary (value))->type, dtmp);
409  break;
410  default:
412  {
413  er_status = ER_QPROC_INVALID_DATATYPE;
414  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
415  }
416  break;
417  }
418 
419  return er_status;
420 }
421 
422 /*
423  * db_sign_dbval - take sign of db_value
424  * return: NO_ERROR
425  * result(out) : resultant db_value
426  * value(in) : input db_value
427  */
428 int
429 db_sign_dbval (DB_VALUE * result, DB_VALUE * value)
430 {
431  DB_TYPE res_type;
432  int itmp;
433  DB_BIGINT bitmp;
434  double dtmp;
435  int er_status = NO_ERROR;
436 
437  res_type = DB_VALUE_DOMAIN_TYPE (value);
438 
439  if (res_type == DB_TYPE_NULL || DB_IS_NULL (value))
440  {
441  return er_status;
442  }
443 
444  switch (res_type)
445  {
446  case DB_TYPE_SHORT:
447  itmp = db_get_short (value);
448  if (itmp == 0)
449  {
450  db_make_int (result, 0);
451  }
452  else if (itmp < 0)
453  {
454  db_make_int (result, -1);
455  }
456  else
457  {
458  db_make_int (result, 1);
459  }
460  break;
461  case DB_TYPE_INTEGER:
462  itmp = db_get_int (value);
463  if (itmp == 0)
464  {
465  db_make_int (result, 0);
466  }
467  else if (itmp < 0)
468  {
469  db_make_int (result, -1);
470  }
471  else
472  {
473  db_make_int (result, 1);
474  }
475  break;
476  case DB_TYPE_BIGINT:
477  bitmp = db_get_bigint (value);
478  if (bitmp == 0)
479  {
480  db_make_int (result, 0);
481  }
482  else if (bitmp < 0)
483  {
484  db_make_int (result, -1);
485  }
486  else
487  {
488  db_make_int (result, 1);
489  }
490  break;
491  case DB_TYPE_FLOAT:
492  dtmp = db_get_float (value);
493  if (dtmp == 0)
494  {
495  db_make_int (result, 0);
496  }
497  else if (dtmp < 0)
498  {
499  db_make_int (result, -1);
500  }
501  else
502  {
503  db_make_int (result, 1);
504  }
505  break;
506  case DB_TYPE_DOUBLE:
507  dtmp = db_get_double (value);
508  if (dtmp == 0)
509  {
510  db_make_int (result, 0);
511  }
512  else if (dtmp < 0)
513  {
514  db_make_int (result, -1);
515  }
516  else
517  {
518  db_make_int (result, 1);
519  }
520  break;
521  case DB_TYPE_NUMERIC:
523  if (dtmp == 0)
524  {
525  db_make_int (result, 0);
526  }
527  else if (dtmp < 0)
528  {
529  db_make_int (result, -1);
530  }
531  else
532  {
533  db_make_int (result, 1);
534  }
535  break;
536  case DB_TYPE_MONETARY:
537  dtmp = (db_get_monetary (value))->amount;
538  if (dtmp == 0)
539  {
540  db_make_int (result, 0);
541  }
542  else if (dtmp < 0)
543  {
544  db_make_int (result, -1);
545  }
546  else
547  {
548  db_make_int (result, 1);
549  }
550  break;
551  default:
552  break;
553  }
554 
555  return er_status;
556 }
557 
558 /*
559  * db_abs_dbval () - take absolute value of db_value
560  * return: NO_ERROR
561  * result(out) : resultant db_value
562  * value(in) : input db_value
563  */
564 int
565 db_abs_dbval (DB_VALUE * result, DB_VALUE * value)
566 {
567  DB_TYPE res_type;
568  short stmp;
569  int itmp;
570  DB_BIGINT bitmp;
571  double dtmp;
572  int er_status = NO_ERROR;
573  DB_VALUE cast_value;
574 
575  res_type = DB_VALUE_DOMAIN_TYPE (value);
576  if (res_type == DB_TYPE_NULL || DB_IS_NULL (value))
577  {
578  return er_status;
579  }
580 
581  switch (res_type)
582  {
583  case DB_TYPE_SHORT:
584  stmp = db_get_short (value);
585  stmp = abs (stmp);
586  db_make_short (result, stmp);
587  break;
588  case DB_TYPE_INTEGER:
589  itmp = db_get_int (value);
590  itmp = abs (itmp);
591  db_make_int (result, itmp);
592  break;
593  case DB_TYPE_BIGINT:
594  bitmp = db_get_bigint (value);
595  bitmp = llabs (bitmp);
596  db_make_bigint (result, bitmp);
597  break;
598  case DB_TYPE_FLOAT:
599  dtmp = db_get_float (value);
600  dtmp = fabs (dtmp);
601  db_make_float (result, (float) dtmp);
602  break;
603 
604  case DB_TYPE_CHAR:
605  case DB_TYPE_VARCHAR:
606  case DB_TYPE_NCHAR:
607  case DB_TYPE_VARNCHAR:
608  db_make_null (&cast_value);
609  er_status = tp_value_str_auto_cast_to_number (value, &cast_value, &res_type);
610  if (er_status != NO_ERROR
612  {
613  return er_status;
614  }
615 
616  assert (res_type == DB_TYPE_DOUBLE);
617 
618  value = &cast_value;
619 
620  /* fall through */
621 
622  case DB_TYPE_DOUBLE:
623  dtmp = db_get_double (value);
624  dtmp = fabs (dtmp);
625  db_make_double (result, (double) dtmp);
626  break;
627  case DB_TYPE_NUMERIC:
628  {
629  unsigned char num[DB_NUMERIC_BUF_SIZE];
630 
632  db_make_numeric (result, num, DB_VALUE_PRECISION (value), DB_VALUE_SCALE (value));
633  break;
634  }
635  case DB_TYPE_MONETARY:
636  dtmp = (db_get_monetary (value))->amount;
637  dtmp = fabs (dtmp);
638  db_make_monetary (result, (db_get_monetary (value))->type, dtmp);
639  break;
640  default:
642  {
643  er_status = ER_QPROC_INVALID_DATATYPE;
644  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
645  }
646  break;
647  }
648 
649  return er_status;
650 }
651 
652 /*
653  * db_exp_dbval () - take exponential value of db_value
654  * return: NO_ERROR
655  * result(out) : resultant db_value
656  * value(in) : input db_value
657  */
658 int
659 db_exp_dbval (DB_VALUE * result, DB_VALUE * value)
660 {
661  DB_TYPE type;
662  short s;
663  int i;
664  float f;
665  double d;
666  double dtmp;
667  DB_BIGINT bi;
668 
669  type = DB_VALUE_DOMAIN_TYPE (value);
670 
671  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
672  {
673  return NO_ERROR;
674  }
675 
676  switch (type)
677  {
678  case DB_TYPE_SHORT:
679  s = db_get_short (value);
680  dtmp = exp ((double) s);
681  break;
682  case DB_TYPE_INTEGER:
683  i = db_get_int (value);
684  dtmp = exp ((double) i);
685  break;
686  case DB_TYPE_BIGINT:
687  bi = db_get_bigint (value);
688  dtmp = exp ((double) bi);
689  break;
690  case DB_TYPE_FLOAT:
691  f = db_get_float (value);
692  dtmp = exp (f);
693  break;
694  case DB_TYPE_DOUBLE:
695  d = db_get_double (value);
696  dtmp = exp (d);
697  break;
698  case DB_TYPE_NUMERIC:
700  dtmp = exp (d);
701  break;
702  case DB_TYPE_MONETARY:
703  d = (db_get_monetary (value))->amount;
704  dtmp = exp (d);
705  break;
706  default:
708  return ER_FAILED;
709  }
710 
711  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
712  {
713  goto exp_overflow;
714  }
715 
716  db_make_double (result, dtmp);
717  return NO_ERROR;
718 
719 exp_overflow:
721  return ER_FAILED;
722 }
723 
724 /*
725  * db_sqrt_dbval () - take sqrt value of db_value
726  * return: NO_ERROR
727  * result(out): resultant db_value
728  * value(in) : input db_value
729  */
730 int
731 db_sqrt_dbval (DB_VALUE * result, DB_VALUE * value)
732 {
733  DB_TYPE type;
734  short s;
735  int i;
736  float f;
737  double d;
738  double dtmp;
739  DB_BIGINT bi;
740 
741  type = DB_VALUE_DOMAIN_TYPE (value);
742 
743  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
744  {
745  return NO_ERROR;
746  }
747 
748  switch (type)
749  {
750  case DB_TYPE_SHORT:
751  s = db_get_short (value);
752  if (s < 0)
753  {
754  goto sqrt_error;
755  }
756  dtmp = sqrt ((double) s);
757  break;
758  case DB_TYPE_INTEGER:
759  i = db_get_int (value);
760  if (i < 0)
761  {
762  goto sqrt_error;
763  }
764  dtmp = sqrt ((double) i);
765  break;
766  case DB_TYPE_BIGINT:
767  bi = db_get_bigint (value);
768  if (bi < 0)
769  {
770  goto sqrt_error;
771  }
772  dtmp = sqrt ((double) bi);
773  break;
774  case DB_TYPE_FLOAT:
775  f = db_get_float (value);
776  if (f < 0)
777  {
778  goto sqrt_error;
779  }
780  dtmp = sqrt (f);
781  break;
782  case DB_TYPE_DOUBLE:
783  d = db_get_double (value);
784  if (d < 0)
785  {
786  goto sqrt_error;
787  }
788  dtmp = sqrt (d);
789  break;
790  case DB_TYPE_NUMERIC:
792  if (d < 0)
793  {
794  goto sqrt_error;
795  }
796  dtmp = sqrt (d);
797  break;
798  case DB_TYPE_MONETARY:
799  d = (db_get_monetary (value))->amount;
800  if (d < 0)
801  {
802  goto sqrt_error;
803  }
804  dtmp = sqrt (d);
805  break;
806  default:
808  return ER_FAILED;
809  break;
810  }
811 
812  db_make_double (result, dtmp);
813  return NO_ERROR;
814 
815 sqrt_error:
817  {
818  db_make_null (result);
819  return NO_ERROR;
820  }
821  else
822  {
824  }
825  return ER_FAILED;
826 }
827 
828 /*
829  * db_power_dbval () - take power value of db_value
830  * return: NO_ERROR, ER_FAILED
831  * result(out) : resultant db_value
832  * value1(in) : first db_value
833  * value2(in) : second db_value
834  */
835 int
836 db_power_dbval (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
837 {
838  double d1, d2;
839  double dtmp;
840  int error = NO_ERROR;
841 
842  if (DB_IS_NULL (value1) || DB_IS_NULL (value2))
843  {
844  db_make_null (result);
845  return NO_ERROR;
846  }
847 
848  error = get_number_dbval_as_double (&d1, value1);
849  if (error != NO_ERROR)
850  {
851  goto pow_error;
852  }
853  error = get_number_dbval_as_double (&d2, value2);
854  if (error != NO_ERROR)
855  {
856  goto pow_error;
857  }
858 
859  if (d1 < 0 && d2 != ceil (d2))
860  {
862  goto pow_error;
863  }
864 
865  dtmp = pow (d1, d2);
866  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
867  {
868  goto pow_overflow;
869  }
870 
871  db_make_double (result, dtmp);
872 
873  return NO_ERROR;
874 
875 pow_overflow:
877  {
878  db_make_null (result);
879  return NO_ERROR;
880  }
881  else
882  {
884  return ER_FAILED;
885  }
886 
887 pow_error:
889  {
890  db_make_null (result);
891  return NO_ERROR;
892  }
893  else
894  {
895  return ER_FAILED;
896  }
897 }
898 
899 /*
900  * db_mod_short () - take mod value of value1(short) with value2
901  * return: NO_ERROR, ER_FAILED
902  * result(out) : resultant db_value
903  * value1(in) : short db_value
904  * value2(in) : second db_value
905  */
906 static int
907 db_mod_short (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
908 {
909 #if !defined(NDEBUG)
910  DB_TYPE type1;
911 #endif
912  DB_TYPE type2;
913  short s1, s2;
914  int i2;
915  float f2;
916  double d2;
917  DB_BIGINT bi2;
918  double dtmp;
919  DB_DATA_STATUS data_stat;
920  unsigned char num[DB_NUMERIC_BUF_SIZE];
921  int p, s;
922  int er_status = NO_ERROR;
923  DB_VALUE cast_value2;
924 
925  assert (result != NULL && value1 != NULL && value2 != NULL);
926 
927  db_make_null (&cast_value2);
928 
929 #if !defined(NDEBUG)
930  type1 = DB_VALUE_DOMAIN_TYPE (value1);
931  assert (type1 == DB_TYPE_SHORT);
932 #endif
933 
934  s1 = db_get_short (value1);
935 
936  type2 = DB_VALUE_DOMAIN_TYPE (value2);
937  switch (type2)
938  {
939  case DB_TYPE_SHORT:
940  s2 = db_get_short (value2);
941  if (s2 == 0)
942  {
943  db_make_short (result, s1);
944  }
945  else
946  {
947  db_make_short (result, (short) (s1 % s2));
948  }
949  break;
950  case DB_TYPE_INTEGER:
951  i2 = db_get_int (value2);
952  if (i2 == 0)
953  {
954  db_make_int (result, s1);
955  }
956  else
957  {
958  db_make_int (result, (int) (s1 % i2));
959  }
960  break;
961  case DB_TYPE_BIGINT:
962  bi2 = db_get_bigint (value2);
963  if (bi2 == 0)
964  {
965  db_make_bigint (result, s1);
966  }
967  else
968  {
969  db_make_bigint (result, (DB_BIGINT) (s1 % bi2));
970  }
971  break;
972  case DB_TYPE_FLOAT:
973  f2 = db_get_float (value2);
974  if (f2 == 0)
975  {
976  db_make_float (result, s1);
977  }
978  else
979  {
980  db_make_float (result, (float) fmod ((float) s1, f2));
981  }
982  break;
983  case DB_TYPE_CHAR:
984  case DB_TYPE_VARCHAR:
985  case DB_TYPE_NCHAR:
986  case DB_TYPE_VARNCHAR:
987  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
988  if (er_status != NO_ERROR
990  {
991  goto exit;
992  }
993 
994  assert (type2 == DB_TYPE_DOUBLE);
995 
996  value2 = &cast_value2;
997 
998  /* fall through */
999 
1000  case DB_TYPE_DOUBLE:
1001  d2 = db_get_double (value2);
1002  if (d2 == 0)
1003  {
1004  db_make_double (result, s1);
1005  }
1006  else
1007  {
1008  db_make_double (result, (double) fmod ((double) s1, d2));
1009  }
1010  break;
1011  case DB_TYPE_NUMERIC:
1013  if (d2 == 0)
1014  {
1015  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1016  }
1017  else
1018  {
1019  dtmp = fmod ((double) s1, d2);
1020  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value2), num, &p, &s);
1021  db_make_numeric (result, num, p, s);
1022  }
1023  break;
1024  case DB_TYPE_MONETARY:
1025  d2 = (db_get_monetary (value2))->amount;
1026  if (d2 == 0)
1027  {
1028  db_make_monetary (result, (db_get_monetary (value2))->type, s1);
1029  }
1030  else
1031  {
1032  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod (s1, d2));
1033  }
1034  break;
1035  default:
1037  {
1039  er_status = ER_QPROC_INVALID_DATATYPE;
1040  }
1041  goto exit;
1042  }
1043 
1044 exit:
1045  return er_status;
1046 }
1047 
1048 /*
1049  * db_mod_int () - take mod value of value1(int) with value2
1050  * return: NO_ERROR, ER_FAILED
1051  * result(out) : resultant db_value
1052  * value1(in) : int db_value
1053  * value2(in) : second db_value
1054  */
1055 static int
1056 db_mod_int (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1057 {
1058 #if !defined(NDEBUG)
1059  DB_TYPE type1;
1060 #endif
1061  DB_TYPE type2;
1062  short s2;
1063  int i1, i2;
1064  float f2;
1065  double d2;
1066  DB_BIGINT bi2;
1067  double dtmp;
1068  DB_DATA_STATUS data_stat;
1069  unsigned char num[DB_NUMERIC_BUF_SIZE];
1070  int p, s;
1071  int er_status = NO_ERROR;
1072  DB_VALUE cast_value2;
1073 
1074  assert (result != NULL && value1 != NULL && value2 != NULL);
1075 
1076  db_make_null (&cast_value2);
1077 
1078 #if !defined(NDEBUG)
1079  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1080  assert (type1 == DB_TYPE_INTEGER);
1081 #endif
1082 
1083  i1 = db_get_int (value1);
1084 
1085  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1086  switch (type2)
1087  {
1088  case DB_TYPE_SHORT:
1089  s2 = db_get_short (value2);
1090  if (s2 == 0)
1091  {
1092  db_make_int (result, i1);
1093  }
1094  else
1095  {
1096  db_make_int (result, (int) (i1 % s2));
1097  }
1098  break;
1099  case DB_TYPE_INTEGER:
1100  i2 = db_get_int (value2);
1101  if (i2 == 0)
1102  {
1103  db_make_int (result, i1);
1104  }
1105  else
1106  {
1107  db_make_int (result, (int) (i1 % i2));
1108  }
1109  break;
1110  case DB_TYPE_BIGINT:
1111  bi2 = db_get_bigint (value2);
1112  if (bi2 == 0)
1113  {
1114  db_make_bigint (result, i1);
1115  }
1116  else
1117  {
1118  db_make_bigint (result, (DB_BIGINT) (i1 % bi2));
1119  }
1120  break;
1121  case DB_TYPE_FLOAT:
1122  f2 = db_get_float (value2);
1123  if (f2 == 0)
1124  {
1125  db_make_float (result, (float) i1);
1126  }
1127  else
1128  {
1129  db_make_float (result, (float) fmod ((float) i1, f2));
1130  }
1131  break;
1132  case DB_TYPE_CHAR:
1133  case DB_TYPE_VARCHAR:
1134  case DB_TYPE_NCHAR:
1135  case DB_TYPE_VARNCHAR:
1136  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1137  if (er_status != NO_ERROR
1139  {
1140  goto exit;
1141  }
1142 
1143  assert (type2 == DB_TYPE_DOUBLE);
1144 
1145  value2 = &cast_value2;
1146 
1147  /* fall through */
1148 
1149  case DB_TYPE_DOUBLE:
1150  d2 = db_get_double (value2);
1151  if (d2 == 0)
1152  {
1153  db_make_double (result, i1);
1154  }
1155  else
1156  {
1157  db_make_double (result, (double) fmod ((double) i1, d2));
1158  }
1159  break;
1160  case DB_TYPE_NUMERIC:
1162  if (d2 == 0)
1163  {
1164  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1165  }
1166  else
1167  {
1168  dtmp = fmod ((double) i1, d2);
1169  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value2), num, &p, &s);
1170  db_make_numeric (result, num, p, s);
1171  }
1172  break;
1173  case DB_TYPE_MONETARY:
1174  d2 = (db_get_monetary (value2))->amount;
1175  if (d2 == 0)
1176  {
1177  db_make_monetary (result, (db_get_monetary (value2))->type, i1);
1178  }
1179  else
1180  {
1181  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod ((double) i1, d2));
1182  }
1183  break;
1184  default:
1186  {
1188  er_status = ER_QPROC_INVALID_DATATYPE;
1189  }
1190  goto exit;
1191  }
1192 
1193 exit:
1194  return er_status;
1195 }
1196 
1197 /*
1198  * db_mod_bigint () - take mod value of value1(bigint) with value2
1199  * return: NO_ERROR, ER_FAILED
1200  * result(out) : resultant db_value
1201  * value1(in) : bigint db_value
1202  * value2(in) : second db_value
1203  */
1204 static int
1205 db_mod_bigint (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1206 {
1207 #if !defined(NDEBUG)
1208  DB_TYPE type1;
1209 #endif
1210  DB_TYPE type2;
1211  short s2;
1212  int i2;
1213  float f2;
1214  double d2;
1215  DB_BIGINT bi1, bi2;
1216  double dtmp;
1217  DB_DATA_STATUS data_stat;
1218  unsigned char num[DB_NUMERIC_BUF_SIZE];
1219  int p, s;
1220  int er_status = NO_ERROR;
1221  DB_VALUE cast_value2;
1222 
1223  assert (result != NULL && value1 != NULL && value2 != NULL);
1224 
1225  db_make_null (&cast_value2);
1226 
1227 #if !defined(NDEBUG)
1228  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1229  assert (type1 == DB_TYPE_BIGINT);
1230 #endif
1231 
1232  bi1 = db_get_bigint (value1);
1233 
1234  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1235  switch (type2)
1236  {
1237  case DB_TYPE_SHORT:
1238  s2 = db_get_short (value2);
1239  if (s2 == 0)
1240  {
1241  db_make_bigint (result, bi1);
1242  }
1243  else
1244  {
1245  db_make_bigint (result, (DB_BIGINT) (bi1 % s2));
1246  }
1247  break;
1248  case DB_TYPE_INTEGER:
1249  i2 = db_get_int (value2);
1250  if (i2 == 0)
1251  {
1252  db_make_bigint (result, bi1);
1253  }
1254  else
1255  {
1256  db_make_bigint (result, (DB_BIGINT) (bi1 % i2));
1257  }
1258  break;
1259  case DB_TYPE_BIGINT:
1260  bi2 = db_get_bigint (value2);
1261  if (bi2 == 0)
1262  {
1263  db_make_bigint (result, bi1);
1264  }
1265  else
1266  {
1267  db_make_bigint (result, (DB_BIGINT) (bi1 % bi2));
1268  }
1269  break;
1270  case DB_TYPE_FLOAT:
1271  f2 = db_get_float (value2);
1272  if (f2 == 0)
1273  {
1274  db_make_float (result, (float) bi1);
1275  }
1276  else
1277  {
1278  db_make_float (result, (float) fmod ((double) bi1, (double) f2));
1279  }
1280  break;
1281  case DB_TYPE_CHAR:
1282  case DB_TYPE_VARCHAR:
1283  case DB_TYPE_NCHAR:
1284  case DB_TYPE_VARNCHAR:
1285  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1286  if (er_status != NO_ERROR
1288  {
1289  goto exit;
1290  }
1291 
1292  assert (type2 == DB_TYPE_DOUBLE);
1293 
1294  value2 = &cast_value2;
1295 
1296  /* fall through */
1297 
1298  case DB_TYPE_DOUBLE:
1299  d2 = db_get_double (value2);
1300  if (d2 == 0)
1301  {
1302  db_make_double (result, (double) bi1);
1303  }
1304  else
1305  {
1306  db_make_double (result, (double) fmod ((double) bi1, d2));
1307  }
1308  break;
1309  case DB_TYPE_NUMERIC:
1311  if (d2 == 0)
1312  {
1313  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1314  }
1315  else
1316  {
1317  dtmp = fmod ((double) bi1, d2);
1318  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value2), num, &p, &s);
1319  db_make_numeric (result, num, p, s);
1320  }
1321  break;
1322  case DB_TYPE_MONETARY:
1323  d2 = (db_get_monetary (value2))->amount;
1324  if (d2 == 0)
1325  {
1326  db_make_monetary (result, (db_get_monetary (value2))->type, (double) bi1);
1327  }
1328  else
1329  {
1330  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod ((double) bi1, d2));
1331  }
1332  break;
1333  default:
1335  {
1337  er_status = ER_QPROC_INVALID_DATATYPE;
1338  }
1339  goto exit;
1340  }
1341 
1342 exit:
1343  return er_status;
1344 }
1345 
1346 /*
1347  * db_mod_float () - take mod value of value1(float) with value2
1348  * return: NO_ERROR, ER_FAILED
1349  * result(out) : resultant db_value
1350  * value1(in) : float db_value
1351  * value2(in) : second db_value
1352  */
1353 static int
1354 db_mod_float (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1355 {
1356 #if !defined(NDEBUG)
1357  DB_TYPE type1;
1358 #endif
1359  DB_TYPE type2;
1360  short s2;
1361  int i2;
1362  float f1, f2;
1363  double d2;
1364  DB_BIGINT bi2;
1365  int er_status = NO_ERROR;
1366  DB_VALUE cast_value2;
1367 
1368  assert (result != NULL && value1 != NULL && value2 != NULL);
1369 
1370  db_make_null (&cast_value2);
1371 
1372 #if !defined(NDEBUG)
1373  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1374  assert (type1 == DB_TYPE_FLOAT);
1375 #endif
1376 
1377  f1 = db_get_float (value1);
1378 
1379  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1380  switch (type2)
1381  {
1382  case DB_TYPE_SHORT:
1383  s2 = db_get_short (value2);
1384  if (s2 == 0)
1385  {
1386  db_make_float (result, f1);
1387  }
1388  else
1389  {
1390  db_make_float (result, (float) fmod (f1, (float) s2));
1391  }
1392  break;
1393  case DB_TYPE_INTEGER:
1394  i2 = db_get_int (value2);
1395  if (i2 == 0)
1396  {
1397  db_make_float (result, f1);
1398  }
1399  else
1400  {
1401  db_make_float (result, (float) fmod (f1, (float) i2));
1402  }
1403  break;
1404  case DB_TYPE_BIGINT:
1405  bi2 = db_get_bigint (value2);
1406  if (bi2 == 0)
1407  {
1408  db_make_float (result, f1);
1409  }
1410  else
1411  {
1412  db_make_float (result, (float) fmod ((double) f1, (double) bi2));
1413  }
1414  break;
1415  case DB_TYPE_FLOAT:
1416  f2 = db_get_float (value2);
1417  if (f2 == 0)
1418  {
1419  db_make_float (result, f1);
1420  }
1421  else
1422  {
1423  db_make_float (result, (float) fmod (f1, f2));
1424  }
1425  break;
1426  case DB_TYPE_CHAR:
1427  case DB_TYPE_VARCHAR:
1428  case DB_TYPE_NCHAR:
1429  case DB_TYPE_VARNCHAR:
1430  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1431  if (er_status != NO_ERROR
1433  {
1434  goto exit;
1435  }
1436 
1437  assert (type2 == DB_TYPE_DOUBLE);
1438 
1439  value2 = &cast_value2;
1440 
1441  /* fall through */
1442 
1443  case DB_TYPE_DOUBLE:
1444  d2 = db_get_double (value2);
1445  if (d2 == 0)
1446  {
1447  db_make_double (result, f1);
1448  }
1449  else
1450  {
1451  db_make_double (result, (double) fmod ((double) f1, d2));
1452  }
1453  break;
1454  case DB_TYPE_NUMERIC:
1456  /* common type of float and numeric is double. */
1457  if (d2 == 0)
1458  {
1459  db_make_double (result, f1);
1460  }
1461  else
1462  {
1463  db_make_double (result, fmod ((double) f1, d2));
1464  }
1465  break;
1466  case DB_TYPE_MONETARY:
1467  d2 = (db_get_monetary (value2))->amount;
1468  if (d2 == 0)
1469  {
1470  db_make_monetary (result, (db_get_monetary (value2))->type, f1);
1471  }
1472  else
1473  {
1474  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod ((double) f1, d2));
1475  }
1476  break;
1477  default:
1479  {
1481  er_status = ER_QPROC_INVALID_DATATYPE;
1482  }
1483  goto exit;
1484  }
1485 
1486 exit:
1487  return er_status;
1488 }
1489 
1490 /*
1491  * db_mod_double () - take mod value of value1(double) with value2
1492  * return: NO_ERROR, ER_FAILED
1493  * result(out) : resultant db_value
1494  * value1(in) : double db_value
1495  * value2(in) : second db_value
1496  */
1497 static int
1498 db_mod_double (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1499 {
1500 #if !defined(NDEBUG)
1501  DB_TYPE type1;
1502 #endif
1503  DB_TYPE type2;
1504  short s2;
1505  int i2;
1506  float f2;
1507  double d1, d2;
1508  DB_BIGINT bi2;
1509  int er_status = NO_ERROR;
1510  DB_VALUE cast_value2;
1511 
1512  assert (result != NULL && value1 != NULL && value2 != NULL);
1513 
1514  db_make_null (&cast_value2);
1515 
1516 #if !defined(NDEBUG)
1517  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1518  assert (type1 == DB_TYPE_DOUBLE);
1519 #endif
1520 
1521  d1 = db_get_double (value1);
1522 
1523  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1524  switch (type2)
1525  {
1526  case DB_TYPE_SHORT:
1527  s2 = db_get_short (value2);
1528  if (s2 == 0)
1529  {
1530  db_make_double (result, d1);
1531  }
1532  else
1533  {
1534  db_make_double (result, (double) fmod (d1, (double) s2));
1535  }
1536  break;
1537  case DB_TYPE_INTEGER:
1538  i2 = db_get_int (value2);
1539  if (i2 == 0)
1540  {
1541  db_make_double (result, d1);
1542  }
1543  else
1544  {
1545  db_make_double (result, (double) fmod (d1, (double) i2));
1546  }
1547  break;
1548  case DB_TYPE_BIGINT:
1549  bi2 = db_get_bigint (value2);
1550  if (bi2 == 0)
1551  {
1552  db_make_double (result, d1);
1553  }
1554  else
1555  {
1556  db_make_double (result, (double) fmod (d1, (double) bi2));
1557  }
1558  break;
1559  case DB_TYPE_FLOAT:
1560  f2 = db_get_float (value2);
1561  if (f2 == 0)
1562  {
1563  db_make_double (result, d1);
1564  }
1565  else
1566  {
1567  db_make_double (result, (double) fmod (d1, (double) f2));
1568  }
1569  break;
1570  case DB_TYPE_CHAR:
1571  case DB_TYPE_VARCHAR:
1572  case DB_TYPE_NCHAR:
1573  case DB_TYPE_VARNCHAR:
1574  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1575  if (er_status != NO_ERROR
1577  {
1578  goto exit;
1579  }
1580 
1581  assert (type2 == DB_TYPE_DOUBLE);
1582 
1583  value2 = &cast_value2;
1584 
1585  /* fall through */
1586 
1587  case DB_TYPE_DOUBLE:
1588  d2 = db_get_double (value2);
1589  if (d2 == 0)
1590  {
1591  db_make_double (result, d1);
1592  }
1593  else
1594  {
1595  db_make_double (result, (double) fmod (d1, d2));
1596  }
1597  break;
1598  case DB_TYPE_NUMERIC:
1600  if (d2 == 0)
1601  {
1602  db_make_double (result, d1);
1603  }
1604  else
1605  {
1606  db_make_double (result, (double) fmod (d1, d2));
1607  }
1608  break;
1609  case DB_TYPE_MONETARY:
1610  d2 = (db_get_monetary (value2))->amount;
1611  if (d2 == 0)
1612  {
1613  db_make_monetary (result, (db_get_monetary (value2))->type, d1);
1614  }
1615  else
1616  {
1617  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod (d1, d2));
1618  }
1619  break;
1620  default:
1622  {
1624  er_status = ER_QPROC_INVALID_DATATYPE;
1625  }
1626  goto exit;
1627  }
1628 
1629 exit:
1630  return er_status;
1631 }
1632 
1633 /*
1634  * db_mod_string () - take mod value of value1(string) with value2
1635  * return: NO_ERROR, ER_FAILED
1636  * result(out) : resultant db_value
1637  * value1(in) : string db_value
1638  * value2(in) : second db_value
1639  */
1640 static int
1641 db_mod_string (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1642 {
1643  DB_TYPE type1;
1644  int er_status = NO_ERROR;
1645  DB_VALUE cast_value1;
1646 
1647  assert (result != NULL && value1 != NULL && value2 != NULL);
1648 
1649  db_make_null (&cast_value1);
1650 
1651 #if !defined(NDEBUG)
1652  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1653  assert (TP_IS_CHAR_TYPE (type1));
1654 #endif
1655 
1656  er_status = tp_value_str_auto_cast_to_number (value1, &cast_value1, &type1);
1657  if (er_status != NO_ERROR
1659  {
1660  return er_status;
1661  }
1662 
1663  assert (type1 == DB_TYPE_DOUBLE);
1664 
1665  value1 = &cast_value1;
1666 
1667  return db_mod_double (result, value1, value2);
1668 }
1669 
1670 /*
1671  * db_mod_numeric () - take mod value of value1(numeric) with value2
1672  * return: NO_ERROR, ER_FAILED
1673  * result(out) : resultant db_value
1674  * value1(in) : numeric db_value
1675  * value2(in) : second db_value
1676  */
1677 static int
1678 db_mod_numeric (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1679 {
1680 #if !defined(NDEBUG)
1681  DB_TYPE type1;
1682 #endif
1683  DB_TYPE type2;
1684  short s2;
1685  int i2;
1686  float f2;
1687  double d1, d2;
1688  DB_BIGINT bi2;
1689  double dtmp;
1690  DB_DATA_STATUS data_stat;
1691  unsigned char num[DB_NUMERIC_BUF_SIZE];
1692  int p, s;
1693  int er_status = NO_ERROR;
1694  DB_VALUE cast_value2;
1695 
1696  assert (result != NULL && value1 != NULL && value2 != NULL);
1697 
1698  db_make_null (&cast_value2);
1699 
1700 #if !defined(NDEBUG)
1701  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1702  assert (type1 == DB_TYPE_NUMERIC);
1703 #endif
1704 
1706 
1707  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1708  switch (type2)
1709  {
1710  case DB_TYPE_SHORT:
1711  s2 = db_get_short (value2);
1712  if (s2 == 0)
1713  {
1714  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1715  }
1716  else
1717  {
1718  dtmp = fmod (d1, (double) s2);
1719  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value1), num, &p, &s);
1720  db_make_numeric (result, num, p, s);
1721  }
1722  break;
1723  case DB_TYPE_INTEGER:
1724  i2 = db_get_int (value2);
1725  if (i2 == 0)
1726  {
1727  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1728  }
1729  else
1730  {
1731  dtmp = fmod (d1, (double) i2);
1732  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value1), num, &p, &s);
1733  db_make_numeric (result, num, p, s);
1734  }
1735  break;
1736  case DB_TYPE_BIGINT:
1737  bi2 = db_get_bigint (value2);
1738  if (bi2 == 0)
1739  {
1740  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1741  }
1742  else
1743  {
1744  dtmp = fmod (d1, (double) bi2);
1745  (void) numeric_internal_double_to_num (dtmp, DB_VALUE_SCALE (value1), num, &p, &s);
1746  db_make_numeric (result, num, p, s);
1747  }
1748  break;
1749  case DB_TYPE_FLOAT:
1750  f2 = db_get_float (value2);
1751  /* common type of float and numeric is double */
1752  if (f2 == 0)
1753  {
1754  db_make_double (result, d1);
1755  }
1756  else
1757  {
1758  db_make_double (result, fmod (d1, (double) f2));
1759  }
1760  break;
1761  case DB_TYPE_CHAR:
1762  case DB_TYPE_VARCHAR:
1763  case DB_TYPE_NCHAR:
1764  case DB_TYPE_VARNCHAR:
1765  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1766  if (er_status != NO_ERROR
1768  {
1769  goto exit;
1770  }
1771 
1772  assert (type2 == DB_TYPE_DOUBLE);
1773 
1774  value2 = &cast_value2;
1775 
1776  /* fall through */
1777 
1778  case DB_TYPE_DOUBLE:
1779  d2 = db_get_double (value2);
1780  if (d2 == 0)
1781  {
1782  db_make_double (result, d1);
1783  }
1784  else
1785  {
1786  db_make_double (result, (double) fmod (d1, d2));
1787  }
1788  break;
1789  case DB_TYPE_NUMERIC:
1791  if (d2 == 0)
1792  {
1793  (void) numeric_db_value_coerce_to_num (value1, result, &data_stat);
1794  }
1795  else
1796  {
1797  dtmp = fmod (d1, d2);
1798  (void) numeric_internal_double_to_num (dtmp, MAX (DB_VALUE_SCALE (value1), DB_VALUE_SCALE (value2)), num, &p,
1799  &s);
1800  db_make_numeric (result, num, p, s);
1801  }
1802  break;
1803  case DB_TYPE_MONETARY:
1804  d2 = (db_get_monetary (value2))->amount;
1805  if (d2 == 0)
1806  {
1807  db_make_monetary (result, (db_get_monetary (value2))->type, d1);
1808  }
1809  else
1810  {
1811  db_make_monetary (result, (db_get_monetary (value2))->type, (double) fmod (d1, d2));
1812  }
1813  break;
1814  default:
1816  {
1818  er_status = ER_QPROC_INVALID_DATATYPE;
1819  }
1820  goto exit;
1821  }
1822 
1823 exit:
1824  return er_status;
1825 }
1826 
1827 /*
1828  * db_mod_monetary () - take mod value of value1(monetary) with value2
1829  * return: NO_ERROR, ER_FAILED
1830  * result(out) : resultant db_value
1831  * value1(in) : monetary db_value
1832  * value2(in) : second db_value
1833  */
1834 static int
1835 db_mod_monetary (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1836 {
1837 #if !defined(NDEBUG)
1838  DB_TYPE type1;
1839 #endif
1840  DB_TYPE type2;
1841  double d1, d2;
1842  int er_status = NO_ERROR;
1843  DB_VALUE cast_value2;
1844 
1845  assert (result != NULL && value1 != NULL && value2 != NULL);
1846 
1847  db_make_null (&cast_value2);
1848 
1849 #if !defined(NDEBUG)
1850  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1851  assert (type1 == DB_TYPE_MONETARY);
1852 #endif
1853 
1854  d1 = (db_get_monetary (value1))->amount;
1855  d2 = 0;
1856 
1857  type2 = DB_VALUE_DOMAIN_TYPE (value2);
1858  switch (type2)
1859  {
1860  case DB_TYPE_SHORT:
1861  d2 = db_get_short (value2);
1862  break;
1863  case DB_TYPE_INTEGER:
1864  d2 = db_get_int (value2);
1865  break;
1866  case DB_TYPE_BIGINT:
1867  d2 = (double) db_get_bigint (value2);
1868  break;
1869  case DB_TYPE_FLOAT:
1870  d2 = db_get_float (value2);
1871  break;
1872  case DB_TYPE_CHAR:
1873  case DB_TYPE_VARCHAR:
1874  case DB_TYPE_NCHAR:
1875  case DB_TYPE_VARNCHAR:
1876  er_status = tp_value_str_auto_cast_to_number (value2, &cast_value2, &type2);
1877  if (er_status != NO_ERROR
1879  {
1880  goto exit;
1881  }
1882 
1883  assert (type2 == DB_TYPE_DOUBLE);
1884 
1885  value2 = &cast_value2;
1886 
1887  /* fall through */
1888 
1889  case DB_TYPE_DOUBLE:
1890  d2 = db_get_double (value2);
1891  break;
1892  case DB_TYPE_NUMERIC:
1894  break;
1895  case DB_TYPE_MONETARY:
1896  d2 = (db_get_monetary (value2))->amount;
1897  break;
1898  default:
1900  {
1902  er_status = ER_QPROC_INVALID_DATATYPE;
1903  }
1904  goto exit;
1905  }
1906 
1907  if (d2 == 0)
1908  {
1909  db_make_monetary (result, (db_get_monetary (value1))->type, d1);
1910  }
1911  else
1912  {
1913  db_make_monetary (result, (db_get_monetary (value1))->type, (double) fmod (d1, d2));
1914  }
1915 
1916 exit:
1917  return er_status;
1918 }
1919 
1920 /*
1921  * db_mod_dbval () - take mod value of db_value
1922  * return: NO_ERROR, ER_FAILED
1923  * result(out) : resultant db_value
1924  * value1(in) : first db_value
1925  * value2(in) : second db_value
1926  */
1927 int
1928 db_mod_dbval (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
1929 {
1930  DB_TYPE type1;
1931 
1932  if (DB_IS_NULL (value1) || DB_IS_NULL (value2))
1933  {
1934  return NO_ERROR;
1935  }
1936 
1937  type1 = DB_VALUE_DOMAIN_TYPE (value1);
1938  switch (type1)
1939  {
1940  case DB_TYPE_SHORT:
1941  return db_mod_short (result, value1, value2);
1942 
1943  case DB_TYPE_INTEGER:
1944  return db_mod_int (result, value1, value2);
1945 
1946  case DB_TYPE_BIGINT:
1947  return db_mod_bigint (result, value1, value2);
1948 
1949  case DB_TYPE_FLOAT:
1950  return db_mod_float (result, value1, value2);
1951 
1952  case DB_TYPE_CHAR:
1953  case DB_TYPE_VARCHAR:
1954  case DB_TYPE_NCHAR:
1955  case DB_TYPE_VARNCHAR:
1956  return db_mod_string (result, value1, value2);
1957 
1958  case DB_TYPE_DOUBLE:
1959  return db_mod_double (result, value1, value2);
1960 
1961  case DB_TYPE_NUMERIC:
1962  return db_mod_numeric (result, value1, value2);
1963 
1964  case DB_TYPE_MONETARY:
1965  return db_mod_monetary (result, value1, value2);
1966 
1967  default:
1969  {
1972  }
1973  else
1974  {
1975  return NO_ERROR;
1976  }
1977  }
1978 }
1979 
1980 /*
1981  * round_double ()
1982  * return: num rounded to integer places to the right of the decimal point
1983  * num(in) :
1984  * integer(in):
1985  */
1986 static double
1987 round_double (double num, double integer)
1988 {
1989  /*
1990  * Under high optimization level, some optimizers (e.g, gcc -O3 on linux)
1991  * generates a wrong result without "volatile".
1992  */
1993  volatile double scale_up, result;
1994 
1995  if (num == 0)
1996  {
1997  return num;
1998  }
1999 
2000  scale_up = pow (10, integer);
2001 
2002  if (!FINITE (num * scale_up))
2003  {
2005  }
2006 
2007  result = round (num * scale_up) / scale_up;
2008 
2009  return result;
2010 }
2011 
2012 /*
2013  * move_n_days () - move forward or backward n days from a given date,
2014  *
2015  * return: error code
2016  * yearp(in/out):
2017  * monthp(in/out) :
2018  * dayp(in/out) :
2019  * interval(in) : how many days to move, negative number means back
2020  */
2021 static int
2022 move_n_days (int *monthp, int *dayp, int *yearp, const int interval)
2023 {
2024  /* no need to judge if arguments are illegal as it has been done previously */
2025  DB_DATE date;
2026  int error;
2027 
2028  error = db_date_encode (&date, *monthp, *dayp, *yearp);
2029  if (error != NO_ERROR)
2030  {
2031  return error;
2032  }
2033  date += interval;
2034  db_date_decode (&date, monthp, dayp, yearp);
2035 
2036  return NO_ERROR;
2037 }
2038 
2039 /*
2040  * db_round_date () - returns a date round by value2('year' | 'month' | 'day')
2041  *
2042  * return: NO_ERROR, ER_FAILED
2043  * result(out): resultant db_value
2044  * value1(in) : first db_value
2045  * value2(in) : second db_value
2046  */
2047 
2048 static int
2049 round_date (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
2050 {
2051  DB_DATETIME *pdatetime, local_dt;
2052  DB_DATETIMETZ *pdatetimetz;
2053  DB_TIMESTAMP *pstamp;
2054  DB_TIMESTAMPTZ *pstamptz;
2055  DB_DATE *pdate;
2056  DB_DATE date;
2057  DB_TIME time;
2058  DB_TYPE type;
2059  int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, millisecond = 0;
2060  int weekday = 0;
2061  int error = NO_ERROR;
2062  TIMESTAMP_FORMAT format;
2063 
2064  /* get format */
2065  error = db_get_date_format (value2, &format);
2066  if (error != NO_ERROR)
2067  {
2068  goto end;
2069  }
2070 
2071  /* get all values in the date */
2072  type = DB_VALUE_DOMAIN_TYPE (value1);
2073 
2074  if (type == DB_TYPE_TIMESTAMP || type == DB_TYPE_TIMESTAMPLTZ)
2075  {
2076  pstamp = db_get_timestamp (value1);
2077  (void) db_timestamp_decode_ses (pstamp, &date, &time);
2078  db_date_decode (&date, &month, &day, &year);
2079  db_time_decode (&time, &hour, &minute, &second);
2080  }
2081  else if (type == DB_TYPE_TIMESTAMPTZ)
2082  {
2083  pstamptz = db_get_timestamptz (value1);
2084  error = db_timestamp_decode_w_tz_id (&pstamptz->timestamp, &pstamptz->tz_id, &date, &time);
2085  if (error != NO_ERROR)
2086  {
2087  goto end;
2088  }
2089  db_date_decode (&date, &month, &day, &year);
2090  db_time_decode (&time, &hour, &minute, &second);
2091  }
2092  else if (type == DB_TYPE_DATETIME)
2093  {
2094  pdatetime = db_get_datetime (value1);
2095  db_datetime_decode (pdatetime, &month, &day, &year, &hour, &minute, &second, &millisecond);
2096  }
2097  else if (type == DB_TYPE_DATETIMELTZ)
2098  {
2099  pdatetime = db_get_datetime (value1);
2100 
2101  error = tz_datetimeltz_to_local (pdatetime, &local_dt);
2102  if (error != NO_ERROR)
2103  {
2104  goto end;
2105  }
2106 
2107  db_datetime_decode (&local_dt, &month, &day, &year, &hour, &minute, &second, &millisecond);
2108  }
2109  else if (type == DB_TYPE_DATETIMETZ)
2110  {
2111  pdatetimetz = db_get_datetimetz (value1);
2112 
2113  error = tz_utc_datetimetz_to_local (&pdatetimetz->datetime, &pdatetimetz->tz_id, &local_dt);
2114 
2115  if (error != NO_ERROR)
2116  {
2117  goto end;
2118  }
2119 
2120  db_datetime_decode (&local_dt, &month, &day, &year, &hour, &minute, &second, &millisecond);
2121  }
2122  else if (type == DB_TYPE_DATE)
2123  {
2124  pdate = db_get_date (value1);
2125  db_date_decode (pdate, &month, &day, &year);
2126  hour = minute = second = millisecond = 0;
2127  }
2128  else
2129  {
2130  error = ER_QPROC_INVALID_DATATYPE;
2131  goto end;
2132  }
2133 
2134  /* apply round according to format */
2135  switch (format)
2136  {
2137  case DT_YYYY:
2138  case DT_YY:
2139  if (month >= 7) /* rounds up on July 1 */
2140  {
2141  year++;
2142  }
2143  month = 1;
2144  day = 1;
2145  break;
2146  case DT_MONTH:
2147  case DT_MON:
2148  case DT_MM:
2149  if (day >= 16) /* rounds up on the 16th days */
2150  {
2151  if (month == 12)
2152  {
2153  year++;
2154  month = 1;
2155  }
2156  else
2157  {
2158  month++;
2159  }
2160  }
2161  day = 1;
2162  break;
2163  case DT_DD:
2164  if (hour >= 12) /* rounds up on 12:00 AM */
2165  {
2166  error = move_n_days (&month, &day, &year, 1);
2167  if (error != NO_ERROR)
2168  {
2169  goto end;
2170  }
2171  }
2172  break;
2173  case DT_HH24:
2174  case DT_HH12:
2175  case DT_HH:
2176  case DT_H:
2177  if (minute >= 30) /* rounds up on HH:30 */
2178  {
2179  if (++hour == 24) /* rounds up to next day */
2180  {
2181  error = move_n_days (&month, &day, &year, 1);
2182  if (error != NO_ERROR)
2183  {
2184  goto end;
2185  }
2186  }
2187  }
2188  break;
2189  case DT_MI:
2190  if (second >= 30) /* rounds up on HH:MM:30 */
2191  {
2192  if (++minute == 60)
2193  {
2194  if (++hour == 24)
2195  {
2196  error = move_n_days (&month, &day, &year, 1);
2197  if (error != NO_ERROR)
2198  {
2199  goto end;
2200  }
2201  }
2202  }
2203  }
2204  break;
2205  case DT_SS:
2206  if (millisecond >= 500) /* rounds up on HH:MM:SS.500 */
2207  {
2208  if (++second == 60)
2209  {
2210  if (++minute == 60)
2211  {
2212  if (++hour == 24)
2213  {
2214  error = move_n_days (&month, &day, &year, 1);
2215  if (error != NO_ERROR)
2216  {
2217  goto end;
2218  }
2219  }
2220  }
2221  }
2222  }
2223  break;
2224  case DT_MS: /* do nothing */
2225  break;
2226  case DT_Q: /* quarter */
2227  /* rounds up on the 16th day of the second month of the quarter */
2228  if (month < 2 || (month == 2 && day < 16))
2229  {
2230  month = 1;
2231  }
2232  else if (month < 5 || (month == 5 && day < 16))
2233  {
2234  month = 4;
2235  }
2236  else if (month < 8 || (month == 8 && day < 16))
2237  {
2238  month = 7;
2239  }
2240  else if (month < 11 || (month == 11 && day < 16))
2241  {
2242  month = 10;
2243  }
2244  else
2245  {
2246  month = 1;
2247  year++;
2248  }
2249  day = 1;
2250  break;
2251  case DT_DAY:
2252  case DT_DY: /* rounds up on thursday of a week */
2253  case DT_D:
2254  if (hour >= 12) /* first round 'dd' */
2255  {
2256  error = move_n_days (&month, &day, &year, 1);
2257  if (error != NO_ERROR)
2258  {
2259  goto end;
2260  }
2261  }
2262 
2263  weekday = day_of_week (julian_encode (month, day, year));
2264  if (weekday < 4)
2265  {
2266  error = move_n_days (&month, &day, &year, -weekday);
2267  }
2268  else
2269  {
2270  error = move_n_days (&month, &day, &year, 7 - weekday);
2271  }
2272  if (error != NO_ERROR)
2273  {
2274  goto end;
2275  }
2276  break;
2277  case DT_CC:
2278  default:
2279  error = ER_QSTR_INVALID_FORMAT;
2280  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0);
2281  goto end;
2282  }
2283 
2284  /* check for boundary and throw overflow */
2285  if (year < 0 || year > 9999)
2286  {
2287  error = ER_IT_DATA_OVERFLOW;
2289  goto end;
2290  }
2291 
2292  /* re-create new date */
2293  error = db_make_date (result, month, day, year);
2294 
2295 end:
2297  {
2298  error = NO_ERROR;
2299  db_make_null (result);
2300  er_clear ();
2301  }
2302 
2303  return error;
2304 }
2305 
2306 /*
2307  * db_round_dbval () - returns value1 rounded to value2 places right of
2308  * the decimal point
2309  * return: NO_ERROR, ER_FAILED
2310  * result(out): resultant db_value
2311  * value1(in) : first db_value
2312  * value2(in) : second db_value
2313  */
2314 int
2315 db_round_dbval (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
2316 {
2317  DB_TYPE type1, type2;
2318  short s1;
2319  int i1;
2320  float f1;
2321  double d1, d2 = 0.0;
2322  DB_BIGINT bi1, bi2, bi_tmp;
2323  double dtmp;
2324  unsigned char num[DB_NUMERIC_BUF_SIZE];
2325  char num_string[(2 * DB_MAX_NUMERIC_PRECISION) + 4];
2326  char *ptr, *end;
2327  int need_round = 0;
2328  int p, s;
2329  DB_VALUE cast_value, cast_format;
2330  int er_status = NO_ERROR;
2331  TP_DOMAIN *domain = NULL;
2332 
2333  db_make_null (&cast_value);
2334  db_make_null (&cast_format);
2335 
2336  if (DB_IS_NULL (value1) || DB_IS_NULL (value2))
2337  {
2338  return NO_ERROR;
2339  }
2340 
2341  type1 = DB_VALUE_DOMAIN_TYPE (value1);
2342  type2 = DB_VALUE_DOMAIN_TYPE (value2);
2343 
2344  /* first check if round a date: */
2345  if (type1 == DB_TYPE_DATETIME || type1 == DB_TYPE_DATETIMELTZ || type1 == DB_TYPE_DATETIMETZ
2346  || type1 == DB_TYPE_TIMESTAMP || type1 == DB_TYPE_TIMESTAMPLTZ || type1 == DB_TYPE_TIMESTAMPTZ
2347  || type1 == DB_TYPE_DATE)
2348  { /* round date */
2349  if (QSTR_IS_ANY_CHAR (type2) && strcasecmp (DB_GET_STRING_SAFE (value2), "default") == 0)
2350  {
2351  db_make_string (&cast_format, "dd");
2352  value2 = &cast_format;
2353  }
2354  return round_date (result, value1, value2);
2355  }
2356 
2357  /* cast value1 to double */
2358  if (!TP_IS_NUMERIC_TYPE (type1))
2359  {
2360  type1 = DB_TYPE_UNKNOWN;
2361  /* try type double */
2363  if (tp_value_coerce (value1, &cast_value, domain) != DOMAIN_COMPATIBLE)
2364  {
2366  {
2367  er_clear ();
2368  return NO_ERROR;
2369  }
2370  else
2371  {
2374  }
2375  }
2376  type1 = DB_TYPE_DOUBLE;
2377  value1 = &cast_value;
2378  }
2379 
2380  /* get value2 */
2381  if (type2 == DB_TYPE_INTEGER)
2382  {
2383  d2 = (double) db_get_int (value2);
2384  }
2385  else if (type2 == DB_TYPE_BIGINT)
2386  {
2387  d2 = (double) db_get_bigint (value2);
2388  }
2389  else if (type2 == DB_TYPE_SHORT)
2390  {
2391  d2 = (double) db_get_short (value2);
2392  }
2393  else if (type2 == DB_TYPE_DOUBLE)
2394  {
2395  d2 = db_get_double (value2);
2396  }
2397  else /* cast to INTEGER */
2398  {
2399  if (QSTR_IS_ANY_CHAR (type2) && strcasecmp (DB_GET_STRING_SAFE (value2), "default") == 0)
2400  {
2401  db_make_int (&cast_format, 0);
2402  value2 = &cast_format;
2403  type2 = DB_TYPE_INTEGER;
2404  d2 = 0;
2405  }
2406  else
2407  {
2408  /* try type int */
2409  type2 = DB_TYPE_UNKNOWN;
2411  if (tp_value_coerce (value2, &cast_format, domain) != DOMAIN_COMPATIBLE)
2412  {
2414  {
2415  er_clear ();
2416  return NO_ERROR;
2417  }
2418  else
2419  {
2422  }
2423  }
2424  type2 = DB_TYPE_INTEGER;
2425  value2 = &cast_format;
2426  d2 = db_get_int (value2);
2427  }
2428  }
2429 
2430  /* round double */
2431  switch (type1)
2432  {
2433  case DB_TYPE_SHORT:
2434  s1 = db_get_short (value1);
2435  dtmp = round_double (s1, d2);
2436  db_make_short (result, (short) dtmp);
2437  break;
2438  case DB_TYPE_INTEGER:
2439  i1 = db_get_int (value1);
2440  dtmp = round_double (i1, d2);
2441  db_make_int (result, (int) dtmp);
2442  break;
2443  case DB_TYPE_BIGINT:
2444  bi1 = db_get_bigint (value1);
2445  dtmp = round_double ((double) bi1, d2);
2446  bi_tmp = (DB_BIGINT) dtmp;
2447 #if defined(AIX)
2448  /* in AIX, double to long will not overflow, make it the same as linux. */
2449  if (dtmp == (double) DB_BIGINT_MAX)
2450  {
2451  bi_tmp = DB_BIGINT_MIN;
2452  }
2453 #endif
2454  db_make_bigint (result, bi_tmp);
2455  break;
2456  case DB_TYPE_FLOAT:
2457  f1 = db_get_float (value1);
2458  dtmp = round_double (f1, d2);
2459  db_make_float (result, (float) dtmp);
2460  break;
2461  case DB_TYPE_CHAR:
2462  case DB_TYPE_VARCHAR:
2463  case DB_TYPE_NCHAR:
2464  case DB_TYPE_VARNCHAR:
2465  db_make_null (&cast_value);
2466  er_status = tp_value_str_auto_cast_to_number (value1, &cast_value, &type1);
2467  if (er_status != NO_ERROR
2469  {
2470  return er_status;
2471  }
2472 
2473  assert (type1 == DB_TYPE_DOUBLE);
2474  value1 = &cast_value;
2475 
2476  /* fall through */
2477  case DB_TYPE_DOUBLE:
2478  d1 = db_get_double (value1);
2479  dtmp = round_double (d1, d2);
2480  db_make_double (result, (double) dtmp);
2481  break;
2482  case DB_TYPE_NUMERIC:
2483  memset (num_string, 0, sizeof (num_string));
2484  numeric_coerce_num_to_dec_str (db_locate_numeric (value1), num_string);
2485  p = DB_VALUE_PRECISION (value1);
2486  s = DB_VALUE_SCALE (value1);
2487  end = num_string + strlen (num_string);
2488 
2489  if (type2 == DB_TYPE_BIGINT)
2490  {
2491  bi2 = db_get_bigint (value2);
2492  }
2493  else if (type2 == DB_TYPE_INTEGER)
2494  {
2495  bi2 = db_get_int (value2);
2496  }
2497  else if (type2 == DB_TYPE_SHORT)
2498  {
2499  bi2 = db_get_short (value2);
2500  }
2501  else /* double */
2502  {
2503  bi2 = (DB_BIGINT) db_get_double (value2);
2504  }
2505  ptr = end - s + bi2;
2506 
2507  if (end < ptr)
2508  { /* no need to round, return as it is */
2509  *result = *value1;
2510  break;
2511  }
2512  else if (ptr < num_string)
2513  { /* return zero */
2514  memset (num_string, 0, sizeof (num_string));
2515  }
2516  else
2517  {
2518  if (*ptr >= '5')
2519  {
2520  need_round = 1;
2521  }
2522  while (ptr < end)
2523  {
2524  *ptr++ = '0';
2525  }
2526  if (need_round)
2527  {
2528  /* round up */
2529  int done = 0;
2530 
2531  for (ptr = end - s + bi2 - 1; ptr >= num_string && !done; ptr--)
2532  {
2533  if (*ptr == '9')
2534  {
2535  *ptr = '0';
2536  }
2537  else
2538  {
2539  *ptr += 1;
2540  done = 1;
2541  }
2542  }
2543 
2544  for (ptr = num_string; ptr < end; ptr++)
2545  {
2546  if ('1' <= *ptr && *ptr <= '9')
2547  {
2548  if (strlen (ptr) > DB_MAX_NUMERIC_PRECISION)
2549  {
2550  /* overflow happened during round up */
2552  {
2553  er_clear ();
2554  return NO_ERROR;
2555  }
2556  else
2557  {
2560  pr_type_name (TP_DOMAIN_TYPE (domain)));
2561  return ER_IT_DATA_OVERFLOW;
2562  }
2563  }
2564  break;
2565  }
2566  }
2567  }
2568  }
2569 
2570  numeric_coerce_dec_str_to_num (num_string, num);
2571  db_make_numeric (result, num, p, s);
2572  break;
2573  case DB_TYPE_MONETARY:
2574  d1 = (db_get_monetary (value1))->amount;
2575  dtmp = round_double (d1, d2);
2576  db_make_monetary (result, (db_get_monetary (value1))->type, dtmp);
2577  break;
2578  default:
2580  {
2583  }
2584  }
2585 
2587  {
2588  er_clear ();
2589  db_make_null (result);
2590  }
2591 
2592  return er_errid ();
2593 }
2594 
2595 /*
2596  * db_log_dbval () -
2597  * return: NO_ERROR, ER_FAILED
2598  * result(out): resultant db_value
2599  * value1(in) : first db_value
2600  * value2(in) : second db_value
2601  */
2602 int
2603 db_log_dbval (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
2604 {
2605  DB_TYPE type1, type2;
2606  short s1, s2;
2607  int i1, i2;
2608  float f1, f2;
2609  double d1, d2;
2610  DB_BIGINT bi1, bi2;
2611  double dtmp = 0.0;
2612 
2613  if (DB_IS_NULL (value1) || DB_IS_NULL (value2))
2614  {
2615  return NO_ERROR;
2616  }
2617 
2618  type1 = DB_VALUE_DOMAIN_TYPE (value1);
2619  type2 = DB_VALUE_DOMAIN_TYPE (value2);
2620 
2621  switch (type1)
2622  {
2623  case DB_TYPE_SHORT:
2624  s1 = db_get_short (value1);
2625  if (s1 <= 1)
2626  {
2627  goto log_error;
2628  }
2629 
2630  switch (type2)
2631  {
2632  case DB_TYPE_SHORT:
2633  s2 = db_get_short (value2);
2634  if (s2 <= 0)
2635  {
2636  goto log_error;
2637  }
2638  dtmp = log10 ((double) s2) / log10 ((double) s1);
2639  break;
2640  case DB_TYPE_INTEGER:
2641  i2 = db_get_int (value2);
2642  if (i2 <= 0)
2643  {
2644  goto log_error;
2645  }
2646  dtmp = log10 ((double) i2) / log10 ((double) s1);
2647  break;
2648  case DB_TYPE_BIGINT:
2649  bi2 = db_get_bigint (value2);
2650  if (bi2 <= 0)
2651  {
2652  goto log_error;
2653  }
2654  dtmp = log10 ((double) bi2) / log10 ((double) s1);
2655  break;
2656  case DB_TYPE_FLOAT:
2657  f2 = db_get_float (value2);
2658  if (f2 <= 0)
2659  {
2660  goto log_error;
2661  }
2662  dtmp = log10 ((double) f2) / log10 ((double) s1);
2663  break;
2664  case DB_TYPE_DOUBLE:
2665  d2 = db_get_double (value2);
2666  if (d2 <= 0)
2667  {
2668  goto log_error;
2669  }
2670  dtmp = log10 ((double) d2) / log10 ((double) s1);
2671  break;
2672  case DB_TYPE_NUMERIC:
2674  if (d2 <= 0)
2675  {
2676  goto log_error;
2677  }
2678  dtmp = log10 ((double) d2) / log10 ((double) s1);
2679  break;
2680  case DB_TYPE_MONETARY:
2681  d2 = (db_get_monetary (value2))->amount;
2682  if (d2 <= 0)
2683  {
2684  goto log_error;
2685  }
2686  dtmp = log10 (d2) / log10 ((double) s1);
2687  break;
2688  default:
2690  break;
2691  }
2692  break;
2693 
2694  case DB_TYPE_BIGINT:
2695  bi1 = db_get_bigint (value1);
2696  if (bi1 <= 1)
2697  {
2698  goto log_error;
2699  }
2700 
2701  switch (type2)
2702  {
2703  case DB_TYPE_SHORT:
2704  s2 = db_get_short (value2);
2705  if (s2 <= 0)
2706  {
2707  goto log_error;
2708  }
2709  dtmp = log10 ((double) s2) / log10 ((double) bi1);
2710  break;
2711  case DB_TYPE_INTEGER:
2712  i2 = db_get_int (value2);
2713  if (i2 <= 0)
2714  {
2715  goto log_error;
2716  }
2717  dtmp = log10 ((double) i2) / log10 ((double) bi1);
2718  break;
2719  case DB_TYPE_BIGINT:
2720  bi2 = db_get_bigint (value2);
2721  if (bi2 <= 0)
2722  {
2723  goto log_error;
2724  }
2725  dtmp = log10 ((double) bi2) / log10 ((double) bi1);
2726  break;
2727  case DB_TYPE_FLOAT:
2728  f2 = db_get_float (value2);
2729  if (f2 <= 0)
2730  {
2731  goto log_error;
2732  }
2733  dtmp = log10 ((double) f2) / log10 ((double) bi1);
2734  break;
2735  case DB_TYPE_DOUBLE:
2736  d2 = db_get_double (value2);
2737  if (d2 <= 0)
2738  {
2739  goto log_error;
2740  }
2741  dtmp = log10 ((double) d2) / log10 ((double) bi1);
2742  break;
2743  case DB_TYPE_NUMERIC:
2745  if (d2 <= 0)
2746  {
2747  goto log_error;
2748  }
2749  dtmp = log10 ((double) d2) / log10 ((double) bi1);
2750  break;
2751  case DB_TYPE_MONETARY:
2752  d2 = (db_get_monetary (value2))->amount;
2753  if (d2 <= 0)
2754  {
2755  goto log_error;
2756  }
2757  dtmp = log10 (d2) / log10 ((double) bi1);
2758  break;
2759  default:
2761  break;
2762  }
2763  break;
2764 
2765  case DB_TYPE_INTEGER:
2766  i1 = db_get_int (value1);
2767  if (i1 <= 1)
2768  {
2769  goto log_error;
2770  }
2771 
2772  switch (type2)
2773  {
2774  case DB_TYPE_SHORT:
2775  s2 = db_get_short (value2);
2776  if (s2 <= 0)
2777  {
2778  goto log_error;
2779  }
2780  dtmp = log10 ((double) s2) / log10 ((double) i1);
2781  break;
2782  case DB_TYPE_INTEGER:
2783  i2 = db_get_int (value2);
2784  if (i2 <= 0)
2785  {
2786  goto log_error;
2787  }
2788  dtmp = log10 ((double) i2) / log10 ((double) i1);
2789  break;
2790  case DB_TYPE_BIGINT:
2791  bi2 = db_get_bigint (value2);
2792  if (bi2 <= 0)
2793  {
2794  goto log_error;
2795  }
2796  dtmp = log10 ((double) bi2) / log10 ((double) i1);
2797  break;
2798  case DB_TYPE_FLOAT:
2799  f2 = db_get_float (value2);
2800  if (f2 <= 0)
2801  {
2802  goto log_error;
2803  }
2804  dtmp = log10 ((double) f2) / log10 ((double) i1);
2805  break;
2806  case DB_TYPE_DOUBLE:
2807  d2 = db_get_double (value2);
2808  if (d2 <= 0)
2809  {
2810  goto log_error;
2811  }
2812  dtmp = log10 (d2) / log10 ((double) i1);
2813  break;
2814  case DB_TYPE_NUMERIC:
2816  if (d2 <= 0)
2817  {
2818  goto log_error;
2819  }
2820  dtmp = log10 (d2) / log10 ((double) i1);
2821  break;
2822  case DB_TYPE_MONETARY:
2823  d2 = (db_get_monetary (value2))->amount;
2824  if (d2 <= 0)
2825  {
2826  goto log_error;
2827  }
2828  dtmp = log10 (d2) / log10 ((double) i1);
2829  break;
2830  default:
2832  break;
2833  }
2834  break;
2835 
2836  case DB_TYPE_FLOAT:
2837  f1 = db_get_float (value1);
2838  if (f1 <= 1)
2839  {
2840  goto log_error;
2841  }
2842 
2843  switch (type2)
2844  {
2845  case DB_TYPE_SHORT:
2846  s2 = db_get_short (value2);
2847  if (s2 <= 0)
2848  {
2849  goto log_error;
2850  }
2851  dtmp = log10 ((double) s2) / log10 ((double) f1);
2852  break;
2853  case DB_TYPE_INTEGER:
2854  i2 = db_get_int (value2);
2855  if (i2 <= 0)
2856  {
2857  goto log_error;
2858  }
2859  dtmp = log10 ((double) i2) / log10 ((double) f1);
2860  break;
2861  case DB_TYPE_BIGINT:
2862  bi2 = db_get_bigint (value2);
2863  if (bi2 <= 0)
2864  {
2865  goto log_error;
2866  }
2867  dtmp = log10 ((double) bi2) / log10 ((double) f1);
2868  break;
2869  case DB_TYPE_FLOAT:
2870  f2 = db_get_float (value2);
2871  if (f2 <= 0)
2872  {
2873  goto log_error;
2874  }
2875  dtmp = log10 ((double) f2) / log10 ((double) f1);
2876  break;
2877  case DB_TYPE_DOUBLE:
2878  d2 = db_get_double (value2);
2879  if (d2 <= 0)
2880  {
2881  goto log_error;
2882  }
2883  dtmp = log10 (d2) / log10 ((double) f1);
2884  break;
2885  case DB_TYPE_NUMERIC:
2887  if (d2 <= 0)
2888  {
2889  goto log_error;
2890  }
2891  dtmp = log10 (d2) / log10 (f1);
2892  break;
2893  case DB_TYPE_MONETARY:
2894  d2 = (db_get_monetary (value2))->amount;
2895  if (d2 <= 0)
2896  {
2897  goto log_error;
2898  }
2899  dtmp = log10 (d2) / log10 (f1);
2900  break;
2901  default:
2903  break;
2904  }
2905  break;
2906 
2907  case DB_TYPE_DOUBLE:
2908  d1 = db_get_double (value1);
2909  if (d1 <= 1)
2910  {
2911  goto log_error;
2912  }
2913 
2914  switch (type2)
2915  {
2916  case DB_TYPE_SHORT:
2917  s2 = db_get_short (value2);
2918  if (s2 <= 0)
2919  {
2920  goto log_error;
2921  }
2922  dtmp = log10 ((double) s2) / log10 (d1);
2923  break;
2924  case DB_TYPE_INTEGER:
2925  i2 = db_get_int (value2);
2926  if (i2 <= 0)
2927  {
2928  goto log_error;
2929  }
2930  dtmp = log10 ((double) i2) / log10 (d1);
2931  break;
2932  case DB_TYPE_BIGINT:
2933  bi2 = db_get_bigint (value2);
2934  if (bi2 <= 0)
2935  {
2936  goto log_error;
2937  }
2938  dtmp = log10 ((double) bi2) / log10 (d1);
2939  break;
2940  case DB_TYPE_FLOAT:
2941  f2 = db_get_float (value2);
2942  if (f2 <= 0)
2943  {
2944  goto log_error;
2945  }
2946  dtmp = log10 ((double) f2) / log10 (d1);
2947  break;
2948  case DB_TYPE_DOUBLE:
2949  d2 = db_get_double (value2);
2950  if (d2 <= 0)
2951  {
2952  goto log_error;
2953  }
2954  dtmp = log10 (d2) / log10 (d1);
2955  break;
2956  case DB_TYPE_NUMERIC:
2958  if (d2 <= 0)
2959  {
2960  goto log_error;
2961  }
2962  dtmp = log10 (d2) / log10 (d1);
2963  break;
2964  case DB_TYPE_MONETARY:
2965  d2 = (db_get_monetary (value2))->amount;
2966  if (d2 <= 0)
2967  {
2968  goto log_error;
2969  }
2970  dtmp = log10 (d2) / log10 (d1);
2971  break;
2972  default:
2974  break;
2975  }
2976  break;
2977 
2978  case DB_TYPE_NUMERIC:
2980  if (d1 <= 1)
2981  {
2982  goto log_error;
2983  }
2984 
2985  switch (type2)
2986  {
2987  case DB_TYPE_SHORT:
2988  s2 = db_get_short (value2);
2989  if (s2 <= 0)
2990  {
2991  goto log_error;
2992  }
2993  dtmp = log10 ((double) s2) / log10 (d1);
2994  break;
2995  case DB_TYPE_INTEGER:
2996  i2 = db_get_int (value2);
2997  if (i2 <= 0)
2998  {
2999  goto log_error;
3000  }
3001  dtmp = log10 ((double) i2) / log10 (d1);
3002  break;
3003  case DB_TYPE_BIGINT:
3004  bi2 = db_get_bigint (value2);
3005  if (bi2 <= 0)
3006  {
3007  goto log_error;
3008  }
3009  dtmp = log10 ((double) bi2) / log10 (d1);
3010  break;
3011  case DB_TYPE_FLOAT:
3012  f2 = db_get_float (value2);
3013  if (f2 <= 0)
3014  {
3015  goto log_error;
3016  }
3017  dtmp = log10 ((double) f2) / log10 (d1);
3018  break;
3019  case DB_TYPE_DOUBLE:
3020  d2 = db_get_double (value2);
3021  if (d2 <= 0)
3022  {
3023  goto log_error;
3024  }
3025  dtmp = log10 (d2) / log10 (d1);
3026  break;
3027  case DB_TYPE_NUMERIC:
3029  if (d2 <= 0)
3030  {
3031  goto log_error;
3032  }
3033  dtmp = log10 (d2) / log10 (d1);
3034  break;
3035  case DB_TYPE_MONETARY:
3036  d2 = (db_get_monetary (value2))->amount;
3037  if (d2 <= 0)
3038  {
3039  goto log_error;
3040  }
3041  dtmp = log10 (d2) / log10 (d1);
3042  break;
3043  default:
3045  break;
3046  }
3047  break;
3048 
3049  case DB_TYPE_MONETARY:
3050  d1 = (db_get_monetary (value1))->amount;
3051  if (d1 <= 1)
3052  {
3053  goto log_error;
3054  }
3055 
3056  switch (type2)
3057  {
3058  case DB_TYPE_SHORT:
3059  s2 = db_get_short (value2);
3060  if (s2 <= 0)
3061  {
3062  goto log_error;
3063  }
3064  dtmp = log10 ((double) s2) / log10 (d1);
3065  break;
3066  case DB_TYPE_INTEGER:
3067  i2 = db_get_int (value2);
3068  if (i2 <= 0)
3069  {
3070  goto log_error;
3071  }
3072  dtmp = log10 ((double) i2) / log10 (d1);
3073  break;
3074  case DB_TYPE_BIGINT:
3075  bi2 = db_get_bigint (value2);
3076  if (bi2 <= 0)
3077  {
3078  goto log_error;
3079  }
3080  dtmp = log10 ((double) bi2) / log10 (d1);
3081  break;
3082  case DB_TYPE_FLOAT:
3083  f2 = db_get_float (value2);
3084  if (f2 <= 0)
3085  {
3086  goto log_error;
3087  }
3088  dtmp = log10 ((double) f2) / log10 (d1);
3089  break;
3090  case DB_TYPE_DOUBLE:
3091  d2 = db_get_double (value2);
3092  if (d2 <= 0)
3093  {
3094  goto log_error;
3095  }
3096  dtmp = log10 (d2) / log10 (d1);
3097  break;
3098  case DB_TYPE_NUMERIC:
3100  if (d2 <= 0)
3101  {
3102  goto log_error;
3103  }
3104  dtmp = log10 (d2) / log10 (d1);
3105  break;
3106  case DB_TYPE_MONETARY:
3107  d2 = (db_get_monetary (value2))->amount;
3108  if (d2 <= 0)
3109  {
3110  goto log_error;
3111  }
3112  dtmp = log10 (d2) / log10 (d1);
3113  break;
3114  default:
3116  break;
3117  }
3118  break;
3119 
3120  default:
3122  return ER_FAILED;
3123  }
3124 
3125  db_make_double (result, dtmp);
3126  return NO_ERROR;
3127 
3128 log_error:
3130  return ER_FAILED;
3131 }
3132 
3133 /*
3134  * truncate_double ()
3135  * return: num truncated to integer places
3136  * num(in) :
3137  * integer(in):
3138  */
3139 static double
3140 truncate_double (double num, double integer)
3141 {
3142  /*
3143  * Under high optimization level, some optimizers (e.g, gcc -O3 on linux)
3144  * generates a wrong result without "volatile".
3145  */
3146  double scale_up, num_scale_up, result;
3147 
3148  if (num == 0)
3149  {
3150  return num;
3151  }
3152 
3153  scale_up = pow (10, integer);
3154  num_scale_up = num * scale_up;
3155  if (num > 0)
3156  {
3157  result = floor (num_scale_up);
3158  }
3159  else
3160  {
3161  result = ceil (num_scale_up);
3162  }
3163 
3164  if (num_scale_up == result) /* no need to calculate, return as it is */
3165  {
3166  result = num; /* to avoid possible truncation */
3167  }
3168  else
3169  {
3170  result = result / scale_up;
3171  }
3172 
3173  return result;
3174 }
3175 
3176 /*
3177  * truncate_bigint ()
3178  * return: num truncated to integer places
3179  * num(in) :
3180  * integer(in):
3181  */
3182 static DB_BIGINT
3184 {
3185  if (num == 0 || integer >= 0)
3186  {
3187  return num;
3188  }
3189 
3190  integer = (DB_BIGINT) pow (10, (double) -integer);
3191  num -= num % integer;
3192 
3193  return num;
3194 }
3195 
3196 /*
3197  * truncate_date ()
3198  * return: error or no error
3199  * date(in) :
3200  * fmt(in):
3201  */
3202 static int
3203 truncate_date (DB_DATE * date, const DB_VALUE * format_str)
3204 {
3205  int year, month, day;
3206  int error = NO_ERROR;
3207  TIMESTAMP_FORMAT format;
3208  int weekday;
3209  int days_months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
3210 
3211  assert (date != NULL);
3212  assert (format_str != NULL);
3213 
3214  error = db_get_date_format (format_str, &format);
3215  if (error != NO_ERROR)
3216  {
3217  goto end;
3218  }
3219 
3220  if (format == DT_INVALID)
3221  {
3222  error = ER_QSTR_INVALID_FORMAT;
3223  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0);
3224  goto end;
3225  }
3226 
3227  db_date_decode (date, &month, &day, &year);
3228 
3229  /* truncate datetime according to format */
3230  switch (format)
3231  {
3232  case DT_YYYY:
3233  case DT_YY:
3234  month = day = 1;
3235  break;
3236  case DT_MONTH:
3237  case DT_MON:
3238  case DT_MM:
3239  day = 1;
3240  break;
3241  case DT_DD:
3242  case DT_HH24:
3243  case DT_HH12:
3244  case DT_HH:
3245  case DT_H:
3246  case DT_MI:
3247  case DT_SS:
3248  case DT_MS:
3249  /* do nothing */
3250  break;
3251  case DT_Q: /* quarter */
3252  month = (month - 1) / 3 * 3 + 1;
3253  day = 1;
3254  break;
3255  case DT_DAY: /* week day */
3256  case DT_DY:
3257  case DT_D:
3258  weekday = day_of_week (*date);
3259  day = day - weekday; /* Sunday is the first day of a week */
3260 
3261  /* need adjust */
3262  if (day < 1)
3263  {
3264  month = month - 1;
3265  if (month < 1)
3266  {
3267  year = year - 1;
3268  month = 12;
3269  }
3270  if (month == 2 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
3271  {
3272  days_months[2] = 29;
3273  }
3274 
3275  day = day + days_months[month];
3276  }
3277  break;
3278  case DT_CC: /* one greater than the first two digits of a four-digit year */
3279  default:
3280  error = ER_QSTR_INVALID_FORMAT;
3281  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0);
3282  goto end;
3283  }
3284 
3285  error = db_date_encode (date, month, day, year);
3286  if (error != NO_ERROR)
3287  {
3288  goto end;
3289  }
3290 
3291 end:
3292 
3293  return error;
3294 }
3295 
3296 /*
3297  * db_trunc_dbval () - return dbval1 truncated to dbval2 decimal places
3298  *
3299  * There are four overloads
3300  * The first one is used to truncate number
3301  * trunc(PT_GENERIC_TYPE_NUMBER, PT_TYPE_INTEGER)
3302  * The second,third and fourth are used to truncate date/datetime/timestamp with formart
3303  * trunc(PT_TYPE_DATE/PT_TYPE_DATETIME/PT_TYPE_TIMESTAMP, PT_TYPE_CHAR)
3304  *
3305  * return: NO_ERROR, ER_FAILED
3306  * result(out): resultant db_value
3307  * value1(in) : first db_value
3308  * value2(in) : second db_value
3309  */
3310 int
3311 db_trunc_dbval (DB_VALUE * result, DB_VALUE * value1, DB_VALUE * value2)
3312 {
3313  DB_TYPE type1, type2;
3314  DB_BIGINT bi2;
3315  double dtmp;
3316  DB_VALUE cast_value, cast_format;
3317  int er_status = NO_ERROR;
3318  DB_DATE date;
3319  TP_DOMAIN *domain;
3320  TP_DOMAIN_STATUS cast_status;
3321 
3322  db_make_null (&cast_value);
3323  db_make_null (&cast_format);
3324 
3325  if (DB_IS_NULL (value1) || DB_IS_NULL (value2))
3326  {
3327  return NO_ERROR;
3328  }
3329 
3330  type1 = DB_VALUE_DOMAIN_TYPE (value1);
3331  type2 = DB_VALUE_DOMAIN_TYPE (value2);
3332  if (type2 != DB_TYPE_SHORT && type2 != DB_TYPE_INTEGER && type2 != DB_TYPE_BIGINT && type2 != DB_TYPE_NUMERIC
3333  && type2 != DB_TYPE_FLOAT && type2 != DB_TYPE_DOUBLE && type2 != DB_TYPE_MONETARY && !QSTR_IS_ANY_CHAR (type2))
3334  {
3335  er_status = ER_QPROC_INVALID_DATATYPE;
3336  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
3337 
3338  goto end;
3339  }
3340 
3341  /* convert value1 to double when it's a string */
3342  switch (type1)
3343  {
3344  case DB_TYPE_SHORT:
3345  case DB_TYPE_INTEGER:
3346  case DB_TYPE_BIGINT:
3347  case DB_TYPE_NUMERIC:
3348  case DB_TYPE_FLOAT:
3349  case DB_TYPE_DOUBLE:
3350  case DB_TYPE_MONETARY:
3351  case DB_TYPE_DATE:
3352  case DB_TYPE_DATETIME:
3353  case DB_TYPE_DATETIMELTZ:
3354  case DB_TYPE_DATETIMETZ:
3355  case DB_TYPE_TIMESTAMP:
3356  case DB_TYPE_TIMESTAMPLTZ:
3357  case DB_TYPE_TIMESTAMPTZ:
3358  break;
3359  default: /* convert to double */
3360  type1 = DB_TYPE_UNKNOWN;
3361 
3362  /* try type double */
3363  db_make_null (&cast_value);
3365  cast_status = tp_value_coerce (value1, &cast_value, domain);
3366  if (cast_status == DOMAIN_COMPATIBLE)
3367  {
3368  type1 = DB_TYPE_DOUBLE;
3369  value1 = &cast_value;
3370  }
3371 
3372  /* convert fail */
3373  if (type1 == DB_TYPE_UNKNOWN)
3374  {
3376  {
3377  db_make_null (result);
3378  er_clear ();
3379  er_status = NO_ERROR;
3380  }
3381  else
3382  {
3383  er_status = ER_QPROC_INVALID_DATATYPE;
3384  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
3385  }
3386 
3387  goto end;
3388  }
3389  }
3390 
3391  /* translate default fmt */
3392  if (type2 == DB_TYPE_CHAR && strcasecmp (db_get_string (value2), "default") == 0)
3393  {
3394  if (TP_IS_DATE_TYPE (type1))
3395  {
3396  db_make_string (&cast_format, "dd");
3397  }
3398  else
3399  {
3400  db_make_int (&cast_format, 0);
3401  type2 = DB_TYPE_INTEGER;
3402  }
3403 
3404  value2 = &cast_format;
3405  }
3406 
3407  if (type2 == DB_TYPE_INTEGER)
3408  {
3409  bi2 = db_get_int (value2);
3410  }
3411  else if (type2 == DB_TYPE_BIGINT)
3412  {
3413  bi2 = db_get_bigint (value2);
3414  }
3415  else if (type2 == DB_TYPE_SHORT)
3416  {
3417  bi2 = db_get_short (value2);
3418  }
3419  else if (type1 != DB_TYPE_DATE && type1 != DB_TYPE_DATETIME && type1 != DB_TYPE_DATETIMELTZ
3420  && type1 != DB_TYPE_DATETIMETZ && type1 != DB_TYPE_TIMESTAMP && type1 != DB_TYPE_TIMESTAMPLTZ
3421  && type1 != DB_TYPE_TIMESTAMPTZ)
3422  {
3424  cast_status = tp_value_coerce (value2, &cast_format, domain);
3425  if (cast_status != DOMAIN_COMPATIBLE)
3426  {
3428  {
3429  db_make_null (result);
3430  er_clear ();
3431  er_status = NO_ERROR;
3432  }
3433  else
3434  {
3435  er_status = ER_QSTR_INVALID_FORMAT;
3436  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
3437  }
3438 
3439  goto end;
3440  }
3441 
3442  bi2 = db_get_bigint (&cast_format);
3443  }
3444  else
3445  {
3446  bi2 = 0; /* to make compiler be silent */
3447  }
3448 
3449  switch (type1)
3450  {
3451  case DB_TYPE_SHORT:
3452  {
3453  short s1;
3454 
3455  s1 = db_get_short (value1);
3456  dtmp = truncate_double (s1, (double) bi2);
3457  db_make_short (result, (short) dtmp);
3458  }
3459  break;
3460  case DB_TYPE_INTEGER:
3461  {
3462  int i1;
3463 
3464  i1 = db_get_int (value1);
3465  dtmp = truncate_double (i1, (double) bi2);
3466  db_make_int (result, (int) dtmp);
3467  }
3468  break;
3469  case DB_TYPE_BIGINT:
3470  {
3471  DB_BIGINT bi1;
3472 
3473  bi1 = db_get_bigint (value1);
3474  bi1 = truncate_bigint (bi1, bi2);
3475  db_make_bigint (result, bi1);
3476  }
3477  break;
3478  case DB_TYPE_FLOAT:
3479  {
3480  float f1;
3481 
3482  f1 = db_get_float (value1);
3483  dtmp = truncate_double (f1, (double) bi2);
3484  db_make_float (result, (float) dtmp);
3485  }
3486  break;
3487 
3488  case DB_TYPE_DOUBLE:
3489  {
3490  double d1;
3491 
3492  d1 = db_get_double (value1);
3493  dtmp = truncate_double (d1, (double) bi2);
3494  db_make_double (result, (double) dtmp);
3495  }
3496  break;
3497  case DB_TYPE_NUMERIC:
3498  {
3499  unsigned char num[DB_NUMERIC_BUF_SIZE];
3500  char num_string[(2 * DB_MAX_NUMERIC_PRECISION) + 4];
3501  char *ptr, *end;
3502  int p, s;
3503 
3504  memset (num_string, 0, sizeof (num_string));
3505  numeric_coerce_num_to_dec_str (db_locate_numeric (value1), num_string);
3506  p = DB_VALUE_PRECISION (value1);
3507  s = DB_VALUE_SCALE (value1);
3508  end = num_string + strlen (num_string);
3509  ptr = end - s + bi2;
3510 
3511  if (end < ptr)
3512  {
3513  /* no need to round, return as it is */
3514  *result = *value1;
3515  break;
3516  }
3517  else if (ptr < num_string)
3518  {
3519  /* return zero */
3520  memset (num_string, 0, sizeof (num_string));
3521  }
3522  else
3523  {
3524  while (ptr < end)
3525  {
3526  *ptr++ = '0';
3527  }
3528  }
3529  numeric_coerce_dec_str_to_num (num_string, num);
3530  db_make_numeric (result, num, p, s);
3531  }
3532  break;
3533  case DB_TYPE_MONETARY:
3534  {
3535  double d1;
3536 
3537  d1 = (db_get_monetary (value1))->amount;
3538  dtmp = truncate_double (d1, (double) bi2);
3539  db_make_monetary (result, (db_get_monetary (value1))->type, dtmp);
3540  }
3541  break;
3542  case DB_TYPE_DATE:
3543  case DB_TYPE_DATETIME:
3544  case DB_TYPE_DATETIMELTZ:
3545  case DB_TYPE_DATETIMETZ:
3546  case DB_TYPE_TIMESTAMP:
3547  case DB_TYPE_TIMESTAMPLTZ:
3548  case DB_TYPE_TIMESTAMPTZ:
3549  if (type1 == DB_TYPE_DATE)
3550  {
3551  date = *(db_get_date (value1));
3552  }
3553  else if (type1 == DB_TYPE_DATETIME)
3554  {
3555  date = db_get_datetime (value1)->date;
3556  }
3557  else if (type1 == DB_TYPE_DATETIMELTZ)
3558  {
3559  DB_DATETIME local_dt, *p_dt;
3560 
3561  p_dt = db_get_datetime (value1);
3562 
3563  er_status = tz_datetimeltz_to_local (p_dt, &local_dt);
3564  if (er_status != NO_ERROR)
3565  {
3567  {
3568  er_clear ();
3569  db_make_null (result);
3570  er_status = NO_ERROR;
3571  }
3572  goto end;
3573  }
3574 
3575  date = local_dt.date;
3576  }
3577  else if (type1 == DB_TYPE_DATETIMETZ)
3578  {
3579  DB_DATETIME local_dt;
3580  DB_DATETIMETZ *p_dt_tz;
3581 
3582  p_dt_tz = db_get_datetimetz (value1);
3583 
3584  er_status = tz_utc_datetimetz_to_local (&p_dt_tz->datetime, &p_dt_tz->tz_id, &local_dt);
3585 
3586  if (er_status != NO_ERROR)
3587  {
3589  {
3590  er_clear ();
3591  db_make_null (result);
3592  er_status = NO_ERROR;
3593  }
3594  goto end;
3595  }
3596 
3597  date = local_dt.date;
3598  }
3599  else if (type1 == DB_TYPE_TIMESTAMPTZ)
3600  {
3601  DB_TIMESTAMPTZ *p_ts_tz;
3602 
3603  p_ts_tz = db_get_timestamptz (value1);
3604  er_status = db_timestamp_decode_w_tz_id (&p_ts_tz->timestamp, &p_ts_tz->tz_id, &date, NULL);
3605  if (er_status != NO_ERROR)
3606  {
3608  {
3609  er_clear ();
3610  db_make_null (result);
3611  er_status = NO_ERROR;
3612  }
3613  goto end;
3614  }
3615  }
3616  else
3617  {
3618  assert (type1 == DB_TYPE_TIMESTAMP || type1 == DB_TYPE_TIMESTAMPLTZ);
3619  (void) db_timestamp_decode_ses (db_get_timestamp (value1), &date, NULL);
3620  }
3621 
3622  er_status = truncate_date (&date, value2);
3623  if (er_status != NO_ERROR)
3624  {
3626  {
3627  er_clear ();
3628  er_status = NO_ERROR;
3629  }
3630  goto end;
3631  }
3632  else
3633  {
3634  db_value_put_encoded_date (result, &date);
3635  }
3636  break;
3637  default:
3639  {
3640  er_status = ER_QPROC_INVALID_DATATYPE;
3641  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, er_status, 0);
3642  goto end;
3643  }
3644  }
3645 
3646 end:
3647  pr_clear_value (&cast_value);
3648  pr_clear_value (&cast_format);
3649 
3650  return er_status;
3651 }
3652 
3653 /*
3654  * db_random_dbval () - take random integer
3655  * return: NO_ERROR
3656  * result(out) : resultant db_value
3657  */
3658 int
3660 {
3661  db_make_int (result, lrand48 ());
3662 
3663  return NO_ERROR;
3664 }
3665 
3666 /*
3667  * db_drandom_dbval () - take random double
3668  * return: NO_ERROR
3669  * result(out) : resultant db_value
3670  */
3671 int
3673 {
3674  db_make_double (result, drand48 ());
3675 
3676  return NO_ERROR;
3677 }
3678 
3679 /*
3680  * get_number_dbval_as_double () -
3681  * return: NO_ERROR/error code
3682  * d(out) : double
3683  * value(in) : input db_value
3684  */
3685 static int
3686 get_number_dbval_as_double (double *d, const DB_VALUE * value)
3687 {
3688  short s;
3689  int i;
3690  float f;
3691  double dtmp;
3692  DB_BIGINT bi;
3693 
3694  switch (DB_VALUE_DOMAIN_TYPE (value))
3695  {
3696  case DB_TYPE_SHORT:
3697  s = db_get_short (value);
3698  dtmp = (double) s;
3699  break;
3700  case DB_TYPE_INTEGER:
3701  i = db_get_int (value);
3702  dtmp = (double) i;
3703  break;
3704  case DB_TYPE_BIGINT:
3705  bi = db_get_bigint (value);
3706  dtmp = (double) bi;
3707  break;
3708  case DB_TYPE_FLOAT:
3709  f = db_get_float (value);
3710  dtmp = (double) f;
3711  break;
3712  case DB_TYPE_DOUBLE:
3713  dtmp = db_get_double (value);
3714  break;
3715  case DB_TYPE_NUMERIC:
3717  break;
3718  case DB_TYPE_MONETARY:
3719  dtmp = (db_get_monetary (value))->amount;
3720  break;
3721  default:
3724  }
3725 
3726  *d = dtmp;
3727  return NO_ERROR;
3728 }
3729 
3730 /*
3731  * db_cos_dbval () - computes cosine value of db_value
3732  * return: NO_ERROR
3733  * result(out): resultant db_value
3734  * value(in) : input db_value
3735  */
3736 int
3737 db_cos_dbval (DB_VALUE * result, DB_VALUE * value)
3738 {
3739  DB_TYPE type;
3740  int err;
3741  double dtmp;
3742 
3743  type = DB_VALUE_DOMAIN_TYPE (value);
3744  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3745  {
3746  db_make_null (result);
3747  return NO_ERROR;
3748  }
3749 
3750  err = get_number_dbval_as_double (&dtmp, value);
3751  if (err != NO_ERROR)
3752  {
3753  return err;
3754  }
3755 
3756  dtmp = cos (dtmp);
3757 
3758  db_make_double (result, dtmp);
3759  return NO_ERROR;
3760 }
3761 
3762 /*
3763  * db_sin_dbval () - computes sine value of db_value
3764  * return: NO_ERROR
3765  * result(out): resultant db_value
3766  * value(in) : input db_value
3767  */
3768 int
3769 db_sin_dbval (DB_VALUE * result, DB_VALUE * value)
3770 {
3771  DB_TYPE type;
3772  int err;
3773  double dtmp;
3774 
3775  type = DB_VALUE_DOMAIN_TYPE (value);
3776  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3777  {
3778  db_make_null (result);
3779  return NO_ERROR;
3780  }
3781 
3782  err = get_number_dbval_as_double (&dtmp, value);
3783  if (err != NO_ERROR)
3784  {
3785  return err;
3786  }
3787 
3788  dtmp = sin (dtmp);
3789 
3790  db_make_double (result, dtmp);
3791  return NO_ERROR;
3792 }
3793 
3794 /*
3795  * db_tan_dbval () - computes tangent value of db_value
3796  * return: NO_ERROR
3797  * result(out): resultant db_value
3798  * value(in) : input db_value
3799  */
3800 int
3801 db_tan_dbval (DB_VALUE * result, DB_VALUE * value)
3802 {
3803  DB_TYPE type;
3804  int err;
3805  double dtmp;
3806 
3807  type = DB_VALUE_DOMAIN_TYPE (value);
3808  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3809  {
3810  db_make_null (result);
3811  return NO_ERROR;
3812  }
3813 
3814  err = get_number_dbval_as_double (&dtmp, value);
3815  if (err != NO_ERROR)
3816  {
3817  return err;
3818  }
3819 
3820  dtmp = tan (dtmp);
3821 
3822  db_make_double (result, dtmp);
3823  return NO_ERROR;
3824 }
3825 
3826 /*
3827  * db_cot_dbval () - computes cotangent value of db_value
3828  * return: NO_ERROR
3829  * result(out): resultant db_value
3830  * value(in) : input db_value
3831  */
3832 int
3833 db_cot_dbval (DB_VALUE * result, DB_VALUE * value)
3834 {
3835  DB_TYPE type;
3836  int err;
3837  double dtmp;
3838 
3839  type = DB_VALUE_DOMAIN_TYPE (value);
3840  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3841  {
3842  db_make_null (result);
3843  return NO_ERROR;
3844  }
3845 
3846  err = get_number_dbval_as_double (&dtmp, value);
3847  if (err != NO_ERROR)
3848  {
3849  return err;
3850  }
3851 
3852  if (dtmp == 0)
3853  {
3854  db_make_null (result);
3855  }
3856  else
3857  {
3858  dtmp = 1 / tan (dtmp);
3859  db_make_double (result, dtmp);
3860  }
3861 
3862  return NO_ERROR;
3863 }
3864 
3865 /*
3866  * db_acos_dbval () - computes arc cosine value of db_value
3867  * return: NO_ERROR
3868  * result(out): resultant db_value
3869  * value(in) : input db_value
3870  */
3871 int
3872 db_acos_dbval (DB_VALUE * result, DB_VALUE * value)
3873 {
3874  DB_TYPE type;
3875  int err;
3876  double dtmp;
3877 
3878  type = DB_VALUE_DOMAIN_TYPE (value);
3879  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3880  {
3881  db_make_null (result);
3882  return NO_ERROR;
3883  }
3884 
3885  err = get_number_dbval_as_double (&dtmp, value);
3886  if (err != NO_ERROR)
3887  {
3888  return err;
3889  }
3890 
3891  if (dtmp < -1 || dtmp > 1)
3892  {
3893  goto error;
3894  }
3895 
3896  dtmp = acos (dtmp);
3897 
3898  db_make_double (result, dtmp);
3899  return NO_ERROR;
3900 
3901 error:
3903  {
3904  db_make_null (result);
3905  return NO_ERROR;
3906  }
3907  else
3908  {
3911  }
3912 }
3913 
3914 /*
3915  * db_asin_dbval () - computes arc sine value of db_value
3916  * return: NO_ERROR
3917  * result(out): resultant db_value
3918  * value(in) : input db_value
3919  */
3920 int
3921 db_asin_dbval (DB_VALUE * result, DB_VALUE * value)
3922 {
3923  DB_TYPE type;
3924  int err;
3925  double dtmp;
3926 
3927  type = DB_VALUE_DOMAIN_TYPE (value);
3928  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3929  {
3930  db_make_null (result);
3931  return NO_ERROR;
3932  }
3933 
3934  err = get_number_dbval_as_double (&dtmp, value);
3935  if (err != NO_ERROR)
3936  {
3937  return err;
3938  }
3939 
3940  if (dtmp < -1 || dtmp > 1)
3941  {
3942  goto error;
3943  }
3944 
3945  dtmp = asin (dtmp);
3946 
3947  db_make_double (result, dtmp);
3948  return NO_ERROR;
3949 
3950 error:
3952  {
3953  db_make_null (result);
3954  return NO_ERROR;
3955  }
3956  else
3957  {
3960  }
3961 }
3962 
3963 /*
3964  * db_atan_dbval () - computes arc tangent value of value2 / value
3965  * return: NO_ERROR
3966  * result(out): resultant db_value
3967  * value(in) : input db_value
3968  */
3969 int
3970 db_atan_dbval (DB_VALUE * result, DB_VALUE * value)
3971 {
3972  DB_TYPE type;
3973  int err;
3974  double dtmp;
3975 
3976  type = DB_VALUE_DOMAIN_TYPE (value);
3977  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
3978  {
3979  db_make_null (result);
3980  return NO_ERROR;
3981  }
3982 
3983  err = get_number_dbval_as_double (&dtmp, value);
3984  if (err != NO_ERROR)
3985  {
3986  return err;
3987  }
3988 
3989  dtmp = atan (dtmp);
3990 
3991  db_make_double (result, dtmp);
3992  return NO_ERROR;
3993 }
3994 
3995 /*
3996  * db_atan2_dbval () - computes arc tangent value of value2 / value
3997  * return: NO_ERROR
3998  * result(out): resultant db_value
3999  * value(in) : input db_value
4000  * value2(in) : second input db_value
4001  * OBS: this should have been done like db_power_dbval, i.e. switch in switch
4002  * but this yields in very much code so we prefered to get all values
4003  * separated and then convert all to double. Then just one call of atan2.
4004  */
4005 int
4006 db_atan2_dbval (DB_VALUE * result, DB_VALUE * value, DB_VALUE * value2)
4007 {
4008  DB_TYPE type, type2;
4009  int err;
4010  double d, d2, dtmp;
4011 
4012  /* arg1 */
4013  type = DB_VALUE_DOMAIN_TYPE (value);
4014  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
4015  {
4016  db_make_null (result);
4017  return NO_ERROR;
4018  }
4019 
4020  err = get_number_dbval_as_double (&d, value);
4021  if (err != NO_ERROR)
4022  {
4023  return err;
4024  }
4025 
4026  /* arg2 */
4027  type2 = DB_VALUE_DOMAIN_TYPE (value2);
4028  if (type2 == DB_TYPE_NULL || DB_IS_NULL (value2))
4029  {
4030  db_make_null (result);
4031  return NO_ERROR;
4032  }
4033 
4034  err = get_number_dbval_as_double (&d2, value2);
4035  if (err != NO_ERROR)
4036  {
4037  return err;
4038  }
4039 
4040  /* function call, all is double type */
4041  dtmp = atan2 (d, d2);
4042 
4043  db_make_double (result, dtmp);
4044  return NO_ERROR;
4045 }
4046 
4047 /*
4048  * db_degrees_dbval () - computes radians from value in degrees
4049  * return: NO_ERROR
4050  * result(out): resultant db_value
4051  * value(in) : input db_value
4052  */
4053 int
4055 {
4056  DB_TYPE type;
4057  int err;
4058  double dtmp;
4059 
4060  type = DB_VALUE_DOMAIN_TYPE (value);
4061  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
4062  {
4063  db_make_null (result);
4064  return NO_ERROR;
4065  }
4066 
4067  err = get_number_dbval_as_double (&dtmp, value);
4068  if (err != NO_ERROR)
4069  {
4070  return err;
4071  }
4072 
4073  dtmp = dtmp * (double) 57.295779513082320876798154814105; /* 180 / PI */
4074  db_make_double (result, dtmp);
4075 
4076  return NO_ERROR;
4077 }
4078 
4079 /*
4080  * db_radians_dbval () - converts degrees in value to radians
4081  * return: NO_ERROR
4082  * result(out): resultant db_value
4083  * value(in) : input db_value
4084  */
4085 int
4087 {
4088  DB_TYPE type;
4089  int err;
4090  double dtmp;
4091 
4092  type = DB_VALUE_DOMAIN_TYPE (value);
4093  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
4094  {
4095  db_make_null (result);
4096  return NO_ERROR;
4097  }
4098 
4099  err = get_number_dbval_as_double (&dtmp, value);
4100  if (err != NO_ERROR)
4101  {
4102  return err;
4103  }
4104 
4105  dtmp = dtmp * (double) 0.017453292519943295769236907684886; /* PI / 180 */
4106  db_make_double (result, dtmp);
4107 
4108  return NO_ERROR;
4109 }
4110 
4111 /*
4112  * db_log_generic_dbval () - computes log of db_value in base
4113  * return: NO_ERROR
4114  * result(out): resultant db_value
4115  * value(in) : input db_value
4116  */
4117 int
4118 db_log_generic_dbval (DB_VALUE * result, DB_VALUE * value, long b)
4119 {
4120  DB_TYPE type;
4121  int err;
4122  double dtmp;
4123  double base = ((b == -1) ? (2.7182818284590452353) : (double) b);
4124 
4125  type = DB_VALUE_DOMAIN_TYPE (value);
4126  if (type == DB_TYPE_NULL || DB_IS_NULL (value))
4127  {
4128  db_make_null (result);
4129  return NO_ERROR;
4130  }
4131 
4132  err = get_number_dbval_as_double (&dtmp, value);
4133  if (err != NO_ERROR)
4134  {
4135  return err;
4136  }
4137 
4138  if (dtmp > 0)
4139  {
4140  dtmp = log10 (dtmp) / log10 (base);
4141  db_make_double (result, dtmp);
4142  }
4143  else
4144  {
4145  const char *log_func;
4146 
4147  switch (b)
4148  {
4149  case -1:
4150  log_func = "ln()";
4151  break;
4152  case 2:
4153  log_func = "log2()";
4154  break;
4155  case 10:
4156  log_func = "log10()";
4157  break;
4158  default:
4159  assert (0);
4160  log_func = "unknown";
4161  break;
4162  }
4163 
4166  }
4167 
4168  return NO_ERROR;
4169 }
4170 
4171 /*
4172  * db_bit_count_dbval () - bit count of db_value
4173  * return:
4174  * result(out): resultant db_value
4175  * value(in) : input db_value
4176  */
4177 int
4179 {
4180  DB_TYPE type;
4181  short s;
4182  int i, c = 0;
4183  float f;
4184  double d;
4185  DB_BIGINT bi;
4186  DB_VALUE tmpval, *tmpval_p;
4187 
4188  if (value == NULL)
4189  {
4190  return ER_FAILED;
4191  }
4192 
4193  tmpval_p = value;
4194  type = DB_VALUE_DOMAIN_TYPE (value);
4195 
4196  if (DB_IS_NULL (value))
4197  {
4198  db_make_null (result);
4199  }
4200  else
4201  {
4202  switch (type)
4203  {
4204  case DB_TYPE_SHORT:
4205  s = db_get_short (value);
4206  for (c = 0; s; c++)
4207  {
4208  s &= s - 1;
4209  }
4210  break;
4211 
4212  case DB_TYPE_INTEGER:
4213  i = db_get_int (value);
4214  for (c = 0; i; c++)
4215  {
4216  i &= i - 1;
4217  }
4218  break;
4219 
4220  case DB_TYPE_BIGINT:
4221  bi = db_get_bigint (value);
4222  for (c = 0; bi; c++)
4223  {
4224  bi &= bi - 1;
4225  }
4226  break;
4227 
4228  case DB_TYPE_FLOAT:
4229  f = db_get_float (value);
4230  if (f < 0)
4231  {
4232  i = (int) (f - 0.5f);
4233  }
4234  else
4235  {
4236  i = (int) (f + 0.5f);
4237  }
4238  for (c = 0; i; c++)
4239  {
4240  i &= i - 1;
4241  }
4242  break;
4243 
4244  case DB_TYPE_MONETARY:
4245  d = (db_get_monetary (value))->amount;
4246  if (d < 0)
4247  {
4248  bi = (DB_BIGINT) (d - 0.5f);
4249  }
4250  else
4251  {
4252  bi = (DB_BIGINT) (d + 0.5f);
4253  }
4254  for (c = 0; bi; c++)
4255  {
4256  bi &= bi - 1;
4257  }
4258  break;
4259 
4260  case DB_TYPE_NUMERIC:
4261  if (tp_value_cast (value, &tmpval, &tp_Double_domain, false) != DOMAIN_COMPATIBLE)
4262  {
4264  return ER_FAILED;
4265  }
4266  tmpval_p = &tmpval;
4267  /* FALLTHRU */
4268  case DB_TYPE_DOUBLE:
4269  d = db_get_double (tmpval_p);
4270  if (d < 0)
4271  {
4272  bi = (DB_BIGINT) (d - 0.5f);
4273  }
4274  else
4275  {
4276  bi = (DB_BIGINT) (d + 0.5f);
4277  }
4278  for (c = 0; bi; c++)
4279  {
4280  bi &= bi - 1;
4281  }
4282  break;
4283 
4284  default:
4287  }
4288 
4289  db_make_int (result, c);
4290  }
4291 
4292  return NO_ERROR;
4293 }
4294 
4295 /*
4296  * db_typeof_dbval() -
4297  * return:
4298  * result(out):
4299  * value(in) : input db_value
4300  */
4301 int
4302 db_typeof_dbval (DB_VALUE * result, DB_VALUE * value)
4303 {
4304  DB_TYPE type;
4305  const char *type_name;
4306  char *buf;
4307 
4308  type = DB_VALUE_TYPE (value);
4309  type_name = pr_type_name (type);
4310  if (type_name == NULL)
4311  {
4312  db_make_null (result);
4313  return NO_ERROR;
4314  }
4315 
4316  switch (type)
4317  {
4318  case DB_TYPE_CHAR:
4319  case DB_TYPE_VARCHAR:
4320  case DB_TYPE_NCHAR:
4321  case DB_TYPE_VARNCHAR:
4322  case DB_TYPE_BIT:
4323  case DB_TYPE_VARBIT:
4324  case DB_TYPE_NUMERIC:
4325  buf = (char *) db_private_alloc (NULL, 128);
4326  if (buf == NULL)
4327  {
4329  return ER_OUT_OF_VIRTUAL_MEMORY;
4330  }
4331 
4332  if (type == DB_TYPE_NUMERIC)
4333  {
4334  snprintf (buf, 128, "%s (%u, %u)", type_name, value->domain.numeric_info.precision,
4335  value->domain.numeric_info.scale);
4336  }
4337  else
4338  {
4339  snprintf (buf, 128, "%s (%d)", type_name, value->domain.char_info.length);
4340  }
4341 
4342  db_make_string (result, buf);
4343  result->need_clear = true;
4344  break;
4345 
4346  default:
4347  db_make_string (result, type_name);
4348  }
4349 
4350  return NO_ERROR;
4351 }
4352 
4353 /*
4354  * get_number_dbval_as_long_double () -
4355  * return:
4356  * long double(out):
4357  * value(in) :
4358  */
4359 static int
4360 get_number_dbval_as_long_double (long double *ld, const DB_VALUE * value)
4361 {
4362  short s;
4363  int i;
4364  float f;
4365  long double dtmp;
4366  DB_BIGINT bi;
4367  char num_string[2 * DB_MAX_NUMERIC_PRECISION + 2];
4368  char *tail_ptr = NULL;
4369 
4370  switch (DB_VALUE_DOMAIN_TYPE (value))
4371  {
4372  case DB_TYPE_SHORT:
4373  s = db_get_short (value);
4374  dtmp = (long double) s;
4375  break;
4376 
4377  case DB_TYPE_INTEGER:
4378  i = db_get_int (value);
4379  dtmp = (long double) i;
4380  break;
4381 
4382  case DB_TYPE_BIGINT:
4383  bi = db_get_bigint (value);
4384  dtmp = (long double) bi;
4385  break;
4386 
4387  case DB_TYPE_FLOAT:
4388  f = db_get_float (value);
4389  dtmp = (long double) f;
4390  break;
4391 
4392  case DB_TYPE_DOUBLE:
4393  dtmp = (long double) db_get_double (value);
4394  break;
4395 
4396  case DB_TYPE_NUMERIC:
4398 #ifdef _ISOC99_SOURCE
4399  dtmp = strtold (num_string, &tail_ptr) / powl (10.0, DB_VALUE_SCALE (value));
4400 #else
4401  dtmp = atof (num_string) / pow (10.0, DB_VALUE_SCALE (value));
4402 #endif
4403  break;
4404 
4405  case DB_TYPE_MONETARY:
4406  dtmp = (long double) (db_get_monetary (value))->amount;
4407  break;
4408 
4409  default:
4410  assert (false);
4413  }
4414 
4415  *ld = dtmp;
4416  return NO_ERROR;
4417 }
4418 
4419 /*
4420  * db_width_bucket_calculate_numeric() -
4421  * return:
4422  * result(out):
4423  * value1-4(in) : input db_value
4424  */
4425 static int
4426 db_width_bucket_calculate_numeric (double *result, const DB_VALUE * value1, const DB_VALUE * value2,
4427  const DB_VALUE * value3, const DB_VALUE * value4)
4428 {
4429  int er_status = NO_ERROR, c;
4430  DB_VALUE cmp_result;
4431  DB_VALUE n1, n2, n3, n4;
4432  double res = 0.0;
4433 
4434  assert (value1 != NULL && value2 != NULL && value3 != NULL && value4 != NULL && result != NULL);
4435 
4437  && DB_VALUE_TYPE (value3) == DB_TYPE_NUMERIC && DB_VALUE_TYPE (value4) == DB_TYPE_NUMERIC);
4438 
4439  db_make_null (&cmp_result);
4440  db_make_null (&n1);
4441  db_make_null (&n2);
4442  db_make_null (&n3);
4443  db_make_null (&n4);
4444 
4445  er_status = numeric_db_value_compare (value2, value3, &cmp_result);
4446  if (er_status != NO_ERROR)
4447  {
4448  return er_status;
4449  }
4450 
4451  c = db_get_int (&cmp_result);
4452  if (c == 0 || c == -1)
4453  {
4454  /* value2 <= value3 */
4455 
4456  er_status = numeric_db_value_compare (value1, value2, &cmp_result);
4457  if (er_status != NO_ERROR)
4458  {
4459  return er_status;
4460  }
4461 
4462  if (db_get_int (&cmp_result) < 0)
4463  {
4464  res = 0.0;
4465  }
4466  else
4467  {
4468  er_status = numeric_db_value_compare (value3, value1, &cmp_result);
4469  if (er_status != NO_ERROR)
4470  {
4471  return er_status;
4472  }
4473 
4474  if (db_get_int (&cmp_result) < 1)
4475  {
4477  res += 1.0;
4478  }
4479  else
4480  {
4481  /* floor ((v1-v2)/((v3-v2)/v4)) + 1 */
4482  er_status = numeric_db_value_sub (value1, value2, &n1);
4483  if (er_status != NO_ERROR)
4484  {
4485  return er_status;
4486  }
4487 
4488  er_status = numeric_db_value_sub (value3, value2, &n2);
4489  if (er_status != NO_ERROR)
4490  {
4491  return er_status;
4492  }
4493 
4494  er_status = numeric_db_value_div (&n2, value4, &n3);
4495  if (er_status != NO_ERROR)
4496  {
4497  return er_status;
4498  }
4499 
4500  er_status = numeric_db_value_div (&n1, &n3, &n4);
4501  if (er_status != NO_ERROR)
4502  {
4503  return er_status;
4504  }
4505 
4507  if (OR_CHECK_DOUBLE_OVERFLOW (res))
4508  {
4509  return ER_IT_DATA_OVERFLOW;
4510  }
4511 
4512  res = floor (res) + 1.0;
4513  }
4514  }
4515  }
4516  else
4517  {
4518  /* value2 > value3 */
4519  assert (c == 1);
4520 
4521  er_status = numeric_db_value_compare (value2, value1, &cmp_result);
4522  if (er_status != NO_ERROR)
4523  {
4524  return er_status;
4525  }
4526 
4527  if (db_get_int (&cmp_result) < 0)
4528  {
4529  res = 0.0;
4530  }
4531  else
4532  {
4533  er_status = numeric_db_value_compare (value2, value3, &cmp_result);
4534  if (er_status != NO_ERROR)
4535  {
4536  return er_status;
4537  }
4538 
4539  if (db_get_int (&cmp_result) < 1)
4540  {
4542  res += 1.0;
4543  }
4544  else
4545  {
4546  /* floor ((v2-v1)/((v2-v3)/v4)) + 1 */
4547  er_status = numeric_db_value_sub (value2, value1, &n1);
4548  if (er_status != NO_ERROR)
4549  {
4550  return er_status;
4551  }
4552 
4553  er_status = numeric_db_value_sub (value2, value3, &n2);
4554  if (er_status != NO_ERROR)
4555  {
4556  return er_status;
4557  }
4558 
4559  er_status = numeric_db_value_div (&n2, value4, &n3);
4560  if (er_status != NO_ERROR)
4561  {
4562  return er_status;
4563  }
4564 
4565  er_status = numeric_db_value_div (&n1, &n3, &n4);
4566  if (er_status != NO_ERROR)
4567  {
4568  return er_status;
4569  }
4570 
4572  if (OR_CHECK_DOUBLE_OVERFLOW (res))
4573  {
4574  return ER_IT_DATA_OVERFLOW;
4575  }
4576 
4577  res = floor (res) + 1.0;
4578  }
4579  }
4580  }
4581 
4582  if (OR_CHECK_DOUBLE_OVERFLOW (res))
4583  {
4585  }
4586 
4587  *result = res;
4588  return NO_ERROR;
4589 }
4590 
4591 /*
4592  * db_width_bucket() -
4593  * return:
4594  * result(out):
4595  * value1-4(in) : input db_value
4596  */
4597 int
4598 db_width_bucket (DB_VALUE * result, const DB_VALUE * value1, const DB_VALUE * value2, const DB_VALUE * value3,
4599  const DB_VALUE * value4)
4600 {
4601 #define RETURN_ERROR(err) \
4602  do \
4603  { \
4604  if (prm_get_bool_value (PRM_ID_RETURN_NULL_ON_FUNCTION_ERRORS) == true) \
4605  { \
4606  db_make_null (result); \
4607  er_clear (); \
4608  return NO_ERROR; \
4609  } \
4610  else \
4611  { \
4612  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, (err), 0); \
4613  return (err); \
4614  } \
4615  } \
4616  while (0)
4617 
4618 #define RETURN_ERROR_WITH_ARG(err, arg) \
4619  do \
4620  { \
4621  if (prm_get_bool_value (PRM_ID_RETURN_NULL_ON_FUNCTION_ERRORS) == true) \
4622  { \
4623  db_make_null (result); \
4624  er_clear (); \
4625  return NO_ERROR; \
4626  } \
4627  else \
4628  { \
4629  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, (err), 1, (arg)); \
4630  return (err); \
4631  } \
4632  } \
4633  while (0)
4634 
4635 #define MAX_DOMAIN_NAME_SIZE 150
4636 
4637  double d1, d2, d3, d4, d_ret;
4638  double tmp_d1 = 0.0, tmp_d2 = 0.0, tmp_d3 = 0.0, tmp_d4 = 0.0;
4639  DB_TYPE type, cast_type;
4640  DB_VALUE cast_value1, cast_value2, cast_value3, cast_value4;
4641  TP_DOMAIN *cast_domain = NULL;
4642  TP_DOMAIN *numeric_domain = NULL;
4643  TP_DOMAIN_STATUS cast_status;
4644  bool is_deal_with_numeric = false;
4645  int er_status = NO_ERROR;
4646  char buf[MAX_DOMAIN_NAME_SIZE];
4647 
4648  assert (result != NULL && value1 != NULL && value2 != NULL && value3 != NULL && value4 != NULL);
4649 
4650  db_make_null (&cast_value1);
4651  db_make_null (&cast_value2);
4652  db_make_null (&cast_value3);
4653  db_make_null (&cast_value4);
4654 
4655  if (DB_VALUE_TYPE (value1) == DB_TYPE_NULL || DB_VALUE_TYPE (value2) == DB_TYPE_NULL
4656  || DB_VALUE_TYPE (value3) == DB_TYPE_NULL || DB_VALUE_TYPE (value4) == DB_TYPE_NULL)
4657  {
4658  db_make_null (result);
4659  return NO_ERROR;
4660  }
4661 
4662  d4 = db_get_double (value4);
4663  if (d4 < 1 || d4 >= DB_INT32_MAX)
4664  {
4666  }
4667 
4668  d4 = (int) floor (d4);
4669 
4670  /* find the common type of value1, value2 and value3 and cast them to the common type */
4671  type = DB_VALUE_DOMAIN_TYPE (value1);
4672  switch (type)
4673  {
4674  case DB_TYPE_CHAR:
4675  case DB_TYPE_VARCHAR:
4676  case DB_TYPE_NCHAR:
4677  case DB_TYPE_VARNCHAR:
4678  /* try double */
4679  cast_type = DB_TYPE_UNKNOWN;
4680  cast_domain = tp_domain_resolve_default (DB_TYPE_DOUBLE);
4681  cast_status = tp_value_coerce (value1, &cast_value1, cast_domain);
4682  if (cast_status == DOMAIN_COMPATIBLE)
4683  {
4684  cast_type = DB_TYPE_DOUBLE;
4685  }
4686  else
4687  {
4688  /* try datetime date, timestamp is compatible with datetime */
4690  cast_status = tp_value_coerce (value1, &cast_value1, cast_domain);
4691  if (cast_status == DOMAIN_COMPATIBLE)
4692  {
4693  cast_type = DB_TYPE_DATETIME;
4694  }
4695  else
4696  {
4697  /* try time */
4698  er_clear (); // forget previous error to try datetime
4699 
4700  cast_domain = tp_domain_resolve_default (DB_TYPE_TIME);
4701  cast_status = tp_value_coerce (value1, &cast_value1, cast_domain);
4702  if (cast_status == DOMAIN_COMPATIBLE)
4703  {
4704  cast_type = DB_TYPE_TIME;
4705  }
4706  else
4707  {
4709  }
4710  }
4711  }
4712 
4713  value1 = &cast_value1;
4714 
4715  /* coerce value2 with the type of value1 */
4716  if (cast_type != DB_VALUE_DOMAIN_TYPE (value2))
4717  {
4718  cast_domain = tp_domain_resolve_default (cast_type);
4719  cast_status = tp_value_coerce (value2, &cast_value2, cast_domain);
4720  if (cast_status != DOMAIN_COMPATIBLE)
4721  {
4723  }
4724 
4725  value2 = &cast_value2;
4726  }
4727 
4728  /* coerce value3 with the type of value1 */
4729  if (cast_type != DB_VALUE_DOMAIN_TYPE (value3))
4730  {
4731  cast_domain = tp_domain_resolve_default (cast_type);
4732  cast_status = tp_value_coerce (value3, &cast_value3, cast_domain);
4733  if (cast_status != DOMAIN_COMPATIBLE)
4734  {
4736  }
4737 
4738  value3 = &cast_value3;
4739  }
4740  break;
4741 
4742  default:
4743  break;
4744  }
4745 
4746  /* the type of value1 is fixed */
4747  type = DB_VALUE_DOMAIN_TYPE (value1);
4748  switch (type)
4749  {
4750  case DB_TYPE_DATE:
4751  d1 = (double) *db_get_date (value1);
4752  d2 = (double) *db_get_date (value2);
4753  d3 = (double) *db_get_date (value3);
4754  break;
4755 
4756  case DB_TYPE_DATETIME:
4757  case DB_TYPE_DATETIMELTZ:
4758  /* double can hold datetime type */
4759  d1 = ((double) db_get_datetime (value1)->date) * MILLISECONDS_OF_ONE_DAY + db_get_datetime (value1)->time;
4760  d2 = ((double) db_get_datetime (value2)->date) * MILLISECONDS_OF_ONE_DAY + db_get_datetime (value2)->time;
4761  d3 = ((double) db_get_datetime (value3)->date) * MILLISECONDS_OF_ONE_DAY + db_get_datetime (value3)->time;
4762  break;
4763 
4764  case DB_TYPE_DATETIMETZ:
4765  /* double can hold datetime type */
4766  d1 = (((double) db_get_datetimetz (value1)->datetime.date) * MILLISECONDS_OF_ONE_DAY
4767  + db_get_datetimetz (value1)->datetime.time);
4768  d2 = (((double) db_get_datetimetz (value2)->datetime.date) * MILLISECONDS_OF_ONE_DAY
4769  + db_get_datetimetz (value2)->datetime.time);
4770  d3 = (((double) db_get_datetimetz (value3)->datetime.date) * MILLISECONDS_OF_ONE_DAY
4771  + db_get_datetimetz (value3)->datetime.time);
4772  break;
4773 
4774  case DB_TYPE_TIMESTAMP:
4775  case DB_TYPE_TIMESTAMPLTZ:
4776  d1 = (double) *db_get_timestamp (value1);
4777  d2 = (double) *db_get_timestamp (value2);
4778  d3 = (double) *db_get_timestamp (value3);
4779  break;
4780 
4781  case DB_TYPE_TIMESTAMPTZ:
4782  d1 = (double) (db_get_timestamptz (value1)->timestamp);
4783  d2 = (double) (db_get_timestamptz (value2)->timestamp);
4784  d3 = (double) (db_get_timestamptz (value3)->timestamp);
4785  break;
4786 
4787  case DB_TYPE_TIME:
4788  d1 = (double) *db_get_time (value1);
4789  d2 = (double) *db_get_time (value2);
4790  d3 = (double) *db_get_time (value3);
4791  break;
4792 
4793  case DB_TYPE_SHORT:
4794  case DB_TYPE_INTEGER:
4795  case DB_TYPE_FLOAT:
4796  case DB_TYPE_DOUBLE:
4797  case DB_TYPE_MONETARY:
4798  if (get_number_dbval_as_double (&d1, value1) != NO_ERROR)
4799  {
4801  }
4802  if (get_number_dbval_as_double (&d2, value2) != NO_ERROR)
4803  {
4805  }
4806  if (get_number_dbval_as_double (&d3, value3) != NO_ERROR)
4807  {
4809  }
4810  break;
4811 
4812  case DB_TYPE_BIGINT:
4813  case DB_TYPE_NUMERIC:
4814  d1 = d2 = d3 = 0; /* to make compiler be silent */
4815 
4816  /* gcc fully support long double (80 or 128bits) if long double is not fully supported, do calculation with
4817  * numeric */
4818  numeric_domain = tp_domain_new (DB_TYPE_NUMERIC);
4819  if (numeric_domain == NULL)
4820  {
4821  RETURN_ERROR (er_errid ());
4822  }
4823 
4824  cast_domain = numeric_domain;
4825 
4826  cast_domain->precision = 2 * DB_BIGINT_PRECISION;
4827  cast_domain->scale = DB_FLOAT_DECIMAL_PRECISION;
4828 
4829  if (type == DB_TYPE_BIGINT)
4830  {
4831  /* cast bigint to numeric Compiler doesn't support long double (80 or 128bits), so we use numeric instead. If
4832  * a high precision lib is introduced or long double is full supported, remove this part and use the lib or
4833  * long double to calculate. */
4834  /* convert value1 */
4835  cast_status = tp_value_coerce (value1, &cast_value1, cast_domain);
4836  if (cast_status != DOMAIN_COMPATIBLE)
4837  {
4838  tp_domain_name (numeric_domain, buf, MAX_DOMAIN_NAME_SIZE);
4839  tp_domain_free (numeric_domain);
4841  }
4842 
4843  value1 = &cast_value1;
4844  }
4845 
4846  /* cast value2, value3, value4 to numeric to make the calculation */
4847  if (DB_VALUE_DOMAIN_TYPE (value2) != DB_TYPE_NUMERIC)
4848  {
4849  cast_status = tp_value_coerce (value2, &cast_value2, cast_domain);
4850  if (cast_status != DOMAIN_COMPATIBLE)
4851  {
4852  tp_domain_name (numeric_domain, buf, MAX_DOMAIN_NAME_SIZE);
4853  tp_domain_free (numeric_domain);
4855  }
4856 
4857  value2 = &cast_value2;
4858  }
4859 
4860  if (DB_VALUE_DOMAIN_TYPE (value3) != DB_TYPE_NUMERIC)
4861  {
4862  cast_status = tp_value_coerce (value3, &cast_value3, cast_domain);
4863  if (cast_status != DOMAIN_COMPATIBLE)
4864  {
4865  tp_domain_name (numeric_domain, buf, MAX_DOMAIN_NAME_SIZE);
4866  tp_domain_free (numeric_domain);
4868  }
4869 
4870  value3 = &cast_value3;
4871  }
4872 
4873  db_make_int (&cast_value4, ((int) d4));
4874  cast_domain->precision = DB_INTEGER_PRECISION;
4875  cast_domain->scale = 0;
4876  cast_status = tp_value_coerce (&cast_value4, &cast_value4, cast_domain);
4877  if (cast_status != DOMAIN_COMPATIBLE)
4878  {
4879  tp_domain_free (numeric_domain);
4881  }
4882 
4883  value4 = &cast_value4;
4884 
4885  is_deal_with_numeric = true;
4886 
4887  tp_domain_free (numeric_domain);
4888  numeric_domain = NULL;
4889  break;
4890 
4891  default:
4893  }
4894 
4895  if (is_deal_with_numeric)
4896  {
4897  er_status = db_width_bucket_calculate_numeric (&d_ret, value1, value2, value3, value4);
4898  if (er_status != NO_ERROR)
4899  {
4900  RETURN_ERROR (er_status);
4901  }
4902  }
4903  else
4904  {
4905  if (d2 <= d3)
4906  {
4907  if (d1 < d2)
4908  {
4909  d_ret = 0.0;
4910  }
4911  else if (d3 <= d1)
4912  {
4913  d_ret = d4 + 1.0;
4914  }
4915  else
4916  {
4917  /* d_ret = floor ((d1 - d2) / ((d3 - d2) / d4)) + 1.0 */
4918  tmp_d1 = d1 - d2;
4919  tmp_d2 = d3 - d2;
4920  tmp_d3 = tmp_d2 / d4;
4921  tmp_d4 = tmp_d1 / tmp_d3;
4922  d_ret = floor (tmp_d4) + 1.0;
4923  }
4924  }
4925  else
4926  {
4927  if (d2 < d1)
4928  {
4929  d_ret = 0.0;
4930  }
4931  else if (d1 <= d3)
4932  {
4933  d_ret = d4 + 1.0;
4934  }
4935  else
4936  {
4937  /* d_ret = floor ((d2 - d1) / ((d2 - d3) / d4)) + 1.0 */
4938  tmp_d1 = d2 - d1;
4939  tmp_d2 = d2 - d3;
4940  tmp_d3 = tmp_d2 / d4;
4941  tmp_d4 = tmp_d1 / tmp_d3;
4942  d_ret = floor (tmp_d4) + 1.0;
4943  }
4944  }
4945  }
4946 
4947  /* check overflow */
4948  if (OR_CHECK_DOUBLE_OVERFLOW (tmp_d1) || OR_CHECK_DOUBLE_OVERFLOW (tmp_d2))
4949  {
4951  }
4952  else if (OR_CHECK_DOUBLE_OVERFLOW (tmp_d3) || OR_CHECK_DOUBLE_OVERFLOW (tmp_d4))
4953  {
4955  }
4956  else if (OR_CHECK_INT_OVERFLOW (d_ret))
4957  {
4959  }
4960 
4961  db_make_int (result, ((int) d_ret));
4962 
4963  return er_status;
4964 
4965 #undef RETURN_ERROR
4966 #undef RETURN_ERROR_WITH_ARG
4967 }
4968 
4969 /*
4970  * db_sleep() - sleep milli-secs
4971  * return:
4972  * result(out):
4973  * value(in) : input db_value
4974  */
4975 int
4976 db_sleep (DB_VALUE * result, DB_VALUE * value)
4977 {
4978  int error = NO_ERROR;
4979  long million_sec = 0;
4980 
4981  assert (result != NULL && value != NULL);
4983 
4984  db_make_null (result);
4985 
4986  if (DB_IS_NULL (value) || db_get_double (value) < 0.0)
4987  {
4988  error = ER_OBJ_INVALID_ARGUMENTS;
4989  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0);
4990 
4991  goto end;
4992  }
4993 
4994  million_sec = (long) (db_get_double (value) * 1000L);
4995 
4996  error = msleep (million_sec);
4997  if (error == NO_ERROR)
4998  {
4999  db_make_int (result, 0);
5000  }
5001  else
5002  {
5003  db_make_int (result, 1);
5004 
5005  error = NO_ERROR;
5006  }
5007 
5008 end:
5009 
5010  return error;
5011 }
5012 
5013 /*
5014  * db_crc32_dbval() - crc32
5015  * return: error code
5016  * result(out):
5017  * value(in) : input db_value
5018  */
5019 int
5020 db_crc32_dbval (DB_VALUE * result, DB_VALUE * value)
5021 {
5022  DB_TYPE type;
5023  int error_status = NO_ERROR;
5024  int hash_result = 0;
5025 
5026  assert (result != (DB_VALUE *) NULL);
5027 
5028  if (DB_IS_NULL (value))
5029  {
5030  PRIM_SET_NULL (result);
5031  return error_status;
5032  }
5033  else
5034  {
5035  type = DB_VALUE_DOMAIN_TYPE (value);
5036 
5037  if (QSTR_IS_ANY_CHAR (type))
5038  {
5039  crypt_crc32 (db_get_string (value), db_get_string_size (value), &hash_result);
5040  db_make_int (result, hash_result);
5041  }
5042  else
5043  {
5044  error_status = ER_QSTR_INVALID_DATA_TYPE;
5045  goto error;
5046  }
5047  }
5048 
5049  return error_status;
5050 
5051 error:
5052  PRIM_SET_NULL (result);
5054  {
5055  er_clear ();
5056  return NO_ERROR;
5057  }
5058  else
5059  {
5060  if (er_errid () == NO_ERROR)
5061  {
5062  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error_status, 0);
5063  }
5064  return error_status;
5065  }
5066 }
5067 
5068 int
5069 db_evaluate_json_contains (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5070 {
5071  int error_code = NO_ERROR;
5072  JSON_DOC_STORE source;
5073 
5074  db_make_null (result);
5075  if (num_args < 2)
5076  {
5077  assert (false);
5078  return ER_FAILED;
5079  }
5080 
5081  if (is_any_arg_null (arg, num_args))
5082  {
5083  return NO_ERROR;
5084  }
5085 
5086  const DB_VALUE *json = arg[0];
5087  const DB_VALUE *value = arg[1];
5088  const DB_VALUE *path = num_args == 3 ? arg[2] : NULL;
5089 
5090  error_code = db_value_to_json_doc (*json, false, source);
5091  if (error_code != NO_ERROR)
5092  {
5093  return error_code;
5094  }
5095 
5096  if (path != NULL)
5097  {
5098  JSON_DOC_STORE extracted_doc;
5099  /* *INDENT-OFF* */
5100  std::string raw_path;
5101  error_code = db_value_to_json_path (*path, F_JSON_CONTAINS, raw_path);
5102  if (error_code != NO_ERROR)
5103  {
5104  ASSERT_ERROR ();
5105  return error_code;
5106  }
5107  error_code = db_json_extract_document_from_path (source.get_immutable (), raw_path, extracted_doc);
5108  source = std::move (extracted_doc);
5109  /* *INDENT-ON* */
5110  if (error_code != NO_ERROR)
5111  {
5112  ASSERT_ERROR ();
5113  return error_code;
5114  }
5115  }
5116  else
5117  {
5118  //
5119  }
5120 
5121  if (source.get_immutable () != NULL)
5122  {
5123  bool has_member = false;
5124  JSON_DOC_STORE value_doc;
5125 
5126  int error_code = db_value_to_json_doc (*value, false, value_doc);
5127  if (error_code != NO_ERROR)
5128  {
5129  ASSERT_ERROR ();
5130  return error_code;
5131  }
5132 
5133  error_code = db_json_value_is_contained_in_doc (source.get_immutable (), value_doc.get_immutable (), has_member);
5134  if (error_code != NO_ERROR)
5135  {
5136  ASSERT_ERROR ();
5137  return error_code;
5138  }
5139  db_make_int (result, has_member ? 1 : 0);
5140  }
5141  return NO_ERROR;
5142 }
5143 
5144 int
5145 db_evaluate_json_type_dbval (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5146 {
5147  db_make_null (result);
5148  if (num_args != 1)
5149  {
5150  assert (false);
5151  return ER_FAILED;
5152  }
5153  DB_VALUE *json = arg[0];
5154  if (DB_IS_NULL (json))
5155  {
5156  return NO_ERROR;;
5157  }
5158  else
5159  {
5160  const char *type;
5161  unsigned int length;
5162  JSON_DOC_STORE doc;
5163 
5164  int error_code = db_value_to_json_doc (*json, false, doc);
5165  if (error_code != NO_ERROR)
5166  {
5167  return error_code;
5168  }
5169 
5170  type = db_json_get_type_as_str (doc.get_immutable ());
5171  length = strlen (type);
5172 
5173  return db_make_varchar (result, length, type, length, LANG_COERCIBLE_CODESET, LANG_COERCIBLE_COLL);
5174  }
5175 }
5176 
5177 int
5178 db_evaluate_json_valid (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5179 {
5180  db_make_null (result);
5181  if (num_args != 1)
5182  {
5183  assert (false);
5184  return ER_FAILED;
5185  }
5186  DB_VALUE *value = arg[0];
5187  if (DB_IS_NULL (value))
5188  {
5189  return NO_ERROR;
5190  }
5191  DB_TYPE type = db_value_domain_type (value);
5192  bool valid;
5193  if (type == DB_TYPE_JSON)
5194  {
5195  valid = true;
5196  }
5197  else if (TP_IS_CHAR_TYPE (type))
5198  {
5199  /* *INDENT-OFF* */
5200  valid = db_json_is_valid (std::string (db_get_string (value), db_get_string_size (value)).c_str ());
5201  /* *INDENT-ON* */
5202  }
5203  else
5204  {
5205  valid = false;
5206  }
5207  db_make_int (result, valid ? 1 : 0);
5208  return NO_ERROR;
5209 }
5210 
5211 int
5212 db_evaluate_json_length (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5213 {
5214  JSON_DOC_STORE source_doc;
5215  int error_code = NO_ERROR;
5216 
5217  db_make_null (result);
5218  if (num_args < 1 || num_args > 2)
5219  {
5220  assert (false);
5221  return ER_FAILED;
5222  }
5223 
5224  if (is_any_arg_null (arg, num_args))
5225  {
5226  return NO_ERROR;
5227  }
5228 
5229  DB_VALUE *json = arg[0];
5230  DB_VALUE *path = (num_args == 1) ? NULL : arg[1];
5231  unsigned int length;
5232 
5233  error_code = db_value_to_json_doc (*json, false, source_doc);
5234  if (error_code != NO_ERROR)
5235  {
5236  return error_code;
5237  }
5238 
5239  if (path != NULL)
5240  {
5241  JSON_DOC_STORE extracted_doc;
5242  /* *INDENT-OFF* */
5243  std::string raw_path;
5244  error_code = db_value_to_json_path (*path, F_JSON_LENGTH, raw_path);
5245  if (error_code != NO_ERROR)
5246  {
5247  ASSERT_ERROR ();
5248  return error_code;
5249  }
5250  error_code = db_json_extract_document_from_path (source_doc.get_immutable (), raw_path, extracted_doc, false);
5251  source_doc = std::move (extracted_doc);
5252  /* *INDENT-ON* */
5253  if (error_code != NO_ERROR)
5254  {
5255  ASSERT_ERROR ();
5256  return error_code;
5257  }
5258  }
5259 
5260  if (source_doc.get_immutable () != NULL)
5261  {
5262  length = db_json_get_length (source_doc.get_immutable ());
5263  db_make_int (result, length);
5264  }
5265  return NO_ERROR;
5266 }
5267 
5268 int
5269 db_evaluate_json_depth (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5270 {
5271  db_make_null (result);
5272  if (num_args != 1)
5273  {
5274  assert (false);
5275  return ER_FAILED;
5276  }
5277  DB_VALUE *json = arg[0];
5278  if (DB_IS_NULL (json))
5279  {
5280  return NO_ERROR;
5281  }
5282  JSON_DOC_STORE source_doc;
5283  int error_code = db_value_to_json_doc (*json, false, source_doc);
5284  if (error_code != NO_ERROR)
5285  {
5286  return error_code;
5287  }
5288  unsigned int depth = db_json_get_depth (source_doc.get_immutable ());
5289 
5290  return db_make_int (result, depth);
5291 }
5292 
5293 int
5294 db_evaluate_json_quote (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5295 {
5296  if (num_args != 1)
5297  {
5298  assert (false);
5299  return ER_FAILED;
5300  }
5301  return db_string_quote (arg[0], result);
5302 }
5303 
5304 int
5305 db_evaluate_json_unquote (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5306 {
5307  int error_code = NO_ERROR;
5308  db_make_null (result);
5309  if (num_args != 1)
5310  {
5311  assert (false);
5312  return ER_FAILED;
5313  }
5314  DB_VALUE *json = arg[0];
5315  if (DB_IS_NULL (json))
5316  {
5317  return NO_ERROR;
5318  }
5319  char *str = NULL;
5320  JSON_DOC_STORE source_doc;
5321  error_code = db_value_to_json_doc (*json, false, source_doc);
5322  if (error_code != NO_ERROR)
5323  {
5324  ASSERT_ERROR ();
5325  return error_code;
5326  }
5327  error_code = db_json_unquote (*source_doc.get_immutable (), str);
5328  if (error_code != NO_ERROR)
5329  {
5330  return error_code;
5331  }
5332 
5333  error_code = db_make_string (result, str);
5334  if (error_code != NO_ERROR)
5335  {
5336  return error_code;
5337  }
5338 
5339  // db_json_unquote uses strdup, therefore set need_clear flag
5340  result->need_clear = true;
5341  return NO_ERROR;
5342 }
5343 
5344 int
5345 db_evaluate_json_pretty (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5346 {
5347  int error_code = NO_ERROR;
5348  db_make_null (result);
5349 
5350  if (num_args != 1)
5351  {
5352  assert (false);
5353  return ER_FAILED;
5354  }
5355  DB_VALUE *json = arg[0];
5356  if (DB_IS_NULL (json))
5357  {
5358  return NO_ERROR;
5359  }
5360  char *str = NULL;
5361  JSON_DOC_STORE source_doc;
5362  error_code = db_value_to_json_doc (*json, false, source_doc);
5363  if (error_code != NO_ERROR)
5364  {
5365  ASSERT_ERROR ();
5366  return error_code;
5367  }
5368  db_json_pretty_func (*source_doc.get_immutable (), str);
5369 
5370  error_code = db_make_string (result, str);
5371  if (error_code != NO_ERROR)
5372  {
5373  return error_code;
5374  }
5375 
5376  // db_json_pretty_func uses strdup, therefore set need_clear flag
5377  result->need_clear = true;
5378 
5379  return error_code;
5380 }
5381 
5382 int
5383 db_accumulate_json_arrayagg (const DB_VALUE * json_db_val, DB_VALUE * json_res)
5384 {
5385  int error_code = NO_ERROR;
5386  JSON_DOC_STORE val_doc;
5387  JSON_DOC_STORE result_doc;
5388 
5389  if (DB_IS_NULL (json_db_val))
5390  {
5391  // this case should not be possible because we already wrapped a NULL value into a JSON with type DB_JSON_NULL
5392  assert (false);
5393  db_make_null (json_res);
5394  return ER_FAILED;
5395  }
5396 
5397  // get the current value
5398  error_code = db_value_to_json_value (*json_db_val, val_doc);
5399  if (error_code != NO_ERROR)
5400  {
5401  ASSERT_ERROR ();
5402  return error_code;
5403  }
5404 
5405  // append to existing document
5406  // allocate only first time
5407  if (DB_IS_NULL (json_res))
5408  {
5409  result_doc.create_mutable_reference ();
5410  }
5411  else
5412  {
5413  result_doc.set_mutable_reference (db_get_json_document (json_res));
5414  }
5415 
5416  if (result_doc.get_immutable () == NULL)
5417  {
5418  db_make_null (json_res);
5419  return ER_FAILED;
5420  }
5421 
5422  db_json_add_element_to_array (result_doc.get_mutable (), val_doc.get_immutable ());
5423 
5424  db_make_json_from_doc_store_and_release (*json_res, result_doc);
5425  return error_code;
5426 }
5427 
5428 /*
5429  * db_accumulate_json_objectagg () - Construct a Member (key-value pair) and add it in the result_json
5430  *
5431  * return : error_code
5432  * json_key (in) : the key of the pair
5433  * json_val (in) : the value of the pair
5434  * json_res (in) : the DB_VALUE that contains the document where we want to insert
5435  */
5436 int
5437 db_accumulate_json_objectagg (const DB_VALUE * json_key, const DB_VALUE * json_db_val, DB_VALUE * json_res)
5438 {
5439  int error_code = NO_ERROR;
5440  JSON_DOC_STORE val_doc;
5441  JSON_DOC_STORE result_doc;
5442 
5443  // this case should not be possible because we checked before if the key is NULL
5444  // and wrapped the value with a JSON with DB_JSON_NULL type
5445  if (DB_IS_NULL (json_key) || DB_IS_NULL (json_db_val))
5446  {
5447  assert (false);
5448  db_make_null (json_res);
5449  return ER_FAILED;
5450  }
5451 
5452  // get the current key
5453  /* *INDENT-OFF* */
5454  std::string key_str;
5455  /* *INDENT-ON* */
5456  error_code = db_value_to_json_key (*json_key, key_str);
5457  if (error_code != NO_ERROR)
5458  {
5459  ASSERT_ERROR ();
5460  return error_code;
5461  }
5462 
5463  // get the current value
5464  error_code = db_value_to_json_value (*json_db_val, val_doc);
5465  if (error_code != NO_ERROR)
5466  {
5467  ASSERT_ERROR ();
5468  return error_code;
5469  }
5470 
5471  // append to existing document
5472  // allocate only first time
5473  if (DB_IS_NULL (json_res))
5474  {
5475  result_doc.create_mutable_reference ();
5476  }
5477  else
5478  {
5479  result_doc.set_mutable_reference (db_get_json_document (json_res));
5480  }
5481 
5482  if (result_doc.get_immutable () == NULL)
5483  {
5484  db_make_null (json_res);
5485  return ER_FAILED;
5486  }
5487 
5488  error_code = db_json_add_member_to_object (result_doc.get_mutable (), key_str.c_str (), val_doc.get_immutable ());
5489  db_make_json_from_doc_store_and_release (*json_res, result_doc);
5490  if (error_code == ER_JSON_DUPLICATE_KEY)
5491  {
5492  // ignore
5493  er_clear ();
5494  error_code = NO_ERROR;
5495  }
5496  return error_code;
5497 }
5498 
5499 //
5500 // db_evaluate_json_extract () - extract paths from JSON and return a JSON object if there is only one path or a JSON
5501 // array if there are multiple paths
5502 //
5503 // return : error code
5504 // result (in) : result
5505 // args[] (in) :
5506 // num_args (in) :
5507 //
5508 // TODO: we need to change the args type of all JSON function to const DB_VALUE *[]
5509 //
5510 int
5511 db_evaluate_json_extract (DB_VALUE * result, DB_VALUE * const *args, int num_args)
5512 {
5513  db_make_null (result);
5514 
5515  if (num_args < 2)
5516  {
5517  // should be detected early
5518  assert (false);
5519  return ER_FAILED;
5520  }
5521 
5522  // there are multiple paths; the result of extract is a JSON_ARRAY with all extracted values
5523  int error_code = NO_ERROR;
5524  JSON_DOC_STORE source_doc;
5525 
5526  if (is_any_arg_null (args, num_args))
5527  {
5528  return NO_ERROR;
5529  }
5530 
5531  error_code = db_value_to_json_doc (*args[0], false, source_doc);
5532  if (error_code != NO_ERROR)
5533  {
5534  ASSERT_ERROR ();
5535  return error_code;
5536  }
5537  /* *INDENT-OFF* */
5538  std::vector<std::string> paths;
5539  /* *INDENT-ON* */
5540  for (int path_idx = 1; path_idx < num_args; path_idx++)
5541  {
5542  const DB_VALUE *path_value = args[path_idx];
5543  paths.emplace_back ();
5544  error_code = db_value_to_json_path (*path_value, F_JSON_EXTRACT, paths.back ());
5545  if (error_code != NO_ERROR)
5546  {
5547  ASSERT_ERROR ();
5548  return error_code;
5549  }
5550  }
5551 
5552  JSON_DOC_STORE res_doc;
5553  error_code = db_json_extract_document_from_path (source_doc.get_immutable (), paths, res_doc);
5554  if (error_code != NO_ERROR)
5555  {
5556  ASSERT_ERROR ();
5557  return error_code;
5558  }
5559 
5560  if (db_json_get_type (res_doc.get_immutable ()) != DB_JSON_NULL)
5561  {
5562  db_make_json_from_doc_store_and_release (*result, res_doc);
5563  }
5564 
5565  return NO_ERROR;
5566 }
5567 
5568 int
5569 db_evaluate_json_object (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5570 {
5571  int i;
5572  int error_code = NO_ERROR;
5573  JSON_DOC_STORE value_doc;
5574 
5575  db_make_null (result);
5576 
5577  if (num_args % 2 != 0)
5578  {
5579  assert (false); // should be caught earlier
5581  return ER_FAILED;
5582  }
5583 
5584  JSON_DOC_STORE new_doc;
5585  new_doc.set_mutable_reference (db_json_make_json_object ());
5586 
5587  for (i = 0; i < num_args; i += 2)
5588  {
5589  if (DB_IS_NULL (arg[i]))
5590  {
5593  }
5594 
5595  /* *INDENT-OFF* */
5596  std::string value_key;
5597  /* *INDENT-ON* */
5598  error_code = db_value_to_json_key (*arg[i], value_key);
5599  if (error_code != NO_ERROR)
5600  {
5601  ASSERT_ERROR ();
5602  return error_code;
5603  }
5604  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
5605  if (error_code != NO_ERROR)
5606  {
5607  ASSERT_ERROR ();
5608  return error_code;
5609  }
5610 
5611  error_code =
5612  db_json_add_member_to_object (new_doc.get_mutable (), value_key.c_str (), value_doc.get_immutable ());
5613  if (error_code != NO_ERROR)
5614  {
5615  ASSERT_ERROR ();
5616  return error_code;
5617  }
5618  }
5619 
5620  db_make_json_from_doc_store_and_release (*result, new_doc);
5621 
5622  return NO_ERROR;
5623 }
5624 
5625 int
5626 db_evaluate_json_array (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5627 {
5628  int error_code;
5629  JSON_DOC_STORE new_doc;
5630  new_doc.set_mutable_reference (db_json_make_json_array ());
5631  JSON_DOC_STORE value_doc;
5632 
5633  db_make_null (result);
5634 
5635  for (int i = 0; i < num_args; i++)
5636  {
5637  error_code = db_value_to_json_value (*arg[i], value_doc);
5638  if (error_code != NO_ERROR)
5639  {
5640  ASSERT_ERROR ();
5641  return error_code;
5642  }
5643 
5644  db_json_add_element_to_array (new_doc.get_mutable (), value_doc.get_immutable ());
5645  }
5646 
5647  db_make_json_from_doc_store_and_release (*result, new_doc);
5648 
5649  return NO_ERROR;
5650 }
5651 
5652 int
5653 db_evaluate_json_insert (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5654 {
5655  int i, error_code = NO_ERROR;
5656  JSON_DOC_STORE new_doc;
5657  JSON_DOC_STORE value_doc;
5658 
5659  db_make_null (result);
5660 
5661  if (num_args < 3 || num_args % 2 == 0)
5662  {
5663  assert (false);
5665  return ER_FAILED;
5666  }
5667 
5668  if (DB_IS_NULL (arg[0]))
5669  {
5670  return db_make_null (result);
5671  }
5672 
5673  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
5674  if (error_code != NO_ERROR)
5675  {
5676  ASSERT_ERROR ();
5677  return error_code;
5678  }
5679 
5680  for (i = 1; i < num_args; i += 2)
5681  {
5682  if (DB_IS_NULL (arg[i]))
5683  {
5684  return db_make_null (result);
5685  }
5686 
5687  // extract path
5688  /* *INDENT-OFF* */
5689  std::string value_path;
5690  /* *INDENT-ON* */
5691  error_code = db_value_to_json_path (*arg[i], F_JSON_INSERT, value_path);
5692  if (error_code != NO_ERROR)
5693  {
5694  ASSERT_ERROR ();
5695  return error_code;
5696  }
5697 
5698  // extract json value
5699  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
5700  if (error_code != NO_ERROR)
5701  {
5702  ASSERT_ERROR ();
5703  return error_code;
5704  }
5705 
5706  // insert into result the value at required path
5707  error_code = db_json_insert_func (value_doc.get_immutable (), *new_doc.get_mutable (), value_path.c_str ());
5708  if (error_code != NO_ERROR)
5709  {
5710  ASSERT_ERROR ();
5711  return error_code;
5712  }
5713  }
5714 
5715  db_make_json_from_doc_store_and_release (*result, new_doc);
5716 
5717  return NO_ERROR;
5718 }
5719 
5720 int
5721 db_evaluate_json_replace (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5722 {
5723  int i, error_code = NO_ERROR;
5724  JSON_DOC_STORE new_doc;
5725  JSON_DOC_STORE value_doc;
5726 
5727  db_make_null (result);
5728 
5729  if (num_args < 3 || num_args % 2 == 0)
5730  {
5731  assert_release (false);
5733  return ER_FAILED;
5734  }
5735 
5736  if (DB_IS_NULL (arg[0]))
5737  {
5738  return NO_ERROR;
5739  }
5740 
5741  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
5742  if (error_code != NO_ERROR)
5743  {
5744  ASSERT_ERROR ();
5745  return error_code;
5746  }
5747 
5748  for (i = 1; i < num_args; i += 2)
5749  {
5750  if (DB_IS_NULL (arg[i]))
5751  {
5752  return db_make_null (result);
5753  }
5754 
5755  // extract path
5756  /* *INDENT-OFF* */
5757  std::string value_path;
5758  /* *INDENT-ON* */
5759  error_code = db_value_to_json_path (*arg[i], F_JSON_REPLACE, value_path);
5760  if (error_code != NO_ERROR)
5761  {
5762  ASSERT_ERROR ();
5763  return error_code;
5764  }
5765 
5766  // extract json value
5767  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
5768  if (error_code != NO_ERROR)
5769  {
5770  ASSERT_ERROR ();
5771  return error_code;
5772  }
5773 
5774  // insert into result the value at requred path
5775  error_code = db_json_replace_func (value_doc.get_immutable (), *new_doc.get_mutable (), value_path.c_str ());
5776  if (error_code != NO_ERROR)
5777  {
5778  return error_code;
5779  }
5780  }
5781 
5782  db_make_json_from_doc_store_and_release (*result, new_doc);
5783 
5784  return NO_ERROR;
5785 }
5786 
5787 int
5788 db_evaluate_json_set (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5789 {
5790  int i, error_code = NO_ERROR;
5791  JSON_DOC_STORE new_doc;
5792  JSON_DOC_STORE value_doc;
5793 
5794  db_make_null (result);
5795 
5796  if (num_args < 3 || num_args % 2 == 0)
5797  {
5798  assert_release (false);
5800  return ER_FAILED;
5801  }
5802 
5803  if (DB_IS_NULL (arg[0]))
5804  {
5805  return NO_ERROR;
5806  }
5807 
5808  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
5809  if (error_code != NO_ERROR)
5810  {
5811  ASSERT_ERROR ();
5812  return error_code;
5813  }
5814 
5815  for (i = 1; i < num_args; i += 2)
5816  {
5817  if (DB_IS_NULL (arg[i]))
5818  {
5819  return db_make_null (result);
5820  }
5821 
5822  // extract path
5823  /* *INDENT-OFF* */
5824  std::string value_path;
5825  /* *INDENT-ON* */
5826  error_code = db_value_to_json_path (*arg[i], F_JSON_SET, value_path);
5827  if (error_code != NO_ERROR)
5828  {
5829  ASSERT_ERROR ();
5830  return error_code;
5831  }
5832 
5833  // extract json value
5834  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
5835  if (error_code != NO_ERROR)
5836  {
5837  ASSERT_ERROR ();
5838  return error_code;
5839  }
5840 
5841  // insert into result the value at requred path
5842  error_code = db_json_set_func (value_doc.get_immutable (), *new_doc.get_mutable (), value_path.c_str ());
5843  if (error_code != NO_ERROR)
5844  {
5845  return error_code;
5846  }
5847  }
5848 
5849  db_make_json_from_doc_store_and_release (*result, new_doc);
5850 
5851  return NO_ERROR;
5852 }
5853 
5854 int
5855 db_evaluate_json_keys (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5856 {
5857  int error_code = NO_ERROR;
5858  JSON_DOC_STORE new_doc;
5859  /* *INDENT-OFF* */
5860  std::string path;
5861  /* *INDENT-ON* */
5862 
5863  db_make_null (result);
5864 
5865  if (num_args > 2)
5866  {
5867  assert_release (false);
5869  return ER_FAILED;
5870  }
5871 
5872  if (is_any_arg_null (arg, num_args))
5873  {
5874  return NO_ERROR;
5875  }
5876 
5877  if (num_args == 1)
5878  {
5879  path = "";
5880  }
5881  else
5882  {
5883  error_code = db_value_to_json_path (*arg[1], F_JSON_KEYS, path);
5884  if (error_code != NO_ERROR)
5885  {
5886  ASSERT_ERROR ();
5887  return error_code;
5888  }
5889  }
5890 
5891  error_code = db_value_to_json_doc (*arg[0], false, new_doc);
5892  if (error_code != NO_ERROR)
5893  {
5894  ASSERT_ERROR ();
5895  return error_code;
5896  }
5897 
5898  JSON_DOC_STORE result_json;
5899  result_json.create_mutable_reference ();
5900  error_code = db_json_keys_func (*new_doc.get_immutable (), *result_json.get_mutable (), path.c_str ());
5901  if (error_code != NO_ERROR)
5902  {
5903  ASSERT_ERROR ();
5904  return error_code;
5905  }
5906 
5907  db_make_json_from_doc_store_and_release (*result, result_json);
5908  return NO_ERROR;
5909 }
5910 
5911 int
5912 db_evaluate_json_remove (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5913 {
5914  int i, error_code;
5915  JSON_DOC_STORE new_doc;
5916  // *INDENT-OFF*
5917  std::string path;
5918  // *INDENT-ON*
5919 
5920  db_make_null (result);
5921 
5922  if (num_args < 2)
5923  {
5924  assert (false);
5926  return ER_FAILED;
5927  }
5928 
5929  if (is_any_arg_null (arg, num_args))
5930  {
5931  return NO_ERROR;
5932  }
5933 
5934  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
5935  if (error_code != NO_ERROR)
5936  {
5937  ASSERT_ERROR ();
5938  return error_code;
5939  }
5940 
5941  for (i = 1; i < num_args; i++)
5942  {
5943  error_code = db_value_to_json_path (*arg[i], F_JSON_REMOVE, path);
5944  if (error_code != NO_ERROR)
5945  {
5946  ASSERT_ERROR ();
5947  return error_code;
5948  }
5949 
5950  error_code = db_json_remove_func (*new_doc.get_mutable (), path.c_str ());
5951  if (error_code != NO_ERROR)
5952  {
5953  ASSERT_ERROR ();
5954  return error_code;
5955  }
5956  }
5957 
5958  db_make_json_from_doc_store_and_release (*result, new_doc);
5959 
5960  return NO_ERROR;
5961 }
5962 
5963 int
5964 db_evaluate_json_array_append (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
5965 {
5966  int i, error_code = NO_ERROR;
5967  JSON_DOC_STORE new_doc;
5968  JSON_DOC_STORE value_doc;
5969 
5970  db_make_null (result);
5971 
5972  if (num_args < 3 || num_args % 2 == 0)
5973  {
5974  assert (false);
5976  return ER_FAILED;
5977  }
5978 
5979  if (DB_IS_NULL (arg[0]))
5980  {
5981  return NO_ERROR;
5982  }
5983 
5984  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
5985  if (error_code != NO_ERROR)
5986  {
5987  ASSERT_ERROR ();
5988  return error_code;
5989  }
5990 
5991  for (i = 1; i < num_args; i += 2)
5992  {
5993  if (DB_IS_NULL (arg[i]))
5994  {
5995  return db_make_null (result);
5996  }
5997 
5998  // extract path
5999  /* *INDENT-OFF* */
6000  std::string value_path;
6001  /* *INDENT-ON* */
6002  error_code = db_value_to_json_path (*arg[i], F_JSON_ARRAY_APPEND, value_path);
6003  if (error_code != NO_ERROR)
6004  {
6005  ASSERT_ERROR ();
6006  return error_code;
6007  }
6008 
6009  // extract json value
6010  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
6011  if (error_code != NO_ERROR)
6012  {
6013  ASSERT_ERROR ();
6014  return error_code;
6015  }
6016 
6017  // insert into result the value at required path
6018  error_code = db_json_array_append_func (value_doc.get_immutable (), *new_doc.get_mutable (), value_path.c_str ());
6019  if (error_code != NO_ERROR)
6020  {
6021  ASSERT_ERROR ();
6022  return error_code;
6023  }
6024  }
6025 
6026  db_make_json_from_doc_store_and_release (*result, new_doc);
6027 
6028  return NO_ERROR;
6029 }
6030 
6031 int
6032 db_evaluate_json_array_insert (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
6033 {
6034  int i, error_code = NO_ERROR;
6035  JSON_DOC_STORE new_doc;
6036  JSON_DOC_STORE value_doc;
6037 
6038  db_make_null (result);
6039 
6040  if (num_args < 3 || num_args % 2 == 0)
6041  {
6042  assert (false);
6044  return ER_FAILED;
6045  }
6046 
6047  if (DB_IS_NULL (arg[0]))
6048  {
6049  return NO_ERROR;
6050  }
6051 
6052  error_code = db_value_to_json_doc (*arg[0], true, new_doc);
6053  if (error_code != NO_ERROR)
6054  {
6055  ASSERT_ERROR ();
6056  return error_code;
6057  }
6058 
6059  for (i = 1; i < num_args; i += 2)
6060  {
6061  if (DB_IS_NULL (arg[i]))
6062  {
6063  return db_make_null (result);
6064  }
6065 
6066  // extract path
6067  /* *INDENT-OFF* */
6068  std::string value_path;
6069  /* *INDENT-ON* */
6070  error_code = db_value_to_json_path (*arg[i], F_JSON_ARRAY_INSERT, value_path);
6071  if (error_code != NO_ERROR)
6072  {
6073  ASSERT_ERROR ();
6074  return error_code;
6075  }
6076 
6077  // extract json value
6078  error_code = db_value_to_json_value (*arg[i + 1], value_doc);
6079  if (error_code != NO_ERROR)
6080  {
6081  ASSERT_ERROR ();
6082  return error_code;
6083  }
6084 
6085  // insert into result the value at required path
6086  error_code = db_json_array_insert_func (value_doc.get_immutable (), *new_doc.get_mutable (), value_path.c_str ());
6087  if (error_code != NO_ERROR)
6088  {
6089  ASSERT_ERROR ();
6090  return error_code;
6091  }
6092  }
6093 
6094  db_make_json_from_doc_store_and_release (*result, new_doc);
6095 
6096  return NO_ERROR;
6097 }
6098 
6099 int
6100 db_evaluate_json_contains_path (DB_VALUE * result, DB_VALUE * const *arg, const int num_args)
6101 {
6102  bool exists = false;
6103  int error_code = NO_ERROR;
6104  JSON_DOC_STORE doc;
6105  /* *INDENT-OFF* */
6106  std::vector<std::string> paths;
6107  /* *INDENT-ON* */
6108  db_make_null (result);
6109 
6110  if (is_any_arg_null (arg, num_args))
6111  {
6112  return NO_ERROR;
6113  }
6114 
6115  error_code = db_value_to_json_doc (*arg[0], false, doc);
6116  if (error_code != NO_ERROR)
6117  {
6118  return error_code;
6119  }
6120 
6121  bool find_all;
6122  error_code = is_str_find_all (arg[1], find_all);
6123  if (error_code != NO_ERROR)
6124  {
6125  ASSERT_ERROR ();
6126  return error_code;
6127  }
6128 
6129  for (int i = 2; i < num_args; ++i)
6130  {
6131  /* *INDENT-OFF* */
6132  paths.emplace_back ();
6133  /* *INDENT-ON* */
6134  error_code = db_value_to_json_path (*arg[i], F_JSON_CONTAINS_PATH, paths.back ());
6135  if (error_code != NO_ERROR)
6136  {
6137  ASSERT_ERROR ();
6138  return error_code;
6139  }
6140  }
6141 
6142  error_code = db_json_contains_path (doc.get_immutable (), paths, find_all, exists);
6143  if (error_code != NO_ERROR)
6144  {
6145  return error_code;
6146  }
6147 
6148  db_make_int (result, (int) exists);
6149  return error_code;
6150 }
6151 
6152 /*
6153  * db_evaluate_json_merge_preserve ()
6154  *
6155  * this function accumulate-merges jsons preserving members having duplicate keys
6156  * so merge (j1, j2, j3, j4) = merge (j1, (merge (j2, merge (j3, j4))))
6157  *
6158  * result (out): the merge result
6159  * arg (in): the arguments for the merge function
6160  * num_args (in)
6161  */
6162 int
6163 db_evaluate_json_merge_preserve (DB_VALUE * result, DB_VALUE * const *arg, const int num_args)
6164 {
6165  int error_code;
6166  JSON_DOC *accumulator = nullptr;
6167  JSON_DOC_STORE accumulator_owner;
6168  JSON_DOC_STORE doc;
6169 
6170  if (num_args < 2)
6171  {
6172  db_make_null (result);
6173  return NO_ERROR;
6174  }
6175 
6176  if (is_any_arg_null (arg, num_args))
6177  {
6178  db_make_null (result);
6179  return NO_ERROR;
6180  }
6181 
6182  for (int i = 0; i < num_args; ++i)
6183  {
6184  error_code = db_value_to_json_doc (*arg[i], false, doc);
6185  if (error_code != NO_ERROR)
6186  {
6187  ASSERT_ERROR ();
6188  return error_code;
6189  }
6190 
6191  error_code = db_json_merge_preserve_func (doc.get_immutable (), accumulator);
6192  accumulator_owner.set_mutable_reference (accumulator);
6193  if (error_code != NO_ERROR)
6194  {
6195  return error_code;
6196  }
6197  }
6198 
6199  db_make_json_from_doc_store_and_release (*result, accumulator_owner);
6200 
6201  return NO_ERROR;
6202 }
6203 
6204 /*
6205  * db_evaluate_json_merge_patch ()
6206  *
6207  * this function accumulate-merges jsons and patches members having duplicate keys
6208  * so merge (j1, j2, j3, j4) = merge (j1, (merge (j2, merge (j3, j4))))
6209  *
6210  * result (out): the merge result
6211  * arg (in): the arguments for the merge function
6212  * num_args (in)
6213  */
6214 int
6215 db_evaluate_json_merge_patch (DB_VALUE * result, DB_VALUE * const *arg, const int num_args)
6216 {
6217  int error_code;
6218  JSON_DOC *accumulator = nullptr;
6219  JSON_DOC_STORE accumulator_owner;
6220  JSON_DOC_STORE doc;
6221 
6222  if (num_args < 2)
6223  {
6224  db_make_null (result);
6225  return NO_ERROR;
6226  }
6227 
6228  if (is_any_arg_null (arg, num_args))
6229  {
6230  db_make_null (result);
6231  return NO_ERROR;
6232  }
6233 
6234  for (int i = 0; i < num_args; ++i)
6235  {
6236  error_code = db_value_to_json_doc (*arg[i], false, doc);
6237  if (error_code != NO_ERROR)
6238  {
6239  return error_code;
6240  }
6241 
6242  error_code = db_json_merge_patch_func (doc.get_immutable (), accumulator);
6243  accumulator_owner.set_mutable_reference (accumulator);
6244  if (error_code != NO_ERROR)
6245  {
6246  return error_code;
6247  }
6248  }
6249 
6250  db_make_json_from_doc_store_and_release (*result, accumulator_owner);
6251 
6252  return NO_ERROR;
6253 }
6254 
6255 /* *INDENT-OFF* */
6256 
6257 /*
6258  * JSON_SEARCH (json_doc, one/all, pattern [, escape_char, path_1,... path_n])
6259  *
6260  * db_json_search_dbval ()
6261  * function that finds paths of json_values that match the pattern argument
6262  * result (out): json string or json array if there are more paths that match
6263  * args (in): the arguments for the json_search function
6264  * num_args (in)
6265  */
6266 
6267 int
6268 db_evaluate_json_search (DB_VALUE *result, DB_VALUE * const * args, const int num_args)
6269 {
6270  int error_code = NO_ERROR;
6271  JSON_DOC_STORE doc;
6272  const size_t ESCAPE_CHAR_ARG_INDEX = 3;
6273 
6274  if (num_args < 3)
6275  {
6277  return ER_FAILED;
6278  }
6279 
6280  for (int i = 0; i < num_args; ++i)
6281  {
6282  // only escape char might be null
6283  if (i != ESCAPE_CHAR_ARG_INDEX && DB_IS_NULL (args[i]))
6284  {
6285  return db_make_null (result);
6286  }
6287  }
6288 
6289  error_code = db_value_to_json_doc (*args[0], false, doc);
6290  if (error_code != NO_ERROR)
6291  {
6292  return error_code;
6293  }
6294 
6295  bool find_all;
6296  error_code = is_str_find_all (args[1], find_all);
6297  if (error_code != NO_ERROR)
6298  {
6299  ASSERT_ERROR ();
6300  return error_code;
6301  }
6302 
6303  DB_VALUE *pattern = args[2];
6304  const DB_VALUE *esc_char = nullptr;
6305  const char * slash_str = "\\";
6306  DB_VALUE default_slash_str_dbval;
6307 
6308  if (num_args >= 4)
6309  {
6310  esc_char = args[3];
6311  }
6312  else
6313  {
6314  // No escape char arg provided
6316  {
6317  // This is equivalent to compat_mode=mysql. In this mode '\\' is default escape character for LIKE pattern
6318  db_make_string (&default_slash_str_dbval, slash_str);
6319  esc_char = &default_slash_str_dbval;
6320  }
6321  }
6322 
6323  std::vector<std::string> starting_paths;
6324  for (int i = 4; i < num_args; ++i)
6325  {
6326  starting_paths.emplace_back ();
6327  error_code = db_value_to_json_path (*args[i], F_JSON_SEARCH, starting_paths.back ());
6328  if (error_code != NO_ERROR)
6329  {
6330  ASSERT_ERROR ();
6331  return error_code;
6332  }
6333  }
6334 
6335  if (starting_paths.empty ())
6336  {
6337  starting_paths.push_back ("$");
6338  }
6339 
6340  std::vector<JSON_PATH> paths;
6341  error_code = db_json_search_func (*doc.get_immutable (), pattern, esc_char, paths, starting_paths, find_all);
6342  if (error_code != NO_ERROR)
6343  {
6344  return error_code;
6345  }
6346 
6347  if (paths.empty ())
6348  {
6349  return db_make_null (result);
6350  }
6351 
6352  JSON_DOC *result_json = nullptr;
6353  if (paths.size () == 1)
6354  {
6355  std::string path = paths[0].dump_json_path ();
6356  error_code = db_json_path_unquote_object_keys_external (path);
6357  if (error_code)
6358  {
6359  return error_code;
6360  }
6361 
6362  char *escaped;
6363  size_t escaped_size;
6364  error_code = db_string_escape_str (path.c_str (), path.size (), &escaped, &escaped_size);
6365  cubmem::private_unique_ptr<char> escaped_unique_ptr (escaped, NULL);
6366  if (error_code)
6367  {
6368  return error_code;
6369  }
6370  error_code = db_json_get_json_from_str (escaped, result_json, escaped_size);
6371  if (error_code != NO_ERROR)
6372  {
6373  ASSERT_ERROR ();
6374  return error_code;
6375  }
6376  return db_make_json (result, result_json, true);
6377  }
6378 
6379  JSON_DOC_STORE result_json_owner;
6380  JSON_DOC_STORE json_array_elem_owner;
6381  result_json_owner.create_mutable_reference ();
6382  for (std::size_t i = 0; i < paths.size (); ++i)
6383  {
6384  std::string path = paths[i].dump_json_path ();
6385 
6386  error_code = db_json_path_unquote_object_keys_external (path);
6387  if (error_code != NO_ERROR)
6388  {
6389  return error_code;
6390  }
6391 
6392  char *escaped;
6393  size_t escaped_size;
6394  error_code = db_string_escape_str (path.c_str (), path.size (), &escaped, &escaped_size);
6395  cubmem::private_unique_ptr<char> escaped_unique_ptr (escaped, NULL);
6396  if (error_code)
6397  {
6398  return error_code;
6399  }
6400 
6401  JSON_DOC *json_array_elem = nullptr;
6402  error_code = db_json_get_json_from_str (escaped, json_array_elem, escaped_size);
6403  json_array_elem_owner.set_mutable_reference (json_array_elem);
6404  if (error_code != NO_ERROR)
6405  {
6406  ASSERT_ERROR ();
6407  return error_code;
6408  }
6409 
6410  db_json_add_element_to_array (result_json_owner.get_mutable (), json_array_elem_owner.get_immutable ());
6411  }
6412 
6413  db_make_json_from_doc_store_and_release (*result, result_json_owner);
6414  return NO_ERROR;
6415 }
6416 /* *INDENT-ON* */
6417 
6418 int
6419 db_evaluate_json_get_all_paths (DB_VALUE * result, DB_VALUE * const *arg, int const num_args)
6420 {
6421  int error_code = NO_ERROR;
6422  JSON_DOC_STORE new_doc;
6423 
6424  db_make_null (result);
6425 
6426  if (num_args != 1)
6427  {
6428  assert (false);
6430  return ER_FAILED;
6431  }
6432 
6433  if (DB_IS_NULL (arg[0]))
6434  {
6435  return NO_ERROR;
6436  }
6437 
6438  error_code = db_value_to_json_doc (*arg[0], false, new_doc);
6439  if (error_code != NO_ERROR)
6440  {
6441  ASSERT_ERROR ();
6442  return error_code;
6443  }
6444 
6445  JSON_DOC *result_json = db_json_allocate_doc ();
6446  error_code = db_json_get_all_paths_func (*new_doc.get_immutable (), result_json);
6447 
6448  db_make_json (result, result_json, true);
6449 
6450  return NO_ERROR;
6451 }
6452 
6453 int
6454 db_least_or_greatest (DB_VALUE * arg1, DB_VALUE * arg2, DB_VALUE * result, bool least)
6455 {
6456  int error_code = NO_ERROR;
6457  bool can_compare = false;
6458  DB_VALUE_COMPARE_RESULT cmp_result = DB_UNK;
6459 
6460  cmp_result = tp_value_compare_with_error (arg1, arg2, 1, 0, &can_compare);
6461 
6462  if (cmp_result == DB_EQ)
6463  {
6464  pr_clone_value (arg1, result);
6465  }
6466  else if (cmp_result == DB_GT)
6467  {
6468  if (least)
6469  {
6470  pr_clone_value (arg2, result);
6471  }
6472  else
6473  {
6474  pr_clone_value (arg1, result);
6475  }
6476  }
6477  else if (cmp_result == DB_LT)
6478  {
6479  if (least)
6480  {
6481  pr_clone_value (arg1, result);
6482  }
6483  else
6484  {
6485  pr_clone_value (arg2, result);
6486  }
6487  }
6488  else if (cmp_result == DB_UNK && can_compare == false)
6489  {
6490  return ER_FAILED;
6491  }
6492  else
6493  {
6494  assert_release (DB_IS_NULL (arg1) || DB_IS_NULL (arg2));
6495  db_make_null (result);
6496  return NO_ERROR;
6497  }
6498 
6499  return error_code;
6500 }
6501 
6502 static int
6503 is_str_find_all (DB_VALUE * val, bool & find_all)
6504 {
6505  if (DB_IS_NULL (val))
6506  {
6509  }
6510 
6511  if (!DB_IS_STRING (val))
6512  {
6515  }
6516 
6517  // *INDENT-OFF*
6518  std::string find_all_str (db_get_string (val), db_get_string_size (val));
6519  std::transform (find_all_str.begin (), find_all_str.end (), find_all_str.begin (), [] (unsigned char c)
6520  {
6521  return std::tolower (c);
6522  });
6523  // *INDENT-ON*
6524 
6525  find_all = false;
6526  if (find_all_str == "all")
6527  {
6528  find_all = true;
6529  }
6530  if (!find_all && find_all_str != "one")
6531  {
6534  }
6535  return NO_ERROR;
6536 }
6537 
6538 static bool
6539 is_any_arg_null (DB_VALUE * const *args, int num_args)
6540 {
6541  for (int i = 0; i < num_args; ++i)
6542  {
6543  if (DB_IS_NULL (args[i]))
6544  {
6545  return true;
6546  }
6547  }
6548  return false;
6549 }
int julian_encode(int m, int d, int y)
Definition: db_date.c:113
int db_evaluate_json_extract(DB_VALUE *result, DB_VALUE *const *args, int num_args)
Definition: arithmetic.c:5511
DB_C_FLOAT db_get_float(const DB_VALUE *value)
int db_sin_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3769
struct db_domain_info::char_info char_info
static int db_mod_monetary(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1835
static int is_str_find_all(DB_VALUE *val, bool &find_all)
Definition: arithmetic.c:6503
TP_DOMAIN_STATUS tp_value_coerce(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain)
int db_evaluate_json_replace(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5721
#define ER_QPROC_OVERFLOW_POWER
Definition: error_code.h:1088
int db_make_json(DB_VALUE *value, JSON_DOC *json_document, bool need_clear)
#define NO_ERROR
Definition: error_code.h:46
int db_evaluate_json_merge_preserve(DB_VALUE *result, DB_VALUE *const *arg, const int num_args)
Definition: arithmetic.c:6163
static int db_width_bucket_calculate_numeric(double *result, const DB_VALUE *value1, const DB_VALUE *value2, const DB_VALUE *value3, const DB_VALUE *value4)
Definition: arithmetic.c:4426
unsigned int db_json_get_length(const JSON_DOC *document)
Definition: db_json.cpp:1046
#define ER_PROC_WIDTH_BUCKET_COUNT
Definition: error_code.h:1400
#define ASSERT_ERROR()
int db_json_merge_patch_func(const JSON_DOC *source, JSON_DOC *&dest)
Definition: db_json.cpp:2469
#define db_locate_numeric(value)
#define QSTR_IS_ANY_CHAR(s)
Definition: string_opfunc.h:46
void db_make_json_from_doc_store_and_release(DB_VALUE &value, JSON_DOC_STORE &doc_store)
Definition: db_json.cpp:3333
int db_json_replace_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1743
JSON_DOC * db_get_json_document(const DB_VALUE *value)
#define DB_GET_STRING_SAFE(v)
Definition: dbtype.h:122
int db_get_date_format(const DB_VALUE *format_str, TIMESTAMP_FORMAT *format)
int db_make_bigint(DB_VALUE *value, const DB_BIGINT num)
int db_get_int(const DB_VALUE *value)
DB_TIMESTAMP timestamp
Definition: dbtype_def.h:766
DB_TYPE
Definition: dbtype_def.h:670
DB_C_DOUBLE db_get_double(const DB_VALUE *value)
#define ER_FAILED
Definition: error_code.h:47
void numeric_coerce_num_to_double(DB_C_NUMERIC num, int scale, double *adouble)
int db_make_varchar(DB_VALUE *value, const int max_char_length, DB_CONST_C_CHAR str, const int char_str_byte_size, const int codeset, const int collation_id)
int db_datetime_decode(const DB_DATETIME *datetime, int *month, int *day, int *year, int *hour, int *minute, int *second, int *millisecond)
Definition: db_date.c:4574
int db_evaluate_json_object(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5569
int numeric_db_value_div(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
int db_evaluate_json_keys(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5855
DB_VALUE_COMPARE_RESULT tp_value_compare_with_error(const DB_VALUE *value1, const DB_VALUE *value2, int do_coercion, int total_order, bool *can_compare)
static int get_number_dbval_as_double(double *d, const DB_VALUE *value)
Definition: arithmetic.c:3686
int db_make_numeric(DB_VALUE *value, const DB_C_NUMERIC num, const int precision, const int scale)
int db_power_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:836
int db_asin_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3921
#define assert_release(e)
Definition: error_manager.h:96
#define ER_QSTR_INVALID_DATA_TYPE
Definition: error_code.h:746
DB_C_NUMERIC db_get_numeric(const DB_VALUE *value)
static double truncate_double(double num, double integer)
Definition: arithmetic.c:3140
bool db_json_is_valid(const char *json_str)
Definition: db_json.cpp:994
int numeric_db_value_compare(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
int db_evaluate_json_quote(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5294
int db_make_date(DB_VALUE *value, const int month, const int day, const int year)
DB_DATETIMETZ * db_get_datetimetz(const DB_VALUE *value)
int db_accumulate_json_arrayagg(const DB_VALUE *json_db_val, DB_VALUE *json_res)
Definition: arithmetic.c:5383
int db_exp_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:659
int db_json_extract_document_from_path(const JSON_DOC *document, const std::string &path, JSON_DOC_STORE &result, bool allow_wildcards)
Definition: db_json.cpp:1153
int db_round_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2315
void db_json_pretty_func(const JSON_DOC &doc, char *&result_str)
Definition: db_json.cpp:2881
int db_json_remove_func(JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1911
int db_value_to_json_path(const DB_VALUE &path_value, FUNC_TYPE fcode, std::string &path_str)
Definition: db_json.cpp:3163
int db_json_value_is_contained_in_doc(const JSON_DOC *doc, const JSON_DOC *value, bool &result)
Definition: db_json.cpp:2976
int numeric_db_value_coerce_to_num(DB_VALUE *src, DB_VALUE *dest, DB_DATA_STATUS *data_status)
int er_errid(void)
#define RETURN_ERROR_WITH_ARG(err, arg)
int numeric_db_value_sub(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
void crypt_crc32(const char *src, int src_len, int *dest)
Definition: crypt_opfunc.c:672
const char * name
#define ER_QSTR_INVALID_FORMAT
Definition: error_code.h:977
#define DB_VALUE_PRECISION(value)
Definition: dbtype.h:73
enum tp_domain_status TP_DOMAIN_STATUS
int db_sign_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:429
#define ER_QPROC_OVERFLOW_DIVISION
Definition: error_code.h:907
#define FINITE(x)
Definition: porting.h:388
#define MAX_DOMAIN_NAME_SIZE
int db_atan2_dbval(DB_VALUE *result, DB_VALUE *value, DB_VALUE *value2)
Definition: arithmetic.c:4006
int db_log_generic_dbval(DB_VALUE *result, DB_VALUE *value, long b)
Definition: arithmetic.c:4118
DB_DOMAIN_INFO domain
Definition: dbtype_def.h:1082
#define DB_VALUE_SCALE(value)
Definition: dbtype.h:74
int db_sleep(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4976
JSON_DOC * db_json_make_json_array()
Definition: db_json.cpp:2347
#define DB_FLOAT_DECIMAL_PRECISION
Definition: dbtype_def.h:589
int db_evaluate_json_valid(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5178
DB_TIMESTAMPTZ * db_get_timestamptz(const DB_VALUE *value)
int db_date_encode(DB_DATE *date, int month, int day, int year)
Definition: db_date.c:275
static void db_json_add_element_to_array(JSON_DOC *doc, const JSON_VALUE *value)
Definition: db_json.cpp:1538
TP_DOMAIN tp_Double_domain
int db_make_short(DB_VALUE *value, const DB_C_SHORT num)
int db_evaluate_json_depth(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5269
int db_make_string(DB_VALUE *value, DB_CONST_C_CHAR str)
DB_MONETARY * db_get_monetary(const DB_VALUE *value)
unsigned int DB_TIMESTAMP
Definition: dbtype_def.h:759
#define OR_CHECK_DOUBLE_OVERFLOW(i)
int db_json_get_all_paths_func(const JSON_DOC &doc, JSON_DOC *&result_json)
Definition: db_json.cpp:2852
TP_DOMAIN * tp_domain_resolve_default(DB_TYPE type)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
static int round_date(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2049
int db_evaluate_json_array(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5626
int db_cot_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3833
#define DB_MAX_NUMERIC_PRECISION
Definition: dbtype_def.h:522
int db_log_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2603
int db_bit_count_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4178
#define assert(x)
static int db_mod_double(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1498
int db_make_monetary(DB_VALUE *value, const DB_CURRENCY type, const double amount)
static int db_mod_int(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1056
JSON_DOC * db_json_make_json_object()
Definition: db_json.cpp:2339
#define LANG_COERCIBLE_CODESET
DB_TYPE db_value_domain_type(const DB_VALUE *value)
struct db_domain_info::numeric_info numeric_info
int prm_get_integer_value(PARAM_ID prm_id)
#define ER_QPROC_OVERFLOW_SUBTRACTION
Definition: error_code.h:905
#define ER_IT_DATA_OVERFLOW
Definition: error_code.h:505
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:50
int tp_domain_name(const TP_DOMAIN *domain, char *buffer, int maxlen)
int db_json_array_insert_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:2124
#define DB_INTEGER_PRECISION
Definition: dbtype_def.h:580
int db_json_get_json_from_str(const char *json_raw, JSON_DOC *&doc, size_t json_raw_length)
Definition: db_json.cpp:1608
DB_DATETIME datetime
Definition: dbtype_def.h:783
int db_tan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3801
#define DB_VALUE_DOMAIN_TYPE(value)
Definition: dbtype.h:70
#define DB_INT32_MAX
Definition: dbtype_def.h:633
#define TP_IS_NUMERIC_TYPE(typeid)
#define DB_IS_STRING(value)
Definition: dbtype.h:65
int db_acos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3872
#define OR_CHECK_INT_OVERFLOW(i)
#define ER_QPROC_OVERFLOW_ADDITION
Definition: error_code.h:538
static int get_number_dbval_as_long_double(long double *ld, const DB_VALUE *value)
Definition: arithmetic.c:4360
const char * db_json_get_type_as_str(const JSON_DOC *document)
Definition: db_json.cpp:1002
#define ER_JSON_DUPLICATE_KEY
Definition: error_code.h:1553
int db_evaluate_json_pretty(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5345
TP_DOMAIN_STATUS tp_value_cast(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain, bool implicit_coercion)
#define TP_DOMAIN_TYPE(dom)
int db_evaluate_json_unquote(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5305
int db_json_merge_preserve_func(const JSON_DOC *source, JSON_DOC *&dest)
Definition: db_json.cpp:2501
#define NULL
Definition: freelistheap.h:34
#define ER_INVALID_ONE_ALL_ARGUMENT
Definition: error_code.h:1558
unsigned char * DB_C_NUMERIC
Definition: dbtype_def.h:1214
struct pr_type * type
Definition: object_domain.h:76
int tz_datetimeltz_to_local(const DB_DATETIME *dt_ltz, DB_DATETIME *dt_local)
Definition: tz_support.c:1628
#define LANG_COERCIBLE_COLL
const char * pr_type_name(DB_TYPE id)
#define MILLISECONDS_OF_ONE_DAY
static double round_double(double num, double integer)
Definition: arithmetic.c:1987
#define err(fd,...)
Definition: porting.h:431
static int db_mod_float(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1354
int db_value_put_encoded_date(DB_VALUE *value, const DB_DATE *date)
Definition: db_macro.c:1383
void PRIM_SET_NULL(DB_VALUE *value)
int db_timestamp_decode_w_tz_id(const DB_TIMESTAMP *utime, const TZ_ID *tz_id, DB_DATE *date, DB_TIME *timeval)
Definition: db_date.c:902
TP_DOMAIN tp_Date_domain
int db_drandom_dbval(DB_VALUE *result)
Definition: arithmetic.c:3672
void numeric_coerce_num_to_dec_str(DB_C_NUMERIC num, char *dec_str)
#define db_private_alloc(thrd, size)
Definition: memory_alloc.h:227
static int db_mod_string(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1641
need_clear_type need_clear
Definition: dbtype_def.h:1084
static int db_mod_numeric(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1678
int db_trunc_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:3311
int pr_clear_value(DB_VALUE *value)
int db_timestamp_decode_ses(const DB_TIMESTAMP *utime, DB_DATE *date, DB_TIME *timeval)
Definition: db_date.c:764
#define ER_QPROC_FUNCTION_ARG_ERROR
Definition: error_code.h:1170
int db_json_search_func(const JSON_DOC &doc, const DB_VALUE *pattern, const DB_VALUE *esc_char, std::vector< JSON_PATH > &paths, const std::vector< std::string > &patterns, bool find_all)
Definition: db_json.cpp:1961
DB_BIGINT db_get_bigint(const DB_VALUE *value)
DB_JSON_TYPE db_json_get_type(const JSON_DOC *doc)
Definition: db_json.cpp:2519
#define DB_BIGINT_PRECISION
Definition: dbtype_def.h:577
TIMESTAMP_FORMAT
#define ER_JSON_OBJECT_NAME_IS_NULL
Definition: error_code.h:1550
static int move_n_days(int *monthp, int *dayp, int *yearp, const int interval)
Definition: arithmetic.c:2022
int64_t DB_BIGINT
Definition: dbtype_def.h:751
#define d1
int db_evaluate_json_array_insert(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:6032
int db_json_path_unquote_object_keys_external(std::string &sql_path)
Definition: db_json.cpp:2765
#define TP_IS_CHAR_TYPE(typeid)
static int db_mod_bigint(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1205
static void error(const char *msg)
Definition: gencat.c:331
int db_make_float(DB_VALUE *value, const DB_C_FLOAT num)
int db_json_array_append_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:2042
int db_evaluate_json_insert(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5653
int numeric_internal_double_to_num(double adouble, int dst_scale, DB_C_NUMERIC num, int *prec, int *scale)
int db_cos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3737
int tz_utc_datetimetz_to_local(const DB_DATETIME *dt_utc, const TZ_ID *tz_id, DB_DATETIME *dt_local)
Definition: tz_support.c:1571
int db_json_unquote(const JSON_DOC &doc, char *&result_str)
Definition: db_json.cpp:1090
#define RETURN_ERROR(err)
#define ARG_FILE_LINE
Definition: error_manager.h:44
int db_evaluate_json_contains_path(DB_VALUE *result, DB_VALUE *const *arg, const int num_args)
Definition: arithmetic.c:6100
#define DB_BIGINT_MAX
Definition: dbtype_def.h:640
int db_json_keys_func(const JSON_DOC &doc, JSON_DOC &result_json, const char *raw_path)
Definition: db_json.cpp:2919
int pr_clone_value(const DB_VALUE *src, DB_VALUE *dest)
int db_evaluate_json_contains(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5069
unsigned int DB_TIME
Definition: dbtype_def.h:754
unsigned int db_json_get_depth(const JSON_DOC *doc)
Definition: db_json.cpp:1079
int db_sqrt_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:731
unsigned int DB_DATE
Definition: dbtype_def.h:771
int db_string_escape_str(const char *src_str, size_t src_size, char **res_string, size_t *dest_size)
#define DB_BIGINT_MIN
Definition: dbtype_def.h:641
DB_DATA_STATUS
void numeric_coerce_dec_str_to_num(const char *dec_str, DB_C_NUMERIC result)
#define strlen(s1)
Definition: intl_support.c:43
DB_DATE * db_get_date(const DB_VALUE *value)
int db_evaluate_json_remove(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5912
int db_evaluate_json_get_all_paths(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:6419
#define ER_OBJ_INVALID_ARGUMENTS
Definition: error_code.h:275
unsigned int date
Definition: dbtype_def.h:776
int db_radians_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4086
bool prm_get_bool_value(PARAM_ID prm_id)
DB_TIMESTAMP * db_get_timestamp(const DB_VALUE *value)
int db_get_string_size(const DB_VALUE *value)
DB_C_SHORT db_get_short(const DB_VALUE *value)
#define ER_QPROC_OVERFLOW_EXP
Definition: error_code.h:1171
int db_evaluate_json_merge_patch(DB_VALUE *result, DB_VALUE *const *arg, const int num_args)
Definition: arithmetic.c:6215
void er_clear(void)
#define TP_IS_DATE_TYPE(typeid)
int db_crc32_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:5020
int db_least_or_greatest(DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *result, bool least)
Definition: arithmetic.c:6454
#define DB_VALUE_TYPE(value)
Definition: dbtype.h:72
int i
Definition: dynamic_load.c:954
int db_random_dbval(DB_VALUE *result)
Definition: arithmetic.c:3659
int db_make_null(DB_VALUE *value)
int tp_value_str_auto_cast_to_number(DB_VALUE *src, DB_VALUE *dest, DB_TYPE *val_type)
#define DB_IS_NULL(value)
Definition: dbtype.h:63
int db_make_double(DB_VALUE *value, const DB_C_DOUBLE num)
int db_mod_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1928
int db_floor_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:87
void db_date_decode(const DB_DATE *date, int *monthp, int *dayp, int *yearp)
Definition: db_date.c:338
int db_json_add_member_to_object(JSON_DOC *doc, const char *name, const char *value)
Definition: db_json.cpp:1408
DB_DATETIME * db_get_datetime(const DB_VALUE *value)
int db_json_contains_path(const JSON_DOC *document, const std::vector< std::string > &paths, bool find_all, bool &result)
Definition: db_json.cpp:1265
int db_string_quote(const DB_VALUE *str, DB_VALUE *res)
#define DB_NUMERIC_BUF_SIZE
Definition: dbtype_def.h:573
int db_make_int(DB_VALUE *value, const int num)
int db_json_insert_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1665
int msleep(const long msec)
Definition: porting.c:2657
void numeric_db_value_abs(DB_C_NUMERIC src_num, DB_C_NUMERIC dest_num)
static int truncate_date(DB_DATE *date, const DB_VALUE *format_str)
Definition: arithmetic.c:3203
int db_atan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3970
int db_evaluate_json_length(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5212
int db_evaluate_json_type_dbval(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5145
static bool is_any_arg_null(DB_VALUE *const *args, int num_args)
Definition: arithmetic.c:6539
int db_accumulate_json_objectagg(const DB_VALUE *json_key, const DB_VALUE *json_db_val, DB_VALUE *json_res)
Definition: arithmetic.c:5437
int db_typeof_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4302
int db_value_to_json_value(const DB_VALUE &db_val, JSON_DOC_STORE &json_doc)
Definition: db_json.cpp:3254
DB_TIME * db_get_time(const DB_VALUE *value)
#define ER_QPROC_INVALID_DATATYPE
Definition: error_code.h:534
int db_evaluate_json_set(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5788
int db_abs_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:565
void db_time_decode(DB_TIME *timeval, int *hourp, int *minutep, int *secondp)
Definition: db_date.c:432
static int db_mod_short(DB_VALUE *value, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:907
DB_VALUE_COMPARE_RESULT
Definition: dbtype_def.h:199
int db_evaluate_json_search(DB_VALUE *result, DB_VALUE *const *args, const int num_args)
Definition: arithmetic.c:6268
int db_value_to_json_doc(const DB_VALUE &db_val, bool force_copy, JSON_DOC_STORE &json_doc)
Definition: db_json.cpp:3183
int day_of_week(int jul_day)
Definition: db_date.c:176
TP_DOMAIN * tp_domain_new(DB_TYPE type)
int db_evaluate_json_array_append(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5964
int db_value_to_json_key(const DB_VALUE &db_val, std::string &key_str)
Definition: db_json.cpp:3309
int db_ceil_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:254
static DB_BIGINT truncate_bigint(DB_BIGINT num, DB_BIGINT integer)
Definition: arithmetic.c:3183
int db_degrees_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4054
const char ** p
Definition: dynamic_load.c:945
JSON_DOC * db_json_allocate_doc()
Definition: db_json.cpp:2332
void tp_domain_free(TP_DOMAIN *dom)
DB_CONST_C_CHAR db_get_string(const DB_VALUE *value)
unsigned int time
Definition: dbtype_def.h:777
int db_json_set_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1838
#define ER_QPROC_POWER_ERROR
Definition: error_code.h:1087
int db_width_bucket(DB_VALUE *result, const DB_VALUE *value1, const DB_VALUE *value2, const DB_VALUE *value3, const DB_VALUE *value4)
Definition: arithmetic.c:4598