/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/pt_property.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/yql/cql/ql/ptree/pt_property.h" |
15 | | |
16 | | #include "yb/common/ql_type.h" |
17 | | |
18 | | #include "yb/util/logging.h" |
19 | | #include "yb/util/status_format.h" |
20 | | #include "yb/util/stol_utils.h" |
21 | | #include "yb/util/string_case.h" |
22 | | |
23 | | #include "yb/yql/cql/ql/ptree/pt_expr.h" |
24 | | |
25 | | namespace yb { |
26 | | namespace ql { |
27 | | |
28 | | using strings::Substitute; |
29 | | |
30 | | PTProperty::PTProperty(MemoryContext *memctx, |
31 | | YBLocationPtr loc, |
32 | | const MCSharedPtr<MCString>& lhs, |
33 | | const PTExpr::SharedPtr& rhs) |
34 | | : TreeNode(memctx, loc), |
35 | | lhs_(lhs), |
36 | 1.95k | rhs_(rhs) { |
37 | 1.95k | } |
38 | | |
39 | | PTProperty::PTProperty(MemoryContext *memctx, |
40 | | YBLocationPtr loc) |
41 | 1.08k | : TreeNode(memctx, loc) { |
42 | 1.08k | } |
43 | | |
44 | 2.42k | PTProperty::~PTProperty() { |
45 | 2.42k | } |
46 | | |
47 | | Status PTProperty::GetIntValueFromExpr(PTExpr::SharedPtr expr, |
48 | | const string& property_name, |
49 | 774 | int64_t *val) { |
50 | 774 | DCHECK_ONLY_NOTNULL(val); |
51 | | |
52 | 774 | if (expr == nullptr) { |
53 | 0 | return STATUS(InvalidArgument, Substitute("Invalid integer value for '$0'", property_name)); |
54 | 0 | } |
55 | | |
56 | 774 | if (expr->ql_type_id() == DataType::VARINT || expr->ql_type_id() == DataType::STRING88 ) { |
57 | 732 | MCSharedPtr<MCString> str_val; |
58 | 732 | if (expr->ql_type_id() == DataType::STRING) { |
59 | 46 | str_val = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
60 | 686 | } else { |
61 | 686 | DCHECK(expr->ql_type_id() == DataType::VARINT); |
62 | 686 | str_val = std::dynamic_pointer_cast<PTConstVarInt>(expr)->Eval(); |
63 | 686 | } |
64 | 732 | return ResultToStatus(&CheckedStoll)(val, *str_val); |
65 | 732 | } else if (42 QLType::IsInteger(expr->ql_type_id())42 ) { |
66 | 0 | *val = std::dynamic_pointer_cast<PTConstInt>(expr)->Eval(); |
67 | 0 | return Status::OK(); |
68 | 0 | } |
69 | 42 | return STATUS(InvalidArgument, Substitute("Invalid integer value for '$0'", property_name)); |
70 | 774 | } |
71 | | |
72 | | Status PTProperty::GetDoubleValueFromExpr(PTExpr::SharedPtr expr, |
73 | | const string& property_name, |
74 | 215 | long double *val) { |
75 | 215 | DCHECK_ONLY_NOTNULL(val); |
76 | | |
77 | 215 | if (expr == nullptr) { |
78 | 0 | return STATUS_FORMAT(InvalidArgument, "Invalid float value for '$0'", property_name); |
79 | 0 | } |
80 | 215 | if (QLType::IsNumeric(expr->ql_type_id())) { |
81 | 131 | if (QLType::IsInteger(expr->ql_type_id())) { |
82 | 19 | DCHECK(expr->ql_type_id() == DataType::VARINT); |
83 | 19 | RETURN_NOT_OK(std::static_pointer_cast<PTConstVarInt>(expr)->ToDouble(val, false)); |
84 | 112 | } else { |
85 | 112 | DCHECK(expr->ql_type_id() == DataType::DECIMAL); |
86 | 112 | RETURN_NOT_OK(std::static_pointer_cast<PTConstDecimal>(expr)->ToDouble(val, false)); |
87 | 112 | } |
88 | 127 | return Status::OK(); |
89 | 131 | } else if (84 expr->ql_type_id() == DataType::STRING84 ) { |
90 | 52 | auto str_val = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
91 | 52 | return ResultToStatus(&CheckedStold)(val, *str_val); |
92 | 52 | } |
93 | 32 | return STATUS_FORMAT(InvalidArgument, "Invalid float value for '$0'", property_name); |
94 | 215 | } |
95 | | |
96 | | Status PTProperty::GetBoolValueFromExpr(PTExpr::SharedPtr expr, |
97 | | const string& property_name, |
98 | 1.00k | bool *val) { |
99 | 1.00k | DCHECK_ONLY_NOTNULL(val); |
100 | | |
101 | 1.00k | if (expr == nullptr) { |
102 | 4 | return STATUS(InvalidArgument, Substitute("'$0' should either be true or false", |
103 | 4 | property_name)); |
104 | 4 | } |
105 | 1.00k | if (expr->ql_type_id() == DataType::BOOL) { |
106 | 951 | *val = std::dynamic_pointer_cast<PTConstBool>(expr)->Eval(); |
107 | 951 | return Status::OK(); |
108 | 951 | } else if (52 expr->ql_type_id() == DataType::STRING52 ) { |
109 | 44 | auto mcstr = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
110 | 44 | string str_val; |
111 | 44 | ToLowerCase(mcstr->c_str(), &str_val); |
112 | 44 | if (str_val == "true") { |
113 | 20 | *val = true; |
114 | 20 | return Status::OK(); |
115 | 24 | } else if (str_val == "false") { |
116 | 18 | *val = false; |
117 | 18 | return Status::OK(); |
118 | 18 | } |
119 | 6 | return STATUS(InvalidArgument, Substitute("'$0' should either be true or false, not $1", |
120 | 44 | property_name, str_val)); |
121 | 44 | } |
122 | 8 | return STATUS(InvalidArgument, Substitute("'$0' should either be true or false", property_name)); |
123 | 1.00k | } |
124 | | |
125 | | Status PTProperty::GetStringValueFromExpr(PTExpr::SharedPtr expr, |
126 | | bool to_lower_case, |
127 | | const string& property_name, |
128 | 815 | string *val) { |
129 | 815 | DCHECK_ONLY_NOTNULL(val); |
130 | | |
131 | 815 | if (expr && expr->ql_type_id() == DataType::STRING) { |
132 | 784 | auto mcstr = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
133 | 784 | if (to_lower_case) { |
134 | 602 | ToLowerCase(mcstr->c_str(), val); |
135 | 602 | } else { |
136 | 182 | *val = mcstr->c_str(); |
137 | 182 | } |
138 | 784 | return Status::OK(); |
139 | 784 | } |
140 | 31 | return STATUS(InvalidArgument, Substitute("Invalid string value for '$0'", property_name)); |
141 | 815 | } |
142 | | |
143 | | } // namespace ql |
144 | | } // namespace yb |