YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/docdb/primitive_value_util.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
#include "yb/docdb/primitive_value_util.h"
14
15
#include "yb/common/ql_expr.h"
16
#include "yb/common/ql_value.h"
17
#include "yb/common/pgsql_protocol.pb.h"
18
#include "yb/common/schema.h"
19
20
#include "yb/docdb/primitive_value.h"
21
22
#include "yb/util/result.h"
23
#include "yb/util/status_format.h"
24
25
using std::vector;
26
27
namespace yb {
28
namespace docdb {
29
30
// Add primary key column values to the component group. Verify that they are in the same order
31
// as in the table schema.
32
Status QLKeyColumnValuesToPrimitiveValues(
33
    const google::protobuf::RepeatedPtrField<QLExpressionPB> &column_values,
34
    const Schema &schema, size_t column_idx, const size_t column_count,
35
10.4M
    vector<PrimitiveValue> *components) {
36
8.79M
  for (const auto& column_value : column_values) {
37
8.79M
    if (!schema.is_key_column(column_idx)) {
38
0
      auto status = STATUS_FORMAT(
39
0
          Corruption, "Column at $0 is not key column in $1", column_idx, schema.ToString());
40
0
      LOG(DFATAL) << status;
41
0
      return status;
42
0
    }
43
44
8.79M
    if (!column_value.has_value() || IsNull(column_value.value())) {
45
4.29k
      components->push_back(PrimitiveValue(ValueType::kNullLow));
46
8.78M
    } else {
47
8.78M
      components->push_back(PrimitiveValue::FromQLValuePB(
48
8.78M
          column_value.value(), schema.column(column_idx).sorting_type()));
49
8.78M
    }
50
8.79M
    column_idx++;
51
8.79M
  }
52
10.4M
  return Status::OK();
53
10.4M
}
54
55
// ------------------------------------------------------------------------------------------------
56
Result<vector<PrimitiveValue>> InitKeyColumnPrimitiveValues(
57
    const google::protobuf::RepeatedPtrField<PgsqlExpressionPB> &column_values,
58
    const Schema &schema,
59
8.60M
    size_t start_idx) {
60
8.60M
  vector<PrimitiveValue> values;
61
8.60M
  values.reserve(column_values.size());
62
8.60M
  size_t column_idx = start_idx;
63
6.71M
  for (const auto& column_value : column_values) {
64
6.71M
    if (!schema.is_key_column(column_idx)) {
65
0
      auto status = STATUS_FORMAT(
66
0
          Corruption, "Column at $0 is not key column in $1", column_idx, schema.ToString());
67
0
      LOG(DFATAL) << status;
68
0
      return status;
69
0
    }
70
6.71M
    const auto sorting_type = schema.column(column_idx).sorting_type();
71
6.71M
    if (column_value.has_value()) {
72
6.71M
      const auto& value = column_value.value();
73
298k
      values.push_back(IsNull(value) ? PrimitiveValue::NullValue(sorting_type)
74
6.41M
                                     : PrimitiveValue::FromQLValuePB(value, sorting_type));
75
1.77k
    } else {
76
      // TODO(neil) The current setup only works for CQL as it assumes primary key value must not
77
      // be dependent on any column values. This needs to be fixed as PostgreSQL expression might
78
      // require a read from a table.
79
      //
80
      // Use regular executor for now.
81
1.77k
      QLExprExecutor executor;
82
1.77k
      QLExprResult result;
83
1.77k
      RETURN_NOT_OK(executor.EvalExpr(column_value, nullptr, result.Writer(), &schema));
84
85
1.77k
      values.push_back(PrimitiveValue::FromQLValuePB(result.Value(), sorting_type));
86
1.77k
    }
87
6.71M
    column_idx++;
88
6.71M
  }
89
8.60M
  return std::move(values);
90
8.60M
}
91
92
}  // namespace docdb
93
}  // namespace yb