/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.73k | rhs_(rhs) { |
37 | 1.73k | } |
38 | | |
39 | | PTProperty::PTProperty(MemoryContext *memctx, |
40 | | YBLocationPtr loc) |
41 | 932 | : TreeNode(memctx, loc) { |
42 | 932 | } |
43 | | |
44 | 2.36k | PTProperty::~PTProperty() { |
45 | 2.36k | } |
46 | | |
47 | | Status PTProperty::GetIntValueFromExpr(PTExpr::SharedPtr expr, |
48 | | const string& property_name, |
49 | 594 | int64_t *val) { |
50 | 594 | DCHECK_ONLY_NOTNULL(val); |
51 | | |
52 | 594 | if (expr == nullptr) { |
53 | 0 | return STATUS(InvalidArgument, Substitute("Invalid integer value for '$0'", property_name)); |
54 | 0 | } |
55 | | |
56 | 594 | if (expr->ql_type_id() == DataType::VARINT || expr->ql_type_id() == DataType::STRING) { |
57 | 552 | MCSharedPtr<MCString> str_val; |
58 | 552 | if (expr->ql_type_id() == DataType::STRING) { |
59 | 46 | str_val = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
60 | 506 | } else { |
61 | 506 | DCHECK(expr->ql_type_id() == DataType::VARINT); |
62 | 506 | str_val = std::dynamic_pointer_cast<PTConstVarInt>(expr)->Eval(); |
63 | 506 | } |
64 | 552 | return ResultToStatus(&CheckedStoll)(val, *str_val); |
65 | 42 | } else if (QLType::IsInteger(expr->ql_type_id())) { |
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 | 42 | } |
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 | 84 | } else if (expr->ql_type_id() == DataType::STRING) { |
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 | 32 | } |
95 | | |
96 | | Status PTProperty::GetBoolValueFromExpr(PTExpr::SharedPtr expr, |
97 | | const string& property_name, |
98 | 977 | bool *val) { |
99 | 977 | DCHECK_ONLY_NOTNULL(val); |
100 | | |
101 | 977 | if (expr == nullptr) { |
102 | 4 | return STATUS(InvalidArgument, Substitute("'$0' should either be true or false", |
103 | 4 | property_name)); |
104 | 4 | } |
105 | 973 | if (expr->ql_type_id() == DataType::BOOL) { |
106 | 922 | *val = std::dynamic_pointer_cast<PTConstBool>(expr)->Eval(); |
107 | 922 | return Status::OK(); |
108 | 51 | } else if (expr->ql_type_id() == DataType::STRING) { |
109 | 42 | auto mcstr = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
110 | 42 | string str_val; |
111 | 42 | ToLowerCase(mcstr->c_str(), &str_val); |
112 | 42 | if (str_val == "true") { |
113 | 18 | *val = true; |
114 | 18 | 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 | 6 | property_name, str_val)); |
121 | 6 | } |
122 | 9 | return STATUS(InvalidArgument, Substitute("'$0' should either be true or false", property_name)); |
123 | 9 | } |
124 | | |
125 | | Status PTProperty::GetStringValueFromExpr(PTExpr::SharedPtr expr, |
126 | | bool to_lower_case, |
127 | | const string& property_name, |
128 | 753 | string *val) { |
129 | 753 | DCHECK_ONLY_NOTNULL(val); |
130 | | |
131 | 753 | if (expr && expr->ql_type_id() == DataType::STRING) { |
132 | 722 | auto mcstr = std::dynamic_pointer_cast<PTConstText>(expr)->Eval(); |
133 | 722 | if (to_lower_case) { |
134 | 596 | ToLowerCase(mcstr->c_str(), val); |
135 | 126 | } else { |
136 | 126 | *val = mcstr->c_str(); |
137 | 126 | } |
138 | 722 | return Status::OK(); |
139 | 722 | } |
140 | 31 | return STATUS(InvalidArgument, Substitute("Invalid string value for '$0'", property_name)); |
141 | 31 | } |
142 | | |
143 | | } // namespace ql |
144 | | } // namespace yb |