CUBRID Engine  latest
type_checking.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  * type_checking.c - auxiliary functions for parse tree translation
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <float.h>
31 #include <math.h>
32 #include <limits.h>
33 #include <vector>
34 
35 #if defined(WINDOWS)
36 #include "porting.h"
37 #include "wintcp.h"
38 #else /* ! WINDOWS */
39 #include <sys/time.h>
40 #endif /* ! WINDOWS */
41 
42 #include "authenticate.h"
43 #include "error_manager.h"
44 #include "parser.h"
45 #include "parser_message.h"
46 #include "parse_type.hpp"
47 #include "set_object.h"
48 #include "arithmetic.h"
49 #include "string_opfunc.h"
50 #include "object_domain.h"
51 #include "object_primitive.h"
52 #include "object_representation.h"
53 #include "semantic_check.h"
54 #include "xasl_generation.h"
55 #include "language_support.h"
56 #include "schema_manager.h"
57 #include "system_parameter.h"
58 #include "network_interface_cl.h"
59 #include "object_template.h"
60 #include "db.h"
61 #include "tz_support.h"
62 #include "func_type.hpp"
63 
64 #include "dbtype.h"
65 
66 #define SET_EXPECTED_DOMAIN(node, dom) \
67  do \
68  { \
69  (node)->expected_domain = (dom); \
70  if ((node)->or_next) \
71  { \
72  PT_NODE *_or_next = (node)->or_next; \
73  while (_or_next) \
74  { \
75  if (_or_next->type_enum == PT_TYPE_MAYBE && _or_next->expected_domain == NULL) \
76  { \
77  _or_next->expected_domain = (dom); \
78  } \
79  _or_next = _or_next->or_next; \
80  } \
81  } \
82  } \
83  while (0)
84 
85 #if defined(ENABLE_UNUSED_FUNCTION)
86 typedef struct generic_function_record
87 {
88  const char *function_name;
89  const char *return_type;
90  int func_ptr_offset;
91 } GENERIC_FUNCTION_RECORD;
92 
93 static int pt_Generic_functions_sorted = 0;
94 
95 static GENERIC_FUNCTION_RECORD pt_Generic_functions[] = {
96  /* Make sure that this table is in synch with the generic function table. Don't remove the first dummy position.
97  * It's a place holder. */
98  {"AAA_DUMMY", "integer", 0}
99 };
100 #endif /* ENABLE_UNUSED_FUNCTION */
101 
102 #define PT_ARE_COMPARABLE_CHAR_TYPE(typ1, typ2) \
103  ((PT_IS_SIMPLE_CHAR_STRING_TYPE (typ1) && PT_IS_SIMPLE_CHAR_STRING_TYPE (typ2)) \
104  || (PT_IS_NATIONAL_CHAR_STRING_TYPE (typ1) && PT_IS_NATIONAL_CHAR_STRING_TYPE (typ2)))
105 
106 #define PT_ARE_COMPARABLE_NUMERIC_TYPE(typ1, typ2) \
107  ((PT_IS_NUMERIC_TYPE (typ1) && PT_IS_NUMERIC_TYPE (typ2)) \
108  || (PT_IS_NUMERIC_TYPE (typ1) && typ2 == PT_TYPE_MAYBE) \
109  || (typ1 == PT_TYPE_MAYBE && PT_IS_NUMERIC_TYPE (typ2)))
110 
111 /* Two types are comparable if they are NUMBER types or same CHAR type */
112 #define PT_ARE_COMPARABLE(typ1, typ2) \
113  ((typ1 == typ2) || PT_ARE_COMPARABLE_CHAR_TYPE (typ1, typ2) || PT_ARE_COMPARABLE_NUMERIC_TYPE (typ1, typ2))
114 
115 #define PT_IS_RECURSIVE_EXPRESSION(node) \
116  ((node)->node_type == PT_EXPR && (PT_IS_LEFT_RECURSIVE_EXPRESSION (node) || PT_IS_RIGHT_RECURSIVE_EXPRESSION (node)))
117 
118 #define PT_IS_LEFT_RECURSIVE_EXPRESSION(node) \
119  ((node)->info.expr.op == PT_GREATEST || (node)->info.expr.op == PT_LEAST || (node)->info.expr.op == PT_COALESCE)
120 
121 #define PT_IS_RIGHT_RECURSIVE_EXPRESSION(node) \
122  ((node)->info.expr.op == PT_CASE || (node)->info.expr.op == PT_DECODE)
123 
124 #define PT_IS_CAST_MAYBE(node) \
125  ((node)->node_type == PT_EXPR && (node)->info.expr.op == PT_CAST \
126  && (node)->info.expr.arg1 != NULL && (node)->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
127 
128 #define PT_NODE_IS_SESSION_VARIABLE(node) \
129  ((((node) != NULL) && \
130  ((node)->node_type == PT_EXPR) && \
131  (((node)->info.expr.op == PT_EVALUATE_VARIABLE) || \
132  (((node)->info.expr.op == PT_CAST) && \
133  ((node)->info.expr.arg1 != NULL) && \
134  ((node)->info.expr.arg1->node_type == PT_EXPR) && \
135  ((node)->info.expr.arg1->info.expr.op == PT_EVALUATE_VARIABLE)) \
136  )) ? true : false )
137 
139 {
144 
152  {PT_GT_INF, PT_EQ, PT_BETWEEN_INF_LE},
155  {PT_EQ, PT_LT_INF, PT_BETWEEN_GE_INF},
157 };
158 
159 #define COMPARE_BETWEEN_OPERATOR_COUNT \
160  sizeof(pt_Compare_between_operator_table) / \
161  sizeof(COMPARE_BETWEEN_OPERATOR)
162 
163 #define PT_COLL_WRAP_TYPE_FOR_MAYBE(type) \
164  ((PT_IS_CHAR_STRING_TYPE (type)) ? (type) : PT_TYPE_VARCHAR)
165 
166 /* maximum number of overloads for an expression */
167 #define MAX_OVERLOADS 16
168 
169 /* SQL expression signature */
170 typedef struct expression_signature
171 {
177 
178 /* SQL expression definition */
179 typedef struct expression_definition
180 {
185 
186 /* collation for a parse tree node */
187 typedef enum collation_result
188 {
193 
194 static PT_TYPE_ENUM pt_infer_common_type (const PT_OP_TYPE op, PT_TYPE_ENUM * arg1, PT_TYPE_ENUM * arg2,
195  PT_TYPE_ENUM * arg3, const TP_DOMAIN * expected_domain);
201  const PT_TYPE_ENUM arg_type, PT_NODE * data_type);
202 static PT_NODE *pt_coerce_expr_arguments (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * arg1, PT_NODE * arg2,
203  PT_NODE * arg3, EXPRESSION_SIGNATURE sig);
205  PT_NODE * arg3, EXPRESSION_SIGNATURE sig);
206 static bool pt_is_range_expression (const PT_OP_TYPE op);
207 static bool pt_are_unmatchable_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type);
208 static PT_TYPE_ENUM pt_get_equivalent_type_with_op (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type,
209  PT_OP_TYPE op);
210 static PT_NODE *pt_evaluate_new_data_type (const PT_TYPE_ENUM old_type, const PT_TYPE_ENUM new_type,
211  PT_NODE * data_type);
212 static PT_TYPE_ENUM pt_get_common_collection_type (const PT_NODE * set, bool * is_multitype);
213 static bool pt_is_collection_of_type (const PT_NODE * collection, const PT_TYPE_ENUM collection_type,
214  const PT_TYPE_ENUM element_type);
215 static bool pt_is_symmetric_type (const PT_TYPE_ENUM type_enum);
216 static PT_NODE *pt_propagate_types (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * otype1, PT_NODE * otype2);
217 static int pt_union_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
218  DB_VALUE * result, PT_NODE * o2);
219 static int pt_difference_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
220  DB_VALUE * result, PT_NODE * o2);
221 static int pt_product_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
222  DB_VALUE * result, PT_NODE * o2);
225 static PT_NODE *pt_eval_type_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
226 static PT_NODE *pt_eval_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
227 static PT_NODE *pt_fold_constants_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
228 static PT_NODE *pt_fold_constants_post (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
230 static bool pt_is_able_to_determine_return_type (const PT_OP_TYPE op);
234 static int pt_upd_domain_info (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_OP_TYPE op,
235  PT_TYPE_ENUM common_type, PT_NODE * node);
239  PT_TYPE_ENUM * result_type);
240 static int pt_coerce_3args (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_NODE * arg3);
245 static PT_NODE *pt_fold_const_expr (PARSER_CONTEXT * parser, PT_NODE * expr, void *arg);
247 static const char *pt_class_name (const PT_NODE * type);
250  PT_TYPE_ENUM desired_type);
251 static int pt_coerce_value_internal (PARSER_CONTEXT * parser, PT_NODE * src, PT_NODE * dest,
252  PT_TYPE_ENUM desired_type, PT_NODE * data_type, bool check_string_precision,
253  bool implicit_coercion);
254 static int pt_coerce_value_explicit (PARSER_CONTEXT * parser, PT_NODE * src, PT_NODE * dest, PT_TYPE_ENUM desired_type,
255  PT_NODE * data_type);
256 #if defined(ENABLE_UNUSED_FUNCTION)
257 static int generic_func_casecmp (const void *a, const void *b);
258 static void init_generic_funcs (void);
259 #endif /* ENABLE_UNUSED_FUNCTION */
261  PT_TYPE_ENUM lhs_type, DB_VALUE * rhs_val, PT_TYPE_ENUM rhs_type);
263  PT_TYPE_ENUM arg1_type, PT_TYPE_ENUM arg2_type, PT_NODE * arg1,
264  PT_NODE * arg2);
265 static int pt_character_length_for_node (PT_NODE * node, const PT_TYPE_ENUM coerce_type);
268 static bool pt_check_const_fold_op_w_args (PT_OP_TYPE op, DB_VALUE * arg1, DB_VALUE * arg2, DB_VALUE * arg3,
269  TP_DOMAIN * domain);
270 static bool pt_is_range_or_comp (PT_OP_TYPE op);
271 static bool pt_is_op_w_collation (const PT_OP_TYPE op);
273  PT_COLL_INFER * coll_infer);
275  PT_COLL_INFER * coll_infer, const bool is_inner_collection,
276  bool * is_first_element);
277 static PT_NODE *pt_coerce_node_collection_of_collection (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id,
278  const INTL_CODESET codeset, bool force_mode,
279  bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe,
280  PT_TYPE_ENUM wrap_type_collection);
281 static int pt_check_expr_collation (PARSER_CONTEXT * parser, PT_NODE ** node);
285 static bool pt_is_enumeration_special_comparison (PT_NODE * arg1, PT_OP_TYPE op, PT_NODE * arg2);
289 static PT_TYPE_ENUM pt_wrap_type_for_collation (const PT_NODE * arg1, const PT_NODE * arg2, const PT_NODE * arg3,
290  PT_TYPE_ENUM * wrap_type_collection);
291 static void pt_fix_arguments_collation_flag (PT_NODE * expr);
294 static void pt_update_host_var_data_type (PARSER_CONTEXT * parser, PT_NODE * hv_node);
295 static bool pt_cast_needs_wrap_for_collation (PT_NODE * node, const INTL_CODESET codeset);
296 
297 /*
298  * pt_get_expression_definition () - get the expression definition for the
299  * expression op.
300  * return: true if the expression has a definition, false otherwise
301  * op(in) : the expression operator
302  * def(in/out): the expression definition
303  */
304 static bool
306 {
308  int num;
309 
310  assert (def != NULL);
311 
312  def->op = op;
313 
316 
319 
322 
325 
326  switch (op)
327  {
328  case PT_AND:
329  case PT_OR:
330  case PT_XOR:
331  num = 0;
332 
333  /* one overload */
334 
335  /* arg1 */
338 
339  /* arg2 */
342 
343  /* return type */
346 
347  def->overloads[num++] = sig;
348 
349  def->overloads_count = num;
350  break;
351 
352  case PT_NOT:
353  num = 0;
354 
355  /* one overload */
356 
357  /* arg1 */
360 
361  /* return type */
364 
365  def->overloads[num++] = sig;
366 
367  def->overloads_count = num;
368  break;
369 
370  case PT_ACOS:
371  case PT_ASIN:
372  case PT_ATAN:
373  case PT_COS:
374  case PT_COT:
375  case PT_DEGREES:
376  case PT_EXP:
377  case PT_LN:
378  case PT_LOG10:
379  case PT_LOG2:
380  case PT_SQRT:
381  case PT_RADIANS:
382  case PT_SIN:
383  case PT_TAN:
384  num = 0;
385 
386  /* one overload */
387 
388  /* arg1 */
391 
392  /* return type */
395 
396  def->overloads[num++] = sig;
397 
398  def->overloads_count = num;
399  break;
400 
401  case PT_ABS:
402  num = 0;
403 
404  /* one overload */
405 
406  /* arg1 */
409 
410  /* return type */
413 
414  def->overloads[num++] = sig;
415 
416  def->overloads_count = num;
417  break;
418 
419  case PT_ATAN2:
420  case PT_LOG:
421  case PT_POWER:
422  num = 0;
423 
424  /* one overload */
425 
426  /* arg1 */
429 
430  /* arg2 */
433 
434  /* return type */
437 
438  def->overloads[num++] = sig;
439 
440  def->overloads_count = num;
441  break;
442 
443  case PT_RAND:
444  case PT_RANDOM:
445  num = 0;
446 
447  /* one overload */
448 
449  /* arg1 */
452 
453  /* return type */
456 
457  def->overloads[num++] = sig;
458 
459  /* arg1 */
462 
463  /* return type */
466 
467  def->overloads[num++] = sig;
468 
469  def->overloads_count = num;
470  break;
471 
472  case PT_DRAND:
473  case PT_DRANDOM:
474  num = 0;
475 
476  /* one overload */
477 
478  /* arg1 */
481 
482  /* return type */
485 
486  def->overloads[num++] = sig;
487 
488  /* arg1 */
491 
492  /* return type */
495 
496  def->overloads[num++] = sig;
497 
498  def->overloads_count = num;
499  break;
500 
501  case PT_BIT_AND:
502  case PT_BIT_XOR:
503  case PT_BIT_OR:
504  case PT_BITSHIFT_LEFT:
505  case PT_BITSHIFT_RIGHT:
506  num = 0;
507 
508  /* one overload */
509 
510  /* arg1 */
513 
514  /* arg2 */
517 
518  /* return type */
521 
522  def->overloads[num++] = sig;
523 
524  def->overloads_count = num;
525  break;
526 
527  case PT_BIT_LENGTH:
528  case PT_OCTET_LENGTH:
529  num = 0;
530 
531  /* two overloads */
532 
533  /* arg1 */
536  /* return type */
537 
540  def->overloads[num++] = sig;
541 
542  /* arg1 */
545 
546  /* return type */
549  def->overloads[num++] = sig;
550 
551  def->overloads_count = num;
552  break;
553 
554  case PT_BIT_COUNT:
555  num = 0;
556 
557  /* one overload */
558 
559  /* arg1 */
562 
563  /* return type */
566 
567  def->overloads[num++] = sig;
568 
569  def->overloads_count = num;
570  break;
571 
572  case PT_BIT_NOT:
573  num = 0;
574 
575  /* one overload */
576 
577  /* arg1 */
580 
581  /* return type */
584 
585  def->overloads[num++] = sig;
586 
587  def->overloads_count = num;
588  break;
589 
590  case PT_BETWEEN:
591  case PT_NOT_BETWEEN:
592  num = 0;
593 
594  /* one overload */
595 
596  /* arg1 */
599 
600  /* arg2 */
603 
604  /* arg3 */
607 
608  /* return type */
611 
612  def->overloads[num++] = sig;
613 
614  def->overloads_count = num;
615  break;
616 
617  case PT_LIKE:
618  case PT_NOT_LIKE:
619  num = 0;
620 
621  /* four overloads */
622 
623  /* BOOL PT_LIKE([VAR]CHAR, [VAR]CHAR); */
626 
629 
632 
633  def->overloads[num++] = sig;
634 
635  /* BOOL PT_LIKE([VAR]NCHAR, [VAR]NCHAR); */
638 
641 
644 
645  def->overloads[num++] = sig;
646 
647  /* BOOL PT_LIKE([VAR]CHAR, [VAR]CHAR, [VAR]CHAR); */
650 
653 
656 
659 
660  def->overloads[num++] = sig;
661 
662  /* BOOL PT_LIKE([VAR]NCHAR, [VAR]NCHAR, [VAR]NCHAR); */
665 
668 
671 
674 
675  def->overloads[num++] = sig;
676 
677  def->overloads_count = num;
678  break;
679 
680  case PT_RLIKE:
681  case PT_NOT_RLIKE:
682  case PT_RLIKE_BINARY:
683  case PT_NOT_RLIKE_BINARY:
684  num = 0;
685 
686  /* two overloads */
687 
688  /* BOOL PT_RLIKE([VAR]CHAR, [VAR]CHAR, INT); */
691 
694 
697 
700 
701  def->overloads[num++] = sig;
702 
703  /* BOOL PT_RLIKE([VAR]NCHAR, [VAR]NCHAR, INT); */
706 
709 
712 
715 
716  def->overloads[num++] = sig;
717 
718  def->overloads_count = num;
719  break;
720 
721  case PT_CEIL:
722  case PT_FLOOR:
723  num = 0;
724 
725  /* one overload */
726 
727  /* arg1 */
730 
731  /* return type */
734 
735  def->overloads[num++] = sig;
736 
737  def->overloads_count = num;
738  break;
739 
740  case PT_CHR:
741  num = 0;
742 
743  /* one overload */
744 
745  /* arg1 */
748 
749  /* arg2 */
752 
753  /* return type */
756 
757  def->overloads[num++] = sig;
758 
759  def->overloads_count = num;
760  break;
761 
762  case PT_CHAR_LENGTH:
763  num = 0;
764 
765  /* one overload */
766 
767  /* arg1 */
770 
771  /* return type */
774 
775  def->overloads[num++] = sig;
776 
777  def->overloads_count = num;
778  break;
779 
780  case PT_REPEAT:
781  num = 0;
782 
783  /* one overload */
784 
785  /* arg1 */
788 
789  /* arg2 */
792 
793  /* return type */
796 
797  def->overloads[num++] = sig;
798 
799  def->overloads_count = num;
800  break;
801 
802  case PT_ADD_MONTHS:
803  num = 0;
804 
805  /* one overload */
806  /* arg1 */
809 
810  /* arg2 */
813 
814  /* return type */
817 
818  def->overloads[num++] = sig;
819 
820  def->overloads_count = num;
821  break;
822 
823  case PT_FROMDAYS:
824  num = 0;
825 
826  /* two overload */
827 
832  def->overloads[num++] = sig;
833 
838  def->overloads[num++] = sig;
839 
840  def->overloads_count = num;
841  break;
842 
843  case PT_LOWER:
844  case PT_UPPER:
845  num = 0;
846 
847  /* one overload */
848 
849  /* arg1 */
852 
853  /* return type */
856 
857  def->overloads[num++] = sig;
858 
859  def->overloads_count = num;
860  break;
861 
862  case PT_HEX:
863  num = 0;
864 
865  /* three overloads */
866 
867  /* HEX (STRING) */
868  /* arg1 */
871  /* return type */
874  def->overloads[num++] = sig;
875 
876  /* HEX (NUMBER) */
877  /* arg1 */
880  /* return type */
883  def->overloads[num++] = sig;
884 
885  /* HEX (BIT) */
886  /* arg1 */
889  /* return type */
892  def->overloads[num++] = sig;
893 
894  def->overloads_count = num;
895  break;
896 
897  case PT_ASCII:
898  num = 0;
899 
900  /* two overloads */
901 
902  /* ASCII (STRING) */
903  /* arg1 */
906  /* return type */
909  def->overloads[num++] = sig;
910 
911  /* ASCII (BIT) */
912  /* arg1 */
915  /* return type */
918  def->overloads[num++] = sig;
919 
920  def->overloads_count = num;
921  break;
922 
923  case PT_CONV:
924  num = 0;
925 
926  /* three overloads */
927 
928  /* CONV(NUMBER, SMALLINT, SMALLINT) */
929  /* arg1 */
932  /* arg2 */
935  /* arg3 */
938  /* return type */
941  def->overloads[num++] = sig;
942 
943  /* CONV(VARCHAR, SMALLINT, SMALLINT) */
944  /* arg1 */
947  /* arg2 */
950  /* arg3 */
953  /* return type */
956  def->overloads[num++] = sig;
957 
958  /* CONV(BIT, SMALLINT, SMALLINT) */
959  /* arg1 */
962  /* arg2 */
965  /* arg3 */
968  /* return type */
971  def->overloads[num++] = sig;
972 
973  def->overloads_count = num;
974  break;
975 
976  case PT_DATEF:
977  case PT_REVERSE:
978  num = 0;
979 
980  /* one overload */
981 
982  /* arg1 */
985 
986  /* return type */
989 
990  def->overloads[num++] = sig;
991 
992  def->overloads_count = num;
993  break;
994  case PT_DISK_SIZE:
995  num = 0;
996 
997  /* one overload */
998 
999  /* arg1 */
1002 
1003  /* return type */
1006 
1007  def->overloads[num++] = sig;
1008 
1009  def->overloads_count = num;
1010  break;
1011  case PT_LIKE_LOWER_BOUND:
1012  case PT_LIKE_UPPER_BOUND:
1013  num = 0;
1014 
1015  /* two overloads */
1016 
1017  /* arg1 */
1020  /* return type */
1023  def->overloads[num++] = sig;
1024 
1025  /* arg1 */
1028  /* arg2 */
1031  /* return type */
1034  def->overloads[num++] = sig;
1035 
1036  def->overloads_count = num;
1037  break;
1038 
1039  case PT_BIN:
1040  num = 0;
1041 
1042  /* one overload */
1043 
1044  /* arg1 */
1047  /* return type */
1050  def->overloads[num++] = sig;
1051 
1052  def->overloads_count = num;
1053  break;
1054 
1055  case PT_ADDTIME:
1056  num = 0;
1057 
1058  /* 12 overloads */
1059 
1060  /* arg1 */
1063  /* arg2 */
1066  /* return type */
1069  def->overloads[num++] = sig;
1070 
1073  /* arg2 */
1076  /* return type */
1079  def->overloads[num++] = sig;
1080 
1083  /* arg2 */
1086  /* return type */
1089  def->overloads[num++] = sig;
1090 
1091  /* arg1 */
1094  /* arg2 */
1097  /* return type */
1100  def->overloads[num++] = sig;
1101 
1102  /* arg1 */
1105  /* arg2 */
1108  /* return type */
1111  def->overloads[num++] = sig;
1112 
1113  /* arg1 */
1116  /* arg2 */
1119  /* return type */
1122  def->overloads[num++] = sig;
1123 
1124  /* arg1 */
1127  /* arg2 */
1130  /* return type */
1133  def->overloads[num++] = sig;
1134 
1135  /* arg1 */
1138  /* arg2 */
1141  /* return type */
1144  def->overloads[num++] = sig;
1145 
1146  /* arg1 */
1149  /* arg2 */
1152  /* return type */
1155  def->overloads[num++] = sig;
1156 
1157  /* arg1 */
1160  /* arg2 */
1163  /* return type */
1166  def->overloads[num++] = sig;
1167 
1168  /* arg1 */
1171  /* arg2 */
1174  /* return type */
1177  def->overloads[num++] = sig;
1178 
1179  /* arg1 */
1182  /* arg2 */
1185  /* return type */
1188  def->overloads[num++] = sig;
1189 
1190  def->overloads_count = num;
1191  break;
1192 
1193  case PT_TRIM:
1194  case PT_LTRIM:
1195  case PT_RTRIM:
1196  num = 0;
1197 
1198  /* two overloads */
1199 
1200  /* arg1 */
1203  /* return type */
1206  def->overloads[num++] = sig;
1207 
1208  /* arg1 */
1211  /* arg2 */
1214  /* return type */
1217  def->overloads[num++] = sig;
1218 
1219  def->overloads_count = num;
1220  break;
1221 
1222  case PT_MAKEDATE:
1223  num = 0;
1224 
1225  /* arg1 */
1228  /* arg2 */
1233  def->overloads[num++] = sig;
1234 
1235  /* arg1 */
1238  /* arg2 */
1241  /* return type */
1244  def->overloads[num++] = sig;
1245 
1246  def->overloads_count = num;
1247  break;
1248 
1249  case PT_MAKETIME:
1250  num = 0;
1251 
1252  /* two overloads */
1253 
1254  /* arg1 */
1257  /* arg2 */
1260  /* arg3 */
1263  /* return type */
1266  def->overloads[num++] = sig;
1267 
1268  /* arg1 */
1271  /* arg2 */
1274  /* arg3 */
1277  /* return type */
1280  def->overloads[num++] = sig;
1281 
1282  def->overloads_count = num;
1283  break;
1284 
1285  case PT_SECTOTIME:
1286  num = 0;
1287 
1288  /* two overloads */
1289 
1290  /* arg1 */
1293  /* return type */
1296  def->overloads[num++] = sig;
1297 
1298  /* arg1 */
1301  /* return type */
1304  def->overloads[num++] = sig;
1305 
1306  def->overloads_count = num;
1307  break;
1308 
1309  case PT_YEARF:
1310  case PT_DAYF:
1311  case PT_MONTHF:
1312  case PT_DAYOFMONTH:
1313  case PT_DAYOFWEEK:
1314  case PT_DAYOFYEAR:
1315  case PT_QUARTERF:
1316  case PT_TODAYS:
1317  case PT_WEEKDAY:
1318  num = 0;
1319 
1320  /* two overloads */
1321 
1322  /* arg1 */
1325  /* return type */
1328  def->overloads[num++] = sig;
1329 
1330  /* arg1 */
1333  /* return type */
1336  def->overloads[num++] = sig;
1337 
1338  def->overloads_count = num;
1339  break;
1340 
1341  case PT_LAST_DAY:
1342  num = 0;
1343 
1344  /* one overload */
1345 
1346  /* arg1 */
1349 
1350  /* return type */
1353 
1354  def->overloads[num++] = sig;
1355 
1356  def->overloads_count = num;
1357  break;
1358 
1359  case PT_CONCAT:
1361  num = 0;
1362 
1363  /* two overloads */
1364 
1365  /* arg1 */
1368  /* arg2 */
1371  /* return type */
1374  def->overloads[num++] = sig;
1375 
1376  /* arg1 */
1379  /* arg2 */
1382  /* return type */
1385  def->overloads[num++] = sig;
1386 
1387  def->overloads_count = num;
1388  break;
1389 
1390  case PT_CONCAT_WS:
1391  num = 0;
1392 
1393  /* two overloads */
1394 
1395  /* arg1 */
1398  /* arg2 */
1401  /* arg2 */
1404  /* return type */
1407  def->overloads[num++] = sig;
1408 
1409  /* arg1 */
1412  /* arg2 */
1415  /* arg2 */
1418  /* return type */
1421  def->overloads[num++] = sig;
1422 
1423  def->overloads_count = num;
1424  break;
1425 
1426  case PT_DATABASE:
1427  case PT_SCHEMA:
1428  case PT_VERSION:
1429  case PT_CURRENT_USER:
1430  case PT_LIST_DBS:
1431  case PT_SYS_GUID:
1432  case PT_USER:
1433  num = 0;
1434 
1435  /* one overload */
1436 
1437  /* no arguments, just a return type */
1440 
1441  def->overloads[num++] = sig;
1442 
1443  def->overloads_count = num;
1444  break;
1445 
1447  num = 0;
1448 
1449  /* one overload */
1450 
1451  /* no arguments, just a return type */
1454 
1455  def->overloads[num++] = sig;
1456 
1457  def->overloads_count = num;
1458  break;
1459 
1460  case PT_CURRENT_VALUE:
1461  num = 0;
1462 
1463  /* one overload */
1464 
1467 
1470 
1471  def->overloads[num++] = sig;
1472 
1473  def->overloads_count = num;
1474  break;
1475 
1476  case PT_NEXT_VALUE:
1477  num = 0;
1478 
1479  /* one overload */
1480 
1481  /* arg1 */
1484 
1485  /* arg2 */
1488 
1491 
1492  def->overloads[num++] = sig;
1493 
1494  def->overloads_count = num;
1495  break;
1496 
1497  case PT_DATE_FORMAT:
1498  num = 0;
1499 
1500  /* two overloads */
1501 
1502  /* arg1 */
1505  /* arg2 */
1508  /* arg3 */
1511  /* return type */
1514  def->overloads[num++] = sig;
1515 
1516  /* arg1 */
1519  /* arg2 */
1522  /* arg3 */
1525  /* return type */
1528  def->overloads[num++] = sig;
1529 
1530  def->overloads_count = num;
1531  break;
1532 
1533  case PT_DIV:
1534  case PT_MOD:
1535  num = 0;
1536 
1537  /* one overload */
1538 
1539  /* arg1 */
1542  /* arg2 */
1545 
1548  def->overloads[num++] = sig;
1549 
1550  def->overloads_count = num;
1551  break;
1552 
1553  case PT_DIVIDE:
1554  case PT_MODULUS:
1555  num = 0;
1556 
1557  /* one overload */
1558 
1559  /* arg1 */
1562  /* arg2 */
1565 
1568  def->overloads[num++] = sig;
1569 
1570  def->overloads_count = num;
1571  break;
1572 
1573  case PT_TIMES:
1574  num = 0;
1575 
1576  /* two overloads */
1577 
1578  /* arg1 */
1581  /* arg2 */
1584 
1587  def->overloads[num++] = sig;
1588 
1589  /* arg1 */
1592  /* arg2 */
1595 
1598  def->overloads[num++] = sig;
1599 
1600  def->overloads_count = num;
1601  break;
1602 
1603  case PT_PLUS:
1604  num = 0;
1605 
1606  /* number + number */
1613  def->overloads[num++] = sig;
1614 
1616  {
1617  /* char + char */
1624  def->overloads[num++] = sig;
1625 
1626  /* nchar + nchar */
1633  def->overloads[num++] = sig;
1634 
1635  /* bit + bit */
1642  def->overloads[num++] = sig;
1643  }
1644 
1645  /* collection + collection */
1652  def->overloads[num++] = sig;
1653 
1654  def->overloads_count = num;
1655 
1656  break;
1657 
1658  case PT_MINUS:
1659  num = 0;
1660 
1661  /* 4 overloads */
1662 
1669  def->overloads[num++] = sig;
1670 
1677  def->overloads[num++] = sig;
1678 
1685  def->overloads[num++] = sig;
1686 
1693  def->overloads[num++] = sig;
1694 
1695  def->overloads_count = num;
1696  break;
1697 
1698  case PT_HOURF:
1699  case PT_MINUTEF:
1700  case PT_SECONDF:
1701  case PT_TIMETOSEC:
1702  num = 0;
1703 
1704  /* 2 overloads */
1705 
1710  def->overloads[num++] = sig;
1711 
1716  def->overloads[num++] = sig;
1717 
1718  def->overloads_count = num;
1719  break;
1720 
1721  case PT_INSTR:
1722  num = 0;
1723 
1724  /* two overloads */
1725 
1726  /* arg1 */
1729  /* arg2 */
1732  /* arg3 */
1735  /* return type */
1738  def->overloads[num++] = sig;
1739 
1740  /* arg1 */
1743  /* arg2 */
1746  /* arg3 */
1749  /* return type */
1752  def->overloads[num++] = sig;
1753 
1754  def->overloads_count = num;
1755  break;
1756 
1757  case PT_LEFT:
1758  case PT_RIGHT:
1759  num = 0;
1760 
1761  /* two overloads */
1762 
1763  /* arg1 */
1766  /* arg2 */
1769 
1770  /* return type */
1773  def->overloads[num++] = sig;
1774 
1775  /* arg1 */
1778  /* arg2 */
1781 
1782  /* return type */
1785  def->overloads[num++] = sig;
1786 
1787  def->overloads_count = num;
1788  break;
1789 
1790  case PT_LOCATE:
1791  num = 0;
1792 
1793  /* four overloads */
1794 
1795  /* arg1 */
1798  /* arg2 */
1801  /* arg3 */
1804 
1805  /* return type */
1808  def->overloads[num++] = sig;
1809 
1810  /* arg1 */
1813  /* arg2 */
1816  /* arg3 */
1819 
1820  /* return type */
1823  def->overloads[num++] = sig;
1824 
1825  /* arg1 */
1828  /* arg2 */
1831  /* arg3 */
1834 
1835  /* return type */
1838  def->overloads[num++] = sig;
1839 
1840  /* arg1 */
1843  /* arg2 */
1846  /* arg3 */
1849 
1850  /* return type */
1853  def->overloads[num++] = sig;
1854 
1855  def->overloads_count = num;
1856  break;
1857 
1858  case PT_POSITION:
1859  case PT_STRCMP:
1860  case PT_FINDINSET:
1861  num = 0;
1862 
1863  /* two overloads */
1864 
1865  /* arg1 */
1868  /* arg2 */
1871 
1872  /* return type */
1875  def->overloads[num++] = sig;
1876 
1877  /* arg1 */
1880  /* arg2 */
1883 
1884  /* return type */
1887  def->overloads[num++] = sig;
1888 
1889  def->overloads_count = num;
1890  break;
1891 
1892  case PT_SUBSTRING_INDEX:
1893  num = 0;
1894 
1895  /* two overloads */
1896 
1897  /* arg1 */
1900  /* arg2 */
1903  /* arg3 */
1906 
1907  /* return type */
1910  def->overloads[num++] = sig;
1911 
1912  /* arg1 */
1915  /* arg2 */
1918  /* arg3 */
1921 
1922  /* return type */
1925  def->overloads[num++] = sig;
1926 
1927  def->overloads_count = num;
1928  break;
1929 
1930  case PT_LPAD:
1931  case PT_RPAD:
1932  num = 0;
1933 
1934  /* two overloads */
1935 
1936  /* arg1 */
1939 
1940  /* arg2 */
1943 
1944  /* arg3 */
1947 
1948  /* return type */
1951  def->overloads[num++] = sig;
1952 
1953  /* arg1 */
1956 
1957  /* arg2 */
1960 
1961  /* arg3 */
1964 
1965  /* return type */
1968  def->overloads[num++] = sig;
1969 
1970  def->overloads_count = num;
1971  break;
1972 
1973  case PT_MD5:
1974  num = 0;
1975 
1976  /* one overload */
1977 
1978  /* arg1 */
1981 
1982  /* return type */
1985  def->overloads[num++] = sig;
1986 
1987  def->overloads_count = num;
1988  break;
1989  case PT_SHA_ONE:
1990  num = 0;
1991 
1992  /* one overload */
1993 
1994  /* arg1 */
1997 
1998  /* return type */
2001  def->overloads[num++] = sig;
2002 
2003  def->overloads_count = num;
2004  break;
2005 
2006  case PT_SHA_TWO:
2007  num = 0;
2008 
2009  /* one overload */
2010 
2011  /* arg1 */
2014 
2015  /* arg2 */
2018 
2019  /* return type */
2022  def->overloads[num++] = sig;
2023 
2024  def->overloads_count = num;
2025  break;
2026 
2027  case PT_AES_ENCRYPT:
2028  num = 0;
2029 
2030  /* one overload */
2031 
2032  /* arg1 */
2035 
2036  /* arg2 */
2039 
2040  /* return type */
2043  def->overloads[num++] = sig;
2044 
2045  def->overloads_count = num;
2046  break;
2047 
2048  case PT_AES_DECRYPT:
2049  num = 0;
2050 
2051  /* one overload */
2052 
2053  /* arg1 */
2056 
2057  /* arg2 */
2060 
2061  /* return type */
2064  def->overloads[num++] = sig;
2065 
2066  def->overloads_count = num;
2067  break;
2068 
2069  case PT_TO_BASE64:
2070  case PT_FROM_BASE64:
2071  num = 0;
2072 
2073  /* one overload */
2074 
2075  /* arg1 */
2078 
2079  /* return type */
2082  def->overloads[num++] = sig;
2083 
2084  def->overloads_count = num;
2085  break;
2086 
2087  case PT_MID:
2088  num = 0;
2089 
2090  /* one overload */
2091 
2092  /* arg1 */
2095 
2096  /* arg2 */
2099 
2100  /* arg3 */
2103 
2104  /* return type */
2107  def->overloads[num++] = sig;
2108 
2109  def->overloads_count = num;
2110  break;
2111 
2112  case PT_SUBSTRING:
2113  num = 0;
2114 
2115  /* two overloads */
2116 
2117  /* SUBSTRING (string, int, int) */
2126  def->overloads[num++] = sig;
2127 
2128  /* SUBSTRING (string, int) */
2137  def->overloads[num++] = sig;
2138 
2139  def->overloads_count = num;
2140  break;
2141 
2142  case PT_MONTHS_BETWEEN:
2143  num = 0;
2144 
2145  /* one overload */
2146 
2147  /* arg1 */
2150 
2151  /* arg2 */
2154 
2155  /* return type */
2158  def->overloads[num++] = sig;
2159 
2160  def->overloads_count = num;
2161  break;
2162 
2163  case PT_PI:
2164  num = 0;
2165 
2166  /* one overload */
2167 
2168  /* return type */
2171  def->overloads[num++] = sig;
2172 
2173  def->overloads_count = num;
2174  break;
2175 
2176  case PT_REPLACE:
2177  case PT_TRANSLATE:
2178  num = 0;
2179 
2180  /* two overloads */
2181 
2182  /* arg1 */
2185  /* arg2 */
2188  /* arg3 */
2191  /* return type */
2194  def->overloads[num++] = sig;
2195 
2196  /* arg1 */
2199  /* arg2 */
2202  /* arg3 */
2205  /* return type */
2208  def->overloads[num++] = sig;
2209 
2210  def->overloads_count = num;
2211  break;
2212 
2213  case PT_SPACE:
2214  num = 0;
2215 
2216  /* one overload */
2217 
2218  /* arg1 */
2221 
2222  /* return type */
2225  def->overloads[num++] = sig;
2226 
2227  def->overloads_count = num;
2228  break;
2229 
2230  case PT_STRCAT:
2231  num = 0;
2232 
2233  /* two overloads */
2234 
2235  /* arg1 */
2238 
2239  /* arg2 */
2242 
2243  /* return type */
2246  def->overloads[num++] = sig;
2247 
2248  /* arg1 */
2251 
2252  /* arg2 */
2255 
2256  /* return type */
2259  def->overloads[num++] = sig;
2260 
2261  def->overloads_count = num;
2262  break;
2263 
2264  case PT_UTC_DATE:
2265  case PT_SYS_DATE:
2266  case PT_CURRENT_DATE:
2267  num = 0;
2268 
2269  /* one overload */
2270 
2271  /* return type */
2274  def->overloads[num++] = sig;
2275 
2276  def->overloads_count = num;
2277  break;
2278 
2279  case PT_SYS_DATETIME:
2280  case PT_CURRENT_DATETIME:
2281  num = 0;
2282 
2283  /* one overload */
2284 
2285  /* return type */
2288  def->overloads[num++] = sig;
2289 
2290  def->overloads_count = num;
2291  break;
2292 
2293  case PT_UTC_TIME:
2294  case PT_SYS_TIME:
2295  case PT_CURRENT_TIME:
2296  num = 0;
2297 
2298  /* one overload */
2299 
2300  /* return type */
2303  def->overloads[num++] = sig;
2304 
2305  def->overloads_count = num;
2306  break;
2307 
2308  case PT_SYS_TIMESTAMP:
2309  case PT_UTC_TIMESTAMP:
2310  case PT_CURRENT_TIMESTAMP:
2311  num = 0;
2312 
2313  /* one overload */
2314 
2315  /* return type */
2318  def->overloads[num++] = sig;
2319 
2320  def->overloads_count = num;
2321  break;
2322 
2323  case PT_TIME_FORMAT:
2324  num = 0;
2325 
2326  /* three overloads */
2327 
2328  /* arg1 */
2331  /* arg2 */
2334  /* arg3 */
2337  /* return type */
2340  def->overloads[num++] = sig;
2341 
2342  /* arg1 */
2345  /* arg2 */
2348  /* arg3 */
2351  /* return type */
2354  def->overloads[num++] = sig;
2355 
2356  /* arg1 */
2359  /* arg2 */
2362  /* arg3 */
2365  /* return type */
2368  def->overloads[num++] = sig;
2369 
2370  def->overloads_count = num;
2371  break;
2372 
2373  case PT_TIMEF:
2374  num = 0;
2375 
2376  /* four overloads */
2377 
2378  /* arg1 */
2381  /* return type */
2384  def->overloads[num++] = sig;
2385 
2386  /* arg1 */
2389  /* return type */
2392  def->overloads[num++] = sig;
2393 
2394  /* arg1 */
2397  /* return type */
2400  def->overloads[num++] = sig;
2401 
2402  /* arg1 */
2405  /* return type */
2408  def->overloads[num++] = sig;
2409 
2410  def->overloads_count = num;
2411  break;
2412 
2413  case PT_TO_DATE:
2414  num = 0;
2415 
2416  /* one overload */
2417 
2418  /* arg1 */
2421  /* arg2 */
2424  /* arg3 */
2427 
2428  /* return type */
2431  def->overloads[num++] = sig;
2432 
2433  def->overloads_count = num;
2434  break;
2435 
2436  case PT_TO_DATETIME:
2437  num = 0;
2438 
2439  /* two overloads */
2440 
2441  /* arg1 */
2444  /* arg2 */
2447  /* arg3 */
2450 
2451  /* return type */
2454  def->overloads[num++] = sig;
2455 
2456  /* arg1 */
2459  /* arg2 */
2462  /* arg3 */
2465 
2466  /* return type */
2469  def->overloads[num++] = sig;
2470 
2471  def->overloads_count = num;
2472  break;
2473 
2474  case PT_TO_TIME:
2475  num = 0;
2476 
2477  /* two overloads */
2478 
2479  /* arg1 */
2482  /* arg2 */
2485  /* arg3 */
2488 
2489  /* return type */
2492  def->overloads[num++] = sig;
2493 
2494  /* arg1 */
2497  /* arg2 */
2500  /* arg3 */
2503 
2504  /* return type */
2507  def->overloads[num++] = sig;
2508 
2509  def->overloads_count = num;
2510  break;
2511 
2512  case PT_TO_TIMESTAMP:
2513  num = 0;
2514 
2515  /* two overloads */
2516 
2517  /* arg1 */
2520  /* arg2 */
2523  /* arg3 */
2526 
2527  /* return type */
2530  def->overloads[num++] = sig;
2531 
2532  /* arg1 */
2535  /* arg2 */
2538  /* arg3 */
2541 
2542  /* return type */
2545  def->overloads[num++] = sig;
2546 
2547  def->overloads_count = num;
2548  break;
2549 
2550  case PT_TO_NUMBER:
2551  num = 0;
2552 
2553  /* one overload */
2554 
2555  /* arg1 */
2558  /* arg2 */
2561  /* arg3 */
2564 
2565  /* return type */
2568  def->overloads[num++] = sig;
2569 
2570  def->overloads_count = num;
2571  break;
2572 
2573  case PT_WEEKF:
2574  num = 0;
2575 
2576  /* two overloads */
2577 
2578  /* arg1 */
2581  /* arg2 */
2584  /* return type */
2587  def->overloads[num++] = sig;
2588 
2589  /* arg1 */
2592  /* arg2 */
2595  /* return type */
2598  def->overloads[num++] = sig;
2599 
2600  def->overloads_count = num;
2601  break;
2602 
2603  case PT_CLOB_LENGTH:
2604  num = 0;
2605 
2606  /* one overload */
2607 
2608  /* arg1 */
2611 
2612  /* return type */
2615  def->overloads[num++] = sig;
2616 
2617  def->overloads_count = num;
2618  break;
2619 
2620  case PT_BLOB_LENGTH:
2621  num = 0;
2622 
2623  /* one overload */
2624 
2625  /* arg1 */
2628 
2629  /* return type */
2632  def->overloads[num++] = sig;
2633 
2634  def->overloads_count = num;
2635  break;
2636 
2637  case PT_BIT_TO_BLOB:
2638  num = 0;
2639 
2640  /* one overload */
2641 
2642  /* arg1 */
2645 
2646  /* return type */
2649  def->overloads[num++] = sig;
2650 
2651  def->overloads_count = num;
2652  break;
2653 
2654  case PT_CHAR_TO_CLOB:
2655  num = 0;
2656 
2657  /* one overload */
2658 
2659  /* arg1 */
2662 
2663  /* return type */
2666  def->overloads[num++] = sig;
2667 
2668  def->overloads_count = num;
2669  break;
2670 
2671  case PT_CHAR_TO_BLOB:
2672  num = 0;
2673 
2674  /* one overload */
2675 
2676  /* arg1 */
2679 
2680  /* return type */
2683  def->overloads[num++] = sig;
2684 
2685  def->overloads_count = num;
2686  break;
2687 
2688  case PT_BLOB_FROM_FILE:
2689  num = 0;
2690 
2691  /* one overload */
2692 
2693  /* arg1 */
2696 
2697  /* return type */
2700  def->overloads[num++] = sig;
2701 
2702  def->overloads_count = num;
2703  break;
2704 
2705  case PT_CLOB_FROM_FILE:
2706  num = 0;
2707 
2708  /* one overload */
2709 
2710  /* arg1 */
2713 
2714  /* return type */
2717  def->overloads[num++] = sig;
2718 
2719  def->overloads_count = num;
2720  break;
2721 
2722  case PT_BLOB_TO_BIT:
2723  num = 0;
2724 
2725  /* one overload */
2726 
2727  /* arg1 */
2730 
2731  /* return type */
2734  def->overloads[num++] = sig;
2735 
2736  def->overloads_count = num;
2737  break;
2738 
2739  case PT_CLOB_TO_CHAR:
2740  num = 0;
2741 
2742  /* one overload */
2743 
2744  /* arg1 */
2747 
2748  /* arg2 */
2751 
2752  /* return type */
2755  def->overloads[num++] = sig;
2756 
2757  def->overloads_count = num;
2758  break;
2759 
2760  case PT_INST_NUM:
2761  case PT_ROWNUM:
2762  case PT_ORDERBY_NUM:
2763  num = 0;
2764 
2765  /* one overload */
2766 
2767  /* return type */
2770  def->overloads[num++] = sig;
2771 
2772  def->overloads_count = num;
2773  break;
2774 
2775  case PT_LEVEL:
2776  case PT_CONNECT_BY_ISCYCLE:
2777  case PT_CONNECT_BY_ISLEAF:
2778  case PT_ROW_COUNT:
2779  num = 0;
2780 
2781  /* one overload */
2782 
2783  /* return type */
2786  def->overloads[num++] = sig;
2787 
2788  def->overloads_count = num;
2789  break;
2790 
2791  case PT_LAST_INSERT_ID:
2792  num = 0;
2793 
2794  /* one overload */
2795 
2796  /* return type */
2799  def->overloads[num++] = sig;
2800 
2801  def->overloads_count = num;
2802  break;
2803 
2804  case PT_DATEDIFF:
2805  num = 0;
2806 
2807  /* one overload */
2808 
2811 
2814 
2815  /* return type */
2818  def->overloads[num++] = sig;
2819 
2820  def->overloads_count = num;
2821  break;
2822 
2823  case PT_TIMEDIFF:
2824  num = 0;
2825 
2826  /* 3 overloads */
2827 
2828  /* arg1 */
2831  /* arg2 */
2834  /* return type */
2837  def->overloads[num++] = sig;
2838 
2839  /* arg1 */
2842  /* arg2 */
2845  /* return type */
2848  def->overloads[num++] = sig;
2849 
2850  /* arg1 */
2853  /* arg2 */
2856  /* return type */
2859  def->overloads[num++] = sig;
2860 
2861  def->overloads_count = num;
2862  break;
2863 
2864  case PT_INCR:
2865  case PT_DECR:
2866  num = 0;
2867 
2868  /* two overloads */
2869 
2872  /* return type */
2875  def->overloads[num++] = sig;
2876 
2883  /* return type */
2886  def->overloads[num++] = sig;
2887 
2888  def->overloads_count = num;
2889  break;
2890 
2891  case PT_FORMAT:
2892  num = 0;
2893 
2894  /* one overload */
2895 
2898 
2901 
2904 
2905  /* return type */
2908  def->overloads[num++] = sig;
2909 
2910  def->overloads_count = num;
2911  break;
2912 
2913  case PT_ROUND:
2914  num = 0;
2915  /* nine overloads */
2916  /* first overload for number: */
2919 
2922 
2923  /* return type */
2926 
2927  def->overloads[num++] = sig;
2928 
2929  /* overload for round('123', '1') */
2932 
2935 
2936  /* return type */
2939 
2940  def->overloads[num++] = sig;
2941 
2942  /* overload for round(date, 'year|month|day') */
2945 
2948 
2949  /* return type */
2952 
2953  def->overloads[num++] = sig;
2954 
2955  /* overload for round(datetime, 'year|month|day') */
2958 
2961 
2962  /* return type */
2965 
2966  def->overloads[num++] = sig;
2967 
2968  /* overload for round(timestamp, 'year|month|day') */
2971 
2974 
2975  /* return type */
2978 
2979  def->overloads[num++] = sig;
2980 
2981  /* overload for round(timestamptz, 'year|month|day') */
2984 
2987 
2988  /* return type */
2991 
2992  def->overloads[num++] = sig;
2993 
2994  /* overload for round(timestampltz, 'year|month|day') */
2997 
3000 
3001  /* return type */
3004 
3005  def->overloads[num++] = sig;
3006 
3007  /* overload for round(datetimetz, 'year|month|day') */
3010 
3013 
3014  /* return type */
3017 
3018  def->overloads[num++] = sig;
3019 
3020  /* overload for round(datetimeltz, 'year|month|day') */
3023 
3026 
3027  /* return type */
3030 
3031  def->overloads[num++] = sig;
3032 
3033  def->overloads_count = num;
3034  break;
3035 
3036  case PT_TRUNC:
3037  num = 0;
3038 
3039  /* nine overloads */
3040 
3041  /* number types */
3044 
3047 
3048  /* return type */
3051  def->overloads[num++] = sig;
3052 
3053  /* number types 2 */
3056 
3059 
3060  /* return type */
3063  def->overloads[num++] = sig;
3064 
3065  /* date */
3068 
3071 
3072  /* return type */
3075  def->overloads[num++] = sig;
3076 
3077  /* datetime */
3080 
3083 
3084  /* return type */
3087  def->overloads[num++] = sig;
3088 
3089  /* timestamp */
3092 
3095 
3096  /* return type */
3099  def->overloads[num++] = sig;
3100 
3101  /* datetimeltz */
3104 
3107 
3108  /* return type */
3111  def->overloads[num++] = sig;
3112 
3113  /* datetimetz */
3116 
3119 
3120  /* return type */
3123  def->overloads[num++] = sig;
3124 
3125  /* timestampltz */
3128 
3131 
3132  /* return type */
3135  def->overloads[num++] = sig;
3136 
3137  /* timestamptz */
3140 
3143 
3144  /* return type */
3147  def->overloads[num++] = sig;
3148 
3149  def->overloads_count = num;
3150  break;
3151 
3152  case PT_DEFAULTF:
3153  num = 0;
3154 
3155  /* one overload */
3156 
3159 
3160  /* return type */
3163  def->overloads[num++] = sig;
3164 
3165  def->overloads_count = num;
3166  break;
3167 
3168  case PT_INDEX_CARDINALITY:
3169  num = 0;
3170 
3171  /* one overload */
3172 
3181  def->overloads[num++] = sig;
3182 
3183  def->overloads_count = num;
3184  break;
3185 
3186  case PT_SIGN:
3187  num = 0;
3188 
3189  /* one overload */
3190 
3193 
3194  /* return type */
3197  def->overloads[num++] = sig;
3198 
3199  def->overloads_count = num;
3200  break;
3201 
3202  case PT_SETEQ:
3203  case PT_SETNEQ:
3204  case PT_SUBSET:
3205  case PT_SUBSETEQ:
3206  case PT_SUPERSET:
3207  case PT_SUPERSETEQ:
3208  num = 0;
3209 
3210  /* one overload */
3211 
3214 
3217 
3218  /* return type */
3221  def->overloads[num++] = sig;
3222 
3223  def->overloads_count = num;
3224  break;
3225 
3226  case PT_GE:
3227  case PT_GT:
3228  case PT_LT:
3229  case PT_LE:
3230  num = 0;
3231 
3232  /* two overloads */
3233 
3236 
3239 
3242  def->overloads[num++] = sig;
3243 
3246 
3249 
3252  def->overloads[num++] = sig;
3253 
3254  def->overloads_count = num;
3255  break;
3256 
3257  case PT_EQ:
3258  case PT_NE:
3259  case PT_NULLSAFE_EQ:
3260  num = 0;
3261 
3262  /* one overload */
3263 
3266 
3269 
3272  def->overloads[num++] = sig;
3273 
3274  def->overloads_count = num;
3275  break;
3276 
3277  case PT_NVL:
3278  case PT_IFNULL:
3279  case PT_COALESCE:
3280  num = 0;
3281 
3282  /* arg1 : generic string , arg2 : generic string */
3289  def->overloads[num++] = sig;
3290 
3291  /* arg1 : generic string , arg2 : generic any */
3298  def->overloads[num++] = sig;
3299 
3300  /* arg1 : generic bit , arg2 : generic bit */
3307  def->overloads[num++] = sig;
3308 
3309  /* arg1 : generic bit , arg2 : generic any */
3316  def->overloads[num++] = sig;
3317 
3318  /* arg1 : generic number , arg2 : generic number */
3325  def->overloads[num++] = sig;
3326 
3327  /* arg1 : generic number , arg2 : generic any */
3334  def->overloads[num++] = sig;
3335 
3336  /* arg1 : generic date , arg2 : generic date */
3343  def->overloads[num++] = sig;
3344 
3345  /* arg1 : generic date , arg2 : generic any */
3352  def->overloads[num++] = sig;
3353 
3354  /* arg1 : PT_TYPE_TIME , arg2 : PT_TYPE_TIME */
3361  def->overloads[num++] = sig;
3362 
3363  /* arg1 : PT_TYPE_TIME , arg2 : generic any */
3370  def->overloads[num++] = sig;
3371 
3372  /* arg1 : generic sequence, arg2 : generic sequence */
3379  def->overloads[num++] = sig;
3380 
3381  /* arg1 : generic sequence, arg2 : generic type any */
3388  def->overloads[num++] = sig;
3389 
3390  /* arg1 : generic lob, arg2 : generic lob */
3397  def->overloads[num++] = sig;
3398 
3399  /* arg1 : generic sequence, arg2 : generic type any */
3406  def->overloads[num++] = sig;
3407 
3408  /* arg1 : generic any, arg2 : generic any */
3411 
3414 
3417  def->overloads[num++] = sig;
3418 
3419  def->overloads_count = num;
3420 
3421  break;
3422 
3423  case PT_NULLIF:
3424  case PT_LEAST:
3425  case PT_GREATEST:
3426  num = 0;
3427 
3428  /* one overload */
3429 
3432 
3435 
3438  def->overloads[num++] = sig;
3439 
3440  def->overloads_count = num;
3441  break;
3442 
3443  case PT_NVL2:
3444  num = 0;
3445 
3446  /* arg1, arg1, arg3 : generic string */
3455  def->overloads[num++] = sig;
3456 
3457  /* arg1 : generic string , arg2, arg3 : generic any */
3466  def->overloads[num++] = sig;
3467 
3468  /* arg1, arg2, arg3 : generic bit */
3477  def->overloads[num++] = sig;
3478 
3479  /* arg1 : generic bit , arg2, arg3 : generic any */
3488  def->overloads[num++] = sig;
3489 
3490  /* arg1, arg2, arg3 : generic number */
3499  def->overloads[num++] = sig;
3500 
3501  /* arg1 : generic number , arg2, arg3 : generic any */
3510  def->overloads[num++] = sig;
3511 
3512  /* arg1, arg2, arg3 : generic date */
3521  def->overloads[num++] = sig;
3522 
3523  /* arg1 : generic date , arg2, arg3 : generic any */
3532  def->overloads[num++] = sig;
3533 
3534  /* arg1, arg2, arg3 : PT_TYPE_TIME */
3543  def->overloads[num++] = sig;
3544 
3545  /* arg1 : PT_TYPE_TIME , arg2, arg3 : generic any */
3554  def->overloads[num++] = sig;
3555 
3556  /* arg1, arg2, arg3 : generic sequence */
3565  def->overloads[num++] = sig;
3566 
3567  /* arg1 : generic sequence, arg2, arg3 : generic type any */
3576  def->overloads[num++] = sig;
3577 
3578  /* arg1, arg2, arg3 : generic lob */
3587  def->overloads[num++] = sig;
3588 
3589  /* arg1 : generic lob, arg2, arg3 : generic type any */
3598  def->overloads[num++] = sig;
3599 
3600  /* arg1, arg2, arg3 : generic any */
3609  def->overloads[num++] = sig;
3610 
3611  def->overloads_count = num;
3612 
3613  break;
3614  case PT_CONNECT_BY_ROOT:
3615  case PT_PRIOR:
3616  case PT_QPRIOR:
3617  num = 0;
3618 
3619  /* one overload */
3620 
3623 
3626  def->overloads[num++] = sig;
3627 
3628  def->overloads_count = num;
3629  break;
3630 
3631  case PT_UNARY_MINUS:
3632  num = 0;
3633 
3634  /* one overload */
3635 
3638 
3641  def->overloads[num++] = sig;
3642 
3643  def->overloads_count = num;
3644  break;
3645 
3646  case PT_TO_CHAR:
3647  num = 0;
3648 
3649  /* two overloads */
3650 
3653 
3656 
3659 
3662  def->overloads[num++] = sig;
3663 
3666 
3669 
3672 
3675  def->overloads[num++] = sig;
3676 
3677  def->overloads_count = num;
3678  break;
3679 
3680  case PT_ISNULL:
3681  case PT_IS_NULL:
3682  case PT_IS_NOT_NULL:
3683  num = 0;
3684 
3685  /* one overload */
3686 
3689 
3692  def->overloads[num++] = sig;
3693 
3694  def->overloads_count = num;
3695  break;
3696 
3697  case PT_IS:
3698  case PT_IS_NOT:
3699  num = 0;
3700 
3701  /* one overload */
3702 
3705 
3708 
3711  def->overloads[num++] = sig;
3712 
3713  def->overloads_count = num;
3714  break;
3715 
3716  case PT_SUBDATE:
3717  case PT_ADDDATE:
3718  num = 0;
3719 
3720  /* 8 overloads */
3721 
3724 
3727 
3730  def->overloads[num++] = sig;
3731 
3734 
3737 
3740  def->overloads[num++] = sig;
3741 
3744 
3747 
3750  def->overloads[num++] = sig;
3751 
3754 
3757 
3760  def->overloads[num++] = sig;
3761 
3764 
3767 
3770  def->overloads[num++] = sig;
3771 
3774 
3777 
3780  def->overloads[num++] = sig;
3781 
3784 
3787 
3790  def->overloads[num++] = sig;
3791 
3794 
3797 
3800  def->overloads[num++] = sig;
3801 
3802  def->overloads_count = num;
3803  break;
3804 
3805  case PT_GE_SOME:
3806  case PT_GT_SOME:
3807  case PT_LT_SOME:
3808  case PT_LE_SOME:
3809  case PT_GE_ALL:
3810  case PT_GT_ALL:
3811  case PT_LT_ALL:
3812  case PT_LE_ALL:
3813  num = 0;
3814 
3815  /* two overloads */
3816 
3819 
3821  sig.arg2_type.val.type = PT_TYPE_SET;
3822 
3825  def->overloads[num++] = sig;
3826 
3829 
3832 
3835  def->overloads[num++] = sig;
3836 
3837  def->overloads_count = num;
3838  break;
3839 
3840  case PT_EQ_SOME:
3841  case PT_NE_SOME:
3842  case PT_EQ_ALL:
3843  case PT_NE_ALL:
3844  case PT_IS_IN:
3845  case PT_IS_NOT_IN:
3846  num = 0;
3847 
3848  /* two overloads */
3849 
3852 
3854  sig.arg2_type.val.type = PT_TYPE_SET;
3855 
3858  def->overloads[num++] = sig;
3859 
3862 
3865 
3868  def->overloads[num++] = sig;
3869 
3870  def->overloads_count = num;
3871  break;
3872 
3873  case PT_BETWEEN_EQ_NA:
3874  case PT_BETWEEN_GE_INF:
3875  case PT_BETWEEN_GT_INF:
3876  case PT_BETWEEN_INF_LE:
3877  case PT_BETWEEN_INF_LT:
3878  case PT_LT_INF:
3879  case PT_GT_INF:
3880  /* these expressions are introduced during query rewriting and are used in the range operator. we should set the
3881  * return type to the type of the argument */
3882 
3883  num = 0;
3884 
3885  /* one overload */
3886 
3889 
3892  def->overloads[num++] = sig;
3893 
3894  def->overloads_count = num;
3895  break;
3896 
3897  case PT_UNIX_TIMESTAMP:
3898  num = 0;
3899 
3900  /* three overloads */
3901 
3902  /* UNIX_TIMESTAMP(string) */
3907  def->overloads[num++] = sig;
3908 
3909  /* UNIX_TIMESTAMP(date/time type) */
3914  def->overloads[num++] = sig;
3915 
3916  /* UNIX_TIMESTAMP (void) */
3921  def->overloads[num++] = sig;
3922 
3923  def->overloads_count = num;
3924  break;
3925 
3926  case PT_FROM_UNIXTIME:
3927  num = 0;
3928 
3929  /* two overloads */
3930 
3933 
3936 
3939 
3942  def->overloads[num++] = sig;
3943 
3946 
3949 
3952 
3955  def->overloads[num++] = sig;
3956 
3957  def->overloads_count = num;
3958  break;
3959 
3960  case PT_TIMESTAMP:
3961  num = 0;
3962 
3963  /* eight overloads */
3964 
3965  /* TIMESTAMP(STRING) */
3970  def->overloads[num++] = sig;
3971 
3972  /* TIMESTAMP(DATETIME) */
3977  def->overloads[num++] = sig;
3978 
3979  /* TIMESTAMP(STRING,STRING) */
3986  def->overloads[num++] = sig;
3987 
3988  /* TIMESTAMP(STRING,NUMBER) */
3995  def->overloads[num++] = sig;
3996 
3997 
3998  /* TIMESTAMP(STRING,TIME) */
4005  def->overloads[num++] = sig;
4006 
4007  /* TIMESTAMP(DATETIME,STRING) */
4014  def->overloads[num++] = sig;
4015 
4016  /* TIMESTAMP(DATETIME,TIME) */
4023  def->overloads[num++] = sig;
4024 
4025  /* TIMESTAMP(DATETIME,NUMBER) */
4032  def->overloads[num++] = sig;
4033 
4034  def->overloads_count = num;
4035  break;
4036 
4037  case PT_TYPEOF:
4038  num = 0;
4039 
4040  /* one overload */
4041 
4046  def->overloads[num++] = sig;
4047 
4048  def->overloads_count = num;
4049  break;
4050 
4051  case PT_EXTRACT:
4052  num = 0;
4053 
4054  /* five overloads */
4055 
4058  /* return type */
4061  def->overloads[num++] = sig;
4062 
4065  /* return type */
4068  def->overloads[num++] = sig;
4069 
4072  /* return type */
4075  def->overloads[num++] = sig;
4076 
4079  /* return type */
4082  def->overloads[num++] = sig;
4083 
4086  /* return type */
4089  def->overloads[num++] = sig;
4090 
4091  def->overloads_count = num;
4092  break;
4093 
4094  case PT_EVALUATE_VARIABLE:
4095  num = 0;
4096 
4097  /* one overload */
4098 
4101 
4104 
4105  def->overloads[num++] = sig;
4106 
4107  def->overloads_count = num;
4108  break;
4109 
4110  case PT_DEFINE_VARIABLE:
4111  num = 0;
4112 
4113  /* two overloads */
4114 
4117 
4120 
4123  def->overloads[num++] = sig;
4124 
4127 
4130 
4133  def->overloads[num++] = sig;
4134 
4135  def->overloads_count = num;
4136  break;
4137 
4138  case PT_EXEC_STATS:
4139  num = 0;
4140 
4141  /* one overload */
4142 
4147  def->overloads[num++] = sig;
4148 
4149  def->overloads_count = num;
4150  break;
4151 
4153  num = 0;
4154 
4155  /* one overload */
4156 
4157  /* arg1 */
4160 
4161  /* return type */
4164 
4165  def->overloads[num++] = sig;
4166 
4167  def->overloads_count = num;
4168  break;
4169 
4170  case PT_INET_ATON:
4171  num = 0;
4172 
4173  /* one overload */
4174 
4175  /* arg1 */
4178 
4179  /* return type */
4182 
4183  def->overloads[num++] = sig;
4184 
4185  def->overloads_count = num;
4186  break;
4187 
4188  case PT_INET_NTOA:
4189  num = 0;
4190 
4191  /* one overload */
4192 
4193  /* arg1 */
4196 
4197  /* return type */
4200 
4201  def->overloads[num++] = sig;
4202 
4203  def->overloads_count = num;
4204  break;
4205 
4206  case PT_COERCIBILITY:
4207  num = 0;
4208 
4209  /* one overload */
4210 
4211  /* arg1 */
4214 
4215  /* return type */
4218 
4219  def->overloads[num++] = sig;
4220 
4221  def->overloads_count = num;
4222  break;
4223 
4224  case PT_CHARSET:
4225  case PT_COLLATION:
4226  num = 0;
4227 
4228  /* one overload */
4229 
4230  /* arg1 */
4233 
4234  /* return type */
4237 
4238  def->overloads[num++] = sig;
4239 
4240  def->overloads_count = num;
4241  break;
4242 
4243  case PT_WIDTH_BUCKET:
4244  num = 0;
4245 
4246  /* 10 overloads */
4247 
4248  /* generic number */
4251 
4253  sig.arg2_type.val.generic_type = PT_GENERIC_TYPE_NUMBER; /* between */
4254 
4257 
4260 
4261  def->overloads[num++] = sig;
4262 
4263  /* generic string */
4266 
4268  sig.arg2_type.val.generic_type = PT_GENERIC_TYPE_STRING; /* between */
4269 
4272 
4275 
4276  def->overloads[num++] = sig;
4277 
4278  /* date */
4281 
4283  sig.arg2_type.val.type = PT_TYPE_DATE; /* between */
4284 
4287 
4290 
4291  def->overloads[num++] = sig;
4292 
4293  /* datetime */
4296 
4298  sig.arg2_type.val.type = PT_TYPE_DATETIME; /* between */
4299 
4302 
4305 
4306  def->overloads[num++] = sig;
4307 
4308  /* timestamp */
4311 
4313  sig.arg2_type.val.type = PT_TYPE_TIMESTAMP; /* between */
4314 
4317 
4320 
4321  def->overloads[num++] = sig;
4322 
4323  /* time */
4326 
4328  sig.arg2_type.val.type = PT_TYPE_TIME; /* between */
4329 
4332 
4335 
4336  def->overloads[num++] = sig;
4337 
4338  /* datetime with local timezone */
4341 
4343  sig.arg2_type.val.type = PT_TYPE_DATETIMELTZ; /* between */
4344 
4347 
4350 
4351  def->overloads[num++] = sig;
4352 
4353  /* datetime with timezone */
4356 
4358  sig.arg2_type.val.type = PT_TYPE_DATETIMETZ; /* between */
4359 
4362 
4365 
4366  def->overloads[num++] = sig;
4367 
4368  /* timestamp with local timezone */
4371 
4373  sig.arg2_type.val.type = PT_TYPE_TIMESTAMPLTZ; /* between */
4374 
4377 
4380 
4381  def->overloads[num++] = sig;
4382 
4383  /* timestamp with timezone */
4386 
4388  sig.arg2_type.val.type = PT_TYPE_TIMESTAMPTZ; /* between */
4389 
4392 
4395 
4396  def->overloads[num++] = sig;
4397 
4398  def->overloads_count = num;
4399  break;
4400 
4401  case PT_TRACE_STATS:
4402  num = 0;
4403 
4404  /* one overload */
4405 
4408  def->overloads[num++] = sig;
4409 
4410  def->overloads_count = num;
4411  break;
4412 
4413  case PT_INDEX_PREFIX:
4414  num = 0;
4415 
4416  /* three overloads */
4417 
4418  /* arg1 */
4421  /* arg2 */
4424  /* arg3 */
4427  /* return type */
4430  def->overloads[num++] = sig;
4431 
4432  /* arg1 */
4435  /* arg2 */
4438  /* arg3 */
4441  /* return type */
4444  def->overloads[num++] = sig;
4445 
4446  /* arg1 */
4449  /* arg2 */
4452  /* arg3 */
4455  /* return type */
4458  def->overloads[num++] = sig;
4459 
4460  def->overloads_count = num;
4461  break;
4462 
4463  case PT_SLEEP:
4464  num = 0;
4465 
4466  /* one overload */
4467 
4470 
4473  def->overloads[num++] = sig;
4474 
4475  def->overloads_count = num;
4476  break;
4477 
4478  case PT_DBTIMEZONE:
4479  case PT_SESSIONTIMEZONE:
4480  num = 0;
4481 
4482  /* one overload */
4483 
4484  /* return type */
4487  def->overloads[num++] = sig;
4488 
4489  def->overloads_count = num;
4490  break;
4491 
4492  case PT_TZ_OFFSET:
4493  num = 0;
4494 
4495  /* one overload */
4496 
4497  /* arg1 */
4500 
4501  /* return type */
4504  def->overloads[num++] = sig;
4505 
4506  def->overloads_count = num;
4507  break;
4508 
4509  case PT_NEW_TIME:
4510  num = 0;
4511  /* three overloads */
4512 
4513  /* arg1 */
4516 
4517  /* arg2 */
4520 
4521  /* arg3 */
4524 
4525  /* return type */
4528  def->overloads[num++] = sig;
4529 
4530  /* arg1 */
4533 
4534  /* arg2 */
4537 
4538  /* arg3 */
4541 
4542  /* return type */
4545  def->overloads[num++] = sig;
4546 
4547  /* arg1 */
4550 
4551  /* arg2 */
4554 
4555  /* arg3 */
4558 
4559  /* return type */
4562  def->overloads[num++] = sig;
4563 
4564  def->overloads_count = num;
4565  break;
4566 
4567  case PT_FROM_TZ:
4568  num = 0;
4569  /* two overloads */
4570 
4571  /* arg1 */
4574 
4575  /* arg2 */
4578 
4579  /* return type */
4582  def->overloads[num++] = sig;
4583 
4584  /* arg1 */
4587 
4588  /* arg2 */
4591 
4592  /* return type */
4595  def->overloads[num++] = sig;
4596 
4597  def->overloads_count = num;
4598  break;
4599 
4600  case PT_CONV_TZ:
4601  num = 0;
4602  /* four overloads */
4603 
4604  /* arg1 */
4607 
4608  /* return type */
4611  def->overloads[num++] = sig;
4612 
4613  /* arg1 */
4616 
4617  /* return type */
4620  def->overloads[num++] = sig;
4621 
4622  /* arg1 */
4625 
4626  /* return type */
4629  def->overloads[num++] = sig;
4630 
4631  /* arg1 */
4634 
4635  /* return type */
4638  def->overloads[num++] = sig;
4639 
4640  def->overloads_count = num;
4641  break;
4642 
4643  case PT_TO_DATETIME_TZ:
4644  num = 0;
4645 
4646  /* two overloads */
4647 
4648  /* arg1 */
4651  /* arg2 */
4654  /* arg3 */
4657 
4658  /* return type */
4661  def->overloads[num++] = sig;
4662 
4663  /* arg1 */
4666  /* arg2 */
4669  /* arg3 */
4672 
4673  /* return type */
4676  def->overloads[num++] = sig;
4677 
4678  def->overloads_count = num;
4679  break;
4680 
4681  case PT_TO_TIMESTAMP_TZ:
4682  num = 0;
4683 
4684  /* two overloads */
4685 
4686  /* arg1 */
4689  /* arg2 */
4692  /* arg3 */
4695 
4696  /* return type */
4699  def->overloads[num++] = sig;
4700 
4701  /* arg1 */
4704  /* arg2 */
4707  /* arg3 */
4710 
4711  /* return type */
4714  def->overloads[num++] = sig;
4715 
4716  def->overloads_count = num;
4717  break;
4718  case PT_CRC32:
4719  num = 0;
4720 
4721  /* one overload */
4722 
4723  /* arg1 */
4726 
4727  /* return type */
4730  def->overloads[num++] = sig;
4731 
4732  def->overloads_count = num;
4733  break;
4734  case PT_SCHEMA_DEF:
4735  num = 0;
4736 
4737  /* one overload */
4738 
4739  /* arg1 */
4742 
4743  /* return type */
4746  def->overloads[num++] = sig;
4747 
4748  def->overloads_count = num;
4749  break;
4750 
4751  default:
4752  return false;
4753  }
4754 
4756 
4757  return true;
4758 }
4759 
4760 /*
4761  * pt_coerce_expression_argument () - check if arg has the correct type and
4762  * wrap arg with a cast node to def_type
4763  * if needed.
4764  * return : NO_ERROR on success, ER_FAILED on error
4765  * parser(in) : the parser context
4766  * expr(in) : the expression to which arg belongs to
4767  * arg (in/out) : the expression argument that will be checked
4768  * def_type(in) : the type that was evaluated from the expression definition
4769  * data_type(in): precision and scale information for arg
4770  */
4771 static int
4773  PT_NODE * data_type)
4774 {
4775  PT_NODE *node = *arg;
4776  PT_NODE *new_node = NULL, *new_dt = NULL;
4777  TP_DOMAIN *d;
4778  int scale = DB_DEFAULT_SCALE, precision = DB_DEFAULT_PRECISION;
4779 
4780  if (node == NULL)
4781  {
4782  if (def_type != PT_TYPE_NONE)
4783  {
4784  return ER_FAILED;
4785  }
4786  return NO_ERROR;
4787  }
4788 
4789  if (node->type_enum == PT_TYPE_NULL)
4790  {
4791  /* no coercion needed for NULL arguments */
4792  return NO_ERROR;
4793  }
4794 
4795  if (def_type == node->type_enum)
4796  {
4797  return NO_ERROR;
4798  }
4799 
4800  if (def_type == PT_TYPE_LOGICAL)
4801  {
4802  /* no cast for type logical. this is an error and we should report it */
4803  return ER_FAILED;
4804  }
4805 
4806  /* set default scale and precision for parametrized types */
4807  switch (def_type)
4808  {
4809  case PT_TYPE_BIGINT:
4810  precision = DB_DEFAULT_PRECISION;
4811  scale = DB_DEFAULT_SCALE;
4812  break;
4813 
4814  case PT_TYPE_NUMERIC:
4816  {
4817  precision = DB_DEFAULT_NUMERIC_PRECISION;
4818  scale = 0;
4819  }
4820  else
4821  {
4822  precision = DB_DEFAULT_NUMERIC_PRECISION;
4824  }
4825  break;
4826 
4827  case PT_TYPE_VARCHAR:
4828  precision = TP_FLOATING_PRECISION_VALUE;
4829  scale = 0;
4830  break;
4831 
4832  case PT_TYPE_VARNCHAR:
4833  precision = TP_FLOATING_PRECISION_VALUE;
4834  scale = 0;
4835  break;
4836  case PT_TYPE_ENUMERATION:
4837  {
4838  /* Because enumerations should always be casted to a fully specified type, we only accept casting to an
4839  * enumeration type if we have a symmetrical expression (meaning that the arguments of the expression should
4840  * have the same type) and one of the arguments is already an enumeration. In this case, we will cast the
4841  * argument that is not an enumeration to the enumeration type of the other argument */
4842  PT_NODE *arg1, *arg2, *arg3;
4843  if (expr == NULL)
4844  {
4845  assert (false);
4846  return ER_FAILED;
4847  }
4848  if (!pt_is_symmetric_op (expr->info.expr.op))
4849  {
4850  assert (false);
4851  return ER_FAILED;
4852  }
4853 
4854  arg1 = expr->info.expr.arg1;
4855  arg2 = expr->info.expr.arg2;
4856  arg3 = expr->info.expr.arg3;
4857  /* we already know that arg is not an enumeration so we have to look for an enumeration between the other
4858  * arguments of expr */
4859  if (arg1 != NULL && arg1->type_enum == PT_TYPE_ENUMERATION)
4860  {
4861  new_dt = arg1->data_type;
4862  }
4863  else if (arg2 != NULL && arg2->type_enum == PT_TYPE_ENUMERATION)
4864  {
4865  new_dt = arg2->data_type;
4866  }
4867  else if (arg3 != NULL && arg3->type_enum == PT_TYPE_ENUMERATION)
4868  {
4869  new_dt = arg3->data_type;
4870  }
4871  if (new_dt == NULL)
4872  {
4873  assert (false);
4876  return ER_FAILED;
4877  }
4878  break;
4879  }
4880  default:
4881  precision = DB_DEFAULT_PRECISION;
4882  scale = DB_DEFAULT_SCALE;
4883  break;
4884  }
4885 
4886  if (node->type_enum == PT_TYPE_MAYBE)
4887  {
4888  if ((node->node_type == PT_EXPR && pt_is_op_hv_late_bind (node->info.expr.op))
4889  || (node->node_type == PT_SELECT && node->info.query.is_subquery == PT_IS_SUBQUERY))
4890  {
4891  /* wrap with cast, instead of setting expected domain */
4892  new_node = pt_wrap_with_cast_op (parser, node, def_type, precision, scale, new_dt);
4893 
4894  if (new_node == NULL)
4895  {
4896  return ER_FAILED;
4897  }
4898  /* reset expected domain of wrapped argument to NULL: it will be replaced at XASL generation with
4899  * DB_TYPE_VARIABLE domain */
4900  node->expected_domain = NULL;
4901 
4902  *arg = new_node;
4903  }
4904  else
4905  {
4906  if (new_dt != NULL)
4907  {
4908  d = pt_data_type_to_db_domain (parser, new_dt, NULL);
4909  }
4910  else
4911  {
4912  d =
4915  }
4916  if (d == NULL)
4917  {
4918  return ER_FAILED;
4919  }
4920  /* make sure the returned domain is cached */
4921  d = tp_domain_cache (d);
4922  SET_EXPECTED_DOMAIN (node, d);
4923  }
4924 
4925  if (node->node_type == PT_HOST_VAR)
4926  {
4927  pt_preset_hostvar (parser, node);
4928  }
4929  }
4930  else
4931  {
4932  if (PT_IS_COLLECTION_TYPE (def_type))
4933  {
4934  if (data_type == NULL)
4935  {
4936  if (PT_IS_COLLECTION_TYPE (node->type_enum))
4937  {
4938  data_type = parser_copy_tree_list (parser, node->data_type);
4939  }
4940  else
4941  {
4942  /* this is might not be an error and we might do more damage if we cast it */
4943  return NO_ERROR;
4944  }
4945  }
4946  new_node = pt_wrap_collection_with_cast_op (parser, node, def_type, data_type, false);
4947  }
4948  else
4949  {
4950  new_node = pt_wrap_with_cast_op (parser, node, def_type, precision, scale, new_dt);
4951  }
4952  if (new_node == NULL)
4953  {
4954  return ER_FAILED;
4955  }
4957  {
4958  assert (new_node->node_type == PT_EXPR);
4960  }
4961 
4962  *arg = new_node;
4963  }
4964 
4965  return NO_ERROR;
4966 }
4967 
4968 /*
4969  * pt_are_unmatchable_types () - check if the two types cannot be matched
4970  * return : true if the two types cannot be matched
4971  * def_type(in) : an expression definition type
4972  * op_type(in) : an argument type
4973  */
4974 static bool
4975 pt_are_unmatchable_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type)
4976 {
4977  /* PT_TYPE_NONE does not match anything */
4978  if (op_type == PT_TYPE_NONE && !(def_type.type == pt_arg_type::NORMAL && def_type.val.type == PT_TYPE_NONE))
4979  {
4980  return true;
4981  }
4982 
4983  if (op_type != PT_TYPE_NONE && def_type.type == pt_arg_type::NORMAL && def_type.val.type == PT_TYPE_NONE)
4984  {
4985  return true;
4986  }
4987 
4988  return false;
4989 }
4990 
4991 static PT_NODE *
4993 {
4994  if (new_type == PT_TYPE_NUMERIC)
4995  {
4996  switch (old_type)
4997  {
4998  case PT_TYPE_SMALLINT:
4999  case PT_TYPE_INTEGER:
5000  case PT_TYPE_BIGINT:
5001  return data_type;
5002  default:
5003  /* any other type should set maximum scale and precision since we cannot correctly evaluate them during type
5004  * checking */
5005  return NULL;
5006  }
5007  }
5008  return NULL;
5009 }
5010 
5011 
5012 /*
5013  * pt_infer_common_type - get the common type of the three arguments
5014  *
5015  * return : the common type
5016  * op(in) : expression identifier
5017  * arg1(in/out) : type of the first argument
5018  * arg2(in/out) : type of the second argument
5019  * arg3(in/out) : type of the third argument
5020  *
5021  * Notes: Unlike pt_common_type_op, this function infers a type for host
5022  * variables also. We can do this here because this function is called
5023  * after the expression signature has been applied. This means that
5024  * a type is left as PT_TYPE_MAYBE because the expression is symmetric
5025  * and the signature defines PT_GENERIC_TYPE_ANY or
5026  * PT_GENERIC_TYPE_PRIMITIVE for this argument.
5027  * There are some issues with collection types that this function does
5028  * not address. Collections are composed types so it's not enough to
5029  * decide that the common type is a collection type. We should also
5030  * infer the common type of the collection elements. This should be
5031  * done in the calling function because we have access to the actual
5032  * arguments there.
5033  */
5034 static PT_TYPE_ENUM
5036  const TP_DOMAIN * expected_domain)
5037 {
5038  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5039  PT_TYPE_ENUM arg1_eq_type = *arg1;
5040  PT_TYPE_ENUM arg2_eq_type = *arg2;
5041  PT_TYPE_ENUM arg3_eq_type = *arg3;
5042  PT_TYPE_ENUM expected_type = PT_TYPE_NONE;
5043  assert (pt_is_symmetric_op (op));
5044 
5045  /* We ignore PT_TYPE_NONE arguments because, in the context of this function, if an argument is of type PT_TYPE_NONE
5046  * then it is not defined in the signature that we have decided to use. */
5047 
5048  if (expected_domain != NULL)
5049  {
5050  expected_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (expected_domain));
5051  }
5052 
5053  common_type = arg1_eq_type;
5054  if (arg1_eq_type != PT_TYPE_NONE)
5055  {
5056  /* arg1 is defined */
5057  if (arg2_eq_type != PT_TYPE_NONE)
5058  {
5059  /* arg2 is defined */
5060  common_type = pt_common_type_op (arg1_eq_type, op, arg2_eq_type);
5061  if (common_type == PT_TYPE_MAYBE && op != PT_COALESCE)
5062  {
5063  /* "mirror" the known argument type to the other argument if the later is PT_TYPE_MAYBE */
5064  if (!pt_is_op_hv_late_bind (op))
5065  {
5066  if (arg1_eq_type != PT_TYPE_MAYBE)
5067  {
5068  /* then arg2_eq_type is PT_TYPE_MAYBE */
5069  arg2_eq_type = arg1_eq_type;
5070  common_type = arg1_eq_type;
5071  }
5072  else if (arg2_eq_type != PT_TYPE_MAYBE)
5073  {
5074  arg1_eq_type = arg2_eq_type;
5075  common_type = arg2_eq_type;
5076  }
5077  }
5078  }
5079  }
5080  }
5081  if (arg3_eq_type != PT_TYPE_NONE)
5082  {
5083  /* at this point either all arg1_eq_type, arg2_eq_type and common_type are PT_TYPE_MAYBE, or are already set to a
5084  * PT_TYPE_ENUM */
5085  common_type = pt_common_type_op (common_type, op, arg3_eq_type);
5086  if (common_type == PT_TYPE_MAYBE)
5087  {
5088  /* either arg3_eq_type is PT_TYPE_MABYE or arg1, arg2 and common_type were PT_TYPE_MABYE */
5089  if (arg3_eq_type == PT_TYPE_MAYBE)
5090  {
5091  if (arg1_eq_type != PT_TYPE_MAYBE)
5092  {
5093  common_type = arg1_eq_type;
5094  arg3_eq_type = common_type;
5095  }
5096  }
5097  else
5098  {
5099  arg1_eq_type = arg3_eq_type;
5100  arg2_eq_type = arg3_eq_type;
5101  common_type = arg3_eq_type;
5102  }
5103  }
5104  }
5105  if (common_type == PT_TYPE_MAYBE && expected_type != PT_TYPE_NONE && !pt_is_op_hv_late_bind (op))
5106  {
5107  /* if expected type if not PT_TYPE_NONE then a expression higher up in the parser tree has set an expected domain
5108  * for this node and we can use it to set the expected domain of the arguments */
5109  common_type = expected_type;
5110  }
5111 
5112  /* final check : common_type should be PT_TYPE_MAYBE at this stage only for a small number of operators (PLUS,
5113  * MINUS,..) and only when at least one of the arguments is PT_TYPE_MAYBE; -when common type is PT_TYPE_MAYBE, then
5114  * leave all arguments as they are: either TYPE_MAYBE, either a concrete TYPE - without cast. The operator's
5115  * arguments will be resolved at execution in this case -if common type is a concrete type, then force all other
5116  * arguments to the common type (this should apply for most operators) */
5117  if (common_type != PT_TYPE_MAYBE)
5118  {
5119  *arg1 = (arg1_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5120  *arg2 = (arg2_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5121  *arg3 = (arg3_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5122  }
5123  return common_type;
5124 }
5125 
5126 /*
5127  * pt_get_common_collection_type - get the common type of the elements in a
5128  * collection
5129  * return : the common type
5130  * set(in) : the collection
5131  * is_multitype : specifies that there are at least two different types in
5132  * collection's elements.
5133  *
5134  * Note: The PT_TYPE_MAYBE is ignored and all numeric types
5135  * are counted as one.
5136  */
5137 static PT_TYPE_ENUM
5138 pt_get_common_collection_type (const PT_NODE * set, bool * is_multitype)
5139 {
5140  PT_TYPE_ENUM common_type = PT_TYPE_NONE, temp_type = PT_TYPE_NONE;
5141  bool is_multitype_temp = false;
5142  PT_NODE *data_type = NULL;
5143  assert (set != NULL);
5144  assert (PT_IS_COLLECTION_TYPE (set->type_enum));
5145 
5146  if (set->node_type == PT_EXPR && set->info.expr.op == PT_CAST)
5147  {
5148  /* cast nodes keep element information in the cast_type node */
5149  data_type = set->info.expr.cast_type;
5150  }
5151  else
5152  {
5153  data_type = set->data_type;
5154  }
5155 
5156  if (data_type)
5157  {
5158  common_type = data_type->type_enum;
5159  for (data_type = data_type->next; data_type; data_type = data_type->next)
5160  {
5161  temp_type = data_type->type_enum;
5162  if (common_type != temp_type && temp_type != PT_TYPE_MAYBE
5163  && (!PT_IS_NUMERIC_TYPE (temp_type) || !PT_IS_NUMERIC_TYPE (common_type)))
5164  {
5165  is_multitype_temp = true;
5166  }
5167  common_type = pt_common_type (common_type, temp_type);
5168  }
5169  }
5170 
5171  if (is_multitype)
5172  {
5173  *is_multitype = is_multitype_temp;
5174  }
5175 
5176  return common_type;
5177 }
5178 
5179 /*
5180  * pt_is_collection_of_type - check if a node is of type
5181  * collection_type of element_type
5182  * return : true if the node is of type collection of element_type
5183  * node(in) : the node to be checked
5184  * collection_type(in) : the type of the collection (SET, MULTISET, etc)
5185  * element_type(in) : the type of the elements in the collection
5186  */
5187 static bool
5188 pt_is_collection_of_type (const PT_NODE * node, const PT_TYPE_ENUM collection_type, const PT_TYPE_ENUM element_type)
5189 {
5190  PT_NODE *temp = NULL;
5191  assert (node != NULL);
5192 
5193  if (!PT_IS_COLLECTION_TYPE (node->type_enum))
5194  {
5195  /* if it's not a collection return false */
5196  return false;
5197  }
5198 
5199  if (node->type_enum != collection_type)
5200  {
5201  /* if it's not the same type of collection return false */
5202  return false;
5203  }
5204 
5205  for (temp = node->data_type; temp; temp = temp->next)
5206  {
5207  if (element_type != temp->type_enum)
5208  {
5209  /* if collection has an element of different type than element_type return false */
5210  return false;
5211  }
5212  }
5213 
5214  return true;
5215 }
5216 
5217 /*
5218  * pt_coerce_range_expr_arguments - apply signature sig to the arguments of the
5219  * logical expression expr
5220  * return : the (possibly modified) expr or NULL on error
5221  * parser(in) : the parser context
5222  * expr(in) : the SQL expression
5223  * arg1(in) : first argument of the expression
5224  * arg2(in) : second argument of the expression
5225  * arg3(in) : third argument of the expression
5226  * sig(in) : the expression signature
5227  *
5228  */
5229 static PT_NODE *
5232 {
5233  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
5234  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
5235  PT_TYPE_ENUM arg1_eq_type = PT_TYPE_NONE, arg2_eq_type = PT_TYPE_NONE;
5236  PT_TYPE_ENUM arg3_eq_type = PT_TYPE_NONE;
5237  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5238  PT_OP_TYPE op;
5239  int error = NO_ERROR;
5240 
5241  op = expr->info.expr.op;
5242 
5243  arg1 = expr->info.expr.arg1;
5244  if (arg1)
5245  {
5246  arg1_type = arg1->type_enum;
5247  }
5248 
5249  arg2 = expr->info.expr.arg2;
5250  if (arg2)
5251  {
5252  arg2_type = arg2->type_enum;
5253  }
5254 
5255  arg3 = expr->info.expr.arg3;
5256  if (arg3)
5257  {
5258  arg3_type = arg3->type_enum;
5259  }
5260 
5261  arg1_eq_type = pt_get_equivalent_type (sig.arg1_type, arg1_type);
5262  arg2_eq_type = pt_get_equivalent_type (sig.arg2_type, arg2_type);
5263  arg3_eq_type = pt_get_equivalent_type (sig.arg3_type, arg3_type);
5264 
5265  /* for range expressions the second argument may be a collection or a query. */
5266  if (PT_IS_QUERY_NODE_TYPE (arg2->node_type))
5267  {
5268  /* the select list must have only one element and the first argument has to be of the same type as the argument
5269  * from the select list */
5270  PT_NODE *arg2_list = NULL;
5271 
5272  /* duplicates are not relevant; order by is not relevant; */
5274  pt_try_remove_order_by (parser, arg2);
5275 
5276  arg2_list = pt_get_select_list (parser, arg2);
5277  if (PT_IS_COLLECTION_TYPE (arg2_list->type_enum) && arg2_list->node_type == PT_FUNCTION)
5278  {
5279  expr->type_enum = PT_TYPE_LOGICAL;
5280  return expr;
5281  }
5282  if (pt_length_of_select_list (arg2_list, EXCLUDE_HIDDEN_COLUMNS) != 1)
5283  {
5285  return NULL;
5286  }
5287  arg2_type = arg2_list->type_enum;
5288  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
5289  {
5290  /* In case of 'ENUM IN [query]' we need to convert all elements of right argument to the ENUM type in order
5291  * to preserve an eventual index scan on left argument */
5292  common_type = PT_TYPE_ENUMERATION;
5293  }
5294  else
5295  {
5296  common_type = pt_common_type_op (arg1_eq_type, op, arg2_type);
5297  }
5298  if (PT_IS_COLLECTION_TYPE (common_type))
5299  {
5300  /* we cannot make a decision during type checking in this case */
5301  return expr;
5302  }
5303  if ((PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_NUMERIC_TYPE (common_type)) || common_type == PT_TYPE_MAYBE)
5304  {
5305  /* do not cast between numeric types */
5306  arg1_eq_type = arg1_type;
5307  }
5308  else
5309  {
5310  arg1_eq_type = common_type;
5311  }
5312  if ((PT_IS_NUMERIC_TYPE (arg2_type) && PT_IS_NUMERIC_TYPE (common_type)) || common_type == PT_TYPE_MAYBE)
5313  {
5314  arg2_eq_type = arg2_type;
5315  }
5316  else
5317  {
5318  arg2_eq_type = common_type;
5319  }
5320 
5321  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5322  if (error != NO_ERROR)
5323  {
5324  return NULL;
5325  }
5326  else
5327  {
5328  expr->info.expr.arg1 = arg1;
5329  }
5330 
5331  if (pt_wrap_select_list_with_cast_op (parser, arg2, arg2_eq_type, 0, 0, NULL, false) != NO_ERROR)
5332  {
5333  return NULL;
5334  }
5335  }
5336  else if (PT_IS_COLLECTION_TYPE (arg2_type))
5337  {
5338  /* Because we're using collections, semantically, all three cases below are valid: 1. SELECT * FROM tbl WHERE
5339  * int_col in {integer, set, object, date} 2. SELECT * FROM tbl WHERE int_col in {str, str, str} 3. SELECT * FROM
5340  * tbl WHERE int_col in {integer, integer, integer} We will only coerce arg2 if there is a common type between
5341  * arg1 and all elements from the collection arg2. We do not consider the case in which we cannot discern a
5342  * common type to be a semantic error and we rely on the functionality of the comparison operators to be applied
5343  * correctly on the expression during expression evaluation. This means that we consider that the user knew what
5344  * he was doing in the first case but wanted to write something else in the second case. */
5345  PT_TYPE_ENUM collection_type = PT_TYPE_NONE;
5346  bool should_cast = false;
5347  PT_NODE *data_type = NULL;
5348 
5349  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
5350  {
5351  /* In case of 'ENUM IN (...)' we need to convert all elements of right argument to the ENUM type in order to
5352  * preserve an eventual index scan on left argument */
5353  common_type = arg1_eq_type = collection_type = PT_TYPE_ENUMERATION;
5354  }
5355  else
5356  {
5357  common_type = collection_type = pt_get_common_collection_type (arg2, &should_cast);
5358  if (common_type != PT_TYPE_NONE)
5359  {
5360  common_type = pt_common_type_op (arg1_eq_type, op, common_type);
5361  if (common_type != PT_TYPE_NONE)
5362  {
5363  /* collection's common type is STRING type, casting may not be needed */
5364  if (!PT_IS_CHAR_STRING_TYPE (common_type) || !PT_IS_CHAR_STRING_TYPE (arg1_eq_type))
5365  {
5366  arg1_eq_type = common_type;
5367  }
5368  }
5369  }
5370  }
5371  if (common_type == PT_TYPE_OBJECT || PT_IS_COLLECTION_TYPE (common_type))
5372  {
5373  /* we cannot make a cast decision here. to keep backwards compatibility we will call pt_coerce_value which
5374  * will only work on constants */
5375  PT_NODE *temp = NULL, *msg_temp = NULL;
5376  msg_temp = parser->error_msgs;
5377  (void) pt_coerce_value (parser, arg2, arg2, PT_TYPE_SET, arg2->data_type);
5378  if (pt_has_error (parser))
5379  {
5380  /* ignore errors */
5381  parser_free_tree (parser, parser->error_msgs);
5382  parser->error_msgs = msg_temp;
5383  }
5384  if (pt_coerce_value (parser, arg1, arg1, common_type, NULL) != NO_ERROR)
5385  {
5386  expr->type_enum = PT_TYPE_NONE;
5387  }
5388  if (arg2->node_type == PT_VALUE)
5389  {
5390  for (temp = arg2->info.value.data_value.set; temp; temp = temp->next)
5391  {
5392  if (common_type != temp->type_enum)
5393  {
5394  msg_temp = parser->error_msgs;
5395  parser->error_msgs = NULL;
5396  (void) pt_coerce_value (parser, temp, temp, common_type, NULL);
5397  if (pt_has_error (parser))
5398  {
5399  parser_free_tree (parser, parser->error_msgs);
5400  }
5401  parser->error_msgs = msg_temp;
5402  }
5403  }
5404  }
5405 
5406  return expr;
5407  }
5408 
5409  if (PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_NUMERIC_TYPE (arg1_eq_type))
5410  {
5411  /* do not cast between numeric types */
5412  arg1_eq_type = arg1_type;
5413  }
5414  else
5415  {
5416  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5417  if (arg1 == NULL)
5418  {
5419  return NULL;
5420  }
5421  expr->info.expr.arg1 = arg1;
5422  }
5423 
5424  /* verify if we should cast arg2 to appropriate type */
5425  if (common_type == PT_TYPE_NONE /* check if there is a valid common type between members of arg2 and arg1. */
5426  || (!should_cast /* check if there are at least two different types in arg2 */
5427  && (collection_type == common_type
5428  || (PT_IS_NUMERIC_TYPE (collection_type) && PT_IS_NUMERIC_TYPE (common_type))
5429  || (PT_IS_CHAR_STRING_TYPE (collection_type) && PT_IS_CHAR_STRING_TYPE (common_type)))))
5430  {
5431  return expr;
5432  }
5433 
5434  /* we can perform an implicit cast here */
5435  data_type = parser_new_node (parser, PT_DATA_TYPE);
5436  if (data_type == NULL)
5437  {
5438  return NULL;
5439  }
5440  data_type->info.data_type.dec_precision = 0;
5441  data_type->info.data_type.precision = 0;
5442  data_type->type_enum = common_type;
5443  if (PT_IS_PARAMETERIZED_TYPE (common_type))
5444  {
5445  PT_NODE *temp = NULL;
5446  int precision = 0, scale = 0;
5447  int units = LANG_SYS_CODESET; /* code set */
5448  int collation_id = LANG_SYS_COLLATION; /* collation_id */
5449  bool keep_searching = true;
5450  for (temp = arg2->data_type; temp != NULL && keep_searching; temp = temp->next)
5451  {
5452  if (temp->type_enum == PT_TYPE_NULL)
5453  {
5454  continue;
5455  }
5456 
5457  switch (common_type)
5458  {
5459  case PT_TYPE_CHAR:
5460  case PT_TYPE_NCHAR:
5461  case PT_TYPE_BIT:
5462  /* CHAR, NCHAR types can be common type for one of all arguments is string type */
5463  if (precision < temp->info.data_type.precision)
5464  {
5465  precision = temp->info.data_type.precision;
5466  }
5467  break;
5468  case PT_TYPE_VARCHAR:
5469  case PT_TYPE_VARNCHAR:
5470  /* either all elements are already of string types or we set maximum precision */
5471  if (!PT_IS_CHAR_STRING_TYPE (temp->type_enum))
5472  {
5473  precision = TP_FLOATING_PRECISION_VALUE;
5474  /* no need to look any further, we've already found a type for which we have to set maximum
5475  * precision */
5476  keep_searching = false;
5477  break;
5478  }
5479  if (precision < temp->info.data_type.precision)
5480  {
5481  precision = temp->info.data_type.precision;
5482  }
5483  break;
5484  case PT_TYPE_VARBIT:
5485  /* either all elements are already of bit types or we set maximum precision */
5486  if (!PT_IS_BIT_STRING_TYPE (temp->type_enum))
5487  {
5488  precision = TP_FLOATING_PRECISION_VALUE;
5489  /* no need to look any further, we've already found a type for which we have to set maximum
5490  * precision */
5491  keep_searching = false;
5492  break;
5493  }
5494  if (precision < temp->info.data_type.precision)
5495  {
5496  precision = temp->info.data_type.precision;
5497  }
5498  break;
5499  case PT_TYPE_NUMERIC:
5500  /* either all elements are numeric or all are discrete numbers */
5501  if (temp->type_enum == PT_TYPE_NUMERIC)
5502  {
5503  if (precision < temp->info.data_type.precision)
5504  {
5505  precision = temp->info.data_type.precision;
5506  }
5507  if (scale < temp->info.data_type.dec_precision)
5508  {
5509  scale = temp->info.data_type.dec_precision;
5510  }
5511  }
5512  else if (PT_IS_DISCRETE_NUMBER_TYPE (temp->type_enum))
5513  {
5514  if (precision < TP_BIGINT_PRECISION)
5515  {
5516  precision = TP_BIGINT_PRECISION;
5517  }
5518  }
5519  else
5520  {
5521  assert (false);
5522  }
5523  break;
5524 
5525  default:
5526  assert (false);
5527  break;
5528  }
5529 
5530  if (PT_IS_STRING_TYPE (common_type) && PT_IS_STRING_TYPE (temp->type_enum))
5531  {
5532  /* A bigger codesets's number can represent more characters. */
5533  /* to_do : check to use functions pt_common_collation() or pt_make_cast_with_compatble_info(). */
5534  if (units < temp->info.data_type.units)
5535  {
5536  units = temp->info.data_type.units;
5537  collation_id = temp->info.data_type.collation_id;
5538  }
5539  }
5540  }
5541  data_type->info.data_type.precision = precision;
5542  data_type->info.data_type.dec_precision = scale;
5543  data_type->info.data_type.units = units;
5544  data_type->info.data_type.collation_id = collation_id;
5545  }
5546 
5547  arg2 = pt_wrap_collection_with_cast_op (parser, arg2, sig.arg2_type.val.type, data_type, false);
5548  if (!arg2)
5549  {
5550  return NULL;
5551  }
5552 
5553  expr->info.expr.arg2 = arg2;
5554  }
5555  else if (PT_IS_HOSTVAR (arg2))
5556  {
5558  SET_EXPECTED_DOMAIN (arg2, d);
5559  pt_preset_hostvar (parser, arg2);
5560 
5561  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5562  if (error != NO_ERROR)
5563  {
5564  return NULL;
5565  }
5566  expr->info.expr.arg1 = arg1;
5567  }
5568  else
5569  {
5570  /* This is a semantic error */
5571  return NULL;
5572  }
5573  return expr;
5574 }
5575 
5576 /*
5577  * pt_coerce_expr_arguments - apply signature sig to the arguments of the
5578  * expression expr
5579  * return : the (possibly modified) expr or NULL on error
5580  * parser(in) : the parser context
5581  * expr(in) : the SQL expression
5582  * arg1(in) : first argument of the expression
5583  * arg2(in) : second argument of the expression
5584  * arg3(in) : third argument of the expression
5585  * sig(in) : the expression signature
5586  */
5587 static PT_NODE *
5590 {
5591  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
5592  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
5593  PT_TYPE_ENUM arg1_eq_type = PT_TYPE_NONE, arg2_eq_type = PT_TYPE_NONE;
5594  PT_TYPE_ENUM arg3_eq_type = PT_TYPE_NONE;
5595  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5596  PT_NODE *arg1_dt = NULL;
5597  PT_NODE *arg2_dt = NULL;
5598  PT_NODE *arg3_dt = NULL;
5599  PT_OP_TYPE op;
5600  int error = NO_ERROR;
5601  PT_NODE *between = NULL;
5602  PT_NODE *between_ge_lt = NULL;
5603  PT_NODE *b1 = NULL;
5604  PT_NODE *b2 = NULL;
5605 
5606  op = expr->info.expr.op;
5607 
5608  arg1 = expr->info.expr.arg1;
5609  if (arg1)
5610  {
5611  arg1_type = arg1->type_enum;
5612  }
5613 
5614  arg2 = expr->info.expr.arg2;
5615  if (arg2)
5616  {
5617  arg2_type = arg2->type_enum;
5618 
5619  if (op == PT_WIDTH_BUCKET)
5620  {
5621  arg2_type = pt_get_common_arg_type_of_width_bucket (parser, expr);
5622  }
5623  }
5624 
5625  arg3 = expr->info.expr.arg3;
5626  if (arg3)
5627  {
5628  arg3_type = arg3->type_enum;
5629  }
5630 
5631  arg1_eq_type = pt_get_equivalent_type_with_op (sig.arg1_type, arg1_type, op);
5632  arg2_eq_type = pt_get_equivalent_type_with_op (sig.arg2_type, arg2_type, op);
5633  arg3_eq_type = pt_get_equivalent_type_with_op (sig.arg3_type, arg3_type, op);
5634 
5635  if (pt_is_symmetric_op (op))
5636  {
5637  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
5638  {
5639  /* In case of 'ENUM = const' we need to convert the right argument to the ENUM type in order to preserve an
5640  * eventual index scan on left argument */
5641  common_type = PT_TYPE_ENUMERATION;
5642  }
5643  else
5644  {
5645  /* We should make sure that, for symmetric operators, all arguments are of the same type. */
5646  common_type = pt_infer_common_type (op, &arg1_eq_type, &arg2_eq_type, &arg3_eq_type, expr->expected_domain);
5647  }
5648 
5649  if (common_type == PT_TYPE_NONE)
5650  {
5651  /* this is an error */
5652  return NULL;
5653  }
5654 
5655  if (pt_is_range_or_comp (op)
5656  && (!PT_IS_NUMERIC_TYPE (arg1_type) || !PT_IS_NUMERIC_TYPE (arg2_type)
5657  || (arg3_type != PT_TYPE_NONE && !PT_IS_NUMERIC_TYPE (arg3_type))))
5658  {
5659  /* cast value when comparing column with value */
5660  if (PT_IS_NAME_NODE (arg1) && PT_IS_VALUE_NODE (arg2)
5661  && (arg3_type == PT_TYPE_NONE || PT_IS_VALUE_NODE (arg3)) && arg1_type != PT_TYPE_ENUMERATION)
5662  {
5663  arg1_eq_type = arg2_eq_type = arg1_type;
5664  if (arg3_type != PT_TYPE_NONE)
5665  {
5666  arg3_eq_type = arg1_type;
5667  }
5668 
5669  /* if column type is number (except NUMERIC) and operator is not EQ cast const node to DOUBLE to enhance
5670  * correctness of results */
5671  if (PT_IS_NUMERIC_TYPE (arg1_type) && arg1_type != PT_TYPE_NUMERIC && op != PT_EQ && op != PT_EQ_SOME
5672  && op != PT_EQ_ALL)
5673  {
5674  if (arg2_type != arg1_type)
5675  {
5676  arg2_eq_type = PT_TYPE_DOUBLE;
5677  }
5678  if (arg3_type != PT_TYPE_NONE && arg3_type != arg1_type)
5679  {
5680  arg3_eq_type = PT_TYPE_DOUBLE;
5681  }
5682  }
5683  }
5684  else if (PT_IS_NAME_NODE (arg2) && PT_IS_VALUE_NODE (arg1) && arg3_type == PT_TYPE_NONE
5685  && arg2_type != PT_TYPE_ENUMERATION)
5686  {
5687  arg1_eq_type = arg2_eq_type = arg2_type;
5688  if (arg1_type != arg2_type && PT_IS_NUMERIC_TYPE (arg2_type) && arg2_type != PT_TYPE_NUMERIC
5689  && op != PT_EQ && op != PT_EQ_SOME && op != PT_EQ_ALL)
5690  {
5691  arg1_eq_type = PT_TYPE_DOUBLE;
5692  }
5693  }
5694  }
5695 
5696  if (pt_is_comp_op (op))
5697  {
5698  /* do not cast between numeric types or char types for comparison operators */
5699  if (PT_ARE_COMPARABLE (arg1_eq_type, arg1_type))
5700  {
5701  arg1_eq_type = arg1_type;
5702  }
5703  if (PT_ARE_COMPARABLE (arg2_eq_type, arg2_type))
5704  {
5705  arg2_eq_type = arg2_type;
5706  }
5707  if (PT_ARE_COMPARABLE (arg3_eq_type, arg3_type))
5708  {
5709  arg3_eq_type = arg3_type;
5710  }
5711  }
5712 
5713  if (PT_IS_COLLECTION_TYPE (common_type))
5714  {
5715  /* We do not perform implicit casts on collection types. The following code will only handle constant
5716  * arguments. */
5717  if ((arg1_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg1_eq_type))
5718  || (arg2_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg2_eq_type))
5719  || (arg3_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg3_eq_type)))
5720  {
5721  return NULL;
5722  }
5723  else
5724  {
5725  if (pt_is_symmetric_type (common_type))
5726  {
5727  if (arg1_eq_type != common_type && arg1_eq_type != PT_TYPE_NONE)
5728  {
5729  pt_coerce_value (parser, arg1, arg1, common_type, PT_NODE_DATA_TYPE (arg1));
5730  arg1_type = common_type;
5731  }
5732  if (arg2_type != common_type && arg2_eq_type != PT_TYPE_NONE)
5733  {
5734  pt_coerce_value (parser, arg2, arg2, common_type, PT_NODE_DATA_TYPE (arg2));
5735  arg2_type = common_type;
5736  }
5737  }
5738  /* we're not casting collection types in this context but we should "propagate" types */
5739  if (arg2_type != PT_TYPE_NONE)
5740  {
5741  pt_propagate_types (parser, expr, PT_NODE_DATA_TYPE (arg1), PT_NODE_DATA_TYPE (arg2));
5742  }
5743  if (arg3_type != PT_TYPE_NONE)
5744  {
5745  pt_propagate_types (parser, expr, PT_NODE_DATA_TYPE (arg2), PT_NODE_DATA_TYPE (arg3));
5746  }
5747  expr->info.expr.arg1 = arg1;
5748  expr->info.expr.arg2 = arg2;
5749  expr->info.expr.arg3 = arg3;
5750  return expr;
5751  }
5752  }
5753  }
5754 
5755  /* We might have decided a new type for arg1 based on the common_type but, if the signature defines an exact type, we
5756  * should keep it. For example, + is a symmetric operator but also defines date + bigint which is not symmetric and
5757  * we have to keep the bigint type even if the common type is date. This is why, before coercing expression
5758  * arguments, we check the signature that we decided to apply */
5759  if (sig.arg1_type.type == pt_arg_type::NORMAL)
5760  {
5761  arg1_eq_type = sig.arg1_type.val.type;
5762  }
5764  {
5765  /* In case of recursive expression (PT_GREATEST, PT_LEAST, ...) the common type is stored in recursive_type */
5766  arg1_eq_type = expr->info.expr.recursive_type;
5767  }
5768  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, arg1_dt);
5769  if (error != NO_ERROR)
5770  {
5771  return NULL;
5772  }
5773  else
5774  {
5775  expr->info.expr.arg1 = arg1;
5776  }
5777 
5778  if (sig.arg2_type.type == pt_arg_type::NORMAL)
5779  {
5780  arg2_eq_type = sig.arg2_type.val.type;
5781  }
5783  {
5784  /* In case of recursive expression (PT_GREATEST, PT_LEAST, ...) the common type is stored in recursive_type */
5785  arg2_eq_type = expr->info.expr.recursive_type;
5786  }
5787 
5788  if (op != PT_WIDTH_BUCKET)
5789  {
5790  error = pt_coerce_expression_argument (parser, expr, &arg2, arg2_eq_type, arg2_dt);
5791  if (error != NO_ERROR)
5792  {
5793  return NULL;
5794  }
5795  else
5796  {
5797  expr->info.expr.arg2 = arg2;
5798  }
5799  }
5800  else
5801  {
5802  /* width_bucket is a special case. It has 4 params the 2nd and 3rd args are coerced here */
5803  between = expr->info.expr.arg2;
5804  if (between == NULL || between->node_type != PT_EXPR || between->info.expr.op != PT_BETWEEN)
5805  {
5806  return NULL;
5807  }
5808 
5809  between_ge_lt = between->info.expr.arg2;
5810  if (between_ge_lt == NULL || between_ge_lt->node_type != PT_EXPR
5811  || between_ge_lt->info.expr.op != PT_BETWEEN_GE_LT)
5812  {
5813  return NULL;
5814  }
5815 
5816  /* 2nd, 3rd param of width_bucket */
5817  b1 = between_ge_lt->info.expr.arg1;
5818  b2 = between_ge_lt->info.expr.arg2;
5819  assert (b1 != NULL && b2 != NULL);
5820 
5821  error = pt_coerce_expression_argument (parser, between_ge_lt, &b1, arg2_eq_type, arg1_dt);
5822  if (error != NO_ERROR)
5823  {
5824  return NULL;
5825  }
5826  else
5827  {
5828  between_ge_lt->info.expr.arg1 = b1;
5829  }
5830 
5831  error = pt_coerce_expression_argument (parser, between_ge_lt, &b2, arg2_eq_type, arg2_dt);
5832  if (error != NO_ERROR)
5833  {
5834  return NULL;
5835  }
5836  else
5837  {
5838  between_ge_lt->info.expr.arg2 = b2;
5839  }
5840  }
5841 
5842  if (sig.arg3_type.type == pt_arg_type::NORMAL)
5843  {
5844  arg3_eq_type = sig.arg3_type.val.type;
5845  }
5846  error = pt_coerce_expression_argument (parser, expr, &arg3, arg3_eq_type, arg3_dt);
5847  if (error != NO_ERROR)
5848  {
5849  return NULL;
5850  }
5851  else
5852  {
5853  expr->info.expr.arg3 = arg3;
5854  }
5855 
5856  return expr;
5857 }
5858 
5859 /*
5860  * pt_is_range_expression () - return true if the expression is evaluated
5861  * as a logical expression
5862  * return : true if the expression is of type logical
5863  * op (in) : the expression
5864  */
5865 static bool
5867 {
5868  switch (op)
5869  {
5870  case PT_GE_SOME:
5871  case PT_GT_SOME:
5872  case PT_LT_SOME:
5873  case PT_LE_SOME:
5874  case PT_GE_ALL:
5875  case PT_GT_ALL:
5876  case PT_LT_ALL:
5877  case PT_LE_ALL:
5878  case PT_EQ_SOME:
5879  case PT_NE_SOME:
5880  case PT_EQ_ALL:
5881  case PT_NE_ALL:
5882  case PT_IS_IN:
5883  case PT_IS_NOT_IN:
5884  return true;
5885  default:
5886  return false;
5887  }
5888  return false;
5889 }
5890 
5891 /*
5892  * pt_is_range_or_comp () - return true if the operator is range or comparison
5893  * return:
5894  * op(in):
5895  */
5896 static bool
5898 {
5899  switch (op)
5900  {
5901  case PT_EQ:
5902  case PT_NE:
5903  case PT_GE:
5904  case PT_GT:
5905  case PT_LT:
5906  case PT_LE:
5907  case PT_NULLSAFE_EQ:
5908  case PT_GT_INF:
5909  case PT_LT_INF:
5910  case PT_BETWEEN:
5911  case PT_BETWEEN_GE_LE:
5912  case PT_BETWEEN_GE_LT:
5913  case PT_BETWEEN_GT_LE:
5914  case PT_BETWEEN_GT_LT:
5915  case PT_BETWEEN_INF_LE:
5916  case PT_BETWEEN_INF_LT:
5917  case PT_BETWEEN_GE_INF:
5918  case PT_BETWEEN_GT_INF:
5919  return true;
5920  default:
5921  return false;
5922  }
5923 }
5924 
5925 static bool
5927 {
5928  if (pt_is_operator_logical (op))
5929  {
5930  return true;
5931  }
5932 
5933  switch (op)
5934  {
5935  case PT_NVL:
5936  case PT_IFNULL:
5937  case PT_ISNULL:
5938  case PT_NVL2:
5939  case PT_COALESCE:
5940  case PT_NULLIF:
5941  case PT_TRANSLATE:
5942  case PT_RAND:
5943  case PT_DRAND:
5944  case PT_RANDOM:
5945  case PT_DRANDOM:
5946  case PT_CONCAT:
5947  case PT_CONCAT_WS:
5948  case PT_TO_CHAR:
5949  return true;
5950  case PT_REPLACE:
5952  default:
5953  return false;
5954  }
5955 }
5956 
5957 /*
5958  * pt_apply_expressions_definition () - evaluate which expression signature
5959  * best matches the received arguments
5960  * and cast arguments to the types
5961  * described in the signature
5962  * return : NO_ERROR or error code
5963  * parser(in) : the parser context
5964  * node(in/out): an SQL expression
5965  */
5966 static int
5968 {
5969  PT_OP_TYPE op;
5970  PT_NODE *arg1 = NULL, *arg2 = NULL, *arg3 = NULL;
5971  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
5972  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
5974  PT_NODE *expr = *node;
5975  int matches = 0, best_match = -1, i = 0;
5977 
5978  if (expr->node_type != PT_EXPR)
5979  {
5980  return NO_ERROR;
5981  }
5982  op = expr->info.expr.op;
5983  if (!pt_get_expression_definition (op, &def))
5984  {
5985  *node = NULL;
5986  return NO_ERROR;
5987  }
5988 
5990  {
5991  arg1_type = arg2_type = expr->info.expr.recursive_type;
5992  }
5993  else
5994  {
5995  arg1 = expr->info.expr.arg1;
5996  if (arg1)
5997  {
5998  arg1_type = arg1->type_enum;
5999  }
6000 
6001  arg2 = expr->info.expr.arg2;
6002  if (arg2)
6003  {
6004  arg2_type = arg2->type_enum;
6005 
6006  if (op == PT_WIDTH_BUCKET)
6007  {
6008  arg2_type = pt_get_common_arg_type_of_width_bucket (parser, expr);
6009  }
6010  }
6011  }
6012 
6013  arg3 = expr->info.expr.arg3;
6014  if (arg3)
6015  {
6016  arg3_type = arg3->type_enum;
6017  }
6018 
6019  /* check the expression contains NULL argument. If the op does not specially treat NULL args, for instance, NVL,
6020  * NVL2, IS [NOT] NULL and so on, just decide the retun type as NULL. */
6022  && ((arg1 && !arg1->flag.is_added_by_parser && arg1_type == PT_TYPE_NULL)
6023  || (arg2 && !arg2->flag.is_added_by_parser && arg2_type == PT_TYPE_NULL)
6024  || (arg3 && !arg3->flag.is_added_by_parser && arg3_type == PT_TYPE_NULL)))
6025  {
6026  expr->type_enum = PT_TYPE_NULL;
6027  return NO_ERROR;
6028  }
6029 
6030  matches = -1;
6031  best_match = 0;
6032  for (i = 0; i < def.overloads_count; i++)
6033  {
6034  int match_cnt = 0;
6035  if (pt_are_unmatchable_types (def.overloads[i].arg1_type, arg1_type))
6036  {
6037  match_cnt = -1;
6038  continue;
6039  }
6040  if (pt_are_equivalent_types (def.overloads[i].arg1_type, arg1_type))
6041  {
6042  match_cnt++;
6043  }
6044  if (pt_are_unmatchable_types (def.overloads[i].arg2_type, arg2_type))
6045  {
6046  match_cnt = -1;
6047  continue;
6048  }
6049  if (pt_are_equivalent_types (def.overloads[i].arg2_type, arg2_type))
6050  {
6051  match_cnt++;
6052  }
6053  if (pt_are_unmatchable_types (def.overloads[i].arg3_type, arg3_type))
6054  {
6055  match_cnt = -1;
6056  continue;
6057  }
6058  if (pt_are_equivalent_types (def.overloads[i].arg3_type, arg3_type))
6059  {
6060  match_cnt++;
6061  }
6062  if (match_cnt == 3)
6063  {
6064  best_match = i;
6065  break;
6066  }
6067  else if (match_cnt > matches)
6068  {
6069  matches = match_cnt;
6070  best_match = i;
6071  }
6072  }
6073 
6074  if (best_match == -1)
6075  {
6076  /* if best_match is -1 then we have an expression definition but it cannot be applied on this arguments. */
6077  expr->node_type = PT_NODE_NONE;
6078  return ER_FAILED;
6079  }
6080 
6081  sig = def.overloads[best_match];
6082  if (pt_is_range_expression (op))
6083  {
6084  /* Range expressions are expressions that compare an argument with a subquery or a collection. We handle these
6085  * expressions separately */
6086  expr = pt_coerce_range_expr_arguments (parser, expr, arg1, arg2, arg3, sig);
6087  }
6088  else
6089  {
6090  expr = pt_coerce_expr_arguments (parser, expr, arg1, arg2, arg3, sig);
6091  }
6092  if (expr == NULL)
6093  {
6094  return ER_FAILED;
6095  }
6096 
6097  if (pt_is_op_hv_late_bind (op)
6098  && (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE))
6099  {
6100  expr->type_enum = PT_TYPE_MAYBE;
6101  }
6102  else
6103  {
6104  expr->type_enum = pt_expr_get_return_type (expr, sig);
6105  }
6106 
6107  /* re-read arguments to include the wrapped-cast */
6108  arg1 = expr->info.expr.arg1;
6109  arg2 = expr->info.expr.arg2;
6110 
6112  && pt_upd_domain_info (parser, arg1, arg2, op, expr->type_enum, expr) != NO_ERROR)
6113  {
6114  expr = NULL;
6115  return ER_FAILED;
6116  }
6117  *node = expr;
6118  return NO_ERROR;
6119 }
6120 
6121 /*
6122  * pt_expr_get_return_type () - get the return type of an expression based on
6123  * the expression signature and the types of its
6124  * arguments
6125  * return : the expression return type
6126  * expr(in): an SQL expression
6127  * sig(in) : the expression signature
6128  *
6129  * Remarks: This function is called after the expression signature has been
6130  * decided and the expression arguments have been wrapped with CASTs
6131  * to the type described in the signature. At this point we can
6132  * decide the return type based on the argument types which are
6133  * proper CUBRID types (i.e.: not generic types)
6134  */
6135 static PT_TYPE_ENUM
6137 {
6138  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
6139  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
6140 
6142  {
6143  /* if the signature does not define a generic type, return the defined type */
6144  return sig.return_type.val.type;
6145  }
6146 
6147  if (expr->info.expr.arg1)
6148  {
6149  arg1_type = expr->info.expr.arg1->type_enum;
6150  if (arg1_type == PT_TYPE_MAYBE)
6151  {
6152  if (expr->info.expr.arg1->expected_domain)
6153  {
6154  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6155  * type */
6156  arg1_type = pt_db_to_type_enum (expr->info.expr.arg1->expected_domain->type->id);
6157  }
6158  }
6159  }
6160 
6161  if (expr->info.expr.arg2)
6162  {
6163  arg2_type = expr->info.expr.arg2->type_enum;
6164  if (arg2_type == PT_TYPE_MAYBE)
6165  {
6166  if (expr->info.expr.arg2->expected_domain)
6167  {
6168  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6169  * type */
6170  arg2_type = pt_db_to_type_enum (expr->info.expr.arg2->expected_domain->type->id);
6171  }
6172  }
6173  }
6174 
6175  if (expr->info.expr.arg3)
6176  {
6177  arg3_type = expr->info.expr.arg3->type_enum;
6178  if (arg3_type == PT_TYPE_MAYBE)
6179  {
6180  if (expr->info.expr.arg3->expected_domain)
6181  {
6182  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6183  * type */
6184  arg3_type = pt_db_to_type_enum (expr->info.expr.arg3->expected_domain->type->id);
6185  }
6186  }
6187  }
6188 
6189  /* the return type of the signature is a generic type */
6190  switch (sig.return_type.val.generic_type)
6191  {
6193  {
6194  /* The return type might be CHAR, VARCHAR, NCHAR or VARNCHAR. Since not all arguments are required to be of
6195  * string type, we have to infer the return type based only on the string type arguments */
6196  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
6197  if (PT_IS_STRING_TYPE (arg1_type))
6198  {
6199  common_type = arg1_type;
6200  if (PT_IS_STRING_TYPE (arg2_type))
6201  {
6202  common_type = pt_common_type (arg1_type, arg2_type);
6203  if (PT_IS_STRING_TYPE (arg3_type))
6204  {
6205  common_type = pt_common_type (common_type, arg3_type);
6206  }
6207  return common_type;
6208  }
6209 
6210  if (PT_IS_STRING_TYPE (arg3_type))
6211  {
6212  common_type = pt_common_type (common_type, arg3_type);
6213  }
6214  return common_type;
6215  }
6216  /* arg1 is not string type */
6217  if (PT_IS_STRING_TYPE (arg2_type))
6218  {
6219  common_type = arg2_type;
6220  if (PT_IS_STRING_TYPE (arg3_type))
6221  {
6222  common_type = pt_common_type (common_type, arg3_type);
6223  }
6224  return common_type;
6225  }
6226 
6227  /* arg1 and arg2 are not of string type */
6228  if (PT_IS_STRING_TYPE (arg3_type))
6229  {
6230  return arg3_type;
6231  }
6232 
6233  if (common_type != PT_TYPE_NONE)
6234  {
6235  return common_type;
6236  }
6237  break;
6238  }
6239 
6241  {
6243  /* if one or the arguments is of national string type the return type must be VARNCHAR, else it is VARCHAR */
6244  if (pt_are_equivalent_types (type, arg1_type) || pt_are_equivalent_types (type, arg2_type)
6245  || pt_are_equivalent_types (type, arg3_type))
6246  {
6247  return PT_TYPE_VARNCHAR;
6248  }
6249  return PT_TYPE_VARCHAR;
6250  }
6251 
6252  case PT_GENERIC_TYPE_CHAR:
6253  if (arg1_type == PT_TYPE_VARCHAR || arg2_type == PT_TYPE_VARCHAR || arg3_type == PT_TYPE_VARCHAR)
6254  {
6255  return PT_TYPE_VARCHAR;
6256  }
6257  return PT_TYPE_CHAR;
6258 
6259  case PT_GENERIC_TYPE_NCHAR:
6260  if (arg1_type == PT_TYPE_VARNCHAR || arg2_type == PT_TYPE_VARNCHAR || arg3_type == PT_TYPE_VARNCHAR)
6261  {
6262  return PT_TYPE_VARNCHAR;
6263  }
6264  return PT_TYPE_NCHAR;
6265 
6268  case PT_GENERIC_TYPE_ANY:
6269  case PT_GENERIC_TYPE_DATE:
6272  case PT_GENERIC_TYPE_BIT:
6273  {
6274  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
6275  if (arg2_type == PT_TYPE_NONE
6277  {
6278  return arg1_type;
6279  }
6280  common_type = pt_common_type (arg1_type, arg2_type);
6281  if (arg3_type != PT_TYPE_NONE)
6282  {
6283  common_type = pt_common_type (common_type, arg3_type);
6284  }
6285 
6286  if (common_type != PT_TYPE_NONE)
6287  {
6288  return common_type;
6289  }
6290  break;
6291  }
6292  default:
6293  break;
6294  }
6295 
6296  /* we might reach this point on expressions with a single argument of value null */
6297  if (arg1_type == PT_TYPE_NULL)
6298  {
6299  return PT_TYPE_NULL;
6300  }
6301  return PT_TYPE_NONE;
6302 }
6303 
6304 /*
6305  * pt_is_symmetric_type () -
6306  * return:
6307  * type_enum(in):
6308  */
6309 static bool
6311 {
6312  switch (type_enum)
6313  {
6314  case PT_TYPE_INTEGER:
6315  case PT_TYPE_BIGINT:
6316  case PT_TYPE_FLOAT:
6317  case PT_TYPE_DOUBLE:
6318  case PT_TYPE_NUMERIC:
6319  case PT_TYPE_SMALLINT:
6320  case PT_TYPE_MONETARY:
6321 
6322  case PT_TYPE_LOGICAL:
6323 
6324  case PT_TYPE_SET:
6325  case PT_TYPE_MULTISET:
6326  case PT_TYPE_SEQUENCE:
6327  case PT_TYPE_OBJECT:
6328 
6329  case PT_TYPE_VARCHAR:
6330  case PT_TYPE_CHAR:
6331  case PT_TYPE_VARNCHAR:
6332  case PT_TYPE_NCHAR:
6333  case PT_TYPE_VARBIT:
6334  case PT_TYPE_BIT:
6335  return true;
6336 
6337  default:
6338  return false;
6339  }
6340 }
6341 
6342 /*
6343  * pt_is_symmetric_op () -
6344  * return:
6345  * op(in):
6346  */
6347 bool
6349 {
6350  switch (op)
6351  {
6352  case PT_FUNCTION_HOLDER:
6353  case PT_ASSIGN:
6354  case PT_GE_SOME:
6355  case PT_GT_SOME:
6356  case PT_LT_SOME:
6357  case PT_LE_SOME:
6358  case PT_GE_ALL:
6359  case PT_GT_ALL:
6360  case PT_LT_ALL:
6361  case PT_LE_ALL:
6362  case PT_EQ_SOME:
6363  case PT_NE_SOME:
6364  case PT_EQ_ALL:
6365  case PT_NE_ALL:
6366  case PT_IS_IN:
6367  case PT_IS_NOT_IN:
6368  case PT_IS_NULL:
6369  case PT_IS_NOT_NULL:
6370  case PT_POSITION:
6371  case PT_FINDINSET:
6372  case PT_SUBSTRING:
6373  case PT_SUBSTRING_INDEX:
6374  case PT_OCTET_LENGTH:
6375  case PT_BIT_LENGTH:
6376  case PT_CHAR_LENGTH:
6377  case PT_BIN:
6378  case PT_TRIM:
6379  case PT_LTRIM:
6380  case PT_RTRIM:
6381  case PT_LIKE_LOWER_BOUND:
6382  case PT_LIKE_UPPER_BOUND:
6383  case PT_LPAD:
6384  case PT_RPAD:
6385  case PT_REPEAT:
6386  case PT_REPLACE:
6387  case PT_TRANSLATE:
6388  case PT_ADD_MONTHS:
6389  case PT_LAST_DAY:
6390  case PT_MONTHS_BETWEEN:
6391  case PT_SYS_DATE:
6392  case PT_CURRENT_DATE:
6393  case PT_SYS_TIME:
6394  case PT_CURRENT_TIME:
6395  case PT_SYS_TIMESTAMP:
6396  case PT_CURRENT_TIMESTAMP:
6397  case PT_SYS_DATETIME:
6398  case PT_CURRENT_DATETIME:
6399  case PT_UTC_TIME:
6400  case PT_UTC_DATE:
6401  case PT_TO_CHAR:
6402  case PT_TO_DATE:
6403  case PT_TO_TIME:
6404  case PT_TO_TIMESTAMP:
6405  case PT_TO_DATETIME:
6406  case PT_TO_NUMBER:
6407  case PT_CURRENT_VALUE:
6408  case PT_NEXT_VALUE:
6409  case PT_CAST:
6410  case PT_EXTRACT:
6411  case PT_INST_NUM:
6412  case PT_ROWNUM:
6413  case PT_ORDERBY_NUM:
6414  case PT_CONNECT_BY_ISCYCLE:
6415  case PT_CONNECT_BY_ISLEAF:
6416  case PT_LEVEL:
6417  case PT_CONNECT_BY_ROOT:
6419  case PT_QPRIOR:
6420  case PT_CURRENT_USER:
6422  case PT_CHR:
6423  case PT_ROUND:
6424  case PT_TRUNC:
6425  case PT_INSTR:
6426  case PT_TIME_FORMAT:
6427  case PT_TIMESTAMP:
6428  case PT_TIMEF:
6429  case PT_YEARF:
6430  case PT_MONTHF:
6431  case PT_DAYF:
6432  case PT_DAYOFMONTH:
6433  case PT_HOURF:
6434  case PT_MINUTEF:
6435  case PT_SECONDF:
6436  case PT_QUARTERF:
6437  case PT_WEEKDAY:
6438  case PT_DAYOFWEEK:
6439  case PT_DAYOFYEAR:
6440  case PT_TODAYS:
6441  case PT_FROMDAYS:
6442  case PT_TIMETOSEC:
6443  case PT_SECTOTIME:
6444  case PT_WEEKF:
6445  case PT_MAKETIME:
6446  case PT_MAKEDATE:
6447  case PT_ADDTIME:
6448  case PT_SCHEMA:
6449  case PT_DATABASE:
6450  case PT_VERSION:
6451  case PT_UNIX_TIMESTAMP:
6452  case PT_FROM_UNIXTIME:
6453  case PT_IS:
6454  case PT_IS_NOT:
6455  case PT_CONCAT:
6456  case PT_CONCAT_WS:
6457  case PT_FIELD:
6458  case PT_LEFT:
6459  case PT_RIGHT:
6460  case PT_LOCATE:
6461  case PT_MID:
6462  case PT_REVERSE:
6463  case PT_DISK_SIZE:
6464  case PT_ADDDATE:
6465  case PT_DATE_ADD:
6466  case PT_SUBDATE:
6467  case PT_DATE_SUB:
6468  case PT_FORMAT:
6469  case PT_ATAN2:
6470  case PT_DATE_FORMAT:
6471  case PT_USER:
6472  case PT_STR_TO_DATE:
6473  case PT_LIST_DBS:
6474  case PT_SYS_GUID:
6475  case PT_IF:
6476  case PT_POWER:
6477  case PT_BIT_TO_BLOB:
6478  case PT_BLOB_FROM_FILE:
6479  case PT_BLOB_LENGTH:
6480  case PT_BLOB_TO_BIT:
6481  case PT_CHAR_TO_BLOB:
6482  case PT_CHAR_TO_CLOB:
6483  case PT_CLOB_FROM_FILE:
6484  case PT_CLOB_LENGTH:
6485  case PT_CLOB_TO_CHAR:
6486  case PT_TYPEOF:
6487  case PT_INDEX_CARDINALITY:
6488  case PT_INCR:
6489  case PT_DECR:
6490  case PT_RAND:
6491  case PT_RANDOM:
6492  case PT_DRAND:
6493  case PT_DRANDOM:
6494  case PT_PI:
6495  case PT_ROW_COUNT:
6496  case PT_LAST_INSERT_ID:
6497  case PT_ABS:
6498  case PT_BETWEEN_EQ_NA:
6499  case PT_BETWEEN_GE_INF:
6500  case PT_BETWEEN_GT_INF:
6501  case PT_BETWEEN_INF_LE:
6502  case PT_BETWEEN_INF_LT:
6503  case PT_LT_INF:
6504  case PT_GT_INF:
6505  case PT_CASE:
6506  case PT_DECODE:
6507  case PT_LIKE_ESCAPE:
6508  case PT_RLIKE:
6509  case PT_NOT_RLIKE:
6510  case PT_RLIKE_BINARY:
6511  case PT_NOT_RLIKE_BINARY:
6512  case PT_EVALUATE_VARIABLE:
6513  case PT_DEFINE_VARIABLE:
6514  case PT_EXEC_STATS:
6515  case PT_CONV:
6516  case PT_IFNULL:
6517  case PT_NVL:
6518  case PT_NVL2:
6519  case PT_COALESCE:
6521  case PT_CHARSET:
6522  case PT_COERCIBILITY:
6523  case PT_COLLATION:
6524  case PT_WIDTH_BUCKET:
6525  case PT_TRACE_STATS:
6526  case PT_SHA_ONE:
6527  case PT_SHA_TWO:
6528  case PT_AES_ENCRYPT:
6529  case PT_AES_DECRYPT:
6530  case PT_INDEX_PREFIX:
6531  case PT_SLEEP:
6532  case PT_DBTIMEZONE:
6533  case PT_SESSIONTIMEZONE:
6534  case PT_TZ_OFFSET:
6535  case PT_NEW_TIME:
6536  case PT_FROM_TZ:
6537  case PT_TO_DATETIME_TZ:
6538  case PT_TO_TIMESTAMP_TZ:
6539  case PT_UTC_TIMESTAMP:
6540  case PT_CRC32:
6541  case PT_SCHEMA_DEF:
6542  case PT_CONV_TZ:
6543  return false;
6544 
6545  default:
6546  return true;
6547  }
6548 }
6549 
6550 /*
6551  * pt_propagate_types () - propagate datatypes upward to this expr node
6552  * return: expr's new data_type if all OK, NULL otherwise
6553  * parser(in): the parser context
6554  * expr(in/out): expr node needing the data_type decoration
6555  * otype1(in): data_type of expr's 1st operand
6556  * otype2(in): data_type of expr's 2nd operand
6557  */
6558 
6559 static PT_NODE *
6561 {
6562  PT_NODE *o1, *o2;
6563 
6564  assert (parser != NULL);
6565 
6566  if (!expr || !otype1 || !otype2)
6567  {
6568  return NULL;
6569  }
6570 
6571  o1 = parser_copy_tree_list (parser, otype1);
6572  o2 = parser_copy_tree_list (parser, otype2);
6573 
6574  /* append one to the other */
6575  if (expr->data_type)
6576  {
6577  parser_free_tree (parser, expr->data_type);
6578  }
6579 
6580  expr->data_type = parser_append_node (o2, o1);
6581 
6582  return expr->data_type;
6583 }
6584 
6585 /*
6586  * pt_union_sets () - compute result = set1 + set2
6587  * return: 1 if all OK, 0 otherwise
6588  * parser(in): the parser context
6589  * domain(in): tp_domain of result
6590  * set1(in): a set/multiset db_value
6591  * set2(in): a set/multiset db_value
6592  * result(out): an empty db_value container
6593  * o2(in): a PT_NODE containing the source line & column number of set2
6594  * (used purely for generating error messages)
6595  */
6596 
6597 static int
6598 pt_union_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2, DB_VALUE * result,
6599  PT_NODE * o2)
6600 {
6601  DB_SET *set, *s1, *s2;
6602  int error;
6603 
6604  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
6605 
6606  s1 = db_get_set (set1);
6607  s2 = db_get_set (set2);
6608  error = set_union (s1, s2, &set, domain);
6609  if (error < 0)
6610  {
6611  PT_ERRORc (parser, o2, db_error_string (3));
6612  }
6613 
6614  set_make_collection (result, set);
6615 
6616  return (!pt_has_error (parser));
6617 }
6618 
6619 /*
6620  * pt_difference_sets () - compute result = set1 - set2
6621  * return: 1 if all OK, 0 otherwise
6622  * parser(in): the parser context
6623  * domain(in): tp_domain of result
6624  * set1(in): a set/multiset db_value
6625  * set2(in): a set/multiset db_value
6626  * result(out): an empty db_value container
6627  * o2(in): a PT_NODE containing the source line & column number of set1
6628  * (used purely for generating error messages)
6629  */
6630 
6631 static int
6633  PT_NODE * o2)
6634 {
6635  DB_SET *set, *s1, *s2;
6636  int error;
6637 
6638  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
6639 
6640  s1 = db_get_set (set1);
6641  s2 = db_get_set (set2);
6642  error = set_difference (s1, s2, &set, domain);
6643  if (error < 0)
6644  {
6645  PT_ERRORc (parser, o2, db_error_string (3));
6646  }
6647 
6648  set_make_collection (result, set);
6649 
6650  return (!pt_has_error (parser));
6651 }
6652 
6653 /*
6654  * pt_product_sets () - compute result = set1 * set2
6655  * return: 1 if all OK, 0 otherwise
6656  * parser(in): the parser context
6657  * domain(in): tp_domain of result
6658  * set1(in): a set/multiset db_value
6659  * set2(in): a set/multiset db_value
6660  * result(out): an empty db_value container
6661  * o2(in): a PT_NODE containing the source line & column number of set1
6662  * (used purely for generating error messages)
6663  */
6664 static int
6666  PT_NODE * o2)
6667 {
6668  DB_SET *set, *s1, *s2;
6669  int error;
6670 
6671  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
6672 
6673  s1 = db_get_set (set1);
6674  s2 = db_get_set (set2);
6675  error = set_intersection (s1, s2, &set, domain);
6676  if (error < 0)
6677  {
6678  PT_ERRORc (parser, o2, db_error_string (3));
6679  }
6680 
6681  set_make_collection (result, set);
6682 
6683  return (!pt_has_error (parser));
6684 }
6685 
6686 
6687 /*
6688  * pt_where_type () - Test for constant folded where clause,
6689  * and fold as necessary
6690  * return:
6691  * parser(in):
6692  * where(in/out):
6693  *
6694  * Note :
6695  * Unfortunately, NULL is allowed in this test to provide
6696  * for constant folded clauses.
6697  *
6698  */
6699 
6700 PT_NODE *
6702 {
6703  PT_NODE *cnf_node, *dnf_node, *cnf_prev, *dnf_prev;
6704  bool cut_off;
6705  int line = 0, column = 0;
6706  short location;
6707 
6708  if (where)
6709  {
6710  line = where->line_number;
6711  column = where->column_number;
6712  }
6713 
6714  /* traverse CNF list and keep track the pointer to previous node */
6715  cnf_prev = NULL;
6716  while ((cnf_node = ((cnf_prev) ? cnf_prev->next : where)))
6717  {
6718  /* save location */
6719  location = 0;
6720  switch (cnf_node->node_type)
6721  {
6722  case PT_EXPR:
6723  location = cnf_node->info.expr.location;
6724  break;
6725 
6726  case PT_VALUE:
6727  location = cnf_node->info.value.location;
6728  break;
6729 
6730  case PT_HOST_VAR:
6731  /* TRUE/FALSE can be bound */
6732  break;
6733 
6734  default:
6735  /* stupid where cond. treat it as false condition example: SELECT * FROM foo WHERE id; */
6736  goto always_false;
6737  }
6738 
6739  if (cnf_node->type_enum == PT_TYPE_NA || cnf_node->type_enum == PT_TYPE_NULL)
6740  {
6741  /* on_cond does not confused with where conjunct is a NULL, treat it as false condition */
6742  goto always_false;
6743  }
6744 
6745  if (cnf_node->type_enum != PT_TYPE_MAYBE && cnf_node->type_enum != PT_TYPE_LOGICAL
6746  && !(cnf_node->type_enum == PT_TYPE_NA || cnf_node->type_enum == PT_TYPE_NULL))
6747  {
6748  /* If the conjunct is not a NULL or a logical type, then there's a problem. But don't say anything if
6749  * somebody else has already complained */
6750  if (!pt_has_error (parser))
6751  {
6753  }
6754  break;
6755  }
6756 
6757  cut_off = false;
6758 
6759  if (cnf_node->or_next == NULL)
6760  {
6761  if (cnf_node->node_type == PT_VALUE && cnf_node->type_enum == PT_TYPE_LOGICAL
6762  && cnf_node->info.value.data_value.i == 1)
6763  {
6764  cut_off = true;
6765  }
6766  else
6767  {
6768  /* do not fold node which already have been folded */
6769  if (cnf_node->node_type == PT_VALUE && cnf_node->type_enum == PT_TYPE_LOGICAL
6770  && cnf_node->info.value.data_value.i == 0)
6771  {
6772  if (cnf_node == where && cnf_node->next == NULL)
6773  {
6774  return where;
6775  }
6776  goto always_false;
6777  }
6778  }
6779  }
6780  else
6781  {
6782  /* traverse DNF list and keep track of the pointer to previous node */
6783  dnf_prev = NULL;
6784  while ((dnf_node = ((dnf_prev) ? dnf_prev->or_next : cnf_node)))
6785  {
6786  if (dnf_node->node_type == PT_VALUE && dnf_node->type_enum == PT_TYPE_LOGICAL
6787  && dnf_node->info.value.data_value.i == 1)
6788  {
6789  cut_off = true;
6790  break;
6791  }
6792 
6793  if (dnf_node->node_type == PT_VALUE && dnf_node->type_enum == PT_TYPE_LOGICAL
6794  && dnf_node->info.value.data_value.i == 0)
6795  {
6796  /* cut it off from DNF list */
6797  if (dnf_prev)
6798  {
6799  dnf_prev->or_next = dnf_node->or_next;
6800  }
6801  else
6802  {
6803  if (cnf_prev)
6804  {
6805  if (dnf_node->or_next)
6806  {
6807  cnf_prev->next = dnf_node->or_next;
6808  dnf_node->or_next->next = dnf_node->next;
6809  }
6810  else
6811  {
6812  goto always_false;
6813  }
6814 
6815  cnf_node = cnf_prev->next;
6816  }
6817  else
6818  {
6819  if (dnf_node->or_next)
6820  {
6821  where = dnf_node->or_next;
6822  dnf_node->or_next->next = dnf_node->next;
6823  }
6824  else
6825  {
6826  goto always_false;
6827  }
6828 
6829  cnf_node = where;
6830  } /* else (cnf_prev) */
6831  } /* else (dnf_prev) */
6832  dnf_node->next = NULL;
6833  dnf_node->or_next = NULL;
6834  parser_free_tree (parser, dnf_node);
6835  if (where == NULL)
6836  {
6837  goto always_false;
6838  }
6839  continue;
6840  }
6841 
6842  dnf_prev = (dnf_prev) ? dnf_prev->or_next : dnf_node;
6843  } /* while (dnf_node) */
6844  } /* else (cnf_node->or_next == NULL) */
6845 
6846  if (cut_off)
6847  {
6848  /* cut if off from CNF list */
6849  if (cnf_prev)
6850  {
6851  cnf_prev->next = cnf_node->next;
6852  }
6853  else
6854  {
6855  where = cnf_node->next;
6856  }
6857  cnf_node->next = NULL;
6858  parser_free_tree (parser, cnf_node);
6859  }
6860  else
6861  {
6862  cnf_prev = (cnf_prev) ? cnf_prev->next : cnf_node;
6863  }
6864  } /* while (cnf_node) */
6865 
6866  return where;
6867 
6868 always_false:
6869 
6870  /* If any conjunct is false, the entire WHERE clause is false. Jack the return value to be a single false node (being
6871  * sure to unlink the node from the "next" chain if we reuse the incoming node). */
6872  parser_free_tree (parser, where);
6873  where = parser_new_node (parser, PT_VALUE);
6874  if (where == NULL)
6875  {
6876  PT_INTERNAL_ERROR (parser, "allocate new node");
6877  return NULL;
6878  }
6879 
6880  where->line_number = line;
6881  where->column_number = column;
6882  where->type_enum = PT_TYPE_LOGICAL;
6883  where->info.value.data_value.i = 0;
6884  where->info.value.location = location;
6885  (void) pt_value_to_db (parser, where);
6886 
6887  return where;
6888 }
6889 
6890 /*
6891  * pt_where_type_keep_true () - The same as pt_where_type but if the expression
6892  * is true it is folded to a true value rather than a NULL.
6893  */
6894 PT_NODE *
6896 {
6897  PT_NODE *save_where = where;
6898 
6899  where = pt_where_type (parser, where);
6900  if (where == NULL && save_where != NULL)
6901  {
6902  /* TODO: The line/column number is lost. */
6903  where = parser_new_node (parser, PT_VALUE);
6904  if (where == NULL)
6905  {
6906  PT_INTERNAL_ERROR (parser, "allocate new node");
6907  return NULL;
6908  }
6909  where->type_enum = PT_TYPE_LOGICAL;
6910  where->info.value.data_value.i = 1;
6911  (void) pt_value_to_db (parser, where);
6912  }
6913  return where;
6914 }
6915 
6916 /*
6917  * pt_false_where () - Test for constant folded where in select
6918  * that evaluated false. also check that it is not an aggregate select
6919  * return:
6920  * parser(in):
6921  * node(in):
6922  */
6923 
6924 bool
6926 {
6927  PT_NODE *from, *where;
6928 
6929  where = NULL;
6930 
6931  switch (node->node_type)
6932  {
6933  case PT_VALUE:
6934  where = node;
6935  break;
6936 
6937  case PT_SELECT:
6938 
6939  /* If the "connect by" condition is false the query still has to return all the "start with" tuples. Therefore we
6940  * do not check that "connect by" is false. */
6941  if (node->info.query.q.select.start_with)
6942  {
6943  where = node->info.query.q.select.start_with;
6944  if (pt_false_search_condition (parser, where) == true)
6945  {
6946  return true;
6947  }
6948  }
6949  if (node->info.query.q.select.after_cb_filter)
6950  {
6951  where = node->info.query.q.select.after_cb_filter;
6952  if (pt_false_search_condition (parser, where) == true)
6953  {
6954  return true;
6955  }
6956  }
6957 
6958  if (node->info.query.orderby_for)
6959  {
6960  where = node->info.query.orderby_for;
6961  if (pt_false_search_condition (parser, where) == true)
6962  {
6963  return true;
6964  }
6965  }
6966 
6967  if (pt_is_single_tuple (parser, node))
6968  {
6969  return false;
6970  }
6971 
6972  if (node->info.query.q.select.group_by)
6973  {
6974  where = node->info.query.q.select.having;
6975  if (pt_false_search_condition (parser, where) == true)
6976  {
6977  return true;
6978  }
6979  }
6980 
6981  for (from = node->info.query.q.select.from; from; from = from->next)
6982  {
6983  /* exclude outer join spec from folding */
6985  || (from->next
6986  && ((from->next->info.spec.join_type == PT_JOIN_LEFT_OUTER)
6987  || (from->next->info.spec.join_type == PT_JOIN_RIGHT_OUTER))))
6988  {
6989  continue;
6990  }
6991  else if (from->info.spec.derived_table_type == PT_IS_SUBQUERY
6993  {
6994  PT_NODE *derived_table;
6995 
6996  derived_table = from->info.spec.derived_table;
6997  if (PT_IS_FALSE_WHERE_VALUE (derived_table))
6998  {
6999  return true;
7000  }
7001  }
7002  else if (PT_SPEC_IS_CTE (from))
7003  {
7004  PT_NODE *cte = from->info.spec.cte_pointer;
7005  PT_NODE *cte_non_recursive;
7006 
7007  CAST_POINTER_TO_NODE (cte);
7008 
7009  if (cte)
7010  {
7011  cte_non_recursive = cte->info.cte.non_recursive_part;
7012 
7013  if (PT_IS_FALSE_WHERE_VALUE (cte_non_recursive))
7014  {
7015  return true;
7016  }
7017  }
7018  }
7019  }
7020 
7021  where = node->info.query.q.select.where;
7022  break;
7023 
7024  case PT_UPDATE:
7025  where = node->info.update.search_cond;
7026  break;
7027 
7028  case PT_DELETE:
7029  where = node->info.delete_.search_cond;
7030  break;
7031 
7032  case PT_MERGE:
7033  where = node->info.merge.search_cond;
7034  break;
7035 
7036  default:
7037  break;
7038  }
7039 
7040  return pt_false_search_condition (parser, where);
7041 }
7042 
7043 
7044 /*
7045  * pt_false_search_condition () - Test for constant-folded search condition
7046  * that evaluated false
7047  * return: 1 if any of the conjuncts are effectively false, 0 otherwise
7048  * parser(in):
7049  * node(in):
7050  */
7051 
7052 bool
7054 {
7055  while (node)
7056  {
7057  if (node->or_next == NULL
7058  && (node->type_enum == PT_TYPE_NA || node->type_enum == PT_TYPE_NULL
7059  || (node->node_type == PT_VALUE && node->type_enum == PT_TYPE_LOGICAL
7060  && node->info.value.data_value.i == 0)))
7061  {
7062  return true;
7063  }
7064 
7065  node = node->next;
7066  }
7067 
7068  return false;
7069 }
7070 
7071 /*
7072  * pt_to_false_subquery () -
7073  * return:
7074  * parser(in):
7075  * node(in/out):
7076  */
7077 static PT_NODE *
7079 {
7080  PT_NODE *next;
7081  int line, column;
7082  const char *alias_print;
7083  PT_SELECT_INFO *subq;
7084  int col_cnt, i;
7085  PT_NODE *col, *set, *spec;
7086 
7087  if (node->info.query.flag.has_outer_spec == 1 || node->info.query.flag.is_sort_spec
7088  || node->info.query.flag.is_insert_select)
7089  {
7090  /* rewrite as empty subquery for example, SELECT a, b FROM x LEFT OUTER JOIN y WHERE 0 <> 0 => SELECT null, null
7091  * FROM table({}) as av6749(av_1) WHERE 0 <> 0 */
7092 
7093  if (node->node_type != PT_SELECT)
7094  {
7095  return NULL;
7096  }
7097 
7098  subq = &(node->info.query.q.select);
7099 
7100  /* rewrite SELECT list */
7102 
7103  parser_free_tree (parser, subq->list);
7104  subq->list = NULL;
7105 
7106  for (i = 0; i < col_cnt; i++)
7107  {
7108  col = parser_new_node (parser, PT_VALUE);
7109  if (col)
7110  {
7111  col->type_enum = PT_TYPE_NULL;
7112  subq->list = parser_append_node (subq->list, col);
7113  }
7114  else
7115  {
7117  return NULL;
7118  }
7119  }
7120 
7121  /* rewrite FROM list */
7122  set = parser_new_node (parser, PT_VALUE);
7123  spec = parser_new_node (parser, PT_SPEC);
7124  if (set && spec)
7125  {
7126  parser_free_tree (parser, subq->from);
7127  subq->from = NULL;
7128 
7129  set->type_enum = PT_TYPE_SEQUENCE;
7130 
7131  spec->info.spec.id = (UINTPTR) spec;
7132  spec->info.spec.derived_table = set;
7134 
7135  /* set line number to dummy class, dummy attr */
7136  spec->info.spec.range_var = pt_name (parser, "av6749");
7137  spec->info.spec.as_attr_list = pt_name (parser, "av_1");
7138 
7139  if (spec->info.spec.as_attr_list)
7140  {
7142  }
7143  subq->from = spec;
7144  }
7145  else
7146  {
7148  return NULL;
7149  }
7150 
7151  /* clear unnecessary node info */
7152  if (node->info.query.order_by && !node->info.query.q.select.connect_by)
7153  {
7154  parser_free_tree (parser, node->info.query.order_by);
7155  node->info.query.order_by = NULL;
7156  }
7157 
7158  if (node->info.query.orderby_for)
7159  {
7160  parser_free_tree (parser, node->info.query.orderby_for);
7161  node->info.query.orderby_for = NULL;
7162  }
7163 
7164  node->info.query.correlation_level = 0;
7165  node->info.query.all_distinct = PT_ALL;
7166 
7167  /* clear unnecessary subq info */
7168  if (subq->group_by)
7169  {
7170  parser_free_tree (parser, subq->group_by);
7171  subq->group_by = NULL;
7172  }
7173 
7174  if (subq->connect_by)
7175  {
7176  parser_free_tree (parser, subq->connect_by);
7177  subq->connect_by = NULL;
7178  }
7179 
7180  if (subq->start_with)
7181  {
7182  parser_free_tree (parser, subq->start_with);
7183  subq->start_with = NULL;
7184  }
7185 
7186  if (subq->after_cb_filter)
7187  {
7188  parser_free_tree (parser, subq->after_cb_filter);
7189  subq->after_cb_filter = NULL;
7190  }
7191 
7192  if (subq->having)
7193  {
7194  parser_free_tree (parser, subq->having);
7195  subq->having = NULL;
7196  }
7197 
7198  if (subq->using_index)
7199  {
7200  parser_free_tree (parser, subq->using_index);
7201  subq->using_index = NULL;
7202  }
7203 
7204  subq->hint = PT_HINT_NONE;
7205  }
7206  else
7207  {
7208  int hidden = node->flag.is_hidden_column;
7209 
7210  /* rewrite as null value */
7211  next = node->next;
7212  line = node->line_number;
7213  column = node->column_number;
7214  alias_print = node->alias_print;
7215 
7216  node->next = NULL;
7217  parser_free_tree (parser, node);
7218 
7219  node = parser_new_node (parser, PT_VALUE);
7220  if (!node)
7221  {
7223  return NULL;
7224  }
7225 
7226  node->line_number = line;
7227  node->column_number = column;
7228  node->alias_print = alias_print;
7229  node->type_enum = PT_TYPE_NULL;
7230  node->info.value.location = 0;
7231  node->flag.is_hidden_column = hidden;
7232  node->next = next; /* restore link */
7233  }
7234 
7235  return node;
7236 }
7237 
7238 /*
7239  * pt_eval_recursive_expr_type () - evaluates type for recursive expression nodes.
7240  * return: evaluated node
7241  * parser(in):
7242  * recursive_expr(in): recursive expression node to evaluate.
7243  */
7244 static PT_NODE *
7246 {
7247  PT_OP_TYPE op;
7248 
7249  if (recursive_expr == NULL)
7250  {
7251  return NULL;
7252  }
7253 
7254  if (PT_IS_RECURSIVE_EXPRESSION (recursive_expr))
7255  {
7256  op = recursive_expr->info.expr.op;
7257  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recursive_expr))
7258  {
7259  if (recursive_expr->info.expr.arg1 != NULL && PT_IS_RECURSIVE_EXPRESSION (recursive_expr->info.expr.arg1)
7260  && op == recursive_expr->info.expr.arg1->info.expr.op)
7261  {
7262  recursive_expr->info.expr.arg1 = pt_eval_recursive_expr_type (parser, recursive_expr->info.expr.arg1);
7263  }
7264  }
7265  else
7266  {
7267  if (recursive_expr->info.expr.arg2 != NULL && PT_IS_RECURSIVE_EXPRESSION (recursive_expr->info.expr.arg2)
7268  && op == recursive_expr->info.expr.arg2->info.expr.op)
7269  {
7270  recursive_expr->info.expr.arg2 = pt_eval_recursive_expr_type (parser, recursive_expr->info.expr.arg2);
7271  }
7272  }
7273  }
7274 
7275  return pt_eval_expr_type (parser, recursive_expr);
7276 }
7277 
7278 /*
7279  * pt_eval_type_pre () -
7280  * return:
7281  * parser(in):
7282  * node(in):
7283  * arg(in):
7284  * continue_walk(in):
7285  */
7286 static PT_NODE *
7287 pt_eval_type_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
7288 {
7289  PT_NODE *arg1, *arg2;
7290  PT_NODE *derived_table;
7291  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
7292 
7293  /* To ensure that after exit of recursive expression node the evaluation of type will continue in normal mode */
7294  *continue_walk = PT_CONTINUE_WALK;
7295  if (sc_info->donot_fold == true)
7296  { /* skip folding */
7297  return node;
7298  }
7299 
7300  switch (node->node_type)
7301  {
7302  case PT_SPEC:
7303  derived_table = node->info.spec.derived_table;
7304  if (pt_is_query (derived_table))
7305  {
7306  /* exclude outer join spec from folding */
7308  || (node->next && (node->next->info.spec.join_type == PT_JOIN_LEFT_OUTER
7309  || (node->next->info.spec.join_type == PT_JOIN_RIGHT_OUTER))))
7310  {
7311  derived_table->info.query.flag.has_outer_spec = 1;
7312  }
7313  else
7314  {
7315  derived_table->info.query.flag.has_outer_spec = 0;
7316  }
7317  }
7318  break;
7319 
7320  case PT_SORT_SPEC:
7321  /* if sort spec expression is query, mark it as such */
7322  if (node->info.sort_spec.expr && PT_IS_QUERY (node->info.sort_spec.expr))
7323  {
7325  }
7326  break;
7327 
7328  case PT_UNION:
7329  case PT_DIFFERENCE:
7330  case PT_INTERSECTION:
7331  /* propagate to children */
7332  arg1 = node->info.query.q.union_.arg1;
7333  arg2 = node->info.query.q.union_.arg2;
7334  if (arg1 != NULL)
7335  {
7337  }
7338  if (arg2 != NULL)
7339  {
7341  }
7342 
7343  /* rewrite limit clause as numbering expression and add it to the corresponding predicate */
7344  if (node->info.query.limit && node->info.query.flag.rewrite_limit)
7345  {
7346  PT_NODE *limit, *t_node;
7347  PT_NODE **expr_pred;
7348 
7349  /* If both ORDER BY clause and LIMIT clause are specified, we will rewrite LIMIT to ORDERBY_NUM(). For
7350  * example, (SELECT ...) UNION (SELECT ...) ORDER BY a LIMIT 10 will be rewritten to: (SELECT ...) UNION
7351  * (SELECT ...) ORDER BY a FOR ORDERBY_NUM() <= 10 If LIMIT clause is only specified, we will rewrite the
7352  * query at query optimization step. See qo_rewrite_queries() function for more information. */
7353  if (node->info.query.order_by != NULL)
7354  {
7355  expr_pred = &node->info.query.orderby_for;
7356  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7357  if (limit != NULL)
7358  {
7359  t_node = *expr_pred;
7360  while (t_node != NULL && t_node->next != NULL)
7361  {
7362  t_node = t_node->next;
7363  }
7364  if (t_node == NULL)
7365  {
7366  t_node = *expr_pred = limit;
7367  }
7368  else
7369  {
7370  t_node->info.expr.paren_type = 1;
7371  t_node->next = limit;
7372  }
7373 
7374  node->info.query.flag.rewrite_limit = 0;
7375  }
7376  else
7377  {
7379  }
7380  }
7381  }
7382  break;
7383 
7384  case PT_SELECT:
7385  /* rewrite limit clause as numbering expression and add it to the corresponding predicate */
7386  if (node->info.query.limit && node->info.query.flag.rewrite_limit)
7387  {
7388  PT_NODE *limit, *t_node;
7389  PT_NODE **expr_pred;
7390 
7391  if (node->info.query.order_by)
7392  {
7393  expr_pred = &node->info.query.orderby_for;
7394  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7395  }
7396  else if (node->info.query.q.select.group_by)
7397  {
7398  expr_pred = &node->info.query.q.select.having;
7399  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_LAST_OPCODE, true);
7400  }
7401  else if (node->info.query.all_distinct == PT_DISTINCT)
7402  {
7403  /* When a distinct query has neither orderby nor groupby clause, limit must be orderby_num predicate. */
7404  expr_pred = &node->info.query.orderby_for;
7405  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7406  }
7407  else
7408  {
7409  expr_pred = &node->info.query.q.select.where;
7410  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_INST_NUM, false);
7411  }
7412 
7413  if (limit != NULL)
7414  {
7415  t_node = *expr_pred;
7416  while (t_node != NULL && t_node->next != NULL)
7417  {
7418  t_node = t_node->next;
7419  }
7420  if (t_node == NULL)
7421  {
7422  t_node = *expr_pred = limit;
7423  }
7424  else
7425  {
7426  t_node->info.expr.paren_type = 1;
7427  t_node->next = limit;
7428  }
7429 
7430  node->info.query.flag.rewrite_limit = 0;
7431  }
7432  else
7433  {
7435  }
7436  }
7437  break;
7438 
7439  case PT_INSERT:
7440  /* mark inserted sub-query as belonging to insert statement */
7442  {
7444  }
7445  break;
7446 
7447  case PT_DELETE:
7448  /* rewrite limit clause as numbering expression and add it to search condition */
7449  if (node->info.delete_.limit && node->info.delete_.rewrite_limit)
7450  {
7451  PT_NODE *t_node = node->info.delete_.search_cond;
7452  PT_NODE *limit = pt_limit_to_numbering_expr (parser, node->info.delete_.limit, PT_INST_NUM, false);
7453 
7454  if (limit != NULL)
7455  {
7456  while (t_node != NULL && t_node->next != NULL)
7457  {
7458  t_node = t_node->next;
7459  }
7460  if (t_node == NULL)
7461  {
7462  node->info.delete_.search_cond = limit;
7463  }
7464  else
7465  {
7466  t_node->info.expr.paren_type = 1;
7467  t_node->next = limit;
7468  }
7469 
7470  node->info.delete_.rewrite_limit = 0;
7471  }
7472  else
7473  {
7475  }
7476  }
7477  break;
7478 
7479  case PT_UPDATE:
7480  /* rewrite limit clause as numbering expression and add it to search condition */
7481  if (node->info.update.limit && node->info.update.rewrite_limit)
7482  {
7483  PT_NODE **expr_pred = NULL;
7484  PT_NODE *t_node = NULL, *limit = NULL;
7485 
7486  if (node->info.update.order_by)
7487  {
7488  expr_pred = &node->info.update.orderby_for;
7489  limit = pt_limit_to_numbering_expr (parser, node->info.update.limit, PT_ORDERBY_NUM, false);
7490  }
7491  else
7492  {
7493  expr_pred = &node->info.update.search_cond;
7494  limit = pt_limit_to_numbering_expr (parser, node->info.update.limit, PT_INST_NUM, false);
7495  }
7496 
7497  if (limit != NULL)
7498  {
7499  t_node = *expr_pred;
7500  while (t_node != NULL && t_node->next != NULL)
7501  {
7502  t_node = t_node->next;
7503  }
7504  if (t_node == NULL)
7505  {
7506  t_node = *expr_pred = limit;
7507  }
7508  else
7509  {
7510  t_node->info.expr.paren_type = 1;
7511  t_node->next = limit;
7512  }
7513 
7514  node->info.update.rewrite_limit = 0;
7515  }
7516  else
7517  {
7519  }
7520  }
7521  break;
7522 
7523  case PT_EXPR:
7524  {
7525  PT_NODE *recurs_expr = node, *node_tmp = NULL;
7526  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
7527  bool has_enum = false;
7528  PT_NODE **recurs_arg = NULL, **norm_arg = NULL;
7529  PT_OP_TYPE op = recurs_expr->info.expr.op;
7530 
7531  /* Because the recursive expressions with more than two arguments are build as PT_GREATEST(PT_GREATEST(...,
7532  * argn-1), argn) we need to compute the common type between all arguments in order to give a correct return
7533  * type. Let's say we have the following call to PT_GREATEST: greatest(e1, e2, e3, 2) where e1, e2, e3 are
7534  * ENUMs. The internal form is rewrited to greatest(greatest(greatest(e1, e2), e3), 2). For the inner call the
7535  * common type will be STRING. For middle call the common type will be STRING and for the outer call the common
7536  * type will be DOUBLE and both arguments will be casted to DOUBLE including the returned STRING of the middle
7537  * (or even inner) call. If the string does not have a numeric format, the call will fail. The natural
7538  * behaviour is a conversion of all enum arguments to the type of '2' (integer). So we compute the common type
7539  * of all arguments and store it in the recursive_type member of the PT_EXPR_INFO structure and use it in
7540  * pt_eval_type function (and other places) as common type. */
7542  {
7543  break;
7544  }
7545 
7546  while (recurs_expr != NULL && PT_IS_RECURSIVE_EXPRESSION (recurs_expr) && op == recurs_expr->info.expr.op)
7547  {
7548  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recurs_expr))
7549  {
7550  norm_arg = &recurs_expr->info.expr.arg2;
7551  recurs_arg = &recurs_expr->info.expr.arg1;
7552  }
7553  else
7554  {
7555  norm_arg = &recurs_expr->info.expr.arg1;
7556  recurs_arg = &recurs_expr->info.expr.arg2;
7557  }
7558  /* In order to correctly compute the common type we need to know the type of each argument and therefore we
7559  * compute it. */
7560  node_tmp = pt_semantic_type (parser, *norm_arg, (SEMANTIC_CHK_INFO *) arg);
7561  if (*norm_arg == NULL || pt_has_error (parser))
7562  {
7563  return node;
7564  }
7565  *norm_arg = node_tmp;
7566  if ((*norm_arg)->type_enum != PT_TYPE_ENUMERATION)
7567  {
7568  if (common_type == PT_TYPE_NONE)
7569  {
7570  common_type = (*norm_arg)->type_enum;
7571  }
7572  else
7573  {
7574  common_type = pt_common_type (common_type, (*norm_arg)->type_enum);
7575  }
7576  }
7577  else
7578  {
7579  has_enum = true;
7580  }
7581  if (*recurs_arg == NULL || !PT_IS_RECURSIVE_EXPRESSION (*recurs_arg) || op != (*recurs_arg)->info.expr.op)
7582  {
7583  node_tmp = pt_semantic_type (parser, *recurs_arg, (SEMANTIC_CHK_INFO *) arg);
7584  if (node_tmp == NULL || pt_has_error (parser))
7585  {
7586  return node;
7587  }
7588  *recurs_arg = node_tmp;
7589  if ((*recurs_arg)->type_enum != PT_TYPE_ENUMERATION)
7590  {
7591  if (common_type == PT_TYPE_NONE)
7592  {
7593  common_type = (*recurs_arg)->type_enum;
7594  }
7595  else
7596  {
7597  common_type = pt_common_type (common_type, (*recurs_arg)->type_enum);
7598  }
7599  }
7600  else
7601  {
7602  has_enum = true;
7603  }
7604  }
7605  if (recurs_expr->info.expr.arg3 != NULL)
7606  {
7607  node_tmp = pt_semantic_type (parser, recurs_expr->info.expr.arg3, (SEMANTIC_CHK_INFO *) arg);
7608  if (node_tmp == NULL || pt_has_error (parser))
7609  {
7610  return node;
7611  }
7612  recurs_expr->info.expr.arg3 = node_tmp;
7613  }
7614  recurs_expr = *recurs_arg;
7615  }
7616 
7617  if (has_enum)
7618  {
7619  if (common_type == PT_TYPE_NONE)
7620  {
7621  /* Proceed to normal type evaluation: each function node evaluates only its own arguments */
7622  break;
7623  }
7624  common_type = pt_common_type (common_type, PT_TYPE_ENUMERATION);
7625  recurs_expr = node;
7626  while (PT_IS_RECURSIVE_EXPRESSION (recurs_expr) && op == recurs_expr->info.expr.op)
7627  {
7628  recurs_expr->info.expr.recursive_type = common_type;
7629 
7630  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recurs_expr))
7631  {
7632  recurs_expr = recurs_expr->info.expr.arg1;
7633  }
7634  else
7635  {
7636  recurs_expr = recurs_expr->info.expr.arg2;
7637  }
7638  }
7639  }
7640 
7641  node = pt_eval_recursive_expr_type (parser, node);
7642 
7643  if (op == PT_DECODE || op == PT_CASE)
7644  {
7645  /* the rest of recursive expressions are checked by normal collation inference */
7646  if (pt_check_recursive_expr_collation (parser, &node) != NO_ERROR)
7647  {
7648  node = NULL;
7649  }
7650  }
7651  /* Because for recursive functions we evaluate type here, we don't need to evaluate it twice so we skip the
7652  * normal path of type evaluation */
7653  *continue_walk = PT_LIST_WALK;
7654  return node;
7655  }
7656  break;
7657 
7658  default:
7659  break;
7660  }
7661 
7662  return node;
7663 }
7664 
7665 static PT_NODE *
7666 pt_fold_constants_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
7667 {
7668  if (node == NULL)
7669  {
7670  return node;
7671  }
7672 
7673  // check if constant folding for sub-tree should be suppressed
7674  switch (node->node_type)
7675  {
7676  case PT_FUNCTION:
7677  if (node->info.function.function_type == F_BENCHMARK)
7678  {
7679  // we want to test full execution of sub-tree; don't fold it!
7680  *continue_walk = PT_LIST_WALK;
7681  }
7682  break;
7683  default:
7684  // nope
7685  break;
7686  }
7687 
7688  return node;
7689 }
7690 
7691 /*
7692  * pt_fold_constants_post () - perform constant folding on the specified node
7693  * return : the node after constant folding
7694  *
7695  * parser(in) : the parser context
7696  * node(in) : the node to be folded
7697  * arg(in) :
7698  * continue_walk(in):
7699  */
7700 static PT_NODE *
7701 pt_fold_constants_post (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
7702 {
7703  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
7704 
7705  if (node == NULL)
7706  {
7707  return node;
7708  }
7709 
7710  if (sc_info->donot_fold == true)
7711  {
7712  /* skip folding */
7713  return node;
7714  }
7715 
7716  switch (node->node_type)
7717  {
7718  case PT_EXPR:
7719  node = pt_fold_const_expr (parser, node, arg);
7720  break;
7721  case PT_FUNCTION:
7722  if (node->info.function.function_type == F_BENCHMARK)
7723  {
7724  // restore walking; I hope this was continue_walk!
7725  *continue_walk = PT_CONTINUE_WALK;
7726  }
7727  else
7728  {
7729  node = pt_fold_const_function (parser, node);
7730  }
7731  break;
7732  default:
7733  break;
7734  }
7735 
7736  if (node == NULL)
7737  {
7738  PT_INTERNAL_ERROR (parser, "pt_fold_constants_post");
7739  return NULL;
7740  }
7741 
7742  return node;
7743 }
7744 
7745 /*
7746  * pt_eval_type () -
7747  * return:
7748  * parser(in):
7749  * node(in):
7750  * arg(in):
7751  * continue_walk(in):
7752  */
7753 static PT_NODE *
7754 pt_eval_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
7755 {
7756  PT_NODE *dt = NULL, *arg1 = NULL, *arg2 = NULL;
7757  PT_NODE *spec = NULL;
7758  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
7759  PT_NODE *list;
7760  STATEMENT_SET_FOLD do_fold;
7761  PT_MISC_TYPE is_subquery;
7762 
7763  switch (node->node_type)
7764  {
7765  case PT_EXPR:
7766  node = pt_eval_expr_type (parser, node);
7767 #if 1 //original code but it doesn't check for errors
7768  if (node == NULL)
7769  {
7770  assert (false);
7771  PT_INTERNAL_ERROR (parser, "pt_eval_type");
7772  return NULL;
7773  }
7774 #else //ToDo: checks for errors but generates regressions that should be analyzed
7775  if (pt_has_error (parser))
7776  {
7777  if (node == NULL)
7778  {
7779  PT_INTERNAL_ERROR (parser, "pt_eval_type");
7780  }
7781  return NULL;
7782  }
7783 #endif
7784  break;
7785 
7786  case PT_FUNCTION:
7787  node = pt_eval_function_type (parser, node);
7788  if (node == NULL)
7789  {
7790  assert (false);
7791  PT_INTERNAL_ERROR (parser, "pt_eval_type");
7792  return NULL;
7793  }
7794  break;
7795 
7796  case PT_METHOD_CALL:
7797  node = pt_eval_method_call_type (parser, node);
7798  break;
7799 
7800  case PT_CREATE_INDEX:
7801  node->info.index.where = pt_where_type (parser, node->info.index.where);
7802  break;
7803 
7804  case PT_DELETE:
7805  node->info.delete_.search_cond = pt_where_type (parser, node->info.delete_.search_cond);
7806  break;
7807 
7808  case PT_UPDATE:
7809  node->info.update.search_cond = pt_where_type (parser, node->info.update.search_cond);
7810  break;
7811 
7812  case PT_MERGE:
7813  node->info.merge.search_cond = pt_where_type (parser, node->info.merge.search_cond);
7817 
7818  if (parser->flag.set_host_var == 0)
7819  {
7820  PT_NODE *v;
7821  PT_NODE *list = NULL;
7822  PT_NODE *attr = NULL;
7823  DB_DOMAIN *d;
7824 
7825  /* insert part */
7826  for (list = node->info.merge.insert.value_clauses; list != NULL; list = list->next)
7827  {
7828  attr = node->info.merge.insert.attr_list;
7829  for (v = list->info.node_list.list; v != NULL && attr != NULL; v = v->next, attr = attr->next)
7830  {
7831  if (PT_IS_HOSTVAR (v) && v->expected_domain == NULL)
7832  {
7833  d = pt_node_to_db_domain (parser, attr, NULL);
7834  d = tp_domain_cache (d);
7835  SET_EXPECTED_DOMAIN (v, d);
7836  pt_preset_hostvar (parser, v);
7837  }
7838  }
7839  }
7840  }
7841  break;
7842 
7843  case PT_SELECT:
7844  if (node->info.query.q.select.list)
7845  {
7846  /* for value query, compatibility check for rows */
7847  if (PT_IS_VALUE_QUERY (node))
7848  {
7849  if (pt_check_type_compatibility_of_values_query (parser, node) == NULL)
7850  {
7851  break;
7852  }
7853  list = node->info.query.q.select.list->info.node_list.list;
7854  assert (list != NULL);
7855 
7856  node->type_enum = list->type_enum;
7857  dt = list->data_type;
7858  }
7859  else
7860  {
7861  node->type_enum = node->info.query.q.select.list->type_enum;
7862  dt = node->info.query.q.select.list->data_type;
7863  }
7864 
7865  if (dt)
7866  {
7867  if (node->data_type)
7868  {
7869  parser_free_tree (parser, node->data_type);
7870  }
7871 
7872  node->data_type = parser_copy_tree_list (parser, dt);
7873  }
7874  }
7875 
7876  for (spec = node->info.query.q.select.from; spec; spec = spec->next)
7877  {
7878  if (spec->node_type == PT_SPEC && spec->info.spec.on_cond)
7879  {
7880  spec->info.spec.on_cond = pt_where_type (parser, spec->info.spec.on_cond);
7881  }
7882  }
7883 
7884  node->info.query.q.select.where = pt_where_type (parser, node->info.query.q.select.where);
7885 
7886  is_subquery = node->info.query.is_subquery;
7887 
7888  if (sc_info->donot_fold == false
7889  && (is_subquery == PT_IS_SUBQUERY || is_subquery == PT_IS_UNION_SUBQUERY
7890  || is_subquery == PT_IS_CTE_NON_REC_SUBQUERY || is_subquery == PT_IS_CTE_REC_SUBQUERY)
7891  && pt_false_where (parser, node))
7892  {
7893  node = pt_to_false_subquery (parser, node);
7894  }
7895  else
7896  {
7900  node->info.query.q.select.having = pt_where_type (parser, node->info.query.q.select.having);
7901  node->info.query.orderby_for = pt_where_type (parser, node->info.query.orderby_for);
7902  }
7903  break;
7904 
7905  case PT_DO:
7906  if (node->info.do_.expr)
7907  {
7908  node->type_enum = node->info.do_.expr->type_enum;
7909  dt = node->info.do_.expr->data_type;
7910  if (dt)
7911  {
7912  if (node->data_type)
7913  {
7914  parser_free_tree (parser, node->data_type);
7915  }
7916 
7917  node->data_type = parser_copy_tree_list (parser, dt);
7918  }
7919  }
7920  break;
7921 
7922  case PT_INSERT:
7923  if (node->info.insert.spec)
7924  {
7925  node->type_enum = PT_TYPE_OBJECT;
7926  dt = parser_new_node (parser, PT_DATA_TYPE);
7927  if (dt)
7928  {
7929  dt->type_enum = PT_TYPE_OBJECT;
7930  node->data_type = dt;
7932  }
7933  }
7934 
7935  if (parser->flag.set_host_var == 0)
7936  {
7937  PT_NODE *v = NULL;
7938  PT_NODE *list = NULL;
7939  PT_NODE *attr = NULL;
7940  DB_DOMAIN *d;
7941 
7942  for (list = node->info.insert.value_clauses; list != NULL; list = list->next)
7943  {
7944  attr = node->info.insert.attr_list;
7945  for (v = list->info.node_list.list; v != NULL && attr != NULL; v = v->next, attr = attr->next)
7946  {
7947  if (PT_IS_HOSTVAR (v) && v->expected_domain == NULL)
7948  {
7949  d = pt_node_to_db_domain (parser, attr, NULL);
7950  d = tp_domain_cache (d);
7951  SET_EXPECTED_DOMAIN (v, d);
7952  pt_preset_hostvar (parser, v);
7953  }
7954  }
7955  }
7956  }
7957  break;
7958 
7959  case PT_UNION:
7960  case PT_DIFFERENCE:
7961  case PT_INTERSECTION:
7962  /* a PT_CTE node is actually a union between two queries */
7963  case PT_CTE:
7964 
7965  /* check if union can be folded */
7966  do_fold = pt_check_union_is_foldable (parser, node);
7967  if (do_fold != STATEMENT_SET_FOLD_NOTHING)
7968  {
7969  node = pt_fold_union (parser, node, do_fold);
7970  }
7971  else
7972  {
7973  /* check that signatures are compatible */
7974  if (node->node_type == PT_CTE)
7975  {
7976  arg1 = node->info.cte.non_recursive_part;
7977  arg2 = node->info.cte.recursive_part;
7978  if (arg2 != NULL && (pt_false_where (parser, arg1) || pt_false_where (parser, arg2)))
7979  {
7980  /* the recursive_part can be removed if one of the parts has false_where */
7981  parser_free_tree (parser, node->info.cte.recursive_part);
7982  arg2 = node->info.cte.recursive_part = NULL;
7983  }
7984 
7985  if (arg2 == NULL)
7986  {
7987  /* then the CTE is not recursive (just one part) */
7988  break;
7989  }
7990  }
7991  else
7992  {
7993  arg1 = node->info.query.q.union_.arg1;
7994  arg2 = node->info.query.q.union_.arg2;
7995  }
7996 
7997  if ((arg1 && PT_IS_VALUE_QUERY (arg1)) || (arg2 && PT_IS_VALUE_QUERY (arg2)))
7998  {
8000  {
8001  break;
8002  }
8003  }
8004  else
8005  {
8006  if (!pt_check_union_compatibility (parser, node))
8007  {
8008  break;
8009  }
8010  }
8011 
8012  node->type_enum = arg1->type_enum;
8013  dt = arg1->data_type;
8014  if (dt)
8015  {
8016  node->data_type = parser_copy_tree_list (parser, dt);
8017  }
8018  }
8019  break;
8020 
8021  case PT_VALUE:
8022  case PT_NAME:
8023  case PT_DOT_:
8024  /* these cases have types already assigned to them by parser and semantic name resolution. */
8025  break;
8026 
8027  case PT_HOST_VAR:
8028  if (node->type_enum == PT_TYPE_NONE && node->info.host_var.var_type == PT_HOST_IN)
8029  {
8030  /* type is not known yet (i.e, compile before bind a value) */
8031  node->type_enum = PT_TYPE_MAYBE;
8032  }
8033  break;
8034  case PT_SET_OPT_LVL:
8035  case PT_GET_OPT_LVL:
8036  node = pt_eval_opt_type (parser, node);
8037  break;
8038 
8039  default:
8040  break;
8041  }
8042 
8043  return node;
8044 }
8045 
8046 /*
8047  * pt_chop_to_one_select_item () -
8048  * return: none
8049  * parser(in):
8050  * node(in/out): an EXISTS subquery
8051  */
8052 static void
8054 {
8055  if (pt_is_query (node))
8056  {
8057  if (node->node_type == PT_SELECT)
8058  {
8059  /* chop to one select item */
8060  if (node->info.query.q.select.list && node->info.query.q.select.list->next)
8061  {
8062  parser_free_tree (parser, node->info.query.q.select.list->next);
8063  node->info.query.q.select.list->next = NULL;
8064  }
8065  }
8066  else
8067  {
8070  }
8071 
8072  /* remove unneeded order by */
8073  if (node->info.query.order_by && !node->info.query.q.select.connect_by)
8074  {
8075  parser_free_tree (parser, node->info.query.order_by);
8076  node->info.query.order_by = NULL;
8077  }
8078  }
8079 }
8080 
8081 /*
8082  * pt_append_query_select_list () - append to the query's lists attrs
8083  *
8084  * result: query with all lists updated
8085  * parser(in):
8086  * query(in):
8087  * attrs(in): list of attributes to append to the query
8088  */
8089 PT_NODE *
8091 {
8092  if (!attrs)
8093  {
8094  return query;
8095  }
8096 
8097  switch (query->node_type)
8098  {
8099  case PT_SELECT:
8100  {
8101  PT_NODE *select_list = query->info.query.q.select.list;
8102 
8103  query->info.query.q.select.list = parser_append_node (attrs, select_list);
8104  break;
8105  }
8106  case PT_DIFFERENCE:
8107  case PT_INTERSECTION:
8108  case PT_UNION:
8109  query->info.query.q.union_.arg1 = pt_append_query_select_list (parser, query->info.query.q.union_.arg1, attrs);
8110  query->info.query.q.union_.arg1 = pt_append_query_select_list (parser, query->info.query.q.union_.arg2, attrs);
8111  break;
8112  default:
8113  break;
8114  }
8115 
8116  return query;
8117 }
8118 
8119 /*
8120  * pt_wrap_select_list_with_cast_op () - cast the nodes of a select list
8121  * to the type new_type, using the specified
8122  * precision and scale
8123  * return : NO_ERROR on success, error code on failure
8124  * parser(in) : parser context
8125  * query(in) : the select query
8126  * new_type(in) : the new_type
8127  * p(in) : precision
8128  * s(in) : scale
8129  * data_type(in) : the data type of new_type
8130  * force_wrap(in): forces wrapping with cast for collatable nodes
8131  */
8132 int
8134  PT_NODE * data_type, bool force_wrap)
8135 {
8136  switch (query->node_type)
8137  {
8138  case PT_SELECT:
8139  {
8140  PT_NODE *item = NULL;
8141  PT_NODE *prev = NULL;
8142  PT_NODE *new_node = NULL;
8143  PT_NODE *select_list = NULL;
8144  PT_NODE *node_list = NULL;
8145 
8146  /* values query's select_list is pt_node_list-->pt_node_list-->... */
8147  if (PT_IS_VALUE_QUERY (query))
8148  {
8149  for (node_list = query->info.query.q.select.list; node_list != NULL; node_list = node_list->next)
8150  {
8151  select_list = node_list->info.node_list.list;
8152 
8153  prev = NULL;
8154  for (item = select_list; item != NULL; prev = item, item = item->next)
8155  {
8156  new_node = NULL;
8157  if (item->type_enum == new_type && !force_wrap)
8158  {
8159  continue;
8160  }
8161 
8162  new_node = pt_wrap_with_cast_op (parser, item, new_type, p, s, data_type);
8163  if (new_node == NULL)
8164  {
8165  return ER_FAILED;
8166  }
8167 
8168  if (new_node != item)
8169  {
8170  item = new_node;
8171  PT_SET_VALUE_QUERY (item);
8172  /* first node in the list */
8173  if (prev == NULL)
8174  {
8175  node_list->info.node_list.list = item;
8176  }
8177  else
8178  {
8179  prev->next = item;
8180  }
8181  }
8182  }
8183  }
8184  }
8185  else
8186  {
8187  select_list = pt_get_select_list (parser, query);
8188 
8189  for (item = select_list; item != NULL; prev = item, item = item->next)
8190  {
8191  if (item->flag.is_hidden_column)
8192  {
8193  continue;
8194  }
8195  new_node = NULL;
8196  if (item->type_enum == new_type && !force_wrap)
8197  {
8198  continue;
8199  }
8200  new_node = pt_wrap_with_cast_op (parser, item, new_type, p, s, data_type);
8201  if (new_node == NULL)
8202  {
8203  return ER_FAILED;
8204  }
8205 
8206  if (new_node != item)
8207  {
8208  item = new_node;
8209  /* first node in the list */
8210  if (prev == NULL)
8211  {
8212  query->info.query.q.select.list = item;
8213  }
8214  else
8215  {
8216  prev->next = item;
8217  }
8218  }
8219  }
8220  }
8221  break;
8222  }
8223  case PT_DIFFERENCE:
8224  case PT_INTERSECTION:
8225  case PT_UNION:
8226  {
8227  int err = NO_ERROR;
8228  /* wrap with cast union select values for queries arg1 and arg2 */
8229  err =
8230  pt_wrap_select_list_with_cast_op (parser, query->info.query.q.union_.arg1, new_type, p, s, data_type,
8231  force_wrap);
8232  if (err != NO_ERROR)
8233  {
8234  return err;
8235  }
8236 
8237  err =
8238  pt_wrap_select_list_with_cast_op (parser, query->info.query.q.union_.arg2, new_type, p, s, data_type,
8239  force_wrap);
8240  if (err != NO_ERROR)
8241  {
8242  return err;
8243  }
8244 
8245  break;
8246  }
8247  default:
8248  return 0;
8249  break;
8250  }
8251  return NO_ERROR;
8252 }
8253 
8254 /*
8255  * pt_wrap_collection_with_cast_op () - wrap a node with a cast to a
8256  * collection type
8257  *
8258  * return: the wrapped node or null on error
8259  * parser(in) : parser context
8260  * arg(in/out) : the node to be wrapped
8261  * set_type(in) : the collection type
8262  * data_type(in): the type of the elements of the collection
8263  * p(in) : the precision of the collection elements
8264  * s(in) : the scale of the collection elements
8265  */
8266 
8267 PT_NODE *
8269  bool for_collation)
8270 {
8271  PT_NODE *new_att, *set_dt, *next_att;
8272 
8273  if (arg->node_type == PT_FUNCTION && set_data->next == NULL)
8274  {
8275  /* if the argument is function and domain to cast has only one type */
8276  switch (arg->info.function.function_type)
8277  {
8278  case F_SET:
8279  case F_MULTISET:
8280  case F_SEQUENCE:
8281  {
8282  PT_NODE **first = &arg->info.function.arg_list, *arg_list = *first, *prev = NULL;
8283  bool is_numeric = PT_IS_NUMERIC_TYPE (set_data->type_enum);
8284 
8285  /* walk through set members and cast them to set_data->type_enum if needed */
8286  while (arg_list)
8287  {
8288  next_att = arg_list->next;
8289 
8290  if ((set_data->type_enum != arg_list->type_enum
8291  && (!is_numeric || !PT_IS_NUMERIC_TYPE (arg_list->type_enum)))
8292  || (for_collation == true && PT_HAS_COLLATION (set_data->type_enum)
8293  && PT_HAS_COLLATION (arg_list->type_enum) && arg_list->data_type != NULL
8294  && set_data->info.data_type.collation_id != arg_list->data_type->info.data_type.collation_id))
8295  {
8296  /* Set the expected domain of host variable to type set_data so that at runtime the host variable
8297  * should be casted to it if needed */
8298  if (arg_list->type_enum == PT_TYPE_MAYBE && arg_list->node_type == PT_HOST_VAR)
8299  {
8300  if (for_collation == false)
8301  {
8302  arg_list->expected_domain = pt_data_type_to_db_domain (parser, set_data, NULL);
8303  pt_preset_hostvar (parser, arg_list);
8304  }
8305  }
8306  else
8307  {
8308  new_att = pt_wrap_with_cast_op (parser, arg_list, set_data->type_enum, 0, 0, set_data);
8309  if (!new_att)
8310  {
8312  return NULL;
8313  }
8314 
8316  {
8317  assert (new_att->node_type == PT_EXPR);
8319  }
8320 
8321  if (prev)
8322  {
8323  prev->next = new_att;
8324  new_att->next = next_att;
8325  }
8326  else
8327  {
8328  *first = new_att;
8329  }
8330  arg_list = new_att;
8331  }
8332  }
8333  prev = arg_list;
8334  arg_list = next_att;
8335  }
8336  }
8337  return arg;
8338  default:
8339  break;
8340  }
8341 
8342  }
8343 
8344  next_att = arg->next;
8345  arg->next = NULL;
8346  new_att = parser_new_node (parser, PT_EXPR);
8347  set_dt = parser_new_node (parser, PT_DATA_TYPE);
8348 
8349  if (!new_att || !set_dt)
8350  {
8352  return NULL;
8353  }
8354 
8355  /* move alias */
8356  new_att->line_number = arg->line_number;
8357  new_att->column_number = arg->column_number;
8358  new_att->alias_print = arg->alias_print;
8359  new_att->flag.is_hidden_column = arg->flag.is_hidden_column;
8360  arg->alias_print = NULL;
8361 
8362 
8363  /* set the data type of the collection */
8364  set_dt->type_enum = set_type;
8365  set_dt->data_type = parser_copy_tree_list (parser, set_data);
8366 
8367  new_att->type_enum = set_type;
8368  new_att->info.expr.op = PT_CAST;
8369  new_att->info.expr.cast_type = set_dt;
8370  new_att->info.expr.arg1 = arg;
8371  new_att->next = next_att;
8372 
8373  new_att->data_type = set_data;
8375  return new_att;
8376 }
8377 
8378 /*
8379  * pt_wrap_with_cast_op () -
8380  * return:
8381  * parser(in):
8382  * arg(in/out):
8383  * new_type(in):
8384  * p(in):
8385  * s(in):
8386  * desired_dt(in):
8387  */
8388 PT_NODE *
8389 pt_wrap_with_cast_op (PARSER_CONTEXT * parser, PT_NODE * arg, PT_TYPE_ENUM new_type, int p, int s, PT_NODE * desired_dt)
8390 {
8391  PT_NODE *new_att, *new_dt, *next_att;
8392 
8393  assert (arg != NULL);
8394  if (arg == NULL)
8395  {
8396  return NULL;
8397  }
8398 
8399  next_att = arg->next;
8400  arg->next = NULL;
8401  new_att = parser_new_node (parser, PT_EXPR);
8402  if (new_att == NULL)
8403  {
8404  return NULL;
8405  }
8406 
8407  if (PT_IS_COLLECTION_TYPE (new_type))
8408  {
8409  new_dt = parser_new_node (parser, PT_DATA_TYPE);
8410  if (new_dt == NULL)
8411  {
8412  return NULL;
8413  }
8414  new_dt->type_enum = new_type;
8415  new_dt->info.data_type.precision = p;
8416  new_dt->info.data_type.dec_precision = s;
8417 
8418  if (desired_dt != NULL && !PT_IS_COLLECTION_TYPE (desired_dt->type_enum))
8419  {
8420  /* desired_dt contains a list of types of elements from the collection */
8421  new_dt->data_type = parser_copy_tree_list (parser, desired_dt);
8422  }
8423  /* If desired_dt is not null but is a collection type, we can't actually make a decision here because we can't
8424  * make a distinction between collection of collections and simple collections. In the case of collection of
8425  * collections, the correct type of each element will be set when the collection is validated in the context in
8426  * which it is used */
8427  }
8428  else if (desired_dt == NULL)
8429  {
8430  new_dt = parser_new_node (parser, PT_DATA_TYPE);
8431  if (new_dt == NULL)
8432  {
8433  return NULL;
8434  }
8435  new_dt->type_enum = new_type;
8436  new_dt->info.data_type.precision = p;
8437  new_dt->info.data_type.dec_precision = s;
8438  if (arg->data_type != NULL && PT_HAS_COLLATION (arg->data_type->type_enum))
8439  {
8440  new_dt->info.data_type.units = arg->data_type->info.data_type.units;
8442  }
8443 
8444  if (PT_HAS_COLLATION (new_type) && arg->type_enum == PT_TYPE_MAYBE)
8445  {
8446  /* when wrapping a TYPE MAYBE, we don't change the collation */
8450  }
8451  }
8452  else
8453  {
8454  new_dt = parser_copy_tree_list (parser, desired_dt);
8455  }
8456 
8457  /* move alias */
8458  new_att->line_number = arg->line_number;
8459  new_att->column_number = arg->column_number;
8460  new_att->alias_print = arg->alias_print;
8461  new_att->flag.is_hidden_column = arg->flag.is_hidden_column;
8462  arg->alias_print = NULL;
8463 
8464  new_att->type_enum = new_type;
8465  new_att->info.expr.op = PT_CAST;
8467  new_att->info.expr.cast_type = new_dt;
8468  new_att->info.expr.arg1 = arg;
8469  new_att->next = next_att;
8470 
8471  new_att->data_type = parser_copy_tree_list (parser, new_dt);
8473 
8474  return new_att;
8475 }
8476 
8477 /*
8478  * pt_preset_hostvar () -
8479  * return: none
8480  * parser(in):
8481  * hv_node(in):
8482  */
8483 void
8485 {
8486  pt_hv_consistent_data_type_with_domain (parser, hv_node);
8487  if (parser->host_var_count <= hv_node->info.host_var.index)
8488  {
8489  /* automated parameters are not needed an expected domain */
8490  return;
8491  }
8492 
8493  parser->host_var_expected_domains[hv_node->info.host_var.index] = hv_node->expected_domain;
8494 }
8495 
8496 /* pt_set_expected_domain - set the expected tomain of a PT_NODE
8497  * return: void
8498  * node (in/out) : the node to wich to set the expected domain
8499  * domain (in) : the expected domain
8500  */
8501 void
8503 {
8504  PT_NODE *_or_next = NULL;
8505  node->expected_domain = domain;
8506  _or_next = node->or_next;
8507  while (_or_next)
8508  {
8509  if (_or_next->type_enum == PT_TYPE_MAYBE && _or_next->expected_domain == NULL)
8510  {
8511  _or_next->expected_domain = domain;
8512  }
8513  _or_next = _or_next->or_next;
8514  }
8515 }
8516 
8517 /*
8518  * pt_is_able_to_determine_return_type () -
8519  * return: true if the type of the return value can be determined
8520  * regardless of its arguments, otherwise false.
8521  * op(in):
8522  */
8523 static bool
8525 {
8526  switch (op)
8527  {
8528  case PT_CAST:
8529  case PT_TO_NUMBER:
8530  case PT_TO_CHAR:
8531  case PT_TO_DATE:
8532  case PT_TO_TIME:
8533  case PT_TO_TIMESTAMP:
8534  case PT_TO_DATETIME:
8535  case PT_POSITION:
8536  case PT_FINDINSET:
8537  case PT_OCTET_LENGTH:
8538  case PT_BIT_LENGTH:
8539  case PT_CHAR_LENGTH:
8540  case PT_TIME_FORMAT:
8541  case PT_TIMESTAMP:
8542  case PT_UNIX_TIMESTAMP:
8543  case PT_SIGN:
8544  case PT_CHR:
8545  case PT_ADD_MONTHS:
8546  case PT_LAST_DAY:
8547  case PT_MONTHS_BETWEEN:
8548  case PT_DATE_ADD:
8549  case PT_ADDDATE:
8550  case PT_FORMAT:
8551  case PT_DATE_SUB:
8552  case PT_SUBDATE:
8553  case PT_DATE_FORMAT:
8554  case PT_STR_TO_DATE:
8555  case PT_SIN:
8556  case PT_COS:
8557  case PT_TAN:
8558  case PT_ASIN:
8559  case PT_ACOS:
8560  case PT_ATAN:
8561  case PT_ATAN2:
8562  case PT_COT:
8563  case PT_LOG:
8564  case PT_EXP:
8565  case PT_SQRT:
8566  case PT_DEGREES:
8567  case PT_LN:
8568  case PT_LOG2:
8569  case PT_LOG10:
8570  case PT_POWER:
8571  case PT_FIELD:
8572  case PT_LOCATE:
8573  case PT_STRCMP:
8574  case PT_RADIANS:
8575  case PT_BIT_AND:
8576  case PT_BIT_OR:
8577  case PT_BIT_XOR:
8578  case PT_BIT_NOT:
8579  case PT_BITSHIFT_LEFT:
8580  case PT_BITSHIFT_RIGHT:
8581  case PT_BIT_COUNT:
8582  case PT_DATEDIFF:
8583  case PT_TIMEDIFF:
8584  case PT_DATEF:
8585  case PT_TIMEF:
8586  case PT_ISNULL:
8587  case PT_RAND:
8588  case PT_DRAND:
8589  case PT_RANDOM:
8590  case PT_DRANDOM:
8591  case PT_BIT_TO_BLOB:
8592  case PT_BLOB_FROM_FILE:
8593  case PT_BLOB_LENGTH:
8594  case PT_BLOB_TO_BIT:
8595  case PT_CHAR_TO_BLOB:
8596  case PT_CHAR_TO_CLOB:
8597  case PT_CLOB_FROM_FILE:
8598  case PT_CLOB_LENGTH:
8599  case PT_CLOB_TO_CHAR:
8600  case PT_TYPEOF:
8601  case PT_YEARF:
8602  case PT_MONTHF:
8603  case PT_DAYF:
8604  case PT_DAYOFMONTH:
8605  case PT_HOURF:
8606  case PT_MINUTEF:
8607  case PT_SECONDF:
8608  case PT_QUARTERF:
8609  case PT_WEEKDAY:
8610  case PT_DAYOFWEEK:
8611  case PT_DAYOFYEAR:
8612  case PT_TODAYS:
8613  case PT_FROMDAYS:
8614  case PT_TIMETOSEC:
8615  case PT_SECTOTIME:
8616  case PT_WEEKF:
8617  case PT_MAKEDATE:
8618  case PT_MAKETIME:
8619  case PT_BIN:
8620  case PT_CASE:
8621  case PT_DECODE:
8622  case PT_LIKE:
8623  case PT_NOT_LIKE:
8624  case PT_RLIKE:
8625  case PT_NOT_RLIKE:
8626  case PT_RLIKE_BINARY:
8627  case PT_NOT_RLIKE_BINARY:
8628  case PT_EVALUATE_VARIABLE:
8629  case PT_DEFINE_VARIABLE:
8630  case PT_HEX:
8631  case PT_ASCII:
8632  case PT_CONV:
8634  case PT_INET_ATON:
8635  case PT_INET_NTOA:
8636  case PT_CHARSET:
8637  case PT_COERCIBILITY:
8638  case PT_COLLATION:
8639  case PT_WIDTH_BUCKET:
8640  case PT_AES_ENCRYPT:
8641  case PT_AES_DECRYPT:
8642  case PT_SHA_ONE:
8643  case PT_SHA_TWO:
8644  case PT_SLEEP:
8645  case PT_TO_DATETIME_TZ:
8646  case PT_TO_TIMESTAMP_TZ:
8647  case PT_CRC32:
8648  case PT_DISK_SIZE:
8649  case PT_SCHEMA_DEF:
8650  return true;
8651 
8652  default:
8653  return false;
8654  }
8655 }
8656 
8657 /*
8658  * pt_get_common_datetime_type () -
8659  * return:
8660  * common_type(in):
8661  * arg1_type(in):
8662  * arg2_type(in):
8663  * arg1(in):
8664  * arg1(in):
8665  */
8666 static PT_TYPE_ENUM
8668  PT_TYPE_ENUM arg2_type, PT_NODE * arg1, PT_NODE * arg2)
8669 {
8670  PT_TYPE_ENUM arg_type, arg_base_type;
8671  PT_NODE *arg_ptr = NULL;
8672  PT_NODE *arg_base = NULL;
8673 
8674  assert (arg1_type != arg2_type);
8675 
8676  if (arg1_type == common_type)
8677  {
8678  arg_base_type = arg1_type;
8679  arg_base = arg1;
8680  arg_type = arg2_type;
8681  arg_ptr = arg2;
8682  }
8683  else
8684  {
8685  arg_base_type = arg2_type;
8686  arg_base = arg2;
8687  arg_type = arg1_type;
8688  arg_ptr = arg1;
8689  }
8690 
8691  if (PT_IS_CHAR_STRING_TYPE (arg_type))
8692  {
8693  if (pt_coerce_str_to_time_date_utime_datetime (parser, arg_ptr, &arg_type) == NO_ERROR)
8694  {
8695  PT_TYPE_ENUM result_type = pt_common_type (arg_base_type, arg_type);
8696 
8697  if (arg_type != arg_base_type)
8698  {
8699  return pt_get_common_datetime_type (parser, result_type, arg_base_type, arg_type, arg_base, arg_ptr);
8700  }
8701 
8702  return result_type;
8703  }
8704  }
8705  else if (PT_IS_DATE_TIME_TYPE (arg_type))
8706  {
8707  if (pt_coerce_value (parser, arg_ptr, arg_ptr, common_type, NULL) == NO_ERROR)
8708  {
8709  return common_type;
8710  }
8711  }
8712  else if (PT_IS_NUMERIC_TYPE (arg_type))
8713  {
8714  return common_type;
8715  }
8716 
8717  return PT_TYPE_NONE;
8718 }
8719 
8720 /*
8721  * pt_eval_expr_type () -
8722  * return:
8723  * parser(in):
8724  * node(in):
8725  */
8726 static PT_NODE *
8728 {
8729  PT_OP_TYPE op;
8730  PT_NODE *arg1 = NULL, *arg2 = NULL, *arg3 = NULL;
8731  PT_NODE *arg1_hv = NULL, *arg2_hv = NULL, *arg3_hv = NULL;
8732  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
8733  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE, common_type = PT_TYPE_NONE;
8734  TP_DOMAIN *d;
8735  PT_NODE *cast_type;
8736  PT_NODE *new_att;
8737  PT_TYPE_ENUM new_type;
8738  int first_node;
8739  PT_NODE *expr = NULL;
8740  bool check_expr_coll = true;
8741  DB_TYPE dbtype2 = DB_TYPE_UNKNOWN, dbtype3 = DB_TYPE_UNKNOWN;
8742 
8743  /* by the time we get here, the leaves have already been typed. this is because this function is called from a post
8744  * function of a parser_walk_tree, after all leaves have been visited. */
8745 
8746  op = node->info.expr.op;
8748  {
8749  /* handle special cases for the enumeration type */
8750  node = pt_fix_enumeration_comparison (parser, node);
8751  if (node == NULL)
8752  {
8753  return NULL;
8754  }
8755  op = node->info.expr.op;
8756  if (pt_has_error (parser))
8757  {
8758  goto error;
8759  }
8760  }
8761  /* shortcut for FUNCTION HOLDER */
8762  if (op == PT_FUNCTION_HOLDER)
8763  {
8764  if (pt_has_error (parser))
8765  {
8766  goto error;
8767  }
8768  PT_NODE *func = NULL;
8769  /* this may be a 2nd pass, tree may be already const folded */
8770  if (node->info.expr.arg1->node_type == PT_FUNCTION)
8771  {
8772  func = pt_eval_function_type (parser, node->info.expr.arg1);
8773  node->type_enum = func->type_enum;
8774  if (node->data_type == NULL && func->data_type != NULL)
8775  {
8776  node->data_type = parser_copy_tree (parser, func->data_type);
8777  }
8778  }
8779  else
8780  {
8781  assert (node->info.expr.arg1->node_type == PT_VALUE);
8782  }
8783  return node;
8784  }
8785 
8786 
8787  arg1 = node->info.expr.arg1;
8788  if (arg1)
8789  {
8790  arg1_type = arg1->type_enum;
8791 
8792  if (arg1->node_type == PT_HOST_VAR && arg1->type_enum == PT_TYPE_MAYBE)
8793  {
8794  arg1_hv = arg1;
8795  }
8796 
8797  /* Special case handling for unary operators on host variables (-?) or (prior ?) or (connect_by_root ?) */
8798  if (arg1->node_type == PT_EXPR
8799  && (arg1->info.expr.op == PT_UNARY_MINUS || arg1->info.expr.op == PT_PRIOR
8800  || arg1->info.expr.op == PT_CONNECT_BY_ROOT || arg1->info.expr.op == PT_QPRIOR
8801  || arg1->info.expr.op == PT_BIT_NOT || arg1->info.expr.op == PT_BIT_COUNT)
8802  && arg1->type_enum == PT_TYPE_MAYBE && arg1->info.expr.arg1->node_type == PT_HOST_VAR
8803  && arg1->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
8804  {
8805  arg1_hv = arg1->info.expr.arg1;
8806  }
8807  }
8808 
8809  arg2 = node->info.expr.arg2;
8810  if (arg2)
8811  {
8812  if (arg2->or_next == NULL)
8813  {
8814  arg2_type = arg2->type_enum;
8815  }
8816  else
8817  {
8818  PT_NODE *temp;
8819  PT_TYPE_ENUM temp_type;
8820 
8821  common_type = PT_TYPE_NONE;
8822  /* do traverse multi-args in RANGE operator */
8823  for (temp = arg2; temp; temp = temp->or_next)
8824  {
8825  temp_type = pt_common_type (arg1_type, temp->type_enum);
8826  if (temp_type != PT_TYPE_NONE)
8827  {
8828  common_type = (common_type == PT_TYPE_NONE) ? temp_type : pt_common_type (common_type, temp_type);
8829  }
8830  }
8831  arg2_type = common_type;
8832  }
8833 
8834  if (arg2->node_type == PT_HOST_VAR && arg2->type_enum == PT_TYPE_MAYBE)
8835  {
8836  arg2_hv = arg2;
8837  }
8838 
8839  /* Special case handling for unary operators on host variables (-?) or (prior ?) or (connect_by_root ?) */
8840  if (arg2->node_type == PT_EXPR
8841  && (arg2->info.expr.op == PT_UNARY_MINUS || arg2->info.expr.op == PT_PRIOR
8842  || arg2->info.expr.op == PT_CONNECT_BY_ROOT || arg2->info.expr.op == PT_QPRIOR
8843  || arg2->info.expr.op == PT_BIT_NOT || arg2->info.expr.op == PT_BIT_COUNT)
8844  && arg2->type_enum == PT_TYPE_MAYBE && arg2->info.expr.arg1->node_type == PT_HOST_VAR
8845  && arg2->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
8846  {
8847  arg2_hv = arg2->info.expr.arg1;
8848  }
8849  }
8850 
8851  arg3 = node->info.expr.arg3;
8852  if (arg3)
8853  {
8854  arg3_type = arg3->type_enum;
8855  if (arg3->node_type == PT_HOST_VAR && arg3->type_enum == PT_TYPE_MAYBE)
8856  {
8857  arg3_hv = arg3;
8858  }
8859  }
8860 
8861  /*
8862  * At this point, arg1_hv is non-NULL (and equal to arg1) if it represents
8863  * a dynamic host variable, i.e., a host var parameter that hasn't had
8864  * a value supplied at compile time. Same for arg2_hv and arg3_hv...
8865  */
8866  common_type = arg1_type;
8867  expr = node;
8868 
8869  /* adjust expression definition to fit the signature implementation */
8870  switch (op)
8871  {
8872  case PT_PLUS:
8873  if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
8874  {
8876  || (!PT_IS_STRING_TYPE (arg1_type) && !PT_IS_STRING_TYPE (arg2_type)))
8877  {
8878  node->type_enum = PT_TYPE_NULL;
8879  goto error;
8880  }
8881  }
8882  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
8883  {
8884  node->type_enum = PT_TYPE_MAYBE;
8885  goto cannot_use_signature;
8886  }
8888  {
8889  /* in mysql mode, PT_PLUS is not defined on date and number */
8890  break;
8891  }
8892  /* PT_PLUS has four overloads for which we cannot apply symmetric rule 1. DATE/TIME type + NUMBER 2. NUMBER +
8893  * DATE/TIME type 3. DATE/TIME type + STRING 4. STRING + DATE/TIME type STRING/NUMBER operand involved is
8894  * coerced to BIGINT. For these overloads, PT_PLUS is a syntactic sugar for the ADD_DATE expression. Even
8895  * though both PLUS and MINUS have this behavior, we cannot treat them in the same place because, for this
8896  * case, PT_PLUS is commutative and PT_MINUS isn't */
8897  if (PT_IS_DATE_TIME_TYPE (arg1_type)
8898  && (PT_IS_NUMERIC_TYPE (arg2_type) || PT_IS_CHAR_STRING_TYPE (arg2_type) || arg2_type == PT_TYPE_ENUMERATION
8899  || arg2_type == PT_TYPE_MAYBE))
8900  {
8901  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg2_type))
8902  {
8903  /* coerce first argument to BIGINT */
8904  int err = pt_coerce_expression_argument (parser, node, &arg2, PT_TYPE_BIGINT, NULL);
8905  if (err != NO_ERROR)
8906  {
8907  node->type_enum = PT_TYPE_NONE;
8908  goto error;
8909  }
8910  }
8911  node->info.expr.arg2 = arg2;
8912  node->type_enum = arg1_type;
8913  goto error;
8914  }
8915  if (PT_IS_DATE_TIME_TYPE (arg2_type)
8916  && (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_CHAR_STRING_TYPE (arg1_type) || arg1_type == PT_TYPE_ENUMERATION
8917  || arg1_type == PT_TYPE_MAYBE))
8918  {
8919  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg1_type))
8920  {
8921  int err = pt_coerce_expression_argument (parser, node, &arg1, PT_TYPE_BIGINT, NULL);
8922  if (err != NO_ERROR)
8923  {
8924  node->type_enum = PT_TYPE_NONE;
8925  goto error;
8926  }
8927  }
8928  node->info.expr.arg1 = arg1;
8929  node->type_enum = arg2_type;
8930  goto error;
8931  }
8932  break;
8933  case PT_MINUS:
8934  if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
8935  {
8936  node->type_enum = PT_TYPE_NULL;
8937  goto error;
8938  }
8939  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
8940  {
8941  node->type_enum = PT_TYPE_MAYBE;
8942  goto cannot_use_signature;
8943  }
8945  {
8946  /* in mysql mode - does is not defined on date and number */
8947  break;
8948  }
8949  if (PT_IS_DATE_TIME_TYPE (arg1_type) && (PT_IS_NUMERIC_TYPE (arg2_type) || arg2_type == PT_TYPE_ENUMERATION))
8950  {
8951  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg2_type))
8952  {
8953  /* coerce arg2 to bigint */
8954  int err = pt_coerce_expression_argument (parser, expr, &arg2, PT_TYPE_BIGINT, NULL);
8955  if (err != NO_ERROR)
8956  {
8957  node->type_enum = PT_TYPE_NONE;
8958  goto error;
8959  }
8960  node->info.expr.arg2 = arg2;
8961  }
8962  node->type_enum = arg1_type;
8963  goto error;
8964  }
8965  break;
8966  case PT_BETWEEN_AND:
8967  case PT_BETWEEN_GE_LE:
8968  case PT_BETWEEN_GE_LT:
8969  case PT_BETWEEN_GT_LE:
8970  case PT_BETWEEN_GT_LT:
8971  /* these expressions will be handled by PT_BETWEEN */
8972  node->type_enum = pt_common_type (arg1_type, arg2_type);
8973  goto error;
8974  break;
8975  case PT_BETWEEN:
8976  case PT_NOT_BETWEEN:
8977  /* between and range operators are written like: PT_BETWEEN(arg1, PT_BETWEEN_AND(arg2,arg3)) We convert it to
8978  * PT_BETWEEN(arg1, arg2, arg2) to be able to decide the correct common type of all arguments and we will
8979  * convert it back once we apply the correct casts */
8980  if (arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))
8981  {
8982  arg2 = node->info.expr.arg2;
8983  node->info.expr.arg2 = arg2->info.expr.arg1;
8984  node->info.expr.arg3 = arg2->info.expr.arg2;
8985  }
8986  break;
8987  case PT_LIKE:
8988  case PT_NOT_LIKE:
8989  /* [NOT] LIKE operators with an escape clause are parsed like PT_LIKE(arg1, PT_LIKE_ESCAPE(arg2, arg3)). We
8990  * convert it to PT_LIKE(arg1, arg2, arg3) to be able to decide the correct common type of all arguments and we
8991  * will convert it back once we apply the correct casts.
8992  *
8993  * A better approach would be to modify the parser to output PT_LIKE(arg1, arg2, arg3) directly. */
8994 
8995  if (arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_LIKE_ESCAPE)
8996  {
8997  arg2 = node->info.expr.arg2;
8998  node->info.expr.arg2 = arg2->info.expr.arg1;
8999  node->info.expr.arg3 = arg2->info.expr.arg2;
9000  }
9001  break;
9002 
9003  case PT_LIKE_LOWER_BOUND:
9004  case PT_LIKE_UPPER_BOUND:
9005  /* Check if arguments have been handled by PT_LIKE and only the result type needs to be set */
9006  if (arg1->type_enum == PT_TYPE_MAYBE && arg1->expected_domain)
9007  {
9009  goto error;
9010  }
9011  break;
9012 
9013  case PT_IS_IN:
9014  case PT_IS_NOT_IN:
9015  if (arg2->node_type == PT_VALUE)
9016  {
9017  if (PT_IS_COLLECTION_TYPE (arg2->type_enum) && arg2->info.value.data_value.set
9018  && arg2->info.value.data_value.set->next == NULL)
9019  {
9020  /* only one element in set. convert expr as EQ/NE expr. */
9021  PT_NODE *new_arg2;
9022 
9023  new_arg2 = arg2->info.value.data_value.set;
9024 
9025  /* free arg2 */
9026  arg2->info.value.data_value.set = NULL;
9027  parser_free_tree (parser, node->info.expr.arg2);
9028 
9029  /* rewrite arg2 */
9030  node->info.expr.arg2 = new_arg2;
9031  node->info.expr.op = (op == PT_IS_IN) ? PT_EQ : PT_NE;
9032  }
9033  else if (PT_IS_NULL_NODE (arg2))
9034  {
9035  return node;
9036  }
9037  }
9038  break;
9039 
9040  case PT_TO_CHAR:
9041  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9042  {
9043  arg1->line_number = node->line_number;
9044  arg1->column_number = node->column_number;
9045  arg1->alias_print = node->alias_print;
9046  node->alias_print = NULL;
9047  arg1->next = node->next;
9048  node->next = NULL;
9049  if (arg1->node_type == PT_EXPR)
9050  {
9051  arg1->info.expr.location = node->info.expr.location;
9052  }
9053  else if (arg1->node_type == PT_VALUE)
9054  {
9055  arg1->info.value.location = node->info.expr.location;
9056  }
9057  node->info.expr.arg1 = NULL;
9058  parser_free_node (parser, node);
9059 
9060  node = parser_copy_tree_list (parser, arg1);
9061  parser_free_node (parser, arg1);
9062 
9063  return node;
9064  }
9065  else if (PT_IS_NUMERIC_TYPE (arg1_type))
9066  {
9067  bool has_user_format = false;
9068  bool has_user_lang = false;
9069  const char *lang_str;
9070 
9071  assert (arg3 != NULL && arg3->node_type == PT_VALUE && arg3_type == PT_TYPE_INTEGER);
9072  /* change locale from date_lang (set by grammar) to number_lang */
9073  (void) lang_get_lang_id_from_flag (arg3->info.value.data_value.i, &has_user_format, &has_user_lang);
9074  if (!has_user_lang)
9075  {
9076  int lang_flag;
9078  (void) lang_set_flag_from_lang (lang_str, has_user_format, has_user_lang, &lang_flag);
9079  arg3->info.value.data_value.i = lang_flag;
9080  arg3->info.value.db_value_is_initialized = 0;
9081  pt_value_to_db (parser, arg3);
9082  }
9083  }
9084 
9085  break;
9086 
9087  case PT_FROM_TZ:
9088  case PT_NEW_TIME:
9089  {
9090  if (arg1_type != PT_TYPE_DATETIME && arg1_type != PT_TYPE_TIME && arg1_type != PT_TYPE_MAYBE)
9091  {
9092  node->type_enum = PT_TYPE_NULL;
9093  goto error;
9094  }
9095  }
9096  break;
9097 
9098  default:
9099  break;
9100  }
9101 
9102  if (pt_apply_expressions_definition (parser, &expr) != NO_ERROR)
9103  {
9104  expr = NULL;
9105  node->type_enum = PT_TYPE_NONE;
9106  goto error;
9107  }
9108 
9109  if (expr != NULL && PT_GET_COLLATION_MODIFIER (expr) != -1)
9110  {
9111  if (!PT_HAS_COLLATION (arg1_type) && !PT_HAS_COLLATION (arg2_type) && !PT_HAS_COLLATION (arg3_type)
9112  && !PT_HAS_COLLATION (node->type_enum) && (expr->info.expr.op != PT_CAST || arg1_type != PT_TYPE_MAYBE))
9113  {
9114  if (!pt_has_error (parser))
9115  {
9117  }
9118  node->type_enum = PT_TYPE_NONE;
9119  goto error;
9120  }
9121  }
9122 
9123  if (expr != NULL)
9124  {
9125  assert (check_expr_coll);
9126  if (pt_check_expr_collation (parser, &expr) != NO_ERROR)
9127  {
9128  expr = NULL;
9129  node->type_enum = PT_TYPE_NONE;
9130  return node;
9131  }
9132  }
9133 
9134  check_expr_coll = false;
9135 
9136  if (expr != NULL)
9137  {
9138  expr = pt_wrap_expr_w_exp_dom_cast (parser, expr);
9139 
9140  node = expr;
9141  expr = NULL;
9142 
9143  switch (op)
9144  {
9145  case PT_BETWEEN:
9146  case PT_NOT_BETWEEN:
9147  /* between and rage operators are written like: PT_BETWEEN(arg1, PT_BETWEEN_AND(arg2,arg3)) We convert it to
9148  * PT_BETWEEN(arg1, arg2, arg2) to be able to decide the correct common type of all arguments and we will
9149  * convert it back once we apply the correct casts */
9150  if (arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))
9151  {
9152  arg2->info.expr.arg1 = node->info.expr.arg2;
9153  arg2->info.expr.arg2 = node->info.expr.arg3;
9154  node->info.expr.arg2 = arg2;
9155  node->info.expr.arg3 = NULL;
9156  }
9157  break;
9158 
9159  case PT_LIKE:
9160  case PT_NOT_LIKE:
9161  /* convert PT_LIKE(arg1, arg2, arg3) back to PT_LIKE(arg1, PT_LIKE_ESCAPE(arg2, arg3)) A better approach
9162  * would be to modify the parser to output PT_LIKE(arg1, arg2, arg3) directly. */
9163  if (arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_LIKE_ESCAPE)
9164  {
9165 
9166  arg2->info.expr.arg1 = node->info.expr.arg2;
9167  arg2->info.expr.arg2 = node->info.expr.arg3;
9168  node->info.expr.arg2 = arg2;
9169  node->info.expr.arg3 = NULL;
9170  }
9171  break;
9172 
9173  case PT_RAND:
9174  case PT_RANDOM:
9175  case PT_DRAND:
9176  case PT_DRANDOM:
9177  /* to keep mysql compatibility we should consider a NULL argument as the value 0. This is the only place
9178  * where we can perform this check */
9179  arg1 = node->info.expr.arg1;
9180  if (arg1 && arg1->type_enum == PT_TYPE_NULL && arg1->node_type == PT_VALUE)
9181  {
9182  arg1->type_enum = arg1_type = PT_TYPE_INTEGER;
9183  db_make_int (&arg1->info.value.db_value, 0);
9184  }
9185  break;
9186 
9187  case PT_EXTRACT:
9188  if (arg1_type == PT_TYPE_MAYBE)
9189  {
9190  assert (node->type_enum == PT_TYPE_INTEGER);
9191  }
9192  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
9193  {
9194  node->type_enum = arg1_type;
9195  }
9196  else if (PT_IS_CHAR_STRING_TYPE (arg1_type) || PT_IS_DATE_TIME_TYPE (arg1_type))
9197  {
9198  int incompatible_extract_type = false;
9199 
9200  node->type_enum = PT_TYPE_NONE;
9201  switch (node->info.expr.qualifier)
9202  {
9203  case PT_YEAR:
9204  case PT_MONTH:
9205  case PT_DAY:
9206  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9207  {
9208  arg1_type = PT_TYPE_NONE;
9209  if (pt_check_and_coerce_to_date (parser, arg1) == NO_ERROR)
9210  {
9211  arg1_type = PT_TYPE_DATE;
9212  }
9213  else
9214  {
9215  parser_free_tree (parser, parser->error_msgs);
9216  parser->error_msgs = NULL;
9217 
9218  /* try coercing to utime */
9219  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
9220  {
9221  arg1_type = PT_TYPE_TIMESTAMP;
9222  }
9223  else
9224  {
9225  parser_free_tree (parser, parser->error_msgs);
9226  parser->error_msgs = NULL;
9227 
9228  /* try coercing to datetime */
9229  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9230  {
9231  arg1_type = PT_TYPE_DATETIME;
9232  }
9233  }
9234  }
9235  }
9236 
9237  if (PT_HAS_DATE_PART (arg1_type))
9238  {
9239  node->type_enum = PT_TYPE_INTEGER;
9240  }
9241  else if (arg1_type == PT_TYPE_TIME)
9242  {
9243  incompatible_extract_type = true;
9244  }
9245  break;
9246 
9247  case PT_HOUR:
9248  case PT_MINUTE:
9249  case PT_SECOND:
9250  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9251  {
9252  arg1_type = PT_TYPE_NONE;
9253  if (pt_check_and_coerce_to_time (parser, arg1) == NO_ERROR)
9254  {
9255  arg1_type = PT_TYPE_TIME;
9256  }
9257  else
9258  {
9259  parser_free_tree (parser, parser->error_msgs);
9260  parser->error_msgs = NULL;
9261 
9262  /* try coercing to utime */
9263  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
9264  {
9265  arg1_type = PT_TYPE_TIMESTAMP;
9266  }
9267  else
9268  {
9269  parser_free_tree (parser, parser->error_msgs);
9270  parser->error_msgs = NULL;
9271 
9272  /* try coercing to datetime */
9273  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9274  {
9275  arg1_type = PT_TYPE_DATETIME;
9276  }
9277  }
9278  }
9279  }
9280 
9281  if (PT_HAS_TIME_PART (arg1_type))
9282  {
9283  node->type_enum = PT_TYPE_INTEGER;
9284  }
9285  else if (arg1_type == PT_TYPE_DATE)
9286  {
9287  incompatible_extract_type = true;
9288  }
9289  break;
9290 
9291  case PT_MILLISECOND:
9292  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9293  {
9294  arg1_type = PT_TYPE_NONE;
9295  /* try coercing to datetime */
9296  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9297  {
9298  arg1_type = PT_TYPE_DATETIME;
9299  }
9300  }
9301 
9302  if (arg1_type == PT_TYPE_DATETIME || arg1_type == PT_TYPE_DATETIMELTZ
9303  || arg1_type == PT_TYPE_DATETIMETZ)
9304  {
9305  node->type_enum = PT_TYPE_INTEGER;
9306  }
9307  else if (arg1_type == PT_TYPE_DATE || arg1_type == PT_TYPE_TIME || arg1_type == PT_TYPE_TIMESTAMP
9308  || arg1_type == PT_TYPE_TIMESTAMPLTZ || arg1_type == PT_TYPE_TIMESTAMPTZ)
9309  {
9310  incompatible_extract_type = true;
9311  }
9312  break;
9313  default:
9314  break;
9315  }
9316 
9317  if (incompatible_extract_type)
9318  {
9321  return node;
9322  }
9323 
9324  if (node->type_enum != PT_TYPE_NONE && (node->data_type = parser_new_node (parser, PT_DATA_TYPE)) != NULL)
9325  {
9326  node->data_type->type_enum = node->type_enum;
9327  }
9328  }
9329  else
9330  {
9331  /* argument is not date, string, MAYBE or NULL */
9332  node->type_enum = PT_TYPE_NONE;
9333  }
9334  break;
9335 
9336  case PT_COALESCE:
9337  if (common_type != PT_TYPE_NONE && arg1_type != PT_TYPE_NA && arg1_type != PT_TYPE_NULL
9338  && arg2_type != PT_TYPE_NA && arg2_type != PT_TYPE_NULL && PT_IS_COLLECTION_TYPE (common_type))
9339  {
9340  pt_propagate_types (parser, node, arg1->data_type, arg2->data_type);
9341  }
9342  break;
9343 
9344  case PT_TIMEDIFF:
9345  if (PT_IS_DATE_TIME_TYPE (arg1_type) && PT_IS_DATE_TIME_TYPE (arg2_type))
9346  {
9347  if (arg1_type == PT_TYPE_TIME || arg1_type == PT_TYPE_DATE)
9348  {
9349  if (arg2_type != arg1_type)
9350  {
9351  node->type_enum = PT_TYPE_NONE;
9352  }
9353  }
9354  else
9355  {
9356  /* arg1_type is PT_TYPE_DATETIME or PT_TYPE_TIMESTAMP. */
9357  if (arg2_type == PT_TYPE_TIME || arg2_type == PT_TYPE_DATE)
9358  {
9359  node->type_enum = PT_TYPE_NONE;
9360  }
9361  }
9362  }
9363  break;
9364 
9365  case PT_FROM_TZ:
9366  if (arg1_type == PT_TYPE_DATETIME)
9367  {
9368  node->type_enum = PT_TYPE_DATETIMETZ;
9369  }
9370  break;
9371  default:
9372  break;
9373  }
9374  goto error;
9375  }
9376 
9377  if (pt_is_symmetric_op (op))
9378  {
9379  /*
9380  * At most one of these next two cases will hold... these will
9381  * make a dynamic host var (one about whose type we know nothing
9382  * at this point) assume the type of its "mate" in a symmetric
9383  * dyadic operator.
9384  */
9385  if (arg1_hv && arg2_type != PT_TYPE_NONE && arg2_type != PT_TYPE_MAYBE)
9386  {
9387  if (arg1_hv != arg1)
9388  {
9389  /* special case of the unary minus on host var no error will be returned in this case */
9390  (void) pt_coerce_value (parser, arg1_hv, arg1_hv, arg2_type, arg2->data_type);
9391  arg1_type = arg1->type_enum = arg1_hv->type_enum;
9392  d = pt_xasl_type_enum_to_domain (arg1_type);
9393  SET_EXPECTED_DOMAIN (arg1, d);
9394  SET_EXPECTED_DOMAIN (arg1_hv, d);
9395  pt_preset_hostvar (parser, arg1_hv);
9396  }
9397  else
9398  {
9399  /* no error will be returned in this case */
9400  (void) pt_coerce_value (parser, arg1, arg1, arg2_type, arg2->data_type);
9401  arg1_type = arg1->type_enum;
9402  d = pt_xasl_type_enum_to_domain (arg1_type);
9403  SET_EXPECTED_DOMAIN (arg1, d);
9404  pt_preset_hostvar (parser, arg1);
9405  }
9406  }
9407 
9408  if (arg2_hv && arg1_type != PT_TYPE_NONE && arg1_type != PT_TYPE_MAYBE)
9409  {
9410  if (arg2_hv != arg2)
9411  {
9412  /* special case of the unary minus on host var no error will be returned in this case */
9413  (void) pt_coerce_value (parser, arg2_hv, arg2_hv, arg1_type, arg1->data_type);
9414  arg2_type = arg2->type_enum = arg2_hv->type_enum;
9415  d = pt_xasl_type_enum_to_domain (arg2_type);
9416  SET_EXPECTED_DOMAIN (arg2, d);
9417  SET_EXPECTED_DOMAIN (arg2_hv, d);
9418  pt_preset_hostvar (parser, arg2_hv);
9419  }
9420  else
9421  {
9422  /* no error will be returned in this case */
9423  (void) pt_coerce_value (parser, arg2, arg2, arg1_type, arg1->data_type);
9424  arg2_type = arg2->type_enum;
9425  d = pt_xasl_type_enum_to_domain (arg2_type);
9426  SET_EXPECTED_DOMAIN (arg2, d);
9427  pt_preset_hostvar (parser, arg2);
9428  }
9429  }
9430 
9431  if (arg2)
9432  {
9433  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
9434  {
9435  /* In case of 'ENUM = const' or 'ENUM IN ...' we need to convert the right argument to the ENUM type in
9436  * order to preserve an eventual index scan on left argument */
9437  common_type = PT_TYPE_ENUMERATION;
9438  }
9439  else
9440  {
9441  common_type = pt_common_type_op (arg1_type, op, arg2_type);
9442  }
9443  }
9444 
9445  if (pt_is_symmetric_type (common_type))
9446  {
9447  PT_NODE *data_type;
9448 
9449  if (arg1_type != common_type)
9450  {
9451  /*
9452  * pt_coerce_value may fail here, but it shouldn't be
9453  * considered a real failure yet, because it could still
9454  * be rescued by the gruesome date/time stuff below.
9455  * DON'T set common_type here, or you'll surely screw up
9456  * the next case when you least expect it.
9457  */
9458  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
9459  {
9460  data_type = NULL;
9461  }
9462  else if (PT_IS_COLLECTION_TYPE (common_type))
9463  {
9464  data_type = arg1->data_type;
9465  }
9466  else
9467  {
9468  data_type = arg2->data_type;
9469  }
9470 
9471  pt_coerce_value (parser, arg1, arg1, common_type, data_type);
9472  arg1_type = arg1->type_enum;
9473  }
9474 
9475  if (arg2 && arg2_type != common_type)
9476  {
9477  /* Same warning as above... */
9478  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
9479  {
9480  data_type = NULL;
9481  }
9482  else if (PT_IS_COLLECTION_TYPE (common_type))
9483  {
9484  data_type = arg2->data_type;
9485  }
9486  else
9487  {
9488  data_type = arg1->data_type;
9489  }
9490 
9491  pt_coerce_value (parser, arg2, arg2, common_type, data_type);
9492  arg2_type = arg2->type_enum;
9493  }
9494  }
9495  }
9496  else
9497  {
9499  {
9500  if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
9501  {
9502  if (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type))
9503  {
9504  d = pt_node_to_db_domain (parser, arg1, NULL);
9505  d = tp_domain_cache (d);
9506  SET_EXPECTED_DOMAIN (arg2, d);
9507  if (arg2->node_type == PT_HOST_VAR)
9508  {
9509  pt_preset_hostvar (parser, arg2);
9510  }
9511  }
9512  }
9513 
9514  if (PT_IS_FUNCTION (arg2) && PT_IS_COLLECTION_TYPE (arg2->type_enum))
9515  {
9516  /* a IN (?, ...) */
9517  PT_NODE *temp;
9518 
9519  for (temp = arg2->info.function.arg_list; temp; temp = temp->next)
9520  {
9521  if (temp->node_type == PT_HOST_VAR && temp->type_enum == PT_TYPE_MAYBE)
9522  {
9523  if (arg1_type != PT_TYPE_NONE && arg1_type != PT_TYPE_MAYBE)
9524  {
9525  (void) pt_coerce_value (parser, temp, temp, arg1_type, arg1->data_type);
9527  SET_EXPECTED_DOMAIN (temp, d);
9528  if (temp->node_type == PT_HOST_VAR)
9529  {
9530  pt_preset_hostvar (parser, temp);
9531  }
9532  }
9533  }
9534  }
9535  }
9536 
9537  if (arg3 && arg3->type_enum == PT_TYPE_MAYBE)
9538  {
9539  if (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type))
9540  {
9541  d = pt_node_to_db_domain (parser, arg1, NULL);
9542  d = tp_domain_cache (d);
9543  SET_EXPECTED_DOMAIN (arg3, d);
9544  if (arg3->node_type == PT_HOST_VAR)
9545  {
9546  pt_preset_hostvar (parser, arg3);
9547  }
9548  }
9549  }
9550  }
9551  }
9552 
9553  if ((common_type == PT_TYPE_NA || common_type == PT_TYPE_NULL) && node->or_next)
9554  {
9555  common_type = node->or_next->type_enum;
9556  }
9557 
9558  node->type_enum = common_type;
9559 
9561  {
9562  /* Because we can determine the return type of the expression regardless of its argument, go further to determine
9563  * it. temporary reset to NONE. */
9564  node->type_enum = PT_TYPE_NONE;
9565  }
9566 
9567  if (node->type_enum == PT_TYPE_MAYBE)
9568  {
9569  /* There can be a unbinded host variable at compile time */
9571  {
9572  /* don't touch the args. leave it as it is */
9573  return node;
9574  }
9575 
9576  if (arg1_type == PT_TYPE_MAYBE)
9577  {
9578  if (node->expected_domain
9581  {
9582  SET_EXPECTED_DOMAIN (arg1, node->expected_domain);
9583  if (arg1->node_type == PT_HOST_VAR)
9584  {
9585  pt_preset_hostvar (parser, arg1);
9586  }
9587  }
9588  else if (arg2 && (PT_IS_NUMERIC_TYPE (arg2_type) || PT_IS_STRING_TYPE (arg2_type)))
9589  {
9590  if (arg1->node_type == PT_NAME)
9591  { /* subquery derived table */
9592  PT_NODE *new_att;
9593  int p, s;
9594 
9595  if (arg2->data_type)
9596  {
9597  p = arg2->data_type->info.data_type.precision;
9598  s = arg2->data_type->info.data_type.dec_precision;
9599  }
9600  else
9601  {
9604  }
9605 
9606  new_att = pt_wrap_with_cast_op (parser, arg1, arg2_type, p, s, arg2->data_type);
9607  if (new_att == NULL)
9608  {
9609  node->type_enum = PT_TYPE_NONE;
9610  goto error;
9611  }
9612  node->info.expr.arg1 = arg1 = new_att;
9613  arg1_type = arg2_type;
9614  }
9615  else
9616  { /* has a hostvar */
9617  d = pt_node_to_db_domain (parser, arg2, NULL);
9618  d = tp_domain_cache (d);
9619  SET_EXPECTED_DOMAIN (arg1, d);
9620  if (arg1->node_type == PT_HOST_VAR)
9621  {
9622  pt_preset_hostvar (parser, arg1);
9623  }
9624  }
9625  }
9626  }
9627 
9628  if (arg2_type == PT_TYPE_MAYBE)
9629  {
9630  if (node->expected_domain
9633  {
9634  SET_EXPECTED_DOMAIN (arg2, node->expected_domain);
9635  if (arg2->node_type == PT_HOST_VAR)
9636  {
9637  pt_preset_hostvar (parser, arg2);
9638  }
9639  }
9640  else if (arg1 && (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type)))
9641  {
9642  if (arg2->node_type == PT_NAME)
9643  { /* subquery derived table */
9644  PT_NODE *new_att;
9645  int p, s;
9646 
9647  if (arg1->data_type)
9648  {
9649  p = arg1->data_type->info.data_type.precision;
9651  }
9652  else
9653  {
9656  }
9657 
9658  new_att = pt_wrap_with_cast_op (parser, arg2, arg1_type, p, s, arg1->data_type);
9659  if (new_att == NULL)
9660  {
9661  node->type_enum = PT_TYPE_NONE;
9662  goto error;
9663  }
9664  node->info.expr.arg2 = arg2 = new_att;
9665  arg2_type = arg1_type;
9666  }
9667  else
9668  { /* has a hostvar */
9669  d = pt_node_to_db_domain (parser, arg1, NULL);
9670  d = tp_domain_cache (d);
9671  SET_EXPECTED_DOMAIN (arg2, d);
9672  if (arg2->node_type == PT_HOST_VAR)
9673  {
9674  pt_preset_hostvar (parser, arg2);
9675  }
9676  }
9677  }
9678  }
9679 
9680  return node;
9681  } /* if node->type_enum == PT_TYPE_MAYBE */
9682 
9683  switch (op)
9684  {
9685  case PT_IF:
9686  if (arg1_type == PT_TYPE_MAYBE)
9687  {
9689  SET_EXPECTED_DOMAIN (arg1, d);
9690  pt_preset_hostvar (parser, arg1);
9691  }
9692  else if (arg1_type != PT_TYPE_LOGICAL && arg2_type != PT_TYPE_NULL)
9693  {
9694  node->type_enum = PT_TYPE_NONE;
9695  break;
9696  }
9697  if (arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE)
9698  {
9699  if (arg2_type == PT_TYPE_MAYBE && arg3_type == PT_TYPE_MAYBE)
9700  {
9701  if (node->expected_domain != NULL)
9702  {
9703  /* the expected domain might have been set by a different pass through the tree */
9704  common_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (node->expected_domain));
9705  }
9706  else
9707  {
9708  common_type = PT_TYPE_VARCHAR;
9709  }
9710  }
9711  else
9712  {
9713  common_type = (arg2_type == PT_TYPE_MAYBE) ? arg3_type : arg2_type;
9714  }
9715  }
9716  else
9717  {
9718  /* We have to decide a common type for arg2 and arg3. We cannot use pt_common_type_op because this function
9719  * is designed mostly for arithmetic expression. This is why we use the tp_is_more_general_type here. */
9720  dbtype2 = pt_type_enum_to_db (arg2_type);
9721  dbtype3 = pt_type_enum_to_db (arg3_type);
9722  if (tp_more_general_type (dbtype2, dbtype3) > 0)
9723  {
9724  common_type = arg2_type;
9725  }
9726  else
9727  {
9728  common_type = arg3_type;
9729  }
9730  }
9731  if (common_type == PT_TYPE_LOGICAL)
9732  {
9733  /* we will end up with logical here if arg3 and arg2 are logical */
9734  common_type = PT_TYPE_INTEGER;
9735  }
9736  // CBRD-22431 hack:
9737  // we have an issue with different precision domains when value is packed into a list file and then used by a
9738  // list scan. in the issue, the value is folded and packed with fixed precision, but the unpacking expects
9739  // no precision, corrupting the read.
9740  // next line is a quick fix to force no precision domain; however, we should consider a more robus list scan
9741  // implementation that always matches domains used to generate the list file
9742  common_type = pt_to_variable_size_type (common_type);
9743  if (pt_coerce_expression_argument (parser, node, &arg2, common_type, NULL) != NO_ERROR)
9744  {
9745  node->type_enum = PT_TYPE_NONE;
9746  break;
9747  }
9748  node->info.expr.arg2 = arg2;
9749 
9750  if (pt_coerce_expression_argument (parser, node, &arg3, common_type, NULL) != NO_ERROR)
9751  {
9752  node->type_enum = PT_TYPE_NONE;
9753  break;
9754  }
9755  node->info.expr.arg3 = arg3;
9756  node->type_enum = common_type;
9757 
9758  if (PT_HAS_COLLATION (common_type))
9759  {
9760  check_expr_coll = true;
9761  }
9762  break;
9763 
9764  case PT_FIELD:
9765  if ((arg1 && arg1_type == PT_TYPE_LOGICAL) || (arg2 && arg2_type == PT_TYPE_LOGICAL)
9766  || pt_list_has_logical_nodes (arg3))
9767  {
9768  node->type_enum = PT_TYPE_NONE;
9769  break;
9770  }
9771 
9772  check_expr_coll = true;
9773 
9774  node->type_enum = common_type = PT_TYPE_INTEGER;
9775 
9776  if (arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_NA && arg3_type != PT_TYPE_MAYBE)
9777  {
9778  first_node = (arg3->next && arg3->next->info.value.data_value.i == 1);
9779 
9780  if (!((first_node ? PT_IS_STRING_TYPE (arg1_type) : true)
9781  && (arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA ? PT_IS_STRING_TYPE (arg2_type) : true)
9782  && (PT_IS_STRING_TYPE (arg3_type) || arg3_type == PT_TYPE_ENUMERATION))
9783  && !((first_node ? PT_IS_NUMERIC_TYPE (arg1_type) : true)
9784  && (arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA ? PT_IS_NUMERIC_TYPE (arg2_type) : true)
9785  && (PT_IS_NUMERIC_TYPE (arg3_type) || arg3_type == PT_TYPE_ENUMERATION)))
9786  {
9787  /* cast to type of first parameter */
9788 
9789  if (arg3_type == PT_TYPE_CHAR)
9790  {
9791  new_type = PT_TYPE_VARCHAR;
9792  }
9793  else if (arg3_type == PT_TYPE_NCHAR)
9794  {
9795  new_type = PT_TYPE_VARNCHAR;
9796  }
9797  else
9798  {
9799  new_type = arg3_type;
9800  }
9801 
9802  if (first_node)
9803  {
9804  new_att = pt_wrap_with_cast_op (parser, arg1, new_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
9805  if (new_att == NULL)
9806  {
9807  node->type_enum = PT_TYPE_NONE;
9808  goto error;
9809  }
9810  assert (new_att->node_type == PT_EXPR);
9812  node->info.expr.arg1 = arg1 = new_att;
9813  arg1_type = new_type;
9814  }
9815 
9816  new_att = pt_wrap_with_cast_op (parser, arg2, new_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
9817  if (new_att == NULL)
9818  {
9819  node->type_enum = PT_TYPE_NONE;
9820  goto error;
9821  }
9822  assert (new_att->node_type == PT_EXPR);
9824  node->info.expr.arg2 = arg2 = new_att;
9825  arg2_type = new_type;
9826  }
9827  }
9828  break;
9829 
9830  case PT_CONNECT_BY_ROOT:
9831  node->type_enum = node->info.expr.arg1->type_enum;
9832  break;
9833 
9834  case PT_ASSIGN:
9835  node->data_type = parser_copy_tree_list (parser, arg1->data_type);
9836  node->type_enum = arg1_type;
9837 
9838  if (PT_IS_HOSTVAR (arg2) && arg2->expected_domain == NULL)
9839  {
9840  d = pt_node_to_db_domain (parser, arg1, NULL);
9841  d = tp_domain_cache (d);
9842  SET_EXPECTED_DOMAIN (arg2, d);
9843  pt_preset_hostvar (parser, arg2);
9844  }
9845  if (PT_IS_VALUE_NODE (arg2))
9846  {
9847  if (pt_coerce_value_explicit (parser, arg2, arg2, arg1_type, arg1->data_type) != NO_ERROR)
9848  {
9849  goto error;
9850  }
9851  }
9852  break;
9853 
9854  case PT_LIKE_ESCAPE:
9855  /* PT_LIKE_ESCAPE is handled by PT_LIKE */
9856  break;
9857 
9858  case PT_EXISTS:
9859  if (common_type != PT_TYPE_NONE && (pt_is_query (arg1) || PT_IS_COLLECTION_TYPE (arg1_type)))
9860  {
9861  node->type_enum = PT_TYPE_LOGICAL;
9862  if (pt_is_query (arg1))
9863  {
9864  pt_chop_to_one_select_item (parser, arg1);
9865  }
9866  }
9867  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
9868  {
9869  node->type_enum = PT_TYPE_NULL;
9870  }
9871  else
9872  {
9873  node->type_enum = PT_TYPE_NONE;
9874  }
9875  break;
9876 
9877  case PT_RANGE:
9878  node->type_enum = PT_TYPE_NONE;
9879  if (arg2 == NULL)
9880  {
9881  goto error;
9882  }
9883  node->type_enum = PT_TYPE_LOGICAL;
9884  break;
9885 
9887  /* The argument should already have the type of the spec's OID; see pt_dup_key_update_stmt () */
9888  node->data_type = parser_copy_tree (parser, node->info.expr.arg1->data_type);
9889  node->type_enum = PT_TYPE_OBJECT;
9890  break;
9891 
9892  case PT_STR_TO_DATE:
9893  {
9894  int type_specifier = 0;
9895 
9896  assert (arg3_type == PT_TYPE_INTEGER);
9897 
9898  if (arg2_type == PT_TYPE_NULL)
9899  {
9900  node->type_enum = PT_TYPE_NULL;
9901  break;
9902  }
9903  if (!PT_IS_VALUE_NODE (arg2) && !PT_IS_INPUT_HOSTVAR (arg2)
9904  && !(arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_EVALUATE_VARIABLE))
9905  {
9906  node->type_enum = PT_TYPE_NONE;
9907  break;
9908  }
9909  if (!PT_IS_CHAR_STRING_TYPE (arg1_type))
9910  {
9911  if (arg1_type == PT_TYPE_MAYBE)
9912  {
9914  SET_EXPECTED_DOMAIN (arg1, d);
9915  pt_preset_hostvar (parser, arg1);
9916  }
9917  else
9918  {
9920  if (new_att == NULL)
9921  {
9922  node->type_enum = PT_TYPE_NONE;
9923  goto error;
9924  }
9926  {
9927  assert (new_att->node_type == PT_EXPR);
9929  }
9930  node->info.expr.arg1 = arg1 = new_att;
9931  }
9932  }
9933 
9934  if (arg2_hv && arg2_type == PT_TYPE_MAYBE)
9935  {
9937  SET_EXPECTED_DOMAIN (arg2, d);
9938  pt_preset_hostvar (parser, arg2);
9939  }
9940 
9941  if (arg2->node_type == PT_VALUE)
9942  {
9943  type_specifier = db_check_time_date_format ((char *) arg2->info.value.data_value.str->bytes);
9944  }
9945  else
9946  {
9947  type_specifier = PT_TYPE_MAYBE;
9948  }
9949 
9950  /* default is date (i.e.: -> when no format supplied) */
9951  node->type_enum = PT_TYPE_DATE;
9952  if (type_specifier == TIME_SPECIFIER)
9953  {
9954  node->type_enum = PT_TYPE_TIME;
9955  }
9956  else if (type_specifier == DATE_SPECIFIER)
9957  {
9958  node->type_enum = PT_TYPE_DATE;
9959  }
9960  else if (type_specifier == DATETIME_SPECIFIER)
9961  {
9962  node->type_enum = PT_TYPE_DATETIME;
9963  }
9964  else if (type_specifier == DATETIMETZ_SPECIFIER)
9965  {
9966  node->type_enum = PT_TYPE_DATETIMETZ;
9967  }
9968  else if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
9969  {
9970  /* if other value, the db_str_to_date will return NULL */
9971  node->type_enum = PT_TYPE_NULL;
9972  }
9973  else if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
9974  {
9975  node->type_enum = PT_TYPE_MAYBE;
9976  }
9977  break;
9978  }
9979 
9980  case PT_DATE_ADD:
9981  case PT_DATE_SUB:
9982  if (arg1_hv && arg1_type == PT_TYPE_MAYBE)
9983  {
9984  /* Though arg1 can be date/timestamp/datetime/string, assume it is a string which is the most general. */
9986  SET_EXPECTED_DOMAIN (arg1, d);
9987  pt_preset_hostvar (parser, arg1);
9988  common_type = node->type_enum = PT_TYPE_VARCHAR;
9989  }
9990  if (arg2_hv && arg2_type == PT_TYPE_MAYBE)
9991  {
9993  SET_EXPECTED_DOMAIN (arg2, d);
9994  pt_preset_hostvar (parser, arg2);
9995  }
9996 
9997  /* arg1 -> date or string, arg2 -> integer or string acc to unit */
9998  if ((PT_HAS_DATE_PART (arg1_type) || PT_IS_CHAR_STRING_TYPE (arg1_type) || arg1_type == PT_TYPE_MAYBE)
9999  && (PT_IS_CHAR_STRING_TYPE (arg2_type) || PT_IS_NUMERIC_TYPE (arg2_type) || arg2_type == PT_TYPE_MAYBE))
10000  {
10001  /* if arg2 is integer, unit must be one of MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER,
10002  * YEAR. */
10003  int is_single_unit;
10004 
10005  is_single_unit = (arg3
10006  && (arg3->info.expr.qualifier == PT_MILLISECOND || arg3->info.expr.qualifier == PT_SECOND
10007  || arg3->info.expr.qualifier == PT_MINUTE || arg3->info.expr.qualifier == PT_HOUR
10008  || arg3->info.expr.qualifier == PT_DAY || arg3->info.expr.qualifier == PT_WEEK
10009  || arg3->info.expr.qualifier == PT_MONTH || arg3->info.expr.qualifier == PT_QUARTER
10010  || arg3->info.expr.qualifier == PT_YEAR));
10011 
10012  if (arg1_type == PT_TYPE_DATETIMETZ || arg1_type == PT_TYPE_TIMESTAMPTZ)
10013  {
10014  node->type_enum = PT_TYPE_DATETIMETZ;
10015  }
10016  else if (arg1_type == PT_TYPE_DATETIMELTZ || arg1_type == PT_TYPE_TIMESTAMPLTZ)
10017  {
10019  }
10020  else if (arg1_type == PT_TYPE_DATETIME || arg1_type == PT_TYPE_TIMESTAMP)
10021  {
10022  node->type_enum = PT_TYPE_DATETIME;
10023  }
10024  else if (arg1_type == PT_TYPE_DATE)
10025  {
10026  if (arg3->info.expr.qualifier == PT_DAY || arg3->info.expr.qualifier == PT_WEEK
10027  || arg3->info.expr.qualifier == PT_MONTH || arg3->info.expr.qualifier == PT_QUARTER
10028  || arg3->info.expr.qualifier == PT_YEAR || arg3->info.expr.qualifier == PT_YEAR_MONTH)
10029  {
10030  node->type_enum = PT_TYPE_DATE;
10031  }
10032  else
10033  {
10034  node->type_enum = PT_TYPE_DATETIME;
10035  }
10036  }
10037  else
10038  {
10039  node->type_enum = PT_TYPE_VARCHAR;
10040  common_type = node->type_enum;
10041  }
10042  }
10043  else if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
10044  {
10045  node->type_enum = PT_TYPE_NULL;
10046  }
10047  else
10048  {
10049  /* if we got here, we have a type incompatibility; however, if one of the arguments is of type MAYBE, the
10050  * error will not be caught. NOTE: see label 'error' at end of function */
10051  if (arg1 && arg1->type_enum == PT_TYPE_MAYBE)
10052  {
10053  arg1 = NULL;
10054  }
10055 
10056  if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
10057  {
10058  arg2 = NULL;
10059  }
10060 
10061  if (arg3 && arg3->type_enum == PT_TYPE_MAYBE)
10062  {
10063  arg3 = NULL;
10064  }
10065 
10066  /* set type to NONE so error message is shown */
10067  node->type_enum = PT_TYPE_NONE;
10068  }
10069  break;
10070  case PT_CAST:
10071  cast_type = node->info.expr.cast_type;
10072 
10073  if (PT_EXPR_INFO_IS_FLAGED (node, PT_EXPR_INFO_CAST_COLL_MODIFIER) && cast_type == NULL)
10074  {
10075  if (arg1->type_enum == PT_TYPE_ENUMERATION)
10076  {
10077  LANG_COLLATION *lc;
10078 
10080 
10081  /* silently rewrite the COLLATE modifier into full CAST: CAST (ENUM as STRING) */
10082  cast_type = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
10084  cast_type->info.data_type.units = lc->codeset;
10085  }
10086  else
10087  {
10088  /* cast_type should be the same as arg1 type */
10089  cast_type = parser_copy_tree (parser, arg1->data_type);
10090  }
10091 
10092  /* for HV argument, attempt to resolve using the expected domain of expression's node or arg1 node */
10093  if (cast_type == NULL && node->expected_domain != NULL
10095  {
10096  cast_type = pt_domain_to_data_type (parser, node->expected_domain);
10097  }
10098 
10099  if (cast_type == NULL && arg1->expected_domain != NULL
10101  {
10102  /* create data type from expected domain */
10103  cast_type = pt_domain_to_data_type (parser, arg1->expected_domain);
10104  }
10105 
10106  if (cast_type != NULL)
10107  {
10108  node->info.expr.cast_type = cast_type;
10110  }
10111  }
10112 
10113  if (cast_type && pt_check_cast_op (parser, node))
10114  {
10115  node->type_enum = cast_type->type_enum;
10116  if (pt_is_set_type (cast_type))
10117  {
10118  /* use canonical set data type */
10119  node->data_type = parser_copy_tree_list (parser, cast_type->data_type);
10120  }
10121  else if (PT_IS_COMPLEX_TYPE (cast_type->type_enum))
10122  {
10123  node->data_type = parser_copy_tree_list (parser, cast_type);
10124  }
10125 
10126  /* TODO : this requires a generic fix: maybe 'arg1_hv' should never be set to arg1->info.expr.arg1; arg1 may
10127  * not be necessarily a HV node, but arg1_hv may be a direct link to argument of arg1 (in case arg1 is an
10128  * expression with unary operator)- see code at beginning of function when arguments are checked; for now,
10129  * this fix is enough for PT_CAST ( PT_UNARY_MINUS (.. ) */
10130  if (arg1_hv && arg1_type == PT_TYPE_MAYBE && arg1->node_type == PT_HOST_VAR)
10131  {
10132  d = pt_node_to_db_domain (parser, node, NULL);
10133  d = tp_domain_cache (d);
10134  SET_EXPECTED_DOMAIN (arg1, d);
10135  pt_preset_hostvar (parser, arg1);
10136  }
10137  }
10138  else
10139  {
10140  node->type_enum = PT_TYPE_NONE;
10141  }
10142  break;
10143 
10144  case PT_DECODE:
10145  case PT_CASE:
10146  if (arg3_hv && pt_coerce_value (parser, arg3, arg3, PT_TYPE_LOGICAL, NULL) != NO_ERROR)
10147  {
10148  node->type_enum = PT_TYPE_NONE;
10149  break;
10150  }
10151 
10152  if (arg3_type != PT_TYPE_NA && arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_LOGICAL
10153  && arg3_type != PT_TYPE_MAYBE)
10154  {
10156  return node;
10157  }
10158 
10159  arg3 = NULL;
10160  common_type = node->info.expr.recursive_type;
10161  if (common_type == PT_TYPE_NONE)
10162  {
10163  common_type = pt_common_type_op (arg1_type, op, arg2_type);
10164  }
10165  if (common_type == PT_TYPE_NONE)
10166  {
10167  node->type_enum = PT_TYPE_NONE;
10168  break;
10169  }
10170  if (common_type == PT_TYPE_MAYBE)
10171  {
10172  common_type = (arg1_type == PT_TYPE_MAYBE && arg2_type != PT_TYPE_NULL) ? arg2_type : arg1_type;
10173  }
10174 
10175  if (common_type == PT_TYPE_MAYBE)
10176  {
10177  /* both args are MAYBE, default to VARCHAR */
10178  common_type = PT_TYPE_VARCHAR;
10179  }
10180 
10181  if (PT_IS_COLLECTION_TYPE (common_type))
10182  {
10183  /* we're not casting collections */
10184  pt_propagate_types (parser, node, arg1->data_type, arg2->data_type);
10185  node->type_enum = common_type;
10186  break;
10187  }
10188  node->type_enum = common_type;
10189  if (arg1_type != common_type && arg1_type != PT_TYPE_NULL)
10190  {
10191  /* cast arg1_type to common type */
10192  if (pt_coerce_expression_argument (parser, node, &arg1, common_type, NULL) != NO_ERROR)
10193  {
10194  node->type_enum = PT_TYPE_NONE;
10195  goto error;
10196  }
10197  node->info.expr.arg1 = arg1;
10198  }
10199 
10200  if (arg2_type != common_type)
10201  {
10202  /* arg2 is a case list, we need to walk it and cast all arguments */
10203  PT_NODE *nextcase = arg2;
10204  PT_NODE *prev = node;
10205  while (nextcase)
10206  {
10207  if (nextcase->type_enum == common_type || nextcase->type_enum == PT_TYPE_NULL)
10208  {
10209  break;
10210  }
10211 
10212  if (nextcase->node_type == PT_EXPR
10213  && (nextcase->info.expr.op == PT_CASE || nextcase->info.expr.op == PT_DECODE))
10214  {
10215  /* cast nextcase->arg1 to common type */
10216  PT_NODE *next_arg1 = nextcase->info.expr.arg1;
10217  PT_TYPE_ENUM next_arg1_type = next_arg1->type_enum;
10218  if (next_arg1_type != common_type && next_arg1_type != PT_TYPE_NULL)
10219  {
10220  if (pt_coerce_expression_argument (parser, nextcase, &next_arg1, common_type, NULL) != NO_ERROR)
10221  {
10222  /* abandon implicit casting and return error */
10223  node->type_enum = PT_TYPE_NONE;
10224  goto error;
10225  }
10226  nextcase->info.expr.arg1 = next_arg1;
10227  /* nextcase was already evaluated and may have a data_type set. We need to replace it with the
10228  * cast data_type */
10229  nextcase->type_enum = common_type;
10230  if (nextcase->data_type)
10231  {
10232  parser_free_tree (parser, nextcase->data_type);
10233  nextcase->data_type = parser_copy_tree_list (parser, next_arg1->data_type);
10234  }
10235  }
10236  /* set nextcase to nextcase->arg2 and continue */
10237  prev = nextcase;
10238  nextcase = nextcase->info.expr.arg2;
10239  continue;
10240  }
10241  else
10242  {
10243  /* cast nextcase to common type */
10244  if (pt_coerce_expression_argument (parser, prev, &nextcase, common_type, NULL) != NO_ERROR)
10245  {
10246  /* abandon implicit casting and return error */
10247  node->type_enum = PT_TYPE_NONE;
10248  goto error;
10249  }
10250  prev->info.expr.arg2 = nextcase;
10251  nextcase = NULL;
10252  }
10253  }
10254  }
10255  break;
10256 
10257  case PT_PATH_EXPR_SET:
10258  /* temporary setting with the first element */
10259  node->type_enum = node->info.expr.arg1->type_enum;
10260  break;
10261 
10262  default:
10263  node->type_enum = PT_TYPE_NONE;
10264  break;
10265  }
10266 
10267  if (PT_IS_PARAMETERIZED_TYPE (common_type)
10268  && pt_upd_domain_info (parser, (op == PT_IF) ? arg3 : arg1, arg2, op, common_type, node) != NO_ERROR)
10269  {
10270  node->type_enum = PT_TYPE_NONE;
10271  }
10272 
10273 cannot_use_signature:
10274  if (expr != NULL)
10275  {
10276  expr = pt_wrap_expr_w_exp_dom_cast (parser, expr);
10277  node = expr;
10278  expr = NULL;
10279  }
10280 
10281 error:
10282  if (node->type_enum == PT_TYPE_NONE)
10283  {
10284  if ((arg1 && arg1->type_enum == PT_TYPE_MAYBE) || (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
10285  || (arg3 && arg3->type_enum == PT_TYPE_MAYBE))
10286  {
10287  node->type_enum = PT_TYPE_MAYBE;
10288  }
10289  }
10290 
10291  if (check_expr_coll && node->type_enum != PT_TYPE_NONE)
10292  {
10293  if (pt_check_expr_collation (parser, &node) != NO_ERROR)
10294  {
10295  node->type_enum = PT_TYPE_NONE;
10296  return node;
10297  }
10298  }
10299 
10300  if (node->type_enum == PT_TYPE_NONE)
10301  {
10302  if (!pt_has_error (parser))
10303  {
10304  if (arg2 && arg3)
10305  {
10306  assert (arg1 != NULL);
10308  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum),
10309  pt_show_type_enum (arg2->type_enum), pt_show_type_enum (arg3->type_enum));
10310  }
10311  else if (arg2)
10312  {
10313  assert (arg1 != NULL);
10315  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum),
10316  pt_show_type_enum (arg2->type_enum));
10317  }
10318  else if (arg1)
10319  {
10321  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum));
10322  }
10323  else
10324  {
10327  }
10328  }
10329  }
10330 
10331  return node;
10332 }
10333 
10334 /*
10335  * pt_eval_opt_type () -
10336  * return:
10337  * parser(in):
10338  * node(in/out):
10339  */
10340 static PT_NODE *
10342 {
10344  PT_NODE *arg1, *arg2;
10345 
10346  switch (node->node_type)
10347  {
10348  case PT_GET_OPT_LVL:
10349  option = node->info.get_opt_lvl.option;
10350  if (option == PT_OPT_COST)
10351  {
10352  arg1 = node->info.get_opt_lvl.args;
10353  if (PT_IS_CHAR_STRING_TYPE (arg1->type_enum))
10354  {
10355  node->type_enum = PT_TYPE_VARCHAR;
10356  }
10357  else
10358  {
10361  node->type_enum = PT_TYPE_NONE;
10362  node = NULL;
10363  }
10364  }
10365  else
10366  {
10367  node->type_enum = PT_TYPE_INTEGER;
10368  }
10369  break;
10370 
10371  case PT_SET_OPT_LVL:
10372  node->type_enum = PT_TYPE_NONE;
10373  option = node->info.set_opt_lvl.option;
10374  arg1 = node->info.set_opt_lvl.val;
10375 
10376  switch (option)
10377  {
10378  case PT_OPT_LVL:
10379  if (arg1->type_enum != PT_TYPE_INTEGER)
10380  {
10383  node = NULL;
10384  }
10385  break;
10386 
10387  case PT_OPT_COST:
10388  arg2 = arg1->next;
10390  {
10393  node = NULL;
10394  }
10395  break;
10396 
10397  default:
10398  break;
10399  }
10400  break;
10401 
10402  default:
10403  break;
10404  }
10405 
10406  return node;
10407 }
10408 
10409 /*
10410  * pt_common_type () -
10411  * return: returns the type into which these two types can be coerced
10412  * or PT_TYPE_NONE if no such type exists
10413  * arg1_type(in): a data type
10414  * arg2_type(in): a data type
10415  */
10416 
10419 {
10420  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
10421 
10422  if (arg1_type == arg2_type)
10423  {
10424  if (arg1_type == PT_TYPE_ENUMERATION)
10425  {
10426  /* The common type between two ENUMs is string */
10427  common_type = PT_TYPE_VARCHAR;
10428  }
10429  else
10430  {
10431  common_type = arg1_type;
10432  }
10433  }
10434  else if ((PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_STRING_TYPE (arg2_type))
10435  || (PT_IS_NUMERIC_TYPE (arg2_type) && PT_IS_STRING_TYPE (arg1_type)))
10436  {
10437  common_type = PT_TYPE_DOUBLE;
10438  }
10439  else if ((PT_IS_STRING_TYPE (arg1_type) && arg2_type == PT_TYPE_JSON)
10440  || (arg1_type == PT_TYPE_JSON && PT_IS_STRING_TYPE (arg2_type)))
10441  {
10442  common_type = PT_TYPE_VARCHAR;
10443  }
10444  else if ((PT_IS_NUMERIC_TYPE (arg1_type) && arg2_type == PT_TYPE_MAYBE)
10445  || (PT_IS_NUMERIC_TYPE (arg2_type) && arg1_type == PT_TYPE_MAYBE))
10446  {
10447  common_type = PT_TYPE_DOUBLE;
10448  }
10449  else
10450  {
10451  switch (arg1_type)
10452  {
10453  case PT_TYPE_DOUBLE:
10454  switch (arg2_type)
10455  {
10456  case PT_TYPE_SMALLINT:
10457  case PT_TYPE_INTEGER:
10458  case PT_TYPE_BIGINT:
10459  case PT_TYPE_FLOAT:
10460  case PT_TYPE_DOUBLE:
10461  case PT_TYPE_NUMERIC:
10462  case PT_TYPE_LOGICAL:
10463  case PT_TYPE_ENUMERATION:
10464  common_type = PT_TYPE_DOUBLE;
10465  break;
10466  case PT_TYPE_MONETARY:
10467  common_type = PT_TYPE_MONETARY;
10468  break;
10469  default:
10470  /* badly formed expression */
10471  common_type = PT_TYPE_NONE;
10472  break;
10473  }
10474  break;
10475 
10476  case PT_TYPE_NUMERIC:
10477  switch (arg2_type)
10478  {
10479  case PT_TYPE_NUMERIC:
10480  case PT_TYPE_SMALLINT:
10481  case PT_TYPE_INTEGER:
10482  case PT_TYPE_BIGINT:
10483  case PT_TYPE_LOGICAL:
10484  case PT_TYPE_ENUMERATION:
10485  common_type = PT_TYPE_NUMERIC;
10486  break;
10487  case PT_TYPE_FLOAT:
10488  case PT_TYPE_DOUBLE:
10489  common_type = PT_TYPE_DOUBLE;
10490  break;
10491  case PT_TYPE_MONETARY:
10492  common_type = PT_TYPE_MONETARY;
10493  break;
10494  default:
10495  common_type = PT_TYPE_NONE;
10496  break;
10497  }
10498  break;
10499 
10500  case PT_TYPE_FLOAT:
10501  switch (arg2_type)
10502  {
10503  case PT_TYPE_SMALLINT:
10504  case PT_TYPE_INTEGER:
10505  case PT_TYPE_BIGINT:
10506  case PT_TYPE_FLOAT:
10507  case PT_TYPE_LOGICAL:
10508  case PT_TYPE_ENUMERATION:
10509  common_type = PT_TYPE_FLOAT;
10510  break;
10511  case PT_TYPE_DOUBLE:
10512  case PT_TYPE_NUMERIC:
10513  common_type = PT_TYPE_DOUBLE;
10514  break;
10515  case PT_TYPE_MONETARY:
10516  common_type = PT_TYPE_MONETARY;
10517  break;
10518  default:
10519  common_type = PT_TYPE_NONE;
10520  break;
10521  }
10522  break;
10523 
10524  case PT_TYPE_INTEGER:
10525  switch (arg2_type)
10526  {
10527  case PT_TYPE_SMALLINT:
10528  case PT_TYPE_INTEGER:
10529  case PT_TYPE_LOGICAL:
10530  case PT_TYPE_ENUMERATION:
10531  common_type = PT_TYPE_INTEGER;
10532  break;
10533  case PT_TYPE_BIGINT:
10534  case PT_TYPE_FLOAT:
10535  case PT_TYPE_DOUBLE:
10536  case PT_TYPE_DATE:
10537  case PT_TYPE_MONETARY:
10538  case PT_TYPE_TIMESTAMP:
10539  case PT_TYPE_TIMESTAMPLTZ:
10540  case PT_TYPE_TIMESTAMPTZ:
10541  case PT_TYPE_DATETIME:
10542  case PT_TYPE_DATETIMELTZ:
10543  case PT_TYPE_DATETIMETZ:
10544  case PT_TYPE_TIME:
10545  case PT_TYPE_NUMERIC:
10546  common_type = arg2_type;
10547  break;
10548  default:
10549  common_type = PT_TYPE_NONE;
10550  break;
10551  }
10552  break;
10553 
10554  case PT_TYPE_SMALLINT:
10555  switch (arg2_type)
10556  {
10557  case PT_TYPE_SMALLINT:
10558  case PT_TYPE_LOGICAL:
10559  case PT_TYPE_ENUMERATION:
10560  common_type = PT_TYPE_SMALLINT;
10561  break;
10562  case PT_TYPE_INTEGER:
10563  case PT_TYPE_BIGINT:
10564  case PT_TYPE_FLOAT:
10565  case PT_TYPE_DOUBLE:
10566  case PT_TYPE_DATE:
10567  case PT_TYPE_MONETARY:
10568  case PT_TYPE_TIMESTAMP:
10569  case PT_TYPE_TIMESTAMPLTZ:
10570  case PT_TYPE_TIMESTAMPTZ:
10571  case PT_TYPE_DATETIME:
10572  case PT_TYPE_DATETIMELTZ:
10573  case PT_TYPE_DATETIMETZ:
10574  case PT_TYPE_TIME:
10575  case PT_TYPE_NUMERIC:
10576  common_type = arg2_type;
10577  break;
10578  default:
10579  common_type = PT_TYPE_NONE;
10580  break;
10581  }
10582  break;
10583 
10584  case PT_TYPE_BIGINT:
10585  switch (arg2_type)
10586  {
10587  case PT_TYPE_SMALLINT:
10588  case PT_TYPE_INTEGER:
10589  case PT_TYPE_BIGINT:
10590  case PT_TYPE_LOGICAL:
10591  case PT_TYPE_ENUMERATION:
10592  common_type = PT_TYPE_BIGINT;
10593  break;
10594  case PT_TYPE_FLOAT:
10595  common_type = PT_TYPE_FLOAT;
10596  break;
10597  case PT_TYPE_DOUBLE:
10598  common_type = PT_TYPE_DOUBLE;
10599  break;
10600  case PT_TYPE_DATE:
10601  case PT_TYPE_MONETARY:
10602  case PT_TYPE_TIMESTAMP:
10603  case PT_TYPE_TIMESTAMPLTZ:
10604  case PT_TYPE_TIMESTAMPTZ:
10605  case PT_TYPE_DATETIME:
10606  case PT_TYPE_DATETIMELTZ:
10607  case PT_TYPE_DATETIMETZ:
10608  case PT_TYPE_TIME:
10609  case PT_TYPE_NUMERIC:
10610  common_type = arg2_type;
10611  break;
10612  default:
10613  common_type = PT_TYPE_NONE;
10614  break;
10615  }
10616  break;
10617 
10618  case PT_TYPE_MONETARY:
10619  switch (arg2_type)
10620  {
10621  case PT_TYPE_MONETARY:
10622  case PT_TYPE_SMALLINT:
10623  case PT_TYPE_INTEGER:
10624  case PT_TYPE_BIGINT:
10625  case PT_TYPE_FLOAT:
10626  case PT_TYPE_DOUBLE:
10627  case PT_TYPE_NUMERIC:
10628  case PT_TYPE_LOGICAL:
10629  case PT_TYPE_ENUMERATION:
10630  common_type = PT_TYPE_MONETARY;
10631  break;
10632  default:
10633  common_type = PT_TYPE_NONE;
10634  break;
10635  }
10636  break;
10637 
10638  case PT_TYPE_DATETIME:
10639  switch (arg2_type)
10640  {
10641  case PT_TYPE_SMALLINT:
10642  case PT_TYPE_INTEGER:
10643  case PT_TYPE_BIGINT:
10644  case PT_TYPE_CHAR:
10645  case PT_TYPE_VARCHAR:
10646  case PT_TYPE_ENUMERATION:
10647  case PT_TYPE_NCHAR:
10648  case PT_TYPE_VARNCHAR:
10649  case PT_TYPE_DATETIME:
10650  case PT_TYPE_TIMESTAMP:
10651  case PT_TYPE_DATE:
10652  common_type = PT_TYPE_DATETIME;
10653  break;
10654  case PT_TYPE_TIMESTAMPLTZ:
10655  case PT_TYPE_DATETIMELTZ:
10656  common_type = PT_TYPE_DATETIMELTZ;
10657  break;
10658  case PT_TYPE_TIMESTAMPTZ:
10659  case PT_TYPE_DATETIMETZ:
10660  common_type = PT_TYPE_DATETIMETZ;
10661  break;
10662  default:
10663  common_type = PT_TYPE_NONE;
10664  break;
10665  }
10666  break;
10667 
10668  case PT_TYPE_DATETIMELTZ:
10669  switch (arg2_type)
10670  {
10671  case PT_TYPE_SMALLINT:
10672  case PT_TYPE_INTEGER:
10673  case PT_TYPE_BIGINT:
10674  case PT_TYPE_CHAR:
10675  case PT_TYPE_VARCHAR:
10676  case PT_TYPE_ENUMERATION:
10677  case PT_TYPE_NCHAR:
10678  case PT_TYPE_VARNCHAR:
10679  case PT_TYPE_DATETIME:
10680  case PT_TYPE_TIMESTAMP:
10681  case PT_TYPE_DATE:
10682  case PT_TYPE_TIMESTAMPLTZ:
10683  case PT_TYPE_DATETIMELTZ:
10684  common_type = PT_TYPE_DATETIMELTZ;
10685  break;
10686  case PT_TYPE_TIMESTAMPTZ:
10687  case PT_TYPE_DATETIMETZ:
10688  common_type = PT_TYPE_DATETIMETZ;
10689  break;
10690  default:
10691  common_type = PT_TYPE_NONE;
10692  break;
10693  }
10694  break;
10695 
10696  case PT_TYPE_DATETIMETZ:
10697  switch (arg2_type)
10698  {
10699  case PT_TYPE_SMALLINT:
10700  case PT_TYPE_INTEGER:
10701  case PT_TYPE_BIGINT:
10702  case PT_TYPE_CHAR:
10703  case PT_TYPE_VARCHAR:
10704  case PT_TYPE_ENUMERATION:
10705  case PT_TYPE_NCHAR:
10706  case PT_TYPE_VARNCHAR:
10707  case PT_TYPE_DATETIME:
10708  case PT_TYPE_DATETIMELTZ:
10709  case PT_TYPE_TIMESTAMP:
10710  case PT_TYPE_TIMESTAMPLTZ:
10711  case PT_TYPE_TIMESTAMPTZ:
10712  case PT_TYPE_DATE:
10713  common_type = PT_TYPE_DATETIMETZ;
10714  break;
10715 
10716  default:
10717  common_type = PT_TYPE_NONE;
10718  break;
10719  }
10720  break;
10721 
10722  case PT_TYPE_TIMESTAMP:
10723  switch (arg2_type)
10724  {
10725  case PT_TYPE_SMALLINT:
10726  case PT_TYPE_INTEGER:
10727  case PT_TYPE_BIGINT:
10729  {
10730  common_type = PT_TYPE_TIMESTAMP;
10731  }
10732  else
10733  {
10734  common_type = PT_TYPE_BIGINT;
10735  }
10736  break;
10737 
10738  case PT_TYPE_CHAR:
10739  case PT_TYPE_VARCHAR:
10740  case PT_TYPE_NCHAR:
10741  case PT_TYPE_VARNCHAR:
10742  case PT_TYPE_ENUMERATION:
10743  case PT_TYPE_TIMESTAMP:
10744  case PT_TYPE_DATE:
10745  common_type = PT_TYPE_TIMESTAMP;
10746  break;
10747  case PT_TYPE_DATETIME:
10748  case PT_TYPE_DATETIMELTZ:
10749  case PT_TYPE_DATETIMETZ:
10750  case PT_TYPE_TIMESTAMPLTZ:
10751  case PT_TYPE_TIMESTAMPTZ:
10752  common_type = arg2_type;
10753  break;
10754  default:
10755  common_type = PT_TYPE_NONE;
10756  break;
10757  }
10758  break;
10759 
10760  case PT_TYPE_TIMESTAMPLTZ:
10761  switch (arg2_type)
10762  {
10763  case PT_TYPE_SMALLINT:
10764  case PT_TYPE_INTEGER:
10765  case PT_TYPE_BIGINT:
10767  {
10768  common_type = PT_TYPE_TIMESTAMPLTZ;
10769  }
10770  else
10771  {
10772  common_type = PT_TYPE_BIGINT;
10773  }
10774  break;
10775 
10776  case PT_TYPE_CHAR:
10777  case PT_TYPE_VARCHAR:
10778  case PT_TYPE_NCHAR:
10779  case PT_TYPE_VARNCHAR:
10780  case PT_TYPE_ENUMERATION:
10781  case PT_TYPE_TIMESTAMP:
10782  case PT_TYPE_DATE:
10783  common_type = PT_TYPE_TIMESTAMPLTZ;
10784  break;
10785  case PT_TYPE_DATETIME:
10786  case PT_TYPE_DATETIMELTZ:
10787  common_type = PT_TYPE_DATETIMELTZ;
10788  break;
10789  case PT_TYPE_DATETIMETZ:
10790  case PT_TYPE_TIMESTAMPLTZ:
10791  case PT_TYPE_TIMESTAMPTZ:
10792  common_type = arg2_type;
10793  break;
10794  default:
10795  common_type = PT_TYPE_NONE;
10796  break;
10797  }
10798  break;
10799 
10800  case PT_TYPE_TIMESTAMPTZ:
10801  switch (arg2_type)
10802  {
10803  case PT_TYPE_SMALLINT:
10804  case PT_TYPE_INTEGER:
10805  case PT_TYPE_BIGINT:
10807  {
10808  common_type = PT_TYPE_TIMESTAMPTZ;
10809  }
10810  else
10811  {
10812  common_type = PT_TYPE_BIGINT;
10813  }
10814  break;
10815 
10816  case PT_TYPE_CHAR:
10817  case PT_TYPE_VARCHAR:
10818  case PT_TYPE_NCHAR:
10819  case PT_TYPE_VARNCHAR:
10820  case PT_TYPE_ENUMERATION:
10821  case PT_TYPE_TIMESTAMP:
10822  case PT_TYPE_TIMESTAMPLTZ:
10823  case PT_TYPE_TIMESTAMPTZ:
10824  case PT_TYPE_DATE:
10825  common_type = PT_TYPE_TIMESTAMPTZ;
10826  break;
10827  case PT_TYPE_DATETIME:
10828  case PT_TYPE_DATETIMELTZ:
10829  case PT_TYPE_DATETIMETZ:
10830  common_type = PT_TYPE_DATETIMETZ;
10831  break;
10832  default:
10833  common_type = PT_TYPE_NONE;
10834  break;
10835  }
10836  break;
10837 
10838  case PT_TYPE_TIME:
10839  switch (arg2_type)
10840  {
10841  case PT_TYPE_SMALLINT:
10842  case PT_TYPE_INTEGER:
10843  case PT_TYPE_BIGINT:
10845  {
10846  common_type = PT_TYPE_TIME;
10847  }
10848  else
10849  {
10850  common_type = arg2_type;
10851  }
10852  break;
10853  case PT_TYPE_CHAR:
10854  case PT_TYPE_VARCHAR:
10855  case PT_TYPE_NCHAR:
10856  case PT_TYPE_VARNCHAR:
10857  case PT_TYPE_ENUMERATION:
10858  case PT_TYPE_TIME:
10859  common_type = PT_TYPE_TIME;
10860  break;
10861  default:
10862  common_type = PT_TYPE_NONE;
10863  break;
10864  }
10865  break;
10866 
10867  case PT_TYPE_DATE:
10868  switch (arg2_type)
10869  {
10870  case PT_TYPE_SMALLINT:
10871  case PT_TYPE_INTEGER:
10872  case PT_TYPE_BIGINT:
10874  {
10875  common_type = PT_TYPE_DATE;
10876  }
10877  else
10878  {
10879  common_type = arg2_type;
10880  }
10881  break;
10882 
10883  case PT_TYPE_VARCHAR:
10884  case PT_TYPE_CHAR:
10885  case PT_TYPE_VARNCHAR:
10886  case PT_TYPE_NCHAR:
10887  case PT_TYPE_ENUMERATION:
10888  case PT_TYPE_DATE:
10889  common_type = PT_TYPE_DATE;
10890  break;
10891  case PT_TYPE_TIMESTAMP:
10892  case PT_TYPE_TIMESTAMPLTZ:
10893  case PT_TYPE_TIMESTAMPTZ:
10894  case PT_TYPE_DATETIME:
10895  case PT_TYPE_DATETIMELTZ:
10896  case PT_TYPE_DATETIMETZ:
10897  common_type = arg2_type;
10898  break;
10899 
10900  default:
10901  common_type = PT_TYPE_NONE;
10902  break;
10903  }
10904  break;
10905  case PT_TYPE_CHAR:
10906  switch (arg2_type)
10907  {
10908  case PT_TYPE_DATE:
10909  case PT_TYPE_TIME:
10910  case PT_TYPE_TIMESTAMP:
10911  case PT_TYPE_TIMESTAMPLTZ:
10912  case PT_TYPE_TIMESTAMPTZ:
10913  case PT_TYPE_DATETIME:
10914  case PT_TYPE_DATETIMELTZ:
10915  case PT_TYPE_DATETIMETZ:
10916  case PT_TYPE_VARCHAR:
10917  case PT_TYPE_CHAR:
10918  common_type = arg2_type;
10919  break;
10920  case PT_TYPE_ENUMERATION:
10921  common_type = PT_TYPE_VARCHAR;
10922  break;
10923  default:
10924  common_type = PT_TYPE_NONE;
10925  break;
10926  }
10927  break;
10928 
10929  case PT_TYPE_VARCHAR:
10930  switch (arg2_type)
10931  {
10932  case PT_TYPE_DATE:
10933  case PT_TYPE_TIME:
10934  case PT_TYPE_TIMESTAMP:
10935  case PT_TYPE_TIMESTAMPLTZ:
10936  case PT_TYPE_TIMESTAMPTZ:
10937  case PT_TYPE_DATETIME:
10938  case PT_TYPE_DATETIMELTZ:
10939  case PT_TYPE_DATETIMETZ:
10940  case PT_TYPE_VARCHAR:
10941  common_type = arg2_type;
10942  break;
10943  case PT_TYPE_CHAR:
10944  case PT_TYPE_ENUMERATION:
10945  common_type = PT_TYPE_VARCHAR;
10946  break;
10947  default:
10948  common_type = PT_TYPE_NONE;
10949  break;
10950  }
10951  break;
10952 
10953  case PT_TYPE_NCHAR:
10954  switch (arg2_type)
10955  {
10956  case PT_TYPE_DATE:
10957  case PT_TYPE_TIME:
10958  case PT_TYPE_TIMESTAMP:
10959  case PT_TYPE_TIMESTAMPLTZ:
10960  case PT_TYPE_TIMESTAMPTZ:
10961  case PT_TYPE_DATETIME:
10962  case PT_TYPE_DATETIMELTZ:
10963  case PT_TYPE_DATETIMETZ:
10964  case PT_TYPE_NCHAR:
10965  case PT_TYPE_VARNCHAR:
10966  common_type = arg2_type;
10967  break;
10968  default:
10969  common_type = PT_TYPE_NONE;
10970  break;
10971  }
10972  break;
10973 
10974  case PT_TYPE_VARNCHAR:
10975  switch (arg2_type)
10976  {
10977  case PT_TYPE_DATE:
10978  case PT_TYPE_TIME:
10979  case PT_TYPE_TIMESTAMP:
10980  case PT_TYPE_TIMESTAMPLTZ:
10981  case PT_TYPE_TIMESTAMPTZ:
10982  case PT_TYPE_DATETIME:
10983  case PT_TYPE_DATETIMELTZ:
10984  case PT_TYPE_DATETIMETZ:
10985  case PT_TYPE_VARNCHAR:
10986  common_type = arg2_type;
10987  break;
10988  case PT_TYPE_NCHAR:
10989  common_type = PT_TYPE_VARNCHAR;
10990  break;
10991  default:
10992  common_type = PT_TYPE_NONE;
10993  break;
10994  }
10995  break;
10996 
10997  case PT_TYPE_VARBIT:
10998  switch (arg2_type)
10999  {
11000  case PT_TYPE_VARBIT:
11001  case PT_TYPE_BIT:
11002  common_type = PT_TYPE_VARBIT;
11003  break;
11004  default:
11005  common_type = PT_TYPE_NONE;
11006  break;
11007  }
11008  break;
11009 
11010  case PT_TYPE_BIT:
11011  switch (arg2_type)
11012  {
11013  case PT_TYPE_BIT:
11014  common_type = PT_TYPE_BIT;
11015  break;
11016  case PT_TYPE_VARBIT:
11017  common_type = PT_TYPE_VARBIT;
11018  break;
11019  default:
11020  common_type = PT_TYPE_NONE;
11021  break;
11022  }
11023  break;
11024 
11025  case PT_TYPE_OBJECT:
11026  switch (arg2_type)
11027  {
11028  case PT_TYPE_OBJECT:
11029  common_type = PT_TYPE_OBJECT;
11030  break;
11031  default:
11032  common_type = PT_TYPE_NONE;
11033  break;
11034  }
11035  break;
11036 
11037  case PT_TYPE_SET:
11038  case PT_TYPE_MULTISET:
11039  case PT_TYPE_SEQUENCE:
11040  switch (arg2_type)
11041  {
11042  case PT_TYPE_SET:
11043  case PT_TYPE_MULTISET:
11044  case PT_TYPE_SEQUENCE:
11045  common_type = PT_TYPE_MULTISET;
11046  break;
11047  default:
11048  common_type = PT_TYPE_NONE;
11049  break;
11050  }
11051  break;
11052 
11053  case PT_TYPE_LOGICAL:
11054  switch (arg2_type)
11055  {
11056  case PT_TYPE_LOGICAL:
11057  common_type = PT_TYPE_LOGICAL;
11058  break;
11059  case PT_TYPE_DOUBLE:
11060  common_type = PT_TYPE_DOUBLE;
11061  break;
11062  case PT_TYPE_NUMERIC:
11063  common_type = PT_TYPE_NUMERIC;
11064  break;
11065  case PT_TYPE_FLOAT:
11066  common_type = PT_TYPE_FLOAT;
11067  break;
11068  case PT_TYPE_INTEGER:
11069  common_type = PT_TYPE_INTEGER;
11070  break;
11071  case PT_TYPE_SMALLINT:
11072  common_type = PT_TYPE_SMALLINT;
11073  break;
11074  case PT_TYPE_BIGINT:
11075  common_type = PT_TYPE_BIGINT;
11076  break;
11077  case PT_TYPE_MONETARY:
11078  common_type = PT_TYPE_MONETARY;
11079  break;
11080  default:
11081  common_type = PT_TYPE_NONE;
11082  break;
11083  }
11084  break;
11085 
11086  case PT_TYPE_ENUMERATION:
11087  switch (arg2_type)
11088  {
11089  case PT_TYPE_SMALLINT:
11090  case PT_TYPE_INTEGER:
11091  case PT_TYPE_FLOAT:
11092  case PT_TYPE_BIGINT:
11093  case PT_TYPE_NUMERIC:
11094  case PT_TYPE_DOUBLE:
11095  case PT_TYPE_MONETARY:
11096  case PT_TYPE_VARCHAR:
11097  case PT_TYPE_ENUMERATION:
11098  common_type = arg2_type;
11099  break;
11100  case PT_TYPE_CHAR:
11101  common_type = PT_TYPE_VARCHAR;
11102  break;
11103  default:
11104  common_type = pt_common_type (PT_TYPE_VARCHAR, arg2_type);
11105  break;
11106  }
11107  break;
11108 
11109  default:
11110  common_type = PT_TYPE_NONE;
11111  break;
11112  }
11113  }
11114 
11115  if (common_type == PT_TYPE_NONE)
11116  {
11117  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
11118  {
11119  common_type = PT_TYPE_MAYBE;
11120  }
11121  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
11122  {
11123  common_type = arg2_type;
11124  }
11125  else if (arg2_type == PT_TYPE_NA || arg2_type == PT_TYPE_NULL)
11126  {
11127  common_type = arg1_type;
11128  }
11129  }
11130 
11131  return common_type;
11132 }
11133 
11134 /*
11135  * pt_common_type_op () - return the result type of t1 op t2
11136  * return: returns the result type of t1 op t2
11137  * or PT_TYPE_NONE if no such type exists
11138  * t1(in): a data type
11139  * op(in): a binary operator
11140  * t2(in): a data type
11141  */
11142 
11143 static PT_TYPE_ENUM
11145 {
11146  PT_TYPE_ENUM result_type;
11147 
11148  if (pt_is_op_hv_late_bind (op) && (t1 == PT_TYPE_MAYBE || t2 == PT_TYPE_MAYBE))
11149  {
11150  result_type = PT_TYPE_MAYBE;
11151  }
11152  else
11153  {
11154  result_type = pt_common_type (t1, t2);
11155  }
11156 
11157  switch (op)
11158  {
11159  case PT_MINUS:
11160  case PT_TIMES:
11161  if (result_type == PT_TYPE_SEQUENCE)
11162  {
11163  result_type = PT_TYPE_MULTISET;
11164  }
11165  else if ((PT_IS_STRING_TYPE (t1) && PT_IS_NUMERIC_TYPE (t2))
11166  || (PT_IS_NUMERIC_TYPE (t1) && PT_IS_STRING_TYPE (t2)))
11167  {
11168  /* + and - have their own way of handling this situation */
11169  return PT_TYPE_NONE;
11170  }
11171  break;
11172  case PT_SUPERSET:
11173  case PT_SUPERSETEQ:
11174  case PT_SUBSET:
11175  case PT_SUBSETEQ:
11176  if (result_type == PT_TYPE_SEQUENCE)
11177  {
11178  result_type = PT_TYPE_NONE;
11179  }
11180  break;
11181  case PT_IFNULL:
11182  if (result_type == PT_TYPE_MAYBE)
11183  {
11184  result_type = PT_TYPE_VARCHAR;
11185  }
11186  break;
11187  case PT_COALESCE:
11188  if (t1 == PT_TYPE_MAYBE)
11189  {
11190  if (t2 == PT_TYPE_MAYBE || t2 == PT_TYPE_NULL)
11191  {
11192  result_type = PT_TYPE_MAYBE;
11193  }
11194  else
11195  {
11196  result_type = t2;
11197  }
11198  }
11199  else if (t2 == PT_TYPE_MAYBE)
11200  {
11201  if (t1 == PT_TYPE_MAYBE || t1 == PT_TYPE_NULL)
11202  {
11203  result_type = PT_TYPE_MAYBE;
11204  }
11205  else
11206  {
11207  result_type = t1;
11208  }
11209  }
11210  else if (PT_IS_DATE_TIME_TYPE (result_type) && (t1 != t2))
11211  {
11212  if (PT_IS_DATE_TIME_TYPE (t1))
11213  {
11214  result_type = (PT_IS_DATE_TIME_TYPE (t2)) ? PT_TYPE_DATETIME : t1;
11215  }
11216  else if (PT_IS_DATE_TIME_TYPE (t2))
11217  {
11218  result_type = (PT_IS_DATE_TIME_TYPE (t1)) ? PT_TYPE_DATETIME : t2;
11219  }
11220  }
11221  break;
11222  case PT_IS_IN:
11223  case PT_IS_NOT_IN:
11224  if (result_type == PT_TYPE_MAYBE)
11225  {
11226  if (t1 != PT_TYPE_MAYBE)
11227  {
11228  result_type = t1;
11229  }
11230  else if (t2 != PT_TYPE_MAYBE)
11231  {
11232  result_type = t2;
11233  }
11234  }
11235  break;
11236  default:
11237  break;
11238  }
11239  /*
11240  * true + true must not be logical.
11241  * Same goes for true*true, (i or j)+(i and j) etc.
11242  * Basic rule: if both operands are logical but the operation is not logical,
11243  * then the resulting type MUST be integer. Otherwise strange things happen
11244  * while generating xasl: predicate expressions with wrong operators.
11245  */
11246  if (t1 == PT_TYPE_LOGICAL && t2 == PT_TYPE_LOGICAL && !pt_is_operator_logical (op))
11247  {
11248  result_type = PT_TYPE_INTEGER;
11249  }
11250 
11251  if (pt_is_comp_op (op) && ((PT_IS_NUMERIC_TYPE (t1) && t2 == PT_TYPE_JSON)
11252  || (t1 == PT_TYPE_JSON && PT_IS_NUMERIC_TYPE (t2))))
11253  {
11254  result_type = PT_TYPE_JSON;
11255  }
11256 
11257  return result_type;
11258 }
11259 
11260 
11261 /*
11262  * pt_upd_domain_info () - preparing a node for a binary operation
11263  * involving a parameterized type
11264  * return: NO_ERROR on success, non-zero for ERROR
11265  * parser(in):
11266  * arg1(in):
11267  * arg2(in):
11268  * op(in):
11269  * common_type(in):
11270  * node(out):
11271  */
11272 
11273 static int
11275  PT_NODE * node)
11276 {
11277  int arg1_prec = 0;
11278  int arg1_dec_prec = 0;
11279  int arg1_units = 0;
11280  int arg2_prec = 0;
11281  int arg2_dec_prec = 0;
11282  int arg2_units = 0;
11283  PT_NODE *dt = NULL;
11284  bool do_detect_collation = true;
11286 
11287  if (node->data_type)
11288  { /* node has already been resolved */
11289  return NO_ERROR;
11290  }
11291 
11292  /* Retrieve the domain information for arg1 & arg2 */
11293  if (arg1 && arg1->data_type)
11294  {
11295  arg1_prec = arg1->data_type->info.data_type.precision;
11296  arg1_dec_prec = arg1->data_type->info.data_type.dec_precision;
11297  arg1_units = arg1->data_type->info.data_type.units;
11299  {
11300  collation_flag = TP_DOMAIN_COLL_NORMAL;
11301  }
11302  }
11303  else if (arg1 && arg1->type_enum == PT_TYPE_INTEGER)
11304  {
11305  arg1_prec = TP_INTEGER_PRECISION;
11306  arg1_dec_prec = 0;
11307  arg1_units = 0;
11308  }
11309  else if (arg1 && arg1->type_enum == PT_TYPE_BIGINT)
11310  {
11311  arg1_prec = TP_BIGINT_PRECISION;
11312  arg1_dec_prec = 0;
11313  arg1_units = 0;
11314  }
11315  else if (arg1 && arg1->type_enum == PT_TYPE_SMALLINT)
11316  {
11317  arg1_prec = TP_SMALLINT_PRECISION;
11318  arg1_dec_prec = 0;
11319  arg1_units = 0;
11320  }
11321  else if (arg1 && arg1->type_enum == PT_TYPE_NUMERIC)
11322  {
11323  arg1_prec = DB_DEFAULT_NUMERIC_PRECISION;
11324  arg1_dec_prec = DB_DEFAULT_NUMERIC_SCALE;
11325  arg1_units = 0;
11326  }
11327  else if (arg1 && arg1->type_enum == PT_TYPE_MAYBE)
11328  {
11329  arg1_prec = TP_FLOATING_PRECISION_VALUE;
11330  arg1_dec_prec = 0;
11331  arg1_units = 0;
11332  }
11333  else
11334  {
11335  arg1_prec = 0;
11336  arg1_dec_prec = 0;
11337  arg1_units = 0;
11338  }
11339 
11340  if (arg2 && arg2->data_type)
11341  {
11342  arg2_prec = arg2->data_type->info.data_type.precision;
11343  arg2_dec_prec = arg2->data_type->info.data_type.dec_precision;
11344  arg2_units = arg2->data_type->info.data_type.units;
11346  {
11347  collation_flag = TP_DOMAIN_COLL_NORMAL;
11348  }
11349  }
11350  else if (arg2 && arg2->type_enum == PT_TYPE_INTEGER)
11351  {
11352  arg2_prec = TP_INTEGER_PRECISION;
11353  arg2_dec_prec = 0;
11354  arg2_units = 0;
11355  }
11356  else if (arg2 && arg2->type_enum == PT_TYPE_BIGINT)
11357  {
11358  arg2_prec = TP_BIGINT_PRECISION;
11359  arg2_dec_prec = 0;
11360  arg2_units = 0;
11361  }
11362  else if (arg2 && arg2->type_enum == PT_TYPE_SMALLINT)
11363  {
11364  arg2_prec = TP_SMALLINT_PRECISION;
11365  arg2_dec_prec = 0;
11366  arg2_units = 0;
11367  }
11368  else if (arg2 && arg2->type_enum == PT_TYPE_NUMERIC)
11369  {
11370  arg2_prec = DB_DEFAULT_NUMERIC_PRECISION;
11371  arg2_dec_prec = DB_DEFAULT_NUMERIC_SCALE;
11372  arg2_units = 0;
11373  }
11374  else if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
11375  {
11376  arg2_prec = TP_FLOATING_PRECISION_VALUE;
11377  arg2_dec_prec = 0;
11378  arg2_units = 0;
11379  }
11380  else
11381  {
11382  arg2_prec = 0;
11383  arg2_dec_prec = 0;
11384  arg2_units = 0;
11385  }
11386 
11387  if (op == PT_MINUS || op == PT_PLUS || op == PT_STRCAT || op == PT_SYS_CONNECT_BY_PATH || op == PT_PRIOR
11388  || op == PT_CONNECT_BY_ROOT || op == PT_QPRIOR || op == PT_UNARY_MINUS || op == PT_FLOOR || op == PT_CEIL
11389  || op == PT_ABS || op == PT_ROUND || op == PT_TRUNC || op == PT_CASE || op == PT_NULLIF || op == PT_COALESCE
11390  || op == PT_NVL || op == PT_NVL2 || op == PT_DECODE || op == PT_LEAST || op == PT_GREATEST || op == PT_CHR
11391  || op == PT_BIT_NOT || op == PT_BIT_AND || op == PT_BIT_OR || op == PT_BIT_XOR || op == PT_BITSHIFT_LEFT
11392  || op == PT_BITSHIFT_RIGHT || op == PT_DIV || op == PT_MOD || op == PT_IF || op == PT_IFNULL || op == PT_CONCAT
11393  || op == PT_CONCAT_WS || op == PT_FIELD || op == PT_UNIX_TIMESTAMP || op == PT_BIT_COUNT || op == PT_REPEAT
11394  || op == PT_SPACE || op == PT_MD5 || op == PT_TIMEF || op == PT_AES_ENCRYPT || op == PT_AES_DECRYPT
11395  || op == PT_SHA_TWO || op == PT_SHA_ONE || op == PT_TO_BASE64 || op == PT_FROM_BASE64 || op == PT_DEFAULTF)
11396  {
11397  dt = parser_new_node (parser, PT_DATA_TYPE);
11398  if (dt == NULL)
11399  {
11400  return ER_OUT_OF_VIRTUAL_MEMORY;
11401  }
11402  }
11403  else if (common_type == PT_TYPE_NUMERIC && (op == PT_TIMES || op == PT_POWER || op == PT_DIVIDE || op == PT_MODULUS))
11404  {
11405  dt = parser_new_node (parser, PT_DATA_TYPE);
11406  if (dt == NULL)
11407  {
11408  return ER_OUT_OF_VIRTUAL_MEMORY;
11409  }
11410  }
11411 
11412  switch (op)
11413  {
11414  case PT_MINUS:
11415  case PT_PLUS:
11416  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11417  {
11419  dt->info.data_type.dec_precision = 0;
11420  dt->info.data_type.units = 0;
11421  }
11422  else if (common_type == PT_TYPE_NUMERIC)
11423  {
11424  int integral_digits1, integral_digits2;
11425 
11426  integral_digits1 = arg1_prec - arg1_dec_prec;
11427  integral_digits2 = arg2_prec - arg2_dec_prec;
11428  dt->info.data_type.dec_precision = MAX (arg1_dec_prec, arg2_dec_prec);
11429  dt->info.data_type.precision =
11430  (dt->info.data_type.dec_precision + MAX (integral_digits1, integral_digits2) + 1);
11431  dt->info.data_type.units = 0;
11432  }
11433  else
11434  {
11435  dt->info.data_type.precision = arg1_prec + arg2_prec;
11436  dt->info.data_type.dec_precision = 0;
11437  dt->info.data_type.units = arg1_units;
11438  }
11439  break;
11440 
11441  case PT_REPEAT:
11442  {
11443  int precision = TP_FLOATING_PRECISION_VALUE;
11444  if (arg1_prec != TP_FLOATING_PRECISION_VALUE && arg2->node_type == PT_VALUE
11445  && arg2->type_enum == PT_TYPE_INTEGER)
11446  {
11447  precision = arg1_prec * arg2->info.value.data_value.i;
11448  }
11449  dt->info.data_type.precision = precision;
11450  dt->info.data_type.dec_precision = 0;
11451  dt->info.data_type.units = arg1_units;
11452  }
11453  break;
11454 
11455  case PT_SPACE:
11456  if (arg1->node_type == PT_VALUE)
11457  {
11458  switch (arg1->type_enum)
11459  {
11460  case PT_TYPE_SMALLINT:
11461  if (arg1->info.value.data_value.b < 0)
11462  {
11463  dt->info.data_type.precision = 0;
11464  }
11465  else
11466  {
11468  }
11469  break;
11470  case PT_TYPE_INTEGER:
11472  {
11473  node->type_enum = PT_TYPE_NULL;
11474  }
11475  else
11476  {
11477  if (arg1->info.value.data_value.i < 0)
11478  {
11479  dt->info.data_type.precision = 0;
11480  }
11481  else
11482  {
11484  }
11485  }
11486  break;
11487  case PT_TYPE_BIGINT:
11489  {
11490  node->type_enum = PT_TYPE_NULL;
11491  }
11492  else
11493  {
11494  if (arg1->info.value.data_value.bigint < 0)
11495  {
11496  dt->info.data_type.precision = 0;
11497  }
11498  else
11499  {
11500  dt->info.data_type.precision = (int) arg1->info.value.data_value.bigint;
11501  }
11502  }
11503  break;
11504  default:
11506  break;
11507  }
11508  }
11509  else
11510  {
11512  }
11513  break;
11514 
11515  case PT_CONCAT:
11516  case PT_CONCAT_WS:
11517  case PT_FIELD:
11518  case PT_STRCAT:
11521  dt->info.data_type.dec_precision = 0;
11522  dt->info.data_type.units = 0;
11523  break;
11524 
11525  case PT_TIMES:
11526  case PT_POWER:
11527  if (common_type == PT_TYPE_NUMERIC)
11528  {
11529  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11530  {
11532  dt->info.data_type.dec_precision = 0;
11533  dt->info.data_type.units = 0;
11534  }
11535  else
11536  {
11537  dt->info.data_type.precision = arg1_prec + arg2_prec + 1;
11538  dt->info.data_type.dec_precision = (arg1_dec_prec + arg2_dec_prec);
11539  dt->info.data_type.units = 0;
11540  }
11541  }
11542  break;
11543 
11544  case PT_DIVIDE:
11545  case PT_MODULUS:
11546  if (common_type == PT_TYPE_NUMERIC)
11547  {
11548  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11549  {
11551  dt->info.data_type.dec_precision = 0;
11552  dt->info.data_type.units = 0;
11553  }
11554  else
11555  {
11556  int scaleup = 0;
11557 
11558  if (arg2_dec_prec > 0)
11559  {
11560  scaleup = (MAX (arg1_dec_prec, arg2_dec_prec) + arg2_dec_prec - arg1_dec_prec);
11561  }
11562  dt->info.data_type.precision = arg1_prec + scaleup;
11563  dt->info.data_type.dec_precision = ((arg1_dec_prec > arg2_dec_prec) ? arg1_dec_prec : arg2_dec_prec);
11564  dt->info.data_type.units = 0;
11566  {
11568  {
11569  int org_prec, org_scale, new_prec, new_scale;
11570  int scale_delta;
11571 
11572  org_prec = MIN (38, dt->info.data_type.precision);
11573  org_scale = dt->info.data_type.dec_precision;
11574  scale_delta = (DB_DEFAULT_NUMERIC_DIVISION_SCALE - org_scale);
11575  new_scale = org_scale + scale_delta;
11576  new_prec = org_prec + scale_delta;
11577  if (new_prec > DB_MAX_NUMERIC_PRECISION)
11578  {
11579  new_scale -= (new_prec - DB_MAX_NUMERIC_PRECISION);
11580  new_prec = DB_MAX_NUMERIC_PRECISION;
11581  }
11582 
11583  dt->info.data_type.precision = new_prec;
11584  dt->info.data_type.dec_precision = new_scale;
11585  }
11586  }
11587  }
11588  }
11589  break;
11590  case PT_TIMEF:
11591  dt->info.data_type.precision = 12;
11592  dt->info.data_type.dec_precision = 0;
11593  dt->info.data_type.units = 0;
11594  break;
11595 
11596  case PT_UNARY_MINUS:
11597  case PT_FLOOR:
11598  case PT_CEIL:
11599  case PT_ABS:
11600  case PT_ROUND:
11601  case PT_TRUNC:
11602  case PT_PRIOR:
11603  case PT_CONNECT_BY_ROOT:
11604  case PT_QPRIOR:
11605  case PT_BIT_NOT:
11606  case PT_BIT_AND:
11607  case PT_BIT_OR:
11608  case PT_BIT_XOR:
11609  case PT_BIT_COUNT:
11610  case PT_BITSHIFT_LEFT:
11611  case PT_BITSHIFT_RIGHT:
11612  case PT_DIV:
11613  case PT_MOD:
11614  dt->info.data_type.precision = arg1_prec;
11615  dt->info.data_type.dec_precision = arg1_dec_prec;
11616  dt->info.data_type.units = arg1_units;
11617  break;
11618 
11619  case PT_IF:
11620  case PT_IFNULL:
11621  case PT_CASE:
11622  case PT_NULLIF:
11623  case PT_COALESCE:
11624  case PT_NVL:
11625  case PT_NVL2:
11626  case PT_DECODE:
11627  case PT_LEAST:
11628  case PT_GREATEST:
11629  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11630  {
11632  dt->info.data_type.dec_precision = 0;
11633  dt->info.data_type.units = 0;
11634  }
11635  else if (common_type == PT_TYPE_NUMERIC)
11636  {
11637  int integral_digits1, integral_digits2;
11638 
11639  integral_digits1 = arg1_prec - arg1_dec_prec;
11640  integral_digits2 = arg2_prec - arg2_dec_prec;
11641  dt->info.data_type.dec_precision = MAX (arg1_dec_prec, arg2_dec_prec);
11642  dt->info.data_type.precision = (MAX (integral_digits1, integral_digits2) + dt->info.data_type.dec_precision);
11643  dt->info.data_type.units = 0;
11644  }
11645  else if ((arg1->type_enum != arg2->type_enum) && pt_is_op_with_forced_common_type (op))
11646  {
11648  dt->info.data_type.dec_precision = 0;
11649  dt->info.data_type.units = 0;
11650  }
11651  else
11652  {
11653  dt->info.data_type.precision = MAX (arg1_prec, arg2_prec);
11654  dt->info.data_type.dec_precision = 0;
11655  if (arg1 && arg1->type_enum == common_type)
11656  {
11657  dt->info.data_type.units = arg1_units;
11658  }
11659  else
11660  {
11661  dt->info.data_type.units = arg2_units;
11662  }
11663  }
11664  break;
11665 
11666  case PT_BIT_TO_BLOB:
11667  case PT_CHAR_TO_BLOB:
11668  case PT_BLOB_FROM_FILE:
11669  assert (dt == NULL);
11670  dt = pt_make_prim_data_type (parser, PT_TYPE_BLOB);
11671  if (dt == NULL)
11672  {
11673  return ER_OUT_OF_VIRTUAL_MEMORY;
11674  }
11675  break;
11676 
11677  case PT_BLOB_LENGTH:
11678  case PT_CLOB_LENGTH:
11679  assert (dt == NULL);
11680  dt = pt_make_prim_data_type (parser, PT_TYPE_BIGINT);
11681  if (dt == NULL)
11682  {
11683  return ER_OUT_OF_VIRTUAL_MEMORY;
11684  }
11685  break;
11686 
11687  case PT_BLOB_TO_BIT:
11688  assert (dt == NULL);
11689  dt = pt_make_prim_data_type (parser, PT_TYPE_VARBIT);
11690  if (dt == NULL)
11691  {
11692  return ER_OUT_OF_VIRTUAL_MEMORY;
11693  }
11694  break;
11695  case PT_CLOB_TO_CHAR:
11696  assert (dt == NULL);
11697  dt = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
11698  if (dt == NULL)
11699  {
11700  return ER_OUT_OF_VIRTUAL_MEMORY;
11701  }
11702  if (arg2 != NULL)
11703  {
11704  assert (arg2->node_type == PT_VALUE);
11705  assert (arg2->type_enum == PT_TYPE_INTEGER);
11706  dt->info.data_type.units = arg2->info.value.data_value.i;
11708  do_detect_collation = false;
11709  }
11710  break;
11711 
11712 
11713  case PT_CHAR_TO_CLOB:
11714  case PT_CLOB_FROM_FILE:
11715  assert (dt == NULL);
11716  dt = pt_make_prim_data_type (parser, PT_TYPE_CLOB);
11717  if (dt == NULL)
11718  {
11719  return ER_OUT_OF_VIRTUAL_MEMORY;
11720  }
11721  break;
11722  case PT_LIST_DBS:
11723  case PT_TO_CHAR:
11724  case PT_USER:
11725  case PT_DATABASE:
11726  case PT_SCHEMA:
11727  case PT_CURRENT_USER:
11728  case PT_INET_NTOA:
11729  case PT_SCHEMA_DEF:
11730  assert (dt == NULL);
11731  dt = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
11732  if (op != PT_TO_CHAR)
11733  {
11734  do_detect_collation = false;
11735  }
11736  break;
11737  case PT_SUBSTRING_INDEX:
11738  assert (dt == NULL);
11739  dt = parser_copy_tree_list (parser, arg1->data_type);
11740  break;
11741  case PT_VERSION:
11742  assert (dt == NULL);
11743  dt = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
11744  /* Set a large enough precision so that CUBRID is able to handle versions like 'xxx.xxx.xxx.xxxx'. */
11745  dt->info.data_type.precision = 16;
11746  break;
11747  case PT_SYS_GUID:
11748  assert (dt == NULL);
11749  dt = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
11750  /* Set a large enough precision so that CUBRID is able to handle GUID like 'B5B2D2FA9633460F820589FFDBD8C309'. */
11751  dt->info.data_type.precision = 32;
11752  break;
11753  case PT_CHR:
11754  assert (dt != NULL);
11756  if (arg2 != NULL)
11757  {
11758  assert (arg2->node_type == PT_VALUE);
11759  assert (arg2->type_enum == PT_TYPE_INTEGER);
11760  dt->info.data_type.units = arg2->info.value.data_value.i;
11762  do_detect_collation = false;
11763  }
11764  break;
11765 
11766  case PT_MD5:
11767  assert (dt != NULL);
11768  dt->info.data_type.precision = 32;
11769  break;
11770 
11771  case PT_AES_ENCRYPT:
11772  case PT_AES_DECRYPT:
11773  case PT_SHA_TWO:
11774  assert (dt != NULL);
11776  dt->info.data_type.dec_precision = 0;
11777  dt->info.data_type.units = 0;
11778  break;
11779 
11780  case PT_SHA_ONE:
11781  assert (dt != NULL);
11782  dt->info.data_type.precision = 40;
11783  break;
11784 
11785  case PT_TO_BASE64:
11786  case PT_FROM_BASE64:
11787  assert (dt != NULL);
11789  break;
11790 
11791  case PT_LAST_INSERT_ID:
11792  assert (dt == NULL);
11793  /* last insert id returns NUMERIC (38, 0) */
11794  dt = pt_make_prim_data_type (parser, PT_TYPE_NUMERIC);
11796  dt->info.data_type.dec_precision = 0;
11797  break;
11798 
11799  case PT_BIN:
11800  assert (dt == NULL);
11801  dt = pt_make_prim_data_type (parser, node->type_enum);
11802  if (dt == NULL)
11803  {
11804  break;
11805  }
11806  /* bin returns the binary representation of a BIGINT */
11807  dt->info.data_type.precision = (sizeof (DB_BIGINT) * 8);
11808  do_detect_collation = false;
11809  break;
11810 
11811  case PT_ADDTIME:
11812  if (node->type_enum == PT_TYPE_VARCHAR)
11813  {
11814  assert (dt == NULL);
11815  dt = pt_make_prim_data_type (parser, node->type_enum);
11816  if (dt == NULL)
11817  {
11818  break;
11819  }
11820  /* holds at most a DATETIME type */
11822  }
11823  break;
11824 
11825  case PT_CONV:
11826  if (PT_IS_STRING_TYPE (node->type_enum))
11827  {
11828  assert (dt == NULL);
11829  dt = pt_make_prim_data_type (parser, node->type_enum);
11831  }
11832  do_detect_collation = false;
11833  break;
11834 
11835  case PT_CHARSET:
11836  case PT_COLLATION:
11837  case PT_FORMAT:
11838  case PT_TYPEOF:
11839  case PT_HEX:
11840  do_detect_collation = false;
11841  /* FALLTHRU */
11842  case PT_TRIM:
11843  case PT_LTRIM:
11844  case PT_RTRIM:
11845  case PT_MID:
11846  case PT_REPLACE:
11847  case PT_TRANSLATE:
11848  case PT_TIME_FORMAT:
11849  case PT_LPAD:
11850  case PT_RPAD:
11851  case PT_SUBSTRING:
11852  case PT_COERCIBILITY:
11853  case PT_INDEX_PREFIX:
11854  assert (dt == NULL);
11855  dt = pt_make_prim_data_type (parser, node->type_enum);
11857  break;
11858 
11859  case PT_FUNCTION_HOLDER:
11872  {
11873  assert (dt == NULL);
11874  dt = pt_make_prim_data_type (parser, node->type_enum);
11876  }
11877  break;
11878 
11879  case PT_SUBDATE:
11880  case PT_ADDDATE:
11881  case PT_DATE_SUB:
11882  case PT_DATE_ADD:
11883  case PT_DATEF:
11884  if (PT_IS_STRING_TYPE (node->type_enum))
11885  {
11886  assert (dt == NULL);
11887  dt = pt_make_prim_data_type (parser, node->type_enum);
11888  dt->info.data_type.precision = 32;
11889  }
11890  break;
11891 
11892  case PT_FROM_UNIXTIME:
11893  case PT_DATE_FORMAT:
11894  if (PT_IS_STRING_TYPE (node->type_enum))
11895  {
11896  assert (dt == NULL);
11897  dt = pt_make_prim_data_type (parser, node->type_enum);
11899  }
11900  break;
11901 
11902  case PT_LOWER:
11903  case PT_UPPER:
11904  case PT_REVERSE:
11905  case PT_LEFT:
11906  case PT_RIGHT:
11907  assert (dt == NULL);
11908  if (PT_IS_HOSTVAR (arg1))
11909  {
11910  /* we resolved the node type to a variable char type (VARCHAR orVARNCHAR) but we have to set an unknown
11911  * precision here */
11912  dt = pt_make_prim_data_type (parser, node->type_enum);
11914  }
11915  else
11916  {
11917  dt = parser_copy_tree_list (parser, arg1->data_type);
11918  }
11919  break;
11920 
11921  case PT_TO_NUMBER:
11922  {
11923  int prec = 0, scale = 0;
11924  pt_to_regu_resolve_domain (&prec, &scale, arg2);
11925  dt = pt_make_prim_data_type_fortonum (parser, prec, scale);
11926  break;
11927  }
11928 
11929  case PT_EXEC_STATS:
11930  assert (dt == NULL);
11931  dt = pt_make_prim_data_type (parser, PT_TYPE_BIGINT);
11932  break;
11933 
11934  case PT_TZ_OFFSET:
11935  assert (dt == NULL);
11936  dt = pt_make_prim_data_type (parser, node->type_enum);
11938  dt->info.data_type.units = (int) LANG_SYS_CODESET;
11940  break;
11941 
11942  case PT_TRACE_STATS:
11943  assert (dt == NULL);
11944  dt = pt_make_prim_data_type (parser, PT_TYPE_VARCHAR);
11945  do_detect_collation = false;
11946  break;
11947 
11948  case PT_CRC32:
11949  case PT_DISK_SIZE:
11950  assert (dt == NULL);
11951  dt = pt_make_prim_data_type (parser, PT_TYPE_INTEGER);
11952  break;
11953 
11954  case PT_DEFAULTF:
11955  dt = parser_copy_tree_list (parser, arg1->data_type);
11956  break;
11957 
11958  default:
11959  break;
11960  }
11961 
11962  if (dt)
11963  {
11964  /* in any case the precision can't be greater than the max precision for the result. */
11965  switch (common_type)
11966  {
11967  case PT_TYPE_CHAR:
11970  break;
11971 
11972  case PT_TYPE_VARCHAR:
11975  break;
11976 
11977  case PT_TYPE_NCHAR:
11980  break;
11981 
11982  case PT_TYPE_VARNCHAR:
11985  break;
11986 
11987  case PT_TYPE_BIT:
11990  break;
11991 
11992  case PT_TYPE_VARBIT:
11995  break;
11996 
11997  case PT_TYPE_NUMERIC:
11999  {
12002  }
12003 
12006  break;
12007 
12008  case PT_TYPE_ENUMERATION:
12009  if (arg1 != NULL && arg1->type_enum == PT_TYPE_ENUMERATION)
12010  {
12011  dt = parser_copy_tree_list (parser, arg1->data_type);
12012  }
12013  else if (arg2 != NULL && arg2->type_enum == PT_TYPE_ENUMERATION)
12014  {
12015  dt = parser_copy_tree_list (parser, arg2->data_type);
12016  }
12017  break;
12018 
12019  default:
12020  break;
12021  }
12022 
12023  /* Basic collation inference, add specific code in 'pt_check_expr_collation' */
12024  if (do_detect_collation && PT_HAS_COLLATION (common_type))
12025  {
12026  if (arg1 != NULL && PT_HAS_COLLATION (arg1->type_enum) && arg1->data_type != NULL
12027  && (arg2 == NULL || (arg2 != NULL && !PT_HAS_COLLATION (arg2->type_enum))))
12028  {
12031  }
12032  else if (arg2 != NULL && PT_HAS_COLLATION (arg2->type_enum) && arg2->data_type != NULL
12033  && (arg1 == NULL || (arg1 != NULL && !PT_HAS_COLLATION (arg1->type_enum))))
12034  {
12037  }
12038  else
12039  {
12040  dt->info.data_type.units = (int) LANG_SYS_CODESET;
12042  if ((arg1 == NULL || arg1->type_enum != PT_TYPE_MAYBE)
12043  && (arg2 == NULL || arg2->type_enum != PT_TYPE_MAYBE)
12044  && (!((PT_NODE_IS_SESSION_VARIABLE (arg1)) && (PT_NODE_IS_SESSION_VARIABLE (arg2)))))
12045  {
12046  /* operator without arguments or with arguments has result with system collation */
12047  collation_flag = TP_DOMAIN_COLL_NORMAL;
12048  }
12049  }
12050  dt->info.data_type.collation_flag = collation_flag;
12051  }
12052  node->data_type = dt;
12053  node->data_type->type_enum = common_type;
12054  }
12055 
12056  return NO_ERROR;
12057 }
12058 
12059 /*
12060  * pt_check_and_coerce_to_time () - check if explicit time format and
12061  * coerce to time
12062  * return: NO_ERROR on success, non-zero for ERROR
12063  * parser(in):
12064  * src(in/out): a pointer to the original PT_VALUE
12065  */
12066 static int
12068 {
12069  DB_VALUE *db_src = NULL;
12070  const char *cp;
12071  DB_TYPE dbtype;
12072  int cp_len;
12073 
12074  db_src = pt_value_to_db (parser, src);
12075  if (!db_src)
12076  {
12077  return ER_TIME_CONVERSION;
12078  }
12079 
12080  dbtype = DB_VALUE_TYPE (db_src);
12081  if (dbtype != DB_TYPE_VARCHAR && dbtype != DB_TYPE_CHAR && dbtype != DB_TYPE_VARNCHAR && dbtype != DB_TYPE_NCHAR)
12082  {
12083  return ER_TIME_CONVERSION;
12084  }
12085 
12086  if (db_get_string (db_src) == NULL)
12087  {
12088  return ER_TIME_CONVERSION;
12089  }
12090 
12091  cp = db_get_string (db_src);
12092  cp_len = db_get_string_size (db_src);
12093  if (db_string_check_explicit_time (cp, cp_len) == false)
12094  {
12095  return ER_TIME_CONVERSION;
12096  }
12097 
12098  return pt_coerce_value (parser, src, src, PT_TYPE_TIME, NULL);
12099 }
12100 
12101 /*
12102  * pt_check_and_coerce_to_date () - check if explicit date format and
12103  * coerce to date
12104  * return: NO_ERROR on success, non-zero for ERROR
12105  * parser(in):
12106  * src(in/out): node to be checked
12107  */
12108 static int
12110 {
12111  DB_VALUE *db_src = NULL;
12112  const char *str = NULL;
12113  int str_len;
12114 
12115  assert (src != NULL);
12116 
12117  if (!PT_IS_CHAR_STRING_TYPE (src->type_enum))
12118  {
12119  return ER_DATE_CONVERSION;
12120  }
12121 
12122  db_src = pt_value_to_db (parser, src);
12123  if (DB_IS_NULL (db_src))
12124  {
12125  return ER_DATE_CONVERSION;
12126  }
12127 
12128  if (db_get_string (db_src) == NULL)
12129  {
12130  return ER_DATE_CONVERSION;
12131  }
12132 
12133  str = db_get_string (db_src);
12134  str_len = db_get_string_size (db_src);
12135  if (!db_string_check_explicit_date (str, str_len))
12136  {
12137  return ER_DATE_CONVERSION;
12138  }
12139  return pt_coerce_value (parser, src, src, PT_TYPE_DATE, NULL);
12140 }
12141 
12142 /*
12143  * pt_coerce_str_to_time_date_utime_datetime () - try to coerce into
12144  * a date, time or utime
12145  * return: NO_ERROR on success, non-zero for ERROR
12146  * parser(in):
12147  * src(in/out): a pointer to the original PT_VALUE
12148  * result_type(out): the result type of the coerced result
12149  */
12150 static int
12152 {
12153  int result = -1;
12154 
12155  if (src == NULL || !PT_IS_CHAR_STRING_TYPE (src->type_enum) || pt_has_error (parser))
12156  {
12157  return result;
12158  }
12159 
12160  /* try coercing to time */
12161  if (pt_check_and_coerce_to_time (parser, src) == NO_ERROR)
12162  {
12163  *result_type = PT_TYPE_TIME;
12164  result = NO_ERROR;
12165  }
12166  else
12167  {
12168  /* get rid of error msg from previous coercion */
12169  parser_free_tree (parser, parser->error_msgs);
12170  parser->error_msgs = NULL;
12171 
12172  /* try coercing to date */
12173  if (pt_check_and_coerce_to_date (parser, src) == NO_ERROR)
12174  {
12175  *result_type = PT_TYPE_DATE;
12176  result = NO_ERROR;
12177  }
12178  else
12179  {
12180  parser_free_tree (parser, parser->error_msgs);
12181  parser->error_msgs = NULL;
12182 
12183  /* try coercing to utime */
12184  if (pt_coerce_value (parser, src, src, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
12185  {
12186  *result_type = PT_TYPE_TIMESTAMP;
12187  result = NO_ERROR;
12188  }
12189  else
12190  {
12191  parser_free_tree (parser, parser->error_msgs);
12192  parser->error_msgs = NULL;
12193 
12194  /* try coercing to datetime */
12195  if (pt_coerce_value (parser, src, src, PT_TYPE_DATETIME, NULL) == NO_ERROR)
12196  {
12197  *result_type = PT_TYPE_DATETIME;
12198  result = NO_ERROR;
12199  }
12200  }
12201  }
12202  }
12203 
12204  return result;
12205 }
12206 
12207 
12208 /*
12209  * pt_coerce_3args () - try to coerce 3 opds into their common_type if any
12210  * return: returns 1 on success, 0 otherwise
12211  * parser(in): the parser context
12212  * arg1(in): 1st operand
12213  * arg2(in): 2nd operand
12214  * arg3(in): 3rd operand
12215  */
12216 
12217 static int
12219 {
12220  PT_TYPE_ENUM common_type;
12221  PT_NODE *data_type = NULL;
12222  int result = 1;
12223 
12224  common_type = pt_common_type (arg1->type_enum, pt_common_type (arg2->type_enum, arg3->type_enum));
12225  if (common_type == PT_TYPE_NONE)
12226  {
12227  return 0;
12228  }
12229 
12230  /* try to coerce non-identical args into the common type */
12231  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
12232  {
12233  data_type = NULL;
12234  }
12235  else
12236  {
12237  if (arg1->type_enum == common_type)
12238  {
12239  data_type = arg1->data_type;
12240  }
12241  else if (arg2->type_enum == common_type)
12242  {
12243  data_type = arg2->data_type;
12244  }
12245  else if (arg3->type_enum == common_type)
12246  {
12247  data_type = arg3->data_type;
12248  }
12249  }
12250 
12251  if (arg1->type_enum != common_type)
12252  {
12253  if (PT_IS_COLLECTION_TYPE (common_type))
12254  {
12255  data_type = arg1->data_type;
12256  }
12257 
12258  result = (result && (pt_coerce_value (parser, arg1, arg1, common_type, data_type) == NO_ERROR));
12259  }
12260 
12261  if (arg2->type_enum != common_type)
12262  {
12263  if (PT_IS_COLLECTION_TYPE (common_type))
12264  {
12265  data_type = arg2->data_type;
12266  }
12267 
12268  result = (result && (pt_coerce_value (parser, arg2, arg2, common_type, data_type) == NO_ERROR));
12269  }
12270 
12271  if (arg3->type_enum != common_type)
12272  {
12273  if (PT_IS_COLLECTION_TYPE (common_type))
12274  {
12275  data_type = arg3->data_type;
12276  }
12277 
12278  result = (result && (pt_coerce_value (parser, arg3, arg3, common_type, data_type) == NO_ERROR));
12279  }
12280 
12281  return result;
12282 }
12283 
12284 /* pt_character_length_for_node() -
12285  return: number of characters that a value of the given type can possibly
12286  occuppy when cast to a CHAR type.
12287  node(in): node with type whose character length is to be returned.
12288  coerce_type(in): string type that node will be cast to
12289 */
12290 static int
12292 {
12293  int precision = DB_DEFAULT_PRECISION;
12294 
12295  switch (node->type_enum)
12296  {
12297  case PT_TYPE_DOUBLE:
12298  precision = TP_DOUBLE_AS_CHAR_LENGTH;
12299  break;
12300  case PT_TYPE_FLOAT:
12301  precision = TP_FLOAT_AS_CHAR_LENGTH;
12302  break;
12303  case PT_TYPE_MONETARY:
12304  precision = TP_MONETARY_AS_CHAR_LENGTH;
12305  break;
12306  case PT_TYPE_BIGINT:
12307  precision = TP_BIGINT_AS_CHAR_LENGTH;
12308  break;
12309  case PT_TYPE_INTEGER:
12310  precision = TP_INTEGER_AS_CHAR_LENGTH;
12311  break;
12312  case PT_TYPE_SMALLINT:
12313  precision = TP_SMALLINT_AS_CHAR_LENGTH;
12314  break;
12315  case PT_TYPE_TIME:
12316  precision = TP_TIME_AS_CHAR_LENGTH;
12317  break;
12318  case PT_TYPE_DATE:
12319  precision = TP_DATE_AS_CHAR_LENGTH;
12320  break;
12321  case PT_TYPE_TIMESTAMP:
12322  precision = TP_TIMESTAMP_AS_CHAR_LENGTH;
12323  break;
12324  case PT_TYPE_TIMESTAMPLTZ:
12325  case PT_TYPE_TIMESTAMPTZ:
12326  precision = TP_TIMESTAMPTZ_AS_CHAR_LENGTH;
12327  break;
12328  case PT_TYPE_DATETIME:
12329  precision = TP_DATETIME_AS_CHAR_LENGTH;
12330  break;
12331  case PT_TYPE_DATETIMETZ:
12332  case PT_TYPE_DATETIMELTZ:
12333  precision = TP_DATETIMETZ_AS_CHAR_LENGTH;
12334  break;
12335  case PT_TYPE_NUMERIC:
12336  if (node->data_type == NULL)
12337  {
12338  precision = DB_DEFAULT_NUMERIC_PRECISION + 1; /* sign */
12339  break;
12340  }
12341 
12342  precision = node->data_type->info.data_type.precision;
12343  if (precision == 0 || precision == DB_DEFAULT_PRECISION)
12344  {
12345  precision = DB_DEFAULT_NUMERIC_PRECISION;
12346  }
12347  precision++; /* for sign */
12348 
12352  {
12353  precision++; /* for decimal point */
12354  }
12355  break;
12356  case PT_TYPE_CHAR:
12357  if (node->data_type != NULL)
12358  {
12359  precision = node->data_type->info.data_type.precision;
12360  }
12361 
12362  if (precision == DB_DEFAULT_PRECISION)
12363  {
12364  precision = DB_MAX_CHAR_PRECISION;
12365  }
12366  break;
12367  case PT_TYPE_VARCHAR:
12368  if (node->data_type != NULL)
12369  {
12370  precision = node->data_type->info.data_type.precision;
12371  }
12372 
12373  if (precision == DB_DEFAULT_PRECISION)
12374  {
12375  precision = DB_MAX_VARCHAR_PRECISION;
12376  }
12377  break;
12378  case PT_TYPE_NCHAR:
12379  if (node->data_type != NULL)
12380  {
12381  precision = node->data_type->info.data_type.precision;
12382  }
12383 
12384  if (precision == DB_DEFAULT_PRECISION)
12385  {
12386  precision = DB_MAX_NCHAR_PRECISION;
12387  }
12388  break;
12389  case PT_TYPE_VARNCHAR:
12390  if (node->data_type != NULL)
12391  {
12392  precision = node->data_type->info.data_type.precision;
12393  }
12394 
12395  if (precision == DB_DEFAULT_PRECISION)
12396  {
12397  precision = DB_MAX_VARNCHAR_PRECISION;
12398  }
12399  break;
12400  case PT_TYPE_NULL:
12401  case PT_TYPE_NA:
12402  precision = 0;
12403  break;
12404 
12405  default:
12406  /* for host vars */
12407  switch (coerce_type)
12408  {
12409  case PT_TYPE_VARCHAR:
12410  precision = DB_MAX_VARCHAR_PRECISION;
12411  break;
12412  case PT_TYPE_VARNCHAR:
12413  precision = DB_MAX_VARNCHAR_PRECISION;
12414  break;
12415  default:
12416  precision = TP_FLOATING_PRECISION_VALUE;
12417  break;
12418  }
12419  break;
12420  }
12421 
12422  return precision;
12423 }
12424 
12425 static PT_NODE *
12427 {
12428  switch (node->info.function.function_type)
12429  {
12430  case F_BENCHMARK:
12431  // JSON functions are migrated to new checking function
12432  case F_JSON_ARRAY:
12433  case F_JSON_ARRAY_APPEND:
12434  case F_JSON_ARRAY_INSERT:
12435  case PT_JSON_ARRAYAGG:
12436  case F_JSON_CONTAINS:
12437  case F_JSON_CONTAINS_PATH:
12438  case F_JSON_DEPTH:
12439  case F_JSON_EXTRACT:
12440  case F_JSON_GET_ALL_PATHS:
12441  case F_JSON_KEYS:
12442  case F_JSON_INSERT:
12443  case F_JSON_LENGTH:
12444  case F_JSON_MERGE:
12445  case F_JSON_MERGE_PATCH:
12446  case F_JSON_OBJECT:
12447  case PT_JSON_OBJECTAGG:
12448  case F_JSON_PRETTY:
12449  case F_JSON_QUOTE:
12450  case F_JSON_REMOVE:
12451  case F_JSON_REPLACE:
12452  case F_JSON_SEARCH:
12453  case F_JSON_SET:
12454  case F_JSON_TYPE:
12455  case F_JSON_UNQUOTE:
12456  case F_JSON_VALID:
12457  case F_REGEXP_COUNT:
12458  case F_REGEXP_INSTR:
12459  case F_REGEXP_LIKE:
12460  case F_REGEXP_REPLACE:
12461  case F_REGEXP_SUBSTR:
12462  return pt_eval_function_type_new (parser, node);
12463 
12464  // legacy functions are still managed by old checking function; all should be migrated though
12465  default:
12466  return pt_eval_function_type_old (parser, node);
12467  }
12468 }
12469 
12470 /*
12471  * pt_eval_function_type () -
12472  * return: returns a node of the same type.
12473  * parser(in): parser global context info for reentrancy
12474  * node(in): a parse tree node of type PT_FUNCTION denoting an
12475  * an expression with aggregate functions.
12476  */
12477 static PT_NODE *
12479 {
12480  FUNC_TYPE fcode = node->info.function.function_type;
12481  switch (fcode)
12482  {
12483  case PT_TOP_AGG_FUNC:
12484  case F_MIDXKEY:
12485  case F_TOP_TABLE_FUNC:
12486  case F_VID:
12487  assert (false);
12488  pt_frob_error (parser, node, "ERR unsupported function code: %d", fcode);
12489  return NULL;
12490  default:;
12491  }
12492 
12493  PT_NODE *arg_list = node->info.function.arg_list;
12494  if (!arg_list && fcode != PT_COUNT_STAR && fcode != PT_GROUPBY_NUM && fcode != PT_ROW_NUMBER && fcode != PT_RANK &&
12495  fcode != PT_DENSE_RANK && fcode != PT_CUME_DIST && fcode != PT_PERCENT_RANK && fcode != F_JSON_ARRAY &&
12496  fcode != F_JSON_OBJECT)
12497  {
12499  pt_short_print (parser, node));
12500  return node;
12501  }
12502 
12503  PT_NODE *prev = NULL;
12504  PT_NODE *arg = NULL;
12505  /* to avoid "node->next" ambiguities, wrap any logical node within the arg list with a cast to integer. This way, the
12506  * CNF trees do not mix up with the arg list. */
12507  for (arg = arg_list; arg != NULL; prev = arg, arg = arg->next)
12508  {
12509  if (arg->type_enum == PT_TYPE_LOGICAL)
12510  {
12511  arg = pt_wrap_with_cast_op (parser, arg, PT_TYPE_INTEGER, 0, 0, NULL);
12512  if (arg == NULL)
12513  {
12514  /* the error message is set by pt_wrap_with_cast_op */
12515  node->type_enum = PT_TYPE_NONE;
12516  return node;
12517  }
12518  if (prev != NULL)
12519  {
12520  prev->next = arg;
12521  }
12522  else
12523  {
12524  node->info.function.arg_list = arg_list = arg;
12525  }
12526  }
12527  }
12528 
12529  if (pt_list_has_logical_nodes (arg_list))
12530  {
12533  return node;
12534  }
12535 
12536  func_type::Node funcNode (parser, node);
12537  node = funcNode.type_checking ();
12538  return node;
12539 }
12540 
12541 /*
12542  * pt_eval_function_type () -
12543  * return: returns a node of the same type.
12544  * parser(in): parser global context info for reentrancy
12545  * node(in): a parse tree node of type PT_FUNCTION denoting an
12546  * an expression with aggregate functions.
12547  *
12548  * TODO - remove me when all functions are migrated to new evaluation
12549  */
12550 static PT_NODE *
12552 {
12553  PT_NODE *arg_list;
12554  PT_TYPE_ENUM arg_type;
12555  FUNC_TYPE fcode;
12556  bool check_agg_single_arg = false;
12557  bool is_agg_function = false;
12558  PT_NODE *prev = NULL;
12559  PT_NODE *arg = NULL;
12560 
12561  is_agg_function = pt_is_aggregate_function (parser, node);
12562  arg_list = node->info.function.arg_list;
12563  fcode = node->info.function.function_type;
12564 
12565  if (!arg_list && fcode != PT_COUNT_STAR && fcode != PT_GROUPBY_NUM && fcode != PT_ROW_NUMBER && fcode != PT_RANK
12566  && fcode != PT_DENSE_RANK && fcode != PT_CUME_DIST && fcode != PT_PERCENT_RANK)
12567  {
12569  pt_short_print (parser, node));
12570  return node;
12571  }
12572 
12573  /* to avoid "node->next" ambiguities, wrap any logical node within the arg list with a cast to integer. This way, the
12574  * CNF trees do not mix up with the arg list. */
12575  for (arg = arg_list; arg != NULL; prev = arg, arg = arg->next)
12576  {
12577  if (arg->type_enum == PT_TYPE_LOGICAL)
12578  {
12579  arg = pt_wrap_with_cast_op (parser, arg, PT_TYPE_INTEGER, 0, 0, NULL);
12580  if (arg == NULL)
12581  {
12582  /* the error message is set by pt_wrap_with_cast_op */
12583  node->type_enum = PT_TYPE_NONE;
12584  return node;
12585  }
12586  if (prev != NULL)
12587  {
12588  prev->next = arg;
12589  }
12590  else
12591  {
12592  node->info.function.arg_list = arg_list = arg;
12593  }
12594  }
12595  }
12596 
12597  if (pt_list_has_logical_nodes (arg_list))
12598  {
12600  fcode_get_lowercase_name (fcode), "boolean");
12601  return node;
12602  }
12603 
12604  /*
12605  * Should only get one arg to function; set to 0 if the function
12606  * accepts more than one.
12607  */
12608  check_agg_single_arg = true;
12609  arg_type = (arg_list) ? arg_list->type_enum : PT_TYPE_NONE;
12610 
12611  switch (fcode)
12612  {
12613  case PT_STDDEV:
12614  case PT_STDDEV_POP:
12615  case PT_STDDEV_SAMP:
12616  case PT_VARIANCE:
12617  case PT_VAR_POP:
12618  case PT_VAR_SAMP:
12619  case PT_AVG:
12620  if (arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL && arg_type != PT_TYPE_NA)
12621  {
12622  if (!PT_IS_NUMERIC_TYPE (arg_type))
12623  {
12624  arg_list = pt_wrap_with_cast_op (parser, arg_list, PT_TYPE_DOUBLE, TP_FLOATING_PRECISION_VALUE, 0, NULL);
12625  if (arg_list == NULL)
12626  {
12627  return node;
12628  }
12629  }
12630  node->info.function.arg_list = arg_list;
12631  }
12632 
12633  arg_type = PT_TYPE_DOUBLE;
12634  break;
12635 
12636  case PT_AGG_BIT_AND:
12637  case PT_AGG_BIT_OR:
12638  case PT_AGG_BIT_XOR:
12639  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12640  && arg_type != PT_TYPE_NA)
12641  {
12642  /* cast arg_list to bigint */
12643  arg_list = pt_wrap_with_cast_op (parser, arg_list, PT_TYPE_BIGINT, 0, 0, NULL);
12644  if (arg_list == NULL)
12645  {
12646  return node;
12647  }
12648  arg_type = PT_TYPE_BIGINT;
12649  node->info.function.arg_list = arg_list;
12650  }
12651  break;
12652 
12653  case PT_SUM:
12654  if (!PT_IS_NUMERIC_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12655  && arg_type != PT_TYPE_NA && !pt_is_set_type (arg_list))
12656  {
12657  /* To display the sum as integer and not scientific */
12658  PT_TYPE_ENUM cast_type = (arg_type == PT_TYPE_ENUMERATION ? PT_TYPE_INTEGER : PT_TYPE_DOUBLE);
12659 
12660  /* cast arg_list to double or integer */
12661  arg_list = pt_wrap_with_cast_op (parser, arg_list, cast_type, 0, 0, NULL);
12662  if (arg_list == NULL)
12663  {
12664  return node;
12665  }
12666  arg_type = cast_type;
12667  node->info.function.arg_list = arg_list;
12668  }
12669  break;
12670 
12671  case PT_MAX:
12672  case PT_MIN:
12673  case PT_FIRST_VALUE:
12674  case PT_LAST_VALUE:
12675  case PT_NTH_VALUE:
12676  if (!PT_IS_NUMERIC_TYPE (arg_type) && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type)
12677  && arg_type != PT_TYPE_ENUMERATION && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12678  && arg_type != PT_TYPE_NA)
12679  {
12681  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type));
12682  }
12683  break;
12684 
12685  case PT_LEAD:
12686  case PT_LAG:
12687  case PT_COUNT:
12688  break;
12689 
12690  case PT_GROUP_CONCAT:
12691  {
12692  PT_TYPE_ENUM sep_type;
12693  sep_type = (arg_list->next) ? arg_list->next->type_enum : PT_TYPE_NONE;
12694  check_agg_single_arg = false;
12695 
12696  if (!PT_IS_NUMERIC_TYPE (arg_type) && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type)
12697  && arg_type != PT_TYPE_ENUMERATION && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12698  && arg_type != PT_TYPE_NA)
12699  {
12701  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type));
12702  break;
12703  }
12704 
12705  if (!PT_IS_STRING_TYPE (sep_type) && sep_type != PT_TYPE_NONE)
12706  {
12708  fcode_get_lowercase_name (fcode), pt_show_type_enum (sep_type));
12709  break;
12710  }
12711 
12712  if ((sep_type == PT_TYPE_NCHAR || sep_type == PT_TYPE_VARNCHAR) && arg_type != PT_TYPE_NCHAR
12713  && arg_type != PT_TYPE_VARNCHAR)
12714  {
12716  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12717  break;
12718  }
12719 
12720  if ((arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR) && sep_type != PT_TYPE_NCHAR
12721  && sep_type != PT_TYPE_VARNCHAR && sep_type != PT_TYPE_NONE)
12722  {
12724  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12725  break;
12726  }
12727 
12728  if ((arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT) && sep_type != PT_TYPE_BIT
12729  && sep_type != PT_TYPE_VARBIT && sep_type != PT_TYPE_NONE)
12730  {
12732  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12733  break;
12734  }
12735 
12736  if ((arg_type != PT_TYPE_BIT && arg_type != PT_TYPE_VARBIT)
12737  && (sep_type == PT_TYPE_BIT || sep_type == PT_TYPE_VARBIT))
12738  {
12740  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12741  break;
12742  }
12743  }
12744  break;
12745 
12746  case PT_CUME_DIST:
12747  case PT_PERCENT_RANK:
12748  check_agg_single_arg = false;
12749  break;
12750 
12751  case PT_NTILE:
12752  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12753  && arg_type != PT_TYPE_NA)
12754  {
12755  /* cast arg_list to double */
12756  arg_list = pt_wrap_with_cast_op (parser, arg_list, PT_TYPE_DOUBLE, 0, 0, NULL);
12757  if (arg_list == NULL)
12758  {
12759  return node;
12760  }
12761 
12762  arg_type = PT_TYPE_INTEGER;
12763  node->info.function.arg_list = arg_list;
12764  }
12765  break;
12766 
12767  case F_ELT:
12768  {
12769  /* all types used in the arguments list */
12770  bool has_arg_type[PT_TYPE_MAX - PT_TYPE_MIN] = { false };
12771 
12772  /* a subset of argument types given to ELT that can not be cast to [N]CHAR VARYING */
12773  PT_TYPE_ENUM bad_types[4] = {
12774  PT_TYPE_NONE, PT_TYPE_NONE, PT_TYPE_NONE, PT_TYPE_NONE
12775  };
12776 
12777  PT_NODE *arg = arg_list;
12778 
12779  size_t i = 0; /* used to index has_arg_type */
12780  size_t num_bad = 0; /* used to index bad_types */
12781 
12782  memset (has_arg_type, 0, sizeof (has_arg_type));
12783 
12784  /* check the index argument (null, numeric or host var) */
12786  || arg->type_enum == PT_TYPE_NONE || arg->type_enum == PT_TYPE_NA || arg->type_enum == PT_TYPE_NULL
12787  || arg->type_enum == PT_TYPE_MAYBE)
12788  {
12789  arg = arg->next;
12790  }
12791  else
12792  {
12793  arg_type = PT_TYPE_NONE;
12796  break;
12797  }
12798 
12799  /* make a list of all other argument types (null, [N]CHAR [VARYING], or host var) */
12800  while (arg)
12801  {
12802  if (arg->type_enum < PT_TYPE_MAX)
12803  {
12804  has_arg_type[arg->type_enum - PT_TYPE_MIN] = true;
12805  arg = arg->next;
12806  }
12807  else
12808  {
12809  assert (false); /* invalid data type */
12810  arg_type = PT_TYPE_NONE;
12813  break;
12814  }
12815  }
12816 
12817  /* look for unsupported argument types in the list */
12818  while (i < (sizeof (has_arg_type) / sizeof (has_arg_type[0])))
12819  {
12820  if (has_arg_type[i])
12821  {
12824  && (PT_TYPE_MIN + i != PT_TYPE_LOGICAL) && (PT_TYPE_MIN + i != PT_TYPE_NONE)
12825  && (PT_TYPE_MIN + i != PT_TYPE_NA) && (PT_TYPE_MIN + i != PT_TYPE_NULL)
12826  && (PT_TYPE_MIN + i != PT_TYPE_MAYBE))
12827  {
12828  /* type is not NULL, unknown and is not known coercible to [N]CHAR VARYING */
12829  size_t k = 0;
12830 
12831  while (k < num_bad && bad_types[k] != PT_TYPE_MIN + i)
12832  {
12833  k++;
12834  }
12835 
12836  if (k == num_bad)
12837  {
12838  bad_types[num_bad++] = (PT_TYPE_ENUM) (PT_TYPE_MIN + i);
12839 
12840  if (num_bad == sizeof (bad_types) / sizeof (bad_types[0]))
12841  {
12842  break;
12843  }
12844  }
12845  }
12846  }
12847 
12848  i++;
12849  }
12850 
12851  /* check string category (CHAR or NCHAR) for any string arguments */
12852  if ((num_bad < sizeof (bad_types) / sizeof (bad_types[0]) - 1)
12853  && (has_arg_type[PT_TYPE_CHAR - PT_TYPE_MIN] || has_arg_type[PT_TYPE_VARCHAR - PT_TYPE_MIN])
12854  && (has_arg_type[PT_TYPE_NCHAR - PT_TYPE_MIN] || has_arg_type[PT_TYPE_VARNCHAR - PT_TYPE_MIN]))
12855  {
12856  if (has_arg_type[PT_TYPE_CHAR - PT_TYPE_MIN])
12857  {
12858  bad_types[num_bad++] = PT_TYPE_CHAR;
12859  }
12860  else
12861  {
12862  bad_types[num_bad++] = PT_TYPE_VARCHAR;
12863  }
12864 
12865  if (has_arg_type[PT_TYPE_NCHAR - PT_TYPE_MIN])
12866  {
12867  bad_types[num_bad++] = PT_TYPE_NCHAR;
12868  }
12869  else
12870  {
12871  bad_types[num_bad++] = PT_TYPE_VARNCHAR;
12872  }
12873  }
12874 
12875  /* report any unsupported arguments */
12876  switch (num_bad)
12877  {
12878  case 1:
12879  arg_type = PT_TYPE_NONE;
12881  fcode_get_lowercase_name (fcode), pt_show_type_enum (bad_types[0]));
12882  break;
12883  case 2:
12884  arg_type = PT_TYPE_NONE;
12886  fcode_get_lowercase_name (fcode), pt_show_type_enum (bad_types[0]),
12887  pt_show_type_enum (bad_types[1]));
12888  break;
12889  case 3:
12890  arg_type = PT_TYPE_NONE;
12892  fcode_get_lowercase_name (fcode), pt_show_type_enum (bad_types[0]),
12893  pt_show_type_enum (bad_types[1]), pt_show_type_enum (bad_types[2]));
12894  break;
12895  case 4:
12896  arg_type = PT_TYPE_NONE;
12898  fcode_get_lowercase_name (fcode), pt_show_type_enum (bad_types[0]),
12899  pt_show_type_enum (bad_types[1]), pt_show_type_enum (bad_types[2]),
12900  pt_show_type_enum (bad_types[3]));
12901  break;
12902  }
12903  }
12904  break;
12905 
12906  case F_INSERT_SUBSTRING:
12907  {
12908  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE, arg4_type =
12909  PT_TYPE_NONE;
12911  int num_args = 0;
12912  /* arg_list to array */
12913  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
12914  {
12915  break;
12916  }
12917  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
12918  {
12919  assert (false);
12920  break;
12921  }
12922 
12923  arg1_type = arg_array[0]->type_enum;
12924  arg2_type = arg_array[1]->type_enum;
12925  arg3_type = arg_array[2]->type_enum;
12926  arg4_type = arg_array[3]->type_enum;
12927  /* check arg2 and arg3 */
12928  if (!PT_IS_NUMERIC_TYPE (arg2_type) && !PT_IS_CHAR_STRING_TYPE (arg2_type) && arg2_type != PT_TYPE_MAYBE
12929  && arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA)
12930  {
12931  arg_type = PT_TYPE_NONE;
12933  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
12934  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
12935  break;
12936  }
12937 
12938  if (!PT_IS_NUMERIC_TYPE (arg3_type) && !PT_IS_CHAR_STRING_TYPE (arg3_type) && arg3_type != PT_TYPE_MAYBE
12939  && arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_NA)
12940  {
12941  arg_type = PT_TYPE_NONE;
12943  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
12944  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
12945  break;
12946  }
12947 
12948  /* check arg1 */
12949  if (!PT_IS_NUMERIC_TYPE (arg1_type) && !PT_IS_STRING_TYPE (arg1_type) && !PT_IS_DATE_TIME_TYPE (arg1_type)
12950  && arg1_type != PT_TYPE_ENUMERATION && arg1_type != PT_TYPE_MAYBE && arg1_type != PT_TYPE_NULL
12951  && arg1_type != PT_TYPE_NA)
12952  {
12953  arg_type = PT_TYPE_NONE;
12955  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
12956  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
12957  break;
12958  }
12959  /* check arg4 */
12960  if (!PT_IS_NUMERIC_TYPE (arg4_type) && !PT_IS_STRING_TYPE (arg4_type) && !PT_IS_DATE_TIME_TYPE (arg4_type)
12961  && arg4_type != PT_TYPE_MAYBE && arg4_type != PT_TYPE_NULL && arg4_type != PT_TYPE_NA)
12962  {
12963  arg_type = PT_TYPE_NONE;
12965  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
12966  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
12967  break;
12968  }
12969  }
12970  break;
12971 
12972  case PT_MEDIAN:
12973  case PT_PERCENTILE_CONT:
12974  case PT_PERCENTILE_DISC:
12975  if (arg_type != PT_TYPE_NULL && arg_type != PT_TYPE_NA && !PT_IS_NUMERIC_TYPE (arg_type)
12976  && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE)
12977  {
12979  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg_type));
12980  }
12981 
12982  break;
12983 
12984  default:
12985  check_agg_single_arg = false;
12986  break;
12987  }
12988 
12989  if (is_agg_function && check_agg_single_arg)
12990  {
12991  if (arg_list->next)
12992  {
12994  pt_short_print (parser, node));
12995  }
12996  }
12997 
12998  if (node->type_enum == PT_TYPE_NONE || node->data_type == NULL || !(node->info.function.is_type_checked))
12999  {
13000  /* determine function result type */
13001  switch (fcode)
13002  {
13003  case PT_COUNT:
13004  case PT_COUNT_STAR:
13005  case PT_ROW_NUMBER:
13006  case PT_RANK:
13007  case PT_DENSE_RANK:
13008  case PT_NTILE:
13009  node->type_enum = PT_TYPE_INTEGER;
13010  break;
13011 
13012  case PT_CUME_DIST:
13013  case PT_PERCENT_RANK:
13014  node->type_enum = PT_TYPE_DOUBLE;
13015  break;
13016 
13017  case PT_GROUPBY_NUM:
13018  node->type_enum = PT_TYPE_BIGINT;
13019  break;
13020 
13021  case PT_AGG_BIT_AND:
13022  case PT_AGG_BIT_OR:
13023  case PT_AGG_BIT_XOR:
13024  node->type_enum = PT_TYPE_BIGINT;
13025  break;
13026 
13027  case F_TABLE_SET:
13028  node->type_enum = PT_TYPE_SET;
13029  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13030  break;
13031 
13032  case F_TABLE_MULTISET:
13033  node->type_enum = PT_TYPE_MULTISET;
13034  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13035  break;
13036 
13037  case F_TABLE_SEQUENCE:
13038  node->type_enum = PT_TYPE_SEQUENCE;
13039  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13040  break;
13041 
13042  case F_SET:
13043  node->type_enum = PT_TYPE_SET;
13044  pt_add_type_to_set (parser, arg_list, &node->data_type);
13045  break;
13046 
13047  case F_MULTISET:
13048  node->type_enum = PT_TYPE_MULTISET;
13049  pt_add_type_to_set (parser, arg_list, &node->data_type);
13050  break;
13051 
13052  case F_SEQUENCE:
13053  node->type_enum = PT_TYPE_SEQUENCE;
13054  pt_add_type_to_set (parser, arg_list, &node->data_type);
13055  break;
13056 
13057  case PT_SUM:
13058  node->type_enum = arg_type;
13059  node->data_type = parser_copy_tree_list (parser, arg_list->data_type);
13060  if (arg_type == PT_TYPE_NUMERIC && node->data_type)
13061  {
13063  }
13064  break;
13065 
13066  case PT_AVG:
13067  case PT_STDDEV:
13068  case PT_STDDEV_POP:
13069  case PT_STDDEV_SAMP:
13070  case PT_VARIANCE:
13071  case PT_VAR_POP:
13072  case PT_VAR_SAMP:
13073  node->type_enum = arg_type;
13074  node->data_type = NULL;
13075  break;
13076 
13077  case PT_MEDIAN:
13078  case PT_PERCENTILE_CONT:
13079  case PT_PERCENTILE_DISC:
13080  /* let calculation decide the type */
13081  node->type_enum = PT_TYPE_MAYBE;
13082  node->data_type = NULL;
13083  break;
13084 
13085  case PT_GROUP_CONCAT:
13086  {
13087  if (arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR)
13088  {
13089  node->type_enum = PT_TYPE_VARNCHAR;
13091  if (node->data_type == NULL)
13092  {
13093  assert (false);
13094  }
13096  }
13097  else if (arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT)
13098  {
13099  node->type_enum = PT_TYPE_VARBIT;
13101  if (node->data_type == NULL)
13102  {
13103  node->type_enum = PT_TYPE_NONE;
13104  assert (false);
13105  }
13107  }
13108  else
13109  {
13110  node->type_enum = PT_TYPE_VARCHAR;
13112  if (node->data_type == NULL)
13113  {
13114  node->type_enum = PT_TYPE_NONE;
13115  assert (false);
13116  }
13118  }
13119  }
13120  break;
13121 
13122  case F_INSERT_SUBSTRING:
13123  {
13124  PT_NODE *new_att = NULL;
13125  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE, arg4_type =
13126  PT_TYPE_NONE;
13127  PT_TYPE_ENUM arg1_orig_type, arg2_orig_type, arg3_orig_type, arg4_orig_type;
13129  int num_args;
13130 
13131  /* arg_list to array */
13132  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
13133  {
13134  break;
13135  }
13136  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
13137  {
13138  assert (false);
13139  break;
13140  }
13141 
13142  arg1_orig_type = arg1_type = arg_array[0]->type_enum;
13143  arg2_orig_type = arg2_type = arg_array[1]->type_enum;
13144  arg3_orig_type = arg3_type = arg_array[2]->type_enum;
13145  arg4_orig_type = arg4_type = arg_array[3]->type_enum;
13146  arg_type = PT_TYPE_NONE;
13147 
13148  /* validate and/or convert arguments */
13149  /* arg1 should be VAR-str, but compatible with arg4 (except when arg4 is BIT - no casting to bit on arg1) */
13150  if (!(PT_IS_STRING_TYPE (arg1_type)))
13151  {
13152  PT_TYPE_ENUM upgraded_type = PT_TYPE_NONE;
13153  if (arg4_type == PT_TYPE_NCHAR)
13154  {
13155  upgraded_type = PT_TYPE_VARNCHAR;
13156  }
13157  else
13158  {
13159  upgraded_type = PT_TYPE_VARCHAR;
13160  }
13161 
13162  new_att =
13163  pt_wrap_with_cast_op (parser, arg_array[0], upgraded_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
13164  if (new_att == NULL)
13165  {
13166  break;
13167  }
13168  node->info.function.arg_list = arg_array[0] = new_att;
13169  arg_type = arg1_type = upgraded_type;
13170  }
13171  else
13172  {
13173  arg_type = arg1_type;
13174  }
13175 
13176 
13177  if (arg2_type != PT_TYPE_INTEGER)
13178  {
13179  new_att =
13181  if (new_att == NULL)
13182  {
13183  break;
13184  }
13185  arg_array[0]->next = arg_array[1] = new_att;
13186  arg2_type = PT_TYPE_INTEGER;
13187 
13188  }
13189 
13190  if (arg3_type != PT_TYPE_INTEGER)
13191  {
13192  new_att =
13194  if (new_att == NULL)
13195  {
13196  break;
13197  }
13198  arg_array[1]->next = arg_array[2] = new_att;
13199  arg3_type = PT_TYPE_INTEGER;
13200  }
13201 
13202  /* set result type and precision */
13203  if (arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR)
13204  {
13205  node->type_enum = PT_TYPE_VARNCHAR;
13208  }
13209  else if (arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT)
13210  {
13211  node->type_enum = PT_TYPE_VARBIT;
13214  }
13215  else
13216  {
13217  arg_type = node->type_enum = PT_TYPE_VARCHAR;
13220  }
13221  /* validate and/or set arg4 */
13222  if (!(PT_IS_STRING_TYPE (arg4_type)))
13223  {
13224  new_att = pt_wrap_with_cast_op (parser, arg_array[3], arg_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
13225  if (new_att == NULL)
13226  {
13227  break;
13228  }
13229  arg_array[2]->next = arg_array[3] = new_att;
13230  }
13231  /* final check of arg and arg4 type matching */
13232  if ((arg4_type == PT_TYPE_VARNCHAR || arg4_type == PT_TYPE_NCHAR)
13233  && (arg_type == PT_TYPE_VARCHAR || arg_type == PT_TYPE_CHAR))
13234  {
13235  arg_type = PT_TYPE_NONE;
13237  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_orig_type),
13238  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13239  pt_show_type_enum (arg4_orig_type));
13240  }
13241  else if ((arg_type == PT_TYPE_VARNCHAR || arg_type == PT_TYPE_NCHAR)
13242  && (arg4_type == PT_TYPE_VARCHAR || arg4_type == PT_TYPE_CHAR))
13243  {
13244  arg_type = PT_TYPE_NONE;
13246  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_orig_type),
13247  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13248  pt_show_type_enum (arg4_orig_type));
13249  }
13250  else if ((arg_type == PT_TYPE_VARBIT || arg_type == PT_TYPE_BIT)
13251  && (arg4_type != PT_TYPE_VARBIT && arg4_type != PT_TYPE_BIT))
13252  {
13253  arg_type = PT_TYPE_NONE;
13255  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_orig_type),
13256  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13257  pt_show_type_enum (arg4_orig_type));
13258  }
13259  else if ((arg4_type == PT_TYPE_VARBIT || arg4_type == PT_TYPE_BIT)
13260  && (arg_type != PT_TYPE_VARBIT && arg_type != PT_TYPE_BIT))
13261  {
13262  arg_type = PT_TYPE_NONE;
13264  fcode_get_lowercase_name (fcode), pt_show_type_enum (arg1_orig_type),
13265  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13266  pt_show_type_enum (arg4_orig_type));
13267  }
13268  }
13269  break;
13270 
13271  case F_ELT:
13272  {
13273  PT_NODE *new_att = NULL;
13274  PT_NODE *arg = arg_list, *prev_arg = arg_list;
13275  int max_precision = 0;
13276 
13277  /* check and cast the type for the index argument. */
13279  {
13281 
13282  if (new_att)
13283  {
13284  prev_arg = arg_list = arg = new_att;
13285  node->info.function.arg_list = arg_list;
13286  }
13287  else
13288  {
13289  break;
13290  }
13291  }
13292 
13293  /*
13294  * Look for the first argument of character string type and obtain its category (CHAR/NCHAR). All other
13295  * arguments should be converted to this type, which is also the return type. */
13296 
13297  arg_type = PT_TYPE_NONE;
13298  arg = arg->next;
13299 
13300  while (arg && arg_type == PT_TYPE_NONE)
13301  {
13302  if (PT_IS_CHAR_STRING_TYPE (arg->type_enum))
13303  {
13304  if (arg->type_enum == PT_TYPE_CHAR || arg->type_enum == PT_TYPE_VARCHAR)
13305  {
13306  arg_type = PT_TYPE_VARCHAR;
13307  }
13308  else
13309  {
13310  arg_type = PT_TYPE_VARNCHAR;
13311  }
13312  }
13313  else
13314  {
13315  arg = arg->next;
13316  }
13317  }
13318 
13319  if (arg_type == PT_TYPE_NONE)
13320  {
13321  /* no [N]CHAR [VARYING] argument passed; convert them all to VARCHAR */
13322  arg_type = PT_TYPE_VARCHAR;
13323  }
13324 
13325  /* take the maximum precision among all value arguments */
13326  arg = arg_list->next;
13327 
13328  while (arg)
13329  {
13330  int precision = TP_FLOATING_PRECISION_VALUE;
13331 
13332  precision = pt_character_length_for_node (arg, arg_type);
13333  if (max_precision != TP_FLOATING_PRECISION_VALUE)
13334  {
13335  if (precision == TP_FLOATING_PRECISION_VALUE || max_precision < precision)
13336  {
13337  max_precision = precision;
13338  }
13339  }
13340 
13341  arg = arg->next;
13342  }
13343 
13344  /* cast all arguments to [N]CHAR VARYING(max_precision) */
13345  arg = arg_list->next;
13346  while (arg)
13347  {
13348  if ((arg->type_enum != arg_type) ||
13349  (arg->data_type && arg->data_type->info.data_type.precision != max_precision))
13350  {
13351  PT_NODE *new_attr = pt_wrap_with_cast_op (parser, arg, arg_type,
13352  max_precision, 0, NULL);
13353 
13354  if (new_attr)
13355  {
13356  prev_arg->next = arg = new_attr;
13357  }
13358  else
13359  {
13360  break;
13361  }
13362  }
13363 
13364  arg = arg->next;
13365  prev_arg = prev_arg->next;
13366  }
13367 
13368  /* Return the selected data type and precision */
13369 
13370  node->data_type = pt_make_prim_data_type (parser, arg_type);
13371 
13372  if (node->data_type)
13373  {
13374  node->type_enum = arg_type;
13375  node->data_type->info.data_type.precision = max_precision;
13377  }
13378  }
13379  break;
13380 
13381  default:
13382  /* otherwise, f(x) has same type as x */
13383  node->type_enum = arg_type;
13384  node->data_type = parser_copy_tree_list (parser, arg_list->data_type);
13385  break;
13386  }
13387  /* to prevent recheck of function return type at pt_eval_function_type_old() */
13388  node->info.function.is_type_checked = true;
13389  }
13390 
13391  /* collation checking */
13392  arg_list = node->info.function.arg_list;
13393  switch (fcode)
13394  {
13395  case PT_GROUP_CONCAT:
13396  {
13397  PT_COLL_INFER coll_infer1;
13398  PT_NODE *new_node;
13399  PT_TYPE_ENUM sep_type;
13400 
13401  (void) pt_get_collation_info (arg_list, &coll_infer1);
13402 
13403  sep_type = (arg_list->next) ? arg_list->next->type_enum : PT_TYPE_NONE;
13404  if (PT_HAS_COLLATION (sep_type))
13405  {
13406  assert (arg_list->next != NULL);
13407 
13408  new_node =
13409  pt_coerce_node_collation (parser, arg_list->next, coll_infer1.coll_id, coll_infer1.codeset, false, false,
13411 
13412  if (new_node == NULL)
13413  {
13414  goto error_collation;
13415  }
13416 
13417  arg_list->next = new_node;
13418  }
13419 
13420  if (arg_list->type_enum != PT_TYPE_MAYBE)
13421  {
13422  new_node =
13423  pt_coerce_node_collation (parser, node, coll_infer1.coll_id, coll_infer1.codeset, true, false,
13425 
13426  if (new_node == NULL)
13427  {
13428  goto error_collation;
13429  }
13430 
13431  node = new_node;
13432  }
13433  else if (node->data_type != NULL)
13434  {
13435  /* argument is not determined, collation of result will be resolved at execution */
13437  }
13438  }
13439  break;
13440 
13441  case F_INSERT_SUBSTRING:
13442  {
13443  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg4_type = PT_TYPE_NONE;
13445  int num_args = 0;
13446  PT_COLL_INFER coll_infer1, coll_infer4;
13447  INTL_CODESET common_cs = LANG_SYS_CODESET;
13448  int common_coll = LANG_SYS_COLLATION;
13449  PT_NODE *new_node;
13450  int args_w_coll = 0;
13451 
13452  coll_infer1.codeset = LANG_SYS_CODESET;
13453  coll_infer4.codeset = LANG_SYS_CODESET;
13454  coll_infer1.coll_id = LANG_SYS_COLLATION;
13455  coll_infer4.coll_id = LANG_SYS_COLLATION;
13458  coll_infer1.can_force_cs = true;
13459  coll_infer4.can_force_cs = true;
13460 
13461  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
13462  {
13463  break;
13464  }
13465 
13466  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
13467  {
13468  assert (false);
13469  break;
13470  }
13471 
13472  arg1_type = arg_array[0]->type_enum;
13473  arg4_type = arg_array[3]->type_enum;
13474 
13475  if (PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE)
13476  {
13477  if (pt_get_collation_info (arg_array[0], &coll_infer1))
13478  {
13479  args_w_coll++;
13480  }
13481 
13482  if (arg1_type != PT_TYPE_MAYBE)
13483  {
13484  common_coll = coll_infer1.coll_id;
13485  common_cs = coll_infer1.codeset;
13486  }
13487  }
13488 
13489  if (PT_HAS_COLLATION (arg4_type) || arg4_type == PT_TYPE_MAYBE)
13490  {
13491  if (pt_get_collation_info (arg_array[3], &coll_infer4))
13492  {
13493  args_w_coll++;
13494  }
13495 
13496  if (arg1_type != PT_TYPE_MAYBE)
13497  {
13498  common_coll = coll_infer4.coll_id;
13499  common_cs = coll_infer4.codeset;
13500  }
13501  }
13502 
13503  if (coll_infer1.coll_id == coll_infer4.coll_id)
13504  {
13505  assert (coll_infer1.codeset == coll_infer4.codeset);
13506  common_coll = coll_infer1.coll_id;
13507  common_cs = coll_infer1.codeset;
13508  }
13509  else
13510  {
13511  if (pt_common_collation (&coll_infer1, &coll_infer4, NULL, args_w_coll, false, &common_coll, &common_cs) !=
13512  0)
13513  {
13514  goto error_collation;
13515  }
13516  }
13517 
13518  /* coerce collation of arguments */
13519  if ((common_coll != coll_infer1.coll_id || common_cs != coll_infer1.codeset)
13520  && (PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE))
13521  {
13522  new_node =
13523  pt_coerce_node_collation (parser, arg_array[0], common_coll, common_cs, coll_infer1.can_force_cs, false,
13525  if (new_node == NULL)
13526  {
13527  goto error_collation;
13528  }
13529 
13530  node->info.function.arg_list = arg_array[0] = new_node;
13531  }
13532 
13533  /* coerce collation of arguments */
13534  if ((common_coll != coll_infer4.coll_id || common_cs != coll_infer4.codeset)
13535  && (PT_HAS_COLLATION (arg4_type) || arg4_type == PT_TYPE_MAYBE))
13536  {
13537  new_node =
13538  pt_coerce_node_collation (parser, arg_array[3], common_coll, common_cs, coll_infer4.can_force_cs, false,
13540  if (new_node == NULL)
13541  {
13542  goto error_collation;
13543  }
13544 
13545  arg_array[2]->next = arg_array[3] = new_node;
13546  }
13547 
13548  if ((arg_array[3]->type_enum == PT_TYPE_MAYBE || PT_IS_CAST_MAYBE (arg_array[3]))
13549  && (arg_array[0]->type_enum == PT_TYPE_MAYBE || PT_IS_CAST_MAYBE (arg_array[0])) && node->data_type != NULL)
13550  {
13552  }
13553  else
13554  {
13555  new_node =
13556  pt_coerce_node_collation (parser, node, common_coll, common_cs, true, false, PT_TYPE_NONE, PT_TYPE_NONE);
13557  if (new_node == NULL)
13558  {
13559  goto error_collation;
13560  }
13561 
13562  node = new_node;
13563  }
13564  }
13565  break;
13566 
13567  default:
13568  node = pt_check_function_collation (parser, node);
13569  break;
13570  }
13571 
13572  return node;
13573 
13574 error_collation:
13576  fcode_get_lowercase_name (fcode));
13577 
13578  return node;
13579 }
13580 
13581 /*
13582  * pt_eval_method_call_type () -
13583  * return: returns a node of the same type.
13584  * parser(in): parser global context info for reentrancy
13585  * node(in): a parse tree node of type PT_METHOD_CALL.
13586  */
13587 
13588 static PT_NODE *
13590 {
13591  PT_NODE *method_name;
13592  PT_NODE *on_call_target;
13593  DB_OBJECT *obj = (DB_OBJECT *) 0;
13594  const char *name = (const char *) 0;
13595  DB_METHOD *method = (DB_METHOD *) 0;
13596  DB_DOMAIN *domain;
13597  DB_TYPE type;
13598 
13599  method_name = node->info.method_call.method_name;
13600  on_call_target = node->info.method_call.on_call_target;
13601  if (on_call_target == NULL)
13602  {
13603  return node;
13604  }
13605 
13606  if (method_name->node_type == PT_NAME)
13607  {
13608  name = method_name->info.name.original;
13609  }
13610 
13611  switch (on_call_target->node_type)
13612  {
13613  case PT_VALUE:
13614  obj = on_call_target->info.value.data_value.op;
13615  if (obj && name)
13616  {
13617  if ((method = (DB_METHOD *) db_get_method (obj, name)) == NULL)
13618  {
13619  if (er_errid () == ER_OBJ_INVALID_METHOD)
13620  {
13621  er_clear ();
13622  }
13623 
13624  method = (DB_METHOD *) db_get_class_method (obj, name);
13625  }
13626  }
13627  break;
13628 
13629  case PT_NAME:
13630  if (on_call_target->data_type && on_call_target->data_type->info.data_type.entity)
13631  {
13632  obj = on_call_target->data_type->info.data_type.entity->info.name.db_object;
13633  }
13634 
13635  if (obj && name)
13636  {
13637  method = (DB_METHOD *) db_get_method (obj, name);
13638  if (method == NULL)
13639  {
13640  if (er_errid () == ER_OBJ_INVALID_METHOD)
13641  {
13642  er_clear ();
13643  }
13644  method = (DB_METHOD *) db_get_class_method (obj, name);
13645  }
13646  }
13647  break;
13648 
13649  default:
13650  break;
13651  }
13652 
13653  if (method)
13654  {
13655  domain = db_method_return_domain (method);
13656  if (domain)
13657  {
13658  type = TP_DOMAIN_TYPE (domain);
13659  node->type_enum = pt_db_to_type_enum (type);
13660  }
13661  }
13662 
13663  return node;
13664 }
13665 
13666 /*
13667  * pt_evaluate_db_value_expr () - apply op to db_value opds & place it in result
13668  * return: 1 if evaluation succeeded, 0 otherwise
13669  * parser(in): handle to the parser context
13670  * expr(in): the expression to be applied
13671  * op(in): a PT_OP_TYPE (the desired operation)
13672  * arg1(in): 1st db_value operand
13673  * arg2(in): 2nd db_value operand
13674  * arg3(in): 3rd db_value operand
13675  * result(out): a newly set db_value if successful, untouched otherwise
13676  * domain(in): domain of result (for arithmetic & set ops)
13677  * o1(in): a PT_NODE containing the source line & column position of arg1
13678  * o2(in): a PT_NODE containing the source line & column position of arg2
13679  * o3(in): a PT_NODE containing the source line & column position of arg3
13680  * qualifier(in): trim qualifier or datetime component specifier
13681  */
13682 
13683 int
13685  DB_VALUE * arg3, DB_VALUE * result, TP_DOMAIN * domain, PT_NODE * o1, PT_NODE * o2,
13686  PT_NODE * o3, PT_MISC_TYPE qualifier)
13687 {
13688  DB_TYPE typ;
13689  DB_TYPE typ1, typ2;
13690  PT_TYPE_ENUM rTyp;
13691  int cmp;
13692  DB_VALUE_COMPARE_RESULT cmp_result;
13693  DB_VALUE_COMPARE_RESULT cmp_result2;
13694  DB_VALUE tmp_val;
13695  int error, i;
13696  DB_DATA_STATUS truncation;
13697  TP_DOMAIN_STATUS dom_status;
13698  PT_NODE *between_ge_lt, *between_ge_lt_arg1, *between_ge_lt_arg2;
13699  DB_VALUE *width_bucket_arg2 = NULL, *width_bucket_arg3 = NULL;
13700 
13701  assert (parser != NULL);
13702 
13703  if (!arg1 || !result)
13704  {
13705  return 0;
13706  }
13707 
13708  typ = TP_DOMAIN_TYPE (domain);
13709  rTyp = pt_db_to_type_enum (typ);
13710 
13711  /* do not coerce arg1, arg2 for STRCAT */
13712  if (op == PT_PLUS && PT_IS_STRING_TYPE (rTyp))
13713  {
13715  {
13716  op = PT_STRCAT;
13717  }
13718  else
13719  {
13720  /* parameters should already be coerced to other types */
13721  assert (false);
13722  }
13723  }
13724 
13725  typ1 = (arg1) ? DB_VALUE_TYPE (arg1) : DB_TYPE_NULL;
13726  typ2 = (arg2) ? DB_VALUE_TYPE (arg2) : DB_TYPE_NULL;
13727  cmp = 0;
13728  db_make_null (result);
13729 
13730  switch (op)
13731  {
13732  case PT_NOT:
13733  if (typ1 == DB_TYPE_NULL)
13734  {
13735  db_make_null (result); /* not NULL = NULL */
13736  }
13737  else if (db_get_int (arg1))
13738  {
13739  db_make_int (result, false); /* not true = false */
13740  }
13741  else
13742  {
13743  db_make_int (result, true); /* not false = true */
13744  }
13745  break;
13746 
13747  case PT_PRIOR:
13748  case PT_QPRIOR:
13749  case PT_CONNECT_BY_ROOT:
13750  if (db_value_clone (arg1, result) != NO_ERROR)
13751  {
13752  return 0;
13753  }
13754  break;
13755 
13757  return 0;
13758 
13759  case PT_BIT_NOT:
13760  switch (typ1)
13761  {
13762  case DB_TYPE_NULL:
13763  db_make_null (result);
13764  break;
13765 
13766  case DB_TYPE_INTEGER:
13767  db_make_bigint (result, ~((DB_BIGINT) db_get_int (arg1)));
13768  break;
13769 
13770  case DB_TYPE_BIGINT:
13771  db_make_bigint (result, ~db_get_bigint (arg1));
13772  break;
13773 
13774  case DB_TYPE_SHORT:
13775  db_make_bigint (result, ~((DB_BIGINT) db_get_short (arg1)));
13776  break;
13777 
13778  default:
13779  return 0;
13780  }
13781  break;
13782 
13783  case PT_BIT_AND:
13784  {
13785  DB_BIGINT bi[2];
13786  DB_TYPE dbtype[2];
13787  DB_VALUE *dbval[2];
13788  int i;
13789 
13790  dbtype[0] = typ1;
13791  dbtype[1] = typ2;
13792  dbval[0] = arg1;
13793  dbval[1] = arg2;
13794 
13795  for (i = 0; i < 2; i++)
13796  {
13797  switch (dbtype[i])
13798  {
13799  case DB_TYPE_NULL:
13800  db_make_null (result);
13801  break;
13802 
13803  case DB_TYPE_INTEGER:
13804  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
13805  break;
13806 
13807  case DB_TYPE_BIGINT:
13808  bi[i] = db_get_bigint (dbval[i]);
13809  break;
13810 
13811  case DB_TYPE_SHORT:
13812  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
13813  break;
13814 
13815  default:
13816  return 0;
13817  }
13818  }
13819 
13820  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
13821  {
13822  db_make_bigint (result, bi[0] & bi[1]);
13823  }
13824  }
13825  break;
13826 
13827  case PT_BIT_OR:
13828  {
13829  DB_BIGINT bi[2];
13830  DB_TYPE dbtype[2];
13831  DB_VALUE *dbval[2];
13832  int i;
13833 
13834  dbtype[0] = typ1;
13835  dbtype[1] = typ2;
13836  dbval[0] = arg1;
13837  dbval[1] = arg2;
13838 
13839  for (i = 0; i < 2; i++)
13840  {
13841  switch (dbtype[i])
13842  {
13843  case DB_TYPE_NULL:
13844  db_make_null (result);
13845  break;
13846 
13847  case DB_TYPE_INTEGER:
13848  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
13849  break;
13850 
13851  case DB_TYPE_BIGINT:
13852  bi[i] = db_get_bigint (dbval[i]);
13853  break;
13854 
13855  case DB_TYPE_SHORT:
13856  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
13857  break;
13858 
13859  default:
13860  return 0;
13861  }
13862  }
13863 
13864  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
13865  {
13866  db_make_bigint (result, bi[0] | bi[1]);
13867  }
13868  }
13869  break;
13870 
13871  case PT_BIT_XOR:
13872  {
13873  DB_BIGINT bi[2];
13874  DB_TYPE dbtype[2];
13875  DB_VALUE *dbval[2];
13876  int i;
13877 
13878  dbtype[0] = typ1;
13879  dbtype[1] = typ2;
13880  dbval[0] = arg1;
13881  dbval[1] = arg2;
13882 
13883  for (i = 0; i < 2; i++)
13884  {
13885  switch (dbtype[i])
13886  {
13887  case DB_TYPE_NULL:
13888  db_make_null (result);
13889  break;
13890 
13891  case DB_TYPE_INTEGER:
13892  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
13893  break;
13894 
13895  case DB_TYPE_BIGINT:
13896  bi[i] = db_get_bigint (dbval[i]);
13897  break;
13898 
13899  case DB_TYPE_SHORT:
13900  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
13901  break;
13902 
13903  default:
13904  return 0;
13905  }
13906  }
13907 
13908  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
13909  {
13910  db_make_bigint (result, bi[0] ^ bi[1]);
13911  }
13912  }
13913  break;
13914 
13915  case PT_BITSHIFT_LEFT:
13916  case PT_BITSHIFT_RIGHT:
13917  {
13918  DB_BIGINT bi[2];
13919  DB_TYPE dbtype[2];
13920  DB_VALUE *dbval[2];
13921  int i;
13922 
13923  dbtype[0] = typ1;
13924  dbtype[1] = typ2;
13925  dbval[0] = arg1;
13926  dbval[1] = arg2;
13927 
13928  for (i = 0; i < 2; i++)
13929  {
13930  switch (dbtype[i])
13931  {
13932  case DB_TYPE_NULL:
13933  db_make_null (result);
13934  break;
13935 
13936  case DB_TYPE_INTEGER:
13937  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
13938  break;
13939 
13940  case DB_TYPE_BIGINT:
13941  bi[i] = db_get_bigint (dbval[i]);
13942  break;
13943 
13944  case DB_TYPE_SHORT:
13945  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
13946  break;
13947 
13948  default:
13949  return 0;
13950  }
13951  }
13952 
13953  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
13954  {
13955  if (bi[1] < (int) (sizeof (DB_BIGINT) * 8) && bi[1] >= 0)
13956  {
13957  if (op == PT_BITSHIFT_LEFT)
13958  {
13959  db_make_bigint (result, ((UINT64) bi[0]) << ((UINT64) bi[1]));
13960  }
13961  else
13962  {
13963  db_make_bigint (result, ((UINT64) bi[0]) >> ((UINT64) bi[1]));
13964  }
13965  }
13966  else
13967  {
13968  db_make_bigint (result, 0);
13969  }
13970  }
13971  }
13972  break;
13973 
13974  case PT_DIV:
13975  case PT_MOD:
13976  {
13977  DB_BIGINT bi[2];
13978  DB_TYPE dbtype[2];
13979  DB_VALUE *dbval[2];
13980  int i;
13981 
13982  dbtype[0] = typ1;
13983  dbtype[1] = typ2;
13984  dbval[0] = arg1;
13985  dbval[1] = arg2;
13986 
13987  for (i = 0; i < 2; i++)
13988  {
13989  switch (dbtype[i])
13990  {
13991  case DB_TYPE_NULL:
13992  db_make_null (result);
13993  break;
13994 
13995  case DB_TYPE_INTEGER:
13996  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
13997  break;
13998 
13999  case DB_TYPE_BIGINT:
14000  bi[i] = db_get_bigint (dbval[i]);
14001  break;
14002 
14003  case DB_TYPE_SHORT:
14004  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14005  break;
14006 
14007  default:
14008  return 0;
14009  }
14010  }
14011 
14012  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14013  {
14014  if (bi[1] != 0)
14015  {
14016  if (op == PT_DIV)
14017  {
14018  if (typ1 == DB_TYPE_INTEGER)
14019  {
14020  if (OR_CHECK_INT_DIV_OVERFLOW (bi[0], bi[1]))
14021  {
14022  goto overflow;
14023  }
14024  db_make_int (result, (INT32) (bi[0] / bi[1]));
14025  }
14026  else if (typ1 == DB_TYPE_BIGINT)
14027  {
14028  if (OR_CHECK_BIGINT_DIV_OVERFLOW (bi[0], bi[1]))
14029  {
14030  goto overflow;
14031  }
14032  db_make_bigint (result, bi[0] / bi[1]);
14033  }
14034  else
14035  {
14036  if (OR_CHECK_SHORT_DIV_OVERFLOW (bi[0], bi[1]))
14037  {
14038  goto overflow;
14039  }
14040  db_make_short (result, (INT16) (bi[0] / bi[1]));
14041  }
14042  }
14043  else
14044  {
14045  if (typ1 == DB_TYPE_INTEGER)
14046  {
14047  db_make_int (result, (INT32) (bi[0] % bi[1]));
14048  }
14049  else if (typ1 == DB_TYPE_BIGINT)
14050  {
14051  db_make_bigint (result, bi[0] % bi[1]);
14052  }
14053  else
14054  {
14055  db_make_short (result, (INT16) (bi[0] % bi[1]));
14056  }
14057  }
14058  }
14059  else
14060  {
14062  return 0;
14063  }
14064  }
14065  }
14066  break;
14067 
14068  case PT_IF:
14069  { /* Obs: when this case occurs both args are the same type */
14070  if (DB_IS_NULL (arg1))
14071  {
14072  if (db_value_clone (arg3, result) != NO_ERROR)
14073  {
14074  return 0;
14075  }
14076  }
14077  else
14078  {
14079  if (db_get_int (arg1))
14080  {
14081  if (db_value_clone (arg2, result) != NO_ERROR)
14082  {
14083  return 0;
14084  }
14085  }
14086  else
14087  {
14088  if (db_value_clone (arg3, result) != NO_ERROR)
14089  {
14090  return 0;
14091  }
14092  }
14093  }
14094  }
14095  break;
14096 
14097  case PT_IFNULL:
14098  case PT_COALESCE:
14099  case PT_NVL:
14100  {
14101  DB_VALUE *src;
14102  TP_DOMAIN *target_domain = NULL;
14103  PT_NODE *target_node;
14104 
14105  if (typ == DB_TYPE_VARIABLE)
14106  {
14107  TP_DOMAIN *arg1_domain, *arg2_domain;
14108  TP_DOMAIN tmp_arg1_domain, tmp_arg2_domain;
14109 
14110  arg1_domain = tp_domain_resolve_value (arg1, &tmp_arg1_domain);
14111  arg2_domain = tp_domain_resolve_value (arg2, &tmp_arg2_domain);
14112 
14113  target_domain = tp_infer_common_domain (arg1_domain, arg2_domain);
14114  }
14115  else
14116  {
14117  target_domain = domain;
14118  }
14119 
14120  if (typ1 == DB_TYPE_NULL)
14121  {
14122  src = arg2;
14123  target_node = o2;
14124  }
14125  else
14126  {
14127  src = arg1;
14128  target_node = o1;
14129  }
14130 
14131  if (tp_value_cast (src, result, target_domain, false) != DOMAIN_COMPATIBLE)
14132  {
14133  rTyp = pt_db_to_type_enum (target_domain->type->id);
14135  pt_short_print (parser, target_node), pt_show_type_enum (rTyp));
14136 
14137  return 0;
14138  }
14139  }
14140  return 1;
14141 
14142  case PT_NVL2:
14143  {
14144  DB_VALUE *src;
14145  PT_NODE *target_node;
14146  TP_DOMAIN *target_domain = NULL;
14147  TP_DOMAIN *tmp_domain = NULL;
14148 
14149  if (typ == DB_TYPE_VARIABLE)
14150  {
14151  TP_DOMAIN *arg1_domain, *arg2_domain, *arg3_domain;
14152  TP_DOMAIN tmp_arg1_domain, tmp_arg2_domain, tmp_arg3_domain;
14153 
14154  arg1_domain = tp_domain_resolve_value (arg1, &tmp_arg1_domain);
14155  arg2_domain = tp_domain_resolve_value (arg2, &tmp_arg2_domain);
14156  arg3_domain = tp_domain_resolve_value (arg3, &tmp_arg3_domain);
14157 
14158  tmp_domain = tp_infer_common_domain (arg1_domain, arg2_domain);
14159  target_domain = tp_infer_common_domain (tmp_domain, arg3_domain);
14160  }
14161  else
14162  {
14163  target_domain = domain;
14164  }
14165 
14166  if (typ1 == DB_TYPE_NULL)
14167  {
14168  src = arg3;
14169  target_node = o3;
14170  }
14171  else
14172  {
14173  src = arg2;
14174  target_node = o2;
14175  }
14176 
14177  if (tp_value_cast (src, result, target_domain, false) != DOMAIN_COMPATIBLE)
14178  {
14179  rTyp = pt_db_to_type_enum (target_domain->type->id);
14181  pt_short_print (parser, target_node), pt_show_type_enum (rTyp));
14182  return 0;
14183  }
14184  }
14185  return 1;
14186 
14187  case PT_ISNULL:
14188  if (DB_IS_NULL (arg1))
14189  {
14190  db_make_int (result, true);
14191  }
14192  else
14193  {
14194  db_make_int (result, false);
14195  }
14196  break;
14197 
14198  case PT_UNARY_MINUS:
14199  switch (typ1)
14200  {
14201  case DB_TYPE_NULL:
14202  db_make_null (result); /* -NA = NA, -NULL = NULL */
14203  break;
14204 
14205  case DB_TYPE_INTEGER:
14206  if (db_get_int (arg1) == DB_INT32_MIN)
14207  {
14208  goto overflow;
14209  }
14210  else
14211  {
14212  db_make_int (result, -db_get_int (arg1));
14213  }
14214  break;
14215 
14216  case DB_TYPE_BIGINT:
14217  if (db_get_bigint (arg1) == DB_BIGINT_MIN)
14218  {
14219  goto overflow;
14220  }
14221  else
14222  {
14223  db_make_bigint (result, -db_get_bigint (arg1));
14224  }
14225  break;
14226 
14227  case DB_TYPE_SHORT:
14228  if (db_get_short (arg1) == DB_INT16_MIN)
14229  {
14230  goto overflow;
14231  }
14232  else
14233  {
14234  db_make_short (result, -db_get_short (arg1));
14235  }
14236  break;
14237 
14238  case DB_TYPE_FLOAT:
14239  db_make_float (result, -db_get_float (arg1));
14240  break;
14241 
14242  case DB_TYPE_DOUBLE:
14243  db_make_double (result, -db_get_double (arg1));
14244  break;
14245 
14246  case DB_TYPE_NUMERIC:
14247  if (numeric_db_value_negate (arg1) != NO_ERROR)
14248  {
14249  PT_ERRORc (parser, o1, er_msg ());
14250  return 0;
14251  }
14252 
14253  db_make_numeric (result, db_get_numeric (arg1), DB_VALUE_PRECISION (arg1), DB_VALUE_SCALE (arg1));
14254  break;
14255 
14256  case DB_TYPE_MONETARY:
14257  db_make_monetary (result, DB_CURRENCY_DEFAULT, -db_get_monetary (arg1)->amount);
14258  break;
14259 
14260  case DB_TYPE_CHAR:
14261  case DB_TYPE_VARCHAR:
14262  case DB_TYPE_NCHAR:
14263  case DB_TYPE_VARNCHAR:
14265  db_make_null (&tmp_val);
14266  /* force explicit cast ; scenario : INSERT INTO t VALUE(-?) , USING '10', column is INTEGER */
14267  if (tp_value_cast (arg1, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
14268  {
14270  {
14273  return 0;
14274  }
14275  else
14276  {
14277  db_make_null (result);
14278  er_clear ();
14279  return 1;
14280  }
14281  }
14282  else
14283  {
14284  pr_clear_value (arg1);
14285  *arg1 = tmp_val;
14286  db_make_double (result, -db_get_double (arg1));
14287  }
14288  break;
14289 
14290  default:
14291  return 0; /* an unhandled type is a failure */
14292  }
14293  break;
14294 
14295  case PT_IS_NULL:
14296  if (typ1 == DB_TYPE_NULL)
14297  {
14298  db_make_int (result, true);
14299  }
14300  else
14301  {
14302  db_make_int (result, false);
14303  }
14304  break;
14305 
14306  case PT_IS_NOT_NULL:
14307  if (typ1 == DB_TYPE_NULL)
14308  {
14309  db_make_int (result, false);
14310  }
14311  else
14312  {
14313  db_make_int (result, true);
14314  }
14315  break;
14316 
14317  case PT_IS:
14318  case PT_IS_NOT:
14319  {
14320  int _true, _false;
14321 
14322  _true = (op == PT_IS) ? 1 : 0;
14323  _false = 1 - _true;
14324 
14325  if ((o1 && o1->node_type != PT_VALUE) || (o2 && o2->node_type != PT_VALUE))
14326  {
14327  return 0;
14328  }
14329  if (DB_IS_NULL (arg1))
14330  {
14331  if (DB_IS_NULL (arg2))
14332  {
14333  db_make_int (result, _true);
14334  }
14335  else
14336  {
14337  db_make_int (result, _false);
14338  }
14339  }
14340  else
14341  {
14342  if (DB_IS_NULL (arg2))
14343  {
14344  db_make_int (result, _false);
14345  }
14346  else
14347  {
14348  if (db_get_int (arg1) == db_get_int (arg2))
14349  {
14350  db_make_int (result, _true);
14351  }
14352  else
14353  {
14354  db_make_int (result, _false);
14355  }
14356  }
14357  }
14358  }
14359  break;
14360 
14361  case PT_TYPEOF:
14362  if (db_typeof_dbval (result, arg1) != NO_ERROR)
14363  {
14364  db_make_null (result);
14365  }
14366  break;
14367  case PT_CONCAT_WS:
14368  if (DB_VALUE_TYPE (arg3) == DB_TYPE_NULL)
14369  {
14370  db_make_null (result);
14371  break;
14372  }
14373  /* FALLTHRU */
14374  case PT_CONCAT:
14375  if (typ1 == DB_TYPE_NULL || (typ2 == DB_TYPE_NULL && o2))
14376  {
14377  bool check_empty_string;
14378  check_empty_string = (prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false;
14379 
14380  if (!check_empty_string || !PT_IS_STRING_TYPE (rTyp))
14381  {
14382  if (op != PT_CONCAT_WS)
14383  {
14384  db_make_null (result);
14385  break;
14386  }
14387  }
14388  }
14389 
14390  /* screen out cases we don't evaluate */
14391  if (!PT_IS_STRING_TYPE (rTyp))
14392  {
14393  return 0;
14394  }
14395 
14396  switch (typ)
14397  {
14398  case DB_TYPE_CHAR:
14399  case DB_TYPE_NCHAR:
14400  case DB_TYPE_VARCHAR:
14401  case DB_TYPE_VARNCHAR:
14402  case DB_TYPE_BIT:
14403  case DB_TYPE_VARBIT:
14404  if (o2)
14405  {
14406  if (op == PT_CONCAT_WS)
14407  {
14408  if (typ1 == DB_TYPE_NULL)
14409  {
14410  if (db_value_clone (arg2, result) != NO_ERROR)
14411  {
14412  return 0;
14413  }
14414  }
14415  else if (typ2 == DB_TYPE_NULL)
14416  {
14417  if (db_value_clone (arg1, result) != NO_ERROR)
14418  {
14419  return 0;
14420  }
14421  }
14422  else
14423  {
14424  if (db_string_concatenate (arg1, arg3, &tmp_val, &truncation) < 0 || truncation != DATA_STATUS_OK)
14425  {
14426  PT_ERRORc (parser, o1, er_msg ());
14427  return 0;
14428  }
14429  if (db_string_concatenate (&tmp_val, arg2, result, &truncation) < 0
14430  || truncation != DATA_STATUS_OK)
14431  {
14432  PT_ERRORc (parser, o1, er_msg ());
14433  return 0;
14434  }
14435  }
14436  }
14437  else
14438  {
14439  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
14440  {
14441  PT_ERRORc (parser, o1, er_msg ());
14442  return 0;
14443  }
14444  }
14445  }
14446  else
14447  {
14448  if (db_value_clone (arg1, result) != NO_ERROR)
14449  {
14450  return 0;
14451  }
14452  }
14453  break;
14454 
14455  default:
14456  return 0;
14457  }
14458 
14459  break;
14460 
14461  case PT_FIELD:
14462  if (o1->node_type != PT_VALUE || (o2 && o2->node_type != PT_VALUE) || o3->node_type != PT_VALUE)
14463  {
14464  return 0;
14465  }
14466 
14467  if (DB_IS_NULL (arg3))
14468  {
14469  db_make_int (result, 0);
14470  break;
14471  }
14472 
14473  if (o3 && o3->next && o3->next->info.value.data_value.i == 1)
14474  {
14475  if (tp_value_compare (arg3, arg1, 1, 0) == DB_EQ)
14476  {
14477  db_make_int (result, 1);
14478  }
14479  else if (tp_value_compare (arg3, arg2, 1, 0) == DB_EQ)
14480  {
14481  db_make_int (result, 2);
14482  }
14483  else
14484  {
14485  db_make_int (result, 0);
14486  }
14487  }
14488  else
14489  {
14490  i = db_get_int (arg1);
14491  if (i > 0)
14492  {
14493  db_make_int (result, i);
14494  }
14495  else
14496  {
14497  if (tp_value_compare (arg3, arg2, 1, 0) == DB_EQ)
14498  {
14499  if (o3 && o3->next)
14500  {
14501  db_make_int (result, o3->next->info.value.data_value.i);
14502  }
14503  }
14504  else
14505  {
14506  db_make_int (result, 0);
14507  }
14508  }
14509  }
14510  break;
14511 
14512  case PT_LEFT:
14513  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
14514  {
14515  DB_VALUE tmp_val2;
14516  if (tp_value_coerce (arg2, &tmp_val2, &tp_Integer_domain) != DOMAIN_COMPATIBLE)
14517  {
14520  return 0;
14521  }
14522 
14523  db_make_int (&tmp_val, 0);
14524  error = db_string_substring (SUBSTRING, arg1, &tmp_val, &tmp_val2, result);
14525  if (error < 0)
14526  {
14527  PT_ERRORc (parser, o1, er_msg ());
14528  return 0;
14529  }
14530  else
14531  {
14532  return 1;
14533  }
14534  }
14535  else
14536  {
14537  db_make_null (result);
14538  return 1;
14539  }
14540 
14541  case PT_RIGHT:
14542  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
14543  {
14544  DB_VALUE tmp_val2;
14545 
14546  if (QSTR_IS_BIT (typ1))
14547  {
14548  if (db_string_bit_length (arg1, &tmp_val) != NO_ERROR)
14549  {
14550  PT_ERRORc (parser, o1, er_msg ());
14551  return 0;
14552  }
14553  }
14554  else
14555  {
14556  if (db_string_char_length (arg1, &tmp_val) != NO_ERROR)
14557  {
14558  PT_ERRORc (parser, o1, er_msg ());
14559  return 0;
14560  }
14561  }
14562  if (DB_IS_NULL (&tmp_val))
14563  {
14564  PT_ERRORc (parser, o1, er_msg ());
14565  return 0;
14566  }
14567 
14568  if (tp_value_coerce (arg2, &tmp_val2, &tp_Integer_domain) != DOMAIN_COMPATIBLE)
14569  {
14572  return 0;
14573  }
14574 
14575  /* If len, defined as second argument, is negative value, RIGHT function returns the entire string. It's same
14576  * behavior with LEFT and SUBSTRING. */
14577  if (db_get_int (&tmp_val2) < 0)
14578  {
14579  db_make_int (&tmp_val, 0);
14580  }
14581  else
14582  {
14583  db_make_int (&tmp_val, db_get_int (&tmp_val) - db_get_int (&tmp_val2) + 1);
14584  }
14585  error = db_string_substring (SUBSTRING, arg1, &tmp_val, &tmp_val2, result);
14586  if (error < 0)
14587  {
14588  PT_ERRORc (parser, o1, er_msg ());
14589  return 0;
14590  }
14591  else
14592  {
14593  return 1;
14594  }
14595  }
14596  else
14597  {
14598  db_make_null (result);
14599  return 1;
14600  }
14601 
14602  case PT_REPEAT:
14603  {
14604  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
14605  {
14606  error = db_string_repeat (arg1, arg2, result);
14607  if (error < 0)
14608  {
14609  PT_ERRORc (parser, o1, er_msg ());
14610  return 0;
14611  }
14612  else
14613  {
14614  return 1;
14615  }
14616  }
14617  else
14618  {
14619  db_make_null (result);
14620  return 1;
14621  }
14622  }
14623  /* break is not needed because of return(s) */
14624  case PT_SPACE:
14625  {
14626  if (DB_IS_NULL (arg1))
14627  {
14628  db_make_null (result);
14629  return 1;
14630  }
14631  else
14632  {
14633  error = db_string_space (arg1, result);
14634  if (error < 0)
14635  {
14636  PT_ERRORc (parser, o1, er_msg ());
14637  return 0;
14638  }
14639  else
14640  {
14641  return 1;
14642  }
14643  }
14644  }
14645  break;
14646 
14647  case PT_LOCATE:
14648  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || (o3 && DB_IS_NULL (arg3)))
14649  {
14650  db_make_null (result);
14651  }
14652  else
14653  {
14654  if (!o3)
14655  {
14656  if (db_string_position (arg1, arg2, result) != NO_ERROR)
14657  {
14658  PT_ERRORc (parser, o1, er_msg ());
14659  return 0;
14660  }
14661  }
14662  else
14663  {
14664  DB_VALUE tmp_len, tmp_arg3;
14665  int tmp = db_get_int (arg3);
14666  if (tmp < 1)
14667  {
14668  db_make_int (&tmp_arg3, 1);
14669  }
14670  else
14671  {
14672  db_make_int (&tmp_arg3, tmp);
14673  }
14674 
14675  if (db_string_char_length (arg2, &tmp_len) != NO_ERROR)
14676  {
14677  PT_ERRORc (parser, o2, er_msg ());
14678  return 0;
14679  }
14680  if (DB_IS_NULL (&tmp_len))
14681  {
14682  PT_ERRORc (parser, o2, er_msg ());
14683  return 0;
14684  }
14685 
14686  db_make_int (&tmp_len, db_get_int (&tmp_len) - db_get_int (&tmp_arg3) + 1);
14687 
14688  if (db_string_substring (SUBSTRING, arg2, &tmp_arg3, &tmp_len, &tmp_val) != NO_ERROR)
14689  {
14690  PT_ERRORc (parser, o2, er_msg ());
14691  return 0;
14692  }
14693 
14694  if (db_string_position (arg1, &tmp_val, result) != NO_ERROR)
14695  {
14696  PT_ERRORc (parser, o1, er_msg ());
14697  return 0;
14698  }
14699  if (db_get_int (result) > 0)
14700  {
14701  db_make_int (result, db_get_int (result) + db_get_int (&tmp_arg3) - 1);
14702  }
14703  }
14704  }
14705  break;
14706 
14707  case PT_MID:
14708  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || DB_IS_NULL (arg3))
14709  {
14710  db_make_null (result);
14711  }
14712  else
14713  {
14714  DB_VALUE tmp_len, tmp_arg2, tmp_arg3;
14715  int pos, len;
14716 
14717  pos = db_get_int (arg2);
14718  len = db_get_int (arg3);
14719 
14720  if (pos < 0)
14721  {
14722  if (QSTR_IS_BIT (typ1))
14723  {
14724  if (db_string_bit_length (arg1, &tmp_len) != NO_ERROR)
14725  {
14726  PT_ERRORc (parser, o1, er_msg ());
14727  return 0;
14728  }
14729  }
14730  else
14731  {
14732  if (db_string_char_length (arg1, &tmp_len) != NO_ERROR)
14733  {
14734  PT_ERRORc (parser, o1, er_msg ());
14735  return 0;
14736  }
14737  }
14738  if (DB_IS_NULL (&tmp_len))
14739  {
14740  PT_ERRORc (parser, o1, er_msg ());
14741  return 0;
14742  }
14743  pos = pos + db_get_int (&tmp_len) + 1;
14744  }
14745 
14746  if (pos < 1)
14747  {
14748  db_make_int (&tmp_arg2, 1);
14749  }
14750  else
14751  {
14752  db_make_int (&tmp_arg2, pos);
14753  }
14754 
14755  if (len < 1)
14756  {
14757  db_make_int (&tmp_arg3, 0);
14758  }
14759  else
14760  {
14761  db_make_int (&tmp_arg3, len);
14762  }
14763 
14764  error = db_string_substring (SUBSTRING, arg1, &tmp_arg2, &tmp_arg3, result);
14765  if (error < 0)
14766  {
14767  PT_ERRORc (parser, o1, er_msg ());
14768  return 0;
14769  }
14770  else
14771  {
14772  return 1;
14773  }
14774  }
14775  break;
14776 
14777  case PT_STRCMP:
14778  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2))
14779  {
14780  db_make_null (result);
14781  }
14782  else
14783  {
14784  if (db_string_compare (arg1, arg2, result) != NO_ERROR)
14785  {
14786  PT_ERRORc (parser, o1, er_msg ());
14787  return 0;
14788  }
14789 
14790  cmp = db_get_int (result);
14791  if (cmp < 0)
14792  {
14793  cmp = -1;
14794  }
14795  else if (cmp > 0)
14796  {
14797  cmp = 1;
14798  }
14799  db_make_int (result, cmp);
14800  }
14801  break;
14802 
14803  case PT_REVERSE:
14804  if (DB_IS_NULL (arg1))
14805  {
14806  db_make_null (result);
14807  }
14808  else
14809  {
14810  if (db_string_reverse (arg1, result) != NO_ERROR)
14811  {
14812  PT_ERRORc (parser, o1, er_msg ());
14813  return 0;
14814  }
14815  }
14816  break;
14817 
14818  case PT_DISK_SIZE:
14819  if (DB_IS_NULL (arg1))
14820  {
14821  db_make_int (result, 0);
14822  }
14823  else
14824  {
14825  db_make_int (result, pr_data_writeval_disk_size (arg1));
14826  /* call pr_data_writeval_disk_size function to return the size on disk */
14827  }
14828  break;
14829 
14830  case PT_BIT_COUNT:
14831  if (db_bit_count_dbval (result, arg1) != NO_ERROR)
14832  {
14833  PT_ERRORc (parser, o1, er_msg ());
14834  return 0;
14835  }
14836  break;
14837 
14838  case PT_EXISTS:
14839  if (TP_IS_SET_TYPE (typ1))
14840  {
14841  if (db_set_size (db_get_set (arg1)) > 0)
14842  {
14843  db_make_int (result, true);
14844  }
14845  else
14846  {
14847  db_make_int (result, false);
14848  }
14849  }
14850  else
14851  {
14852  db_make_int (result, true);
14853  }
14854  break;
14855 
14856  case PT_AND:
14857  if ((typ1 == DB_TYPE_NULL && typ2 == DB_TYPE_NULL) || (typ1 == DB_TYPE_NULL && db_get_int (arg2))
14858  || (typ2 == DB_TYPE_NULL && db_get_int (arg1)))
14859  {
14860  db_make_null (result);
14861  }
14862  else if (typ1 != DB_TYPE_NULL && db_get_int (arg1) && typ2 != DB_TYPE_NULL && db_get_int (arg2))
14863  {
14864  db_make_int (result, true);
14865  }
14866  else
14867  {
14868  db_make_int (result, false);
14869  }
14870  break;
14871 
14872  case PT_OR:
14873  if ((typ1 == DB_TYPE_NULL && typ2 == DB_TYPE_NULL) || (typ1 == DB_TYPE_NULL && !db_get_int (arg2))
14874  || (typ2 == DB_TYPE_NULL && !db_get_int (arg1)))
14875  {
14876  db_make_null (result);
14877  }
14878  else if (typ1 != DB_TYPE_NULL && !db_get_int (arg1) && typ2 != DB_TYPE_NULL && !db_get_int (arg2))
14879  {
14880  db_make_int (result, false);
14881  }
14882  else
14883  {
14884  db_make_int (result, true);
14885  }
14886  break;
14887 
14888  case PT_XOR:
14889  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
14890  {
14891  db_make_null (result);
14892  }
14893  else if ((!db_get_int (arg1) && !db_get_int (arg2)) || (db_get_int (arg1) && db_get_int (arg2)))
14894  {
14895  db_make_int (result, false);
14896  }
14897  else
14898  {
14899  db_make_int (result, true);
14900  }
14901  break;
14902 
14903  case PT_PLUS:
14904  case PT_MINUS:
14905  case PT_TIMES:
14906  case PT_DIVIDE:
14907  if (typ == DB_TYPE_VARIABLE && pt_is_op_hv_late_bind (op))
14908  {
14909  rTyp = pt_common_type (pt_db_to_type_enum (typ1), pt_db_to_type_enum (typ2));
14910  /* TODO: override common type for plus, minus this should be done in a more generic code : but,
14911  * pt_common_type_op is expected to return TYPE_NONE in this case */
14912  if (PT_IS_CHAR_STRING_TYPE (rTyp) && (op != PT_PLUS || prm_get_bool_value (PRM_ID_PLUS_AS_CONCAT) == false))
14913  {
14914  rTyp = PT_TYPE_DOUBLE;
14915  }
14916  typ = pt_type_enum_to_db (rTyp);
14917  domain = tp_domain_resolve_default (typ);
14918  }
14919 
14920  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
14921  {
14922  bool check_empty_string;
14923 
14924  check_empty_string = ((prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false);
14925  if (!check_empty_string || op != PT_PLUS || !PT_IS_STRING_TYPE (rTyp))
14926  {
14927  db_make_null (result); /* NULL arith_op any = NULL */
14928  break;
14929  }
14930  }
14931 
14932  /* screen out cases we don't evaluate */
14933  if (!PT_IS_NUMERIC_TYPE (rTyp) && !PT_IS_STRING_TYPE (rTyp) && rTyp != PT_TYPE_SET && rTyp != PT_TYPE_MULTISET
14934  && rTyp != PT_TYPE_SEQUENCE && !PT_IS_DATE_TIME_TYPE (rTyp))
14935  {
14936  return 0;
14937  }
14938 
14939  /* don't coerce dates and times */
14940  if (!TP_IS_DATE_OR_TIME_TYPE (typ)
14941  && !((typ == DB_TYPE_INTEGER || typ == DB_TYPE_BIGINT) && (TP_IS_DATE_OR_TIME_TYPE (typ1) && typ1 == typ2)))
14942  {
14943  /* coerce operands to data type of result */
14944  if (typ1 != typ)
14945  {
14946  db_make_null (&tmp_val);
14947  /* force explicit cast ; scenario : INSERT INTO t VALUE(''1''+?) , USING 10, column is INTEGER */
14948  if (tp_value_cast (arg1, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
14949  {
14951  parser_print_tree (parser, o1), pt_show_type_enum (rTyp));
14952 
14953  return 0;
14954  }
14955  else
14956  {
14957  pr_clear_value (arg1);
14958  *arg1 = tmp_val;
14959  }
14960  }
14961 
14962  if (typ2 != typ)
14963  {
14964  db_make_null (&tmp_val);
14965  /* force explicit cast ; scenario: INSERT INTO t VALUE(? + ''1'') , USING 10, column is INTEGER */
14966  if (tp_value_cast (arg2, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
14967  {
14969  parser_print_tree (parser, o2), pt_show_type_enum (rTyp));
14970 
14971  return 0;
14972  }
14973  else
14974  {
14975  pr_clear_value (arg2);
14976  *arg2 = tmp_val;
14977  }
14978  }
14979  }
14980 
14981  switch (op)
14982  {
14983  case PT_PLUS:
14984  switch (typ)
14985  {
14986  case DB_TYPE_SET:
14987  case DB_TYPE_MULTISET:
14988  case DB_TYPE_SEQUENCE:
14989  if (!pt_union_sets (parser, domain, arg1, arg2, result, o2))
14990  {
14991  return 0; /* set union failed */
14992  }
14993  break;
14994 
14995  case DB_TYPE_CHAR:
14996  case DB_TYPE_NCHAR:
14997  case DB_TYPE_VARCHAR:
14998  case DB_TYPE_VARNCHAR:
14999  case DB_TYPE_BIT:
15000  case DB_TYPE_VARBIT:
15001  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
15002  {
15003  PT_ERRORc (parser, o1, er_msg ());
15004  return 0;
15005  }
15006  break;
15007 
15008  case DB_TYPE_INTEGER:
15009  {
15010  int i1, i2, itmp;
15011 
15012  i1 = db_get_int (arg1);
15013  i2 = db_get_int (arg2);
15014  itmp = i1 + i2;
15015  if (OR_CHECK_ADD_OVERFLOW (i1, i2, itmp))
15016  goto overflow;
15017  else
15018  db_make_int (result, itmp);
15019  break;
15020  }
15021 
15022  case DB_TYPE_BIGINT:
15023  {
15024  DB_BIGINT bi1, bi2, bitmp;
15025 
15026  bi1 = db_get_bigint (arg1);
15027  bi2 = db_get_bigint (arg2);
15028  bitmp = bi1 + bi2;
15029  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, bitmp))
15030  goto overflow;
15031  else
15032  db_make_bigint (result, bitmp);
15033  break;
15034  }
15035 
15036  case DB_TYPE_SHORT:
15037  {
15038  short s1, s2, stmp;
15039 
15040  s1 = db_get_short (arg1);
15041  s2 = db_get_short (arg2);
15042  stmp = s1 + s2;
15043  if (OR_CHECK_ADD_OVERFLOW (s1, s2, stmp))
15044  goto overflow;
15045  else
15046  db_make_short (result, stmp);
15047  break;
15048  }
15049 
15050  case DB_TYPE_FLOAT:
15051  {
15052  float ftmp;
15053 
15054  ftmp = db_get_float (arg1) + db_get_float (arg2);
15055  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
15056  goto overflow;
15057  else
15058  db_make_float (result, ftmp);
15059  break;
15060  }
15061 
15062  case DB_TYPE_DOUBLE:
15063  {
15064  double dtmp;
15065 
15066  dtmp = db_get_double (arg1) + db_get_double (arg2);
15067  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15068  {
15069  goto overflow;
15070  }
15071  else
15072  {
15073  db_make_double (result, dtmp);
15074  }
15075  break;
15076  }
15077 
15078  case DB_TYPE_NUMERIC:
15079  if (numeric_db_value_add (arg1, arg2, result) != NO_ERROR)
15080  {
15081  PT_ERRORc (parser, o1, er_msg ());
15082  return 0;
15083  }
15084 
15085  dom_status = tp_value_coerce (result, result, domain);
15086  if (dom_status != DOMAIN_COMPATIBLE)
15087  {
15088  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
15089  return 0;
15090  }
15091  break;
15092 
15093  case DB_TYPE_MONETARY:
15094  {
15095  double dtmp;
15096 
15097  dtmp = (db_get_monetary (arg1)->amount + db_get_monetary (arg2)->amount);
15098  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15099  {
15100  goto overflow;
15101  }
15102  else
15103  {
15104  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
15105  }
15106  break;
15107  }
15108 
15109  case DB_TYPE_TIME:
15110  {
15111  DB_TIME time, result_time;
15112  int hour, minute, second;
15113  DB_BIGINT itmp;
15114  DB_VALUE *other;
15115 
15116  if (DB_VALUE_TYPE (arg1) == DB_TYPE_TIME)
15117  {
15118  time = *db_get_time (arg1);
15119  other = arg2;
15120  }
15121  else
15122  {
15123  time = *db_get_time (arg2);
15124  other = arg1;
15125  }
15126 
15127  switch (DB_VALUE_TYPE (other))
15128  {
15129  case DB_TYPE_INTEGER:
15130  itmp = db_get_int (other); /* SECONDS_OF_ONE_DAY */
15131  break;
15132  case DB_TYPE_SMALLINT:
15133  itmp = db_get_short (other); /* SECONDS_OF_ONE_DAY */
15134  break;
15135  case DB_TYPE_BIGINT:
15136  itmp = db_get_bigint (other); /* SECONDS_OF_ONE_DAY */
15137  break;
15138  default:
15139  return 0;
15140  }
15141  if (itmp < 0)
15142  {
15143  DB_TIME uother = (DB_TIME) ((-itmp) % 86400);
15144  if (time < uother)
15145  {
15146  time += 86400;
15147  }
15148  result_time = time - uother;
15149  }
15150  else
15151  {
15152  result_time = (itmp + time) % 86400;
15153  }
15154  db_time_decode (&result_time, &hour, &minute, &second);
15155  db_make_time (result, hour, minute, second);
15156  }
15157  break;
15158 
15159  case DB_TYPE_TIMESTAMP:
15160  case DB_TYPE_TIMESTAMPLTZ:
15161  {
15162  DB_UTIME *utime, result_utime;
15163  DB_VALUE *other;
15164  DB_BIGINT bi;
15165 
15167  {
15168  utime = db_get_timestamp (arg1);
15169  other = arg2;
15170  }
15171  else
15172  {
15173  utime = db_get_timestamp (arg2);
15174  other = arg1;
15175  }
15176 
15177  if (*utime == 0)
15178  {
15179  /* operation with zero date returns null */
15180  db_make_null (result);
15182  {
15184  goto error_zerodate;
15185  }
15186  break;
15187  }
15188 
15189  switch (DB_VALUE_TYPE (other))
15190  {
15191  case DB_TYPE_INTEGER:
15192  bi = db_get_int (other);
15193  break;
15194  case DB_TYPE_SMALLINT:
15195  bi = db_get_short (other);
15196  break;
15197  case DB_TYPE_BIGINT:
15198  bi = db_get_bigint (other);
15199  break;
15200  default:
15201  return 0;
15202  }
15203 
15204  if (bi < 0)
15205  {
15206  if (bi == DB_BIGINT_MIN)
15207  {
15208  if (*utime == 0)
15209  {
15210  goto overflow;
15211  }
15212  else
15213  {
15214  bi++;
15215  (*utime)--;
15216  }
15217  }
15218  if (OR_CHECK_UNS_SUB_UNDERFLOW (*utime, -bi, (*utime) + bi))
15219  {
15220  goto overflow;
15221  }
15222  result_utime = (DB_UTIME) ((*utime) + bi);
15223  }
15224  else
15225  {
15226  result_utime = (DB_UTIME) (*utime + bi);
15227  if (OR_CHECK_UNS_ADD_OVERFLOW (*utime, bi, result_utime) || INT_MAX < result_utime)
15228  {
15229  goto overflow;
15230  }
15231  }
15232 
15233  if (typ == DB_TYPE_TIMESTAMPLTZ)
15234  {
15235  db_make_timestampltz (result, result_utime);
15236  }
15237  else
15238  {
15239  db_make_timestamp (result, result_utime);
15240  }
15241  }
15242  break;
15243 
15244  case DB_TYPE_TIMESTAMPTZ:
15245  {
15246  DB_TIMESTAMPTZ *ts_tz_p, ts_tz_res, ts_tz_fixed;
15247  DB_UTIME utime, result_utime;
15248  DB_VALUE *other;
15249  DB_BIGINT bi;
15250 
15251  if (DB_VALUE_TYPE (arg1) == DB_TYPE_TIMESTAMPTZ)
15252  {
15253  ts_tz_p = db_get_timestamptz (arg1);
15254  other = arg2;
15255  }
15256  else
15257  {
15258  ts_tz_p = db_get_timestamptz (arg2);
15259  other = arg1;
15260  }
15261 
15262  utime = ts_tz_p->timestamp;
15263 
15264  if (utime == 0)
15265  {
15266  /* operation with zero date returns null */
15267  db_make_null (result);
15269  {
15271  goto error_zerodate;
15272  }
15273  break;
15274  }
15275 
15276  switch (DB_VALUE_TYPE (other))
15277  {
15278  case DB_TYPE_INTEGER:
15279  bi = db_get_int (other);
15280  break;
15281  case DB_TYPE_SMALLINT:
15282  bi = db_get_short (other);
15283  break;
15284  case DB_TYPE_BIGINT:
15285  bi = db_get_bigint (other);
15286  break;
15287  default:
15288  return 0;
15289  }
15290 
15291  if (bi < 0)
15292  {
15293  if (bi == DB_BIGINT_MIN)
15294  {
15295  if (utime == 0)
15296  {
15297  goto overflow;
15298  }
15299  else
15300  {
15301  bi++;
15302  utime--;
15303  }
15304  }
15305  if (OR_CHECK_UNS_SUB_UNDERFLOW (utime, -bi, utime + bi))
15306  {
15307  goto overflow;
15308  }
15309  result_utime = (DB_UTIME) (utime + bi);
15310  }
15311  else
15312  {
15313  result_utime = (DB_UTIME) (utime + bi);
15314  if (OR_CHECK_UNS_ADD_OVERFLOW (utime, bi, result_utime) || INT_MAX < result_utime)
15315  {
15316  goto overflow;
15317  }
15318  }
15319 
15320  ts_tz_res.timestamp = result_utime;
15321  ts_tz_res.tz_id = ts_tz_p->tz_id;
15322 
15323  if (tz_timestamptz_fix_zone (&ts_tz_res, &ts_tz_fixed) != NO_ERROR)
15324  {
15325  return 0;
15326  }
15327 
15328  db_make_timestamptz (result, &ts_tz_fixed);
15329  }
15330  break;
15331 
15332  case DB_TYPE_DATETIME:
15333  case DB_TYPE_DATETIMELTZ:
15334  {
15335  DB_DATETIME *datetime, result_datetime;
15336  DB_BIGINT bi1, bi2, result_bi, tmp_bi;
15337  DB_VALUE *other;
15338 
15340  {
15341  datetime = db_get_datetime (arg1);
15342  other = arg2;
15343  }
15344  else
15345  {
15346  datetime = db_get_datetime (arg2);
15347  other = arg1;
15348  }
15349 
15350  if (datetime->date == 0 && datetime->time == 0)
15351  {
15352  /* operation with zero date returns null */
15353  db_make_null (result);
15355  {
15357  goto error_zerodate;
15358  }
15359  break;
15360  }
15361 
15362  switch (DB_VALUE_TYPE (other))
15363  {
15364  case DB_TYPE_SMALLINT:
15365  bi2 = (DB_BIGINT) db_get_short (other);
15366  break;
15367  case DB_TYPE_INTEGER:
15368  bi2 = (DB_BIGINT) db_get_int (other);
15369  break;
15370  default:
15371  bi2 = (DB_BIGINT) db_get_bigint (other);
15372  break;
15373  }
15374 
15375  bi1 = ((DB_BIGINT) datetime->date) * MILLISECONDS_OF_ONE_DAY + datetime->time;
15376 
15377  if (bi2 < 0)
15378  {
15379  if (bi2 == DB_BIGINT_MIN)
15380  {
15381  goto overflow;
15382  }
15383  result_bi = bi1 + bi2;
15384  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
15385  {
15386  goto overflow;
15387  }
15388  }
15389  else
15390  {
15391  result_bi = bi1 + bi2;
15392  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, result_bi))
15393  {
15394  goto overflow;
15395  }
15396  }
15397 
15398  tmp_bi = (DB_BIGINT) (result_bi / MILLISECONDS_OF_ONE_DAY);
15399  if (OR_CHECK_INT_OVERFLOW (tmp_bi) || tmp_bi > DB_DATE_MAX || tmp_bi < DB_DATE_MIN)
15400  {
15401  goto overflow;
15402  }
15403  result_datetime.date = (int) tmp_bi;
15404  result_datetime.time = (int) (result_bi % MILLISECONDS_OF_ONE_DAY);
15405 
15406  if (typ == DB_TYPE_DATETIME)
15407  {
15408  db_make_datetime (result, &result_datetime);
15409  }
15410  else
15411  {
15412  db_make_datetimeltz (result, &result_datetime);
15413  }
15414  }
15415  break;
15416 
15417  case DB_TYPE_DATETIMETZ:
15418  {
15419  DB_DATETIMETZ *dt_tz_p, dt_tz_res, dt_tz_fixed;
15420  DB_DATETIME datetime;
15421  DB_BIGINT bi1, bi2, result_bi, tmp_bi;
15422  DB_VALUE *other;
15423 
15424  if (DB_VALUE_TYPE (arg1) == DB_TYPE_DATETIMETZ)
15425  {
15426  dt_tz_p = db_get_datetimetz (arg1);
15427  other = arg2;
15428  }
15429  else
15430  {
15431  dt_tz_p = db_get_datetimetz (arg2);
15432  other = arg1;
15433  }
15434 
15435  datetime = dt_tz_p->datetime;
15436 
15437  if (datetime.date == 0 && datetime.time == 0)
15438  {
15439  /* operation with zero date returns null */
15440  db_make_null (result);
15442  {
15444  goto error_zerodate;
15445  }
15446  break;
15447  }
15448 
15449  switch (DB_VALUE_TYPE (other))
15450  {
15451  case DB_TYPE_SMALLINT:
15452  bi2 = (DB_BIGINT) db_get_short (other);
15453  break;
15454  case DB_TYPE_INTEGER:
15455  bi2 = (DB_BIGINT) db_get_int (other);
15456  break;
15457  default:
15458  bi2 = (DB_BIGINT) db_get_bigint (other);
15459  break;
15460  }
15461 
15462  bi1 = ((DB_BIGINT) datetime.date) * MILLISECONDS_OF_ONE_DAY + datetime.time;
15463 
15464  if (bi2 < 0)
15465  {
15466  if (bi2 == DB_BIGINT_MIN)
15467  {
15468  goto overflow;
15469  }
15470  result_bi = bi1 + bi2;
15471  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
15472  {
15473  goto overflow;
15474  }
15475  }
15476  else
15477  {
15478  result_bi = bi1 + bi2;
15479  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, result_bi))
15480  {
15481  goto overflow;
15482  }
15483  }
15484 
15485  tmp_bi = (DB_BIGINT) (result_bi / MILLISECONDS_OF_ONE_DAY);
15486  if (OR_CHECK_INT_OVERFLOW (tmp_bi) || tmp_bi > DB_DATE_MAX || tmp_bi < DB_DATE_MIN)
15487  {
15488  goto overflow;
15489  }
15490  dt_tz_res.datetime.date = (int) tmp_bi;
15491  dt_tz_res.datetime.time = (int) (result_bi % MILLISECONDS_OF_ONE_DAY);
15492  dt_tz_res.tz_id = dt_tz_p->tz_id;
15493 
15494  if (tz_datetimetz_fix_zone (&dt_tz_res, &dt_tz_fixed) != NO_ERROR)
15495  {
15496  return 0;
15497  }
15498 
15499  db_make_datetimetz (result, &dt_tz_fixed);
15500  }
15501  break;
15502 
15503  case DB_TYPE_DATE:
15504  {
15505  DB_DATE *date, result_date;
15506  DB_VALUE *other;
15507  DB_BIGINT bi;
15508 
15509  if (DB_VALUE_TYPE (arg1) == DB_TYPE_DATE)
15510  {
15511  date = db_get_date (arg1);
15512  other = arg2;
15513  }
15514  else
15515  {
15516  date = db_get_date (arg2);
15517  other = arg1;
15518  }
15519 
15520  if (*date == 0)
15521  {
15522  /* operation with zero date returns null */
15523  db_make_null (result);
15525  {
15527  goto error_zerodate;
15528  }
15529  break;
15530  }
15531 
15532  switch (DB_VALUE_TYPE (other))
15533  {
15534  case DB_TYPE_INTEGER:
15535  bi = db_get_int (other);
15536  break;
15537  case DB_TYPE_SMALLINT:
15538  bi = db_get_short (other);
15539  break;
15540  case DB_TYPE_BIGINT:
15541  bi = db_get_bigint (other);
15542  break;
15543  default:
15544  return 0;
15545  }
15546  if (bi < 0)
15547  {
15548  if (bi == DB_BIGINT_MIN)
15549  {
15550  if (*date == 0)
15551  {
15552  goto overflow;
15553  }
15554  bi++;
15555  (*date)--;
15556  }
15557  if (OR_CHECK_UNS_SUB_UNDERFLOW (*date, -bi, (*date) + bi) || (*date) + bi < DB_DATE_MIN)
15558  {
15559  goto overflow;
15560  }
15561  result_date = (DB_DATE) ((*date) + bi);
15562  }
15563  else
15564  {
15565  result_date = (DB_DATE) (*date + bi);
15566  if (OR_CHECK_UNS_ADD_OVERFLOW (*date, bi, result_date) || result_date > DB_DATE_MAX)
15567  {
15568  goto overflow;
15569  }
15570  }
15571 
15572  db_value_put_encoded_date (result, &result_date);
15573  }
15574  break;
15575  default:
15576  return 0;
15577  }
15578  break;
15579 
15580  case PT_MINUS:
15581  switch (typ)
15582  {
15583  case DB_TYPE_SET:
15584  case DB_TYPE_MULTISET:
15585  if (!pt_difference_sets (parser, domain, arg1, arg2, result, o2))
15586  {
15587  return 0; /* set union failed */
15588  }
15589  break;
15590 
15591  case DB_TYPE_INTEGER:
15592  {
15593  int i1, i2, itmp;
15594 
15595  i1 = db_get_int (arg1);
15596  i2 = db_get_int (arg2);
15597  itmp = i1 - i2;
15598  if (OR_CHECK_SUB_UNDERFLOW (i1, i2, itmp))
15599  goto overflow;
15600  else
15601  db_make_int (result, itmp);
15602  break;
15603  }
15604 
15605  case DB_TYPE_BIGINT:
15606  {
15607  DB_BIGINT bi1, bi2, result_bi;
15608 
15609  bi1 = bi2 = 0;
15610  if (typ1 != typ2)
15611  {
15612  assert (false);
15613 
15614  db_make_null (result);
15615  break;
15616  }
15617 
15618  if (typ1 == DB_TYPE_DATETIME || typ1 == DB_TYPE_DATETIMELTZ)
15619  {
15620  DB_DATETIME *dt1, *dt2;
15621 
15622  dt1 = db_get_datetime (arg1);
15623  dt2 = db_get_datetime (arg2);
15624 
15625  bi1 = (((DB_BIGINT) dt1->date) * MILLISECONDS_OF_ONE_DAY + dt1->time);
15626  bi2 = (((DB_BIGINT) dt2->date) * MILLISECONDS_OF_ONE_DAY + dt2->time);
15627  }
15628  else if (typ1 == DB_TYPE_DATETIMETZ)
15629  {
15630  DB_DATETIMETZ *dt_tz1, *dt_tz2;
15631 
15632  dt_tz1 = db_get_datetimetz (arg1);
15633  dt_tz2 = db_get_datetimetz (arg2);
15634 
15635  bi1 = (((DB_BIGINT) dt_tz1->datetime.date) * MILLISECONDS_OF_ONE_DAY + dt_tz1->datetime.time);
15636  bi2 = (((DB_BIGINT) dt_tz2->datetime.date) * MILLISECONDS_OF_ONE_DAY + dt_tz2->datetime.time);
15637  }
15638  else if (typ1 == DB_TYPE_DATE)
15639  {
15640  DB_DATE *d1, *d2;
15641 
15642  d1 = db_get_date (arg1);
15643  d2 = db_get_date (arg2);
15644 
15645  bi1 = (DB_BIGINT) (*d1);
15646  bi2 = (DB_BIGINT) (*d2);
15647  }
15648  else if (typ1 == DB_TYPE_TIME)
15649  {
15650  DB_TIME *t1, *t2;
15651 
15652  t1 = db_get_time (arg1);
15653  t2 = db_get_time (arg2);
15654 
15655  bi1 = (DB_BIGINT) (*t1);
15656  bi2 = (DB_BIGINT) (*t2);
15657  }
15658  else if (typ1 == DB_TYPE_TIMESTAMP || typ1 == DB_TYPE_TIMESTAMPLTZ)
15659  {
15660  DB_TIMESTAMP *ts1, *ts2;
15661 
15662  ts1 = db_get_timestamp (arg1);
15663  ts2 = db_get_timestamp (arg2);
15664 
15665  bi1 = (DB_BIGINT) (*ts1);
15666  bi2 = (DB_BIGINT) (*ts2);
15667  }
15668  else if (typ1 == DB_TYPE_TIMESTAMPTZ)
15669  {
15670  DB_TIMESTAMPTZ *ts_tz1, *ts_tz2;
15671 
15672  ts_tz1 = db_get_timestamptz (arg1);
15673  ts_tz2 = db_get_timestamptz (arg2);
15674 
15675  bi1 = (DB_BIGINT) (ts_tz1->timestamp);
15676  bi2 = (DB_BIGINT) (ts_tz2->timestamp);
15677  }
15678  else if (typ1 == DB_TYPE_BIGINT)
15679  {
15680  bi1 = db_get_bigint (arg1);
15681  bi2 = db_get_bigint (arg2);
15682  }
15683  else
15684  {
15685  assert (false);
15686 
15687  db_make_null (result);
15688  break;
15689  }
15690 
15691  if ((TP_IS_DATE_TYPE (typ1) && bi1 == 0) || (TP_IS_DATE_TYPE (typ2) && bi2 == 0))
15692  {
15693  /* operation with zero date returns null */
15694  db_make_null (result);
15696  {
15698  goto error_zerodate;
15699  }
15700  break;
15701  }
15702 
15703  result_bi = bi1 - bi2;
15704  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
15705  {
15706  goto overflow;
15707  }
15708  else
15709  {
15710  db_make_bigint (result, result_bi);
15711  }
15712  break;
15713  }
15714 
15715  case DB_TYPE_SHORT:
15716  {
15717  short s1, s2, stmp;
15718 
15719  s1 = db_get_short (arg1);
15720  s2 = db_get_short (arg2);
15721  stmp = s1 - s2;
15722  if (OR_CHECK_SUB_UNDERFLOW (s1, s2, stmp))
15723  goto overflow;
15724  else
15725  db_make_short (result, stmp);
15726  break;
15727  }
15728 
15729  case DB_TYPE_FLOAT:
15730  {
15731  float ftmp;
15732 
15733  ftmp = db_get_float (arg1) - db_get_float (arg2);
15734  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
15735  goto overflow;
15736  else
15737  db_make_float (result, ftmp);
15738  break;
15739  }
15740 
15741  case DB_TYPE_DOUBLE:
15742  {
15743  double dtmp;
15744 
15745  dtmp = db_get_double (arg1) - db_get_double (arg2);
15746  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15747  {
15748  goto overflow;
15749  }
15750  else
15751  {
15752  db_make_double (result, dtmp);
15753  }
15754  break;
15755  }
15756 
15757  case DB_TYPE_NUMERIC:
15758  if (numeric_db_value_sub (arg1, arg2, result) != NO_ERROR)
15759  {
15760  PT_ERRORc (parser, o1, er_msg ());
15761  return 0;
15762  }
15763  dom_status = tp_value_coerce (result, result, domain);
15764  if (dom_status != DOMAIN_COMPATIBLE)
15765  {
15766  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
15767  return 0;
15768  }
15769  break;
15770 
15771  case DB_TYPE_MONETARY:
15772  {
15773  double dtmp;
15774 
15775  dtmp = db_get_monetary (arg1)->amount - db_get_monetary (arg2)->amount;
15776  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15777  {
15778  goto overflow;
15779  }
15780  else
15781  {
15782  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
15783  }
15784  break;
15785  }
15786 
15787  case DB_TYPE_TIME:
15788  {
15789  DB_TIME time, result_time;
15790  int hour, minute, second;
15791  DB_BIGINT bi = 0, ubi = 0;
15792 
15793  switch (DB_VALUE_TYPE (arg2))
15794  {
15795  case DB_TYPE_SHORT:
15796  bi = db_get_short (arg2);
15797  break;
15798  case DB_TYPE_INTEGER:
15799  bi = db_get_int (arg2);
15800  break;
15801  case DB_TYPE_BIGINT:
15802  bi = db_get_bigint (arg2);
15803  break;
15804  default:
15805  assert (false);
15806  break;
15807  }
15808  ubi = (bi < 0) ? -bi : bi;
15809 
15810  assert (typ == DB_VALUE_TYPE (arg1));
15811 
15812  time = *db_get_time (arg1);
15813 
15814  if (time < (DB_TIME) (ubi % 86400))
15815  {
15816  time += 86400;
15817  }
15818  result_time = time - (bi % 86400);
15819 
15820  db_time_decode (&result_time, &hour, &minute, &second);
15821  db_make_time (result, hour, minute, second);
15822  }
15823  break;
15824 
15825  case DB_TYPE_TIMESTAMP:
15826  case DB_TYPE_TIMESTAMPLTZ:
15827  case DB_TYPE_TIMESTAMPTZ:
15828  {
15829  DB_UTIME utime, result_utime;
15830  DB_TIMESTAMPTZ ts_tz, ts_tz_fixed;
15831  DB_BIGINT bi = 0;
15832 
15833  switch (DB_VALUE_TYPE (arg2))
15834  {
15835  case DB_TYPE_SHORT:
15836  bi = db_get_short (arg2);
15837  break;
15838  case DB_TYPE_INTEGER:
15839  bi = db_get_int (arg2);
15840  break;
15841  case DB_TYPE_BIGINT:
15842  bi = db_get_bigint (arg2);
15843  break;
15844  default:
15845  assert (false);
15846  break;
15847  }
15848 
15849  assert (typ == DB_VALUE_TYPE (arg1));
15850  if (typ == DB_TYPE_TIMESTAMPTZ)
15851  {
15852  ts_tz = *db_get_timestamptz (arg1);
15853  utime = ts_tz.timestamp;
15854  }
15855  else
15856  {
15857  utime = *db_get_timestamp (arg1);
15858  }
15859 
15860  if (utime == 0)
15861  {
15862  /* operation with zero date returns null */
15863  db_make_null (result);
15865  {
15867  goto error_zerodate;
15868  }
15869  break;
15870  }
15871 
15872  if (bi < 0)
15873  {
15874  /* we're adding */
15875  result_utime = (DB_UTIME) (utime - bi);
15876  if (OR_CHECK_UNS_ADD_OVERFLOW (utime, -bi, result_utime) || INT_MAX < result_utime)
15877  {
15878  goto overflow;
15879  }
15880  }
15881  else
15882  {
15883  result_utime = (DB_UTIME) (utime - bi);
15884  if (OR_CHECK_UNS_SUB_UNDERFLOW (utime, bi, result_utime))
15885  {
15887  return 0;
15888  }
15889  }
15890  if (typ == DB_TYPE_TIMESTAMPTZ)
15891  {
15892  ts_tz.timestamp = result_utime;
15893  if (tz_timestamptz_fix_zone (&ts_tz, &ts_tz_fixed) != NO_ERROR)
15894  {
15895  return 0;
15896  }
15897  db_make_timestamptz (result, &ts_tz_fixed);
15898  }
15899  else if (typ == DB_TYPE_TIMESTAMPLTZ)
15900  {
15901  db_make_timestampltz (result, result_utime);
15902  }
15903  else
15904  {
15905  db_make_timestamp (result, result_utime);
15906  }
15907  }
15908  break;
15909 
15910  case DB_TYPE_DATETIME:
15911  case DB_TYPE_DATETIMELTZ:
15912  case DB_TYPE_DATETIMETZ:
15913  {
15914  DB_DATETIME datetime, result_datetime;
15915  DB_DATETIMETZ dt_tz, dt_tz_fixed;
15916  DB_BIGINT bi = 0;
15917 
15918  switch (DB_VALUE_TYPE (arg2))
15919  {
15920  case DB_TYPE_SHORT:
15921  bi = db_get_short (arg2);
15922  break;
15923  case DB_TYPE_INTEGER:
15924  bi = db_get_int (arg2);
15925  break;
15926  case DB_TYPE_BIGINT:
15927  bi = db_get_bigint (arg2);
15928  break;
15929  default:
15930  assert (false);
15931  break;
15932  }
15933 
15934  if (typ == DB_TYPE_DATETIMETZ)
15935  {
15936  dt_tz = *db_get_datetimetz (arg1);
15937  datetime = dt_tz.datetime;
15938  }
15939  else
15940  {
15941  datetime = *db_get_datetime (arg1);
15942  }
15943 
15944  if (datetime.date == 0 && datetime.time == 0)
15945  {
15946  /* operation with zero date returns null */
15947  db_make_null (result);
15949  {
15951  goto error_zerodate;
15952  }
15953  break;
15954  }
15955 
15956  error = db_subtract_int_from_datetime (&datetime, bi, &result_datetime);
15957  if (error != NO_ERROR)
15958  {
15960  return 0;
15961  }
15962 
15963  if (typ == DB_TYPE_DATETIMETZ)
15964  {
15965  dt_tz.datetime = result_datetime;
15966  if (tz_datetimetz_fix_zone (&dt_tz, &dt_tz_fixed) != NO_ERROR)
15967  {
15968  return 0;
15969  }
15970  db_make_datetimetz (result, &dt_tz_fixed);
15971  }
15972  else if (typ == DB_TYPE_DATETIMELTZ)
15973  {
15974  db_make_datetimeltz (result, &result_datetime);
15975  }
15976  else
15977  {
15978  db_make_datetime (result, &result_datetime);
15979  }
15980  }
15981  break;
15982 
15983  case DB_TYPE_DATE:
15984  {
15985  DB_DATE *date, result_date;
15986  int month, day, year;
15987  DB_BIGINT bi = 0;
15988 
15989  switch (DB_VALUE_TYPE (arg2))
15990  {
15991  case DB_TYPE_SHORT:
15992  bi = db_get_short (arg2);
15993  break;
15994  case DB_TYPE_INTEGER:
15995  bi = db_get_int (arg2);
15996  break;
15997  case DB_TYPE_BIGINT:
15998  bi = db_get_bigint (arg2);
15999  break;
16000  default:
16001  assert (false);
16002  break;
16003  }
16004  date = db_get_date (arg1);
16005 
16006  if (*date == 0)
16007  {
16008  /* operation with zero date returns null */
16009  db_make_null (result);
16011  {
16013  goto error_zerodate;
16014  }
16015  break;
16016  }
16017 
16018  if (bi < 0)
16019  {
16020  /* we're adding */
16021  result_date = (DB_DATE) (*date - bi);
16022  if (OR_CHECK_UNS_ADD_OVERFLOW (*date, -bi, result_date) || result_date > DB_DATE_MAX)
16023  {
16024  goto overflow;
16025  }
16026  }
16027  else
16028  {
16029  result_date = *date - (DB_DATE) bi;
16030  if (OR_CHECK_UNS_SUB_UNDERFLOW (*date, bi, result_date) || result_date < DB_DATE_MIN)
16031  {
16033  return 0;
16034  }
16035  }
16036  db_date_decode (&result_date, &month, &day, &year);
16037  db_make_date (result, month, day, year);
16038  }
16039  break;
16040 
16041  default:
16042  return 0;
16043  }
16044  break;
16045 
16046  case PT_TIMES:
16047  switch (typ)
16048  {
16049  case DB_TYPE_SET:
16050  case DB_TYPE_MULTISET:
16051  case DB_TYPE_SEQUENCE:
16052  if (!pt_product_sets (parser, domain, arg1, arg2, result, o2))
16053  {
16054  return 0; /* set union failed */
16055  }
16056  break;
16057 
16058  case DB_TYPE_INTEGER:
16059  {
16060  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16061  * multiplication.
16062  */
16063  volatile int i1, i2, itmp;
16064 
16065  i1 = db_get_int (arg1);
16066  i2 = db_get_int (arg2);
16067  itmp = i1 * i2;
16068  if (OR_CHECK_MULT_OVERFLOW (i1, i2, itmp))
16069  {
16070  goto overflow;
16071  }
16072  else
16073  {
16074  db_make_int (result, itmp);
16075  }
16076  break;
16077  }
16078 
16079  case DB_TYPE_BIGINT:
16080  {
16081  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16082  * multiplication.
16083  */
16084  volatile DB_BIGINT bi1, bi2, bitmp;
16085 
16086  bi1 = db_get_bigint (arg1);
16087  bi2 = db_get_bigint (arg2);
16088  bitmp = bi1 * bi2;
16089  if (OR_CHECK_MULT_OVERFLOW (bi1, bi2, bitmp))
16090  {
16091  goto overflow;
16092  }
16093  else
16094  {
16095  db_make_bigint (result, bitmp);
16096  }
16097  break;
16098  }
16099 
16100  case DB_TYPE_SHORT:
16101  {
16102  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16103  * multiplication.
16104  */
16105  volatile short s1, s2, stmp;
16106 
16107  s1 = db_get_short (arg1);
16108  s2 = db_get_short (arg2);
16109  stmp = s1 * s2;
16110  if (OR_CHECK_MULT_OVERFLOW (s1, s2, stmp))
16111  {
16112  goto overflow;
16113  }
16114  else
16115  {
16116  db_make_short (result, stmp);
16117  }
16118  break;
16119  }
16120 
16121  case DB_TYPE_FLOAT:
16122  {
16123  float ftmp;
16124 
16125  ftmp = db_get_float (arg1) * db_get_float (arg2);
16126  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
16127  {
16128  goto overflow;
16129  }
16130  else
16131  {
16132  db_make_float (result, ftmp);
16133  }
16134  break;
16135  }
16136 
16137  case DB_TYPE_DOUBLE:
16138  {
16139  double dtmp;
16140 
16141  dtmp = db_get_double (arg1) * db_get_double (arg2);
16142  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16143  {
16144  goto overflow;
16145  }
16146  else
16147  {
16148  db_make_double (result, dtmp);
16149  }
16150  break;
16151  }
16152 
16153  case DB_TYPE_NUMERIC:
16154  error = numeric_db_value_mul (arg1, arg2, result);
16155  if (error == ER_IT_DATA_OVERFLOW)
16156  {
16157  goto overflow;
16158  }
16159  else if (error != NO_ERROR)
16160  {
16161  PT_ERRORc (parser, o1, er_msg ());
16162  return 0;
16163  }
16164  dom_status = tp_value_coerce (result, result, domain);
16165  if (dom_status != DOMAIN_COMPATIBLE)
16166  {
16167  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
16168  return 0;
16169  }
16170  break;
16171 
16172  case DB_TYPE_MONETARY:
16173  {
16174  double dtmp;
16175 
16176  dtmp = db_get_monetary (arg1)->amount * db_get_monetary (arg2)->amount;
16177  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16178  {
16179  goto overflow;
16180  }
16181  else
16182  {
16183  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
16184  }
16185  break;
16186  }
16187  break;
16188 
16189  default:
16190  return 0;
16191  }
16192  break;
16193 
16194  case PT_DIVIDE:
16195  switch (typ)
16196  {
16197  case DB_TYPE_SHORT:
16198  if (db_get_short (arg2) != 0)
16199  {
16200  db_make_short (result, db_get_short (arg1) / db_get_short (arg2));
16201  return 1;
16202  }
16203  break;
16204 
16205  case DB_TYPE_INTEGER:
16206  if (db_get_int (arg2) != 0)
16207  {
16208  db_make_int (result, (db_get_int (arg1) / db_get_int (arg2)));
16209  return 1;
16210  }
16211  break;
16212  case DB_TYPE_BIGINT:
16213  if (db_get_bigint (arg2) != 0)
16214  {
16215  db_make_bigint (result, (db_get_bigint (arg1) / db_get_bigint (arg2)));
16216  return 1;
16217  }
16218  break;
16219  case DB_TYPE_FLOAT:
16220  if (fabs (db_get_float (arg2)) > FLT_EPSILON)
16221  {
16222  float ftmp;
16223 
16224  ftmp = db_get_float (arg1) / db_get_float (arg2);
16225  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
16226  {
16227  goto overflow;
16228  }
16229  else
16230  {
16231  db_make_float (result, ftmp);
16232  return 1;
16233  }
16234  }
16235  break;
16236 
16237  case DB_TYPE_DOUBLE:
16238  if (fabs (db_get_double (arg2)) > DBL_EPSILON)
16239  {
16240  double dtmp;
16241 
16242  dtmp = db_get_double (arg1) / db_get_double (arg2);
16243  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16244  {
16245  goto overflow;
16246  }
16247  else
16248  {
16249  db_make_double (result, dtmp);
16250  return 1; /* success */
16251  }
16252  }
16253  break;
16254 
16255  case DB_TYPE_NUMERIC:
16256  if (!numeric_db_value_is_zero (arg2))
16257  {
16258  error = numeric_db_value_div (arg1, arg2, result);
16259  if (error == ER_IT_DATA_OVERFLOW)
16260  {
16261  goto overflow;
16262  }
16263  else if (error != NO_ERROR)
16264  {
16265  PT_ERRORc (parser, o1, er_msg ());
16266  return 0;
16267  }
16268 
16269  dom_status = tp_value_coerce (result, result, domain);
16270  if (dom_status != DOMAIN_COMPATIBLE)
16271  {
16272  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
16273  return 0;
16274  }
16275 
16276  return 1;
16277  }
16278  break;
16279 
16280  case DB_TYPE_MONETARY:
16281  if (fabs (db_get_monetary (arg2)->amount) > DBL_EPSILON)
16282  {
16283  double dtmp;
16284 
16285  dtmp = db_get_monetary (arg1)->amount / db_get_monetary (arg2)->amount;
16286  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16287  {
16288  goto overflow;
16289  }
16290  else
16291  {
16292  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
16293  return 1; /* success */
16294  }
16295  }
16296  break;
16297 
16298  default:
16299  return 0;
16300  }
16301 
16303  return 0;
16304 
16305  default:
16306  return 0;
16307  }
16308  break;
16309 
16310  case PT_STRCAT:
16311  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
16312  {
16313  bool check_empty_string;
16314 
16315  check_empty_string = ((prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false);
16316 
16317  if (!check_empty_string || !PT_IS_STRING_TYPE (rTyp))
16318  {
16319  db_make_null (result); /* NULL arith_op any = NULL */
16320  break;
16321  }
16322  }
16323 
16324  /* screen out cases we don't evaluate */
16325  if (!PT_IS_STRING_TYPE (rTyp))
16326  {
16327  return 0;
16328  }
16329 
16330  switch (typ)
16331  {
16332  case DB_TYPE_CHAR:
16333  case DB_TYPE_NCHAR:
16334  case DB_TYPE_VARCHAR:
16335  case DB_TYPE_VARNCHAR:
16336  case DB_TYPE_BIT:
16337  case DB_TYPE_VARBIT:
16338  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
16339  {
16340  PT_ERRORc (parser, o1, er_msg ());
16341  return 0;
16342  }
16343  break;
16344 
16345  default:
16346  return 0;
16347  }
16348  break;
16349 
16350  case PT_MODULUS:
16351  error = db_mod_dbval (result, arg1, arg2);
16352  if (error != NO_ERROR)
16353  {
16354  PT_ERRORc (parser, o1, er_msg ());
16355  return 0;
16356  }
16357  break;
16358 
16359  case PT_PI:
16360  db_make_double (result, 3.14159265358979323846264338);
16361  break;
16362 
16363  case PT_RAND:
16364  /* rand() and drand() should always generate the same value during a statement. To support it, we add lrand and
16365  * drand member to PARSER_CONTEXT. */
16366  if (DB_IS_NULL (arg1))
16367  {
16368  db_make_int (result, parser->lrand);
16369  }
16370  else
16371  {
16372  srand48 (db_get_int (arg1));
16373  db_make_int (result, lrand48 ());
16374  }
16375  break;
16376 
16377  case PT_DRAND:
16378  if (DB_IS_NULL (arg1))
16379  {
16380  db_make_double (result, parser->drand);
16381  }
16382  else
16383  {
16384  srand48 (db_get_int (arg1));
16385  db_make_double (result, drand48 ());
16386  }
16387  break;
16388 
16389  case PT_RANDOM:
16390  /* Generate seed internally if there is no seed given as argument. rand() on select list gets a random value by
16391  * fetch_peek_arith(). But, if rand() is specified on VALUES clause of insert statement, it gets a random value
16392  * by the following codes. In this case, DB_VALUE(arg1) of NULL type is passed. */
16393  if (DB_IS_NULL (arg1))
16394  {
16395  struct timeval t;
16396  gettimeofday (&t, NULL);
16397  srand48 ((long) (t.tv_usec + lrand48 ()));
16398  }
16399  else
16400  {
16401  srand48 (db_get_int (arg1));
16402  }
16403  db_make_int (result, lrand48 ());
16404  break;
16405 
16406  case PT_DRANDOM:
16407  if (DB_IS_NULL (arg1))
16408  {
16409  struct timeval t;
16410  gettimeofday (&t, NULL);
16411  srand48 ((long) (t.tv_usec + lrand48 ()));
16412  }
16413  else
16414  {
16415  srand48 (db_get_int (arg1));
16416  }
16417  db_make_double (result, drand48 ());
16418  break;
16419 
16420  case PT_FLOOR:
16421  error = db_floor_dbval (result, arg1);
16422  if (error != NO_ERROR)
16423  {
16424  PT_ERRORc (parser, o1, er_msg ());
16425  return 0;
16426  }
16427  break;
16428 
16429  case PT_CEIL:
16430  error = db_ceil_dbval (result, arg1);
16431  if (error != NO_ERROR)
16432  {
16433  PT_ERRORc (parser, o1, er_msg ());
16434  return 0;
16435  }
16436  break;
16437 
16438  case PT_SIGN:
16439  error = db_sign_dbval (result, arg1);
16440  if (error != NO_ERROR)
16441  {
16442  PT_ERRORc (parser, o1, er_msg ());
16443  return 0;
16444  }
16445  break;
16446 
16447  case PT_ABS:
16448  error = db_abs_dbval (result, arg1);
16449  if (error != NO_ERROR)
16450  {
16451  PT_ERRORc (parser, o1, er_msg ());
16452  return 0;
16453  }
16454  break;
16455  case PT_POWER:
16456  error = db_power_dbval (result, arg1, arg2);
16457  if (error != NO_ERROR)
16458  {
16459  PT_ERRORc (parser, o1, er_msg ());
16460  return 0;
16461  }
16462  break;
16463 
16464  case PT_ROUND:
16465  error = db_round_dbval (result, arg1, arg2);
16466  if (error != NO_ERROR)
16467  {
16468  PT_ERRORc (parser, o1, er_msg ());
16469  return 0;
16470  }
16471  break;
16472 
16473  case PT_LOG:
16474  error = db_log_dbval (result, arg1, arg2);
16475  if (error != NO_ERROR)
16476  {
16477  PT_ERRORc (parser, o1, er_msg ());
16478  return 0;
16479  }
16480  break;
16481 
16482  case PT_EXP:
16483  error = db_exp_dbval (result, arg1);
16484  if (error != NO_ERROR)
16485  {
16486  PT_ERRORc (parser, o1, er_msg ());
16487  return 0;
16488  }
16489  break;
16490 
16491  case PT_SQRT:
16492  error = db_sqrt_dbval (result, arg1);
16493  if (error != NO_ERROR)
16494  {
16495  PT_ERRORc (parser, o1, er_msg ());
16496  return 0;
16497  }
16498  break;
16499 
16500  case PT_SIN:
16501  error = db_sin_dbval (result, arg1);
16502  if (error != NO_ERROR)
16503  {
16504  PT_ERRORc (parser, o1, er_msg ());
16505  return 0;
16506  }
16507  break;
16508 
16509  case PT_COS:
16510  error = db_cos_dbval (result, arg1);
16511  if (error != NO_ERROR)
16512  {
16513  PT_ERRORc (parser, o1, er_msg ());
16514  return 0;
16515  }
16516  break;
16517 
16518  case PT_TAN:
16519  error = db_tan_dbval (result, arg1);
16520  if (error != NO_ERROR)
16521  {
16522  PT_ERRORc (parser, o1, er_msg ());
16523  return 0;
16524  }
16525  break;
16526 
16527  case PT_COT:
16528  error = db_cot_dbval (result, arg1);
16529  if (error != NO_ERROR)
16530  {
16531  PT_ERRORc (parser, o1, er_msg ());
16532  return 0;
16533  }
16534  break;
16535 
16536  case PT_ACOS:
16537  error = db_acos_dbval (result, arg1);
16538  if (error != NO_ERROR)
16539  {
16540  PT_ERRORc (parser, o1, er_msg ());
16541  return 0;
16542  }
16543  break;
16544 
16545  case PT_ASIN:
16546  error = db_asin_dbval (result, arg1);
16547  if (error != NO_ERROR)
16548  {
16549  PT_ERRORc (parser, o1, er_msg ());
16550  return 0;
16551  }
16552  break;
16553 
16554  case PT_ATAN:
16555  error = db_atan_dbval (result, arg1);
16556  if (error != NO_ERROR)
16557  {
16558  PT_ERRORc (parser, o1, er_msg ());
16559  return 0;
16560  }
16561  break;
16562 
16563  case PT_ATAN2:
16564  error = db_atan2_dbval (result, arg1, arg2);
16565  if (error != NO_ERROR)
16566  {
16567  PT_ERRORc (parser, o1, er_msg ());
16568  return 0;
16569  }
16570  break;
16571 
16572  case PT_DEGREES:
16573  error = db_degrees_dbval (result, arg1);
16574  if (error != NO_ERROR)
16575  {
16576  PT_ERRORc (parser, o1, er_msg ());
16577  return 0;
16578  }
16579  break;
16580 
16581  case PT_DATEF:
16582  error = db_date_dbval (result, arg1, NULL);
16583  if (error != NO_ERROR)
16584  {
16585  PT_ERRORc (parser, o1, er_msg ());
16586  return 0;
16587  }
16588  break;
16589 
16590  case PT_TIMEF:
16591  error = db_time_dbval (result, arg1, NULL);
16592  if (error != NO_ERROR)
16593  {
16594  PT_ERRORc (parser, o1, er_msg ());
16595  return 0;
16596  }
16597  break;
16598 
16599  case PT_RADIANS:
16600  error = db_radians_dbval (result, arg1);
16601  if (error != NO_ERROR)
16602  {
16603  PT_ERRORc (parser, o1, er_msg ());
16604  return 0;
16605  }
16606  break;
16607 
16608  case PT_LN:
16609  error = db_log_generic_dbval (result, arg1, -1 /* e convention */ );
16610  if (error != NO_ERROR)
16611  {
16612  PT_ERRORc (parser, o1, er_msg ());
16613  return 0;
16614  }
16615  break;
16616 
16617  case PT_LOG2:
16618  error = db_log_generic_dbval (result, arg1, 2);
16619  if (error != NO_ERROR)
16620  {
16621  PT_ERRORc (parser, o1, er_msg ());
16622  return 0;
16623  }
16624  break;
16625 
16626  case PT_LOG10:
16627  error = db_log_generic_dbval (result, arg1, 10);
16628  if (error != NO_ERROR)
16629  {
16630  PT_ERRORc (parser, o1, er_msg ());
16631  return 0;
16632  }
16633  break;
16634 
16635  case PT_TRUNC:
16636  error = db_trunc_dbval (result, arg1, arg2);
16637  if (error != NO_ERROR)
16638  {
16639  PT_ERRORc (parser, o1, er_msg ());
16640  return 0;
16641  }
16642  break;
16643 
16644  case PT_CHR:
16645  error = db_string_chr (result, arg1, arg2);
16646  if (error != NO_ERROR)
16647  {
16648  PT_ERRORc (parser, o1, er_msg ());
16649  return 0;
16650  }
16651  break;
16652 
16653  case PT_INSTR:
16654  error = db_string_instr (arg1, arg2, arg3, result);
16655  if (error < 0)
16656  {
16657  PT_ERRORc (parser, o1, er_msg ());
16658  return 0;
16659  }
16660  else
16661  {
16662  return 1;
16663  }
16664 
16665  case PT_LEAST:
16666  error = db_least_or_greatest (arg1, arg2, result, true);
16667  if (error != NO_ERROR)
16668  {
16669  ASSERT_ERROR ();
16670  PT_ERRORc (parser, o1, er_msg ());
16671  return 0;
16672  }
16673 
16674  if (tp_value_cast (result, result, domain, true) != DOMAIN_COMPATIBLE)
16675  {
16677  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
16678  return 0;
16679  }
16680 
16681  return 1;
16682 
16683  case PT_GREATEST:
16684  error = db_least_or_greatest (arg1, arg2, result, false);
16685  if (error != NO_ERROR)
16686  {
16687  ASSERT_ERROR ();
16688  PT_ERRORc (parser, o1, er_msg ());
16689  return 0;
16690  }
16691 
16692  if (tp_value_cast (result, result, domain, true) != DOMAIN_COMPATIBLE)
16693  {
16695  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
16696  return 0;
16697  }
16698 
16699  return 1;
16700 
16701  case PT_POSITION:
16702  error = db_string_position (arg1, arg2, result);
16703  if (error < 0)
16704  {
16705  PT_ERRORc (parser, o1, er_msg ());
16706  return 0;
16707  }
16708  else
16709  {
16710  return 1;
16711  }
16712 
16713  case PT_FINDINSET:
16714  error = db_find_string_in_in_set (arg1, arg2, result);
16715  if (error < 0)
16716  {
16717  PT_ERRORc (parser, o1, er_msg ());
16718  return 0;
16719  }
16720  else
16721  {
16722  return 1;
16723  }
16724 
16725  case PT_SUBSTRING:
16726  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || (o3 && DB_IS_NULL (arg3)))
16727  {
16728  db_make_null (result);
16729  return 1;
16730  }
16731 
16733  {
16734  DB_VALUE tmp_len, tmp_arg2, tmp_arg3;
16735  int pos, len;
16736 
16737  pos = db_get_int (arg2);
16738  if (pos < 0)
16739  {
16740  if (QSTR_IS_BIT (typ1))
16741  {
16742  if (db_string_bit_length (arg1, &tmp_len) != NO_ERROR)
16743  {
16744  PT_ERRORc (parser, o1, er_msg ());
16745  return 0;
16746  }
16747  }
16748  else
16749  {
16750  if (db_string_char_length (arg1, &tmp_len) != NO_ERROR)
16751  {
16752  PT_ERRORc (parser, o1, er_msg ());
16753  return 0;
16754  }
16755  }
16756  if (DB_IS_NULL (&tmp_len))
16757  {
16758  PT_ERRORc (parser, o1, er_msg ());
16759  return 0;
16760  }
16761  pos = pos + db_get_int (&tmp_len) + 1;
16762  }
16763 
16764  if (pos < 1)
16765  {
16766  db_make_int (&tmp_arg2, 1);
16767  }
16768  else
16769  {
16770  db_make_int (&tmp_arg2, pos);
16771  }
16772 
16773  if (o3)
16774  {
16775  len = db_get_int (arg3);
16776  if (len < 1)
16777  {
16778  db_make_int (&tmp_arg3, 0);
16779  }
16780  else
16781  {
16782  db_make_int (&tmp_arg3, len);
16783  }
16784  }
16785  else
16786  {
16787  db_make_null (&tmp_arg3);
16788  }
16789 
16790  error = db_string_substring (pt_misc_to_qp_misc_operand (qualifier), arg1, &tmp_arg2, &tmp_arg3, result);
16791  if (error < 0)
16792  {
16793  PT_ERRORc (parser, o1, er_msg ());
16794  return 0;
16795  }
16796  else
16797  {
16798  return 1;
16799  }
16800  }
16801  else
16802  {
16803  error = db_string_substring (pt_misc_to_qp_misc_operand (qualifier), arg1, arg2, arg3, result);
16804  if (error < 0)
16805  {
16806  PT_ERRORc (parser, o1, er_msg ());
16807  return 0;
16808  }
16809  else
16810  {
16811  return 1;
16812  }
16813  }
16814 
16815  case PT_OCTET_LENGTH:
16816  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
16817  {
16818  db_make_null (result);
16819  return 1;
16820  }
16821 
16822  if (!PT_IS_STRING_TYPE (o1->type_enum))
16823  {
16824  return 0;
16825  }
16826 
16827  db_make_int (result, db_get_string_size (arg1));
16828  return 1;
16829 
16830  case PT_BIT_LENGTH:
16831  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
16832  {
16833  db_make_null (result);
16834  return 1;
16835  }
16836 
16837  if (!PT_IS_STRING_TYPE (o1->type_enum))
16838  {
16839  return 0;
16840  }
16841 
16843  {
16844  db_make_int (result, 8 * db_get_string_size (arg1));
16845  }
16846  else
16847  {
16848  int len = 0;
16849 
16850  /* must be a bit gadget */
16851  (void) db_get_bit (arg1, &len);
16852  db_make_int (result, len);
16853  }
16854  return 1;
16855 
16856  case PT_CHAR_LENGTH:
16857  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
16858  {
16859  db_make_null (result);
16860  return 1;
16861  }
16862  else if (!PT_IS_CHAR_STRING_TYPE (o1->type_enum))
16863  {
16864  return 0;
16865  }
16866  db_make_int (result, db_get_string_length (arg1));
16867  return 1;
16868 
16869  case PT_LOWER:
16870  error = db_string_lower (arg1, result);
16871  if (error < 0)
16872  {
16873  PT_ERRORc (parser, o1, er_msg ());
16874  return 0;
16875  }
16876  else
16877  {
16878  return 1;
16879  }
16880 
16881  case PT_UPPER:
16882  error = db_string_upper (arg1, result);
16883  if (error < 0)
16884  {
16885  PT_ERRORc (parser, o1, er_msg ());
16886  return 0;
16887  }
16888  else
16889  {
16890  return 1;
16891  }
16892 
16893  case PT_HEX:
16894  error = db_hex (arg1, result);
16895  if (error < 0)
16896  {
16897  PT_ERRORc (parser, o1, er_msg ());
16898  return 0;
16899  }
16900  else
16901  {
16902  return 1;
16903  }
16904 
16905  case PT_ASCII:
16906  error = db_ascii (arg1, result);
16907  if (error < 0)
16908  {
16909  PT_ERRORc (parser, o1, er_msg ());
16910  return 0;
16911  }
16912  else
16913  {
16914  return 1;
16915  }
16916 
16917  case PT_CONV:
16918  error = db_conv (arg1, arg2, arg3, result);
16919  if (error < 0)
16920  {
16921  PT_ERRORc (parser, o1, er_msg ());
16922  return 0;
16923  }
16924  else
16925  {
16926  return 1;
16927  }
16928 
16929  case PT_BIN:
16930  error = db_bigint_to_binary_string (arg1, result);
16931  if (error != NO_ERROR)
16932  {
16933  PT_ERRORc (parser, o1, er_msg ());
16934  return 0;
16935  }
16936  else
16937  {
16938  return 1;
16939  }
16940 
16941  case PT_TRIM:
16942  error = db_string_trim (pt_misc_to_qp_misc_operand (qualifier), arg2, arg1, result);
16943  if (error < 0)
16944  {
16945  PT_ERRORc (parser, o1, er_msg ());
16946  return 0;
16947  }
16948  else
16949  {
16950  return 1;
16951  }
16952 
16953  case PT_LIKE_LOWER_BOUND:
16954  case PT_LIKE_UPPER_BOUND:
16955  error = db_like_bound (arg1, arg2, result, (op == PT_LIKE_LOWER_BOUND));
16956  if (error < 0)
16957  {
16958  PT_ERRORc (parser, o1, er_msg ());
16959  return 0;
16960  }
16961  else
16962  {
16963  return 1;
16964  }
16965 
16966  case PT_LTRIM:
16967  error = db_string_trim (LEADING, arg2, arg1, result);
16968  if (error < 0)
16969  {
16970  PT_ERRORc (parser, o1, er_msg ());
16971  return 0;
16972  }
16973  else
16974  {
16975  return 1;
16976  }
16977 
16978  case PT_RTRIM:
16979  error = db_string_trim (TRAILING, arg2, arg1, result);
16980  if (error < 0)
16981  {
16982  PT_ERRORc (parser, o1, er_msg ());
16983  return 0;
16984  }
16985  else
16986  {
16987  return 1;
16988  }
16989 
16990  case PT_FROM_UNIXTIME:
16991  error = db_from_unixtime (arg1, arg2, arg3, result, domain);
16992  if (error < 0)
16993  {
16994  PT_ERRORc (parser, o1, er_msg ());
16995  return 0;
16996  }
16997  else
16998  {
16999  return 1;
17000  }
17001 
17002  case PT_SUBSTRING_INDEX:
17003  error = db_string_substring_index (arg1, arg2, arg3, result);
17004  if (error < 0)
17005  {
17006  PT_ERRORc (parser, o1, er_msg ());
17007  return 0;
17008  }
17009  break;
17010 
17011  case PT_MD5:
17012  error = db_string_md5 (arg1, result);
17013  if (error < 0)
17014  {
17015  PT_ERRORc (parser, o1, er_msg ());
17016  return 0;
17017  }
17018  else
17019  {
17020  return 1;
17021  }
17022 
17023  case PT_SHA_ONE:
17024  error = db_string_sha_one (arg1, result);
17025  if (error < 0)
17026  {
17027  PT_ERRORc (parser, o1, er_msg ());
17028  return 0;
17029  }
17030  else
17031  {
17032  return 1;
17033  }
17034 
17035  case PT_AES_ENCRYPT:
17036  error = db_string_aes_encrypt (arg1, arg2, result);
17037  if (error < 0)
17038  {
17039  PT_ERRORc (parser, o1, er_msg ());
17040  return 0;
17041  }
17042  else
17043  {
17044  return 1;
17045  }
17046 
17047  case PT_AES_DECRYPT:
17048  error = db_string_aes_decrypt (arg1, arg2, result);
17049  if (error < 0)
17050  {
17051  PT_ERRORc (parser, o1, er_msg ());
17052  return 0;
17053  }
17054  else
17055  {
17056  return 1;
17057  }
17058 
17059  case PT_SHA_TWO:
17060  error = db_string_sha_two (arg1, arg2, result);
17061  if (error < 0)
17062  {
17063  PT_ERRORc (parser, o1, er_msg ());
17064  return 0;
17065  }
17066  else
17067  {
17068  return 1;
17069  }
17070 
17071  case PT_TO_BASE64:
17072  error = db_string_to_base64 (arg1, result);
17073  if (error < 0)
17074  {
17075  PT_ERRORc (parser, o1, er_msg ());
17076  return 0;
17077  }
17078  else
17079  {
17080  return 1;
17081  }
17082 
17083  case PT_FROM_BASE64:
17084  error = db_string_from_base64 (arg1, result);
17085  if (error < 0)
17086  {
17087  PT_ERRORc (parser, o1, er_msg ());
17088  return 0;
17089  }
17090  else
17091  {
17092  return 1;
17093  }
17094 
17095  case PT_LPAD:
17096  error = db_string_pad (LEADING, arg1, arg2, arg3, result);
17097  if (error < 0)
17098  {
17099  PT_ERRORc (parser, o1, er_msg ());
17100  return 0;
17101  }
17102  else
17103  {
17104  return 1;
17105  }
17106 
17107  case PT_RPAD:
17108  error = db_string_pad (TRAILING, arg1, arg2, arg3, result);
17109  if (error < 0)
17110  {
17111  PT_ERRORc (parser, o1, er_msg ());
17112  return 0;
17113  }
17114  else
17115  {
17116  return 1;
17117  }
17118 
17119  case PT_REPLACE:
17120  error = db_string_replace (arg1, arg2, arg3, result);
17121  if (error < 0)
17122  {
17123  PT_ERRORc (parser, o1, er_msg ());
17124  return 0;
17125  }
17126  else
17127  {
17128  return 1;
17129  }
17130 
17131  case PT_TRANSLATE:
17132  error = db_string_translate (arg1, arg2, arg3, result);
17133  if (error < 0)
17134  {
17135  PT_ERRORc (parser, o1, er_msg ());
17136  return 0;
17137  }
17138  else
17139  {
17140  return 1;
17141  }
17142 
17143  case PT_ADD_MONTHS:
17144  error = db_add_months (arg1, arg2, result);
17145  if (error < 0)
17146  {
17147  PT_ERRORc (parser, o1, er_msg ());
17148  return 0;
17149  }
17150  else
17151  {
17152  return 1;
17153  }
17154 
17155  case PT_LAST_DAY:
17156  error = db_last_day (arg1, result);
17157  if (error < 0)
17158  {
17159  PT_ERRORc (parser, expr, er_msg ());
17160  return 0;
17161  }
17162  else
17163  {
17164  return 1;
17165  }
17166 
17167  case PT_UNIX_TIMESTAMP:
17168  error = db_unix_timestamp (arg1, result);
17169  if (error < 0)
17170  {
17171  PT_ERRORc (parser, o1, er_msg ());
17172  return 0;
17173  }
17174  else
17175  {
17176  return 1;
17177  }
17178 
17179  case PT_STR_TO_DATE:
17180  error = db_str_to_date (arg1, arg2, arg3, result, NULL);
17181  if (error < 0)
17182  {
17183  PT_ERRORc (parser, o1, er_msg ());
17184  return 0;
17185  }
17186  else
17187  {
17188  return 1;
17189  }
17190 
17191  case PT_TIME_FORMAT:
17192  error = db_time_format (arg1, arg2, arg3, result, domain);
17193  if (error < 0)
17194  {
17195  PT_ERRORc (parser, o1, er_msg ());
17196  return 0;
17197  }
17198  else
17199  {
17200  return 1;
17201  }
17202 
17203  case PT_TIMESTAMP:
17204  if (typ1 == DB_TYPE_NULL)
17205  {
17206  db_make_null (result);
17207  return 1;
17208  }
17209 
17210  error = db_timestamp (arg1, arg2, result);
17211  if (error < 0)
17212  {
17213  PT_ERRORc (parser, o1, er_msg ());
17214  return 0;
17215  }
17216  else
17217  {
17218  return 1;
17219  }
17220 
17221  case PT_YEARF:
17222  case PT_MONTHF:
17223  case PT_DAYF:
17224  error = db_get_date_item (arg1, op, result);
17225  if (error < 0)
17226  {
17227  PT_ERRORc (parser, o1, er_msg ());
17228  return 0;
17229  }
17230  else
17231  {
17232  return 1;
17233  }
17234 
17235  case PT_DAYOFMONTH:
17236  /* day of month is handled like PT_DAYF */
17237  error = db_get_date_item (arg1, PT_DAYF, result);
17238  if (error < 0)
17239  {
17240  PT_ERRORc (parser, o1, er_msg ());
17241  return 0;
17242  }
17243  else
17244  {
17245  return 1;
17246  }
17247 
17248  case PT_HOURF:
17249  case PT_MINUTEF:
17250  case PT_SECONDF:
17251  error = db_get_time_item (arg1, op, result);
17252  if (error < 0)
17253  {
17254  PT_ERRORc (parser, o1, er_msg ());
17255  return 0;
17256  }
17257  else
17258  {
17259  return 1;
17260  }
17261 
17262  case PT_QUARTERF:
17263  {
17264  error = db_get_date_quarter (arg1, result);
17265  if (error < 0)
17266  {
17267  PT_ERRORc (parser, o1, er_msg ());
17268  return 0;
17269  }
17270  else
17271  {
17272  return 1;
17273  }
17274  }
17275 
17276  case PT_WEEKDAY:
17277  case PT_DAYOFWEEK:
17278  {
17279  error = db_get_date_weekday (arg1, op, result);
17280  if (error < 0)
17281  {
17282  PT_ERRORc (parser, o1, er_msg ());
17283  return 0;
17284  }
17285  else
17286  {
17287  return 1;
17288  }
17289  }
17290 
17291  case PT_DAYOFYEAR:
17292  {
17293  error = db_get_date_dayofyear (arg1, result);
17294  if (error < 0)
17295  {
17296  PT_ERRORc (parser, o1, er_msg ());
17297  return 0;
17298  }
17299  else
17300  {
17301  return 1;
17302  }
17303  }
17304 
17305  case PT_TODAYS:
17306  {
17307  error = db_get_date_totaldays (arg1, result);
17308  if (error < 0)
17309  {
17310  PT_ERRORc (parser, o1, er_msg ());
17311  return 0;
17312  }
17313  else
17314  {
17315  return 1;
17316  }
17317  }
17318 
17319  case PT_FROMDAYS:
17320  {
17321  error = db_get_date_from_days (arg1, result);
17322  if (error < 0)
17323  {
17324  PT_ERRORc (parser, o1, er_msg ());
17325  return 0;
17326  }
17327  else
17328  {
17329  return 1;
17330  }
17331  }
17332 
17333  case PT_TIMETOSEC:
17334  {
17335  error = db_convert_time_to_sec (arg1, result);
17336  if (error < 0)
17337  {
17338  PT_ERRORc (parser, o1, er_msg ());
17339  return 0;
17340  }
17341  else
17342  {
17343  return 1;
17344  }
17345  }
17346 
17347  case PT_SECTOTIME:
17348  {
17349  error = db_convert_sec_to_time (arg1, result);
17350  if (error < 0)
17351  {
17352  PT_ERRORc (parser, o1, er_msg ());
17353  return 0;
17354  }
17355  else
17356  {
17357  return 1;
17358  }
17359  }
17360 
17361  case PT_MAKEDATE:
17362  {
17363  error = db_add_days_to_year (arg1, arg2, result);
17364  if (error < 0)
17365  {
17366  PT_ERRORc (parser, o1, er_msg ());
17367  return 0;
17368  }
17369  else
17370  {
17371  return 1;
17372  }
17373  }
17374 
17375  case PT_MAKETIME:
17376  {
17377  error = db_convert_to_time (arg1, arg2, arg3, result);
17378  if (error < 0)
17379  {
17380  PT_ERRORc (parser, o1, er_msg ());
17381  return 0;
17382  }
17383  else
17384  {
17385  return 1;
17386  }
17387  }
17388 
17389  case PT_ADDTIME:
17390  {
17391  error = db_add_time (arg1, arg2, result, NULL);
17392  if (error < 0)
17393  {
17394  PT_ERRORc (parser, o1, er_msg ());
17395  return 0;
17396  }
17397  else
17398  {
17399  return 1;
17400  }
17401  }
17402 
17403  case PT_WEEKF:
17404  {
17405  error = db_get_date_week (arg1, arg2, result);
17406  if (error < 0)
17407  {
17408  PT_ERRORc (parser, o1, er_msg ());
17409  return 0;
17410  }
17411  else
17412  {
17413  return 1;
17414  }
17415  }
17416 
17417  case PT_SCHEMA:
17418  case PT_DATABASE:
17419  db_make_null (result);
17420  error = db_make_string (&tmp_val, db_get_database_name ());
17421  if (error < 0)
17422  {
17423  PT_ERRORc (parser, o1, er_msg ());
17424  return 0;
17425  }
17426 
17427  error = db_value_clone (&tmp_val, result);
17428  if (error < 0)
17429  {
17430  PT_ERRORc (parser, o1, er_msg ());
17431  return 0;
17432  }
17433  else
17434  {
17435  return 1;
17436  }
17437 
17438  case PT_VERSION:
17439  {
17440  db_make_null (result);
17441  error = db_make_string (&tmp_val, db_get_database_version ());
17442  if (error < 0)
17443  {
17444  PT_ERRORc (parser, o1, er_msg ());
17445  return 0;
17446  }
17447 
17448  error = db_value_clone (&tmp_val, result);
17449  if (error < 0)
17450  {
17451  PT_ERRORc (parser, o1, er_msg ());
17452  return 0;
17453  }
17454  else
17455  {
17456  return 1;
17457  }
17458  }
17459 
17460  case PT_MONTHS_BETWEEN:
17461  error = db_months_between (arg1, arg2, result);
17462  if (error < 0)
17463  {
17464  PT_ERRORc (parser, o1, er_msg ());
17465  return 0;
17466  }
17467  else
17468  {
17469  return 1;
17470  }
17471 
17472  case PT_FORMAT:
17473  error = db_format (arg1, arg2, arg3, result, NULL);
17474  if (error < 0)
17475  {
17476  PT_ERRORc (parser, o1, er_msg ());
17477  return 0;
17478  }
17479  else
17480  {
17481  return 1;
17482  }
17483 
17484  case PT_DATE_FORMAT:
17485  error = db_date_format (arg1, arg2, arg3, result, domain);
17486  if (error < 0)
17487  {
17488  PT_ERRORc (parser, o1, er_msg ());
17489  return 0;
17490  }
17491  else
17492  {
17493  return 1;
17494  }
17495 
17496  case PT_ADDDATE:
17497  error = db_date_add_interval_days (result, arg1, arg2);
17498  if (error < 0)
17499  {
17500  PT_ERRORc (parser, o1, er_msg ());
17501  return 0;
17502  }
17503  else
17504  {
17505  return 1;
17506  }
17507 
17508  case PT_DATEDIFF:
17509  error = db_date_diff (arg1, arg2, result);
17510  if (error < 0)
17511  {
17512  PT_ERRORc (parser, o1, er_msg ());
17513  return 0;
17514  }
17515  else
17516  {
17517  return 1;
17518  }
17519  break;
17520 
17521  case PT_TIMEDIFF:
17522  error = db_time_diff (arg1, arg2, result);
17523  if (error < 0)
17524  {
17525  PT_ERRORc (parser, expr, er_msg ());
17526  return 0;
17527  }
17528  else
17529  {
17530  return 1;
17531  }
17532  break;
17533 
17534  case PT_SUBDATE:
17535  error = db_date_sub_interval_days (result, arg1, arg2);
17536  if (error < 0)
17537  {
17538  PT_ERRORc (parser, o1, er_msg ());
17539  return 0;
17540  }
17541  else
17542  {
17543  return 1;
17544  }
17545 
17546  case PT_DATE_ADD:
17547  error = db_date_add_interval_expr (result, arg1, arg2, o3->info.expr.qualifier);
17548  if (error < 0)
17549  {
17550  PT_ERRORc (parser, o1, er_msg ());
17551  return 0;
17552  }
17553  else
17554  {
17555  return 1;
17556  }
17557 
17558  case PT_DATE_SUB:
17559  error = db_date_sub_interval_expr (result, arg1, arg2, o3->info.expr.qualifier);
17560  if (error < 0)
17561  {
17562  PT_ERRORc (parser, o1, er_msg ());
17563  return 0;
17564  }
17565  else
17566  {
17567  return 1;
17568  }
17569 
17570  case PT_SYS_DATE:
17571  {
17572  DB_DATETIME *tmp_datetime;
17573 
17575 
17576  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17577 
17578  db_value_put_encoded_date (result, &tmp_datetime->date);
17579 
17580  return 1;
17581  }
17582 
17583  case PT_UTC_DATE:
17584  {
17585  DB_DATE date;
17586  DB_TIMESTAMP *timestamp;
17587  int year, month, day, hour, minute, second;
17588 
17589  timestamp = db_get_timestamp (&parser->sys_epochtime);
17590  tz_timestamp_decode_no_leap_sec (*timestamp, &year, &month, &day, &hour, &minute, &second);
17591  date = julian_encode (month + 1, day, year);
17592  db_value_put_encoded_date (result, &date);
17593  return 1;
17594  }
17595 
17596  case PT_CURRENT_DATE:
17597  {
17598  TZ_REGION system_tz_region, session_tz_region;
17599  DB_DATETIME *dest_dt, *tmp_datetime;
17600  int err_status = 0;
17601 
17603  tz_get_system_tz_region (&system_tz_region);
17604  tz_get_session_tz_region (&session_tz_region);
17605  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17606  dest_dt = db_get_datetime (&parser->sys_datetime);
17607  err_status =
17608  tz_conv_tz_datetime_w_region (tmp_datetime, &system_tz_region, &session_tz_region, dest_dt, NULL, NULL);
17609  if (err_status != NO_ERROR)
17610  {
17611  return err_status;
17612  }
17613  db_value_put_encoded_date (result, &dest_dt->date);
17614 
17615  return 1;
17616  }
17617 
17618  case PT_UTC_TIME:
17619  {
17620  DB_TIME db_time;
17621  DB_TIMESTAMP *tmp_datetime;
17622 
17623  tmp_datetime = db_get_timestamp (&parser->sys_epochtime);
17624  db_time = (DB_TIME) (*tmp_datetime % SECONDS_OF_ONE_DAY);
17625  db_value_put_encoded_time (result, &db_time);
17626  return 1;
17627  }
17628 
17629  case PT_SYS_TIME:
17630  {
17631  DB_DATETIME *tmp_datetime;
17632  DB_TIME tmp_time;
17633 
17635 
17636  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17637  tmp_time = tmp_datetime->time / 1000;
17638 
17639  db_value_put_encoded_time (result, &tmp_time);
17640 
17641  return 1;
17642  }
17643 
17644  case PT_CURRENT_TIME:
17645  {
17646  DB_DATETIME *tmp_datetime;
17647  DB_TIME cur_time, tmp_time;
17648  const char *t_source, *t_dest;
17649  int err_status = 0, len_source, len_dest;
17650 
17652  t_source = tz_get_system_timezone ();
17653  t_dest = tz_get_session_local_timezone ();
17654  len_source = (int) strlen (t_source);
17655  len_dest = (int) strlen (t_dest);
17656  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17657  tmp_time = tmp_datetime->time / 1000;
17658 
17659  err_status = tz_conv_tz_time_w_zone_name (&tmp_time, t_source, len_source, t_dest, len_dest, &cur_time);
17660  if (err_status != NO_ERROR)
17661  {
17662  return err_status;
17663  }
17664  db_value_put_encoded_time (result, &cur_time);
17665 
17666  return 1;
17667  }
17668 
17669  case PT_SYS_TIMESTAMP:
17670  {
17671  DB_DATETIME *tmp_datetime;
17672  DB_DATE tmp_date = 0;
17673  DB_TIME tmp_time = 0;
17674  DB_TIMESTAMP tmp_timestamp;
17675 
17677 
17678  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17679  tmp_date = tmp_datetime->date;
17680  tmp_time = tmp_datetime->time / 1000;
17681 
17682  (void) db_timestamp_encode_ses (&tmp_date, &tmp_time, &tmp_timestamp, NULL);
17683  db_make_timestamp (result, tmp_timestamp);
17684 
17685  return 1;
17686  }
17687 
17688  case PT_CURRENT_TIMESTAMP:
17689  {
17690  DB_DATETIME *tmp_datetime;
17691  DB_DATE tmp_date = 0;
17692  DB_TIME tmp_time = 0;
17693  DB_TIMESTAMP tmp_timestamp;
17694 
17696 
17697  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17698  tmp_date = tmp_datetime->date;
17699  tmp_time = tmp_datetime->time / 1000;
17700 
17701  (void) db_timestamp_encode_sys (&tmp_date, &tmp_time, &tmp_timestamp, NULL);
17702  db_make_timestamp (result, tmp_timestamp);
17703 
17704  return 1;
17705  }
17706 
17707  case PT_SYS_DATETIME:
17708  {
17709  DB_DATETIME *tmp_datetime;
17710 
17712  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17713 
17714  db_make_datetime (result, tmp_datetime);
17715 
17716  return 1;
17717  }
17718 
17719  case PT_CURRENT_DATETIME:
17720  {
17721  TZ_REGION system_tz_region, session_tz_region;
17722  DB_DATETIME *tmp_datetime, dest_dt;
17723  int err_status;
17724 
17726  tmp_datetime = db_get_datetime (&parser->sys_datetime);
17727  tz_get_system_tz_region (&system_tz_region);
17728  tz_get_session_tz_region (&session_tz_region);
17729  err_status =
17730  tz_conv_tz_datetime_w_region (tmp_datetime, &system_tz_region, &session_tz_region, &dest_dt, NULL, NULL);
17731  if (err_status != NO_ERROR)
17732  {
17733  return err_status;
17734  }
17735 
17736  db_make_datetime (result, &dest_dt);
17737 
17738  return 1;
17739  }
17740 
17741  case PT_CURRENT_USER:
17742  {
17743  const char *username = au_user_name ();
17744 
17745  error = db_make_string_copy (result, username);
17746  db_string_free ((char *) username);
17747  if (error < 0)
17748  {
17749  PT_ERRORc (parser, o1, er_msg ());
17750  return 0;
17751  }
17752  else
17753  {
17754  return 1;
17755  }
17756  }
17757 
17758  case PT_USER:
17759  {
17760  char *user = NULL;
17761 
17762  user = db_get_user_and_host_name ();
17763  db_make_null (result);
17764 
17765  error = db_make_string (&tmp_val, user);
17766  if (error < 0)
17767  {
17768  PT_ERRORc (parser, o1, er_msg ());
17769  db_private_free (NULL, user);
17770  return 0;
17771  }
17772  tmp_val.need_clear = true;
17773 
17774  error = pr_clone_value (&tmp_val, result);
17775  if (error < 0)
17776  {
17777  PT_ERRORc (parser, o1, er_msg ());
17778  return 0;
17779  }
17780  else
17781  {
17782  return 1;
17783  }
17784  }
17785 
17786  case PT_ROW_COUNT:
17787  {
17788  int row_count = db_get_row_count_cache ();
17789  if (row_count == DB_ROW_COUNT_NOT_SET)
17790  {
17791  /* We do not have a cached value of row count. In this case we read it from the server and update the
17792  * cached value */
17793  db_get_row_count (&row_count);
17794  db_update_row_count_cache (row_count);
17795  }
17796  db_make_int (result, row_count);
17797  return 1;
17798  }
17799 
17800  case PT_LAST_INSERT_ID:
17802  {
17803  return 0;
17804  }
17805  else
17806  {
17807  db_value_clone (&tmp_val, result);
17808  return 1;
17809  }
17810 
17812  db_value_clone (&parser->local_transaction_id, result);
17813  return 1;
17814 
17815  case PT_TO_CHAR:
17816  error = db_to_char (arg1, arg2, arg3, result, domain);
17817  if (error < 0)
17818  {
17819  PT_ERRORc (parser, o1, er_msg ());
17820  return 0;
17821  }
17822  else
17823  {
17824  return 1;
17825  }
17826 
17827  case PT_BIT_TO_BLOB:
17828  error = db_bit_to_blob (arg1, result);
17829  if (error < 0)
17830  {
17831  PT_ERRORc (parser, o1, er_msg ());
17832  return 0;
17833  }
17834  else
17835  {
17836  return 1;
17837  }
17838 
17839  case PT_CHAR_TO_BLOB:
17840  error = db_char_to_blob (arg1, result);
17841  if (error < 0)
17842  {
17843  PT_ERRORc (parser, o1, er_msg ());
17844  return 0;
17845  }
17846  else
17847  {
17848  return 1;
17849  }
17850 
17851  case PT_BLOB_FROM_FILE:
17852  error = db_blob_from_file (arg1, result);
17853  if (error < 0)
17854  {
17855  PT_ERRORc (parser, o1, er_msg ());
17856  return 0;
17857  }
17858  else
17859  {
17860  return 1;
17861  }
17862 
17863  case PT_BLOB_TO_BIT:
17864  error = db_blob_to_bit (arg1, arg2, result);
17865  if (error < 0)
17866  {
17867  PT_ERRORc (parser, o1, er_msg ());
17868  return 0;
17869  }
17870  else
17871  {
17872  return 1;
17873  }
17874 
17875  case PT_BLOB_LENGTH:
17876  error = db_blob_length (arg1, result);
17877  if (error < 0)
17878  {
17879  PT_ERRORc (parser, o1, er_msg ());
17880  return 0;
17881  }
17882  else
17883  {
17884  return 1;
17885  }
17886 
17887  case PT_CHAR_TO_CLOB:
17888  error = db_char_to_clob (arg1, result);
17889  if (error < 0)
17890  {
17891  PT_ERRORc (parser, o1, er_msg ());
17892  return 0;
17893  }
17894  else
17895  {
17896  return 1;
17897  }
17898 
17899  case PT_CLOB_FROM_FILE:
17900  error = db_clob_from_file (arg1, result);
17901  if (error < 0)
17902  {
17903  PT_ERRORc (parser, o1, er_msg ());
17904  return 0;
17905  }
17906  else
17907  {
17908  return 1;
17909  }
17910 
17911  case PT_CLOB_TO_CHAR:
17912  error = db_clob_to_char (arg1, arg2, result);
17913  if (error < 0)
17914  {
17915  PT_ERRORc (parser, o1, er_msg ());
17916  return 0;
17917  }
17918  else
17919  {
17920  return 1;
17921  }
17922 
17923  case PT_CLOB_LENGTH:
17924  error = db_clob_length (arg1, result);
17925  if (error < 0)
17926  {
17927  PT_ERRORc (parser, o1, er_msg ());
17928  return 0;
17929  }
17930  else
17931  {
17932  return 1;
17933  }
17934 
17935  case PT_TO_DATE:
17936  error = db_to_date (arg1, arg2, arg3, result);
17937  if (error < 0)
17938  {
17939  PT_ERRORc (parser, o1, er_msg ());
17940  return 0;
17941  }
17942  else
17943  {
17944  return 1;
17945  }
17946 
17947  case PT_TO_TIME:
17948  error = db_to_time (arg1, arg2, arg3, DB_TYPE_TIME, result);
17949  if (error < 0)
17950  {
17951  PT_ERRORc (parser, o1, er_msg ());
17952  return 0;
17953  }
17954  else
17955  {
17956  return 1;
17957  }
17958 
17959  case PT_TO_TIMESTAMP:
17960  error = db_to_timestamp (arg1, arg2, arg3, DB_TYPE_TIMESTAMP, result);
17961  if (error < 0)
17962  {
17963  PT_ERRORc (parser, o1, er_msg ());
17964  return 0;
17965  }
17966  else
17967  {
17968  return 1;
17969  }
17970 
17971  case PT_TO_DATETIME:
17972  error = db_to_datetime (arg1, arg2, arg3, DB_TYPE_DATETIME, result);
17973  if (error < 0)
17974  {
17975  PT_ERRORc (parser, o1, er_msg ());
17976  return 0;
17977  }
17978  else
17979  {
17980  return 1;
17981  }
17982 
17983  case PT_TO_NUMBER:
17984  error = db_to_number (arg1, arg2, arg3, result);
17985  if (error < 0)
17986  {
17987  PT_ERRORc (parser, o1, er_msg ());
17988  return 0;
17989  }
17990  else
17991  {
17992  return 1;
17993  }
17994 
17995  case PT_EVALUATE_VARIABLE:
17996  {
17997  int err = 0;
17999  err = db_get_variable (arg1, result);
18000  if (err != NO_ERROR)
18001  {
18002  PT_ERRORc (parser, o1, er_msg ());
18003  return 0;
18004  }
18005  return 1;
18006  }
18007 
18008  case PT_CAST:
18009  if (TP_DOMAIN_TYPE (domain) == DB_TYPE_VARIABLE)
18010  {
18011  return 0;
18012  }
18013  dom_status = tp_value_cast (arg1, result, domain, false);
18014  if (dom_status != DOMAIN_COMPATIBLE)
18015  {
18016  assert (expr->node_type == PT_EXPR);
18018  {
18019  db_make_null (result);
18020  return 1;
18021  }
18022  if (er_errid () != NO_ERROR)
18023  {
18024  PT_ERRORc (parser, o1, er_msg ());
18025  }
18027  pt_short_print (parser, o1), pt_show_type_enum (rTyp));
18028  return 0;
18029  }
18030  else
18031  {
18032  return 1;
18033  }
18034 
18035  case PT_CASE:
18036  case PT_DECODE:
18037  /* If arg3 = NULL, then arg2 = NULL and arg1 != NULL. For this case, we've already finished checking
18038  * case_search_condition. */
18039  if (arg3 && (DB_VALUE_TYPE (arg3) == DB_TYPE_INTEGER && db_get_int (arg3) != 0))
18040  {
18041  if (tp_value_coerce (arg1, result, domain) != DOMAIN_COMPATIBLE)
18042  {
18044  pt_short_print (parser, o1), pt_show_type_enum (rTyp));
18045  return 0;
18046  }
18047  }
18048  else
18049  {
18050  if (tp_value_coerce (arg2, result, domain) != DOMAIN_COMPATIBLE)
18051  {
18053  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
18054  return 0;
18055  }
18056  }
18057  break;
18058 
18059  case PT_NULLIF:
18060  if (tp_value_compare (arg1, arg2, 1, 0) == DB_EQ)
18061  {
18062  db_make_null (result);
18063  }
18064  else
18065  {
18066  pr_clone_value ((DB_VALUE *) arg1, result);
18067  }
18068  return 1;
18069 
18070  case PT_EXTRACT:
18071  if (typ1 == DB_TYPE_NULL)
18072  {
18073  db_make_null (result);
18074  }
18075  else
18076  {
18077  MISC_OPERAND q_qualifier;
18078  TP_DOMAIN *domain_p;
18079 
18080  q_qualifier = pt_misc_to_qp_misc_operand (qualifier);
18082 
18083  if (q_qualifier == (MISC_OPERAND) 0)
18084  {
18085  return 0;
18086  }
18087 
18088  if (db_string_extract_dbval (q_qualifier, arg1, result, domain_p) != NO_ERROR)
18089  {
18090  return 0;
18091  }
18092  } /* else */
18093  break;
18094 
18095  case PT_EQ:
18096  case PT_NE:
18097  case PT_GE:
18098  case PT_GT:
18099  case PT_LT:
18100  case PT_LE:
18101 
18102  case PT_SETEQ:
18103  case PT_SETNEQ:
18104  case PT_SUPERSETEQ:
18105  case PT_SUPERSET:
18106  case PT_SUBSETEQ:
18107  case PT_SUBSET:
18108 
18109  case PT_NULLSAFE_EQ:
18110 
18111  case PT_IS_IN:
18112  case PT_IS_NOT_IN:
18113  case PT_EQ_SOME:
18114  case PT_NE_SOME:
18115  case PT_GT_SOME:
18116  case PT_GE_SOME:
18117  case PT_LT_SOME:
18118  case PT_LE_SOME:
18119  case PT_EQ_ALL:
18120  case PT_NE_ALL:
18121  case PT_GT_ALL:
18122  case PT_GE_ALL:
18123  case PT_LT_ALL:
18124  case PT_LE_ALL:
18125  case PT_LIKE:
18126  case PT_NOT_LIKE:
18127  case PT_RLIKE:
18128  case PT_NOT_RLIKE:
18129  case PT_RLIKE_BINARY:
18130  case PT_NOT_RLIKE_BINARY:
18131  case PT_BETWEEN:
18132  case PT_NOT_BETWEEN:
18133  case PT_RANGE:
18134 
18135  if (op != PT_BETWEEN && op != PT_NOT_BETWEEN && op != PT_RANGE && (op != PT_EQ || qualifier != PT_EQ_TORDER))
18136  {
18137  if ((typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL) && op != PT_NULLSAFE_EQ)
18138  {
18139  db_make_null (result); /* NULL comp_op any = NULL */
18140  break;
18141  }
18142  }
18143 
18144  switch (op)
18145  {
18146  case PT_EQ:
18147  if (qualifier == PT_EQ_TORDER)
18148  {
18149  cmp_result = tp_value_compare (arg1, arg2, 1, 1);
18150  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ) ? 1 : 0;
18151  break;
18152  }
18153 
18154  /* fall through */
18155  case PT_SETEQ:
18156  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18157  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ) ? 1 : 0;
18158  break;
18159 
18160  case PT_NE:
18161  case PT_SETNEQ:
18162  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18163  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result != DB_EQ) ? 1 : 0;
18164  break;
18165 
18166  case PT_NULLSAFE_EQ:
18167  if ((o1 && o1->node_type != PT_VALUE) || (o2 && o2->node_type != PT_VALUE))
18168  {
18169  return 0;
18170  }
18171  if (arg1 == NULL || arg1->domain.general_info.is_null)
18172  {
18173  if (arg2 == NULL || arg2->domain.general_info.is_null)
18174  {
18175  cmp_result = DB_EQ;
18176  }
18177  else
18178  {
18179  cmp_result = DB_NE;
18180  }
18181  }
18182  else
18183  {
18184  if (arg2 == NULL || arg2->domain.general_info.is_null)
18185  {
18186  cmp_result = DB_NE;
18187  }
18188  else
18189  {
18190  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18191  }
18192  }
18193  cmp = (cmp_result == DB_EQ) ? 1 : 0;
18194  break;
18195 
18196  case PT_SUPERSETEQ:
18197  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18198  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_SUPERSET) ? 1 : 0;
18199  break;
18200 
18201  case PT_SUPERSET:
18202  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18203  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_SUPERSET) ? 1 : 0;
18204  break;
18205 
18206  case PT_SUBSET:
18207  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18208  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_SUBSET) ? 1 : 0;
18209  break;
18210 
18211  case PT_SUBSETEQ:
18212  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18213  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_SUBSET) ? 1 : 0;
18214  break;
18215 
18216  case PT_GE:
18217  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18218  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_GT) ? 1 : 0;
18219  break;
18220 
18221  case PT_GT:
18222  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18223  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_GT) ? 1 : 0;
18224  break;
18225 
18226  case PT_LE:
18227  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18228  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_LT) ? 1 : 0;
18229  break;
18230 
18231  case PT_LT:
18232  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18233  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_LT) ? 1 : 0;
18234  break;
18235 
18236  case PT_EQ_SOME:
18237  case PT_IS_IN:
18238  cmp = set_issome (arg1, db_get_set (arg2), PT_EQ_SOME, 1);
18239  break;
18240 
18241  case PT_NE_SOME:
18242  case PT_GE_SOME:
18243  case PT_GT_SOME:
18244  case PT_LT_SOME:
18245  case PT_LE_SOME:
18246  cmp = set_issome (arg1, db_get_set (arg2), op, 1);
18247  break;
18248 
18249  case PT_EQ_ALL:
18250  cmp = set_issome (arg1, db_get_set (arg2), PT_NE_SOME, 1);
18251  if (cmp == 1)
18252  cmp = 0;
18253  else if (cmp == 0)
18254  cmp = 1;
18255  break;
18256 
18257  case PT_NE_ALL:
18258  case PT_IS_NOT_IN:
18259  cmp = set_issome (arg1, db_get_set (arg2), PT_EQ_SOME, 1);
18260  if (cmp == 1)
18261  cmp = 0;
18262  else if (cmp == 0)
18263  cmp = 1;
18264  break;
18265 
18266  case PT_GE_ALL:
18267  cmp = set_issome (arg1, db_get_set (arg2), PT_LT_SOME, 1);
18268  if (cmp == 1)
18269  cmp = 0;
18270  else if (cmp == 0)
18271  cmp = 1;
18272  break;
18273 
18274  case PT_GT_ALL:
18275  cmp = set_issome (arg1, db_get_set (arg2), PT_LE_SOME, 1);
18276  if (cmp == 1)
18277  cmp = 0;
18278  else if (cmp == 0)
18279  cmp = 1;
18280  break;
18281 
18282  case PT_LT_ALL:
18283  cmp = set_issome (arg1, db_get_set (arg2), PT_GE_SOME, 1);
18284  if (cmp == 1)
18285  cmp = 0;
18286  else if (cmp == 0)
18287  cmp = 1;
18288  break;
18289 
18290  case PT_LE_ALL:
18291  cmp = set_issome (arg1, db_get_set (arg2), PT_GT_SOME, 1);
18292  if (cmp == 1)
18293  cmp = 0;
18294  else if (cmp == 0)
18295  cmp = 1;
18296  break;
18297 
18298  case PT_LIKE:
18299  case PT_NOT_LIKE:
18300  {
18301  DB_VALUE *esc_char = arg3;
18302  DB_VALUE slash_char;
18303  char const *slash_str = "\\";
18304 
18305  if (prm_get_bool_value (PRM_ID_NO_BACKSLASH_ESCAPES) == false && DB_IS_NULL (esc_char))
18306  {
18307  INTL_CODESET arg1_cs = DB_IS_NULL (arg1) ? LANG_SYS_CODESET : db_get_string_codeset (arg1);
18308  int arg1_coll = DB_IS_NULL (arg1) ? LANG_SYS_COLLATION : db_get_string_collation (arg1);
18309  /* when compat_mode=mysql, the slash '\\' is an escape character for LIKE pattern, unless user
18310  * explicitly specifies otherwise. */
18311  esc_char = &slash_char;
18312  if (arg1->domain.general_info.type == DB_TYPE_NCHAR
18314  {
18315  db_make_nchar (esc_char, 1, slash_str, 1, arg1_cs, arg1_coll);
18316  }
18317  else
18318  {
18319  db_make_char (esc_char, 1, slash_str, 1, arg1_cs, arg1_coll);
18320  }
18321 
18322  esc_char->need_clear = false;
18323  }
18324 
18325  if (db_string_like (arg1, arg2, esc_char, &cmp))
18326  {
18327  /* db_string_like() also checks argument types */
18328  return 0;
18329  }
18330  cmp = ((op == PT_LIKE && cmp == V_TRUE) || (op == PT_NOT_LIKE && cmp == V_FALSE)) ? 1 : 0;
18331  }
18332  break;
18333 
18334  case PT_RLIKE:
18335  case PT_NOT_RLIKE:
18336  case PT_RLIKE_BINARY:
18337  case PT_NOT_RLIKE_BINARY:
18338  {
18339  int err = db_string_rlike (arg1, arg2, arg3, NULL, NULL, &cmp);
18340 
18341  switch (err)
18342  {
18343  case NO_ERROR:
18344  break;
18345  case ER_REGEX_COMPILE_ERROR: /* fall through */
18346  case ER_REGEX_EXEC_ERROR:
18347  PT_ERRORc (parser, o1, er_msg ());
18348  /* FALLTHRU */
18349  default:
18350  return 0;
18351  }
18352 
18353  /* negate result if using NOT syntax of operator */
18354  if (op == PT_NOT_RLIKE || op == PT_NOT_RLIKE_BINARY)
18355  {
18356  switch (cmp)
18357  {
18358  case V_TRUE:
18359  cmp = V_FALSE;
18360  break;
18361 
18362  case V_FALSE:
18363  cmp = V_TRUE;
18364  break;
18365 
18366  default:
18367  break;
18368  }
18369  }
18370  }
18371  break;
18372 
18373  case PT_BETWEEN:
18374  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
18375  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg2, arg1);
18376  cmp_result2 = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg3);
18377  if (((cmp_result == DB_UNK) && (cmp_result2 == DB_UNK))
18378  || ((cmp_result == DB_UNK) && ((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))
18379  || ((cmp_result2 == DB_UNK) && ((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18380  {
18381  cmp = -1;
18382  }
18383  else if (((cmp_result != DB_UNK) && (!((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18384  || ((cmp_result2 != DB_UNK) && (!((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))))
18385  {
18386  cmp = 0;
18387  }
18388  else
18389  {
18390  cmp = 1;
18391  }
18392  break;
18393 
18394  case PT_NOT_BETWEEN:
18395  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
18396  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg2, arg1);
18397  cmp_result2 = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg3);
18398  if (((cmp_result == DB_UNK) && (cmp_result2 == DB_UNK))
18399  || ((cmp_result == DB_UNK) && ((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))
18400  || ((cmp_result2 == DB_UNK) && ((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18401  {
18402  cmp = -1;
18403  }
18404  else if (((cmp_result != DB_UNK) && (!((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18405  || ((cmp_result2 != DB_UNK) && (!((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))))
18406  {
18407  cmp = 1;
18408  }
18409  else
18410  {
18411  cmp = 0;
18412  }
18413  break;
18414 
18415  case PT_RANGE:
18416  break;
18417 
18418  default:
18419  return 0;
18420  }
18421 
18422  if (cmp == 1)
18423  db_make_int (result, 1);
18424  else if (cmp == 0)
18425  db_make_int (result, 0);
18426  else
18427  db_make_null (result);
18428  break;
18429 
18430  case PT_INDEX_CARDINALITY:
18431  /* constant folding for this expression is never performed : is always resolved on server */
18432  return 0;
18433  case PT_LIST_DBS:
18434  case PT_SYS_GUID:
18435  case PT_ASSIGN:
18436  case PT_LIKE_ESCAPE:
18437  case PT_BETWEEN_AND:
18438  case PT_BETWEEN_GE_LE:
18439  case PT_BETWEEN_GE_LT:
18440  case PT_BETWEEN_GT_LE:
18441  case PT_BETWEEN_GT_LT:
18442  case PT_BETWEEN_EQ_NA:
18443  case PT_BETWEEN_INF_LE:
18444  case PT_BETWEEN_INF_LT:
18445  case PT_BETWEEN_GE_INF:
18446  case PT_BETWEEN_GT_INF:
18447  /* these don't need to be handled */
18448  return 0;
18449 
18451  {
18452  TP_DOMAIN *enum_domain = NULL;
18453 
18454  assert (expr->info.expr.arg1 != NULL);
18455  assert (expr->data_type != NULL);
18456 
18457  enum_domain = pt_data_type_to_db_domain (parser, expr->data_type, NULL);
18458  if (enum_domain == NULL)
18459  {
18460  return 0;
18461  }
18462 
18463  enum_domain = tp_domain_cache (enum_domain);
18464 
18465  error = db_value_to_enumeration_value (arg1, result, enum_domain);
18466  if (error < 0)
18467  {
18468  PT_ERRORc (parser, o1, er_msg ());
18469  return 0;
18470  }
18471  else
18472  {
18473  if (db_get_enum_short (result) == DB_ENUM_OVERFLOW_VAL)
18474  {
18475  /* To avoid coercing result to enumeration type later on, we consider that this expression cannot be
18476  * folded */
18477  return 0;
18478  }
18479  else
18480  {
18481  return 1;
18482  }
18483  }
18484  }
18485  break;
18486 
18487  case PT_INET_ATON:
18488  error = db_inet_aton (result, arg1);
18489  if (error != NO_ERROR)
18490  {
18491  PT_ERRORc (parser, o1, er_msg ());
18492  return 0;
18493  }
18494  break;
18495 
18496  case PT_INET_NTOA:
18497  error = db_inet_ntoa (result, arg1);
18498  if (error != NO_ERROR)
18499  {
18500  PT_ERRORc (parser, o1, er_msg ());
18501  return 0;
18502  }
18503  break;
18504 
18505  case PT_COERCIBILITY:
18506  /* this expression should always be folded to constant */
18507  assert (false);
18508  break;
18509 
18510  case PT_CHARSET:
18511  case PT_COLLATION:
18512  error = db_get_cs_coll_info (result, arg1, (op == PT_CHARSET) ? 0 : 1);
18513  if (error != NO_ERROR)
18514  {
18515  PT_ERRORc (parser, o1, er_msg ());
18516  return 0;
18517  }
18518  break;
18519 
18520  case PT_WIDTH_BUCKET:
18521  between_ge_lt = o2->info.expr.arg2;
18522  assert (between_ge_lt != NULL && between_ge_lt->node_type == PT_EXPR
18523  && between_ge_lt->info.expr.op == PT_BETWEEN_GE_LT);
18524 
18525  between_ge_lt_arg1 = between_ge_lt->info.expr.arg1;
18526  assert (between_ge_lt_arg1 != NULL && between_ge_lt_arg1->node_type == PT_VALUE);
18527 
18528  between_ge_lt_arg2 = between_ge_lt->info.expr.arg2;
18529  assert (between_ge_lt_arg2 != NULL && between_ge_lt_arg2->node_type == PT_VALUE);
18530 
18531  width_bucket_arg2 = pt_value_to_db (parser, between_ge_lt_arg1);
18532  if (width_bucket_arg2 == NULL)
18533  {
18534  /* error is set in pt_value_to_db */
18535  return 0;
18536  }
18537  width_bucket_arg3 = pt_value_to_db (parser, between_ge_lt_arg2);
18538  if (width_bucket_arg3 == NULL)
18539  {
18540  return 0;
18541  }
18542 
18543  /* get all the parameters */
18544  error = db_width_bucket (result, arg1, width_bucket_arg2, width_bucket_arg3, arg3);
18545  if (error != NO_ERROR)
18546  {
18547  PT_ERRORc (parser, o1, er_msg ());
18548  return 0;
18549  }
18550  break;
18551 
18552  case PT_INDEX_PREFIX:
18553  error = db_string_index_prefix (arg1, arg2, arg3, result);
18554  if (error < 0)
18555  {
18556  PT_ERRORc (parser, o1, er_msg ());
18557  return 0;
18558  }
18559 
18560  break;
18561 
18562  case PT_SLEEP:
18563  error = db_sleep (result, arg1);
18564  if (error < 0)
18565  {
18566  PT_ERRORc (parser, o1, er_msg ());
18567  return 0;
18568  }
18569  break;
18570 
18571  case PT_NEW_TIME:
18572  error = db_new_time (arg1, arg2, arg3, result);
18573  if (error < 0)
18574  {
18575  PT_ERRORc (parser, o1, er_msg ());
18576  return 0;
18577  }
18578  break;
18579 
18580  case PT_FROM_TZ:
18581  error = db_from_tz (arg1, arg2, result);
18582  if (error < 0)
18583  {
18584  PT_ERRORc (parser, o1, er_msg ());
18585  return 0;
18586  }
18587  break;
18588 
18589  case PT_TZ_OFFSET:
18590  {
18591  DB_VALUE timezone;
18592  int timezone_milis;
18593  DB_DATETIME *tmp_datetime, utc_datetime;
18594 
18595  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18596  db_sys_timezone (&timezone);
18597  timezone_milis = db_get_int (&timezone) * 60000;
18598  db_add_int_to_datetime (tmp_datetime, timezone_milis, &utc_datetime);
18599 
18600  if (DB_IS_NULL (arg1))
18601  {
18602  db_make_null (result);
18603  }
18604  else
18605  {
18606  if (db_tz_offset (arg1, result, &utc_datetime) != NO_ERROR)
18607  {
18608  PT_ERRORc (parser, o1, er_msg ());
18609  return 0;
18610  }
18611  }
18612  }
18613  break;
18614 
18615  case PT_TO_DATETIME_TZ:
18616  error = db_to_datetime (arg1, arg2, arg3, DB_TYPE_DATETIMETZ, result);
18617  if (error < 0)
18618  {
18619  PT_ERRORc (parser, o1, er_msg ());
18620  return 0;
18621  }
18622  else
18623  {
18624  return 1;
18625  }
18626  break;
18627 
18628  case PT_CONV_TZ:
18629  error = db_conv_tz (arg1, result);
18630  if (error < 0)
18631  {
18632  PT_ERRORc (parser, o1, er_msg ());
18633  return 0;
18634  }
18635  else
18636  {
18637  return 1;
18638  }
18639  break;
18640 
18641  case PT_TO_TIMESTAMP_TZ:
18642  error = db_to_timestamp (arg1, arg2, arg3, DB_TYPE_TIMESTAMPTZ, result);
18643  if (error < 0)
18644  {
18645  PT_ERRORc (parser, o1, er_msg ());
18646  return 0;
18647  }
18648  else
18649  {
18650  return 1;
18651  }
18652  break;
18653 
18654  case PT_UTC_TIMESTAMP:
18655  {
18656  DB_TIME time;
18657  DB_DATE date;
18658  DB_TIMESTAMP *tmp_timestamp, timestamp;
18659  int year, month, day, hour, minute, second;
18660 
18661  tmp_timestamp = db_get_timestamp (&parser->sys_epochtime);
18662  tz_timestamp_decode_no_leap_sec (*tmp_timestamp, &year, &month, &day, &hour, &minute, &second);
18663  date = julian_encode (month + 1, day, year);
18664  db_time_encode (&time, hour, minute, second);
18665  db_timestamp_encode_ses (&date, &time, &timestamp, NULL);
18666  db_make_timestamp (result, timestamp);
18667  return 1;
18668  }
18669 
18670  case PT_CRC32:
18671  error = db_crc32_dbval (result, arg1);
18672  if (error < 0)
18673  {
18674  PT_ERRORc (parser, o1, er_msg ());
18675  return 0;
18676  }
18677  else
18678  {
18679  return 1;
18680  }
18681 
18682  case PT_SCHEMA_DEF:
18683  error = db_get_schema_def_dbval (result, arg1);
18684  if (error < 0)
18685  {
18686  const char *table_name = NULL;
18687  if (error != ER_QSTR_INVALID_DATA_TYPE)
18688  {
18689  table_name = db_get_string (arg1);
18690  assert (table_name != NULL);
18691 
18692  if (error == ER_OBJ_NOT_A_CLASS)
18693  {
18696  return 0;
18697  }
18698  else if (error == ER_AU_SELECT_FAILURE)
18699  {
18701  table_name);
18702  return 0;
18703  }
18704  }
18705 
18706  PT_ERRORc (parser, o1, er_msg ());
18707  return 0;
18708  }
18709  else
18710  {
18711  return 1;
18712  }
18713 
18714  default:
18715  break;
18716  }
18717 
18718  return 1;
18719 
18720 overflow:
18722  return 0;
18723 
18724 error_zerodate:
18725  PT_ERRORc (parser, o1, er_msg ());
18726  return 0;
18727 }
18728 
18729 /*
18730  * pt_fold_const_expr () - evaluate constant expression
18731  * return: the evaluated expression, if successful,
18732  * unchanged expr, if not successful.
18733  * parser(in): parser global context info for reentrancy
18734  * expr(in): a parse tree representation of a constant expression
18735  */
18736 
18737 static PT_NODE *
18739 {
18740  PT_TYPE_ENUM type1, type2 = PT_TYPE_NONE, type3, result_type;
18741  PT_NODE *opd1 = NULL, *opd2 = NULL, *opd3 = NULL, *result = NULL;
18742  DB_VALUE dummy, dbval_res, *arg1, *arg2, *arg3;
18743  PT_OP_TYPE op;
18744  PT_NODE *expr_next;
18745  int line, column;
18746  short location;
18747  const char *alias_print;
18748  unsigned is_hidden_column;
18749 
18750  // in case of error this functions return the unmodified expr node
18751  // to avoid a memory leak we need to restore the link of expr->next to expr_next
18752  // this will be done in the goto end label
18753  bool has_error = false;
18754  bool was_error_set = false;
18755 
18756  if (expr == NULL)
18757  {
18758  return expr;
18759  }
18760 
18761  if (expr->node_type != PT_EXPR)
18762  {
18763  return expr;
18764  }
18765 
18766  if (expr->flag.do_not_fold)
18767  {
18768  return expr;
18769  }
18770 
18771  location = expr->info.expr.location;
18772 
18773  db_make_null (&dbval_res);
18774 
18775  line = expr->line_number;
18776  column = expr->column_number;
18777  alias_print = expr->alias_print;
18778  expr_next = expr->next;
18779  expr->next = NULL;
18780  result_type = expr->type_enum;
18781  result = expr;
18782  is_hidden_column = expr->flag.is_hidden_column;
18783 
18784  op = expr->info.expr.op;
18785 
18786  if (op == PT_FUNCTION_HOLDER)
18787  {
18788  assert (expr->info.expr.arg1 != NULL);
18789 
18790  if (expr->info.expr.arg1->node_type == PT_VALUE)
18791  {
18792  /* const folding OK , replace current node with arg1 VALUE */
18793  result = parser_copy_tree (parser, expr->info.expr.arg1);
18794  result->info.value.location = location;
18795  result->flag.is_hidden_column = is_hidden_column;
18796  if (result->info.value.text == NULL)
18797  {
18798  result->info.value.text = pt_append_string (parser, NULL, result->alias_print);
18799  }
18800  parser_free_tree (parser, expr);
18801  }
18802  else if (expr->info.expr.arg1->node_type != PT_FUNCTION)
18803  {
18804  assert (false);
18805  }
18806  goto end;
18807  }
18808  /* special handling for only one range - convert to comp op */
18809  if (op == PT_RANGE)
18810  {
18811  PT_NODE *between_and;
18812  PT_OP_TYPE between_op;
18813 
18814  between_and = expr->info.expr.arg2;
18815  between_op = between_and->info.expr.op;
18816  if (between_and->or_next == NULL)
18817  { /* has only one range */
18818  opd1 = expr->info.expr.arg1;
18819  opd2 = between_and->info.expr.arg1;
18820  opd3 = between_and->info.expr.arg2;
18821  if (opd1 && opd1->node_type == PT_VALUE && opd2 && opd2->node_type == PT_VALUE)
18822  { /* both const */
18823  if (between_op == PT_BETWEEN_EQ_NA || between_op == PT_BETWEEN_GT_INF || between_op == PT_BETWEEN_GE_INF
18824  || between_op == PT_BETWEEN_INF_LT || between_op == PT_BETWEEN_INF_LE)
18825  {
18826  /* convert to comp op */
18827  between_and->info.expr.arg1 = NULL;
18828  parser_free_tree (parser, between_and);
18829  expr->info.expr.arg2 = opd2;
18830 
18831  if (between_op == PT_BETWEEN_EQ_NA)
18832  {
18833  op = expr->info.expr.op = PT_EQ;
18834  }
18835  else if (between_op == PT_BETWEEN_GT_INF)
18836  {
18837  op = expr->info.expr.op = PT_GT;
18838  }
18839  else if (between_op == PT_BETWEEN_GE_INF)
18840  {
18841  op = expr->info.expr.op = PT_GE;
18842  }
18843  else if (between_op == PT_BETWEEN_INF_LT)
18844  {
18845  op = expr->info.expr.op = PT_LT;
18846  }
18847  else
18848  {
18849  op = expr->info.expr.op = PT_LE;
18850  }
18851  }
18852  else if (between_op == PT_BETWEEN_GE_LE)
18853  {
18854  if (opd3 && opd3->node_type == PT_VALUE)
18855  {
18856  /* convert to between op */
18857  between_and->info.expr.op = PT_BETWEEN_AND;
18858  op = expr->info.expr.op = PT_BETWEEN;
18859  }
18860  }
18861  }
18862  }
18863  }
18864  else if (op == PT_NEXT_VALUE || op == PT_CURRENT_VALUE || op == PT_BIT_TO_BLOB || op == PT_CHAR_TO_BLOB
18865  || op == PT_BLOB_TO_BIT || op == PT_BLOB_LENGTH || op == PT_CHAR_TO_CLOB || op == PT_CLOB_TO_CHAR
18866  || op == PT_CLOB_LENGTH || op == PT_EXEC_STATS || op == PT_TRACE_STATS || op == PT_TZ_OFFSET)
18867  {
18868  goto end;
18869  }
18870  else if (op == PT_ROW_COUNT)
18871  {
18872  int row_count = db_get_row_count_cache ();
18873  if (row_count == DB_ROW_COUNT_NOT_SET)
18874  {
18875  /* Read the value from the server and cache it */
18876  db_get_row_count (&row_count);
18877  db_update_row_count_cache (row_count);
18878  }
18879  db_make_int (&dbval_res, row_count);
18880  result = pt_dbval_to_value (parser, &dbval_res);
18881  goto end;
18882  }
18883  else if (op == PT_COERCIBILITY)
18884  {
18885  PT_COLL_INFER coll_infer;
18886 
18887  if (pt_get_collation_info (expr->info.expr.arg1, &coll_infer))
18888  {
18889  if (coll_infer.coerc_level >= PT_COLLATION_L2_COERC && coll_infer.coerc_level <= PT_COLLATION_L2_BIN_COERC
18890  && coll_infer.can_force_cs == true)
18891  {
18892  db_make_int (&dbval_res, -1);
18893  }
18894  else
18895  {
18896  db_make_int (&dbval_res, (int) (coll_infer.coerc_level));
18897  }
18898  }
18899  else
18900  {
18902  {
18903  er_clear ();
18904  db_make_null (&dbval_res);
18905  }
18906  else
18907  {
18909  PT_ERRORc (parser, expr, er_msg ());
18910  goto end;
18911  }
18912  }
18913 
18914  result = pt_dbval_to_value (parser, &dbval_res);
18915  goto end;
18916  }
18917 
18918  opd1 = expr->info.expr.arg1;
18919 
18920  if (opd1 && op == PT_DEFAULTF)
18921  {
18922  PT_NODE *default_value, *default_value_date_type;
18923  bool needs_update_precision = false;
18924 
18925  default_value = parser_copy_tree (parser, opd1->info.name.default_value);
18926  if (default_value == NULL)
18927  {
18928  has_error = true;
18929  goto end;
18930  }
18931 
18932  default_value_date_type = opd1->info.name.default_value->data_type;
18933  if (opd1->data_type != NULL)
18934  {
18935  switch (opd1->type_enum)
18936  {
18937  case PT_TYPE_CHAR:
18938  case PT_TYPE_VARCHAR:
18939  case PT_TYPE_NCHAR:
18940  case PT_TYPE_VARNCHAR:
18941  case PT_TYPE_BIT:
18942  case PT_TYPE_VARBIT:
18943  case PT_TYPE_NUMERIC:
18944  case PT_TYPE_ENUMERATION:
18945  if (default_value_date_type == NULL
18946  || (default_value_date_type->info.data_type.precision != opd1->data_type->info.data_type.precision))
18947  {
18948  needs_update_precision = true;
18949  }
18950  break;
18951 
18952  default:
18953  break;
18954  }
18955  }
18956 
18957  if ((opd1->info.name.default_value->type_enum == PT_TYPE_NULL)
18958  || (opd1->info.name.default_value->type_enum == opd1->type_enum && needs_update_precision == false))
18959  {
18960  result = default_value;
18961  }
18962  else
18963  {
18964  PT_NODE *dt, *cast_expr;
18965 
18966  /* need to coerce to opd1->type_enum */
18967  cast_expr = parser_new_node (parser, PT_EXPR);
18968  if (cast_expr == NULL)
18969  {
18970  parser_free_tree (parser, default_value);
18971  has_error = true;
18972 
18973  goto end;
18974  }
18975 
18976  cast_expr->line_number = opd1->info.name.default_value->line_number;
18977  cast_expr->column_number = opd1->info.name.default_value->column_number;
18978  cast_expr->info.expr.op = PT_CAST;
18979  cast_expr->info.expr.arg1 = default_value;
18980  cast_expr->type_enum = opd1->type_enum;
18981  cast_expr->info.expr.location = is_hidden_column;
18982 
18983  if (opd1->data_type)
18984  {
18985  dt = parser_copy_tree (parser, opd1->data_type);
18986  if (dt == NULL)
18987  {
18988  parser_free_tree (parser, default_value);
18989  parser_free_tree (parser, cast_expr);
18990 
18991  has_error = true;
18992  goto end;
18993  }
18994  }
18995  else
18996  {
18997  dt = parser_new_node (parser, PT_DATA_TYPE);
18998  if (dt == NULL)
18999  {
19000  parser_free_tree (parser, default_value);
19001  parser_free_tree (parser, cast_expr);
19002 
19003  has_error = true;
19004  goto end;
19005  }
19006  dt->type_enum = opd1->type_enum;
19007  }
19008 
19009  cast_expr->info.expr.cast_type = dt;
19010  result = cast_expr;
19011  }
19012 
19013  goto end;
19014  }
19015 
19016  if (op == PT_OID_OF_DUPLICATE_KEY)
19017  {
19018  OID null_oid;
19019  PT_NODE *tmp_value = parser_new_node (parser, PT_VALUE);
19020 
19021  if (tmp_value == NULL)
19022  {
19023  has_error = true;
19024  goto end;
19025  }
19026 
19027  /* a NULL OID is returned; the resulting PT_VALUE node will be replaced with a PT_HOST_VAR by the auto
19028  * parameterization step because of the special force_auto_parameterize flag. Also see and pt_dup_key_update_stmt
19029  * () and qo_optimize_queries () */
19030  tmp_value->type_enum = PT_TYPE_OBJECT;
19031  OID_SET_NULL (&null_oid);
19032  db_make_oid (&tmp_value->info.value.db_value, &null_oid);
19033  tmp_value->info.value.db_value_is_initialized = true;
19034  tmp_value->data_type = parser_copy_tree (parser, expr->data_type);
19035  if (tmp_value->data_type == NULL)
19036  {
19037  parser_free_tree (parser, tmp_value);
19038  tmp_value = NULL;
19039  has_error = true;
19040  goto end;
19041  }
19042  result = tmp_value;
19043  goto end;
19044  }
19045 
19046  if (opd1 && opd1->node_type == PT_VALUE)
19047  {
19048  arg1 = pt_value_to_db (parser, opd1);
19049  type1 = opd1->type_enum;
19050  }
19051  else
19052  {
19053  if (op == PT_EQ && expr->info.expr.qualifier == PT_EQ_TORDER)
19054  {
19055  goto end;
19056  }
19057  db_make_null (&dummy);
19058  arg1 = &dummy;
19059  type1 = PT_TYPE_NULL;
19060  }
19061 
19062  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
19063  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
19065  {
19066  opd2 = expr->info.expr.arg2->info.expr.arg1;
19067  }
19068  else
19069  {
19070  opd2 = expr->info.expr.arg2;
19071  }
19072 
19073  if (opd2 && opd2->node_type == PT_VALUE)
19074  {
19075  arg2 = pt_value_to_db (parser, opd2);
19076  type2 = opd2->type_enum;
19077  }
19078  else
19079  {
19080  switch (op)
19081  {
19082  case PT_TRIM:
19083  case PT_LTRIM:
19084  case PT_RTRIM:
19085  {
19086  arg2 = NULL;
19087  }
19088  break;
19089 
19090  case PT_FROM_UNIXTIME:
19091  arg2 = NULL;
19092  break;
19093 
19094  case PT_LIKE_LOWER_BOUND:
19095  case PT_LIKE_UPPER_BOUND:
19096  case PT_TIMESTAMP:
19097  /* If an operand2 exists and it's not a value, do not fold. */
19098  if (opd2 != NULL)
19099  {
19100  goto end;
19101  }
19102  arg2 = NULL;
19103  break;
19104 
19105  case PT_INCR:
19106  case PT_DECR:
19107  {
19108  PT_NODE *entity = NULL, *top, *spec;
19109  PT_NODE *dtype;
19110  const char *attr_name;
19111  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
19112  int attrid, shared;
19113  DB_DOMAIN *dom;
19114 
19115  /* add an argument, oid of instance to do increment */
19116  if (opd1 != NULL && opd1->node_type == PT_NAME)
19117  {
19118  opd2 = pt_name (parser, "");
19119  if (opd2 == NULL)
19120  {
19121  has_error = true;
19122  goto end;
19123  }
19124 
19125  dtype = parser_new_node (parser, PT_DATA_TYPE);
19126  if (dtype == NULL)
19127  {
19128  has_error = true;
19129  goto end;
19130  }
19131 
19132  if (sc_info && (top = sc_info->top_node) && (top->node_type == PT_SELECT))
19133  {
19134  /* if given top node, find domain class, and check if it is a derived class */
19135  spec = pt_find_entity (parser, top->info.query.q.select.from, opd1->info.name.spec_id);
19136  if (spec)
19137  {
19138  entity = spec->info.spec.entity_name;
19139  }
19140  }
19141  else
19142  {
19143  entity = pt_name (parser, opd1->info.name.resolved);
19144  if (entity == NULL)
19145  {
19146  has_error = true;
19147  goto end;
19148  }
19149  entity->info.name.db_object = db_find_class (entity->info.name.original);
19150  }
19151 
19152  if (entity == NULL || entity->info.name.db_object == NULL)
19153  {
19154  PT_ERRORf (parser, expr, "Attribute of derived class " "is not permitted in %s()",
19155  (op == PT_INCR ? "INCR" : "DECR"));
19156  has_error = true;
19157  was_error_set = true;
19158  goto end;
19159  }
19160  dtype->type_enum = PT_TYPE_OBJECT;
19161  dtype->info.data_type.entity = entity;
19163 
19164  opd2->data_type = dtype;
19165  opd2->type_enum = PT_TYPE_OBJECT;
19166  opd2->info.name.meta_class = PT_OID_ATTR;
19167  opd2->info.name.spec_id = opd1->info.name.spec_id;
19168  opd2->info.name.resolved = pt_append_string (parser, NULL, opd1->info.name.resolved);
19169  if (opd2->info.name.resolved == NULL)
19170  {
19171  has_error = true;
19172  goto end;
19173  }
19174 
19175  attr_name = opd1->info.name.original;
19176  expr->info.expr.arg2 = opd2;
19177  }
19178  else if (opd1 != NULL && opd1->node_type == PT_DOT_)
19179  {
19180  PT_NODE *arg2, *arg1 = opd1->info.dot.arg1;
19181 
19182  opd2 = parser_copy_tree_list (parser, arg1);
19183  if (opd2 == NULL)
19184  {
19185  has_error = true;
19186  goto end;
19187  }
19188 
19189  if (opd2->node_type == PT_DOT_)
19190  {
19191  arg2 = opd2->info.dot.arg2;
19192  entity = arg2->data_type->info.data_type.entity;
19193  }
19194  else
19195  {
19196  entity = opd2->data_type->info.data_type.entity;
19197  }
19198 
19199  attr_name = opd1->info.dot.arg2->info.name.original;
19200  expr->info.expr.arg2 = opd2;
19201  }
19202  else
19203  {
19204  PT_ERRORf (parser, expr, "Invalid argument in %s()", (op == PT_INCR ? "INCR" : "DECR"));
19205 
19206  has_error = true;
19207  was_error_set = true;
19208  goto end;
19209  }
19210 
19211  /* add an argument, id of attribute to do increment */
19212  opd3 = parser_new_node (parser, PT_VALUE);
19213  if (opd3 == NULL)
19214  {
19215  has_error = true;
19216  goto end;
19217  }
19218 
19219  if (sm_att_info (entity->info.name.db_object, attr_name, &attrid, &dom, &shared, 0) < 0)
19220  {
19221  has_error = true;
19222  goto end;
19223  }
19224 
19225  opd3->type_enum = PT_TYPE_INTEGER;
19226  opd3->info.value.data_value.i = attrid;
19227  expr->info.expr.arg3 = opd3;
19228  }
19229 
19230  /* fall through */
19231  default:
19232  db_make_null (&dummy);
19233  arg2 = &dummy;
19234  type2 = PT_TYPE_NULL;
19235  }
19236  }
19237 
19239  && (op == PT_STRCAT || op == PT_PLUS || op == PT_CONCAT || op == PT_CONCAT_WS))
19240  {
19241  TP_DOMAIN *domain;
19242 
19243  /* use the caching variant of this function ! */
19244  domain = pt_xasl_node_to_domain (parser, expr);
19245 
19246  if (domain && QSTR_IS_ANY_CHAR_OR_BIT (TP_DOMAIN_TYPE (domain)))
19247  {
19248  if (opd1 && opd1->node_type == PT_VALUE && type1 == PT_TYPE_NULL && PT_IS_STRING_TYPE (type2))
19249  {
19250  /* fold 'null || char_opnd' expr to 'char_opnd' */
19251  result = parser_copy_tree (parser, opd2);
19252  if (result == NULL)
19253  {
19254  has_error = true;
19255  goto end;
19256  }
19257  }
19258  else if (opd2 && opd2->node_type == PT_VALUE && type2 == PT_TYPE_NULL && PT_IS_STRING_TYPE (type1))
19259  {
19260  /* fold 'char_opnd || null' expr to 'char_opnd' */
19261  result = parser_copy_tree (parser, opd1);
19262  if (result == NULL)
19263  {
19264  has_error = true;
19265  goto end;
19266  }
19267  }
19268 
19269  goto end; /* finish folding */
19270  }
19271  }
19272 
19273  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
19274  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
19276  {
19277  opd3 = expr->info.expr.arg2->info.expr.arg2;
19278  }
19279  else
19280  {
19281  opd3 = expr->info.expr.arg3;
19282  }
19283 
19284  if (opd3 && opd3->node_type == PT_VALUE)
19285  {
19286  arg3 = pt_value_to_db (parser, opd3);
19287  type3 = opd3->type_enum;
19288  }
19289  else
19290  {
19291  switch (op)
19292  {
19293  case PT_LPAD:
19294  case PT_RPAD:
19295  case PT_REPLACE:
19296  {
19297  arg3 = NULL;
19298  }
19299  break;
19300  case PT_LIKE:
19301  arg3 = NULL;
19302  type3 = PT_TYPE_NONE;
19303  break;
19304 
19305  default:
19306  db_make_null (&dummy);
19307  arg3 = &dummy;
19308  type3 = PT_TYPE_NULL;
19309  break;
19310  }
19311  }
19312 
19313  /* If the search condition for the CASE op is already determined, optimize the tree and screen out the arguments for
19314  * a possible call of pt_evaluate_db_val_expr. */
19315  if ((op == PT_CASE || op == PT_DECODE) && opd3 && opd3->node_type == PT_VALUE)
19316  {
19317  if (arg3 && (DB_VALUE_TYPE (arg3) == DB_TYPE_INTEGER && db_get_int (arg3)))
19318  {
19319  opd2 = NULL;
19320  }
19321  else
19322  {
19323  opd1 = opd2;
19324  arg1 = arg2;
19325  opd2 = NULL;
19326  }
19327 
19328  if (opd1 && opd1->node_type != PT_VALUE)
19329  {
19330  if (expr->info.expr.arg2 == opd1 && (opd1->info.expr.op == PT_CASE || opd1->info.expr.op == PT_DECODE))
19331  {
19332  opd1->info.expr.continued_case = 0;
19333  }
19334 
19335  if (pt_check_same_datatype (parser, expr, opd1))
19336  {
19337  result = parser_copy_tree_list (parser, opd1);
19338  if (result == NULL)
19339  {
19340  has_error = true;
19341  goto end;
19342  }
19343  }
19344  else
19345  {
19346  PT_NODE *res;
19347 
19348  res = parser_new_node (parser, PT_EXPR);
19349  if (res == NULL)
19350  {
19351  has_error = true;
19352  goto end;
19353  }
19354 
19355  res->line_number = opd1->line_number;
19356  res->column_number = opd1->column_number;
19357  res->info.expr.op = PT_CAST;
19358  res->info.expr.arg1 = parser_copy_tree_list (parser, opd1);
19359  res->type_enum = expr->type_enum;
19360  res->info.expr.location = expr->info.expr.location;
19361  res->flag.is_hidden_column = is_hidden_column;
19363 
19364  if (pt_is_set_type (expr))
19365  {
19366  PT_NODE *sdt;
19367 
19368  sdt = parser_new_node (parser, PT_DATA_TYPE);
19369  if (sdt == NULL)
19370  {
19371  parser_free_tree (parser, res);
19372  has_error = true;
19373  goto end;
19374  }
19375  res->data_type = parser_copy_tree_list (parser, expr->data_type);
19376  sdt->type_enum = expr->type_enum;
19377  sdt->data_type = parser_copy_tree_list (parser, expr->data_type);
19378  res->info.expr.cast_type = sdt;
19379  }
19380  else if (PT_IS_PARAMETERIZED_TYPE (expr->type_enum))
19381  {
19382  res->data_type = parser_copy_tree_list (parser, expr->data_type);
19383  res->info.expr.cast_type = parser_copy_tree_list (parser, expr->data_type);
19384  res->info.expr.cast_type->type_enum = expr->type_enum;
19385  }
19386  else
19387  {
19388  PT_NODE *dt;
19389 
19390  dt = parser_new_node (parser, PT_DATA_TYPE);
19391  if (dt == NULL)
19392  {
19393  parser_free_tree (parser, res);
19394  has_error = true;
19395  goto end;
19396  }
19397  dt->type_enum = expr->type_enum;
19398  res->info.expr.cast_type = dt;
19399  }
19400 
19401  result = res;
19402  }
19403  }
19404 
19405  opd3 = NULL;
19406  }
19407 
19408  /* If the op is AND or OR and one argument is a true/false/NULL and the other is a logical expression, optimize the
19409  * tree so that one of the arguments replaces the node. */
19410  if (opd1 && opd2
19411  && ((opd1->type_enum == PT_TYPE_LOGICAL || opd1->type_enum == PT_TYPE_NULL || opd1->type_enum == PT_TYPE_MAYBE)
19412  && (opd2->type_enum == PT_TYPE_LOGICAL || opd2->type_enum == PT_TYPE_NULL
19413  || opd2->type_enum == PT_TYPE_MAYBE))
19414  && ((opd1->node_type == PT_VALUE && opd2->node_type != PT_VALUE)
19415  || (opd2->node_type == PT_VALUE && opd1->node_type != PT_VALUE)) && (op == PT_AND || op == PT_OR))
19416  {
19417  PT_NODE *val;
19418  PT_NODE *other;
19419  DB_VALUE *db_value;
19420 
19421  if (opd1->node_type == PT_VALUE)
19422  {
19423  val = opd1;
19424  other = opd2;
19425  }
19426  else
19427  {
19428  val = opd2;
19429  other = opd1;
19430  }
19431 
19432  db_value = pt_value_to_db (parser, val);
19433  if (op == PT_AND)
19434  {
19435  if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_NULL)
19436  {
19437  result = val;
19438  }
19439  else if (db_value && db_get_int (db_value) == 1)
19440  {
19441  result = other;
19442  }
19443  else
19444  {
19445  result = val;
19446  }
19447  }
19448  else
19449  { /* op == PT_OR */
19450  if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_NULL)
19451  {
19452  result = other;
19453  }
19454  else if (db_value && db_get_int (db_value) == 1)
19455  {
19456  result = val;
19457  }
19458  else
19459  {
19460  result = other;
19461  }
19462  }
19463 
19464  result = parser_copy_tree_list (parser, result);
19465  if (result == NULL)
19466  {
19467  has_error = true;
19468  goto end;
19469  }
19470  }
19471  else if (opd1
19472  && ((opd1->node_type == PT_VALUE
19474  && (!opd2 || opd2->node_type == PT_VALUE) && (!opd3 || opd3->node_type == PT_VALUE))
19475  || ((opd1->type_enum == PT_TYPE_NA || opd1->type_enum == PT_TYPE_NULL)
19476  && op != PT_CASE && op != PT_TO_CHAR && op != PT_TO_NUMBER && op != PT_TO_DATE && op != PT_TO_TIME
19477  && op != PT_TO_TIMESTAMP && op != PT_TO_DATETIME && op != PT_NULLIF && op != PT_COALESCE
19478  && op != PT_NVL && op != PT_NVL2 && op != PT_DECODE && op != PT_IFNULL && op != PT_TO_DATETIME_TZ
19479  && op != PT_TO_TIMESTAMP_TZ)
19480  || (opd2 && (opd2->type_enum == PT_TYPE_NA || opd2->type_enum == PT_TYPE_NULL)
19481  && op != PT_CASE && op != PT_TO_CHAR && op != PT_TO_NUMBER && op != PT_TO_DATE && op != PT_TO_TIME
19482  && op != PT_TO_TIMESTAMP && op != PT_TO_DATETIME && op != PT_BETWEEN && op != PT_NOT_BETWEEN
19483  && op != PT_SYS_CONNECT_BY_PATH && op != PT_NULLIF && op != PT_COALESCE && op != PT_NVL
19484  && op != PT_NVL2 && op != PT_DECODE && op != PT_IFNULL && op != PT_IF
19485  && (op != PT_RANGE || !opd2->or_next) && op != PT_TO_DATETIME_TZ && op != PT_TO_TIMESTAMP_TZ)
19486  || (opd3 && (opd3->type_enum == PT_TYPE_NA || opd3->type_enum == PT_TYPE_NULL)
19487  && op != PT_BETWEEN && op != PT_NOT_BETWEEN && op != PT_NVL2 && op != PT_IF)
19488  || (opd2 && opd3 && op == PT_IF && (opd2->type_enum == PT_TYPE_NA || opd2->type_enum == PT_TYPE_NULL)
19489  && (opd3->type_enum == PT_TYPE_NA || opd3->type_enum == PT_TYPE_NULL))
19490  /* width_bucket special case */
19491  || (op == PT_WIDTH_BUCKET && pt_is_const_foldable_width_bucket (parser, expr))))
19492  {
19493  PT_MISC_TYPE qualifier = (PT_MISC_TYPE) 0;
19494  TP_DOMAIN *domain;
19495 
19496  if (op == PT_TRIM || op == PT_EXTRACT || op == PT_SUBSTRING || op == PT_EQ)
19497  {
19498  qualifier = expr->info.expr.qualifier;
19499  }
19500 
19501  /* use the caching variant of this function ! */
19502  domain = pt_xasl_node_to_domain (parser, expr);
19503 
19504  /* check to see if we received an error getting the domain */
19505  if (pt_has_error (parser))
19506  {
19507  if (result)
19508  {
19509  if (result != expr)
19510  {
19511  parser_free_tree (parser, result);
19512  }
19513  }
19514 
19515  has_error = true;
19516  was_error_set = true;
19517  goto end;
19518  }
19519 
19520  if (!pt_check_const_fold_op_w_args (op, arg1, arg2, arg3, domain))
19521  {
19522  goto end;
19523  }
19524 
19525  if (pt_evaluate_db_value_expr (parser, expr, op, arg1, arg2, arg3, &dbval_res, domain, opd1, opd2, opd3,
19526  qualifier))
19527  {
19528  result = pt_dbval_to_value (parser, &dbval_res);
19529  if (result)
19530  {
19531  result->expr_before_const_folding = pt_print_bytes (parser, expr);
19532  }
19533  if (result && result->data_type == NULL && result->type_enum != PT_TYPE_NULL)
19534  {
19535  /* data_type may be needed later... e.g. in CTEs */
19536  result->data_type = parser_copy_tree_list (parser, expr->data_type);
19537  }
19538  if (result && expr->or_next && result != expr)
19539  {
19540  PT_NODE *other;
19541  DB_VALUE *db_value;
19542 
19543  /* i.e., op == PT_OR */
19544  db_value = pt_value_to_db (parser, result); /* opd1 */
19545  other = expr->or_next; /* opd2 */
19546  if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_NULL)
19547  {
19548  result = other;
19549  }
19550  else if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_INTEGER && db_get_int (db_value) == 1)
19551  {
19552  parser_free_tree (parser, result->or_next);
19553  result->or_next = NULL;
19554  }
19555  else
19556  {
19557  result = other;
19558  }
19559 
19560  result = parser_copy_tree_list (parser, result);
19561  if (result == NULL)
19562  {
19563  pr_clear_value (&dbval_res);
19564  has_error = true;
19565  goto end;
19566  }
19567  }
19568  }
19569  }
19570  else if (result_type == PT_TYPE_LOGICAL)
19571  {
19572  /* We'll check to see if the expression is always true or false due to type boundary overflows */
19573  if (opd1 && opd1->node_type == PT_VALUE && opd2 && opd2->node_type == PT_VALUE)
19574  {
19575  result = pt_compare_bounds_to_value (parser, expr, op, opd1->type_enum, arg2, type2);
19576  }
19577 
19578  if (result && expr->or_next && result != expr)
19579  {
19580  PT_NODE *other;
19581  DB_VALUE *db_value;
19582 
19583  /* i.e., op == PT_OR */
19584  db_value = pt_value_to_db (parser, result); /* opd1 */
19585  other = result->or_next; /* opd2 */
19586  if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_NULL)
19587  {
19588  result = other;
19589  }
19590  else if (db_value && DB_VALUE_TYPE (db_value) == DB_TYPE_INTEGER && db_get_int (db_value) == 1)
19591  {
19592  parser_free_tree (parser, result->or_next);
19593  result->or_next = NULL;
19594  }
19595  else
19596  {
19597  result = other;
19598  }
19599 
19600  result = parser_copy_tree_list (parser, result);
19601  }
19602 
19603 #if 0
19604  /* We tried to fold trivial expressions which is always true: e.g, inst_num() > 0
19605  * This looks like a nice optimization but it causes defects with rewrite optimization of limit clause.
19606  * Once we fold a rewritten predicate here, limit clause might be bound with an incorrect hostvar.
19607  * Please note that limit clause and rewritten predicates works independently.
19608  * The optimization cannot be applied until we change the design of limit evaluation.
19609  */
19610  if (opd1 && opd1->node_type == PT_EXPR && opd2 && opd2->node_type == PT_VALUE
19611  && (opd1->info.expr.op == PT_INST_NUM || opd1->info.expr.op == PT_ORDERBY_NUM)
19612  && (opd2->type_enum == PT_TYPE_INTEGER || opd2->type_enum == PT_TYPE_BIGINT))
19613  {
19614  DB_BIGINT rvalue;
19615 
19616  if (opd2->type_enum == PT_TYPE_INTEGER)
19617  {
19618  rvalue = opd2->info.value.data_value.i;
19619  }
19620  else if (opd2->type_enum == PT_TYPE_BIGINT)
19621  {
19622  rvalue = opd2->info.value.data_value.bigint;
19623  }
19624  else
19625  {
19626  assert (0);
19627  rvalue = 0;
19628  }
19629 
19630  if ((op == PT_GT && rvalue <= 0) || (op == PT_GE && rvalue <= 1))
19631  {
19632  /* always true */
19633  db_make_int (&dbval_res, 1);
19634  result = pt_dbval_to_value (parser, &dbval_res);
19635  }
19636  }
19637 #endif
19638 
19639  if (result == NULL)
19640  {
19641  has_error = true;
19642  goto end;
19643  }
19644  }
19645 
19646 end:
19647  pr_clear_value (&dbval_res);
19648 
19649  if (has_error)
19650  {
19651  if (!was_error_set)
19652  {
19653  PT_ERRORc (parser, expr, er_msg ());
19654  }
19655 
19656  expr->next = expr_next;
19657  return expr;
19658  }
19659 
19660  if (result)
19661  {
19662  result->line_number = line;
19663  result->column_number = column;
19664  result->alias_print = alias_print;
19665  result->flag.is_hidden_column = is_hidden_column;
19666  result->flag.is_value_query = expr->flag.is_value_query;
19667 
19668  if (result != expr)
19669  {
19670  if (alias_print == NULL || (PT_IS_VALUE_NODE (result) && !result->info.value.text))
19671  {
19672  /* print expr to alias_print */
19673  expr->alias_print = NULL;
19674  PT_NODE_PRINT_TO_ALIAS (parser, expr, PT_CONVERT_RANGE);
19675  }
19676  if (alias_print == NULL)
19677  {
19678  alias_print = expr->alias_print;
19679  }
19680  if (result->alias_print == NULL && expr->flag.is_alias_enabled_expr)
19681  {
19682  result->alias_print = pt_append_string (parser, NULL, alias_print);
19683  }
19684  if (PT_IS_VALUE_NODE (result) && !result->info.value.text)
19685  {
19686  result->info.value.text = pt_append_string (parser, NULL, expr->alias_print);
19687  }
19688  if (PT_IS_VALUE_NODE (result))
19689  {
19690  result->info.value.is_collate_allowed = true;
19691  }
19692  parser_free_tree (parser, expr);
19693  }
19694 
19695  result->next = expr_next;
19696 
19697  if (result->type_enum != PT_TYPE_NA && result->type_enum != PT_TYPE_NULL)
19698  {
19699  result->type_enum = result_type;
19700  }
19701 
19702  if (result->node_type == PT_EXPR)
19703  {
19704  result->info.expr.location = location;
19705  }
19706  else if (result->node_type == PT_VALUE)
19707  {
19708  result->info.value.location = location;
19709  }
19710 
19711  return result;
19712  }
19713  else
19714  {
19715  expr->next = expr_next;
19716  return expr;
19717  }
19718 }
19719 
19720 /*
19721  * pt_evaluate_function_w_args () - evaluate the function to a DB_VALUE
19722  * return: 1, if successful,
19723  * 0, if not successful.
19724  * parser(in): parser global context info for reentrancy
19725  * fcode(in): function code
19726  * args(in): array of arguments' values
19727  * num_args(in): number of arguments
19728  * result(out): result value of function (if evaluated)
19729  */
19730 int
19731 pt_evaluate_function_w_args (PARSER_CONTEXT * parser, FUNC_TYPE fcode, DB_VALUE * args[], const int num_args,
19732  DB_VALUE * result)
19733 {
19734  int error = NO_ERROR, i;
19735 
19736  assert (parser != NULL);
19737  assert (result != NULL);
19738 
19739  if (!result)
19740  {
19741  return 0;
19742  }
19743 
19744  /* init array vars */
19745  for (i = 0; i < num_args; i++)
19746  {
19747  assert (args[i] != NULL);
19748  }
19749 
19750  switch (fcode)
19751  {
19752  case F_INSERT_SUBSTRING:
19753  error = db_string_insert_substring (args[0], args[1], args[2], args[3], result);
19754  if (error != NO_ERROR)
19755  {
19756  return 0;
19757  }
19758  break;
19759 
19760  case F_ELT:
19761  error = db_string_elt (result, args, num_args);
19762  if (error != NO_ERROR)
19763  {
19764  return 0;
19765  }
19766  break;
19767 
19768  case F_JSON_ARRAY:
19769  error = db_evaluate_json_array (result, args, num_args);
19770  break;
19771 
19772  case F_JSON_ARRAY_APPEND:
19773  error = db_evaluate_json_array_append (result, args, num_args);
19774  break;
19775 
19776  case F_JSON_ARRAY_INSERT:
19777  error = db_evaluate_json_array_insert (result, args, num_args);
19778  break;
19779 
19780  case F_JSON_CONTAINS:
19781  error = db_evaluate_json_contains (result, args, num_args);
19782  break;
19783 
19784  case F_JSON_CONTAINS_PATH:
19785  error = db_evaluate_json_contains_path (result, args, num_args);
19786  break;
19787 
19788  case F_JSON_DEPTH:
19789  error = db_evaluate_json_depth (result, args, num_args);
19790  break;
19791 
19792  case F_JSON_EXTRACT:
19793  error = db_evaluate_json_extract (result, args, num_args);
19794  break;
19795 
19796  case F_JSON_GET_ALL_PATHS:
19797  error = db_evaluate_json_get_all_paths (result, args, num_args);
19798  break;
19799 
19800  case F_JSON_INSERT:
19801  error = db_evaluate_json_insert (result, args, num_args);
19802  break;
19803 
19804  case F_JSON_KEYS:
19805  error = db_evaluate_json_keys (result, args, num_args);
19806  break;
19807 
19808  case F_JSON_LENGTH:
19809  error = db_evaluate_json_length (result, args, num_args);
19810  break;
19811 
19812  case F_JSON_MERGE:
19813  error = db_evaluate_json_merge_preserve (result, args, num_args);
19814  break;
19815 
19816  case F_JSON_MERGE_PATCH:
19817  error = db_evaluate_json_merge_patch (result, args, num_args);
19818  break;
19819 
19820  case F_JSON_OBJECT:
19821  error = db_evaluate_json_object (result, args, num_args);
19822  break;
19823 
19824  case F_JSON_PRETTY:
19825  error = db_evaluate_json_pretty (result, args, num_args);
19826  break;
19827 
19828  case F_JSON_QUOTE:
19829  error = db_evaluate_json_quote (result, args, num_args);
19830  break;
19831 
19832  case F_JSON_REPLACE:
19833  error = db_evaluate_json_replace (result, args, num_args);
19834  break;
19835 
19836  case F_JSON_REMOVE:
19837  error = db_evaluate_json_remove (result, args, num_args);
19838  break;
19839 
19840  case F_JSON_SEARCH:
19841  error = db_evaluate_json_search (result, args, num_args);
19842  break;
19843 
19844  case F_JSON_SET:
19845  error = db_evaluate_json_set (result, args, num_args);
19846  break;
19847 
19848  case F_JSON_TYPE:
19849  error = db_evaluate_json_type_dbval (result, args, num_args);
19850  break;
19851 
19852  case F_JSON_UNQUOTE:
19853  error = db_evaluate_json_unquote (result, args, num_args);
19854  break;
19855 
19856  case F_JSON_VALID:
19857  error = db_evaluate_json_valid (result, args, num_args);
19858  break;
19859 
19860  case F_REGEXP_COUNT:
19861  error = db_string_regexp_count (result, args, num_args, NULL, NULL);
19862  break;
19863 
19864  case F_REGEXP_INSTR:
19865  error = db_string_regexp_instr (result, args, num_args, NULL, NULL);
19866  break;
19867 
19868  case F_REGEXP_LIKE:
19869  error = db_string_regexp_like (result, args, num_args, NULL, NULL);
19870  break;
19871 
19872  case F_REGEXP_REPLACE:
19873  error = db_string_regexp_replace (result, args, num_args, NULL, NULL);
19874  break;
19875 
19876  case F_REGEXP_SUBSTR:
19877  error = db_string_regexp_substr (result, args, num_args, NULL, NULL);
19878  break;
19879 
19880  default:
19881  /* a supported function doesn't have const folding code */
19882  assert (false);
19883  break;
19884  }
19885 
19886  if (error != NO_ERROR)
19887  {
19888  PT_ERRORc (parser, NULL, er_msg ());
19889  return 0;
19890  }
19891 
19892  return 1;
19893 }
19894 
19895 /*
19896  * pt_fold_const_function () - evaluate constant function
19897  * return: the evaluated expression, if successful,
19898  * unchanged function, if not successful.
19899  * parser(in): parser global context info for reentrancy
19900  * func(in): a parse tree representation of a possibly constant function
19901  */
19902 static PT_NODE *
19904 {
19905  PT_TYPE_ENUM result_type = PT_TYPE_NONE;
19906  PT_NODE *result = NULL;
19907  DB_VALUE dbval_res;
19908  PT_NODE *func_next;
19909  int line = 0, column = 0;
19910  short location;
19911  const char *alias_print = NULL;
19912 
19913  if (func == NULL)
19914  {
19915  return func;
19916  }
19917 
19918  if (func->node_type != PT_FUNCTION)
19919  {
19920  return func;
19921  }
19922 
19923  /* FUNCTION type set consisting of all constant values is changed to VALUE type set
19924  e.g.) (col1,1) in (..) and col1=1 -> qo_reduce_equality_terms() -> function type (1,1) -> value type (1,1) */
19925  if (pt_is_set_type (func) && func->info.function.function_type == F_SEQUENCE)
19926  {
19927  PT_NODE *func_arg = func->info.function.arg_list;
19928  bool is_const_multi_col = true;
19929 
19930  for ( /* none */ ; func_arg; func_arg = func_arg->next)
19931  {
19932  if (func_arg && func_arg->node_type != PT_VALUE)
19933  {
19934  is_const_multi_col = false;
19935  break;
19936  }
19937  }
19938  if (is_const_multi_col)
19939  {
19940  func->node_type = PT_VALUE;
19941  func_arg = func->info.function.arg_list;
19942  memset (&(func->info), 0, sizeof (func->info));
19943  func->info.value.data_value.set = func_arg;
19944  func->type_enum = PT_TYPE_SEQUENCE;
19945  }
19946  }
19947 
19948  if (func->flag.do_not_fold)
19949  {
19950  return func;
19951  }
19952 
19953  if (func->info.function.function_type == PT_COUNT)
19954  {
19955  parser_node *arg_list = func->info.function.arg_list;
19956  /* do special constant folding; COUNT(1), COUNT(?), COUNT(:x), ... -> COUNT(*) */
19957  if (pt_is_const (arg_list) && !PT_IS_NULL_NODE (arg_list))
19958  {
19959  PT_MISC_TYPE all_or_distinct;
19960  all_or_distinct = func->info.function.all_or_distinct;
19961  if (func->info.function.function_type == PT_COUNT && all_or_distinct != PT_DISTINCT)
19962  {
19964  parser_free_tree (parser, arg_list);
19965  func->info.function.arg_list = NULL;
19966  }
19967  }
19968  func->type_enum = PT_TYPE_INTEGER;
19969  }
19970 
19971  /* only functions wrapped with expressions are supported */
19972  if (!pt_is_expr_wrapped_function (parser, func))
19973  {
19974  return func;
19975  }
19976 
19977  /* PT_FUNCTION doesn't have location attribute as PT_EXPR does temporary set location to 0 ( WHERE clause) */
19978  location = 0;
19979 
19980  db_make_null (&dbval_res);
19981 
19982  line = func->line_number;
19983  column = func->column_number;
19984  alias_print = func->alias_print;
19985  func_next = func->next;
19986  func->next = NULL;
19987  result_type = func->type_enum;
19988  result = func;
19989 
19990  if (pt_evaluate_function (parser, func, &dbval_res) == NO_ERROR)
19991  {
19992  result = pt_dbval_to_value (parser, &dbval_res);
19993  }
19994 
19995  pr_clear_value (&dbval_res);
19996 
19997  if (result)
19998  {
19999  if (result != func)
20000  {
20001  if (alias_print == NULL || (PT_IS_VALUE_NODE (result) && !result->info.value.text))
20002  {
20003  func->alias_print = NULL;
20004  PT_NODE_PRINT_TO_ALIAS (parser, func, PT_CONVERT_RANGE);
20005  }
20006  if (PT_IS_VALUE_NODE (result) && !result->info.value.text)
20007  {
20008  result->info.value.text = func->alias_print;
20009  }
20010  if (alias_print == NULL && func->flag.is_alias_enabled_expr)
20011  {
20012  alias_print = func->alias_print;
20013  }
20014 
20015  if (PT_IS_VALUE_NODE (result))
20016  {
20017  result->info.value.is_collate_allowed = true;
20018  }
20019 
20020  parser_free_tree (parser, func);
20021  }
20022 
20023  /* restore saved func attributes */
20024  result->next = func_next;
20025 
20026  if (result->type_enum != PT_TYPE_NA && result->type_enum != PT_TYPE_NULL)
20027  {
20028  result->type_enum = result_type;
20029  }
20030 
20031  result->line_number = line;
20032  result->column_number = column;
20033  result->alias_print = alias_print;
20034  if (result->node_type == PT_VALUE)
20035  {
20036  /* temporary set location to a 0 the location will be updated after const folding at the upper level : the
20037  * parent node is a PT_EXPR node with a PT_FUNCTION_HOLDER operator type */
20038  result->info.value.location = location;
20039  }
20040  }
20041 
20042  return result;
20043 } /* pt_fold_const_function() */
20044 
20045 /*
20046  * pt_evaluate_function () - evaluate constant function
20047  * return: NO_ERROR, if evaluation successfull,
20048  * an error code, if unsuccessful
20049  * parser(in): parser global context info for reentrancy
20050  * func(in): a parse tree representation of a possibly constant function
20051  * dbval_res(in/out): the result DB_VALUE of evaluation
20052  */
20053 int
20055 {
20056  PT_NODE *operand;
20057  DB_VALUE dummy, **arg_array;
20058  FUNC_TYPE fcode;
20059  int error = NO_ERROR, i;
20060  int num_args = 0;
20061  bool all_args_const = false;
20062 
20063  /* init array variables */
20064  arg_array = NULL;
20065 
20066  if (func == NULL)
20067  {
20068  return ER_FAILED;
20069  }
20070 
20071  if (func->node_type != PT_FUNCTION)
20072  {
20073  return ER_FAILED;
20074  }
20075 
20076  fcode = func->info.function.function_type;
20077  /* only functions wrapped with expressions are supported */
20078  if (!pt_is_expr_wrapped_function (parser, func))
20079  {
20080  return ER_FAILED;
20081  }
20082 
20083  db_make_null (dbval_res);
20084 
20085  /* count function's arguments */
20086  operand = func->info.function.arg_list;
20087  num_args = 0;
20088  while (operand)
20089  {
20090  ++num_args;
20091  operand = operand->next;
20092  }
20093 
20094  if (num_args != 0)
20095  {
20096  arg_array = (DB_VALUE **) calloc (num_args, sizeof (DB_VALUE *));
20097  if (arg_array == NULL)
20098  {
20099  goto end;
20100  }
20101  }
20102 
20103  /* convert all operands to DB_VALUE arguments */
20104  /* for some functions this may not be necessary : you need to break from this loop and solve them at next steps */
20105  all_args_const = true;
20106  operand = func->info.function.arg_list;
20107  for (i = 0; i < num_args; i++)
20108  {
20109  if (operand != NULL && operand->node_type == PT_VALUE)
20110  {
20111  DB_VALUE *arg = NULL;
20112 
20113  arg = pt_value_to_db (parser, operand);
20114  if (arg == NULL)
20115  {
20116  all_args_const = false;
20117  break;
20118  }
20119  else
20120  {
20121  arg_array[i] = arg;
20122  }
20123  }
20124  else
20125  {
20126  db_make_null (&dummy);
20127  arg_array[i] = &dummy;
20128  all_args_const = false;
20129  break;
20130  }
20131  operand = operand->next;
20132  }
20133 
20134  if (all_args_const && i == num_args)
20135  {
20136  TP_DOMAIN *domain;
20137 
20138  /* use the caching variant of this function ! */
20139  domain = pt_xasl_node_to_domain (parser, func);
20140 
20141  /* check if we received an error getting the domain */
20142  if (pt_has_error (parser))
20143  {
20144  pr_clear_value (dbval_res);
20145  error = ER_FAILED;
20146  goto end;
20147  }
20148 
20149  if (pt_evaluate_function_w_args (parser, fcode, arg_array, num_args, dbval_res) != 1)
20150  {
20151  error = ER_FAILED;
20152  goto end;
20153  }
20154  }
20155  else
20156  {
20157  error = ER_FAILED;
20158  }
20159 end:
20160  if (arg_array != NULL)
20161  {
20162  free_and_init (arg_array);
20163  }
20164 
20165  return error;
20166 }
20167 
20168 /*
20169  * pt_semantic_type () - sets data types for all expressions in a parse tree
20170  * and evaluates constant sub expressions
20171  * return:
20172  * parser(in):
20173  * tree(in/out):
20174  * sc_info_ptr(in):
20175  */
20176 
20177 PT_NODE *
20179 {
20180  SEMANTIC_CHK_INFO sc_info = { tree, NULL, 0, 0, 0, false, false };
20181 
20182  if (pt_has_error (parser))
20183  {
20184  return NULL;
20185  }
20186  if (sc_info_ptr == NULL)
20187  {
20188  sc_info_ptr = &sc_info;
20189  }
20190  /* do type checking */
20191  tree = parser_walk_tree (parser, tree, pt_eval_type_pre, sc_info_ptr, pt_eval_type, sc_info_ptr);
20192  /* do constant folding */
20193  tree = parser_walk_tree (parser, tree, pt_fold_constants_pre, NULL, pt_fold_constants_post, sc_info_ptr);
20194  if (pt_has_error (parser))
20195  {
20196  tree = NULL;
20197  }
20198 
20199  return tree;
20200 }
20201 
20202 
20203 /*
20204  * pt_class_name () - return the class name of a data_type node
20205  * return:
20206  * type(in): a data_type node
20207  */
20208 static const char *
20209 pt_class_name (const PT_NODE * type)
20210 {
20211  if (!type || type->node_type != PT_DATA_TYPE || !type->info.data_type.entity
20212  || type->info.data_type.entity->node_type != PT_NAME)
20213  {
20214  return NULL;
20215  }
20216  else
20217  {
20218  return type->info.data_type.entity->info.name.original;
20219  }
20220 }
20221 
20222 /*
20223  * pt_set_default_data_type () -
20224  * return:
20225  * parser(in):
20226  * type(in):
20227  * dtp(in):
20228  */
20229 static int
20231 {
20232  PT_NODE *dt;
20233  int error = NO_ERROR;
20234 
20235  dt = parser_new_node (parser, PT_DATA_TYPE);
20236  if (dt == NULL)
20237  {
20238  return ER_GENERIC_ERROR;
20239  }
20240 
20241  dt->type_enum = type;
20242  switch (type)
20243  {
20244  case PT_TYPE_CHAR:
20245  case PT_TYPE_VARCHAR:
20247  dt->info.data_type.units = (int) LANG_SYS_CODESET;
20249  break;
20250 
20251  case PT_TYPE_NCHAR:
20252  case PT_TYPE_VARNCHAR:
20254  dt->info.data_type.units = (int) LANG_SYS_CODESET;
20256  break;
20257 
20258  case PT_TYPE_BIT:
20259  case PT_TYPE_VARBIT:
20262  break;
20263 
20264  case PT_TYPE_NUMERIC:
20266  /*
20267  * FIX ME!! Is it the case that this will always happen in
20268  * zero-scale context? That's certainly the case when we're
20269  * coercing from integers, but what about floats and doubles?
20270  */
20271  dt->info.data_type.dec_precision = 0;
20272  break;
20273 
20274  default:
20275  PT_INTERNAL_ERROR (parser, "type check");
20276  error = ER_GENERIC_ERROR;
20277  break;
20278  }
20279 
20280  *dtp = dt;
20281  return error;
20282 }
20283 
20284 /*
20285  * pt_is_explicit_coerce_allowed_for_default_value () - check whether explicit coercion is allowed for default value
20286  * return: true, if explicit coercion is allowed
20287  * parser(in): parser context
20288  * data_type(in): data type to coerce
20289  * desired_type(in): desired type
20290  */
20291 static bool
20293  PT_TYPE_ENUM desired_type)
20294 {
20295  /* Complete this function with other types that allow explicit coerce for default value */
20296  if (PT_IS_NUMERIC_TYPE (data_type))
20297  {
20298  if (PT_IS_STRING_TYPE (desired_type))
20299  {
20300  /* We allow explicit coerce from integer to string types. */
20301  return true;
20302  }
20303  }
20304 
20305  return false;
20306 }
20307 
20308 /*
20309  * pt_coerce_value () - coerce a PT_VALUE into another PT_VALUE of compatible type
20310  * return: NO_ERROR on success, non-zero for ERROR
20311  * parser(in):
20312  * src(in): a pointer to the original PT_VALUE
20313  * dest(out): a pointer to the coerced PT_VALUE
20314  * desired_type(in): the desired type of the coerced result
20315  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20316  */
20317 int
20319 {
20320  return pt_coerce_value_internal (parser, src, dest, desired_type, data_type, false, true);
20321 }
20322 
20323 /*
20324  * pt_coerce_value_explicit () - coerce a PT_VALUE into another PT_VALUE of compatible type
20325  * return: NO_ERROR on success, non-zero for ERROR
20326  * parser(in):
20327  * src(in): a pointer to the original PT_VALUE
20328  * dest(out): a pointer to the coerced PT_VALUE
20329  * desired_type(in): the desired type of the coerced result
20330  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20331  */
20332 int
20334  PT_NODE * data_type)
20335 {
20336  return pt_coerce_value_internal (parser, src, dest, desired_type, data_type, true, false);
20337 }
20338 
20339 /*
20340  * pt_coerce_value_for_default_value () - coerce a PT_VALUE of DEFAULT into another PT_VALUE of compatible type
20341  * return: NO_ERROR on success, non-zero for ERROR
20342  * parser(in):
20343  * src(in): a pointer to the original PT_VALUE
20344  * dest(out): a pointer to the coerced PT_VALUE
20345  * desired_type(in): the desired type of the coerced result
20346  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20347  * default_expr_type(in): default expression identifier
20348  */
20349 int
20351  PT_NODE * data_type, DB_DEFAULT_EXPR_TYPE default_expr_type)
20352 {
20353  bool implicit_coercion;
20354 
20355  assert (src != NULL && dest != NULL);
20356 
20357  if (default_expr_type == DB_DEFAULT_NONE && src->node_type == PT_VALUE
20358  && pt_is_explicit_coerce_allowed_for_default_value (parser, src->type_enum, desired_type))
20359  {
20360  implicit_coercion = false; /* explicit coercion */
20361  }
20362  else
20363  {
20364  implicit_coercion = true;
20365  }
20366 
20367  return pt_coerce_value_internal (parser, src, dest, desired_type, data_type, true, implicit_coercion);
20368 }
20369 
20370 /*
20371  * pt_coerce_value_internal () - coerce a PT_VALUE into another PT_VALUE of compatible type
20372  * return: NO_ERROR on success, non-zero for ERROR
20373  * parser(in):
20374  * src(in): a pointer to the original PT_VALUE
20375  * dest(out): a pointer to the coerced PT_VALUE
20376  * desired_type(in): the desired type of the coerced result
20377  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20378  * check_string_precision(in): true, if needs to consider string precision
20379  * do_implicit_coercion(in): true for implicit coercion, false for explicit coercion
20380  */
20381 static int
20383  PT_NODE * data_type, bool check_string_precision, bool implicit_coercion)
20384 {
20385  PT_TYPE_ENUM original_type;
20386  PT_NODE *dest_next;
20387  int err = NO_ERROR;
20388  PT_NODE *temp = NULL;
20389  bool is_collation_change = false;
20390  bool has_type_string;
20391  bool is_same_type;
20392  DB_VALUE *db_src = NULL;
20393  bool need_src_clear = false;
20394 
20395  assert (src != NULL && dest != NULL);
20396 
20397  dest_next = dest->next;
20398 
20399  original_type = src->type_enum;
20400 
20401  if (PT_HAS_COLLATION (original_type) && PT_HAS_COLLATION (desired_type) && src->data_type != NULL && data_type != NULL
20403  {
20404  is_collation_change = true;
20405  }
20406 
20407  has_type_string = PT_IS_STRING_TYPE (original_type);
20408  is_same_type = (original_type == desired_type && !is_collation_change);
20409 
20410  if ((is_same_type && original_type != PT_TYPE_NUMERIC && desired_type != PT_TYPE_OBJECT
20411  && (has_type_string == false || check_string_precision == false))
20412  || original_type == PT_TYPE_NA || original_type == PT_TYPE_NULL)
20413  {
20414  if (src != dest)
20415  {
20416  *dest = *src;
20417  dest->next = dest_next;
20418  }
20419  return NO_ERROR;
20420  }
20421 
20422  if (data_type == NULL && PT_IS_PARAMETERIZED_TYPE (desired_type)
20423  && (err = pt_set_default_data_type (parser, desired_type, &data_type) < 0))
20424  {
20425  return err;
20426  }
20427 
20428  if (is_same_type && src->data_type != NULL)
20429  {
20430  if ((original_type == PT_TYPE_NUMERIC
20431  && (src->data_type->info.data_type.precision == data_type->info.data_type.precision)
20433  || (has_type_string && src->data_type->info.data_type.precision == data_type->info.data_type.precision))
20434  {
20435  /* match */
20436  if (src != dest)
20437  {
20438  *dest = *src;
20439  dest->next = dest_next;
20440  }
20441  return NO_ERROR;
20442  }
20443  }
20444 
20445  if (original_type == PT_TYPE_NONE && src->node_type != PT_HOST_VAR)
20446  {
20447  if (src != dest)
20448  {
20449  *dest = *src;
20450  dest->next = dest_next;
20451  }
20452  dest->type_enum = desired_type;
20453  dest->data_type = parser_copy_tree_list (parser, data_type);
20454  /* don't return, in case further coercion is needed set original type to match desired type to avoid confusing
20455  * type check below */
20456  }
20457 
20458  switch (src->node_type)
20459  {
20460  case PT_HOST_VAR:
20461  /* binding of host variables may be delayed in the case of an esql PREPARE statement until an OPEN cursor or an
20462  * EXECUTE statement. in this case we seem to have no choice but to assume each host variable is typeless and can
20463  * be coerced into any desired type. */
20464  if (parser->flag.set_host_var == 0)
20465  {
20466  dest->type_enum = desired_type;
20467  dest->data_type = parser_copy_tree_list (parser, data_type);
20468  return NO_ERROR;
20469  }
20470 
20471  /* FALLTHRU */
20472 
20473  case PT_VALUE:
20474  {
20475  DB_VALUE db_dest;
20476  TP_DOMAIN *desired_domain;
20477 
20478  db_src = pt_value_to_db (parser, src);
20479  if (!db_src)
20480  {
20481  err = ER_GENERIC_ERROR;
20482  break;
20483  }
20484 
20485  db_make_null (&db_dest);
20486 
20487  /* be sure to use the domain caching versions */
20488  if (data_type)
20489  {
20490  desired_domain = pt_node_data_type_to_db_domain (parser, (PT_NODE *) data_type, desired_type);
20491  /* need a caching version of this function ? */
20492  if (desired_domain != NULL)
20493  {
20494  desired_domain = tp_domain_cache (desired_domain);
20495  }
20496  }
20497  else
20498  {
20499  desired_domain = pt_xasl_type_enum_to_domain (desired_type);
20500  }
20501 
20502  err = tp_value_cast (db_src, &db_dest, desired_domain, implicit_coercion);
20503 
20504  switch (err)
20505  {
20506  case DOMAIN_INCOMPATIBLE:
20508  break;
20509  case DOMAIN_OVERFLOW:
20510  err = ER_IT_DATA_OVERFLOW;
20511  break;
20512  case DOMAIN_ERROR:
20513  assert (er_errid () != NO_ERROR);
20514  err = er_errid ();
20515  break;
20516  default:
20517  break;
20518  }
20519 
20520  if (err == DOMAIN_COMPATIBLE && src->node_type == PT_HOST_VAR
20522  {
20523  /* when the type of the host variable is compatible to coerce, it is enough. NEVER change the node type to
20524  * PT_VALUE. */
20525  pr_clear_value (&db_dest);
20526  return NO_ERROR;
20527  }
20528 
20530  {
20531  if (err == NO_ERROR)
20532  {
20533  (void) pr_clear_value (db_src);
20534  }
20535  else
20536  {
20537  /* still needs db_src to print the message */
20538  need_src_clear = true;
20539  }
20540  }
20541 
20542  if (err >= 0)
20543  {
20544  temp = pt_dbval_to_value (parser, &db_dest);
20545  (void) pr_clear_value (&db_dest);
20546  if (!temp)
20547  {
20548  err = ER_GENERIC_ERROR;
20549  }
20550  else
20551  {
20552  temp->line_number = dest->line_number;
20553  temp->column_number = dest->column_number;
20554  temp->alias_print = dest->alias_print;
20558 
20559  // clear dest before overwriting; make sure data_type is not affected
20560  if (data_type == dest->data_type)
20561  {
20562  dest->data_type = NULL;
20563  }
20564  parser_clear_node (parser, dest);
20565 
20566  *dest = *temp;
20567  if (data_type != NULL)
20568  {
20569  dest->data_type = parser_copy_tree_list (parser, data_type);
20570  if (dest->data_type == NULL)
20571  {
20572  err = ER_GENERIC_ERROR;
20573  }
20574  }
20575  dest->next = dest_next;
20577  parser_free_node (parser, temp);
20578  }
20579  }
20580  }
20581  break;
20582 
20583  case PT_FUNCTION:
20584  if (src == dest)
20585  {
20586  switch (src->info.function.function_type)
20587  {
20588  case F_MULTISET:
20589  case F_SEQUENCE:
20590  switch (desired_type)
20591  {
20592  case PT_TYPE_SET:
20593  dest->info.function.function_type = F_SET;
20594  dest->type_enum = PT_TYPE_SET;
20595  break;
20596  case PT_TYPE_SEQUENCE:
20598  dest->type_enum = PT_TYPE_SEQUENCE;
20599  break;
20600  case PT_TYPE_MULTISET:
20602  dest->type_enum = PT_TYPE_MULTISET;
20603  break;
20604  default:
20605  break;
20606  }
20607  break;
20608 
20609  case F_TABLE_MULTISET:
20610  case F_TABLE_SEQUENCE:
20611  switch (desired_type)
20612  {
20613  case PT_TYPE_SET:
20615  dest->type_enum = PT_TYPE_SET;
20616  break;
20617  case PT_TYPE_SEQUENCE:
20619  dest->type_enum = PT_TYPE_SEQUENCE;
20620  break;
20621  case PT_TYPE_MULTISET:
20623  dest->type_enum = PT_TYPE_MULTISET;
20624  break;
20625  default:
20626  break;
20627  }
20628  break;
20629 
20630  default:
20631  break;
20632  }
20633  }
20634  break;
20635 
20636  default:
20637  err = ((pt_common_type (desired_type, src->type_enum) == PT_TYPE_NONE) ? ER_IT_INCOMPATIBLE_DATATYPE : NO_ERROR);
20638  break;
20639  }
20640 
20641  if (err == ER_IT_DATA_OVERFLOW)
20642  {
20644  pt_short_print (parser, src), pt_show_type_enum (desired_type));
20645  }
20646  else if (err < 0)
20647  {
20649  pt_short_print (parser, src),
20650  (desired_type == PT_TYPE_OBJECT ? pt_class_name (data_type) : pt_show_type_enum (desired_type)));
20651  }
20652 
20653  if (need_src_clear)
20654  {
20655  (void) pr_clear_value (db_src);
20656  }
20657 
20658  return err;
20659 }
20660 
20661 #if defined(ENABLE_UNUSED_FUNCTION)
20662 /*
20663  * generic_func_casecmp () -
20664  * return:
20665  * a(in):
20666  * b(in):
20667  */
20668 static int
20669 generic_func_casecmp (const void *a, const void *b)
20670 {
20671  return intl_identifier_casecmp (((const GENERIC_FUNCTION_RECORD *) a)->function_name,
20672  ((const GENERIC_FUNCTION_RECORD *) b)->function_name);
20673 }
20674 
20675 /*
20676  * init_generic_funcs () -
20677  * return:
20678  * void(in):
20679  */
20680 static void
20681 init_generic_funcs (void)
20682 {
20683  qsort (pt_Generic_functions, (sizeof (pt_Generic_functions) / sizeof (GENERIC_FUNCTION_RECORD)),
20684  sizeof (GENERIC_FUNCTION_RECORD), &generic_func_casecmp);
20685  pt_Generic_functions_sorted = 1;
20686 }
20687 #endif /* ENABLE_UNUSED_FUNCTION */
20688 
20689 /*
20690  * pt_type_generic_func () - Searches the generic_funcs table to find
20691  * the given generic function
20692  * return: 1 if the generic function is defined, 0 otherwise
20693  * parser(in):
20694  * node(in):
20695  */
20696 
20697 int
20699 {
20700 #if !defined(ENABLE_UNUSED_FUNCTION)
20701  /* If you want to use generic function, remove this block. */
20702  return 0;
20703 #else /* !ENABLE_UNUSED_FUNCTION */
20704  GENERIC_FUNCTION_RECORD *record_p, key;
20705  PT_NODE *offset;
20706 
20707  if (!pt_Generic_functions_sorted)
20708  {
20709  init_generic_funcs ();
20710  }
20711 
20712  if (node->node_type != PT_FUNCTION || node->info.function.function_type != PT_GENERIC
20713  || !node->info.function.generic_name)
20714  {
20715  return 0; /* this is not a generic function */
20716  }
20717 
20718  /* Check first to see if the function exists in our table. */
20719  key.function_name = node->info.function.generic_name;
20720  record_p =
20721  (GENERIC_FUNCTION_RECORD *) bsearch (&key, pt_Generic_functions,
20722  (sizeof (pt_Generic_functions) / sizeof (GENERIC_FUNCTION_RECORD)),
20723  sizeof (GENERIC_FUNCTION_RECORD), &generic_func_casecmp);
20724  if (!record_p)
20725  {
20726  return 0; /* we can't find it */
20727  }
20728 
20729  offset = parser_new_node (parser, PT_VALUE);
20730  if (offset == NULL)
20731  {
20732  return 0;
20733  }
20734 
20735  offset->type_enum = PT_TYPE_INTEGER;
20736  offset->info.value.data_value.i = record_p->func_ptr_offset;
20737  node->info.function.arg_list = parser_append_node (node->info.function.arg_list, offset);
20738 
20739  /* type the node */
20740  pt_string_to_data_type (parser, record_p->return_type, node);
20741 
20742  return 1;
20743 #endif /* ENABLE_UNUSED_FUNCTION */
20744 }
20745 
20746 
20747 /*
20748  * pt_compare_bounds_to_value () - compare constant value to base type
20749  * boundaries. If value is out of bounds, we already know the
20750  * result of a logical comparison (<, >, <=, >=, ==)
20751  * return: null or logical value node set to true or false.
20752  * parser(in): parse tree
20753  * expr(in): logical expression to be examined
20754  * op(in): expression operator
20755  * lhs_type(in): type of left hand operand
20756  * rhs_val(in): value of right hand operand
20757  * rhs_type(in): type of right hand operand
20758  *
20759  * Note :
20760  * This function coerces a PT_VALUE to another PT_VALUE of compatible type.
20761  */
20762 
20763 static PT_NODE *
20765  DB_VALUE * rhs_val, PT_TYPE_ENUM rhs_type)
20766 {
20767  bool lhs_less = false;
20768  bool lhs_greater = false;
20769  bool always_false = false;
20770  bool always_false_due_to_null = false;
20771  bool always_true = false;
20772  PT_NODE *result = expr;
20773  double dtmp;
20774 
20775  /* we can't determine anything if the types are the same */
20776  if (lhs_type == rhs_type)
20777  {
20778  return result;
20779  }
20780 
20781  /* check if op is always false due to null */
20782  if (op != PT_IS_NULL && op != PT_IS_NOT_NULL)
20783  {
20784  if (DB_IS_NULL (rhs_val) && rhs_type != PT_TYPE_SET && rhs_type != PT_TYPE_SEQUENCE
20785  && rhs_type != PT_TYPE_MULTISET)
20786  {
20787  always_false_due_to_null = true;
20788  goto end;
20789  }
20790  }
20791 
20792  /* we only allow PT_EQ, PT_GT, PT_GE, PT_LT, PT_LE. */
20793  if (op != PT_EQ && op != PT_GT && op != PT_GE && op != PT_LT && op != PT_LE)
20794  {
20795  return result;
20796  }
20797 
20798  /* we need to extend the following to compare dates and times, but probably not until we make the ranges of PT_* and
20799  * DB_* the same */
20800  switch (lhs_type)
20801  {
20802  case PT_TYPE_SMALLINT:
20803  switch (rhs_type)
20804  {
20805  case PT_TYPE_INTEGER:
20806  if (db_get_int (rhs_val) > DB_INT16_MAX)
20807  lhs_less = true;
20808  else if (db_get_int (rhs_val) < DB_INT16_MIN)
20809  lhs_greater = true;
20810  break;
20811  case PT_TYPE_BIGINT:
20812  if (db_get_bigint (rhs_val) > DB_INT16_MAX)
20813  lhs_less = true;
20814  else if (db_get_bigint (rhs_val) < DB_INT16_MIN)
20815  lhs_greater = true;
20816  break;
20817  case PT_TYPE_FLOAT:
20818  if (db_get_float (rhs_val) > DB_INT16_MAX)
20819  lhs_less = true;
20820  else if (db_get_float (rhs_val) < DB_INT16_MIN)
20821  lhs_greater = true;
20822  break;
20823 
20824  case PT_TYPE_DOUBLE:
20825  if (db_get_double (rhs_val) > DB_INT16_MAX)
20826  lhs_less = true;
20827  else if (db_get_double (rhs_val) < DB_INT16_MIN)
20828  lhs_greater = true;
20829  break;
20830 
20831  case PT_TYPE_NUMERIC:
20832  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
20833  if (dtmp > DB_INT16_MAX)
20834  lhs_less = true;
20835  else if (dtmp < DB_INT16_MIN)
20836  lhs_greater = true;
20837  break;
20838 
20839  case PT_TYPE_MONETARY:
20840  dtmp = (db_get_monetary (rhs_val))->amount;
20841  if (dtmp > DB_INT16_MAX)
20842  lhs_less = true;
20843  else if (dtmp < DB_INT16_MIN)
20844  lhs_greater = true;
20845  break;
20846 
20847  default:
20848  break;
20849  }
20850  break;
20851 
20852  case PT_TYPE_INTEGER:
20853  switch (rhs_type)
20854  {
20855  case PT_TYPE_BIGINT:
20856  if (db_get_bigint (rhs_val) > DB_INT32_MAX)
20857  lhs_less = true;
20858  else if (db_get_bigint (rhs_val) < DB_INT32_MIN)
20859  lhs_greater = true;
20860  break;
20861  case PT_TYPE_FLOAT:
20862  if (db_get_float (rhs_val) > DB_INT32_MAX)
20863  lhs_less = true;
20864  else if (db_get_float (rhs_val) < DB_INT32_MIN)
20865  lhs_greater = true;
20866  break;
20867 
20868  case PT_TYPE_DOUBLE:
20869  if (db_get_double (rhs_val) > DB_INT32_MAX)
20870  lhs_less = true;
20871  else if (db_get_double (rhs_val) < DB_INT32_MIN)
20872  lhs_greater = true;
20873  break;
20874 
20875  case PT_TYPE_NUMERIC:
20876  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
20877  if (dtmp > DB_INT32_MAX)
20878  lhs_less = true;
20879  else if (dtmp < DB_INT32_MIN)
20880  lhs_greater = true;
20881  break;
20882 
20883  case PT_TYPE_MONETARY:
20884  dtmp = (db_get_monetary (rhs_val))->amount;
20885  if (dtmp > DB_INT32_MAX)
20886  lhs_less = true;
20887  else if (dtmp < DB_INT32_MIN)
20888  lhs_greater = true;
20889  break;
20890  default:
20891  break;
20892  }
20893  break;
20894 
20895  case PT_TYPE_BIGINT:
20896  switch (rhs_type)
20897  {
20898  case PT_TYPE_FLOAT:
20899  if (db_get_float (rhs_val) > DB_BIGINT_MAX)
20900  lhs_less = true;
20901  else if (db_get_float (rhs_val) < DB_BIGINT_MIN)
20902  lhs_greater = true;
20903  break;
20904  case PT_TYPE_DOUBLE:
20905  if (db_get_double (rhs_val) > DB_BIGINT_MAX)
20906  lhs_less = true;
20907  else if (db_get_double (rhs_val) < DB_BIGINT_MIN)
20908  lhs_greater = true;
20909  break;
20910  case PT_TYPE_NUMERIC:
20911  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
20912  if (dtmp > DB_BIGINT_MAX)
20913  lhs_less = true;
20914  else if (dtmp < DB_BIGINT_MIN)
20915  lhs_greater = true;
20916  break;
20917  case PT_TYPE_MONETARY:
20918  dtmp = (db_get_monetary (rhs_val))->amount;
20919  if (dtmp > DB_BIGINT_MAX)
20920  lhs_less = true;
20921  else if (dtmp < DB_BIGINT_MIN)
20922  lhs_greater = true;
20923  break;
20924  default:
20925  break;
20926  }
20927  break;
20928 
20929  case PT_TYPE_FLOAT:
20930  switch (rhs_type)
20931  {
20932  case PT_TYPE_DOUBLE:
20933  if (db_get_double (rhs_val) > FLT_MAX)
20934  lhs_less = true;
20935  else if (db_get_double (rhs_val) < -(FLT_MAX))
20936  lhs_greater = true;
20937  break;
20938 
20939  case PT_TYPE_NUMERIC:
20940  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
20941  if (dtmp > FLT_MAX)
20942  lhs_less = true;
20943  else if (dtmp < -(FLT_MAX))
20944  lhs_greater = true;
20945  break;
20946 
20947  case PT_TYPE_MONETARY:
20948  dtmp = (db_get_monetary (rhs_val))->amount;
20949  if (dtmp > FLT_MAX)
20950  lhs_less = true;
20951  else if (dtmp < -(FLT_MAX))
20952  lhs_greater = true;
20953  break;
20954 
20955  default:
20956  break;
20957  }
20958  break;
20959 
20960  default:
20961  break;
20962  }
20963 
20964  if (lhs_less)
20965  {
20966  if (op == PT_EQ || op == PT_GT || op == PT_GE)
20967  {
20968  always_false = true;
20969  }
20970  else if (op == PT_LT || op == PT_LE)
20971  {
20972  always_true = true;
20973  }
20974  }
20975  else if (lhs_greater)
20976  {
20977  if (op == PT_EQ || op == PT_LT || op == PT_LE)
20978  {
20979  always_false = true;
20980  }
20981  else if (op == PT_GT || op == PT_GE)
20982  {
20983  always_true = true;
20984  }
20985  }
20986 
20987 end:
20988  if (always_false)
20989  {
20990  result = parser_new_node (parser, PT_VALUE);
20991  if (result == NULL)
20992  {
20993  PT_INTERNAL_ERROR (parser, "allocate new node");
20994  return NULL;
20995  }
20996 
20997  result->type_enum = PT_TYPE_LOGICAL;
20998  result->info.value.data_value.i = false;
20999  result->info.value.location = expr->info.expr.location;
21000  (void) pt_value_to_db (parser, result);
21001  }
21002  else if (always_false_due_to_null)
21003  {
21004  result = parser_new_node (parser, PT_VALUE);
21005  if (result == NULL)
21006  {
21007  PT_INTERNAL_ERROR (parser, "allocate new node");
21008  return NULL;
21009  }
21010 
21011  result->type_enum = PT_TYPE_NULL;
21012  result->info.value.location = expr->info.expr.location;
21013  (void) pt_value_to_db (parser, result);
21014  }
21015  else if (always_true)
21016  {
21017  result = parser_new_node (parser, PT_VALUE);
21018  if (result == NULL)
21019  {
21020  PT_INTERNAL_ERROR (parser, "allocate new node");
21021  return NULL;
21022  }
21023 
21024  result->type_enum = PT_TYPE_LOGICAL;
21025  result->info.value.data_value.i = true;
21026  result->info.value.location = expr->info.expr.location;
21027  (void) pt_value_to_db (parser, result);
21028  }
21029 
21030  return result;
21031 }
21032 
21033 
21034 /*
21035  * pt_converse_op () - Figure out the converse of a relational operator,
21036  * so that we can flip a relational expression into a canonical form
21037  * return:
21038  * op(in):
21039  */
21040 
21041 PT_OP_TYPE
21043 {
21044  switch (op)
21045  {
21046  case PT_EQ:
21047  return PT_EQ;
21048  case PT_LT:
21049  return PT_GT;
21050  case PT_LE:
21051  return PT_GE;
21052  case PT_GT:
21053  return PT_LT;
21054  case PT_GE:
21055  return PT_LE;
21056  case PT_NE:
21057  return PT_NE;
21058  default:
21059  return (PT_OP_TYPE) 0;
21060  }
21061 }
21062 
21063 
21064 /*
21065  * pt_is_between_range_op () -
21066  * return:
21067  * op(in):
21068  */
21069 int
21071 {
21072  switch (op)
21073  {
21074  case PT_BETWEEN_AND:
21075  case PT_BETWEEN_GE_LE:
21076  case PT_BETWEEN_GE_LT:
21077  case PT_BETWEEN_GT_LE:
21078  case PT_BETWEEN_GT_LT:
21079  case PT_BETWEEN_EQ_NA:
21080  case PT_BETWEEN_INF_LE:
21081  case PT_BETWEEN_INF_LT:
21082  case PT_BETWEEN_GE_INF:
21083  case PT_BETWEEN_GT_INF:
21084  return 1;
21085  default:
21086  return 0;
21087  }
21088 }
21089 
21090 
21091 /*
21092  * pt_is_comp_op () -
21093  * return:
21094  * op(in):
21095  */
21096 int
21098 {
21099  switch (op)
21100  {
21101  case PT_LIKE:
21102  case PT_NOT_LIKE:
21103  case PT_IS_IN:
21104  case PT_IS_NOT_IN:
21105  case PT_IS_NULL:
21106  case PT_IS_NOT_NULL:
21107  case PT_IS:
21108  case PT_IS_NOT:
21109  case PT_EQ_SOME:
21110  case PT_NE_SOME:
21111  case PT_GE_SOME:
21112  case PT_GT_SOME:
21113  case PT_LT_SOME:
21114  case PT_LE_SOME:
21115  case PT_EQ_ALL:
21116  case PT_NE_ALL:
21117  case PT_GE_ALL:
21118  case PT_GT_ALL:
21119  case PT_LT_ALL:
21120  case PT_LE_ALL:
21121  case PT_EQ:
21122  case PT_NE:
21123  case PT_GE:
21124  case PT_GT:
21125  case PT_LT:
21126  case PT_LE:
21127  case PT_NULLSAFE_EQ:
21128  case PT_GT_INF:
21129  case PT_LT_INF:
21130  case PT_BETWEEN:
21131  case PT_BETWEEN_AND:
21132  case PT_BETWEEN_GE_LE:
21133  case PT_BETWEEN_GE_LT:
21134  case PT_BETWEEN_GT_LE:
21135  case PT_BETWEEN_GT_LT:
21136  case PT_BETWEEN_EQ_NA:
21137  case PT_BETWEEN_INF_LE:
21138  case PT_BETWEEN_INF_LT:
21139  case PT_BETWEEN_GE_INF:
21140  case PT_BETWEEN_GT_INF:
21141  case PT_RANGE:
21142  return 1;
21143  default:
21144  return 0;
21145  }
21146 }
21147 
21148 
21149 /*
21150  * pt_negate_op () -
21151  * return:
21152  * op(in):
21153  */
21154 PT_OP_TYPE
21156 {
21157  switch (op)
21158  {
21159  case PT_EQ:
21160  return PT_NE;
21161  case PT_NE:
21162  return PT_EQ;
21163  case PT_SETEQ:
21164  return PT_SETNEQ;
21165  case PT_SETNEQ:
21166  return PT_SETEQ;
21167  case PT_GT:
21168  return PT_LE;
21169  case PT_GE:
21170  return PT_LT;
21171  case PT_LT:
21172  return PT_GE;
21173  case PT_LE:
21174  return PT_GT;
21175  case PT_BETWEEN:
21176  return PT_NOT_BETWEEN;
21177  case PT_NOT_BETWEEN:
21178  return PT_BETWEEN;
21179  case PT_IS_IN:
21180  return PT_IS_NOT_IN;
21181  case PT_IS_NOT_IN:
21182  return PT_IS_IN;
21183  case PT_LIKE:
21184  return PT_NOT_LIKE;
21185  case PT_NOT_LIKE:
21186  return PT_LIKE;
21187  case PT_RLIKE:
21188  return PT_NOT_RLIKE;
21189  case PT_NOT_RLIKE:
21190  return PT_RLIKE;
21191  case PT_RLIKE_BINARY:
21192  return PT_NOT_RLIKE_BINARY;
21193  case PT_NOT_RLIKE_BINARY:
21194  return PT_RLIKE_BINARY;
21195  case PT_IS_NULL:
21196  return PT_IS_NOT_NULL;
21197  case PT_IS_NOT_NULL:
21198  return PT_IS_NULL;
21199  case PT_EQ_SOME:
21200  return PT_NE_ALL;
21201  case PT_NE_SOME:
21202  return PT_EQ_ALL;
21203  case PT_GT_SOME:
21204  return PT_LE_ALL;
21205  case PT_GE_SOME:
21206  return PT_LT_ALL;
21207  case PT_LT_SOME:
21208  return PT_GE_ALL;
21209  case PT_LE_SOME:
21210  return PT_GT_ALL;
21211  case PT_EQ_ALL:
21212  return PT_NE_SOME;
21213  case PT_NE_ALL:
21214  return PT_EQ_SOME;
21215  case PT_GT_ALL:
21216  return PT_LE_SOME;
21217  case PT_GE_ALL:
21218  return PT_LT_SOME;
21219  case PT_LT_ALL:
21220  return PT_GE_SOME;
21221  case PT_LE_ALL:
21222  return PT_GT_SOME;
21223  case PT_IS:
21224  return PT_IS_NOT;
21225  case PT_IS_NOT:
21226  return PT_IS;
21227  default:
21228  return (PT_OP_TYPE) 0;
21229  }
21230 }
21231 
21232 
21233 /*
21234  * pt_comp_to_between_op () -
21235  * return:
21236  * left(in):
21237  * right(in):
21238  * type(in):
21239  * between(out):
21240  */
21241 int
21243 {
21244  size_t i;
21245 
21246  for (i = 0; i < COMPARE_BETWEEN_OPERATOR_COUNT; i++)
21247  {
21248  if (left == pt_Compare_between_operator_table[i].left && right == pt_Compare_between_operator_table[i].right)
21249  {
21250  *between = pt_Compare_between_operator_table[i].between;
21251 
21252  return 0;
21253  }
21254  }
21255 
21256  if (type == PT_RANGE_INTERSECTION)
21257  { /* range intersection */
21258  if ((left == PT_GE && right == PT_EQ) || (left == PT_EQ && right == PT_LE))
21259  {
21260  *between = PT_BETWEEN_EQ_NA;
21261  return 0;
21262  }
21263  }
21264 
21265  return -1;
21266 }
21267 
21268 
21269 /*
21270  * pt_between_to_comp_op () -
21271  * return:
21272  * between(in):
21273  * left(out):
21274  * right(out):
21275  */
21276 int
21278 {
21279  size_t i;
21280 
21281  for (i = 0; i < COMPARE_BETWEEN_OPERATOR_COUNT; i++)
21282  if (between == pt_Compare_between_operator_table[i].between)
21283  {
21284  *left = pt_Compare_between_operator_table[i].left;
21285  *right = pt_Compare_between_operator_table[i].right;
21286 
21287  return 0;
21288  }
21289 
21290  return -1;
21291 }
21292 
21293 /*
21294  * pt_get_equivalent_type_with_op () - get the type to which a node should be
21295  * converted to in order to match an expression
21296  * definition;
21297  * return : the new type
21298  * def_type(in) : the type defined in the expression signature
21299  * arg_type(in) : the type of the received expression argument
21300  * op(in) : operator
21301  *
21302  * Note : this is a wrapper for 'pt_get_equivalent_type' : the default
21303  * equivalent type may be overridden for certain operators
21304  */
21305 static PT_TYPE_ENUM
21307 {
21308  if (pt_is_op_hv_late_bind (op) && (def_type.type == pt_arg_type::GENERIC && arg_type == PT_TYPE_MAYBE))
21309  {
21310  /* leave undetermined type */
21311  return PT_TYPE_MAYBE;
21312  }
21313  return pt_get_equivalent_type (def_type, arg_type);
21314 }
21315 
21316 /*
21317  * pt_is_op_hv_late_bind () - checks if the operator is in the list of
21318  * operators that should perform late binding on
21319  * their host variable arguments
21320  *
21321  * return: true if arguments types should be mirrored
21322  * op(in): operator type
21323  *
21324  * Note: this functions is used by type inference algorithm to check if an
21325  * expression should leave its HV arguments as TYPE_MAYBE (the default
21326  * type inference behavior would be to match it with a concrete type
21327  * according to one of its signatures). Also, such expression is
21328  * wrapped with cast rather then its result type be forced to an
21329  * "expected domain" dictated by the expression context.
21330  */
21331 bool
21333 {
21334  switch (op)
21335  {
21336  case PT_ABS:
21337  case PT_CEIL:
21338  case PT_FLOOR:
21339  case PT_PLUS:
21340  case PT_DIVIDE:
21341  case PT_MODULUS:
21342  case PT_TIMES:
21343  case PT_MINUS:
21344  case PT_ROUND:
21345  case PT_TRUNC:
21346  case PT_UNARY_MINUS:
21347  case PT_EVALUATE_VARIABLE:
21348  case PT_DEFINE_VARIABLE:
21349  case PT_ADDTIME:
21350  case PT_TO_CHAR:
21351  case PT_HEX:
21352  case PT_CONV:
21353  case PT_ASCII:
21354  case PT_IFNULL:
21355  case PT_NVL:
21356  case PT_NVL2:
21357  case PT_COALESCE:
21358  case PT_NULLIF:
21359  case PT_LEAST:
21360  case PT_GREATEST:
21361  case PT_FROM_TZ:
21362  case PT_NEW_TIME:
21363  case PT_STR_TO_DATE:
21364  case PT_HOURF:
21365  case PT_MINUTEF:
21366  case PT_SECONDF:
21367  return true;
21368  default:
21369  return false;
21370  }
21371  return false;
21372 }
21373 
21374 /*
21375  * pt_wrap_expr_w_exp_dom_cast () - checks if the expression requires wrapping
21376  * with a cast to the type set in expected domain and performs the
21377  * wrap with cast if needed.
21378  *
21379  * return: new node (if wrap is performed), or unaltered node, if wrap is
21380  * not needed
21381  * parser(in): parser context
21382  * expr(in): expression node to be checked and wrapped
21383  */
21384 static PT_NODE *
21386 {
21387  /* expressions returning MAYBE, but with an expected domain are wrapped with cast */
21388  if (expr != NULL && expr->type_enum == PT_TYPE_MAYBE && pt_is_op_hv_late_bind (expr->info.expr.op)
21389  && expr->expected_domain != NULL)
21390  {
21391  PT_NODE *new_expr = NULL;
21392 
21393  if (expr->type_enum == PT_TYPE_ENUMERATION)
21394  {
21395  /* expressions should not return PT_TYPE_ENUMERATION */
21396  assert (false);
21397  PT_INTERNAL_ERROR (parser, "INVALID expected domain (PT_TYPE_ENUMERATION)");
21398  return NULL;
21399  }
21400 
21401  new_expr =
21404 
21405  if (new_expr != NULL)
21406  {
21407  /* reset expected domain of wrapped expression to NULL: it will be replaced at XASL generation with
21408  * DB_TYPE_VARIABLE domain */
21409  expr->expected_domain = NULL;
21410  expr = new_expr;
21411  }
21412  }
21413 
21414  return expr;
21415 }
21416 
21417 /*
21418  * pt_is_op_with_forced_common_type () - checks if the operator is in the list
21419  * of operators that should force its arguments to
21420  * the same type if none of the arguments has a
21421  * determined type
21422  *
21423  * return: true if arguments types should be mirrored
21424  * op(in): operator type
21425  *
21426  * Note: this functions is used by type inference algorithm
21427  */
21428 bool
21430 {
21431  switch (op)
21432  {
21433  case PT_IFNULL:
21434  case PT_COALESCE:
21435  case PT_NVL:
21436  case PT_NVL2:
21437  case PT_NULLIF:
21438  case PT_LEAST:
21439  case PT_GREATEST:
21440  case PT_BETWEEN:
21441  return true;
21442  default:
21443  return false;
21444  }
21445  return false;
21446 }
21447 
21448 /*
21449  * pt_check_const_fold_op_w_args () - checks if it is safe to perform constant
21450  * folding on the expression, given the
21451  * arguments' values
21452  * return: true if folding is allowed, false otherwise
21453  * op(in): a PT_OP_TYPE (the desired operation)
21454  * arg1(in): 1st db_value operand
21455  * arg2(in): 2nd db_value operand
21456  * arg3(in): 3rd db_value operand
21457  * domain(in): node domain
21458  *
21459  * Note : this function is used in context of constant folding to check if
21460  * folding an expression produces a large sized (string) result
21461  * which may cause performance problem
21462  */
21463 static bool
21465 {
21466  const int MAX_RESULT_SIZE_ON_CONST_FOLDING = 256;
21467  switch (op)
21468  {
21469  case PT_CAST:
21470  if (TP_DOMAIN_TYPE (domain) == DB_TYPE_CLOB || TP_DOMAIN_TYPE (domain) == DB_TYPE_BLOB)
21471  {
21472  return false;
21473  }
21474  break;
21475 
21476  case PT_SPACE:
21477  if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_INTEGER)
21478  {
21479  int count_i = db_get_int (arg1);
21480  if (count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21481  {
21482  return false;
21483  }
21484  }
21485  else if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_SHORT)
21486  {
21487  short count_sh = db_get_short (arg1);
21488  if (count_sh > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21489  {
21490  return false;
21491  }
21492  }
21493  else if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_BIGINT)
21494  {
21495  DB_BIGINT count_b = db_get_bigint (arg1);
21496  if (count_b > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21497  {
21498  return false;
21499  }
21500  }
21501  break;
21502 
21503  case PT_REPEAT:
21504  if (DB_VALUE_DOMAIN_TYPE (arg2) == DB_TYPE_INTEGER)
21505  {
21506  int count_i = db_get_int (arg2);
21508  {
21509  int arg1_len = db_get_string_size (arg1);
21510 
21511  if (arg1_len * count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21512  {
21513  return false;
21514  }
21515  }
21516  }
21517  break;
21518 
21519  case PT_LPAD:
21520  case PT_RPAD:
21521  /* check if constant folding is OK */
21522  if (DB_VALUE_DOMAIN_TYPE (arg2) == DB_TYPE_INTEGER)
21523  {
21524  int count_i = db_get_int (arg2);
21525  if (arg3 != NULL && QSTR_IS_ANY_CHAR (DB_VALUE_DOMAIN_TYPE (arg3)))
21526  {
21527  int arg3_len = db_get_string_size (arg3);
21528 
21529  if (arg3_len * count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21530  {
21531  return false;
21532  }
21533  }
21534  }
21535  break;
21536 
21537  default:
21538  break;
21539  }
21540 
21541  return true;
21542 }
21543 
21544 /*
21545  * pt_is_op_w_collation () - check if is required to check collation or
21546  * codeset of this operator
21547  *
21548  * return:
21549  * node(in): a parse tree node
21550  *
21551  */
21552 static bool
21554 {
21555  switch (op)
21556  {
21557  case PT_EQ:
21558  case PT_NE:
21559  case PT_GE:
21560  case PT_GT:
21561  case PT_LT:
21562  case PT_LE:
21563  case PT_NULLSAFE_EQ:
21564  case PT_BETWEEN:
21565  case PT_NOT_BETWEEN:
21566  case PT_BETWEEN_AND:
21567  case PT_BETWEEN_GE_LE:
21568  case PT_BETWEEN_GE_LT:
21569  case PT_BETWEEN_GT_LE:
21570  case PT_BETWEEN_GT_LT:
21571  case PT_CONCAT:
21572  case PT_CONCAT_WS:
21573  case PT_PLUS:
21574  case PT_LIKE:
21575  case PT_NOT_LIKE:
21576  case PT_SUBSTRING_INDEX:
21577  case PT_RPAD:
21578  case PT_LPAD:
21579  case PT_MID:
21580  case PT_SUBSTRING:
21581  case PT_REPLACE:
21582  case PT_TRANSLATE:
21583  case PT_COALESCE:
21584  case PT_STRCAT:
21585  case PT_TIME_FORMAT:
21586  case PT_DATE_FORMAT:
21587  case PT_TIMEF:
21588  case PT_DATEF:
21589  case PT_SETEQ:
21590  case PT_SETNEQ:
21591  case PT_SUBSET:
21592  case PT_SUBSETEQ:
21593  case PT_SUPERSET:
21594  case PT_SUPERSETEQ:
21595  case PT_GREATEST:
21596  case PT_LEAST:
21597  case PT_NULLIF:
21598  case PT_LOWER:
21599  case PT_UPPER:
21600  case PT_RTRIM:
21601  case PT_LTRIM:
21602  case PT_TRIM:
21603  case PT_LEFT:
21604  case PT_RIGHT:
21605  case PT_NVL:
21606  case PT_NVL2:
21607  case PT_IFNULL:
21608  case PT_IS_IN:
21609  case PT_IS_NOT_IN:
21610  case PT_EQ_SOME:
21611  case PT_NE_SOME:
21612  case PT_GE_SOME:
21613  case PT_GT_SOME:
21614  case PT_LT_SOME:
21615  case PT_LE_SOME:
21616  case PT_EQ_ALL:
21617  case PT_NE_ALL:
21618  case PT_GE_ALL:
21619  case PT_GT_ALL:
21620  case PT_LT_ALL:
21621  case PT_LE_ALL:
21622  case PT_FINDINSET:
21623  case PT_INSTR:
21624  case PT_LOCATE:
21625  case PT_POSITION:
21626  case PT_STRCMP:
21627  case PT_IF:
21628  case PT_FIELD:
21629  case PT_REVERSE:
21630  case PT_CONNECT_BY_ROOT:
21631  case PT_PRIOR:
21632  case PT_QPRIOR:
21633  case PT_INDEX_PREFIX:
21634  case PT_MINUS:
21635  return true;
21636  default:
21637  return false;
21638  }
21639  return false;
21640 }
21641 
21642 /*
21643  * pt_get_collation_info () - get the collation info of parse tree node
21644  *
21645  * return: true if node has collation
21646  * node(in): a parse tree node
21647  * coll_infer(out): collation inference data
21648  */
21649 bool
21650 pt_get_collation_info (const PT_NODE * node, PT_COLL_INFER * coll_infer)
21651 {
21652  bool has_collation = false;
21653 
21654  assert (node != NULL);
21655  assert (coll_infer != NULL);
21656 
21657  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
21658  coll_infer->codeset = LANG_COERCIBLE_CODESET;
21659  coll_infer->coll_id = LANG_COERCIBLE_COLL;
21660  coll_infer->can_force_cs = false;
21661 
21662  if (node->data_type != NULL)
21663  {
21664  if (PT_HAS_COLLATION (node->type_enum))
21665  {
21666  coll_infer->coll_id = node->data_type->info.data_type.collation_id;
21667  coll_infer->codeset = (INTL_CODESET) node->data_type->info.data_type.units;
21668  has_collation = true;
21669 
21671  {
21672  coll_infer->can_force_cs = true;
21673  }
21674  }
21675  }
21676  else if (node->expected_domain != NULL)
21677  {
21679  {
21680  coll_infer->coll_id = TP_DOMAIN_COLLATION (node->expected_domain);
21681  coll_infer->codeset = TP_DOMAIN_CODESET (node->expected_domain);
21682  has_collation = true;
21683 
21685  {
21686  coll_infer->can_force_cs = true;
21687  }
21688  }
21689  }
21690  else if (node->type_enum == PT_TYPE_MAYBE || (node->node_type == PT_VALUE && PT_HAS_COLLATION (node->type_enum)))
21691  {
21692  coll_infer->coll_id = LANG_SYS_COLLATION;
21693  coll_infer->codeset = LANG_SYS_CODESET;
21694  has_collation = true;
21695 
21696  if (node->type_enum == PT_TYPE_MAYBE)
21697  {
21698  coll_infer->can_force_cs = true;
21699  }
21700  }
21701 
21702  if (has_collation && PT_GET_COLLATION_MODIFIER (node) != -1)
21703  {
21705 
21706  assert (lc != NULL);
21707 
21708  coll_infer->coll_id = PT_GET_COLLATION_MODIFIER (node);
21709 
21710  if (node->data_type != NULL)
21711  {
21712  assert (node->data_type->info.data_type.units == lc->codeset);
21713  }
21714  else if (node->expected_domain != NULL)
21715  {
21717  }
21718 
21719  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
21720  coll_infer->can_force_cs = false;
21721  return has_collation;
21722  }
21723 
21724  switch (node->node_type)
21725  {
21726  case PT_VALUE:
21727  if (coll_infer->coll_id == LANG_COLL_BINARY)
21728  {
21730  }
21731  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
21732  {
21733  coll_infer->coerc_level = PT_COLLATION_L4_BIN_COERC;
21734  }
21735  else
21736  {
21737  coll_infer->coerc_level = PT_COLLATION_L4_COERC;
21738  }
21739  break;
21740 
21741  case PT_HOST_VAR:
21742  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
21743  coll_infer->can_force_cs = true;
21744  break;
21745 
21746  case PT_EXPR:
21747  if (node->info.expr.op == PT_CURRENT_USER || node->info.expr.op == PT_USER || node->info.expr.op == PT_DATABASE
21748  || node->info.expr.op == PT_SCHEMA || node->info.expr.op == PT_VERSION)
21749  {
21750  coll_infer->coerc_level = PT_COLLATION_L3_COERC;
21751  break;
21752  }
21753 
21754  if (node->info.expr.op == PT_EVALUATE_VARIABLE || node->info.expr.op == PT_DEFINE_VARIABLE)
21755  {
21756  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
21757  break;
21758  }
21759 
21761  {
21762  PT_COLL_INFER coll_infer_dummy;
21763  /* collation and codeset of wrapped CAST, but get coercibility from original node */
21764  pt_get_collation_info (node->info.expr.arg1, &coll_infer_dummy);
21765  coll_infer->coerc_level = coll_infer_dummy.coerc_level;
21766 
21768  {
21769  coll_infer->can_force_cs = true;
21770  }
21771  break;
21772  }
21773  /* fall through */
21774  case PT_SELECT:
21775  case PT_UNION:
21776  case PT_DIFFERENCE:
21777  case PT_INTERSECTION:
21778  case PT_FUNCTION:
21779  case PT_METHOD_CALL:
21780  if (coll_infer->coll_id == LANG_COLL_BINARY)
21781  {
21783  }
21784  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
21785  {
21786  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
21787  }
21788  else
21789  {
21790  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
21791  }
21792  break;
21793 
21794  case PT_NAME:
21796  {
21797  if (coll_infer->coll_id == LANG_COLL_BINARY)
21798  {
21800  }
21801  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
21802  {
21803  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
21804  }
21805  else
21806  {
21807  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
21808  }
21809  break;
21810  }
21811  else if (pt_is_input_parameter (node) || node->type_enum == PT_TYPE_MAYBE)
21812  {
21813  coll_infer->coerc_level = PT_COLLATION_L5_COERC;
21814  break;
21815  }
21816  /* Fall through */
21817  case PT_DOT_:
21818  if (coll_infer->coll_id == LANG_COLL_BINARY)
21819  {
21821  }
21822  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
21823  {
21824  coll_infer->coerc_level = PT_COLLATION_L1_BIN_COERC;
21825  }
21826  else
21827  {
21828  coll_infer->coerc_level = PT_COLLATION_L1_COERC;
21829  }
21830  break;
21831 
21832  default:
21833  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
21834  }
21835 
21836  return has_collation;
21837 }
21838 
21839 /*
21840  * pt_get_collation_info_for_collection_type () - get the collation info of a
21841  * parse tree node with collection type
21842  *
21843  * return: NO_COLLATION = node doesn't have collation;
21844  * HAS_COLLATION = node has collation
21845  * ERROR_COLLATION = node has multiple component types with
21846  * collation and collations are not compatible
21847  *
21848  * parser(in)
21849  * node(in): a parse tree node
21850  * coll_infer(out): collation inference data
21851  *
21852  */
21853 static COLLATION_RESULT
21855 {
21856  bool has_collation = false;
21857  bool is_collection_of_collection = false;
21858  bool first_element = true;
21859  int status_inner_collection;
21860 
21861  assert (node != NULL);
21862  assert (coll_infer != NULL);
21863 
21864  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
21865  coll_infer->codeset = LANG_COERCIBLE_CODESET;
21866  coll_infer->coll_id = LANG_COERCIBLE_COLL;
21867  coll_infer->can_force_cs = false;
21868 
21869  if (node->node_type == PT_HOST_VAR && node->expected_domain != NULL
21871  {
21872  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
21873  coll_infer->can_force_cs = true;
21874  return HAS_COLLATION;
21875  }
21876 
21878 
21879  if (node->data_type != NULL)
21880  {
21881  const PT_NODE *current_set_node = node;
21882 
21883  /* if node is a collection of collection, advance to the first element of it */
21884  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
21885  {
21886  if (((node->info.function.function_type == F_TABLE_SET)
21889  && (node->info.function.arg_list->node_type == PT_SELECT))
21890  {
21891  current_set_node = node->info.function.arg_list->info.query.q.select.list;
21892  is_collection_of_collection = true;
21893  }
21894  else if ((node->info.function.function_type == F_SET) || (node->info.function.function_type == F_MULTISET)
21895  || (node->info.function.function_type == F_SEQUENCE))
21896  {
21897  current_set_node = node->info.function.arg_list;
21898  is_collection_of_collection = true;
21899  }
21900  }
21901  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
21902  {
21903  current_set_node = node->info.value.data_value.set;
21904  is_collection_of_collection = true;
21905  }
21906  else if ((node->node_type == PT_SELECT) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
21907  {
21908  current_set_node = node->info.query.q.select.list;
21909  is_collection_of_collection = true;
21910  }
21911 
21912  if (is_collection_of_collection)
21913  {
21914  /* go through the elements of the collection and check their collations */
21915  while (current_set_node != NULL)
21916  {
21917  status_inner_collection =
21918  pt_get_collation_of_collection (parser, current_set_node, coll_infer, true, &first_element);
21919 
21920  if (status_inner_collection == HAS_COLLATION)
21921  {
21922  has_collation = true;
21923  }
21924  else if (status_inner_collection == ERROR_COLLATION)
21925  {
21926  goto error;
21927  }
21928  current_set_node = current_set_node->next;
21929  }
21930  }
21931  else
21932  {
21933  status_inner_collection =
21934  pt_get_collation_of_collection (parser, current_set_node->data_type, coll_infer, false, &first_element);
21935 
21936  if (status_inner_collection == HAS_COLLATION)
21937  {
21938  has_collation = true;
21939  }
21940  else if (status_inner_collection == ERROR_COLLATION)
21941  {
21942  goto error;
21943  }
21944  }
21945  }
21946  else
21947  {
21948  if (node->node_type == PT_FUNCTION && node->info.function.arg_list != NULL
21950  {
21951  /* charset and collation of system */
21952  has_collation = true;
21953 
21955  || node->info.function.function_type == F_SEQUENCE)
21956  {
21957  coll_infer->can_force_cs = true;
21958  coll_infer->coerc_level = PT_COLLATION_L5_COERC;
21959  coll_infer->codeset = LANG_COERCIBLE_CODESET;
21960  coll_infer->coll_id = LANG_COERCIBLE_COLL;
21961  return HAS_COLLATION;
21962  }
21963  }
21964  }
21965 
21966  if (!has_collation)
21967  {
21968  return NO_COLLATION;
21969  }
21970 
21971  if (has_collation && PT_GET_COLLATION_MODIFIER (node) != -1)
21972  {
21974 
21975  assert (lc != NULL);
21976 
21977  coll_infer->coll_id = PT_GET_COLLATION_MODIFIER (node);
21978 
21979  assert (coll_infer->codeset == lc->codeset);
21980 
21981  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
21982  return HAS_COLLATION;
21983  }
21984 
21985  switch (node->node_type)
21986  {
21987  case PT_VALUE:
21988  assert (has_collation);
21989  if (coll_infer->coll_id == LANG_COLL_BINARY)
21990  {
21992  }
21993  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
21994  {
21995  coll_infer->coerc_level = PT_COLLATION_L4_BIN_COERC;
21996  }
21997  else
21998  {
21999  coll_infer->coerc_level = PT_COLLATION_L4_COERC;
22000  }
22001  break;
22002 
22003  case PT_HOST_VAR:
22004  assert (has_collation);
22005  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
22006  break;
22007 
22008  case PT_EXPR:
22009  case PT_FUNCTION:
22010  case PT_SELECT:
22011  case PT_UNION:
22012  case PT_DIFFERENCE:
22013  case PT_INTERSECTION:
22014  if ((!has_collation && LANG_SYS_COLLATION == LANG_COLL_BINARY) || (coll_infer->coll_id == LANG_COLL_BINARY))
22015  {
22017  }
22018  else if (!has_collation || LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22019  {
22020  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
22021  }
22022  else
22023  {
22024  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
22025  }
22026  break;
22027 
22028  case PT_NAME:
22029  case PT_DOT_:
22030  assert (has_collation);
22031  if (coll_infer->coll_id == LANG_COLL_BINARY)
22032  {
22034  }
22035  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22036  {
22037  coll_infer->coerc_level = PT_COLLATION_L1_BIN_COERC;
22038  }
22039  else
22040  {
22041  coll_infer->coerc_level = PT_COLLATION_L1_COERC;
22042  }
22043  break;
22044 
22045  default:
22046  assert (!has_collation);
22047  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22048  }
22049 
22050  return has_collation ? HAS_COLLATION : NO_COLLATION;
22051 
22052 error:
22054 
22055  return ERROR_COLLATION;
22056 }
22057 
22058 /*
22059  * pt_get_collation_of_collection () - get the collation info of a
22060  * parse tree node with a collection type
22061  *
22062  * return: NO_COLLATION = node doesn't have collation;
22063  * HAS_COLLATION = node has collation
22064  * ERROR_COLLATION = node has multiple component types with
22065  * collation and collations are not compatible
22066  *
22067  * parser(in)
22068  * node(in): a parse tree node
22069  * coll_infer(out): collation inference data
22070  * is_inner_collection(in): the node is an inner collection (inside
22071  * another collection)
22072  * first_element(in/out): is this the first element of the outer collection
22073  * (of all of the collections of collection)
22074  *
22075  */
22076 static COLLATION_RESULT
22078  const bool is_inner_collection, bool * is_first_element)
22079 {
22080  const PT_NODE *current_node;
22081  bool has_collation = false;
22082 
22083  /* check all collatable types of collection */
22084  while (node != NULL)
22085  {
22086  if (is_inner_collection)
22087  {
22088  current_node = node->data_type;
22089  }
22090  else
22091  {
22092  current_node = node;
22093  }
22094 
22095  if (current_node != NULL)
22096  {
22097  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22098  {
22099  PT_COLL_INFER coll_infer_elem;
22100  int status;
22101 
22102  status = pt_get_collation_info_for_collection_type (parser, node, &coll_infer_elem);
22103 
22104  if (status == HAS_COLLATION)
22105  {
22106  has_collation = true;
22107  }
22108  else if (status == ERROR_COLLATION)
22109  {
22110  goto error;
22111  }
22112 
22113  if (*is_first_element)
22114  {
22115  coll_infer->coll_id = coll_infer_elem.coll_id;
22116  coll_infer->codeset = coll_infer_elem.codeset;
22117  *is_first_element = false;
22118  }
22119  else if ((coll_infer_elem.coll_id != coll_infer->coll_id)
22120  || (coll_infer_elem.codeset != coll_infer->codeset))
22121  {
22122  /* error : different collations in same collection */
22123  goto error;
22124  }
22125  }
22126  else if (PT_HAS_COLLATION (current_node->type_enum))
22127  {
22128  assert (current_node->node_type == PT_DATA_TYPE);
22129 
22130  has_collation = true;
22131  if (*is_first_element)
22132  {
22133  coll_infer->codeset = (INTL_CODESET) current_node->info.data_type.units;
22134  coll_infer->coll_id = current_node->info.data_type.collation_id;
22135  *is_first_element = false;
22136  }
22137  else if ((coll_infer->coll_id != current_node->info.data_type.collation_id)
22138  || (coll_infer->codeset != current_node->info.data_type.units))
22139  {
22140  /* error : different collations in same collection */
22141  goto error;
22142  }
22143  }
22144  }
22145  node = node->next;
22146  }
22147 
22148  return has_collation ? HAS_COLLATION : NO_COLLATION;
22149 
22150 error:
22152 
22153  return ERROR_COLLATION;
22154 }
22155 
22156 /*
22157  * pt_coerce_node_collection_of_collection () - changes the collation of a
22158  * collection type parse tree node
22159  *
22160  * return: parse node after coercion
22161  * parser(in)
22162  * node(in): a parse tree node
22163  * coll_id(in): collation
22164  * codeset(in): codeset
22165  * force_mode(in): true if codeset and collation have to forced
22166  * use_collate_modifier(in): true if collation coercion should be done using
22167  * a COLLATE expression modifier
22168  * wrap_type_for_maybe(in): type to use for wrap with cast when current type
22169  * is uncertain (PT_TYPE_MAYBE)
22170  * wrap_type_collection(in): collection type to use for wrap with cast when
22171  * current type is uncertain; if this value is not
22172  * of collection type, then the wrap is without
22173  * a collection
22174  */
22175 static PT_NODE *
22177  const INTL_CODESET codeset, bool force_mode, bool use_collate_modifier,
22178  PT_TYPE_ENUM wrap_type_for_maybe, PT_TYPE_ENUM wrap_type_collection)
22179 {
22180  PT_NODE *current_set_node = NULL, *prev_node = NULL, *save_next = NULL;
22181  bool is_collection_of_collection = false;
22182 
22183  if (node->data_type != NULL)
22184  {
22185  /* if node is a collection of collection, advance to the first element of it */
22186  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
22187  {
22188  if (((node->info.function.function_type == F_TABLE_SET)
22191  && (node->info.function.arg_list->node_type == PT_SELECT))
22192  {
22193  current_set_node = node->info.function.arg_list->info.query.q.select.list;
22194  is_collection_of_collection = true;
22195  }
22196  else if ((node->info.function.function_type == F_SET) || (node->info.function.function_type == F_MULTISET)
22197  || (node->info.function.function_type == F_SEQUENCE))
22198  {
22199  current_set_node = node->info.function.arg_list;
22200  is_collection_of_collection = true;
22201  }
22202  }
22203  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
22204  {
22205  current_set_node = node->info.value.data_value.set;
22206  is_collection_of_collection = true;
22207  }
22208 
22209  if (is_collection_of_collection == true)
22210  {
22211  assert (current_set_node != NULL);
22212  /* change the elements of the collection by applying the new collation to them */
22213  while (current_set_node != NULL)
22214  {
22215  save_next = current_set_node->next;
22216 
22217  current_set_node =
22218  pt_coerce_node_collation (parser, current_set_node, coll_id, codeset, force_mode, use_collate_modifier,
22219  wrap_type_for_maybe, wrap_type_collection);
22220 
22221  if (current_set_node != NULL)
22222  {
22223  if (prev_node == NULL)
22224  {
22225  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
22226  {
22227  if (((node->info.function.function_type == F_TABLE_SET)
22230  && (node->info.function.arg_list->node_type == PT_SELECT))
22231  {
22232  node->info.function.arg_list->info.query.q.select.list = current_set_node;
22233  }
22234  else if ((node->info.function.function_type == F_SET)
22235  || (node->info.function.function_type == F_MULTISET)
22236  || (node->info.function.function_type == F_SEQUENCE))
22237  {
22238  node->info.function.arg_list = current_set_node;
22239  }
22240  }
22241  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
22242  {
22243  node->info.value.data_value.set = current_set_node;
22244  }
22245  }
22246  else
22247  {
22248  assert (prev_node != NULL);
22249  prev_node->next = current_set_node;
22250  }
22251 
22252  current_set_node->next = save_next;
22253  }
22254  else
22255  {
22256  assert (current_set_node == NULL);
22257  goto cannot_coerce;
22258  }
22259  prev_node = current_set_node;
22260  current_set_node = current_set_node->next;
22261  } /* while (current_set_node != NULL) */
22262 
22263  if (node->node_type == PT_VALUE)
22264  {
22266  (void) pt_value_to_db (parser, node);
22267  }
22268  } /* if (is_collection_of_collection == true) */
22269  }
22270 
22271  return node;
22272 
22273 cannot_coerce:
22274  if (codeset != LANG_COERCIBLE_CODESET || !LANG_IS_COERCIBLE_COLL (coll_id))
22275  {
22276  return NULL;
22277  }
22278  return node;
22279 }
22280 
22281 /*
22282  * pt_coerce_node_collation () - changes the collation of parse tree node
22283  *
22284  * return: parse node after coercion
22285  * node(in): a parse tree node
22286  * coll_id(in): collation
22287  * codeset(in): codeset
22288  * force_mode(in): true if codeset and collation have to forced
22289  * use_collate_modifier(in): true if collation coercion should be done using
22290  * a COLLATE expression modifier
22291  * wrap_type_for_maybe(in): type to use for wrap with cast when current type
22292  * is uncertain (PT_TYPE_MAYBE)
22293  * wrap_type_collection(in): collection type to use for wrap with cast when
22294  * current type is uncertain; if this value is not
22295  * of collection type, then the wrap is without
22296  * a collection
22297  *
22298  * Note : 'force_mode' controlls how new collation and charset are applied:
22299  * When 'force_mode' in set, collation and charset are forced;
22300  * if not set, and codeset change is detected, then collation
22301  * coercion will require a CAST (wrap with cast) or a value coerce
22302  * in order to ensure charset conversion. If charset doesn't change,
22303  * it is safe to force directly the new collation.
22304  * When 'node' is an argument of an expression 'force_mode' is false;
22305  * 'force_mode' is on when 'node' is a result (expression) : for this
22306  * case it is assumed the algorithm ensures the result's collation and
22307  * codeset by previously applying the coercion on its arguments.
22308  *
22309  * use_collate_modifier : if true and wrap_with_cast is done, the
22310  * CAST operator is flagged with PT_EXPR_INFO_CAST_COLL_MODIFIER; this
22311  * flag is set only when CAST operation does not change charset, but
22312  * only collation; this kind of CAST is not transformed into T_CAST
22313  * during XASL generation, but into a flagged REGU_VARIABLE which
22314  * "knows" to "update" its collation after "fetch". See usage of
22315  * REGU_VARIABLE_APPLY_COLLATION flag.
22316  *
22317  */
22318 PT_NODE *
22319 pt_coerce_node_collation (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id, const INTL_CODESET codeset,
22320  bool force_mode, bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe,
22321  PT_TYPE_ENUM wrap_type_collection)
22322 {
22323  PT_NODE_TYPE original_node_type;
22324  PT_NODE *wrap_dt;
22325  PT_NODE *collection_node;
22326  bool preset_hv_in_collection = false;
22327  bool is_string_literal = false;
22328 
22329  assert (node != NULL);
22330 
22331  wrap_dt = NULL;
22332 
22333  collection_node =
22334  pt_coerce_node_collection_of_collection (parser, node, coll_id, codeset, force_mode, use_collate_modifier,
22335  wrap_type_for_maybe, wrap_type_collection);
22336  if (collection_node != node)
22337  {
22338  return collection_node;
22339  }
22340 
22341  original_node_type = node->node_type;
22342  switch (node->node_type)
22343  {
22344  case PT_NAME:
22345  case PT_DOT_:
22346  if (node->type_enum == PT_TYPE_MAYBE)
22347  {
22348  /* wrap with cast */
22349  wrap_dt = parser_new_node (parser, PT_DATA_TYPE);
22350  if (wrap_dt == NULL)
22351  {
22352  goto cannot_coerce;
22353  }
22354 
22355  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22356 
22357  wrap_dt->type_enum = wrap_type_for_maybe;
22359  wrap_dt->info.data_type.collation_id = coll_id;
22360  wrap_dt->info.data_type.units = codeset;
22362  force_mode = false;
22363  }
22364  else if (!PT_HAS_COLLATION (node->type_enum) && !PT_IS_COLLECTION_TYPE (node->type_enum))
22365  {
22366  goto cannot_coerce;
22367  }
22368  break;
22369  case PT_EXPR:
22370  case PT_SELECT:
22371  case PT_FUNCTION:
22372  case PT_METHOD_CALL:
22373  case PT_UNION:
22374  case PT_INTERSECTION:
22375  case PT_DIFFERENCE:
22377  && node->type_enum != PT_TYPE_MAYBE)
22378  {
22379  goto cannot_coerce;
22380  }
22381 
22382  if ((node->data_type == NULL && node->type_enum == PT_TYPE_MAYBE)
22383  || (PT_IS_COLLECTION_TYPE (node->type_enum) && node->node_type == PT_FUNCTION))
22384  {
22385  /* wrap with cast */
22386  wrap_dt = parser_new_node (parser, PT_DATA_TYPE);
22387  if (wrap_dt == NULL)
22388  {
22389  goto cannot_coerce;
22390  }
22391 
22392  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22393 
22394  wrap_dt->type_enum = wrap_type_for_maybe;
22396  wrap_dt->info.data_type.collation_id = coll_id;
22397  wrap_dt->info.data_type.units = codeset;
22399  force_mode = false;
22400  }
22401  break;
22402  case PT_VALUE:
22403  if (node->data_type == NULL && PT_HAS_COLLATION (node->type_enum) && coll_id != LANG_SYS_COLLATION)
22404  {
22405  /* create a data type */
22406  node->data_type = parser_new_node (parser, PT_DATA_TYPE);
22407  if (node->data_type == NULL)
22408  {
22409  goto cannot_coerce;
22410  }
22411 
22412  node->data_type->type_enum = node->type_enum;
22416  }
22417  break;
22418  case PT_HOST_VAR:
22419  if (node->type_enum == PT_TYPE_MAYBE && node->expected_domain == NULL)
22420  {
22421  TP_DOMAIN *dom_hv;
22422  DB_TYPE exp_db_type;
22423 
22424  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22425 
22426  if (PT_IS_COLLECTION_TYPE (wrap_type_collection))
22427  {
22428  exp_db_type = pt_type_enum_to_db (wrap_type_collection);
22429  dom_hv = tp_domain_resolve_default (exp_db_type);
22430  }
22431  else
22432  {
22433  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
22434  dom_hv = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
22435  }
22436  dom_hv = tp_domain_cache (dom_hv);
22437  SET_EXPECTED_DOMAIN (node, dom_hv);
22438  }
22439  break;
22440  default:
22441  /* by default, no not coerce */
22442  goto cannot_coerce;
22443  }
22444 
22445  if (node->node_type == PT_VALUE && PT_IS_CHAR_STRING_TYPE (node->type_enum)
22446  && node->info.value.is_collate_allowed == false)
22447  {
22448  is_string_literal = true;
22449  }
22450 
22451  if (node->data_type != NULL || wrap_dt != NULL)
22452  {
22454  || node->type_enum == PT_TYPE_MAYBE);
22455 
22456  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22457  {
22458  PT_NODE *dt_node;
22459  PT_NODE *dt = NULL, *arg;
22460  bool apply_wrap_cast = false;
22461 
22462  if (node->data_type == NULL)
22463  {
22464  assert (wrap_dt != NULL);
22465  /* collection without data type : any (?, ?) */
22466  node->data_type = wrap_dt;
22467  force_mode = true;
22468  preset_hv_in_collection = true;
22469  }
22470 
22471  assert (node->data_type != NULL);
22472  dt_node = node->data_type;
22473 
22474  /* check if wrap with cast is necessary */
22475  if (!force_mode || node->node_type != PT_EXPR || node->info.expr.op != PT_CAST)
22476  {
22477  do
22478  {
22479  if (PT_HAS_COLLATION (dt_node->type_enum) && dt_node->info.data_type.collation_id != coll_id)
22480  {
22481  apply_wrap_cast = true;
22482  break;
22483  }
22484 
22485  dt_node = dt_node->next;
22486  }
22487  while (dt_node != NULL);
22488 
22489  if (apply_wrap_cast == false && node->node_type == PT_FUNCTION
22490  && ((node->info.function.function_type == F_MULTISET) || (node->info.function.function_type == F_SET)
22491  || (node->info.function.function_type == F_SEQUENCE)))
22492  {
22493  arg = node->info.function.arg_list;
22494  do
22495  {
22496  if ((PT_HAS_COLLATION (arg->type_enum) && arg->data_type != NULL
22497  && (arg->data_type->info.data_type.collation_id != coll_id))
22498  || (arg->type_enum == PT_TYPE_MAYBE && arg->node_type != PT_HOST_VAR))
22499  {
22500  apply_wrap_cast = true;
22501  break;
22502  }
22503 
22504  arg = arg->next;
22505  }
22506  while (arg != NULL);
22507  }
22508  }
22509 
22510  if (apply_wrap_cast)
22511  {
22512  dt = parser_copy_tree_list (parser, node->data_type);
22513  dt_node = dt;
22514  }
22515  else
22516  {
22517  dt_node = node->data_type;
22518  }
22519 
22520  /* apply new collation and codeset for all collatable sub-types */
22521  do
22522  {
22523  if (PT_HAS_COLLATION (dt_node->type_enum))
22524  {
22525  dt_node->info.data_type.collation_id = coll_id;
22526  dt_node->info.data_type.units = (int) codeset;
22527  if (!PT_IS_COLLECTION_TYPE (node->type_enum))
22528  {
22530  }
22531  else
22532  {
22534  }
22535  }
22536 
22537  dt_node = dt_node->next;
22538  }
22539  while (dt_node != NULL);
22540 
22541  if (apply_wrap_cast)
22542  {
22543  node = pt_wrap_collection_with_cast_op (parser, node, node->type_enum, dt, true);
22544  /* flag PT_EXPR_INFO_CAST_COLL_MODIFIER is not set here; COLLATE modifier is not supported for this
22545  * context */
22546  }
22547  else if (dt != NULL)
22548  {
22549  parser_free_node (parser, dt);
22550  }
22551  }
22552  else if (PT_HAS_COLLATION (node->type_enum) || node->type_enum == PT_TYPE_MAYBE)
22553  {
22554  /* We wrap with cast when: - force_mode is disabled (we apply new collation on existing node), and - it is a
22555  * string literal node with different codeset - it is a other node type with differrent collation - it is not
22556  * a CAST expression - it is not HOST_VAR node */
22557  if (!force_mode
22558  && ((node->data_type != NULL
22559  && ((is_string_literal == false && node->data_type->info.data_type.collation_id != coll_id)
22560  || node->data_type->info.data_type.units != codeset)) || wrap_dt != NULL)
22561  && (node->node_type != PT_EXPR || node->info.expr.op != PT_CAST
22562  || pt_cast_needs_wrap_for_collation (node, codeset)) && node->node_type != PT_HOST_VAR)
22563  {
22564  if (wrap_dt == NULL)
22565  {
22566  wrap_dt = parser_copy_tree_list (parser, node->data_type);
22567  wrap_dt->info.data_type.collation_id = coll_id;
22568  wrap_dt->info.data_type.units = (int) codeset;
22570  }
22571  if (node->node_type == PT_VALUE && codeset == INTL_CODESET_RAW_BYTES
22573  {
22574  /* cannot have values of ENUM type here */
22576  /* converting from multibyte charset to binary charset, may truncate the string data (precision is
22577  * kept); this workaround ensures that new precision (after charset conversion) grows to the size in
22578  * bytes of original data: conversion rule from multibyte charset to binary is to reinterpret the
22579  * bytes as binary characters. */
22580  if (node->info.value.data_value.str != NULL)
22581  {
22582  wrap_dt->info.data_type.precision = node->info.value.data_value.str->length;
22583  }
22584  else if (node->info.value.db_value_is_initialized)
22585  {
22587  }
22588  }
22589 
22590  if (node->node_type == PT_SELECT || node->node_type == PT_DIFFERENCE || node->node_type == PT_INTERSECTION
22591  || node->node_type == PT_UNION)
22592  {
22593  PT_NODE *select_list;
22594  int nb_select_list;
22595 
22596  if (node->node_type == PT_SELECT)
22597  {
22598  select_list = node->info.query.q.select.list;
22599  }
22600  else
22601  {
22602  /* It is enough to count the number of select list items from one of the
22603  * union/intersect/difference arguments. If they would be different, this code would not be
22604  * reached, an arg incompatibility is thrown before. Because of the left side recursivity of
22605  * table ops, arg2 is always a PT_SELECT. */
22606  PT_NODE *union_arg2 = node->info.query.q.union_.arg2;
22607  assert (union_arg2->node_type == PT_SELECT);
22608  select_list = union_arg2->info.query.q.select.list;
22609  }
22610 
22611  nb_select_list = pt_length_of_list (select_list);
22612  if (nb_select_list != 1)
22613  {
22614  goto cannot_coerce;
22615  }
22616  if (pt_wrap_select_list_with_cast_op (parser, node, wrap_dt->type_enum,
22617  wrap_dt->info.data_type.precision,
22618  wrap_dt->info.data_type.dec_precision, wrap_dt,
22619  true) != NO_ERROR)
22620  {
22621  goto cannot_coerce;
22622  }
22623  /* flag PT_EXPR_INFO_CAST_COLL_MODIFIER is not set here; COLLATE modifier is not supported for this
22624  * context */
22625  }
22626  else
22627  {
22628  bool cast_should_fold = false;
22629 
22630  if (node->node_type == PT_VALUE)
22631  {
22632  if (is_string_literal == false && node->data_type
22633  && codeset == node->data_type->info.data_type.units)
22634  {
22635  /* force using COLLATE modifier for VALUEs when codeset is not changed */
22636  use_collate_modifier = true;
22637  }
22638  else
22639  {
22640  cast_should_fold = true;
22641  }
22642  }
22643 
22644  node = pt_wrap_with_cast_op (parser, node, wrap_dt->type_enum, wrap_dt->info.data_type.precision,
22645  wrap_dt->info.data_type.dec_precision, wrap_dt);
22646  if (node != NULL && use_collate_modifier)
22647  {
22648  assert (node->node_type == PT_EXPR && node->info.expr.op == PT_CAST);
22650  PT_SET_NODE_COLL_MODIFIER (node, coll_id);
22651  }
22652 
22653  if (node != NULL && cast_should_fold)
22654  {
22655  assert (node->node_type == PT_EXPR && node->info.expr.op == PT_CAST);
22657  }
22658  }
22659 
22660  /* 'wrap_dt' is copied in 'pt_wrap_with_cast_op' */
22661  parser_free_node (parser, wrap_dt);
22662  }
22663  else
22664  {
22665  node->data_type->info.data_type.collation_id = coll_id;
22666  node->data_type->info.data_type.units = (int) codeset;
22668  }
22669  }
22670  }
22672  && (TP_DOMAIN_COLLATION (node->expected_domain) != coll_id
22674  {
22675  TP_DOMAIN *new_domain;
22676 
22677  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22678  {
22679  goto cannot_coerce;
22680  }
22681 
22683 
22684  if (node->expected_domain->is_cached)
22685  {
22686  /* create new domain */
22687  new_domain = tp_domain_copy (node->expected_domain, false);
22688  new_domain->codeset = (unsigned char) codeset;
22689  new_domain->collation_id = coll_id;
22690  if (node->type_enum == PT_TYPE_MAYBE)
22691  {
22693  {
22694  new_domain->collation_flag = TP_DOMAIN_COLL_NORMAL;
22695  }
22696  else
22697  {
22698  new_domain->collation_flag = TP_DOMAIN_COLL_ENFORCE;
22699  }
22700  }
22701 
22702  /* the existing 'expected_domain' may have been created for this specific node and cached, it will remain
22703  * cached */
22704  new_domain = tp_domain_cache (new_domain);
22705  node->expected_domain = new_domain;
22706  }
22707  else
22708  {
22709  /* safe to change the domain directly */
22710  node->expected_domain->codeset = (unsigned char) codeset;
22711  node->expected_domain->collation_id = coll_id;
22712  if (node->type_enum == PT_TYPE_MAYBE)
22713  {
22715  {
22717  }
22718  else
22719  {
22721  }
22722  }
22723  }
22724  }
22725  else if (node->expected_domain != NULL && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (node->expected_domain)))
22726  {
22727  /* collection domain */
22728  TP_DOMAIN *elem_dom;
22729  TP_DOMAIN *curr_set_dom;
22730  TP_DOMAIN *new_set_dom;
22731  TP_DOMAIN *new_elem_dom;
22732  TP_DOMAIN *save_elem_dom_next;
22733  DB_TYPE exp_db_type;
22734 
22735  /* add domain of string with expected collation */
22736  curr_set_dom = node->expected_domain;
22737  elem_dom = curr_set_dom->setdomain;
22738 
22739  /* copy only parent collection domain */
22740  curr_set_dom->setdomain = NULL;
22741  new_set_dom = tp_domain_copy (curr_set_dom, false);
22742  curr_set_dom->setdomain = elem_dom;
22743  while (elem_dom != NULL)
22744  {
22745  /* create a new domain from this */
22746  save_elem_dom_next = elem_dom->next;
22747  elem_dom->next = NULL;
22748  new_elem_dom = tp_domain_copy (elem_dom, false);
22749  elem_dom->next = save_elem_dom_next;
22750 
22751  if (TP_IS_CHAR_TYPE (TP_DOMAIN_TYPE (elem_dom)))
22752  {
22753  /* for string domains overwrite collation */
22754  new_elem_dom->collation_id = coll_id;
22755  new_elem_dom->codeset = codeset;
22756  }
22757 
22758  tp_domain_add (&(new_set_dom->setdomain), new_elem_dom);
22759  elem_dom = elem_dom->next;
22760  }
22761 
22762  if (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe))
22763  {
22764  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
22765  /* create an expected domain to force collation */
22766  new_elem_dom = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
22767  new_elem_dom = tp_domain_copy (new_elem_dom, false);
22768  tp_domain_add (&(new_set_dom->setdomain), new_elem_dom);
22769  }
22770 
22771  node->expected_domain = tp_domain_cache (new_set_dom);
22772  }
22773 
22774  switch (node->node_type)
22775  {
22776  case PT_VALUE:
22777  if (node->info.value.db_value_is_initialized && node->data_type != NULL)
22778  {
22779  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22780  {
22781  int i;
22782  DB_VALUE *sub_value = NULL;
22783  SETREF *setref = db_get_set (&(node->info.value.db_value));
22784  int set_size = setobj_size (setref->set);
22785 
22786  for (i = 0; i < set_size; i++)
22787  {
22788  setobj_get_element_ptr (setref->set, i, &sub_value);
22789  if (sub_value != NULL && TP_IS_CHAR_TYPE (DB_VALUE_TYPE (sub_value)))
22790  {
22791  db_string_put_cs_and_collation (sub_value, (unsigned char) codeset, coll_id);
22792  }
22793  }
22794  }
22795  else
22796  {
22798  db_string_put_cs_and_collation (&(node->info.value.db_value), (unsigned char) codeset, coll_id);
22799  }
22800  }
22801  break;
22802  case PT_HOST_VAR:
22803  if (node->expected_domain != NULL)
22804  {
22805  pt_preset_hostvar (parser, node);
22806  }
22807  else if (node->data_type != NULL && PT_HAS_COLLATION (node->data_type->type_enum))
22808  {
22809  /* this is a HV from an auto-parametrization */
22810  node->data_type->info.data_type.collation_id = coll_id;
22811  node->data_type->info.data_type.units = (int) codeset;
22813  }
22814  break;
22815  case PT_FUNCTION:
22816  if (preset_hv_in_collection)
22817  {
22818  PT_NODE *arg;
22819  TP_DOMAIN *dom_hv;
22820  DB_TYPE exp_db_type;
22821 
22823 
22824  arg = node->info.function.arg_list;
22825  while (arg != NULL)
22826  {
22827  if (arg->node_type != PT_HOST_VAR)
22828  {
22829  arg = arg->next;
22830  continue;
22831  }
22832 
22833  if (arg->expected_domain != NULL)
22834  {
22836  || TP_DOMAIN_COLLATION (arg->expected_domain) == coll_id)
22837  {
22838  arg = arg->next;
22839  continue;
22840  }
22841 
22842  if (arg->expected_domain->is_cached)
22843  {
22844  /* create new domain */
22845  dom_hv = tp_domain_copy (arg->expected_domain, false);
22846  }
22847  else
22848  {
22849  dom_hv = arg->expected_domain;
22850  }
22851 
22852  dom_hv->codeset = (unsigned char) codeset;
22853  dom_hv->collation_id = coll_id;
22855  }
22856  else
22857  {
22858  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22859  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
22860  /* create an expected domain to force collation */
22861  dom_hv = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
22862  }
22863 
22864  dom_hv = tp_domain_cache (dom_hv);
22865  SET_EXPECTED_DOMAIN (arg, dom_hv);
22866  pt_preset_hostvar (parser, arg);
22867  arg = arg->next;
22868  }
22869  }
22870  break;
22871  case PT_EXPR:
22872  if (is_string_literal == true && node->node_type == PT_EXPR && node->info.expr.op == PT_CAST)
22873  {
22874  PT_NODE *save_next;
22875  /* a PT_VALUE node was wrapped with CAST to change the charset and collation, but the value originated from a
22876  * simple string literal which does not allow COLLATE; this forces a charset conversion and print with the
22877  * new charset introducer, and without COLLATE */
22879  save_next = node->next;
22880  node->next = NULL;
22881  node = pt_fold_const_expr (parser, node, NULL);
22882  if (node != NULL)
22883  {
22884  node->next = save_next;
22885  node->info.value.is_collate_allowed = false;
22886  node->info.value.print_charset = true;
22887  /* print text with new charset */
22888  node->info.value.text = NULL;
22889  PT_NODE_PRINT_VALUE_TO_TEXT (parser, node);
22890  }
22891  break;
22892  }
22893  /* special case : CAST */
22894  if (node->info.expr.op == PT_CAST && node->info.expr.cast_type != NULL)
22895  {
22896  /* propagate the collation and codeset to cast */
22897  PT_NODE *cast_type = node->info.expr.cast_type;
22898 
22899  if (PT_IS_COLLECTION_TYPE (cast_type->type_enum))
22900  {
22901  PT_NODE *dt_node = cast_type->data_type;
22902 
22903  assert (dt_node != NULL);
22904 
22905  /* force collation on each collection component */
22906  do
22907  {
22908  if (PT_HAS_COLLATION (dt_node->type_enum))
22909  {
22910  dt_node->info.data_type.collation_id = coll_id;
22911  dt_node->info.data_type.units = (int) codeset;
22912  if ((original_node_type != PT_EXPR) && (!PT_IS_COLLECTION_TYPE (node->type_enum)))
22913  {
22915  }
22916  else
22917  {
22919  }
22920  }
22921 
22922  dt_node = dt_node->next;
22923  }
22924  while (dt_node != NULL);
22925  }
22926  else
22927  {
22928  assert (PT_HAS_COLLATION (cast_type->type_enum));
22929 
22930  cast_type->info.data_type.collation_id = coll_id;
22931  cast_type->info.data_type.units = (int) codeset;
22932 
22933  assert (node->data_type != NULL);
22934  assert (node->data_type->info.data_type.collation_id == coll_id);
22935 
22937  }
22938  }
22939  break;
22940  default:
22941  break;
22942  }
22943 
22944  return node;
22945 
22946 cannot_coerce:
22947  if (codeset != LANG_COERCIBLE_CODESET || !LANG_IS_COERCIBLE_COLL (coll_id))
22948  {
22949  return NULL;
22950  }
22951  return node;
22952 }
22953 
22954 /*
22955  * pt_common_collation () - compute common collation of an operation
22956  *
22957  * return: 0 comon collation and codeset have been detected, -1 otherwise
22958  * arg1_coll_infer(in): collation inference data of arg1
22959  * arg2_coll_infer(in): collation inference data of arg2
22960  * arg3_coll_infer(in): collation inference data of arg3
22961  * args_w_coll(in): how many arguments have collation
22962  * op_has_3_args(in): true operation has 3 arguments
22963  * common_coll(out): common collation detected
22964  * common_cs(out): common codeset detected
22965  *
22966  */
22967 int
22968 pt_common_collation (PT_COLL_INFER * arg1_coll_infer, PT_COLL_INFER * arg2_coll_infer, PT_COLL_INFER * arg3_coll_infer,
22969  const int args_w_coll, bool op_has_3_args, int *common_coll, INTL_CODESET * common_cs)
22970 {
22971 #define MORE_COERCIBLE(arg1_coll_infer, arg2_coll_infer) \
22972  ((((arg1_coll_infer)->can_force_cs) && !((arg2_coll_infer)->can_force_cs)) \
22973  || ((arg1_coll_infer)->coerc_level > (arg2_coll_infer)->coerc_level \
22974  && (arg1_coll_infer)->can_force_cs == (arg2_coll_infer)->can_force_cs))
22975 
22976  assert (common_coll != NULL);
22977  assert (common_cs != NULL);
22978  assert (arg1_coll_infer != NULL);
22979  assert (arg2_coll_infer != NULL);
22980 
22981  if (op_has_3_args)
22982  {
22983  assert (arg3_coll_infer != NULL);
22984  }
22985 
22986  if (arg1_coll_infer->coll_id != arg2_coll_infer->coll_id
22987  && arg1_coll_infer->coerc_level == arg2_coll_infer->coerc_level
22988  && arg1_coll_infer->can_force_cs == arg2_coll_infer->can_force_cs)
22989  {
22990  goto error;
22991  }
22992  else if (MORE_COERCIBLE (arg1_coll_infer, arg2_coll_infer))
22993  {
22994  /* coerce arg1 collation */
22995  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg2_coll_infer->codeset) && !arg1_coll_infer->can_force_cs)
22996  {
22997  goto error;
22998  }
22999  *common_coll = arg2_coll_infer->coll_id;
23000  *common_cs = arg2_coll_infer->codeset;
23001 
23002  /* check arg3 */
23003  if (op_has_3_args && arg3_coll_infer->coll_id != *common_coll)
23004  {
23005  bool set_arg3 = false;
23006 
23007  if (MORE_COERCIBLE (arg2_coll_infer, arg3_coll_infer))
23008  {
23009  set_arg3 = true;
23010  }
23011  else if (MORE_COERCIBLE (arg3_coll_infer, arg2_coll_infer))
23012  {
23013  set_arg3 = false;
23014  }
23015  else
23016  {
23017  goto error;
23018  }
23019 
23020  if (set_arg3)
23021  {
23022  /* coerce to collation of arg3 */
23023  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg3_coll_infer->codeset)
23024  && !arg2_coll_infer->can_force_cs)
23025  {
23026  goto error;
23027  }
23028  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg3_coll_infer->codeset)
23029  && !arg1_coll_infer->can_force_cs)
23030  {
23031  goto error;
23032  }
23033 
23034  *common_coll = arg3_coll_infer->coll_id;
23035  *common_cs = arg3_coll_infer->codeset;
23036  }
23037  else
23038  {
23039  /* coerce arg3 collation */
23040  if (!INTL_CAN_COERCE_CS (arg3_coll_infer->codeset, arg2_coll_infer->codeset)
23041  && !arg3_coll_infer->can_force_cs)
23042  {
23043  goto error;
23044  }
23045 
23046  assert (*common_coll == arg2_coll_infer->coll_id);
23047  assert (*common_cs == arg2_coll_infer->codeset);
23048  }
23049  }
23050  }
23051  else
23052  {
23053  assert (MORE_COERCIBLE (arg2_coll_infer, arg1_coll_infer)
23054  || arg2_coll_infer->coll_id == arg1_coll_infer->coll_id);
23055 
23056  /* coerce arg2 collation */
23057  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg1_coll_infer->codeset) && !arg2_coll_infer->can_force_cs)
23058  {
23059  goto error;
23060  }
23061 
23062  *common_coll = arg1_coll_infer->coll_id;
23063  *common_cs = arg1_coll_infer->codeset;
23064 
23065  /* check arg3 */
23066  if (op_has_3_args && arg3_coll_infer->coll_id != *common_coll)
23067  {
23068  bool set_arg3 = false;
23069  if (MORE_COERCIBLE (arg1_coll_infer, arg3_coll_infer))
23070  {
23071  set_arg3 = true;
23072  }
23073  else if (MORE_COERCIBLE (arg3_coll_infer, arg1_coll_infer))
23074  {
23075  set_arg3 = false;
23076  }
23077  else
23078  {
23079  goto error;
23080  }
23081 
23082  if (set_arg3)
23083  {
23084  /* coerce to collation of arg3 */
23085  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg3_coll_infer->codeset)
23086  && !arg1_coll_infer->can_force_cs)
23087  {
23088  goto error;
23089  }
23090 
23091  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg3_coll_infer->codeset)
23092  && !arg2_coll_infer->can_force_cs)
23093  {
23094  goto error;
23095  }
23096 
23097  *common_coll = arg3_coll_infer->coll_id;
23098  *common_cs = arg3_coll_infer->codeset;
23099  }
23100  else
23101  {
23102  /* coerce arg3 collation */
23103  if (!INTL_CAN_COERCE_CS (arg3_coll_infer->codeset, arg1_coll_infer->codeset)
23104  && !arg3_coll_infer->can_force_cs)
23105  {
23106  goto error;
23107  }
23108 
23109  assert (*common_coll == arg1_coll_infer->coll_id);
23110  assert (*common_cs == arg1_coll_infer->codeset);
23111  }
23112  }
23113  }
23114 
23115  return NO_ERROR;
23116 
23117 error:
23118 
23119  return ER_FAILED;
23120 
23121 #undef MORE_COERCIBLE
23122 }
23123 
23124 /*
23125  * pt_check_expr_collation () - checks the collation of an expression node
23126  *
23127  * return: error code
23128  * parser(in): parser context
23129  * node(in): a parse tree expression node
23130  *
23131  */
23132 static int
23134 {
23135  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
23136  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
23137  PT_TYPE_ENUM arg1_wrap_type = PT_TYPE_NONE, arg2_wrap_type = PT_TYPE_NONE;
23138  PT_TYPE_ENUM arg3_wrap_type = PT_TYPE_NONE, expr_wrap_type = PT_TYPE_NONE;
23139  PT_TYPE_ENUM arg1_collection_wrap_type = PT_TYPE_NONE, arg2_collection_wrap_type = PT_TYPE_NONE;
23140  PT_OP_TYPE op;
23141  PT_COLL_INFER arg1_coll_inf, arg2_coll_inf, arg3_coll_inf;
23142  PT_NODE *expr = *node;
23143  PT_NODE *arg1, *arg2, *arg3;
23144  PT_NODE *new_node;
23145  int common_coll = LANG_COERCIBLE_COLL;
23147  int args_w_coll_maybe = 0;
23148  int args_having_coll = 0;
23149  bool op_has_3_args;
23150  bool reverse_arg2_arg3;
23151  int expr_coll_modifier = -1;
23152  INTL_CODESET expr_cs_modifier = INTL_CODESET_NONE;
23153  bool use_cast_collate_modifier = false;
23154  bool arg1_need_coerce = false;
23155  bool arg2_need_coerce = false;
23156  bool arg3_need_coerce = false;
23157 
23158  assert (expr != NULL);
23159  assert (expr->node_type == PT_EXPR);
23160 
23161  arg1_coll_inf.coll_id = arg2_coll_inf.coll_id = arg3_coll_inf.coll_id = LANG_COERCIBLE_COLL;
23162  arg1_coll_inf.codeset = arg2_coll_inf.codeset = arg3_coll_inf.codeset = LANG_COERCIBLE_CODESET;
23163  arg1_coll_inf.coerc_level = arg2_coll_inf.coerc_level = arg3_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23164  arg1_coll_inf.can_force_cs = arg2_coll_inf.can_force_cs = arg3_coll_inf.can_force_cs = true;
23165 
23166  /* NULL has no collation */
23167  if (expr->type_enum == PT_TYPE_NULL)
23168  {
23169  return NO_ERROR;
23170  }
23171 
23172  op = expr->info.expr.op;
23173  arg1 = expr->info.expr.arg1;
23174  arg2 = expr->info.expr.arg2;
23175  arg3 = expr->info.expr.arg3;
23176 
23177  expr_coll_modifier = PT_GET_COLLATION_MODIFIER (expr);
23178 
23179  if (expr_coll_modifier != -1)
23180  {
23181  LANG_COLLATION *lc = lang_get_collation (expr_coll_modifier);
23182 
23183  assert (lc != NULL);
23184  expr_cs_modifier = lc->codeset;
23185  }
23186 
23188 
23189  if (!pt_is_op_w_collation (op) || (op == PT_PLUS && prm_get_bool_value (PRM_ID_PLUS_AS_CONCAT) == false))
23190  {
23191  if (expr_coll_modifier != -1)
23192  {
23193  if (!(op == PT_EVALUATE_VARIABLE || pt_is_comp_op (op)) && !PT_HAS_COLLATION (expr->type_enum))
23194  {
23196  goto error_exit;
23197  }
23198  goto coerce_result;
23199  }
23200  return NO_ERROR;
23201  }
23202 
23203  op_has_3_args = (op == PT_CONCAT_WS || op == PT_REPLACE || op == PT_TRANSLATE || op == PT_BETWEEN
23204  || op == PT_NOT_BETWEEN || op == PT_IF || op == PT_FIELD || op == PT_INDEX_PREFIX || op == PT_NVL2);
23205  reverse_arg2_arg3 = (op == PT_RPAD || op == PT_LPAD);
23206 
23207  /* step 1 : get info */
23208  if (reverse_arg2_arg3)
23209  {
23210  PT_NODE *tmp = arg2;
23211  arg2 = arg3;
23212  arg3 = tmp;
23213  }
23214 
23215  if (arg1)
23216  {
23217  arg1_type = arg1->type_enum;
23218  }
23219 
23220  if (arg2)
23221  {
23222  arg2_type = arg2->type_enum;
23223  }
23224 
23225  if (arg3)
23226  {
23227  arg3_type = arg3->type_enum;
23228  }
23229 
23230  /* will not check collation for BETWEEN op when arg1 does not have collation or arg2 is a range expression */
23231  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
23232  && (!(PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE)
23233  || (arg2 && arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))))
23234  {
23235  return NO_ERROR;
23236  }
23237 
23238  if (op == PT_MINUS && (!PT_IS_COLLECTION_TYPE (arg1_type) && !PT_IS_COLLECTION_TYPE (arg2_type))
23239  && (arg1_type != PT_TYPE_MAYBE || arg2_type != PT_TYPE_MAYBE))
23240  {
23241  return NO_ERROR;
23242  }
23243 
23244  if (PT_HAS_COLLATION (arg1_type)
23245  || (arg1_type == PT_TYPE_MAYBE
23246  && (arg1->expected_domain == NULL || !TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg1->expected_domain)))))
23247  {
23248  if (pt_get_collation_info (arg1, &arg1_coll_inf))
23249  {
23250  args_w_coll_maybe++;
23251  }
23252 
23253  if (arg1_coll_inf.can_force_cs == false)
23254  {
23255  args_having_coll++;
23256  common_coll = arg1_coll_inf.coll_id;
23257  common_cs = arg1_coll_inf.codeset;
23258  }
23259  else
23260  {
23261  arg1_need_coerce = true;
23262  }
23263  }
23264  else if (PT_IS_COLLECTION_TYPE (arg1_type)
23265  || (arg1_type == PT_TYPE_MAYBE && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg1->expected_domain))))
23266  {
23267  int status = pt_get_collation_info_for_collection_type (parser, arg1,
23268  &arg1_coll_inf);
23269 
23270  if (status == ERROR_COLLATION)
23271  {
23272  goto error_exit;
23273  }
23274  else if (status == HAS_COLLATION)
23275  {
23276  args_w_coll_maybe++;
23277  if (arg1_coll_inf.can_force_cs == false)
23278  {
23279  args_having_coll++;
23280  common_coll = arg1_coll_inf.coll_id;
23281  common_cs = arg1_coll_inf.codeset;
23282  }
23283  else
23284  {
23285  arg1_need_coerce = true;
23286  }
23287  }
23288  }
23289 
23290  if (PT_HAS_COLLATION (arg2_type)
23291  || (arg2_type == PT_TYPE_MAYBE
23292  && (arg2->expected_domain == NULL || !TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg2->expected_domain)))))
23293  {
23294  if (pt_get_collation_info (arg2, &arg2_coll_inf))
23295  {
23296  args_w_coll_maybe++;
23297  }
23298 
23299  if (arg2_coll_inf.can_force_cs == false)
23300  {
23301  args_having_coll++;
23302  common_coll = arg2_coll_inf.coll_id;
23303  common_cs = arg2_coll_inf.codeset;
23304  }
23305  else
23306  {
23307  arg2_need_coerce = true;
23308  }
23309  }
23310  else if (PT_IS_COLLECTION_TYPE (arg2_type)
23311  || (arg2_type == PT_TYPE_MAYBE && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg2->expected_domain))))
23312  {
23313  int status = pt_get_collation_info_for_collection_type (parser, arg2,
23314  &arg2_coll_inf);
23315 
23316  if (status == ERROR_COLLATION)
23317  {
23318  goto error_exit;
23319  }
23320  else if (status == HAS_COLLATION)
23321  {
23322  args_w_coll_maybe++;
23323  if (arg2_coll_inf.can_force_cs == false)
23324  {
23325  args_having_coll++;
23326  common_coll = arg2_coll_inf.coll_id;
23327  common_cs = arg2_coll_inf.codeset;
23328  }
23329  else
23330  {
23331  arg2_need_coerce = true;
23332  }
23333  }
23334  }
23335 
23336  if (op_has_3_args)
23337  {
23338  if (PT_HAS_COLLATION (arg3_type) || arg3_type == PT_TYPE_MAYBE)
23339  {
23340  if (pt_get_collation_info (arg3, &arg3_coll_inf))
23341  {
23342  args_w_coll_maybe++;
23343  }
23344 
23345  if (arg3_coll_inf.can_force_cs == false)
23346  {
23347  args_having_coll++;
23348  common_coll = arg3_coll_inf.coll_id;
23349  common_cs = arg3_coll_inf.codeset;
23350  }
23351  else
23352  {
23353  arg3_need_coerce = true;
23354  }
23355  }
23356  else if (PT_IS_COLLECTION_TYPE (arg3_type))
23357  {
23358  int status = pt_get_collation_info_for_collection_type (parser, arg3,
23359  &arg3_coll_inf);
23360 
23361  if (status == ERROR_COLLATION)
23362  {
23363  goto error_exit;
23364  }
23365  else if (status == HAS_COLLATION)
23366  {
23367  args_w_coll_maybe++;
23368  if (arg3_coll_inf.can_force_cs == false)
23369  {
23370  args_having_coll++;
23371  common_coll = arg3_coll_inf.coll_id;
23372  common_cs = arg3_coll_inf.codeset;
23373  }
23374  }
23375  }
23376  }
23377 
23378  if (expr_coll_modifier != -1 && pt_is_comp_op (op) && args_w_coll_maybe > 0)
23379  {
23380  /* for comparisons, force the collation of each argument to have the collation of expression */
23381  if (PT_HAS_COLLATION (arg1_type) && arg1_coll_inf.codeset != expr_cs_modifier)
23382  {
23384  lang_get_codeset_name (arg1_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23385  goto error_exit;
23386  }
23387  if (PT_HAS_COLLATION (arg2_type) && arg2_coll_inf.codeset != expr_cs_modifier)
23388  {
23390  lang_get_codeset_name (arg2_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23391  goto error_exit;
23392  }
23393  if (PT_HAS_COLLATION (arg3_type) && arg3_coll_inf.codeset != expr_cs_modifier)
23394  {
23396  lang_get_codeset_name (arg3_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23397  goto error_exit;
23398  }
23399 
23400  common_cs = expr_cs_modifier;
23401  common_coll = expr_coll_modifier;
23402  use_cast_collate_modifier = true;
23403  goto coerce_arg;
23404  }
23405 
23406  if (args_w_coll_maybe <= 1)
23407  {
23408  goto coerce_result;
23409  }
23410 
23411  /* step 2 : compute collation to use */
23412  assert (args_w_coll_maybe >= 2);
23413 
23414  if (op_has_3_args)
23415  {
23416  if (args_w_coll_maybe < 3)
23417  {
23418  if (!(PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg1_type)))
23419  {
23420  arg1_coll_inf.coll_id = common_coll;
23421  arg1_coll_inf.codeset = common_cs;
23422  arg1_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23423  }
23424 
23425  if (!(PT_HAS_COLLATION (arg2_type) || arg2_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg2_type)))
23426  {
23427  arg2_coll_inf.coll_id = common_coll;
23428  arg2_coll_inf.codeset = common_cs;
23429  arg2_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23430  }
23431 
23432  if (!(PT_HAS_COLLATION (arg3_type) || arg3_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg3_type)))
23433  {
23434  arg3_coll_inf.coll_id = common_coll;
23435  arg3_coll_inf.codeset = common_cs;
23436  arg3_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23437  }
23438  }
23439 
23440  if (arg1_coll_inf.coll_id == arg2_coll_inf.coll_id && arg2_coll_inf.coll_id == arg3_coll_inf.coll_id
23441  && (arg1_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE)
23442  && (arg1_need_coerce == false && arg2_need_coerce == false && arg3_need_coerce == false))
23443  {
23444  assert (arg1_coll_inf.codeset == arg2_coll_inf.codeset && arg2_coll_inf.codeset == arg3_coll_inf.codeset);
23445  goto coerce_result;
23446  }
23447  }
23448  else
23449  {
23450  if (arg1_coll_inf.coll_id == arg2_coll_inf.coll_id && (arg1_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE)
23451  && (arg1_need_coerce == false && arg2_need_coerce == false))
23452  {
23453  assert (arg1_coll_inf.codeset == arg2_coll_inf.codeset);
23454  goto coerce_result;
23455  }
23456  }
23457 
23458  assert (arg1_coll_inf.coll_id != arg2_coll_inf.coll_id || arg1_coll_inf.coll_id != arg3_coll_inf.coll_id
23459  || arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE
23460  || arg1_need_coerce == true || arg2_need_coerce == true || arg3_need_coerce == true);
23461 
23462  if (pt_common_collation (&arg1_coll_inf, &arg2_coll_inf, &arg3_coll_inf, args_w_coll_maybe, op_has_3_args,
23463  &common_coll, &common_cs) != 0)
23464  {
23465  goto error;
23466  }
23467 
23468 coerce_arg:
23469  /* step 3 : coerce collation of expression arguments */
23470  if (((arg1_type == PT_TYPE_MAYBE || arg1_need_coerce) && args_having_coll > 0)
23471  || (common_coll != arg1_coll_inf.coll_id && (PT_HAS_COLLATION (arg1_type) || PT_IS_COLLECTION_TYPE (arg1_type))))
23472  {
23473  if (arg1_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg1_type))
23474  {
23475  arg1_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, &arg1_collection_wrap_type);
23476  }
23477  else
23478  {
23479  arg1_wrap_type = PT_TYPE_NONE;
23480  }
23481 
23482  new_node =
23483  pt_coerce_node_collation (parser, arg1, common_coll, common_cs, arg1_coll_inf.can_force_cs,
23484  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg1_wrap_type),
23485  arg1_collection_wrap_type);
23486 
23487  if (new_node == NULL)
23488  {
23489  goto error;
23490  }
23491 
23492  expr->info.expr.arg1 = new_node;
23493  }
23494 
23495  if (((arg2_type == PT_TYPE_MAYBE || arg2_need_coerce) && args_having_coll > 0)
23496  || (common_coll != arg2_coll_inf.coll_id && (PT_HAS_COLLATION (arg2_type) || PT_IS_COLLECTION_TYPE (arg2_type))))
23497  {
23498  if (arg2_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg2_type))
23499  {
23500  arg2_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, &arg1_collection_wrap_type);
23501  }
23502  else
23503  {
23504  arg2_wrap_type = PT_TYPE_NONE;
23505  }
23506 
23507  new_node =
23508  pt_coerce_node_collation (parser, arg2, common_coll, common_cs, arg2_coll_inf.can_force_cs,
23509  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg2_wrap_type),
23510  arg2_collection_wrap_type);
23511 
23512  if (new_node == NULL)
23513  {
23514  goto error;
23515  }
23516 
23517  if (reverse_arg2_arg3)
23518  {
23519  expr->info.expr.arg3 = new_node;
23520  }
23521  else
23522  {
23523  expr->info.expr.arg2 = new_node;
23524  }
23525  }
23526 
23527  if (op_has_3_args
23528  && (((arg3_type == PT_TYPE_MAYBE || arg3_need_coerce) && args_having_coll > 0)
23529  || (common_coll != arg3_coll_inf.coll_id && PT_HAS_COLLATION (arg3_type))))
23530  {
23531  if (arg3_type == PT_TYPE_MAYBE)
23532  {
23533  arg3_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, NULL);
23534  }
23535  else
23536  {
23537  arg3_wrap_type = PT_TYPE_NONE;
23538  }
23539 
23540  new_node =
23541  pt_coerce_node_collation (parser, arg3, common_coll, common_cs, arg3_coll_inf.can_force_cs,
23542  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg3_wrap_type),
23543  PT_TYPE_NONE);
23544 
23545  if (new_node == NULL)
23546  {
23547  goto error;
23548  }
23549 
23550  expr->info.expr.arg3 = new_node;
23551  }
23552 
23553  /* step 4: update collation of expression result */
23554 coerce_result:
23555  if (op == PT_CHR || op == PT_CLOB_TO_CHAR)
23556  {
23557  /* for these operators, we don't want the arguments' collations to infere common collation, but special values of
23558  * arg2 */
23559  common_cs = (INTL_CODESET) expr->data_type->info.data_type.units;
23560  common_coll = expr->data_type->info.data_type.collation_id;
23561  }
23562 
23563  if (expr_coll_modifier != -1 && expr->data_type != NULL)
23564  {
23565  if (expr_cs_modifier != common_cs)
23566  {
23568  lang_get_codeset_name (common_cs), lang_get_codeset_name (expr_cs_modifier));
23569  goto error_exit;
23570  }
23571 
23572  common_coll = expr_coll_modifier;
23573  common_cs = expr_cs_modifier;
23574  }
23575 
23576  switch (op)
23577  {
23578  case PT_COALESCE:
23579  case PT_NVL:
23580  case PT_NVL2:
23581  case PT_IFNULL:
23582  case PT_GREATEST:
23583  case PT_LEAST:
23584  case PT_NULLIF:
23585  if (expr->flag.is_wrapped_res_for_coll)
23586  {
23587  break;
23588  }
23589  if (expr->type_enum == PT_TYPE_MAYBE && args_having_coll > 0)
23590  {
23591  assert (args_w_coll_maybe > 0);
23592  if (op == PT_NVL2)
23593  {
23594  if (PT_HAS_COLLATION (arg1_type))
23595  {
23596  expr_wrap_type = arg1_type;
23597  }
23598  else if (PT_HAS_COLLATION (arg2_type))
23599  {
23600  expr_wrap_type = arg2_type;
23601  }
23602  else
23603  {
23604  expr_wrap_type = arg3_type;
23605  }
23606  }
23607  else
23608  {
23609  if (PT_HAS_COLLATION (arg1_type))
23610  {
23611  expr_wrap_type = arg1_type;
23612  }
23613  else
23614  {
23615  expr_wrap_type = arg2_type;
23616  }
23617  }
23618 
23619  assert (PT_HAS_COLLATION (expr_wrap_type));
23620 
23621  new_node =
23622  pt_coerce_node_collation (parser, expr, common_coll, common_cs, true, false,
23623  PT_COLL_WRAP_TYPE_FOR_MAYBE (expr_wrap_type), PT_TYPE_NONE);
23624 
23625  expr->flag.is_wrapped_res_for_coll = 1;
23626  if (new_node == NULL)
23627  {
23628  goto error;
23629  }
23630 
23631  expr = new_node;
23632  break;
23633  }
23634  /* fall through */
23635  case PT_PLUS:
23636  if (expr->type_enum == PT_TYPE_MAYBE)
23637  {
23638  if (args_having_coll == 0)
23639  {
23640  break;
23641  }
23642  }
23643  else if (!PT_HAS_COLLATION (expr->type_enum))
23644  {
23645  break;
23646  }
23647  /* fall through */
23648  case PT_CONCAT:
23649  case PT_CONCAT_WS:
23650  case PT_RPAD:
23651  case PT_LPAD:
23652  case PT_SUBSTRING:
23653  case PT_MID:
23654  case PT_REPLACE:
23655  case PT_TRANSLATE:
23656  case PT_STRCAT:
23657  case PT_DATE_FORMAT:
23658  case PT_TIME_FORMAT:
23659  case PT_LOWER:
23660  case PT_UPPER:
23661  case PT_REPEAT:
23662  case PT_RTRIM:
23663  case PT_LTRIM:
23664  case PT_TRIM:
23665  case PT_LEFT:
23666  case PT_RIGHT:
23667  case PT_SUBSTRING_INDEX:
23668  case PT_DATEF:
23669  case PT_TIMEF:
23670  case PT_IF:
23671  case PT_REVERSE:
23672  case PT_CONNECT_BY_ROOT:
23673  case PT_PRIOR:
23674  case PT_QPRIOR:
23675  case PT_INDEX_PREFIX:
23676  if (args_having_coll > 0)
23677  {
23678  if (expr->type_enum == PT_TYPE_MAYBE)
23679  {
23680  if (expr->flag.is_wrapped_res_for_coll)
23681  {
23682  break;
23683  }
23684  expr_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, NULL);
23685  expr->flag.is_wrapped_res_for_coll = 1;
23686  }
23687  else
23688  {
23689  expr_wrap_type = PT_TYPE_NONE;
23690  }
23691 
23692  new_node =
23693  pt_coerce_node_collation (parser, expr, common_coll, common_cs, true, false,
23694  PT_COLL_WRAP_TYPE_FOR_MAYBE (expr_wrap_type), PT_TYPE_NONE);
23695  if (new_node == NULL)
23696  {
23697  goto error;
23698  }
23699 
23700  expr = new_node;
23701  }
23702  break;
23703  default:
23704  break;
23705  }
23706 
23707  *node = expr;
23708 
23709  return NO_ERROR;
23710 
23711 error:
23713 
23714 error_exit:
23715  return ER_FAILED;
23716 }
23717 
23718 /*
23719  * pt_check_recursive_expr_collation () - checks the collation of a recursive
23720  * expression node
23721  *
23722  * return: error code
23723  * parser(in): parser context
23724  * node(in): a parse tree expression node
23725  *
23726  */
23727 static int
23729 {
23730  PT_NODE *expr = *node;
23731  PT_OP_TYPE op;
23732  int recurs_coll = -1;
23733  INTL_CODESET recurs_cs = INTL_CODESET_NONE;
23734  PT_COLL_COERC_LEV recurs_coerc_level = PT_COLLATION_NOT_APPLICABLE;
23735  bool need_arg_coerc = false;
23736 
23737  assert (expr != NULL);
23738 
23739  op = expr->info.expr.op;
23740  assert (op == PT_DECODE || op == PT_CASE);
23741 
23742  while (PT_IS_RECURSIVE_EXPRESSION (expr) && op == expr->info.expr.op)
23743  {
23744  PT_NODE *arg1 = expr->info.expr.arg1;
23745  PT_NODE *arg2 = expr->info.expr.arg2;
23746  PT_COLL_INFER arg1_coll_infer, arg2_coll_infer;
23747 
23748  arg1_coll_infer.coll_id = arg2_coll_infer.coll_id = -1;
23749 
23750  if (pt_get_collation_info (arg1, &arg1_coll_infer))
23751  {
23752  if (recurs_coll != -1 && recurs_coll != arg1_coll_infer.coll_id
23753  && recurs_coerc_level == arg1_coll_infer.coerc_level)
23754  {
23755  goto error;
23756  }
23757  else
23758  {
23759  if (recurs_coll != -1)
23760  {
23761  need_arg_coerc = true;
23762  }
23763 
23764  if (recurs_coerc_level > arg1_coll_infer.coerc_level || recurs_coll == -1)
23765  {
23766  recurs_coerc_level = arg1_coll_infer.coerc_level;
23767  recurs_coll = arg1_coll_infer.coll_id;
23768  recurs_cs = arg1_coll_infer.codeset;
23769  }
23770  }
23771  }
23772 
23773  if (arg2 != NULL && (arg2->node_type != PT_EXPR || op != arg2->info.expr.op)
23774  && pt_get_collation_info (arg2, &arg2_coll_infer))
23775  {
23776  if (recurs_coll != -1 && recurs_coll != arg2_coll_infer.coll_id
23777  && recurs_coerc_level == arg2_coll_infer.coerc_level)
23778  {
23779  goto error;
23780  }
23781  else
23782  {
23783  if (recurs_coll != -1)
23784  {
23785  need_arg_coerc = true;
23786  }
23787 
23788  if (recurs_coerc_level > arg2_coll_infer.coerc_level || recurs_coll == -1)
23789  {
23790  recurs_coerc_level = arg2_coll_infer.coerc_level;
23791  recurs_coll = arg2_coll_infer.coll_id;
23792  recurs_cs = arg2_coll_infer.codeset;
23793  }
23794  }
23795  }
23796 
23798  expr = arg2;
23799  }
23800 
23801  expr = *node;
23802  while (need_arg_coerc && PT_IS_RECURSIVE_EXPRESSION (expr) && op == expr->info.expr.op)
23803  {
23804  PT_NODE *arg1 = expr->info.expr.arg1;
23805  PT_NODE *arg2 = expr->info.expr.arg2;
23806  PT_COLL_INFER arg1_coll_infer, arg2_coll_infer;
23807 
23808  arg1_coll_infer.coll_id = arg2_coll_infer.coll_id = -1;
23809 
23810  if (PT_HAS_COLLATION (arg1->type_enum) || arg1->type_enum == PT_TYPE_MAYBE)
23811  {
23812  (void) pt_get_collation_info (arg1, &arg1_coll_infer);
23813  }
23814 
23815  if ((PT_HAS_COLLATION (arg1->type_enum) && arg1_coll_infer.coll_id != recurs_coll)
23816  || arg1->type_enum == PT_TYPE_MAYBE)
23817  {
23818  arg1 =
23819  pt_coerce_node_collation (parser, arg1, recurs_coll, recurs_cs, arg1_coll_infer.can_force_cs, false,
23821  if (arg1 == NULL)
23822  {
23823  goto error;
23824  }
23825  expr->info.expr.arg1 = arg1;
23826  }
23827 
23828  if (arg2 != NULL)
23829  {
23830  if (arg2->node_type != PT_EXPR || op != arg2->info.expr.op)
23831  {
23832  if (PT_HAS_COLLATION (arg2->type_enum) || arg2->type_enum == PT_TYPE_MAYBE)
23833  {
23834  (void) pt_get_collation_info (arg2, &arg2_coll_infer);
23835  }
23836 
23837  if ((PT_HAS_COLLATION (arg2->type_enum) && arg2_coll_infer.coll_id != recurs_coll)
23838  || arg2->type_enum == PT_TYPE_MAYBE)
23839  {
23840  arg2 =
23841  pt_coerce_node_collation (parser, arg2, recurs_coll, recurs_cs, arg2_coll_infer.can_force_cs, false,
23843  if (arg2 == NULL)
23844  {
23845  goto error;
23846  }
23847  expr->info.expr.arg2 = arg2;
23848  }
23849  }
23850  else if (arg2->node_type == PT_EXPR && op == arg2->info.expr.op && PT_HAS_COLLATION (arg2->type_enum))
23851  {
23852  /* force collation on recursive expression node */
23853  arg2 =
23854  pt_coerce_node_collation (parser, arg2, recurs_coll, recurs_cs, true, false,
23856  if (arg2 == NULL)
23857  {
23858  goto error;
23859  }
23860  expr->info.expr.arg2 = arg2;
23861  }
23862  }
23863 
23865  expr = arg2;
23866  }
23867 
23868  expr = *node;
23869  if (recurs_coll != -1 && PT_HAS_COLLATION (expr->type_enum))
23870  {
23871  *node =
23872  pt_coerce_node_collation (parser, expr, recurs_coll, recurs_cs, true, false,
23874 
23875  if (*node == NULL)
23876  {
23877  goto error;
23878  }
23879  }
23880 
23881  return NO_ERROR;
23882 
23883 error:
23885 
23886  return ER_FAILED;
23887 }
23888 
23889 /*
23890 * pt_node_to_enumeration_expr () - wrap node with PT_TO_ENUMERATION_VALUE
23891 * expression
23892 * return : new node or null
23893 * parser (in) :
23894 * data_type (in) :
23895 * node (in) :
23896 */
23897 static PT_NODE *
23899 {
23900  PT_NODE *expr = NULL;
23901  if (parser == NULL || data_type == NULL || node == NULL)
23902  {
23903  assert (false);
23904  return NULL;
23905  }
23906 
23907  if (PT_HAS_COLLATION (node->type_enum) && node->data_type != NULL)
23908  {
23909  if (!INTL_CAN_COERCE_CS (node->data_type->info.data_type.units, data_type->info.data_type.units))
23910  {
23913  return node;
23914  }
23915  }
23916 
23917  expr = parser_new_node (parser, PT_EXPR);
23918  if (expr == NULL)
23919  {
23921  return NULL;
23922  }
23923 
23924  expr->info.expr.arg1 = node;
23926  expr->data_type = parser_copy_tree (parser, data_type);
23928  return expr;
23929 }
23930 
23931 /*
23932 * pt_select_list_to_enumeration_expr () - wrap select list with
23933 * PT_TO_ENUMERATION_VALUE expression
23934 * return : new node or null
23935 * parser (in) :
23936 * data_type (in) :
23937 * node (in) :
23938 */
23939 static PT_NODE *
23941 {
23942  PT_NODE *new_node = NULL;
23943 
23944  if (node == NULL || data_type == NULL)
23945  {
23946  return node;
23947  }
23948 
23949  if (!PT_IS_QUERY_NODE_TYPE (node->node_type))
23950  {
23951  return node;
23952  }
23953  switch (node->node_type)
23954  {
23955  case PT_SELECT:
23956  {
23957  PT_NODE *item = NULL;
23958  PT_NODE *prev = NULL;
23959  PT_NODE *select_list = node->info.query.q.select.list;
23960  for (item = select_list; item != NULL; prev = item, item = item->next)
23961  {
23962  if (item->type_enum == PT_TYPE_ENUMERATION)
23963  {
23964  /* nothing to do here */
23965  continue;
23966  }
23967  new_node = pt_node_to_enumeration_expr (parser, data_type, item);
23968  if (new_node == NULL)
23969  {
23970  return NULL;
23971  }
23972  new_node->next = item->next;
23973  item->next = NULL;
23974  item = new_node;
23975  /* first node in the list */
23976  if (prev == NULL)
23977  {
23978  node->info.query.q.select.list = item;
23979  }
23980  else
23981  {
23982  prev->next = item;
23983  }
23984  }
23985  break;
23986  }
23987  case PT_DIFFERENCE:
23988  case PT_INTERSECTION:
23989  case PT_UNION:
23990  new_node = pt_select_list_to_enumeration_expr (parser, data_type, node->info.query.q.union_.arg1);
23991  if (new_node == NULL)
23992  {
23993  return NULL;
23994  }
23995  node->info.query.q.union_.arg1 = new_node;
23996  new_node = pt_select_list_to_enumeration_expr (parser, data_type, node->info.query.q.union_.arg2);
23997  if (new_node == NULL)
23998  {
23999  return NULL;
24000  }
24001  node->info.query.q.union_.arg2 = new_node;
24002  break;
24003  default:
24004  break;
24005  }
24006  return node;
24007 }
24008 
24009 /*
24010 * pt_is_enumeration_special_comparison () - check if the comparison is a
24011 * '=' comparison that involves ENUM types and constants or if it's a IN
24012 * 'IN' comparison in which the left operator is an ENUM.
24013 * return : true if it is a special ENUM comparison or false otherwise.
24014 * arg1 (in) : left argument
24015 * op (in) : expression operator
24016 * arg2 (in) : right argument
24017 */
24018 static bool
24020 {
24021  PT_NODE *arg_tmp = NULL;
24022 
24023  if (arg1 == NULL || arg2 == NULL)
24024  {
24025  return false;
24026  }
24027 
24028  switch (op)
24029  {
24030  case PT_EQ:
24031  case PT_NE:
24032  case PT_NULLSAFE_EQ:
24033  if (arg1->type_enum != PT_TYPE_ENUMERATION)
24034  {
24035  if (arg2->type_enum != PT_TYPE_ENUMERATION)
24036  {
24037  return false;
24038  }
24039 
24040  arg_tmp = arg1;
24041  arg1 = arg2;
24042  arg2 = arg_tmp;
24043  }
24044  else if (arg2->type_enum == PT_TYPE_ENUMERATION && arg1->data_type != NULL && arg2->data_type != NULL)
24045  {
24046  if (pt_is_same_enum_data_type (arg1->data_type, arg2->data_type))
24047  {
24048  return true;
24049  }
24050  }
24051  if (arg2->node_type == PT_EXPR)
24052  {
24053  if (arg2->info.expr.op != PT_TO_ENUMERATION_VALUE)
24054  {
24055  return false;
24056  }
24057  }
24058  else
24059  {
24060  if (!PT_IS_CONST (arg2))
24061  {
24062  return false;
24063  }
24064  }
24065  return true;
24066  case PT_IS_IN:
24067  case PT_IS_NOT_IN:
24068  case PT_EQ_SOME:
24069  case PT_NE_SOME:
24070  case PT_EQ_ALL:
24071  case PT_NE_ALL:
24072  return (arg1->type_enum == PT_TYPE_ENUMERATION);
24073  default:
24074  return false;
24075  }
24076 }
24077 
24078 /*
24079 * pt_fix_enumeration_comparison () - fix comparisons for enumeration type
24080 * return : modified node or NULL
24081 * parser (in) :
24082 * expr (in) :
24083 */
24084 static PT_NODE *
24086 {
24087  PT_NODE *arg1 = NULL, *arg2 = NULL;
24088  PT_NODE *node = NULL, *save_next = NULL;
24089  PT_NODE *list = NULL, *list_prev = NULL, **list_start = NULL;
24090  PT_OP_TYPE op;
24091  if (expr == NULL || expr->node_type != PT_EXPR)
24092  {
24093  return expr;
24094  }
24095  op = expr->info.expr.op;
24096  arg1 = expr->info.expr.arg1;
24097  arg2 = expr->info.expr.arg2;
24098  if (arg1->type_enum == PT_TYPE_NULL || arg2->type_enum == PT_TYPE_NULL)
24099  {
24100  return expr;
24101  }
24102 
24103  switch (op)
24104  {
24105  case PT_EQ:
24106  case PT_NE:
24107  case PT_NULLSAFE_EQ:
24108  if (PT_IS_CONST (arg1))
24109  {
24110  if (PT_IS_CONST (arg2))
24111  {
24112  /* const op const does not need special handling */
24113  return expr;
24114  }
24115  /* switch arg1 with arg2 so that we have non cost operand on the left side */
24116  node = arg1;
24117  arg1 = arg2;
24118  arg2 = node;
24119  }
24120 
24121  if (arg1->type_enum != PT_TYPE_ENUMERATION || !PT_IS_CONST (arg2))
24122  {
24123  /* we're only handling enumeration comp const */
24124  return expr;
24125  }
24126  if (pt_is_same_enum_data_type (arg1->data_type, arg2->data_type))
24127  {
24128  return expr;
24129  }
24130 
24131  if (arg2->type_enum == PT_TYPE_ENUMERATION && arg2->data_type != NULL)
24132  {
24133  TP_DOMAIN *domain = pt_data_type_to_db_domain (parser, arg2->data_type, NULL);
24134  DB_VALUE *dbval = pt_value_to_db (parser, arg2);
24135 
24136  if (domain == NULL)
24137  {
24138  return NULL;
24139  }
24140  if (dbval != NULL
24141  && ((db_get_enum_string (dbval) == NULL && db_get_enum_short (dbval) == 0)
24142  || ((db_get_enum_string (dbval) != NULL && db_get_enum_short (dbval) > 0)
24143  && tp_domain_select (domain, dbval, 0, TP_EXACT_MATCH) != NULL)))
24144  {
24145  return expr;
24146  }
24147  }
24148  break;
24149  case PT_IS_IN:
24150  case PT_IS_NOT_IN:
24151  case PT_EQ_SOME:
24152  case PT_NE_SOME:
24153  case PT_EQ_ALL:
24154  case PT_NE_ALL:
24155  break;
24156  default:
24157  return expr;
24158  }
24159 
24160  if (arg1->data_type == NULL || arg1->data_type->info.data_type.enumeration == NULL)
24161  {
24162  /* we don't know the actual enumeration type */
24163  return expr;
24164  }
24165 
24166  switch (op)
24167  {
24168  case PT_EQ:
24169  case PT_NE:
24170  case PT_NULLSAFE_EQ:
24171  node = pt_node_to_enumeration_expr (parser, arg1->data_type, arg2);
24172  if (node == NULL)
24173  {
24174  return NULL;
24175  }
24176  arg2 = node;
24177  break;
24178  case PT_IS_IN:
24179  case PT_IS_NOT_IN:
24180  case PT_EQ_SOME:
24181  case PT_NE_SOME:
24182  case PT_EQ_ALL:
24183  case PT_NE_ALL:
24184  if (PT_IS_QUERY_NODE_TYPE (arg2->node_type))
24185  {
24186  node = pt_select_list_to_enumeration_expr (parser, arg1->data_type, arg2);
24187  if (node == NULL)
24188  {
24189  return NULL;
24190  }
24191  arg2 = node;
24192  break;
24193  }
24194  /* not a subquery */
24195  switch (arg2->node_type)
24196  {
24197  case PT_VALUE:
24198  assert (PT_IS_COLLECTION_TYPE (arg2->type_enum) || arg2->type_enum == PT_TYPE_EXPR_SET);
24199  /* convert this value to a multiset */
24200  node = parser_new_node (parser, PT_FUNCTION);
24201  if (node == NULL)
24202  {
24204  return NULL;
24205  }
24206  node->info.function.function_type = F_SET;
24207  node->info.function.arg_list = arg2->info.value.data_value.set;
24208  node->type_enum = arg2->type_enum;
24209 
24210  arg2->info.value.data_value.set = NULL;
24211  parser_free_tree (parser, arg2);
24212  arg2 = node;
24213 
24214  /* fall through */
24215 
24216  case PT_FUNCTION:
24217  list = arg2->info.function.arg_list;
24218  list_start = &arg2->info.function.arg_list;
24219  break;
24220 
24221  default:
24222  return expr;
24223  }
24224 
24225  while (list != NULL)
24226  {
24227  /* Skip nodes that already have been wrapped with PT_TO_ENUMERATION_VALUE expression or have the correct type
24228  */
24229  if ((list->node_type == PT_EXPR && list->info.expr.op == PT_TO_ENUMERATION_VALUE)
24230  || (list->type_enum == PT_TYPE_ENUMERATION
24231  && pt_is_same_enum_data_type (arg1->data_type, list->data_type)))
24232  {
24233  list_prev = list;
24234  list = list->next;
24235  continue;
24236  }
24237 
24238  save_next = list->next;
24239  list->next = NULL;
24240  node = pt_node_to_enumeration_expr (parser, arg1->data_type, list);
24241  if (node == NULL)
24242  {
24243  return NULL;
24244  }
24245 
24246  node->next = save_next;
24247  if (list_prev == NULL)
24248  {
24249  *list_start = node;
24250  }
24251  else
24252  {
24253  list_prev->next = node;
24254  }
24255  list_prev = node;
24256  list = node->next;
24257  }
24258  if (arg2->data_type != NULL)
24259  {
24260  parser_free_tree (parser, arg2->data_type);
24261  arg2->data_type = NULL;
24262  }
24263  (void) pt_add_type_to_set (parser, arg2->info.function.arg_list, &arg2->data_type);
24264  break;
24265 
24266  default:
24267  break;
24268  }
24269 
24270  expr->info.expr.arg1 = arg1;
24271  expr->info.expr.arg2 = arg2;
24272 
24273  return expr;
24274 }
24275 
24276 /*
24277  * pt_get_common_arg_type_of_width_bucket () -
24278  * get the common type of args that should have the same types
24279  * width_bucket (arg1, arg2, arg3, arg4);
24280  * arg2 and arg3 should be the same type
24281  * return:
24282  * parser(in):
24283  * node(in):
24284  */
24285 static PT_TYPE_ENUM
24287 {
24288  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
24289  PT_NODE *arg1 = NULL;
24290  PT_NODE *arg2 = NULL;
24291  PT_NODE *arg3 = NULL;
24292  PT_NODE *between, *between_ge_lt;
24293 
24294  assert (node != NULL && node->node_type == PT_EXPR && node->info.expr.op == PT_WIDTH_BUCKET);
24295 
24296  arg1 = node->info.expr.arg1;
24297  assert (arg1 != NULL);
24298 
24299  between = node->info.expr.arg2;
24300  assert (between != NULL);
24301  if (between->node_type != PT_EXPR || between->info.expr.op != PT_BETWEEN)
24302  {
24303  return PT_TYPE_NONE;
24304  }
24305 
24306  between_ge_lt = between->info.expr.arg2;
24307  assert (between_ge_lt != NULL);
24308  if (between_ge_lt->node_type != PT_EXPR || between_ge_lt->info.expr.op != PT_BETWEEN_GE_LT)
24309  {
24310  return PT_TYPE_NONE;
24311  }
24312 
24313  arg2 = between_ge_lt->info.expr.arg1;
24314  arg3 = between_ge_lt->info.expr.arg2;
24315  if (arg2 == NULL || arg3 == NULL)
24316  {
24317  return PT_TYPE_NONE;
24318  }
24319 
24320  /* get the common type for arg2 and arg3 */
24321  common_type = pt_common_type (arg2->type_enum, arg3->type_enum);
24322  if (common_type == PT_TYPE_NONE)
24323  {
24324  /* if no common type for arg2 and arg3, try arg1_type */
24325  common_type = arg1->type_enum;
24326  }
24327 
24328  return common_type;
24329 }
24330 
24331 /*
24332  * pt_is_const_foldable_width_bucket () - check whether width_bucket function
24333  * is constant foldable or not.
24334  * return: true/false
24335  * parser(in):
24336  * expr(in):
24337  */
24338 static bool
24340 {
24341  PT_NODE *opd1 = NULL, *opd2 = NULL, *opd3 = NULL;
24342  PT_NODE *between_ge_lt = NULL;
24343  PT_NODE *between_ge_lt_arg1 = NULL, *between_ge_lt_arg2 = NULL;
24344 
24345  assert (expr->info.expr.op == PT_WIDTH_BUCKET);
24346 
24347  opd1 = expr->info.expr.arg1;
24348  opd2 = expr->info.expr.arg2;
24349  opd3 = expr->info.expr.arg3;
24350  assert (opd1 != NULL && opd2 != NULL && opd3 != NULL && opd2->node_type == PT_EXPR
24351  && opd2->info.expr.op == PT_BETWEEN);
24352 
24353  if (opd1->node_type == PT_VALUE && opd3->node_type == PT_VALUE)
24354  {
24355  between_ge_lt = opd2->info.expr.arg2;
24356  assert (between_ge_lt != NULL && between_ge_lt->node_type == PT_EXPR
24357  && between_ge_lt->info.expr.op == PT_BETWEEN_GE_LT);
24358 
24359  between_ge_lt_arg1 = between_ge_lt->info.expr.arg1;
24360  assert (between_ge_lt_arg1 != NULL);
24361 
24362  if (between_ge_lt_arg1->node_type == PT_VALUE)
24363  {
24364  between_ge_lt_arg2 = between_ge_lt->info.expr.arg2;
24365  assert (between_ge_lt_arg2 != NULL);
24366 
24367  if (between_ge_lt_arg2->node_type == PT_VALUE)
24368  {
24369  return true;
24370  }
24371  }
24372  }
24373 
24374  return false;
24375 }
24376 
24377 /*
24378  * pt_wrap_type_for_collation () - Determines the string type (VARCHAR or
24379  * VARNCHAR) to be used for wrap with cast or set as expected domain
24380  * onto a "TYPE_MAYBE" argument node. It also determines the
24381  * collection type to use if common argument type is collection.
24382  *
24383  * return: common type to use when wrapping with cast or setting expected
24384  * domain. If 'wrap_type_collection' is needed, this is the type
24385  * of collection component.
24386  *
24387  * arg1(in):
24388  * arg2(in):
24389  * arg3(in):
24390  * wrap_type_collection(out): collection type to use
24391  *
24392  * Note: this function assumes that mixed VARCHAR-VARNCHAR arguments have
24393  * already been detected as errors by type inference.
24394  */
24395 static PT_TYPE_ENUM
24396 pt_wrap_type_for_collation (const PT_NODE * arg1, const PT_NODE * arg2, const PT_NODE * arg3,
24397  PT_TYPE_ENUM * wrap_type_collection)
24398 {
24399  PT_TYPE_ENUM common_type = PT_TYPE_VARCHAR;
24400  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE;
24401 
24402  if (arg1)
24403  {
24404  arg1_type = arg1->type_enum;
24405  }
24406 
24407  if (arg2)
24408  {
24409  arg2_type = arg2->type_enum;
24410  }
24411 
24412  if (arg3)
24413  {
24414  arg3_type = arg3->type_enum;
24415  }
24416 
24417  if (wrap_type_collection != NULL)
24418  {
24419  *wrap_type_collection = PT_TYPE_NONE;
24420  }
24421 
24423  || PT_IS_NATIONAL_CHAR_STRING_TYPE (arg3_type))
24424  {
24425  common_type = PT_TYPE_VARNCHAR;
24426  }
24427  else if (wrap_type_collection != NULL)
24428  {
24429  const PT_NODE *arg_collection = NULL;
24430  assert (!PT_IS_COLLECTION_TYPE (arg3_type));
24431 
24432  if (PT_IS_COLLECTION_TYPE (arg1_type))
24433  {
24434  *wrap_type_collection = arg1_type;
24435  arg_collection = arg1;
24436  }
24437  else if (PT_IS_COLLECTION_TYPE (arg2_type))
24438  {
24439  *wrap_type_collection = arg2_type;
24440  arg_collection = arg2;
24441  }
24442 
24443  if (arg_collection != NULL && arg_collection->data_type != NULL)
24444  {
24445  PT_NODE *dt;
24446  dt = arg_collection->data_type;
24447 
24448  /* check if wrap with cast is necessary */
24449  while (dt != NULL)
24450  {
24452  {
24453  common_type = dt->type_enum;
24454  break;
24455  }
24456  dt = dt->next;
24457  }
24458  }
24459  else if (arg_collection != NULL && arg_collection->expected_domain != NULL)
24460  {
24461  TP_DOMAIN *dom;
24462 
24463  dom = arg_collection->expected_domain->setdomain;
24464  while (dom != NULL)
24465  {
24466  if (TP_IS_CHAR_TYPE (TP_DOMAIN_TYPE (dom)))
24467  {
24468  common_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (dom));
24469  break;
24470  }
24471  }
24472  }
24473  }
24474 
24475  return common_type;
24476 }
24477 
24478 /*
24479  * pt_fix_arguments_collation_flag () - checks an expression node having
24480  * arguments inner-expression of PT_CAST and clears their collation
24481  * flag if expression's signature allows it.
24482  *
24483  * return:
24484  * expr(in):
24485  *
24486  * Note : When doing walk tree, the collation inference may wrap with cast
24487  * the result of expression using TP_DOMAIN_COLL_ENFORCE flag (when the
24488  * collations of arguments can infer a common collation, but the type
24489  * cannot be strictly determined).
24490  * If such expression result is argument in an upper-level expression,
24491  * we atttempt promoting the cast domain to a normal one
24492  * (TP_DOMAIN_COLL_NORMAL).
24493  * Collation flag promoting is required for expressions which allow
24494  * only string type for a certain argument.
24495  * LOWER (? + col) -> the argument of LOWER has the same type as
24496  * 'col', and is CAST (? + col as type with TP_DOMAIN_COLL_ENFORCE)
24497  * This function, changes the CAST to a normal one.
24498  */
24499 static void
24501 {
24503  int i;
24504  PT_TYPE_ENUM arg1_sig_type = PT_TYPE_NONE, arg2_sig_type = PT_TYPE_NONE, arg3_sig_type = PT_TYPE_NONE;
24505  PT_NODE *arg;
24506 
24507  assert (expr->node_type == PT_EXPR);
24508 
24509  if ((expr->info.expr.arg1 == NULL || expr->info.expr.arg1->node_type != PT_EXPR
24510  || expr->info.expr.arg1->info.expr.op != PT_CAST)
24511  && (expr->info.expr.arg2 == NULL || expr->info.expr.arg2->node_type != PT_EXPR
24512  || expr->info.expr.arg2->info.expr.op != PT_CAST)
24513  && (expr->info.expr.arg3 == NULL || expr->info.expr.arg3->node_type != PT_EXPR
24514  || expr->info.expr.arg3->info.expr.op != PT_CAST))
24515  {
24516  return;
24517  }
24518 
24519  if (!pt_get_expression_definition (expr->info.expr.op, &def))
24520  {
24521  return;
24522  }
24523 
24524  /* for each argument, determine a common type between signatures : if all signatures allows only data types having
24525  * collation, we can promote the collation flag, if not - the signature type (argx_sig_type) is set to TYPE_NULL, and
24526  * the collation flag is not promoted */
24527  for (i = 0; i < def.overloads_count; i++)
24528  {
24529  PT_TYPE_ENUM arg_curr_sig_type;
24530 
24531  if (expr->info.expr.arg1 != NULL)
24532  {
24533  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg1_type, expr->info.expr.arg1->type_enum);
24534 
24535  if (arg1_sig_type == PT_TYPE_NONE)
24536  {
24537  arg1_sig_type = arg_curr_sig_type;
24538  }
24539  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24540  {
24541  arg1_sig_type = PT_TYPE_NULL;
24542  }
24543  }
24544 
24545  if (expr->info.expr.arg2 != NULL)
24546  {
24547  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg2_type, expr->info.expr.arg2->type_enum);
24548 
24549  if (arg2_sig_type == PT_TYPE_NONE)
24550  {
24551  arg2_sig_type = arg_curr_sig_type;
24552  }
24553  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24554  {
24555  arg2_sig_type = PT_TYPE_NULL;
24556  }
24557  }
24558 
24559  if (expr->info.expr.arg3 != NULL)
24560  {
24561  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg3_type, expr->info.expr.arg3->type_enum);
24562 
24563 
24564  if (arg3_sig_type == PT_TYPE_NONE)
24565  {
24566  arg3_sig_type = arg_curr_sig_type;
24567  }
24568  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24569  {
24570  arg3_sig_type = PT_TYPE_NULL;
24571  }
24572  }
24573  }
24574 
24575  if (PT_HAS_COLLATION (arg1_sig_type) && expr->info.expr.arg1 != NULL)
24576  {
24577  arg = expr->info.expr.arg1;
24578 
24579  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
24580  {
24582  {
24584  }
24585 
24586  if (arg->info.expr.cast_type != NULL
24588  {
24590  }
24591  }
24592  }
24593 
24594  if (PT_HAS_COLLATION (arg2_sig_type) && expr->info.expr.arg2 != NULL)
24595  {
24596  arg = expr->info.expr.arg2;
24597 
24598  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
24599  {
24601  {
24603  }
24604 
24605  if (arg->info.expr.cast_type != NULL
24607  {
24609  }
24610  }
24611  }
24612 
24613  if (PT_HAS_COLLATION (arg3_sig_type) && expr->info.expr.arg3 != NULL)
24614  {
24615  arg = expr->info.expr.arg3;
24616 
24617  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
24618  {
24620  {
24622  }
24623 
24624  if (arg->info.expr.cast_type != NULL
24626  {
24628  }
24629  }
24630  }
24631 }
24632 
24633 /*
24634  * pt_check_function_collation () - checks the collation of function
24635  * (PT_FUNCTION)
24636  *
24637  * return: error code
24638  * parser(in): parser context
24639  * node(in/out): a parse tree function node
24640  *
24641  */
24642 static PT_NODE *
24644 {
24645  PT_NODE *arg_list, *arg, *prev_arg, *new_node;
24646  PT_COLL_INFER common_coll_infer, res_coll_infer;
24647  bool need_arg_coerc = false;
24648  TP_DOMAIN_COLL_ACTION res_collation_flag = TP_DOMAIN_COLL_LEAVE;
24649  FUNC_TYPE fcode;
24650 
24651  assert (node != NULL);
24652  assert (node->node_type == PT_FUNCTION);
24653 
24654  if (node->info.function.arg_list == NULL)
24655  {
24656  return node;
24657  }
24658 
24659  fcode = node->info.function.function_type;
24660  arg_list = node->info.function.arg_list;
24661  prev_arg = NULL;
24662 
24663  if (fcode == F_ELT)
24664  {
24665  if (arg_list->next == NULL)
24666  {
24667  return node;
24668  }
24669  arg_list = arg_list->next;
24670  prev_arg = arg_list;
24671  }
24672 
24673  arg = arg_list;
24674 
24675  common_coll_infer.coll_id = -1;
24676  common_coll_infer.codeset = INTL_CODESET_NONE;
24677  common_coll_infer.can_force_cs = true;
24678  common_coll_infer.coerc_level = PT_COLLATION_NOT_APPLICABLE;
24679  while (arg != NULL)
24680  {
24681  PT_COLL_INFER arg_coll_infer;
24682 
24683  arg_coll_infer.coll_id = -1;
24684 
24685  if (arg->type_enum != PT_TYPE_MAYBE && !(PT_IS_CAST_MAYBE (arg)))
24686  {
24687  res_collation_flag = TP_DOMAIN_COLL_NORMAL;
24688  }
24689 
24690  if ((PT_HAS_COLLATION (arg->type_enum) || arg->type_enum == PT_TYPE_MAYBE)
24691  && pt_get_collation_info (arg, &arg_coll_infer))
24692  {
24693  bool apply_common_coll = false;
24694  int common_coll;
24695  INTL_CODESET common_cs;
24696 
24697  if (common_coll_infer.coll_id != -1 || common_coll_infer.coll_id != arg_coll_infer.coll_id)
24698  {
24699  need_arg_coerc = true;
24700  }
24701 
24702  if (common_coll_infer.coll_id == -1)
24703  {
24704  apply_common_coll = true;
24705  }
24706  else
24707  {
24708  if (pt_common_collation (&common_coll_infer, &arg_coll_infer, NULL, 2, false, &common_coll, &common_cs) ==
24709  0)
24710  {
24711  if (common_coll != common_coll_infer.coll_id)
24712  {
24713  apply_common_coll = true;
24714  }
24715  }
24716  else
24717  {
24718  goto error_collation;
24719  }
24720  }
24721 
24722  if (apply_common_coll)
24723  {
24724  common_coll_infer = arg_coll_infer;
24725  }
24726  }
24727 
24728  arg = arg->next;
24729  }
24730 
24731  if (common_coll_infer.coll_id == -1)
24732  {
24733  return node;
24734  }
24735 
24736  arg = arg_list;
24737  while (need_arg_coerc && arg != NULL)
24738  {
24739  PT_COLL_INFER arg_coll_infer;
24740 
24741  arg_coll_infer.coll_id = -1;
24742 
24743  if (!(PT_HAS_COLLATION (arg->type_enum) || arg->type_enum == PT_TYPE_MAYBE))
24744  {
24745  prev_arg = arg;
24746  arg = arg->next;
24747  continue;
24748  }
24749 
24750  if (!pt_get_collation_info (arg, &arg_coll_infer))
24751  {
24752  prev_arg = arg;
24753  arg = arg->next;
24754  continue;
24755  }
24756 
24757  if (common_coll_infer.coll_id != arg_coll_infer.coll_id)
24758  {
24759  PT_NODE *save_next;
24760 
24761  save_next = arg->next;
24762 
24763  new_node =
24764  pt_coerce_node_collation (parser, arg, common_coll_infer.coll_id, common_coll_infer.codeset,
24765  arg_coll_infer.can_force_cs, false, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg->type_enum),
24766  PT_TYPE_NONE);
24767 
24768  if (new_node != NULL)
24769  {
24770  arg = new_node;
24771  if (prev_arg == NULL)
24772  {
24773  node->info.function.arg_list = arg;
24774  }
24775  else
24776  {
24777  prev_arg->next = arg;
24778  }
24779  arg->next = save_next;
24780  }
24781  }
24782 
24783  prev_arg = arg;
24784  arg = arg->next;
24785  }
24786 
24787  if (need_arg_coerc)
24788  {
24789  switch (fcode)
24790  {
24791  case F_SET:
24792  case F_MULTISET:
24793  case F_SEQUENCE:
24794  /* add the new data_type to the set of data_types */
24795  pt_add_type_to_set (parser, arg_list, &node->data_type);
24796  break;
24797  default:
24798  break;
24799  }
24800  }
24801 
24802  if (PT_HAS_COLLATION (node->type_enum) && res_collation_flag == TP_DOMAIN_COLL_LEAVE && node->data_type != NULL)
24803  {
24804  node->data_type->info.data_type.collation_flag = res_collation_flag;
24805  }
24806  else if ((PT_HAS_COLLATION (node->type_enum) || node->type_enum == PT_TYPE_MAYBE)
24807  && pt_get_collation_info (node, &res_coll_infer) && res_coll_infer.coll_id != common_coll_infer.coll_id)
24808  {
24809  new_node =
24810  pt_coerce_node_collation (parser, node, common_coll_infer.coll_id, common_coll_infer.codeset, true, false,
24812  if (new_node != NULL)
24813  {
24814  node = new_node;
24815  }
24816  }
24817 
24818  return node;
24819 
24820 error_collation:
24823  return node;
24824 }
24825 
24826 /* pt_hv_consistent_data_type_with_domain - update data type of HOST_VAR node
24827  * to be the same with expected domain
24828  * If data_type is not present, no
24829  * change is made.
24830  * return: void
24831  * node (in/out) :
24832  */
24833 static void
24835 {
24836  PT_NODE *p = NULL;
24837 
24838  assert (node != NULL);
24839  assert (node->expected_domain != NULL);
24840 
24841  if (node->node_type != PT_HOST_VAR)
24842  {
24843  return;
24844  }
24845 
24846  pt_update_host_var_data_type (parser, node);
24847 
24848  for (p = node->or_next; p != NULL; p = p->or_next)
24849  {
24850  pt_update_host_var_data_type (parser, p);
24851  }
24852 }
24853 
24854 /* pt_update_host_var_data_type - update data type of HOST_VAR node
24855  * to be the same with expected domain
24856  * If data_type is not present, no
24857  * change is made.
24858  * return: void
24859  * hv_node (in/out) :
24860  */
24861 static void
24863 {
24864  PT_NODE *dt;
24865  TP_DOMAIN *dom;
24866 
24867  if (hv_node->node_type != PT_HOST_VAR || hv_node->data_type == NULL || hv_node->expected_domain == NULL)
24868  {
24869  return;
24870  }
24871 
24872  dt = hv_node->data_type;
24873  dom = hv_node->expected_domain;
24874 
24876  {
24878  dt->info.data_type.units = dom->codeset;
24880  }
24881  else if (dt->type_enum != pt_db_to_type_enum (TP_DOMAIN_TYPE (dom))
24882  || (dt->type_enum == PT_TYPE_NUMERIC
24883  && (dt->info.data_type.precision != dom->precision || dt->info.data_type.dec_precision != dom->scale)))
24884  {
24885  parser_free_node (parser, hv_node->data_type);
24886  hv_node->data_type = pt_domain_to_data_type (parser, dom);
24887  }
24888 }
24889 
24890 static bool
24892 {
24893  assert (node != NULL);
24894  assert (node->node_type == PT_EXPR);
24895  assert (node->info.expr.op == PT_CAST);
24896 
24897  if (node->info.expr.arg1 != NULL && node->info.expr.arg1->data_type != NULL && node->info.expr.cast_type != NULL
24900  && node->info.expr.cast_type->info.data_type.units != codeset)
24901  {
24902  return true;
24903  }
24904 
24905  return false;
24906 }
24907 
24908 //
24909 // pt_to_variable_size_type () - convert fixed size types to the equivalent variable size types
24910 //
24911 // return : if input type can have variable size, it returns the variable size. otherwise, returns input type
24912 // type_enum (in) : any type
24913 //
24916 {
24917  switch (type_enum)
24918  {
24919  case PT_TYPE_CHAR:
24920  return PT_TYPE_VARCHAR;
24921  case PT_TYPE_NCHAR:
24922  return PT_TYPE_VARNCHAR;
24923  case PT_TYPE_BIT:
24924  return PT_TYPE_VARBIT;
24925  default:
24926  return type_enum;
24927  }
24928 }
#define ER_IT_INCOMPATIBLE_DATATYPE
Definition: error_code.h:503
int julian_encode(int m, int d, int y)
Definition: db_date.c:113
DB_OBJECT * db_find_class(const char *name)
Definition: db_info.c:133
#define PT_NAME_INFO_SET_FLAG(e, f)
Definition: parse_tree.h:2575
#define ER_REGEX_COMPILE_ERROR
Definition: error_code.h:1362
int db_inet_ntoa(DB_VALUE *result_ip_string, const DB_VALUE *number)
int db_evaluate_json_extract(DB_VALUE *result, DB_VALUE *const *args, int num_args)
Definition: arithmetic.c:5511
PT_NODE * pt_name(PARSER_CONTEXT *parser_ptr, const char *name)
DB_C_FLOAT db_get_float(const DB_VALUE *value)
PT_NODE * order_by
Definition: parse_tree.h:2769
int db_sin_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3769
PT_GET_OPT_LVL_INFO get_opt_lvl
Definition: parse_tree.h:3302
PT_NODE * next
Definition: parse_tree.h:3447
PT_NAME_INFO name
Definition: parse_tree.h:3318
int db_date_format(const DB_VALUE *date_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
int db_set_compare(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1833
int set_difference(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3905
#define TP_IS_DATE_OR_TIME_TYPE(typeid)
TP_DOMAIN_STATUS tp_value_coerce(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain)
int db_time_format(const DB_VALUE *src_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
#define PT_HAS_TIME_PART(t)
Definition: parse_tree.h:214
int db_evaluate_json_replace(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5721
PT_OP_TYPE pt_negate_op(PT_OP_TYPE op)
PT_NODE * pt_wrap_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *arg, PT_TYPE_ENUM new_type, int p, int s, PT_NODE *desired_dt)
unsigned is_wrapped_res_for_coll
Definition: parse_tree.h:3483
int db_make_datetime(DB_VALUE *value, const DB_DATETIME *datetime)
#define PT_IS_QUERY(n)
Definition: parse_tree.h:296
int db_find_string_in_in_set(const DB_VALUE *needle, const DB_VALUE *stack, DB_VALUE *result)
static OID null_oid
Definition: file_hash.c:50
PT_NODE * arg_list
Definition: parse_tree.h:2258
#define NO_ERROR
Definition: error_code.h:46
PT_UNION_INFO union_
Definition: parse_tree.h:2782
long int lrand
Definition: parse_tree.h:3588
static int pt_check_expr_collation(PARSER_CONTEXT *parser, PT_NODE **node)
int db_evaluate_json_merge_preserve(DB_VALUE *result, DB_VALUE *const *arg, const int num_args)
Definition: arithmetic.c:6163
MISC_OPERAND pt_misc_to_qp_misc_operand(PT_MISC_TYPE misc_specifier)
Definition: parse_dbi.c:91
int db_get_date_weekday(const DB_VALUE *src_date, const int mode, DB_VALUE *result)
int db_get_date_quarter(const DB_VALUE *src_date, DB_VALUE *result)
int db_get_date_totaldays(const DB_VALUE *src_date, DB_VALUE *result)
#define PT_EXPR_INFO_CAST_SHOULD_FOLD
Definition: parse_tree.h:2227
#define DB_ROW_COUNT_NOT_SET
Definition: dbtype_def.h:486
DB_COLLECTION * db_get_set(const DB_VALUE *value)
PT_METHOD_CALL_INFO method_call
Definition: parse_tree.h:3316
#define DB_MAX_BIT_PRECISION
Definition: dbtype_def.h:549
#define LANG_SYS_COLLATION
int pr_data_writeval_disk_size(DB_VALUE *value)
bool pt_is_aggregate_function(PARSER_CONTEXT *parser, const PT_NODE *node)
static const char * pt_class_name(const PT_NODE *type)
#define LANG_GET_BINARY_COLLATION(c)
PT_UPDATE_INFO update
Definition: parse_tree.h:3353
static PT_NODE * pt_evaluate_new_data_type(const PT_TYPE_ENUM old_type, const PT_TYPE_ENUM new_type, PT_NODE *data_type)
int db_string_space(DB_VALUE const *count, DB_VALUE *result)
PT_ARG_TYPE return_type
PT_MERGE_INFO merge
Definition: parse_tree.h:3315
UINTPTR id
Definition: parse_tree.h:2144
#define PT_COLL_WRAP_TYPE_FOR_MAYBE(type)
PT_OP_TYPE pt_converse_op(PT_OP_TYPE op)
TP_DOMAIN * tp_domain_resolve_default_w_coll(DB_TYPE type, int coll_id, TP_DOMAIN_COLL_ACTION coll_flag)
pt_generic_type_enum generic_type
Definition: parse_type.hpp:71
int tz_timestamptz_fix_zone(const DB_TIMESTAMPTZ *src_ts_tz, DB_TIMESTAMPTZ *dest_ts_tz)
Definition: tz_support.c:1843
int pt_comp_to_between_op(PT_OP_TYPE left, PT_OP_TYPE right, PT_COMP_TO_BETWEEN_OP_CODE_TYPE type, PT_OP_TYPE *between)
DB_VALUE_COMPARE_RESULT tp_value_compare(const DB_VALUE *value1, const DB_VALUE *value2, int allow_coercion, int total_order)
#define PT_IS_CHAR_STRING_TYPE(t)
Definition: parse_tree.h:164
PT_STATEMENT_INFO info
Definition: parse_tree.h:3487
TP_DOMAIN * expected_domain
Definition: parse_tree.h:3452
#define PT_ERRORm(parser, node, setNo, msgNo)
Definition: parse_tree.h:63
#define ASSERT_ERROR()
PT_NODE * using_index
Definition: parse_tree.h:2693
#define MSGCAT_SEMANTIC_CANT_EXTRACT_FROM
#define MSGCAT_SEMANTIC_COLLATE_NOT_ALLOWED
static PT_NODE * pt_coerce_expr_arguments(PARSER_CONTEXT *parser, PT_NODE *expr, PT_NODE *arg1, PT_NODE *arg2, PT_NODE *arg3, EXPRESSION_SIGNATURE sig)
#define db_locate_numeric(value)
static int pt_coerce_str_to_time_date_utime_datetime(PARSER_CONTEXT *parser, PT_NODE *src, PT_TYPE_ENUM *result_type)
PT_MISC_TYPE var_type
Definition: parse_tree.h:2317
STATEMENT_SET_FOLD
PT_NODE * pt_where_type_keep_true(PARSER_CONTEXT *parser, PT_NODE *where)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_2
PT_NODE * expr
Definition: parse_tree.h:3188
int pt_type_generic_func(PARSER_CONTEXT *parser, PT_NODE *node)
TP_DOMAIN * tp_domain_select(const TP_DOMAIN *domain_list, const DB_VALUE *value, int allow_coercion, TP_MATCH exact_match)
#define MSGCAT_SEMANTIC_WANT_LOGICAL_WHERE
bool pt_is_op_hv_late_bind(PT_OP_TYPE op)
#define QSTR_IS_ANY_CHAR(s)
Definition: string_opfunc.h:46
unsigned char codeset
Definition: object_domain.h:91
static PT_NODE * pt_eval_type(PARSER_CONTEXT *parser, PT_NODE *node, void *arg, int *continue_walk)
STATEMENT_SET_FOLD pt_check_union_is_foldable(PARSER_CONTEXT *parser, PT_NODE *union_node)
int set_issome(DB_VALUE *value, DB_COLLECTION *set, PT_OP_TYPE op, int do_coercion)
Definition: set_object.c:3240
#define MSGCAT_SEMANTIC_DATA_OVERFLOW_ON
DB_CONST_C_BIT db_get_bit(const DB_VALUE *value, int *length)
bool obt_Last_insert_id_generated
int db_bigint_to_binary_string(const DB_VALUE *src_bigint, DB_VALUE *result)
#define TP_IS_SET_TYPE(typenum)
bool is_collate_allowed
Definition: parse_tree.h:3067
int db_add_int_to_datetime(DB_DATETIME *datetime, DB_BIGINT bi2, DB_DATETIME *result_datetime)
Definition: db_date.c:4656
PT_MISC_TYPE
Definition: parse_tree.h:983
static PT_NODE * pt_eval_function_type_new(PARSER_CONTEXT *parser, PT_NODE *node)
int db_string_chr(DB_VALUE *res, DB_VALUE *dbval1, DB_VALUE *dbval2)
collation_result
int db_string_index_prefix(const DB_VALUE *string1, const DB_VALUE *string2, const DB_VALUE *index_type, DB_VALUE *prefix_index)
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
#define PT_EXPR_INFO_CAST_WRAP
Definition: parse_tree.h:2236
int tp_more_general_type(const DB_TYPE type1, const DB_TYPE type2)
#define DB_DEFAULT_NUMERIC_DIVISION_SCALE
Definition: dbtype_def.h:570
int db_string_insert_substring(DB_VALUE *src_string, const DB_VALUE *position, const DB_VALUE *length, DB_VALUE *sub_string, DB_VALUE *result)
int db_string_regexp_replace(DB_VALUE *result, DB_VALUE *args[], int const num_args, cub_regex_object **comp_regex, char **comp_pattern)
DB_TYPE
Definition: dbtype_def.h:670
#define LANG_IS_COERCIBLE_COLL(c)
DB_C_DOUBLE db_get_double(const DB_VALUE *value)
void tz_timestamp_decode_no_leap_sec(int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp, int *secondsp)
Definition: tz_support.c:601
#define ER_FAILED
Definition: error_code.h:47
void numeric_coerce_num_to_double(DB_C_NUMERIC num, int scale, double *adouble)
int db_char_to_blob(const DB_VALUE *src_value, DB_VALUE *result_value)
#define COMPARE_BETWEEN_OPERATOR_COUNT
PT_TYPE_ENUM recursive_type
Definition: parse_tree.h:2245
static bool pt_is_op_with_forced_common_type(PT_OP_TYPE op)
int db_new_time(DB_VALUE *time_val, DB_VALUE *tz_source, DB_VALUE *tz_dest, DB_VALUE *result_time)
int pt_node_list_to_array(PARSER_CONTEXT *parser, PT_NODE *arg_list, PT_NODE *arg_array[], const int array_size, int *num_args)
int db_convert_sec_to_time(const DB_VALUE *src, DB_VALUE *result)
#define MSGCAT_SEMANTIC_OUT_OF_MEMORY
static bool pt_cast_needs_wrap_for_collation(PT_NODE *node, const INTL_CODESET codeset)
int db_get_string_collation(const DB_VALUE *value)
PT_MISC_TYPE all_or_distinct
Definition: parse_tree.h:2260
#define PT_EXPR_INFO_SET_FLAG(e, f)
Definition: parse_tree.h:2239
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 numeric_db_value_negate(DB_VALUE *answer)
#define MSGCAT_SEMANTIC_COLLATION_OP_ERROR
PT_NODE * search_cond
Definition: parse_tree.h:2925
struct tp_domain * setdomain
Definition: object_domain.h:82
PT_NODE * arg3
Definition: parse_tree.h:2202
int db_evaluate_json_keys(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5855
PT_SPEC_INFO spec
Definition: parse_tree.h:3346
static PT_NODE * pt_propagate_types(PARSER_CONTEXT *parser, PT_NODE *expr, PT_NODE *otype1, PT_NODE *otype2)
#define PT_NODE_DATA_TYPE(n)
Definition: parse_tree.h:470
int db_timestamp_encode_ses(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
Definition: db_date.c:597
PT_NODE * pt_get_select_list(PARSER_CONTEXT *parser, PT_NODE *query)
Definition: query_result.c:404
static bool pt_is_able_to_determine_return_type(const PT_OP_TYPE op)
PT_NODE * arg2
Definition: parse_tree.h:2664
PT_NODE * pt_make_prim_data_type_fortonum(PARSER_CONTEXT *parser, int prec, int scale)
int db_value_clone(DB_VALUE *src, DB_VALUE *dest)
Definition: db_macro.c:1564
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON_1
#define PT_EXPR_INFO_CAST_NOFAIL
Definition: parse_tree.h:2225
void set_make_collection(DB_VALUE *value, DB_COLLECTION *col)
Definition: set_object.c:3965
int correlation_level
Definition: parse_tree.h:2745
PT_TYPE_ENUM pt_common_type(PT_TYPE_ENUM arg1_type, PT_TYPE_ENUM arg2_type)
#define PT_NODE_PRINT_TO_ALIAS(p, n, c)
Definition: parse_tree.h:578
short continued_case
Definition: parse_tree.h:2242
PT_NODE * type_checking()
Definition: func_type.cpp:809
#define SECONDS_OF_ONE_DAY
enum pt_type_enum PT_TYPE_ENUM
Definition: parse_tree.h:962
int db_make_numeric(DB_VALUE *value, const DB_C_NUMERIC num, const int precision, const int scale)
struct setobj * set
Definition: db_set.h:43
static bool pt_is_range_or_comp(PT_OP_TYPE op)
static PT_NODE * pt_fold_const_expr(PARSER_CONTEXT *parser, PT_NODE *expr, void *arg)
unsigned is_added_by_parser
Definition: parse_tree.h:3481
static bool pt_is_enumeration_special_comparison(PT_NODE *arg1, PT_OP_TYPE op, PT_NODE *arg2)
bool numeric_db_value_is_zero(const DB_VALUE *arg)
static bool pt_is_collection_of_type(const PT_NODE *collection, const PT_TYPE_ENUM collection_type, const PT_TYPE_ENUM element_type)
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
void tz_get_session_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:767
#define MAX_OVERLOADS
#define pt_is_query(n)
Definition: parse_tree.h:258
#define DB_MAX_NCHAR_PRECISION
Definition: dbtype_def.h:541
#define ER_QSTR_INVALID_DATA_TYPE
Definition: error_code.h:746
int db_subtract_int_from_datetime(DB_DATETIME *dt1, DB_BIGINT bi2, DB_DATETIME *result_datetime)
Definition: db_date.c:4612
DB_C_NUMERIC db_get_numeric(const DB_VALUE *value)
void pt_set_expected_domain(PT_NODE *node, TP_DOMAIN *domain)
DB_DOMAIN * pt_node_data_type_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *dt, PT_TYPE_ENUM type)
Definition: parse_dbi.c:2029
PT_EXPR_INFO expr
Definition: parse_tree.h:3299
FUNC_TYPE
#define OR_CHECK_SHORT_DIV_OVERFLOW(a, b)
int setobj_get_element_ptr(COL *col, int index, DB_VALUE **result)
Definition: set_object.c:5708
#define DB_DATE_MIN
Definition: dbtype_def.h:649
int db_value_compare(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1855
static PT_NODE * pt_fold_constants_post(PARSER_CONTEXT *parser, PT_NODE *node, void *arg, int *continue_walk)
#define MSGCAT_SEMANTIC_CS_MATCH_COLLATE
static COMPARE_BETWEEN_OPERATOR pt_Compare_between_operator_table[]
PT_TYPE_ENUM pt_db_to_type_enum(const DB_TYPE t)
Definition: parse_dbi.c:2595
#define pt_is_input_parameter(n)
Definition: parse_tree.h:273
INTL_LANG lang_get_lang_id_from_flag(const int flag, bool *has_user_format, bool *has_user_lang)
PT_TYPE_ENUM pt_get_equivalent_type(const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type)
Definition: func_type.cpp:1482
int db_string_reverse(const DB_VALUE *src_str, DB_VALUE *result_str)
struct pt_query_info::@123 flag
#define MSGCAT_SEMANTIC_COLLECTION_EL_COLLATION_ERROR
#define PT_ERRORmf5(parser, node, setNo, msgNo, arg1, arg2, arg3, arg4, arg5)
Definition: parse_tree.h:68
int db_evaluate_json_quote(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5294
static int pt_coerce_3args(PARSER_CONTEXT *parser, PT_NODE *arg1, PT_NODE *arg2, PT_NODE *arg3)
#define PT_IS_DATE_TIME_TYPE(t)
Definition: parse_tree.h:195
int db_make_date(DB_VALUE *value, const int month, const int day, const int year)
union pt_query_info::@124 q
#define OID_SET_NULL(oidp)
Definition: oid.h:85
DB_DATETIMETZ * db_get_datetimetz(const DB_VALUE *value)
int db_make_datetimeltz(DB_VALUE *value, const DB_DATETIME *datetime)
#define OR_CHECK_ADD_OVERFLOW(a, b, c)
unsigned is_cached
int db_exp_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:659
int db_round_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2315
TP_DOMAIN * tp_domain_resolve_value(const DB_VALUE *val, TP_DOMAIN *dbuf)
int db_string_from_base64(DB_VALUE const *src, DB_VALUE *result)
static PT_NODE * pt_eval_opt_type(PARSER_CONTEXT *parser, PT_NODE *node)
struct pt_merge_info::@125 update
#define TP_IS_STRING_TYPE(typeid)
static PT_NODE * pt_fold_constants_pre(PARSER_CONTEXT *parser, PT_NODE *node, void *arg, int *continue_walk)
#define PT_NODE_IS_SESSION_VARIABLE(node)
TP_DOMAIN * tp_domain_copy(const TP_DOMAIN *domain, bool check_cache)
#define PT_IS_NULL_NODE(e)
Definition: parse_tree.h:122
PT_NODE * group_by
Definition: parse_tree.h:2688
DB_DOMAIN * pt_node_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *node, const char *class_name)
Definition: parse_dbi.c:2244
unsigned set_host_var
Definition: parse_tree.h:3605
int tz_conv_tz_time_w_zone_name(const DB_TIME *time_source, const char *source_zone, int len_source, const char *dest_zone, int len_dest, DB_TIME *time_dest)
Definition: tz_support.c:1528
TP_DOMAIN tp_Integer_domain
int er_errid(void)
int db_time_dbval(DB_VALUE *result, const DB_VALUE *datetime_value, const TP_DOMAIN *domain)
#define PT_IS_STRING_TYPE(t)
Definition: parse_tree.h:148
static int pt_coerce_value_internal(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type, bool check_string_precision, bool implicit_coercion)
int db_from_unixtime(const DB_VALUE *src_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
#define PT_NAME_INFO_IS_FLAGED(e, f)
Definition: parse_tree.h:2574
void db_string_free(char *string)
Definition: db_admin.c:2596
bool pt_false_where(PARSER_CONTEXT *parser, PT_NODE *node)
PT_NODE_LIST_INFO node_list
Definition: parse_tree.h:3320
int db_string_sha_one(DB_VALUE const *src, DB_VALUE *result)
int db_to_timestamp(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_timestamp)
int numeric_db_value_sub(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
int set_size(DB_COLLECTION *set)
Definition: set_object.c:3036
#define TP_TIMESTAMPTZ_AS_CHAR_LENGTH
static int pt_check_and_coerce_to_date(PARSER_CONTEXT *parser, PT_NODE *src)
static bool pt_are_unmatchable_types(const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type)
#define DB_VALUE_PRECISION(value)
Definition: dbtype.h:73
int tz_conv_tz_datetime_w_region(const DB_DATETIME *src_dt, const TZ_REGION *src_tz_region, const TZ_REGION *dest_tz_region, DB_DATETIME *dest_dt, TZ_ID *src_tz_id_out, TZ_ID *dest_tz_id_out)
Definition: tz_support.c:3812
int pt_between_to_comp_op(PT_OP_TYPE between, PT_OP_TYPE *left, PT_OP_TYPE *right)
PT_MISC_TYPE option
Definition: parse_tree.h:2287
static PT_NODE * pt_eval_expr_type(PARSER_CONTEXT *parser, PT_NODE *node)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_4
unsigned rewrite_limit
Definition: parse_tree.h:2883
int db_add_time(const DB_VALUE *left, const DB_VALUE *right, DB_VALUE *result, const TP_DOMAIN *domain)
unsigned is_insert_select
Definition: parse_tree.h:2759
enum tp_domain_status TP_DOMAIN_STATUS
const char * pt_show_misc_type(PT_MISC_TYPE p)
int db_timestamp_encode_sys(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
Definition: db_date.c:617
int db_sign_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:429
PT_NODE * attr_list
Definition: parse_tree.h:2333
PT_NODE * pt_where_type(PARSER_CONTEXT *parser, PT_NODE *where)
void pt_add_type_to_set(PARSER_CONTEXT *parser, const PT_NODE *typs, PT_NODE **set)
Definition: parse_dbi.c:198
#define MSGCAT_SEMANTIC_TIME_UNDERFLOW
static PT_NODE * pt_wrap_expr_w_exp_dom_cast(PARSER_CONTEXT *parser, PT_NODE *expr)
static int pt_check_and_coerce_to_time(PARSER_CONTEXT *parser, PT_NODE *src)
int db_blob_length(const DB_VALUE *src_value, DB_VALUE *result_value)
void pt_to_regu_resolve_domain(int *p_precision, int *p_scale, const PT_NODE *node)
PT_NODE * data_type
Definition: parse_tree.h:3453
#define MSGCAT_SEMANTIC_IS_NOT_A
#define DB_DEFAULT_NUMERIC_SCALE
Definition: dbtype_def.h:567
INTL_CODESET codeset
Definition: parse_tree.h:3673
int db_atan2_dbval(DB_VALUE *result, DB_VALUE *value, DB_VALUE *value2)
Definition: arithmetic.c:4006
struct parser_context::@134 flag
int db_log_generic_dbval(DB_VALUE *result, DB_VALUE *value, long b)
Definition: arithmetic.c:4118
#define MSGCAT_SEMANTIC_WANT_LOGICAL_CASE_COND
static bool pt_check_const_fold_op_w_args(PT_OP_TYPE op, DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *arg3, TP_DOMAIN *domain)
int db_string_substring(const MISC_OPERAND substr_operand, const DB_VALUE *src_string, const DB_VALUE *start_position, const DB_VALUE *extraction_length, DB_VALUE *sub_string)
PT_DOT_INFO dot
Definition: parse_tree.h:3287
DB_DOMAIN_INFO domain
Definition: dbtype_def.h:1082
PT_NODE * arg2
Definition: parse_tree.h:2086
#define DB_VALUE_SCALE(value)
Definition: dbtype.h:74
static PT_NODE * pt_eval_type_pre(PARSER_CONTEXT *parser, PT_NODE *node, void *arg, int *continue_walk)
int db_sleep(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4976
#define MSGCAT_SEMANTIC_WANT_TYPE
#define QSTR_IS_BIT(s)
Definition: string_opfunc.h:44
TP_DOMAIN * pt_xasl_node_to_domain(PARSER_CONTEXT *parser, const PT_NODE *node)
static bool does_op_specially_treat_null_arg(PT_OP_TYPE op)
bool pt_is_operator_logical(PT_OP_TYPE op)
static PT_NODE * pt_node_to_enumeration_expr(PARSER_CONTEXT *parser, PT_NODE *data_type, PT_NODE *node)
#define TP_DOUBLE_AS_CHAR_LENGTH
#define MSGCAT_SEMANTIC_CANT_COERCE_TO
void pt_preset_hostvar(PARSER_CONTEXT *parser, PT_NODE *hv_node)
static int pt_product_sets(PARSER_CONTEXT *parser, TP_DOMAIN *domain, DB_VALUE *set1, DB_VALUE *set2, DB_VALUE *result, PT_NODE *o2)
#define OR_CHECK_BIGINT_DIV_OVERFLOW(a, b)
PT_CTE_INFO cte
Definition: parse_tree.h:3282
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_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
FUNC_TYPE function_type
Definition: parse_tree.h:2259
PT_TYPE_ENUM type_enum
Definition: parse_tree.h:3457
PT_NODE * or_next
Definition: parse_tree.h:3448
int db_check_time_date_format(const char *format_s)
Definition: db_date.c:4914
static int pt_coerce_expression_argument(PARSER_CONTEXT *parser, PT_NODE *expr, PT_NODE **arg, const PT_TYPE_ENUM arg_type, PT_NODE *data_type)
int db_string_aes_decrypt(DB_VALUE const *src, DB_VALUE const *key, DB_VALUE *result)
DB_TYPE pt_type_enum_to_db(const PT_TYPE_ENUM t)
Definition: parse_dbi.c:2314
int db_make_string(DB_VALUE *value, DB_CONST_C_CHAR str)
bool pt_list_has_logical_nodes(PT_NODE *list)
DB_MONETARY * db_get_monetary(const DB_VALUE *value)
#define PT_ERRORmf3(parser, node, setNo, msgNo, arg1, arg2, arg3)
Definition: parse_tree.h:66
PT_NODE * limit
Definition: parse_tree.h:2773
int db_string_position(const DB_VALUE *sub_string, const DB_VALUE *src_string, DB_VALUE *result)
PT_MISC_TYPE derived_table_type
Definition: parse_tree.h:2147
unsigned int DB_TIMESTAMP
Definition: dbtype_def.h:759
int db_unix_timestamp(const DB_VALUE *src_date, DB_VALUE *result_timestamp)
#define OR_CHECK_DOUBLE_OVERFLOW(i)
#define DB_MAX_VARBIT_PRECISION
Definition: dbtype_def.h:552
PT_NODE * cast_type
Definition: parse_tree.h:2203
static PT_TYPE_ENUM pt_get_common_datetime_type(PARSER_CONTEXT *parser, PT_TYPE_ENUM common_type, PT_TYPE_ENUM arg1_type, PT_TYPE_ENUM arg2_type, PT_NODE *arg1, PT_NODE *arg2)
PT_NODE * arg1
Definition: parse_tree.h:2663
int pt_wrap_select_list_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *query, PT_TYPE_ENUM new_type, int p, int s, PT_NODE *data_type, bool force_wrap)
int set_union(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3928
enum pt_coll_coerc_lev PT_COLL_COERC_LEV
Definition: parse_tree.h:3667
#define PT_NODE_PRINT_VALUE_TO_TEXT(p, n)
Definition: parse_tree.h:597
int db_add_days_to_year(const DB_VALUE *src_year, const DB_VALUE *src_days, DB_VALUE *result)
int pt_is_between_range_op(PT_OP_TYPE op)
TP_DOMAIN * tp_domain_resolve_default(DB_TYPE type)
int db_string_concatenate(const DB_VALUE *string1, const DB_VALUE *string2, DB_VALUE *result, DB_DATA_STATUS *data_status)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
bool db_string_check_explicit_date(const char *str, int str_len)
Definition: db_date.c:3629
Definition: db_set.h:35
INTL_CODESET codeset
int db_string_lower(const DB_VALUE *string, DB_VALUE *lower_string)
PT_NODE * arg2
Definition: parse_tree.h:2198
LANG_COLLATION * lang_get_collation(const int coll_id)
PT_NODE * del_search_cond
Definition: parse_tree.h:2930
#define TP_TIMESTAMP_AS_CHAR_LENGTH
int db_evaluate_json_array(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5626
PT_FUNCTION_INFO function
Definition: parse_tree.h:3301
enum pt_arg_type::@135 type
int db_cot_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3833
#define TP_FLOAT_AS_CHAR_LENGTH
int db_value_put_encoded_time(DB_VALUE *value, const DB_TIME *time)
Definition: db_macro.c:1357
static PT_TYPE_ENUM pt_infer_common_type(const PT_OP_TYPE op, PT_TYPE_ENUM *arg1, PT_TYPE_ENUM *arg2, PT_TYPE_ENUM *arg3, const TP_DOMAIN *expected_domain)
#define CAST_POINTER_TO_NODE(p)
Definition: parse_tree.h:607
#define DB_MAX_VARCHAR_PRECISION
Definition: dbtype_def.h:536
DB_OBJECT * db_object
Definition: parse_tree.h:2546
#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 PT_ERRORmf2(parser, node, setNo, msgNo, arg1, arg2)
Definition: parse_tree.h:65
#define DB_INT32_MIN
Definition: dbtype_def.h:632
#define assert(x)
int db_make_monetary(DB_VALUE *value, const DB_CURRENCY type, const double amount)
int db_string_elt(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
PT_MISC_TYPE qualifier
Definition: parse_tree.h:2204
#define PT_IS_INPUT_HOSTVAR(n)
Definition: parse_tree.h:345
static int pt_coerce_value_explicit(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type)
#define PT_IS_QUERY_NODE_TYPE(x)
Definition: parse_tree.h:115
#define LANG_COERCIBLE_CODESET
static PT_TYPE_ENUM pt_get_common_arg_type_of_width_bucket(PARSER_CONTEXT *parser, PT_NODE *node)
#define OR_CHECK_INT_DIV_OVERFLOW(a, b)
int prm_get_integer_value(PARAM_ID prm_id)
#define ER_TIME_CONVERSION
Definition: error_code.h:981
#define ER_GENERIC_ERROR
Definition: error_code.h:49
bool pt_check_cast_op(PARSER_CONTEXT *parser, PT_NODE *node)
DB_OBJECT * op
Definition: parse_tree.h:3039
int db_string_regexp_like(DB_VALUE *result, DB_VALUE *args[], int const num_args, cub_regex_object **comp_regex, char **comp_pattern)
TP_DOMAIN * tp_infer_common_domain(TP_DOMAIN *arg1, TP_DOMAIN *arg2)
char * db_get_database_name(void)
Definition: db_admin.c:432
static PT_NODE * pt_eval_method_call_type(PARSER_CONTEXT *parser, PT_NODE *node)
PT_TYPE_ENUM virt_type_enum
Definition: parse_tree.h:2042
int tp_domain_status_er_set(TP_DOMAIN_STATUS status, const char *file_name, const int line_no, const DB_VALUE *src, const TP_DOMAIN *domain)
static int pt_character_length_for_node(PT_NODE *node, const PT_TYPE_ENUM coerce_type)
DB_VALUE db_value
Definition: parse_tree.h:3059
struct expression_definition EXPRESSION_DEFINITION
EXPRESSION_SIGNATURE overloads[MAX_OVERLOADS]
TP_DOMAIN_COLL_ACTION
Definition: object_domain.h:62
int db_string_replace(const DB_VALUE *src_string, const DB_VALUE *srch_string, const DB_VALUE *repl_string, DB_VALUE *replaced_string)
char * db_get_user_and_host_name(void)
Definition: db_admin.c:1932
#define PT_SET_NODE_COLL_MODIFIER(p, coll)
Definition: parse_tree.h:626
#define OR_CHECK_UNS_ADD_OVERFLOW(a, b, c)
const char * pt_show_type_enum(PT_TYPE_ENUM t)
PT_NODE * pt_wrap_collection_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *arg, PT_TYPE_ENUM set_type, PT_NODE *set_data, bool for_collation)
PT_NODE * cte_pointer
Definition: parse_tree.h:2132
#define ER_IT_DATA_OVERFLOW
Definition: error_code.h:505
#define PT_IS_BIT_STRING_TYPE(t)
Definition: parse_tree.h:170
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:50
PT_NODE * on_cond
Definition: parse_tree.h:2149
#define PT_GET_COLLATION_MODIFIER(p)
Definition: parse_tree.h:619
#define PT_DOES_FUNCTION_HAVE_DIFFERENT_ARGS(op)
Definition: parse_tree.h:427
#define DB_ENUM_OVERFLOW_VAL
Definition: dbtype_def.h:645
#define PT_ARE_COMPARABLE(typ1, typ2)
PT_NODE * entity
Definition: parse_tree.h:2038
#define MSGCAT_SET_PARSER_RUNTIME
static void pt_hv_consistent_data_type_with_domain(PARSER_CONTEXT *parser, PT_NODE *node)
int db_format(const DB_VALUE *value, const DB_VALUE *decimals, const DB_VALUE *number_lang, DB_VALUE *result, const TP_DOMAIN *domain)
PT_DATA_VALUE data_value
Definition: parse_tree.h:3058
#define MORE_COERCIBLE(arg1_coll_infer, arg2_coll_infer)
PT_NODE * recursive_part
Definition: parse_tree.h:1978
static int pt_set_default_data_type(PARSER_CONTEXT *parser, PT_TYPE_ENUM type, PT_NODE **dtp)
#define MSGCAT_SEMANTIC_DATE_UNDERFLOW
PT_NODE * flat_entity_list
Definition: parse_tree.h:2140
PT_NODE * top_node
Definition: parse_tree.h:1707
PT_NODE * pt_check_type_compatibility_of_values_query(PARSER_CONTEXT *parser, PT_NODE *node)
int db_time_encode(DB_TIME *timeval, int hour, int minute, int second)
Definition: db_date.c:370
PT_MISC_TYPE all_distinct
Definition: parse_tree.h:2746
int db_string_compare(const DB_VALUE *string1, const DB_VALUE *string2, DB_VALUE *result)
PT_NODE * method_name
Definition: parse_tree.h:2363
#define MSGCAT_SEMANTIC_OVERFLOW_COERCING_TO
const char * original
Definition: parse_tree.h:2544
static PT_NODE * pt_select_list_to_enumeration_expr(PARSER_CONTEXT *parser, PT_NODE *data_type, PT_NODE *node)
#define ER_ATTEMPT_TO_USE_ZERODATE
Definition: error_code.h:1478
DB_DATETIME datetime
Definition: dbtype_def.h:783
#define PT_ERRORmf4(parser, node, setNo, msgNo, arg1, arg2, arg3, arg4)
Definition: parse_tree.h:67
#define PT_IS_PARAMETERIZED_TYPE(t)
Definition: parse_tree.h:229
int db_tan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3801
static int pt_apply_expressions_definition(PARSER_CONTEXT *parser, PT_NODE **expr)
PT_HINT_ENUM hint
Definition: parse_tree.h:2707
int intl_identifier_casecmp(const char *str1, const char *str2)
int db_string_pad(const MISC_OPERAND pad_operand, const DB_VALUE *src_string, const DB_VALUE *pad_length, const DB_VALUE *pad_charset, DB_VALUE *padded_string)
#define DB_VALUE_DOMAIN_TYPE(value)
Definition: dbtype.h:70
int db_to_datetime(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_datetime)
#define DB_INT32_MAX
Definition: dbtype_def.h:633
int db_ascii(const DB_VALUE *param, DB_VALUE *result)
DB_DOMAIN * db_method_return_domain(DB_METHOD *method)
Definition: db_info.c:1646
DB_VALUE local_transaction_id
Definition: parse_tree.h:3583
#define PT_IS_LEFT_RECURSIVE_EXPRESSION(node)
int db_string_substring_index(DB_VALUE *src_string, DB_VALUE *delim_string, const DB_VALUE *count, DB_VALUE *result)
struct compare_between_operator COMPARE_BETWEEN_OPERATOR
static PT_NODE * pt_eval_function_type_old(PARSER_CONTEXT *parser, PT_NODE *node)
bool print_collation
Definition: parse_tree.h:3065
int db_get_date_item(const DB_VALUE *src_date, const int item_type, DB_VALUE *result)
#define TP_DOMAIN_COLLATION(dom)
PT_NODE_TYPE node_type
Definition: parse_tree.h:3439
DB_BIGINT bigint
Definition: parse_tree.h:3033
#define TP_IS_NUMERIC_TYPE(typeid)
int db_string_upper(const DB_VALUE *string, DB_VALUE *upper_string)
#define PT_IS_NATIONAL_CHAR_STRING_TYPE(t)
Definition: parse_tree.h:156
const char * db_error_string(int level)
Definition: db_admin.c:2116
#define MSGCAT_SEMANTIC_NOT_SINGLE_COL
PT_NODE * parser_copy_tree(PARSER_CONTEXT *parser, const PT_NODE *tree)
static PT_NODE * pt_eval_function_type(PARSER_CONTEXT *parser, PT_NODE *node)
#define PT_IS_VALUE_QUERY(n)
Definition: parse_tree.h:476
int db_acos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3872
DB_VALUE sys_epochtime
Definition: parse_tree.h:3581
#define OR_CHECK_INT_OVERFLOW(i)
#define TP_SMALLINT_AS_CHAR_LENGTH
#define NUM_F_INSERT_SUBSTRING_ARGS
#define pt_cat_error(parser, node, setNo, msgNo,...)
Definition: parse_tree.h:50
int db_date_dbval(DB_VALUE *result, const DB_VALUE *date_value, const TP_DOMAIN *domain)
PT_NODE * having
Definition: parse_tree.h:2692
static void pt_chop_to_one_select_item(PARSER_CONTEXT *parser, PT_NODE *node)
DB_DOMAIN * pt_data_type_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *dt, const char *class_name)
Definition: parse_dbi.c:1805
struct expression_signature EXPRESSION_SIGNATURE
PT_NODE * from
Definition: parse_tree.h:2686
static PT_TYPE_ENUM pt_expr_get_return_type(PT_NODE *expr, const EXPRESSION_SIGNATURE sig)
const char * pt_show_binopcode(PT_OP_TYPE n)
int db_make_timestamptz(DB_VALUE *value, const DB_C_TIMESTAMPTZ *ts_tz_val)
static bool pt_get_expression_definition(const PT_OP_TYPE op, EXPRESSION_DEFINITION *def)
int db_evaluate_json_pretty(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5345
#define PT_IS_CONST(n)
Definition: parse_tree.h:364
unsigned is_alias_enabled_expr
Definition: parse_tree.h:3482
int db_string_put_cs_and_collation(DB_VALUE *value, const int codeset, const int collation_id)
Definition: db_macro.c:4164
PT_NODE * enumeration
Definition: parse_tree.h:2039
UINTPTR spec_id
Definition: parse_tree.h:2543
#define PT_IS_VALUE_NODE(n)
Definition: parse_tree.h:330
PT_NODE * value_clauses
Definition: parse_tree.h:2938
PT_NODE * limit
Definition: parse_tree.h:2072
TP_DOMAIN_STATUS tp_value_cast(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain, bool implicit_coercion)
#define PT_IS_DISCRETE_NUMBER_TYPE(t)
Definition: parse_tree.h:135
#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
PT_DATA_TYPE_INFO data_type
Definition: parse_tree.h:3284
#define TP_INTEGER_AS_CHAR_LENGTH
void pt_frob_error(PARSER_CONTEXT *parser, const PT_NODE *stmt, const char *fmt,...)
void db_update_row_count_cache(const int row_count)
Definition: db_admin.c:2924
PARSER_VARCHAR * str
Definition: parse_tree.h:3036
#define PT_SET_VALUE_QUERY(n)
Definition: parse_tree.h:479
#define DB_DATE_MAX
Definition: dbtype_def.h:650
int db_timestamp(const DB_VALUE *src_datetime1, const DB_VALUE *src_time2, DB_VALUE *result_datetime)
SP_PARSER_CTX * parser
#define TP_BIGINT_AS_CHAR_LENGTH
PT_NODE * arg1
Definition: parse_tree.h:2197
PT_NODE * pt_find_entity(PARSER_CONTEXT *parser, const PT_NODE *scope, UINTPTR id)
static PT_NODE * pt_eval_recursive_expr_type(PARSER_CONTEXT *parser, PT_NODE *gl_expr)
unsigned rewrite_limit
Definition: parse_tree.h:2766
PT_DO_INFO do_
Definition: parse_tree.h:3286
int db_str_to_date(const DB_VALUE *str, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, TP_DOMAIN *domain)
int db_string_translate(const DB_VALUE *src_string, const DB_VALUE *from_string, const DB_VALUE *to_string, DB_VALUE *transed_string)
#define NULL
Definition: freelistheap.h:34
PT_NODE * as_attr_list
Definition: parse_tree.h:2136
unsigned short db_get_enum_short(const DB_VALUE *value)
bool db_string_check_explicit_time(const char *str, int str_len)
Definition: db_date.c:1331
int db_blob_to_bit(const DB_VALUE *src_value, const DB_VALUE *length_value, DB_VALUE *result_value)
const char * er_msg(void)
static PT_TYPE_ENUM pt_get_equivalent_type_with_op(const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type, PT_OP_TYPE op)
static PT_TYPE_ENUM pt_wrap_type_for_collation(const PT_NODE *arg1, const PT_NODE *arg2, const PT_NODE *arg3, PT_TYPE_ENUM *wrap_type_collection)
PT_TYPE_ENUM pt_to_variable_size_type(PT_TYPE_ENUM type_enum)
struct pr_type * type
Definition: object_domain.h:76
int db_string_instr(const DB_VALUE *src_string, const DB_VALUE *sub_string, const DB_VALUE *start_pos, DB_VALUE *result)
PT_NODE * order_by
Definition: parse_tree.h:2875
int db_date_add_interval_days(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *db_days)
#define LANG_COERCIBLE_COLL
unsigned has_outer_spec
Definition: parse_tree.h:2757
PT_NODE * value_clauses
Definition: parse_tree.h:2334
void tz_get_system_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:758
PT_MISC_TYPE is_subquery
Definition: parse_tree.h:2747
if(extra_options)
Definition: dynamic_load.c:958
PT_INDEX_INFO index
Definition: parse_tree.h:3308
#define PT_IS_RIGHT_RECURSIVE_EXPRESSION(node)
int db_blob_from_file(const DB_VALUE *src_value, DB_VALUE *result_value)
static COLLATION_RESULT pt_get_collation_of_collection(PARSER_CONTEXT *parser, const PT_NODE *node, PT_COLL_INFER *coll_infer, const bool is_inner_collection, bool *is_first_element)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON
PT_NODE * pt_limit_to_numbering_expr(PARSER_CONTEXT *parser, PT_NODE *limit, PT_OP_TYPE num_op, bool is_gby_num)
PT_NODE * where
Definition: parse_tree.h:1934
int db_value_to_enumeration_value(const DB_VALUE *src, DB_VALUE *result, const TP_DOMAIN *enum_domain)
static PT_NODE * pt_coerce_node_collection_of_collection(PARSER_CONTEXT *parser, PT_NODE *node, const int coll_id, const INTL_CODESET codeset, bool force_mode, bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe, PT_TYPE_ENUM wrap_type_collection)
int db_get_time_item(const DB_VALUE *src_date, const int item_type, DB_VALUE *result)
#define TP_MONETARY_AS_CHAR_LENGTH
PT_NODE * pt_semantic_type(PARSER_CONTEXT *parser, PT_NODE *tree, SEMANTIC_CHK_INFO *sc_info_ptr)
#define MILLISECONDS_OF_ONE_DAY
char * pt_append_string(const PARSER_CONTEXT *parser, const char *old_string, const char *new_tail)
Definition: parse_tree.c:980
bool pt_are_equivalent_types(const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type)
Definition: func_type.cpp:1359
TP_DOMAIN * tp_domain_cache(TP_DOMAIN *transient)
PARSER_VARCHAR * pt_print_bytes(PARSER_CONTEXT *parser, const PT_NODE *node)
#define err(fd,...)
Definition: porting.h:431
#define PT_ERRORc(parser, node, msg)
Definition: parse_tree.h:55
static bool pt_is_symmetric_type(const PT_TYPE_ENUM type_enum)
#define TP_DATETIME_AS_CHAR_LENGTH
static COLLATION_RESULT pt_get_collation_info_for_collection_type(PARSER_CONTEXT *parser, const PT_NODE *node, PT_COLL_INFER *coll_infer)
int csession_get_last_insert_id(DB_VALUE *value, bool update_last_insert_id)
int db_value_put_encoded_date(DB_VALUE *value, const DB_DATE *date)
Definition: db_macro.c:1383
int db_add_months(const DB_VALUE *src_date, const DB_VALUE *nmonth, DB_VALUE *result_date)
#define TP_BIGINT_PRECISION
int db_from_tz(DB_VALUE *time_val, DB_VALUE *tz, DB_VALUE *time_val_with_tz)
#define PT_IS_COLLECTION_TYPE(t)
Definition: parse_tree.h:143
int pt_coerce_value(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type)
short db_value_is_in_workspace
Definition: parse_tree.h:3061
#define TP_DATETIMETZ_AS_CHAR_LENGTH
#define db_private_free(thrd, ptr)
Definition: memory_alloc.h:229
#define PT_SPEC_IS_CTE(spec_)
Definition: parse_tree.h:694
int db_to_number(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *number_lang, DB_VALUE *result_num)
#define OR_CHECK_SUB_UNDERFLOW(a, b, c)
int pt_check_same_datatype(const PARSER_CONTEXT *parser, const PT_NODE *p, const PT_NODE *q)
static PT_NODE * pt_coerce_range_expr_arguments(PARSER_CONTEXT *parser, PT_NODE *expr, PT_NODE *arg1, PT_NODE *arg2, PT_NODE *arg3, EXPRESSION_SIGNATURE sig)
PT_NODE * arg1
Definition: parse_tree.h:2085
PT_NODE * where
Definition: parse_tree.h:2687
PT_QUERY_INFO query
Definition: parse_tree.h:3325
int numeric_db_value_mul(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
short location
Definition: parse_tree.h:2243
int db_string_aes_encrypt(DB_VALUE const *src, DB_VALUE const *key, DB_VALUE *result)
int tp_domain_add(TP_DOMAIN **dlist, TP_DOMAIN *domain)
#define SET_EXPECTED_DOMAIN(node, dom)
Definition: type_checking.c:66
need_clear_type need_clear
Definition: dbtype_def.h:1084
int db_get_cs_coll_info(DB_VALUE *result, const DB_VALUE *val, const int mode)
int db_date_diff(const DB_VALUE *date_value1, const DB_VALUE *date_value2, DB_VALUE *result)
int db_set_size(DB_SET *set)
Definition: db_set.c:557
int db_trunc_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:3311
unsigned rewrite_limit
Definition: parse_tree.h:2078
#define PT_IS_CAST_MAYBE(node)
int pr_clear_value(DB_VALUE *value)
#define cmp
Definition: mprec.h:351
PT_NODE * spec
Definition: parse_tree.h:2331
const char * text
Definition: parse_tree.h:3056
int pt_evaluate_function(PARSER_CONTEXT *parser, PT_NODE *func, DB_VALUE *dbval_res)
#define TP_TIME_AS_CHAR_LENGTH
DB_BIGINT db_get_bigint(const DB_VALUE *value)
#define MSGCAT_SEMANTIC_AGG_FUN_WANT_1_ARG
int db_get_date_from_days(const DB_VALUE *src, DB_VALUE *result)
int db_string_rlike(const DB_VALUE *src, const DB_VALUE *pattern, const DB_VALUE *case_sensitive, cub_regex_object **comp_regex, char **comp_pattern, int *result)
int db_string_char_length(const DB_VALUE *string, DB_VALUE *char_count)
#define DB_DEFAULT_NUMERIC_PRECISION
Definition: dbtype_def.h:564
TP_DOMAIN ** host_var_expected_domains
Definition: parse_tree.h:3561
static void pt_fix_arguments_collation_flag(PT_NODE *expr)
int db_make_time(DB_VALUE *value, const int hour, const int minute, const int second)
#define DB_DEFAULT_SCALE
Definition: dbtype_def.h:561
PT_NODE * set
Definition: parse_tree.h:3047
bool pt_is_same_enum_data_type(PT_NODE *dt1, PT_NODE *dt2)
Definition: parse_dbi.c:147
int db_make_datetimetz(DB_VALUE *value, const DB_DATETIMETZ *datetimetz)
struct db_domain_info::general_info general_info
int64_t DB_BIGINT
Definition: dbtype_def.h:751
#define d1
#define ER_DATE_CONVERSION
Definition: error_code.h:242
#define PT_IS_FALSE_WHERE_VALUE(node)
Definition: parse_tree.h:701
int db_evaluate_json_array_insert(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:6032
int db_clob_to_char(const DB_VALUE *src_value, const DB_VALUE *codeset_value, DB_VALUE *result_value)
DB_METHOD * db_get_method(DB_OBJECT *obj, const char *name)
Definition: db_info.c:1454
PT_NODE * parser_append_node(PT_NODE *node, PT_NODE *list)
char * pt_short_print(PARSER_CONTEXT *parser, const PT_NODE *node)
int db_last_day(const DB_VALUE *src_date, DB_VALUE *result_day)
TP_DOMAIN * pt_xasl_type_enum_to_domain(const PT_TYPE_ENUM type)
int db_inet_aton(DB_VALUE *result_numbered_ip, const DB_VALUE *string)
DB_CONST_C_CHAR db_get_enum_string(const DB_VALUE *value)
#define TP_IS_CHAR_TYPE(typeid)
#define OR_CHECK_UNS_SUB_UNDERFLOW(a, b, c)
TP_DOMAIN_COLL_ACTION collation_flag
Definition: object_domain.h:94
static int pt_check_recursive_expr_collation(PARSER_CONTEXT *parser, PT_NODE **node)
PT_NODE * orderby_for
Definition: parse_tree.h:2876
static PT_NODE * pt_to_false_subquery(PARSER_CONTEXT *parser, PT_NODE *node)
const char * generic_name
Definition: parse_tree.h:2261
PT_NODE * parser_new_node(PARSER_CONTEXT *parser, PT_NODE_TYPE node_type)
static void error(const char *msg)
Definition: gencat.c:331
#define MSGCAT_SEMANTIC_COERCE_UNSUPPORTED
int db_make_float(DB_VALUE *value, const DB_C_FLOAT num)
void parser_free_node(const PARSER_CONTEXT *parser, PT_NODE *node)
Definition: parse_tree.c:869
int db_date_add_interval_expr(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *expr, const int unit)
int db_string_to_base64(DB_VALUE const *src, DB_VALUE *result)
#define PT_IS_HOSTVAR(n)
Definition: parse_tree.h:342
#define DB_DEFAULT_PRECISION
Definition: dbtype_def.h:558
int db_evaluate_json_insert(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5653
int sm_att_info(MOP classop, const char *name, int *idp, TP_DOMAIN **domainp, int *sharedp, int class_attr)
#define TP_TYPE_HAS_COLLATION(typeid)
int db_cos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3737
#define OR_CHECK_FLOAT_OVERFLOW(i)
short db_value_is_initialized
Definition: parse_tree.h:3060
PT_SORT_SPEC_INFO sort_spec
Definition: parse_tree.h:3343
static bool pt_is_op_w_collation(const PT_OP_TYPE op)
#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
static int pt_difference_sets(PARSER_CONTEXT *parser, TP_DOMAIN *domain, DB_VALUE *set1, DB_VALUE *set2, DB_VALUE *result, PT_NODE *o2)
const char * lang_get_codeset_name(int codeset_id)
#define DB_BIGINT_MAX
Definition: dbtype_def.h:640
int pr_clone_value(const DB_VALUE *src, DB_VALUE *dest)
void parser_free_tree(PARSER_CONTEXT *parser, PT_NODE *tree)
enum collation_result COLLATION_RESULT
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_INDEX
static PT_TYPE_ENUM pt_get_common_collection_type(const PT_NODE *set, bool *is_multitype)
#define TP_INTEGER_PRECISION
#define PT_IS_RECURSIVE_EXPRESSION(node)
#define DB_INT16_MAX
Definition: dbtype_def.h:630
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
#define ER_AU_SELECT_FAILURE
Definition: error_code.h:221
int db_sqrt_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:731
int db_convert_time_to_sec(const DB_VALUE *src_date, DB_VALUE *result)
const char * fcode_get_lowercase_name(FUNC_TYPE ftype)
PT_COLL_COERC_LEV coerc_level
Definition: parse_tree.h:3674
unsigned int DB_DATE
Definition: dbtype_def.h:771
#define MSGCAT_SET_PARSER_SEMANTIC
void pt_try_remove_order_by(PARSER_CONTEXT *parser, PT_NODE *query)
#define DB_BIGINT_MIN
Definition: dbtype_def.h:641
DB_DATA_STATUS
int db_get_date_dayofyear(const DB_VALUE *src_date, DB_VALUE *result)
PT_NODE * pt_check_union_compatibility(PARSER_CONTEXT *parser, PT_NODE *node)
int db_hex(const DB_VALUE *param, DB_VALUE *result)
DB_VALUE sys_datetime
Definition: parse_tree.h:3580
#define free_and_init(ptr)
Definition: memory_alloc.h:147
char * db_get_database_version(void)
Definition: db_admin.c:455
struct pt_merge_info::@126 insert
#define PT_IS_COMPLEX_TYPE(t)
Definition: parse_tree.h:174
#define strlen(s1)
Definition: intl_support.c:43
int pt_common_collation(PT_COLL_INFER *arg1_coll_infer, PT_COLL_INFER *arg2_coll_infer, PT_COLL_INFER *arg3_coll_infer, const int args_w_coll, bool op_has_3_args, int *common_coll, INTL_CODESET *common_cs)
int db_string_trim(const MISC_OPERAND tr_operand, const DB_VALUE *trim_charset, const DB_VALUE *src_string, DB_VALUE *trimmed_string)
bool print_charset
Definition: parse_tree.h:3064
int db_make_timestampltz(DB_VALUE *value, const DB_C_TIMESTAMP ts_val)
PT_NODE * pt_make_prim_data_type(PARSER_CONTEXT *parser, PT_TYPE_ENUM e)
char * prm_get_string_value(PARAM_ID prm_id)
#define PT_HAS_DATE_PART(t)
Definition: parse_tree.h:205
bool pt_get_collation_info(const PT_NODE *node, PT_COLL_INFER *coll_infer)
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_convert_to_time(const DB_VALUE *src_hour, const DB_VALUE *src_minute, const DB_VALUE *src_second, DB_VALUE *result)
PT_NODE * default_value
Definition: parse_tree.h:2553
DB_METHOD * db_get_class_method(DB_OBJECT *obj, const char *name)
Definition: db_info.c:1484
#define ER_REGEX_EXEC_ERROR
Definition: error_code.h:1363
PT_NODE * non_recursive_part
Definition: parse_tree.h:1977
PT_NODE * attr_list
Definition: parse_tree.h:2937
static bool pt_is_const_foldable_width_bucket(PARSER_CONTEXT *parser, PT_NODE *expr)
const char * tz_get_system_timezone(void)
Definition: tz_support.c:749
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
PT_OP_TYPE
Definition: parse_tree.h:1320
PT_NODE * connect_by
Definition: parse_tree.h:2689
int db_make_string_copy(DB_VALUE *value, DB_CONST_C_CHAR str)
int db_to_time(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_time)
unsigned int date
Definition: dbtype_def.h:776
int db_get_row_count_cache(void)
Definition: db_admin.c:2914
#define PT_IS_FUNCTION(n)
Definition: parse_tree.h:311
PT_MISC_TYPE option
Definition: parse_tree.h:2790
enum intl_codeset INTL_CODESET
Definition: intl_support.h:190
int pt_is_comp_op(PT_OP_TYPE op)
int db_radians_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4086
int lang_set_flag_from_lang(const char *lang_str, bool has_user_format, bool has_user_lang, int *flag)
PT_NODE * search_cond
Definition: parse_tree.h:2063
bool prm_get_bool_value(PARAM_ID prm_id)
int db_months_between(const DB_VALUE *start_mon, const DB_VALUE *end_mon, DB_VALUE *result_mon)
static bool pt_is_range_expression(const PT_OP_TYPE op)
#define QSTR_IS_ANY_CHAR_OR_BIT(s)
Definition: string_opfunc.h:47
PT_NODE * start_with
Definition: parse_tree.h:2690
DB_TIMESTAMP * db_get_timestamp(const DB_VALUE *value)
int db_get_string_size(const DB_VALUE *value)
int db_get_variable(DB_VALUE *name, DB_VALUE *value)
Definition: db_admin.c:1072
DB_C_SHORT db_get_short(const DB_VALUE *value)
int pt_is_single_tuple(PARSER_CONTEXT *parser, PT_NODE *select_node)
PT_NODE * after_cb_filter
Definition: parse_tree.h:2691
static int pt_upd_domain_info(PARSER_CONTEXT *parser, PT_NODE *arg1, PT_NODE *arg2, PT_OP_TYPE op, PT_TYPE_ENUM common_type, PT_NODE *node)
static PT_NODE * pt_compare_bounds_to_value(PARSER_CONTEXT *parser, PT_NODE *expr, PT_OP_TYPE op, PT_TYPE_ENUM lhs_type, DB_VALUE *rhs_val, PT_TYPE_ENUM rhs_type)
int db_evaluate_json_merge_patch(DB_VALUE *result, DB_VALUE *const *arg, const int num_args)
Definition: arithmetic.c:6215
PT_MISC_TYPE list_type
Definition: parse_tree.h:2324
void er_clear(void)
int db_to_date(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, DB_VALUE *result_date)
#define TP_IS_DATE_TYPE(typeid)
int set_intersection(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3951
const char * tz_get_session_local_timezone(void)
Definition: tz_support.c:739
int db_string_regexp_instr(DB_VALUE *result, DB_VALUE *args[], int const num_args, cub_regex_object **comp_regex, char **comp_pattern)
PT_DELETE_INFO delete_
Definition: parse_tree.h:3285
int db_string_regexp_count(DB_VALUE *result, DB_VALUE *args[], int const num_args, cub_regex_object **comp_regex, char **comp_pattern)
PT_NODE * limit
Definition: parse_tree.h:2874
#define TP_FLOATING_PRECISION_VALUE
int db_time_diff(const DB_VALUE *val1, const DB_VALUE *val2, DB_VALUE *result)
PT_SET_OPT_LVL_INFO set_opt_lvl
Definition: parse_tree.h:3337
union pt_arg_type::pt_arg_type_val val
int db_crc32_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:5020
unsigned do_not_fold
Definition: parse_tree.h:3475
PT_NODE * error_msgs
Definition: parse_tree.h:3550
int db_least_or_greatest(DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *result, bool least)
Definition: arithmetic.c:6454
char * parser_print_tree(PARSER_CONTEXT *parser, const PT_NODE *node)
#define DB_VALUE_TYPE(value)
Definition: dbtype.h:72
#define DB_CURRENCY_DEFAULT
Definition: dbtype.h:46
int db_string_like(const DB_VALUE *src_string, const DB_VALUE *pattern, const DB_VALUE *esc_char, int *result)
bool pt_is_expr_wrapped_function(PARSER_CONTEXT *parser, const PT_NODE *node)
int i
Definition: dynamic_load.c:954
PT_VALUE_INFO value
Definition: parse_tree.h:3358
int pt_length_of_select_list(PT_NODE *list, int hidden_col)
int db_to_char(const DB_VALUE *src_value, const DB_VALUE *format_or_length, const DB_VALUE *lang_str, DB_VALUE *result_str, const TP_DOMAIN *domain)
int db_make_null(DB_VALUE *value)
PT_NODE * pt_fold_union(PARSER_CONTEXT *parser, PT_NODE *union_node, STATEMENT_SET_FOLD fold_as)
DB_TYPE id
PT_NODE * list
Definition: parse_tree.h:2685
#define DB_IS_NULL(value)
Definition: dbtype.h:63
PT_OP_TYPE op
Definition: parse_tree.h:2200
struct tp_domain * next
Definition: object_domain.h:74
int db_date_sub_interval_days(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *db_days)
int db_string_sha_two(DB_VALUE const *src, DB_VALUE const *hash_len, DB_VALUE *result)
#define pt_is_set_type(n)
Definition: parse_tree.h:267
bool pt_false_search_condition(PARSER_CONTEXT *parser, const PT_NODE *node)
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_date_sub_interval_expr(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *expr, const int unit)
int db_string_bit_length(const DB_VALUE *string, DB_VALUE *bit_count)
static PT_NODE * pt_check_function_collation(PARSER_CONTEXT *parser, PT_NODE *node)
int db_get_schema_def_dbval(DB_VALUE *result, DB_VALUE *name_val)
Definition: db_info.c:2407
int db_clob_from_file(const DB_VALUE *src_value, DB_VALUE *result_value)
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
DB_DATETIME * db_get_datetime(const DB_VALUE *value)
PT_NODE * orderby_for
Definition: parse_tree.h:2770
int db_char_to_clob(const DB_VALUE *src_value, DB_VALUE *result_value)
#define DB_MAX_VARNCHAR_PRECISION
Definition: dbtype_def.h:546
int pt_evaluate_db_value_expr(PARSER_CONTEXT *parser, PT_NODE *expr, PT_OP_TYPE op, DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *arg3, DB_VALUE *result, TP_DOMAIN *domain, PT_NODE *o1, PT_NODE *o2, PT_NODE *o3, PT_MISC_TYPE qualifier)
#define MSGCAT_RUNTIME_IS_NOT_AUTHORIZED_ON
void parser_clear_node(PARSER_CONTEXT *parser, PT_NODE *node)
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON
int db_string_regexp_substr(DB_VALUE *result, DB_VALUE *args[], int const num_args, cub_regex_object **comp_regex, char **comp_pattern)
PT_NODE * pt_append_query_select_list(PARSER_CONTEXT *parser, PT_NODE *query, PT_NODE *attrs)
unsigned is_value_query
Definition: parse_tree.h:3478
#define PT_INTERNAL_ERROR(parser, what)
Definition: parse_tree.h:112
#define TP_SMALLINT_PRECISION
int db_string_repeat(const DB_VALUE *src_string, const DB_VALUE *count, DB_VALUE *result)
int db_make_timestamp(DB_VALUE *value, const DB_C_TIMESTAMP timeval)
int db_make_int(DB_VALUE *value, const int num)
#define PT_IS_NUMERIC_TYPE(t)
Definition: parse_tree.h:125
int db_get_string_length(const DB_VALUE *value)
int db_conv(const DB_VALUE *num, const DB_VALUE *from_base, const DB_VALUE *to_base, DB_VALUE *result)
#define MSGCAT_SEMANTIC_ZERO_DIVIDE
static PT_TYPE_ENUM pt_common_type_op(PT_TYPE_ENUM t1, PT_OP_TYPE op, PT_TYPE_ENUM t2)
int db_tz_offset(const DB_VALUE *src_str, DB_VALUE *result_str, DB_DATETIME *datetime)
#define pt_has_error(parser)
Definition: parser.h:507
int db_make_oid(DB_VALUE *value, const OID *oid)
const char * alias_print
Definition: parse_tree.h:3455
enum pt_node_type PT_NODE_TYPE
Definition: parse_tree.h:904
PT_NODE * pt_domain_to_data_type(PARSER_CONTEXT *parser, DB_DOMAIN *domain)
int db_atan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3970
int db_make_char(DB_VALUE *value, const int char_length, DB_CONST_C_CHAR str, const int char_str_byte_size, const int codeset, const int collation_id)
int db_sys_timezone(DB_VALUE *result_timezone)
const char * resolved
Definition: parse_tree.h:2545
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
int numeric_db_value_add(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
DB_TIMESTAMP DB_UTIME
Definition: dbtype_def.h:761
#define LANG_SYS_CODESET
#define TP_DOMAIN_CODESET(dom)
#define PT_HAS_COLLATION(t)
Definition: parse_tree.h:243
int collation_id
Definition: object_domain.h:92
int db_typeof_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4302
static PT_NODE * pt_fix_enumeration_comparison(PARSER_CONTEXT *parser, PT_NODE *expr)
int db_clob_length(const DB_VALUE *src_value, DB_VALUE *result_value)
DB_TIME * db_get_time(const DB_VALUE *value)
int db_string_md5(DB_VALUE const *val, DB_VALUE *result)
int db_get_row_count(int *row_count)
Definition: db_admin.c:1046
#define INTL_CAN_COERCE_CS(cs_from, cs_to)
Definition: intl_support.h:97
const char * au_user_name(void)
static void pt_update_host_var_data_type(PARSER_CONTEXT *parser, PT_NODE *hv_node)
PT_NODE * pt_check_union_type_compatibility_of_values_query(PARSER_CONTEXT *parser, PT_NODE *node)
int db_evaluate_json_set(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5788
PT_NODE * pt_dbval_to_value(PARSER_CONTEXT *parser, const DB_VALUE *val)
Definition: parse_dbi.c:574
#define MSGCAT_SEMANTIC_INCOMPATIBLE_OPDS
int db_abs_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:565
DB_VALUE * default_value
Definition: esql_cli.c:348
void db_time_decode(DB_TIME *timeval, int *hourp, int *minutep, int *secondp)
Definition: db_date.c:432
int db_conv_tz(DB_VALUE *time_val, DB_VALUE *result_time)
DB_VALUE_COMPARE_RESULT
Definition: dbtype_def.h:199
int column_number
Definition: parse_tree.h:3442
int pt_length_of_list(const PT_NODE *list)
struct parser_node::@132 flag
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_3
PT_NODE * entity_name
Definition: parse_tree.h:2130
int pt_coerce_value_for_default_value(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type, DB_DEFAULT_EXPR_TYPE default_expr_type)
#define DB_INT16_MIN
Definition: dbtype_def.h:629
PT_SELECT_INFO select
Definition: parse_tree.h:2781
DB_VALUE * pt_value_to_db(PARSER_CONTEXT *parser, PT_NODE *value)
Definition: parse_dbi.c:1088
int db_evaluate_json_search(DB_VALUE *result, DB_VALUE *const *args, const int num_args)
Definition: arithmetic.c:6268
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON_3
#define pt_is_const(n)
Definition: parse_tree.h:271
#define TP_DATE_AS_CHAR_LENGTH
static PT_NODE * pt_fold_const_function(PARSER_CONTEXT *parser, PT_NODE *func)
int db_string_extract_dbval(const MISC_OPERAND extr_operand, DB_VALUE *dbval_p, DB_VALUE *result_p, TP_DOMAIN *domain_p)
int db_evaluate_json_array_append(DB_VALUE *result, DB_VALUE *const *arg, int const num_args)
Definition: arithmetic.c:5964
double amount
Definition: dbtype_def.h:831
unsigned is_sort_spec
Definition: parse_tree.h:2758
int setobj_size(COL *col)
Definition: set_object.c:4948
PT_INSERT_INFO insert
Definition: parse_tree.h:3309
PT_HOST_VAR_INFO host_var
Definition: parse_tree.h:3307
int db_ceil_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:254
#define OR_CHECK_MULT_OVERFLOW(a, b, c)
PT_NODE * parser_walk_tree(PARSER_CONTEXT *parser, PT_NODE *node, PT_NODE_WALK_FUNCTION pre_function, void *pre_argument, PT_NODE_WALK_FUNCTION post_function, void *post_argument)
TP_DOMAIN_COLL_ACTION collation_flag
Definition: parse_tree.h:2048
#define PT_EXPR_INFO_IS_FLAGED(e, f)
Definition: parse_tree.h:2238
#define PT_ERRORf(parser, node, msg, arg1)
Definition: parse_tree.h:57
#define PT_IS_NAME_NODE(n)
Definition: parse_tree.h:320
int db_get_string_codeset(const DB_VALUE *value)
#define ER_OBJ_INVALID_METHOD
Definition: error_code.h:279
#define MSGCAT_SEMANTIC_FUNCTION_NO_ARGS
int db_degrees_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4054
PT_NODE * parser_copy_tree_list(PARSER_CONTEXT *parser, PT_NODE *tree)
#define ER_OBJ_NOT_A_CLASS
Definition: error_code.h:288
bool pt_is_symmetric_op(const PT_OP_TYPE op)
const char ** p
Definition: dynamic_load.c:945
static bool pt_is_explicit_coerce_allowed_for_default_value(PARSER_CONTEXT *parser, PT_TYPE_ENUM data_type, PT_TYPE_ENUM desired_type)
DB_CONST_C_CHAR db_get_string(const DB_VALUE *value)
MISC_OPERAND
#define PT_ERRORmf(parser, node, setNo, msgNo, arg1)
Definition: parse_tree.h:64
unsigned is_hidden_column
Definition: parse_tree.h:3470
int db_bit_to_blob(const DB_VALUE *src_value, DB_VALUE *result_value)
unsigned int time
Definition: dbtype_def.h:777
DB_DEFAULT_EXPR_TYPE
Definition: dbtype_def.h:1181
PT_NODE * pt_coerce_node_collation(PARSER_CONTEXT *parser, PT_NODE *node, const int coll_id, const INTL_CODESET codeset, bool force_mode, bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe, PT_TYPE_ENUM wrap_type_collection)
int db_get_date_week(const DB_VALUE *src_date, const DB_VALUE *mode, DB_VALUE *result)
int db_make_nchar(DB_VALUE *value, const int nchar_length, DB_CONST_C_NCHAR str, const int nchar_str_byte_size, const int codeset, const int collation_id)
PT_JOIN_TYPE join_type
Definition: parse_tree.h:2151
PT_COMP_TO_BETWEEN_OP_CODE_TYPE
Definition: parse_tree.h:1496
PT_NODE * on_call_target
Definition: parse_tree.h:2365
#define PT_NAME_GENERATED_DERIVED_SPEC
Definition: parse_tree.h:2569
static int pt_union_sets(PARSER_CONTEXT *parser, TP_DOMAIN *domain, DB_VALUE *set1, DB_VALUE *set2, DB_VALUE *result, PT_NODE *o2)
#define PT_EXPR_INFO_CAST_COLL_MODIFIER
Definition: parse_tree.h:2231
int db_like_bound(const DB_VALUE *const src_pattern, const DB_VALUE *const src_escape, DB_VALUE *const result_bound, const bool compute_lower_bound)
int db_value_domain_init(DB_VALUE *value, const DB_TYPE type, const int precision, const int scale)
Definition: db_macro.c:153
#define TP_DOMAIN_COLLATION_FLAG(dom)
PT_NODE * derived_table
Definition: parse_tree.h:2134
int pt_evaluate_function_w_args(PARSER_CONTEXT *parser, FUNC_TYPE fcode, DB_VALUE *args[], const int num_args, DB_VALUE *result)
PT_NODE * search_cond
Definition: parse_tree.h:2862
int tz_datetimetz_fix_zone(const DB_DATETIMETZ *src_dt_tz, DB_DATETIMETZ *dest_dt_tz)
Definition: tz_support.c:1815
PT_NODE * range_var
Definition: parse_tree.h:2135
#define DB_MAX_CHAR_PRECISION
Definition: dbtype_def.h:533
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