Skip to content

Generate ProtocolToThrift files through make and commit #25162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions presto-native-execution/presto_cpp/main/thrift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
presto_thrift.json
presto_protocol-to-thrift-json.json
30 changes: 30 additions & 0 deletions presto-native-execution/presto_cpp/main/thrift/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

all: ProtocolToThrift.h ProtocolToThrift.cpp
Copy link
Contributor Author

@vhsu14 vhsu14 May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Makefile defines the flow for generating ProtocolToThrift files as defined in README.md. For now, it depends on presto_thrift.thrift, which is a copy of Drift generated IDL. @shangm2 is working on having it auto-generated as part of presto-thrift-spec and we will reference to the generated IDL once that is done.


ProtocolToThrift.h: ProtocolToThrift-hpp.mustache presto_protocol-to-thrift-json.json
echo "// DO NOT EDIT : This file is generated by presto_protocol-to-thrift-json.py" > ProtocolToThrift.h
chevron -d presto_protocol-to-thrift-json.json ProtocolToThrift-hpp.mustache >> ProtocolToThrift.h
clang-format -style=file -i ProtocolToThrift.h

ProtocolToThrift.cpp: ProtocolToThrift-cpp.mustache presto_protocol-to-thrift-json.json
echo "// DO NOT EDIT : This file is generated by presto_protocol-to-thrift-json.py" > ProtocolToThrift.cpp
chevron -d presto_protocol-to-thrift-json.json ProtocolToThrift-cpp.mustache >> ProtocolToThrift.cpp
clang-format -style=file -i ProtocolToThrift.cpp

presto_protocol-to-thrift-json.json: presto_protocol-to-thrift-json.py presto_protocol-to-thrift-json.yml presto_thrift.json ../../presto_protocol/core/presto_protocol_core.json
./presto_protocol-to-thrift-json.py presto_thrift.json ../../presto_protocol/core/presto_protocol_core.json | jq . > presto_protocol-to-thrift-json.json

presto_thrift.json: presto_thrift.thrift ./thrift2json.py
./thrift2json.py presto_thrift.thrift | jq . > presto_thrift.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .thrift file is at presto-thrift-spec/target/thrift/presto-thrift-protocol.thrift


Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

{{! Select all the comment items and expand them here }}
{{#.}}
{{#comment}}
{{comment}}
{{/comment}}
{{/.}}

#include "presto_cpp/main/thrift/ProtocolToThrift.h"

namespace facebook::presto::thrift {

// These could be covered by a more general template but this way only
// conversions to supported Thrift data types can be generated.
void toThrift(const std::string& proto, std::string& thrift) {
thrift = proto;
}
void toThrift(const bool& proto, bool& thrift) {
thrift = proto;
}
void toThrift(const int16_t& proto, int16_t& thrift) {
thrift = proto;
}
void toThrift(const int32_t& proto, int32_t& thrift) {
thrift = proto;
}
void toThrift(const int64_t& proto, int64_t& thrift) {
thrift = proto;
}
void toThrift(const double& proto, double& thrift) {
thrift = proto;
}
void toThrift(const facebook::presto::protocol::Duration& duration, double& thrift) {
thrift = duration.getValue(facebook::presto::protocol::TimeUnit::MILLISECONDS);
}
void toThrift(const facebook::presto::protocol::DataSize& dataSize, double& thrift) {
thrift = dataSize.getValue(facebook::presto::protocol::DataUnit::BYTE);
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, std::shared_ptr<T>& thrift) {
if (proto) {
thrift = std::make_shared<T>();
toThrift(*proto, *thrift);
}
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, T& thrift) {
if (proto) {
toThrift(*proto, thrift);
}
}

template <typename V, typename S>
void toThrift(const std::vector<V>& v, std::set<S>& s) {
S toItem;
for (const auto& fromItem : v) {
toThrift(fromItem, toItem);
s.insert(std::move(toItem));
}
}

template <typename P, typename T>
void toThrift(const std::vector<P>& p, std::vector<T>& t) {
t.reserve(p.size());
T toItem;
for (const auto& fromItem : p) {
toThrift(fromItem, toItem);
t.emplace_back(std::move(toItem));
}
}

template <typename K1, typename V1, typename K2, typename V2>
void toThrift(const std::map<K1, V1>& protoMap, std::map<K2, V2>& thriftMap) {
K2 toKey;
V2 toValue;
for (const auto& [fromKey, fromValue] : protoMap) {
toThrift(fromKey, toKey);
toThrift(fromValue, toValue);
thriftMap.emplace(std::move(toKey), std::move(toValue));
}
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, apache::thrift::optional_field_ref<T> thrift) {
if (proto) {
thrift.ensure();
toThrift(*proto, apache::thrift::can_throw(*thrift));
}
}

void fromThrift(const std::string& thrift, std::string& proto) {
proto = thrift;
}
void fromThrift(const bool& thrift, bool& proto) {
proto = thrift;
}
void fromThrift(const int32_t& thrift, int32_t& proto) {
proto = thrift;
}
void fromThrift(const int64_t& thrift, int64_t& proto) {
proto = thrift;
}
void fromThrift(const double& thrift, double& proto) {
proto = thrift;
}

void fromThrift(const double& thrift, facebook::presto::protocol::Duration& duration) {
duration = facebook::presto::protocol::Duration(thrift, facebook::presto::protocol::TimeUnit::MILLISECONDS);
}

void fromThrift(const double& thrift, facebook::presto::protocol::DataSize& dataSize) {
dataSize = facebook::presto::protocol::DataSize(thrift, facebook::presto::protocol::DataUnit::BYTE);
}

template <typename P, typename T>
void fromThrift(const apache::thrift::optional_field_ref<T>& thrift, std::shared_ptr<P>& proto) {
if (thrift.has_value()) {
proto = std::make_shared<P>();
fromThrift(*thrift, *proto);
}
}

template <typename P, typename T>
void fromThrift(const T& thrift, std::shared_ptr<P>& proto) {
proto = std::make_shared<P>();
fromThrift(thrift, *proto);
}

template <typename P, typename T>
void fromThrift(const std::shared_ptr<P>& thrift, std::shared_ptr<T>& proto) {
if (thrift) {
proto = std::make_shared<T>();
fromThrift(*thrift, *proto);
}
}

template <typename V, typename S>
void fromThrift(const std::set<S>& thrift, std::vector<V>& proto) {
proto.reserve(thrift.size());
V toItem;
for (const auto& fromItem : thrift) {
fromThrift(fromItem, toItem);
proto.emplace_back(std::move(toItem));
}
}

template <typename P, typename T>
void fromThrift(const std::vector<P>& thrift, std::vector<T>& proto) {
proto.reserve(thrift.size());
T toItem;
for (const auto& fromItem : thrift) {
fromThrift(fromItem, toItem);
proto.emplace_back(std::move(toItem));
}
}

template <typename K1, typename V1, typename K2, typename V2>
void fromThrift(const std::map<K1, V1>& thriftMap, std::map<K2, V2>& protoMap) {
K2 toKey;
V2 toValue;
for (const auto& [fromKey, fromValue] : thriftMap) {
fromThrift(fromKey, toKey);
fromThrift(fromValue, toValue);
protoMap.emplace(std::move(toKey), std::move(toValue));
}
}

{{! Select all the items and expand either the "hinc" member or the "struct", "enum" members }}
{{#.}}
{{#cinc}}
{{&cinc}}
{{/cinc}}
{{^cinc}}
{{#struct}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{&class_name}}& thrift) {
{{#fields}}
toThrift(proto.{{proto_name}}, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref());
{{/fields}}
}
void fromThrift(const {{&class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) {
{{#fields}}
fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto.{{proto_name}});
{{/fields}}
}

{{/struct}}
{{#wrapper}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift) {
{{#fields}}
toThrift(proto, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref());
{{/fields}}
}
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) {
{{#fields}}
fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto);
{{/fields}}
}
{{/wrapper}}
{{#enum}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift) {
thrift = ({{class_name}})(static_cast<int>(proto));
}
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) {
proto = (facebook::presto::protocol::{{class_name}})(static_cast<int>(thrift));
}

{{/enum}}
{{/cinc}}
{{/.}}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

{{! Select all the comment items and expand them here }}
{{#.}}
{{#comment}}
{{comment}}
{{/comment}}
{{/.}}

#include "presto_cpp/presto_protocol/core/presto_protocol_core.h"
#include "presto_cpp/main/thrift/gen-cpp2/presto_thrift_types.h"

namespace facebook::presto::thrift {

void toThrift(const facebook::presto::protocol::Duration& duration, double& thrift);
void toThrift(const facebook::presto::protocol::DataSize& dataSize, double& thrift);
void fromThrift(const double& thrift, facebook::presto::protocol::Duration& duration);
void fromThrift(const double& thrift, facebook::presto::protocol::DataSize& dataSize);

{{! Select all the items and expand either the "hinc" member or the "struct", "enum" members }}
{{#.}}
{{#hinc}}
{{&hinc}}
{{/hinc}}
{{^hinc}}
{{#struct}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift);
void fromThrift(const {{&class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto);

{{/struct}}
{{#wrapper}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift);
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto);

{{/wrapper}}
{{#enum}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift);
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto);

{{/enum}}
{{/hinc}}
{{/.}}

} // namespace facebook::presto
Loading
Loading