| 101 | } |
| 102 | |
| 103 | void SparkVariantEncoder::encode( |
| 104 | simdjson::dom::element val, |
| 105 | const StringDictionary& dict, |
| 106 | std::string& out) { |
| 107 | switch (val.type()) { |
| 108 | case simdjson::dom::element_type::NULL_VALUE: |
| 109 | out.push_back(PRIMITIVE_NULL << 2 | BASIC_TYPE_PRIMITIVE); |
| 110 | break; |
| 111 | case simdjson::dom::element_type::BOOL: |
| 112 | if (val.get<bool>().value_unsafe()) { |
| 113 | out.push_back(PRIMITIVE_TRUE << 2 | BASIC_TYPE_PRIMITIVE); |
| 114 | } else { |
| 115 | out.push_back(PRIMITIVE_FALSE << 2 | BASIC_TYPE_PRIMITIVE); |
| 116 | } |
| 117 | break; |
| 118 | case simdjson::dom::element_type::INT64: { |
| 119 | int64_t i = val.get<int64_t>().value_unsafe(); |
| 120 | if (i >= std::numeric_limits<int8_t>::min() && |
| 121 | i <= std::numeric_limits<int8_t>::max()) { |
| 122 | out.push_back(PRIMITIVE_INT1 << 2 | BASIC_TYPE_PRIMITIVE); |
| 123 | int8_t v = static_cast<int8_t>(i); |
| 124 | out.append(reinterpret_cast<const char*>(&v), 1); |
| 125 | } else if ( |
| 126 | i >= std::numeric_limits<int16_t>::min() && |
| 127 | i <= std::numeric_limits<int16_t>::max()) { |
| 128 | out.push_back(PRIMITIVE_INT2 << 2 | BASIC_TYPE_PRIMITIVE); |
| 129 | int16_t v = static_cast<int16_t>(i); |
| 130 | out.append(reinterpret_cast<const char*>(&v), 2); |
| 131 | } else if ( |
| 132 | i >= std::numeric_limits<int32_t>::min() && |
| 133 | i <= std::numeric_limits<int32_t>::max()) { |
| 134 | out.push_back(PRIMITIVE_INT4 << 2 | BASIC_TYPE_PRIMITIVE); |
| 135 | int32_t v = static_cast<int32_t>(i); |
| 136 | out.append(reinterpret_cast<const char*>(&v), 4); |
| 137 | } else { |
| 138 | out.push_back(PRIMITIVE_INT8 << 2 | BASIC_TYPE_PRIMITIVE); |
| 139 | out.append(reinterpret_cast<const char*>(&i), 8); |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | case simdjson::dom::element_type::UINT64: { |
| 144 | uint64_t u = val.get<uint64_t>().value_unsafe(); |
| 145 | if (u <= (uint64_t)std::numeric_limits<int64_t>::max()) { |
| 146 | int64_t i = static_cast<int64_t>(u); |
| 147 | if (i <= std::numeric_limits<int8_t>::max()) { |
| 148 | out.push_back(PRIMITIVE_INT1 << 2 | BASIC_TYPE_PRIMITIVE); |
| 149 | int8_t v = static_cast<int8_t>(i); |
| 150 | out.append(reinterpret_cast<const char*>(&v), 1); |
| 151 | } else if (i <= std::numeric_limits<int16_t>::max()) { |
| 152 | out.push_back(PRIMITIVE_INT2 << 2 | BASIC_TYPE_PRIMITIVE); |
| 153 | int16_t v = static_cast<int16_t>(i); |
| 154 | out.append(reinterpret_cast<const char*>(&v), 2); |
| 155 | } else if (i <= std::numeric_limits<int32_t>::max()) { |
| 156 | out.push_back(PRIMITIVE_INT4 << 2 | BASIC_TYPE_PRIMITIVE); |
| 157 | int32_t v = static_cast<int32_t>(i); |
| 158 | out.append(reinterpret_cast<const char*>(&v), 4); |
| 159 | } else { |
| 160 | out.push_back(PRIMITIVE_INT8 << 2 | BASIC_TYPE_PRIMITIVE); |