|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "velox/exec/fuzzer/VeloxQueryRunner.h" |
| 18 | + |
| 19 | +#include <cpr/cpr.h> // @manual |
| 20 | +#include <fmt/format.h> |
| 21 | +#include <folly/Uri.h> |
| 22 | +#include <folly/json.h> |
| 23 | +#include <re2/re2.h> |
| 24 | +#include <thrift/lib/cpp2/async/RocketClientChannel.h> |
| 25 | +#include "velox/core/PlanNode.h" |
| 26 | +#include "velox/exec/fuzzer/if/gen-cpp2/LocalRunnerService.h" |
| 27 | +#include "velox/exec/tests/utils/QueryAssertions.h" |
| 28 | +#include "velox/functions/prestosql/types/BingTileType.h" |
| 29 | +#include "velox/functions/prestosql/types/GeometryType.h" |
| 30 | +#include "velox/functions/prestosql/types/IPAddressType.h" |
| 31 | +#include "velox/functions/prestosql/types/IPPrefixType.h" |
| 32 | +#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h" |
| 33 | +#include "velox/functions/prestosql/types/UuidType.h" |
| 34 | +#include "velox/serializers/PrestoSerializer.h" |
| 35 | +#include "velox/type/parser/TypeParser.h" |
| 36 | + |
| 37 | +using namespace facebook::velox::runner; |
| 38 | + |
| 39 | +namespace facebook::velox::exec::test { |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +RowTypePtr parseBatchRowType(Batch batch) { |
| 44 | + std::vector<std::string> names; |
| 45 | + std::vector<TypePtr> types; |
| 46 | + |
| 47 | + for (const auto& name : *batch.columnNames()) { |
| 48 | + names.push_back(name); |
| 49 | + } |
| 50 | + |
| 51 | + // Clean up type strings and format according to TypeParser::parseType |
| 52 | + // expectation. Input types are serialized by type()->asRow() for Thrift |
| 53 | + // struct by LocalRunnerService in the format of SOME_COMPLEX_TYPE<TYPE_1, |
| 54 | + // TYPE_2, etc.> (note the '<' and '>'). And in the case of complex type ROW, |
| 55 | + // types follow column name and a semicolon: as an example, ROW<f0:TYPE_1, |
| 56 | + // f1:TYPE_2, etc.>. We need to change these particular character choices to |
| 57 | + // paranthesis (in the case of the angled brackets) and spaces (in the case of |
| 58 | + // the semicolon). As an example, |
| 59 | + // MAP<VARCHAR,TIMESTAMP> -> MAP(VARCHAR, TIMESTAMP) |
| 60 | + // ROW<f0:TYPE_1, f1:TYPE_2, etc.> -> ROW(f0 TYPE_1, f1 TYPE_2, etc.) |
| 61 | + // as expected by TypeParser::parseType. Without this cleanup, parse will |
| 62 | + // crash and fuzzer will fail. |
| 63 | + for (const auto& typeString : *batch.columnTypes()) { |
| 64 | + auto parsedTypeString = typeString; |
| 65 | + std::replace(parsedTypeString.begin(), parsedTypeString.end(), '<', '('); |
| 66 | + std::replace(parsedTypeString.begin(), parsedTypeString.end(), '>', ')'); |
| 67 | + std::replace(parsedTypeString.begin(), parsedTypeString.end(), ':', ' '); |
| 68 | + types.push_back(parseType(parsedTypeString)); |
| 69 | + } |
| 70 | + |
| 71 | + return ROW(std::move(names), std::move(types)); |
| 72 | +} |
| 73 | + |
| 74 | +std::vector<RowVectorPtr> deserializeBatches( |
| 75 | + const std::vector<Batch>& resultBatches, |
| 76 | + memory::MemoryPool* pool) { |
| 77 | + std::vector<RowVectorPtr> queryResults; |
| 78 | + |
| 79 | + auto serde = std::make_unique<serializer::presto::PrestoVectorSerde>(); |
| 80 | + serializer::presto::PrestoVectorSerde::PrestoOptions options; |
| 81 | + |
| 82 | + for (const auto& batch : resultBatches) { |
| 83 | + VELOX_CHECK( |
| 84 | + apache::thrift::is_non_optional_field_set_manually_or_by_serializer( |
| 85 | + batch.serializedData_ref())); |
| 86 | + VELOX_CHECK(!batch.serializedData()->empty()); |
| 87 | + |
| 88 | + // Deserialize binary data. |
| 89 | + const auto& serializedData = *batch.serializedData(); |
| 90 | + ByteRange byteRange{ |
| 91 | + reinterpret_cast<uint8_t*>(const_cast<char*>(serializedData.data())), |
| 92 | + static_cast<int32_t>(serializedData.length()), |
| 93 | + 0}; |
| 94 | + auto byteStream = std::make_unique<BufferInputStream>( |
| 95 | + std::vector<ByteRange>{{byteRange}}); |
| 96 | + |
| 97 | + RowVectorPtr rowVector; |
| 98 | + serde->deserialize( |
| 99 | + byteStream.get(), |
| 100 | + pool, |
| 101 | + parseBatchRowType(batch), |
| 102 | + &rowVector, |
| 103 | + 0, |
| 104 | + &options); |
| 105 | + |
| 106 | + VELOX_CHECK_NOT_NULL(rowVector); |
| 107 | + queryResults.push_back(rowVector); |
| 108 | + } |
| 109 | + |
| 110 | + return queryResults; |
| 111 | +} |
| 112 | + |
| 113 | +std::shared_ptr<apache::thrift::Client<LocalRunnerService>> createThriftClient( |
| 114 | + const std::string& host, |
| 115 | + int port, |
| 116 | + std::chrono::milliseconds timeout, |
| 117 | + folly::EventBase* evb) { |
| 118 | + folly::SocketAddress addr(host, port); |
| 119 | + auto socket = folly::AsyncSocket::newSocket(evb, addr, timeout.count()); |
| 120 | + auto channel = |
| 121 | + apache::thrift::RocketClientChannel::newChannel(std::move(socket)); |
| 122 | + return std::make_shared<apache::thrift::Client<LocalRunnerService>>( |
| 123 | + std::move(channel)); |
| 124 | +} |
| 125 | +} // namespace |
| 126 | + |
| 127 | +VeloxQueryRunner::VeloxQueryRunner( |
| 128 | + memory::MemoryPool* aggregatePool, |
| 129 | + std::string serviceUri, |
| 130 | + std::chrono::milliseconds timeout) |
| 131 | + : ReferenceQueryRunner(aggregatePool), |
| 132 | + serviceUri_(std::move(serviceUri)), |
| 133 | + timeout_(timeout) { |
| 134 | + pool_ = aggregatePool->addLeafChild("leaf"); |
| 135 | + |
| 136 | + folly::Uri uri(serviceUri_); |
| 137 | + thriftHost_ = uri.host(); |
| 138 | + thriftPort_ = uri.port(); |
| 139 | +} |
| 140 | + |
| 141 | +const std::vector<TypePtr>& VeloxQueryRunner::supportedScalarTypes() const { |
| 142 | + static const std::vector<TypePtr> kScalarTypes{ |
| 143 | + BOOLEAN(), |
| 144 | + TINYINT(), |
| 145 | + SMALLINT(), |
| 146 | + INTEGER(), |
| 147 | + BIGINT(), |
| 148 | + REAL(), |
| 149 | + DOUBLE(), |
| 150 | + VARCHAR(), |
| 151 | + VARBINARY(), |
| 152 | + TIMESTAMP(), |
| 153 | + TIMESTAMP_WITH_TIME_ZONE(), |
| 154 | + IPADDRESS(), |
| 155 | + UUID(), |
| 156 | + // https://github.com/facebookincubator/velox/issues/15379 (IPPREFIX) |
| 157 | + // https://github.com/facebookincubator/velox/issues/15380 (Non-orderable |
| 158 | + // custom types such as HYPERLOGLOG, JSON, BINGTILE, GEOMETRY, etc.) |
| 159 | + }; |
| 160 | + return kScalarTypes; |
| 161 | +} |
| 162 | + |
| 163 | +const std::unordered_map<std::string, DataSpec>& |
| 164 | +VeloxQueryRunner::aggregationFunctionDataSpecs() const { |
| 165 | + static const std::unordered_map<std::string, DataSpec> |
| 166 | + kAggregationFunctionDataSpecs{}; |
| 167 | + return kAggregationFunctionDataSpecs; |
| 168 | +} |
| 169 | + |
| 170 | +std::optional<std::string> VeloxQueryRunner::toSql( |
| 171 | + const core::PlanNodePtr& /*plan*/) { |
| 172 | + // We don't need to convert to SQL for VeloxQueryRunner |
| 173 | + // as we're sending the serialized plan directly |
| 174 | + VELOX_FAIL("VeloxQueryRunner does not support SQL conversion"); |
| 175 | +} |
| 176 | + |
| 177 | +bool VeloxQueryRunner::isConstantExprSupported( |
| 178 | + const core::TypedExprPtr& /*expr*/) { |
| 179 | + // Since we're using Velox directly, we support all constant expressions |
| 180 | + return true; |
| 181 | +} |
| 182 | + |
| 183 | +bool VeloxQueryRunner::isSupported( |
| 184 | + const exec::FunctionSignature& /*signature*/) { |
| 185 | + // Since we're using Velox directly, we support all function signatures |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
| 189 | +std::vector<RowVectorPtr> VeloxQueryRunner::execute( |
| 190 | + const std::string& /*sql*/) { |
| 191 | + VELOX_FAIL("VeloxQueryRunner does not support SQL execution"); |
| 192 | +} |
| 193 | + |
| 194 | +std::vector<RowVectorPtr> VeloxQueryRunner::execute( |
| 195 | + const std::string& /*sql*/, |
| 196 | + const std::string& /*sessionProperty*/) { |
| 197 | + VELOX_FAIL("VeloxQueryRunner does not support SQL execution"); |
| 198 | +} |
| 199 | + |
| 200 | +std::pair< |
| 201 | + std::optional<std::multiset<std::vector<velox::variant>>>, |
| 202 | + ReferenceQueryErrorCode> |
| 203 | +VeloxQueryRunner::execute(const core::PlanNodePtr& plan) { |
| 204 | + auto serializedPlan = serializePlan(plan); |
| 205 | + auto queryId = fmt::format("velox_local_query_runner_{}", rand()); |
| 206 | + |
| 207 | + auto client = |
| 208 | + createThriftClient(thriftHost_, thriftPort_, timeout_, &eventBase_); |
| 209 | + |
| 210 | + // Create the request |
| 211 | + ExecutePlanRequest request; |
| 212 | + request.serializedPlan() = serializedPlan; |
| 213 | + request.queryId() = queryId; |
| 214 | + request.numWorkers() = 4; // Default value |
| 215 | + request.numDrivers() = 2; // Default value |
| 216 | + |
| 217 | + // Send the request |
| 218 | + ExecutePlanResponse response; |
| 219 | + try { |
| 220 | + client->sync_execute(response, request); |
| 221 | + } catch (const std::exception& e) { |
| 222 | + VELOX_FAIL("Thrift request failed: {}", e.what()); |
| 223 | + } |
| 224 | + |
| 225 | + // Handle the response |
| 226 | + if (*response.success()) { |
| 227 | + LOG(INFO) << "Reference eval succeeded."; |
| 228 | + return std::make_pair( |
| 229 | + exec::test::materialize( |
| 230 | + deserializeBatches(*response.results(), pool_.get())), |
| 231 | + ReferenceQueryErrorCode::kSuccess); |
| 232 | + } else { |
| 233 | + LOG(INFO) << "Reference eval failed."; |
| 234 | + return std::make_pair( |
| 235 | + std::nullopt, ReferenceQueryErrorCode::kReferenceQueryFail); |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +std::string VeloxQueryRunner::serializePlan(const core::PlanNodePtr& plan) { |
| 240 | + // Serialize the plan to JSON |
| 241 | + folly::dynamic serializedPlan = plan->serialize(); |
| 242 | + return folly::toJson(serializedPlan); |
| 243 | +} |
| 244 | + |
| 245 | +} // namespace facebook::velox::exec::test |
0 commit comments