YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/exec/eval_expr.cc
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include <string>
17
18
#include "yb/common/ql_value.h"
19
20
#include "yb/util/net/inetaddress.h"
21
22
#include "yb/yql/cql/ql/exec/exec_context.h"
23
#include "yb/yql/cql/ql/exec/executor.h"
24
#include "yb/yql/cql/ql/ptree/column_desc.h"
25
#include "yb/yql/cql/ql/ptree/pt_bcall.h"
26
#include "yb/yql/cql/ql/ptree/pt_expr.h"
27
#include "yb/yql/cql/ql/util/statement_params.h"
28
29
namespace yb {
30
namespace ql {
31
32
//--------------------------------------------------------------------------------------------------
33
34
21.0M
Status Executor::PTExprToPB(const PTExpr::SharedPtr& expr, QLExpressionPB *expr_pb) {
35
21.0M
  if (expr == nullptr)
36
47
    return Status::OK();
37
38
  // When selecting from INDEX table, expression's value might be stored in a column.
39
21.0M
  const ColumnDesc *index_desc = expr->index_desc();
40
21.0M
  if (index_desc) {
41
0
    expr_pb->set_column_id(index_desc->id());
42
0
    return Status::OK();
43
0
  }
44
45
21.0M
  switch (expr->expr_op()) {
46
0
    case ExprOperator::kNoOp:
47
0
      return Status::OK();
48
49
7.65M
    case ExprOperator::kConst: {
50
7.65M
      QLValuePB *const_pb = expr_pb->mutable_value();
51
7.65M
      return PTConstToPB(expr, const_pb);
52
0
    }
53
54
898
    case ExprOperator::kCollection: {
55
      // First try to fold literals. Literal-folding is a bit faster than constant folding, so we
56
      // keep this process although constant-folding can also fold literals.
57
898
      QLValuePB *const_pb = expr_pb->mutable_value();
58
898
      if (!PTConstToPB(expr, const_pb).ok()) {
59
        // Use constant folding because literal-folding cannot fold expressions.
60
        // Example: "List<BLOB>" with function calls.
61
        //   [ TextAsBlob('a'), IntAsBlob(1) ]
62
17
        RETURN_NOT_OK(PTExprToPB(static_cast<const PTCollectionExpr*>(expr.get()), expr_pb));
63
10
        return EvalExpr(expr_pb, QLTableRow::empty_row());
64
17
      }
65
881
      return Status::OK();
66
898
    }
67
68
398k
    case ExprOperator::kRef:
69
398k
      return PTExprToPB(static_cast<const PTRef*>(expr.get()), expr_pb);
70
71
31
    case ExprOperator::kSubColRef:
72
31
      return PTExprToPB(static_cast<const PTSubscriptedColumn*>(expr.get()), expr_pb);
73
74
713
    case ExprOperator::kJsonOperatorRef:
75
713
      return PTExprToPB(static_cast<const PTJsonColumnWithOperators*>(expr.get()), expr_pb);
76
77
12.9M
    case ExprOperator::kBindVar:
78
12.9M
      return PTExprToPB(static_cast<const PTBindVar*>(expr.get()), expr_pb);
79
80
2
    case ExprOperator::kAlias:
81
2
      return PTExprToPB(expr->op1(), expr_pb);
82
83
4.50k
    case ExprOperator::kUMinus:
84
4.50k
      return PTUMinusToPB(static_cast<const PTOperator1*>(expr.get()), expr_pb);
85
86
1.41k
    case ExprOperator::kBcall:
87
1.41k
      return PTExprToPB(static_cast<const PTBcall*>(expr.get()), expr_pb);
88
89
0
    case ExprOperator::kLogic1:
90
0
      return PTExprToPB(static_cast<const PTLogic1*>(expr.get()), expr_pb);
91
92
147
    case ExprOperator::kLogic2:
93
147
      return PTExprToPB(static_cast<const PTLogic2*>(expr.get()), expr_pb);
94
95
98
    case ExprOperator::kRelation0:
96
98
      return PTExprToPB(static_cast<const PTRelation0*>(expr.get()), expr_pb);
97
98
0
    case ExprOperator::kRelation1:
99
0
      return PTExprToPB(static_cast<const PTRelation1*>(expr.get()), expr_pb);
100
101
426
    case ExprOperator::kRelation2:
102
426
      return PTExprToPB(static_cast<const PTRelation2*>(expr.get()), expr_pb);
103
104
0
    case ExprOperator::kRelation3:
105
0
      return PTExprToPB(static_cast<const PTRelation3*>(expr.get()), expr_pb);
106
107
0
    default:
108
0
      LOG(FATAL) << "Not supported operator" << static_cast<int>(expr->expr_op());
109
21.0M
  }
110
0
  return Status::OK();
111
21.0M
}
112
113
//--------------------------------------------------------------------------------------------------
114
115
12.9M
CHECKED_STATUS Executor::PTExprToPB(const PTBindVar *bind_pt, QLExpressionPB *expr_pb) {
116
12.9M
  if (!bind_pt->name()) {
117
0
    return STATUS(NotSupported, "Undefined bind variable name, please contact the support");
118
0
  }
119
120
12.9M
  QLValue ql_bind;
121
12.9M
  DCHECK_NOTNULL(bind_pt->name().get());
122
12.9M
  RETURN_NOT_OK(exec_context_->params().GetBindVariable(bind_pt->name()->c_str(),
123
12.9M
                                                        bind_pt->pos(),
124
12.9M
                                                        bind_pt->ql_type(),
125
12.9M
                                                        &ql_bind));
126
12.9M
  *expr_pb->mutable_value() = std::move(*ql_bind.mutable_value());
127
12.9M
  return Status::OK();
128
12.9M
}
129
130
//--------------------------------------------------------------------------------------------------
131
132
398k
CHECKED_STATUS Executor::PTExprToPB(const PTRef *ref_pt, QLExpressionPB *ref_pb) {
133
  // When processing constant folding by client, all columns are not yet accessible, and "PTRef"
134
  // execution should returns an error to indicate that the folding effort failed.
135
398k
  const ColumnDesc *col_desc = ref_pt->desc();
136
398k
  if (!col_desc) {
137
    // Protection against crash. This happens when compiler failed to analyze a column ref.
138
    // Example
139
    //   SELECT [ i ] FROM tab;
140
    // Expression "[ i ]" should have been analyzed and reported during semantic phase. However,
141
    // it requires some effort to fix in semantic phase, this error is reported here for now.
142
3
    return STATUS(RuntimeError, "Failed to read column value");
143
3
  }
144
398k
  ref_pb->set_column_id(col_desc->id());
145
398k
  return Status::OK();
146
398k
}
147
148
31
CHECKED_STATUS Executor::PTExprToPB(const PTSubscriptedColumn *ref_pt, QLExpressionPB *expr_pb) {
149
31
  const ColumnDesc *col_desc = ref_pt->desc();
150
31
  auto col_pb = expr_pb->mutable_subscripted_col();
151
31
  col_pb->set_column_id(col_desc->id());
152
31
  for (auto& arg : ref_pt->args()->node_list()) {
153
31
    RETURN_NOT_OK(PTExprToPB(arg, col_pb->add_subscript_args()));
154
31
  }
155
156
31
  return Status::OK();
157
31
}
158
159
CHECKED_STATUS Executor::PTExprToPB(const PTJsonColumnWithOperators *ref_pt,
160
713
                                    QLExpressionPB *expr_pb) {
161
713
  const ColumnDesc *col_desc = ref_pt->desc();
162
713
  auto col_pb = expr_pb->mutable_json_column();
163
713
  col_pb->set_column_id(col_desc->id());
164
1.13k
  for (auto& arg : ref_pt->operators()->node_list()) {
165
1.13k
    RETURN_NOT_OK(PTJsonOperatorToPB(std::dynamic_pointer_cast<PTJsonOperator>(arg),
166
1.13k
                                     col_pb->add_json_operations()));
167
1.13k
  }
168
169
713
  return Status::OK();
170
713
}
171
172
//--------------------------------------------------------------------------------------------------
173
174
7.31M
CHECKED_STATUS Executor::PTExprToPB(const PTAllColumns *ref_pt, QLReadRequestPB *req) {
175
7.31M
  QLRSRowDescPB *rsrow_desc_pb = req->mutable_rsrow_desc();
176
23.2M
  for (const auto& col_desc : ref_pt->columns()) {
177
23.2M
    req->add_selected_exprs()->set_column_id(col_desc.id());
178
179
    // Add the expression metadata (rsrow descriptor).
180
23.2M
    QLRSColDescPB *rscol_descs_pb = rsrow_desc_pb->add_rscol_descs();
181
23.2M
    rscol_descs_pb->set_name(col_desc.name());
182
23.2M
    col_desc.ql_type()->ToQLTypePB(rscol_descs_pb->mutable_ql_type());
183
23.2M
  }
184
7.31M
  return Status::OK();
185
7.31M
}
186
187
}  // namespace ql
188
}  // namespace yb