Skip to content

Commit 543b48e

Browse files
committed
[native] Add presto.default-namespace Prestissimo config property
1 parent 719bfe2 commit 543b48e

File tree

11 files changed

+189
-31
lines changed

11 files changed

+189
-31
lines changed

presto-docs/src/main/sphinx/presto_cpp/properties.rst

+8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ alphabetical order.
8585
This property provides function signatures for built-in aggregation
8686
functions which are compatible with Velox.
8787

88+
``presto.default-namespace``
89+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
91+
* **Type:** ``string``
92+
* **Default value:** ``presto.default``
93+
94+
Specifies the namespace prefix for native C++ functions.
95+
8896
Worker Properties
8997
-----------------
9098

presto-native-execution/presto_cpp/main/PrestoServer.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ void PrestoServer::run() {
241241
address_ = fmt::format("[{}]", address_);
242242
}
243243
nodeLocation_ = nodeConfig->nodeLocation();
244+
prestoBuiltinFunctionPrefix_ = systemConfig->prestoDefaultNamespacePrefix();
244245
} catch (const velox::VeloxUserError& e) {
245246
PRESTO_STARTUP_LOG(ERROR) << "Failed to start server due to " << e.what();
246247
exit(EXIT_FAILURE);
@@ -1308,11 +1309,12 @@ void PrestoServer::registerCustomOperators() {
13081309
}
13091310

13101311
void PrestoServer::registerFunctions() {
1311-
static const std::string kPrestoDefaultPrefix{"presto.default."};
1312-
velox::functions::prestosql::registerAllScalarFunctions(kPrestoDefaultPrefix);
1312+
velox::functions::prestosql::registerAllScalarFunctions(
1313+
prestoBuiltinFunctionPrefix_);
13131314
velox::aggregate::prestosql::registerAllAggregateFunctions(
1314-
kPrestoDefaultPrefix);
1315-
velox::window::prestosql::registerAllWindowFunctions(kPrestoDefaultPrefix);
1315+
prestoBuiltinFunctionPrefix_);
1316+
velox::window::prestosql::registerAllWindowFunctions(
1317+
prestoBuiltinFunctionPrefix_);
13161318
if (SystemConfig::instance()->registerTestFunctions()) {
13171319
velox::functions::prestosql::registerAllScalarFunctions(
13181320
"json.test_schema.");

presto-native-execution/presto_cpp/main/PrestoServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class PrestoServer {
291291
std::string address_;
292292
std::string nodeLocation_;
293293
folly::SSLContextPtr sslContext_;
294+
std::string prestoBuiltinFunctionPrefix_;
294295
};
295296

296297
} // namespace facebook::presto

presto-native-execution/presto_cpp/main/common/Configs.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ SystemConfig::SystemConfig() {
239239
STR_PROP(kCacheVeloxTtlCheckInterval, "1h"),
240240
BOOL_PROP(kEnableRuntimeMetricsCollection, false),
241241
BOOL_PROP(kPlanValidatorFailOnNestedLoopJoin, false),
242+
STR_PROP(kPrestoDefaultNamespacePrefix, "presto.default"),
242243
};
243244
}
244245

@@ -758,6 +759,10 @@ bool SystemConfig::enableRuntimeMetricsCollection() const {
758759
return optionalProperty<bool>(kEnableRuntimeMetricsCollection).value();
759760
}
760761

762+
std::string SystemConfig::prestoDefaultNamespacePrefix() const {
763+
return optionalProperty(kPrestoDefaultNamespacePrefix).value().append(".");
764+
}
765+
761766
NodeConfig::NodeConfig() {
762767
registeredProps_ =
763768
std::unordered_map<std::string, folly::Optional<std::string>>{

presto-native-execution/presto_cpp/main/common/Configs.h

+5
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ class SystemConfig : public ConfigBase {
655655
static constexpr std::string_view kPlanValidatorFailOnNestedLoopJoin{
656656
"velox-plan-validator-fail-on-nested-loop-join"};
657657

658+
// Specifies the default Presto namespace prefix.
659+
static constexpr std::string_view kPrestoDefaultNamespacePrefix{
660+
"presto.default-namespace"};
661+
658662
SystemConfig();
659663

660664
virtual ~SystemConfig() = default;
@@ -893,6 +897,7 @@ class SystemConfig : public ConfigBase {
893897
bool enableRuntimeMetricsCollection() const;
894898

895899
bool prestoNativeSidecar() const;
900+
std::string prestoDefaultNamespacePrefix() const;
896901
};
897902

898903
/// Provides access to node properties defined in node.properties file.

presto-native-execution/presto_cpp/main/common/Utils.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ void installSignalHandler();
4747
std::string extractMessageBody(
4848
const std::vector<std::unique_ptr<folly::IOBuf>>& body);
4949

50+
inline std::string addDefaultNamespacePrefix(
51+
const std::string& prestoDefaultNamespacePrefix,
52+
const std::string& functionName) {
53+
return fmt::format("{}{}", prestoDefaultNamespacePrefix, functionName);
54+
}
5055
} // namespace facebook::presto::util

presto-native-execution/presto_cpp/main/common/tests/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ target_link_libraries(
1818
presto_common_test
1919
presto_common
2020
presto_exception
21+
velox_aggregates
2122
velox_exception
2223
velox_file
24+
velox_functions_prestosql
25+
velox_function_registry
26+
velox_presto_types
27+
velox_window
2328
${RE2}
2429
GTest::gtest
2530
GTest::gtest_main)

presto-native-execution/presto_cpp/main/common/tests/ConfigTest.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,59 @@
1212
* limitations under the License.
1313
*/
1414
#include <gtest/gtest.h>
15+
#include <unordered_set>
1516
#include "presto_cpp/main/common/ConfigReader.h"
1617
#include "presto_cpp/main/common/Configs.h"
1718
#include "velox/common/base/Exceptions.h"
1819
#include "velox/common/base/tests/GTestUtils.h"
1920
#include "velox/common/file/File.h"
2021
#include "velox/common/file/FileSystems.h"
22+
#include "velox/exec/Aggregate.h"
23+
#include "velox/exec/Window.h"
24+
#include "velox/functions/FunctionRegistry.h"
25+
#include "velox/functions/prestosql/aggregates/RegisterAggregateFunctions.h"
26+
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"
27+
#include "velox/functions/prestosql/window/WindowFunctionsRegistration.h"
2128

2229
namespace facebook::presto::test {
2330

2431
using namespace velox;
2532

33+
namespace {
34+
35+
template <typename FunctionMap>
36+
bool validateDefaultNamespacePrefix(
37+
const FunctionMap& functionMap,
38+
const std::string& prestoDefaultNamespacePrefix) {
39+
static const std::unordered_set<std::string> kBlockList = {
40+
"row_constructor", "in", "is_null"};
41+
42+
std::vector<std::string> prestoDefaultNamespacePrefixParts;
43+
folly::split(
44+
'.',
45+
prestoDefaultNamespacePrefix,
46+
prestoDefaultNamespacePrefixParts,
47+
true);
48+
VELOX_CHECK_EQ(prestoDefaultNamespacePrefixParts.size(), 2);
49+
for (const auto& [functionName, _] : functionMap) {
50+
// Skip internal functions. They don't have any prefix.
51+
if ((kBlockList.count(functionName) != 0) ||
52+
(functionName.find("$internal$") != std::string::npos)) {
53+
continue;
54+
}
55+
56+
std::vector<std::string> parts;
57+
folly::split('.', functionName, parts, true);
58+
VELOX_CHECK_EQ(parts.size(), 3);
59+
if ((parts[0] != prestoDefaultNamespacePrefixParts[0]) ||
60+
(parts[1] != prestoDefaultNamespacePrefixParts[1])) {
61+
return false;
62+
}
63+
}
64+
return true;
65+
}
66+
} // namespace
67+
2668
class ConfigTest : public testing::Test {
2769
protected:
2870
void SetUp() override {
@@ -310,4 +352,35 @@ TEST_F(ConfigTest, readConfigEnvVarTest) {
310352
unsetenv(kEmptyEnvVarName.c_str());
311353
}
312354

355+
TEST_F(ConfigTest, prestoDefaultNamespacePrefix) {
356+
SystemConfig config;
357+
init(
358+
config,
359+
{{std::string(SystemConfig::kPrestoDefaultNamespacePrefix),
360+
"presto.default"}});
361+
std::string prestoBuiltinFunctionPrefix =
362+
config.prestoDefaultNamespacePrefix();
363+
364+
velox::functions::prestosql::registerAllScalarFunctions(
365+
prestoBuiltinFunctionPrefix);
366+
velox::aggregate::prestosql::registerAllAggregateFunctions(
367+
prestoBuiltinFunctionPrefix);
368+
velox::window::prestosql::registerAllWindowFunctions(
369+
prestoBuiltinFunctionPrefix);
370+
371+
// Get all registered scalar functions in Velox
372+
auto scalarFunctions = getFunctionSignatures();
373+
ASSERT_TRUE(validateDefaultNamespacePrefix(
374+
scalarFunctions, prestoBuiltinFunctionPrefix));
375+
376+
// Get all registered aggregate functions in Velox
377+
auto aggregateFunctions = exec::aggregateFunctions().copy();
378+
ASSERT_TRUE(validateDefaultNamespacePrefix(
379+
aggregateFunctions, prestoBuiltinFunctionPrefix));
380+
381+
// Get all registered window functions in Velox
382+
auto windowFunctions = exec::windowFunctions();
383+
ASSERT_TRUE(validateDefaultNamespacePrefix(
384+
windowFunctions, prestoBuiltinFunctionPrefix));
385+
}
313386
} // namespace facebook::presto::test

presto-native-execution/presto_cpp/main/types/PrestoToVeloxExpr.cpp

+70-25
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "presto_cpp/main/types/PrestoToVeloxExpr.h"
1616
#include <boost/algorithm/string/case_conv.hpp>
17+
#include "presto_cpp/main/common/Configs.h"
1718
#include "presto_cpp/presto_protocol/Base64Util.h"
1819
#include "velox/common/base/Exceptions.h"
1920
#include "velox/functions/prestosql/types/JsonType.h"
@@ -33,24 +34,44 @@ std::string toJsonString(const T& value) {
3334
}
3435

3536
std::string mapScalarFunction(const std::string& name) {
37+
static const std::string prestoDefaultNamespacePrefix =
38+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
3639
static const std::unordered_map<std::string, std::string> kFunctionNames = {
3740
// Operator overrides: com.facebook.presto.common.function.OperatorType
38-
{"presto.default.$operator$add", "presto.default.plus"},
39-
{"presto.default.$operator$between", "presto.default.between"},
40-
{"presto.default.$operator$divide", "presto.default.divide"},
41-
{"presto.default.$operator$equal", "presto.default.eq"},
42-
{"presto.default.$operator$greater_than", "presto.default.gt"},
43-
{"presto.default.$operator$greater_than_or_equal", "presto.default.gte"},
41+
{"presto.default.$operator$add",
42+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "plus")},
43+
{"presto.default.$operator$between",
44+
util::addDefaultNamespacePrefix(
45+
prestoDefaultNamespacePrefix, "between")},
46+
{"presto.default.$operator$divide",
47+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "divide")},
48+
{"presto.default.$operator$equal",
49+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "eq")},
50+
{"presto.default.$operator$greater_than",
51+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "gt")},
52+
{"presto.default.$operator$greater_than_or_equal",
53+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "gte")},
4454
{"presto.default.$operator$is_distinct_from",
45-
"presto.default.distinct_from"},
46-
{"presto.default.$operator$less_than", "presto.default.lt"},
47-
{"presto.default.$operator$less_than_or_equal", "presto.default.lte"},
48-
{"presto.default.$operator$modulus", "presto.default.mod"},
49-
{"presto.default.$operator$multiply", "presto.default.multiply"},
50-
{"presto.default.$operator$negation", "presto.default.negate"},
51-
{"presto.default.$operator$not_equal", "presto.default.neq"},
52-
{"presto.default.$operator$subtract", "presto.default.minus"},
53-
{"presto.default.$operator$subscript", "presto.default.subscript"},
55+
util::addDefaultNamespacePrefix(
56+
prestoDefaultNamespacePrefix, "distinct_from")},
57+
{"presto.default.$operator$less_than",
58+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "lt")},
59+
{"presto.default.$operator$less_than_or_equal",
60+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "lte")},
61+
{"presto.default.$operator$modulus",
62+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "mod")},
63+
{"presto.default.$operator$multiply",
64+
util::addDefaultNamespacePrefix(
65+
prestoDefaultNamespacePrefix, "multiply")},
66+
{"presto.default.$operator$negation",
67+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "negate")},
68+
{"presto.default.$operator$not_equal",
69+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "neq")},
70+
{"presto.default.$operator$subtract",
71+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "minus")},
72+
{"presto.default.$operator$subscript",
73+
util::addDefaultNamespacePrefix(
74+
prestoDefaultNamespacePrefix, "subscript")},
5475
// Special form function overrides.
5576
{"presto.default.in", "in"},
5677
};
@@ -66,11 +87,15 @@ std::string mapScalarFunction(const std::string& name) {
6687
}
6788

6889
std::string mapAggregateOrWindowFunction(const std::string& name) {
90+
static const std::string prestoDefaultNamespacePrefix =
91+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
6992
static const std::unordered_map<std::string, std::string> kFunctionNames = {
7093
{"presto.default.$internal$max_data_size_for_stats",
71-
"presto.default.max_data_size_for_stats"},
94+
util::addDefaultNamespacePrefix(
95+
prestoDefaultNamespacePrefix, "max_data_size_for_stats")},
7296
{"presto.default.$internal$sum_data_size_for_stats",
73-
"presto.default.sum_data_size_for_stats"},
97+
util::addDefaultNamespacePrefix(
98+
prestoDefaultNamespacePrefix, "sum_data_size_for_stats")},
7499
};
75100
std::string lowerCaseName = boost::to_lower_copy(name);
76101
auto it = kFunctionNames.find(name);
@@ -167,6 +192,8 @@ std::optional<TypedExprPtr> convertCastToVarcharWithMaxLength(
167192
const std::string& returnType,
168193
const std::vector<TypedExprPtr>& args,
169194
bool nullOnFailure) {
195+
static const std::string prestoDefaultNamespacePrefix =
196+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
170197
if (nullOnFailure) {
171198
VELOX_NYI("TRY_CAST of varchar to {} is not supported.", returnType);
172199
}
@@ -195,7 +222,7 @@ std::optional<TypedExprPtr> convertCastToVarcharWithMaxLength(
195222
std::make_shared<ConstantTypedExpr>(velox::BIGINT(), 1LL),
196223
std::make_shared<ConstantTypedExpr>(velox::BIGINT(), (int64_t)length),
197224
},
198-
"presto.default.substr");
225+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "substr"));
199226
}
200227

201228
/// Converts cast and try_cast functions to CastTypedExpr with nullOnFailure
@@ -212,6 +239,8 @@ std::optional<TypedExprPtr> tryConvertCast(
212239
const std::string& returnType,
213240
const std::vector<TypedExprPtr>& args,
214241
const TypeParser* typeParser) {
242+
static const std::string prestoDefaultNamespacePrefix =
243+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
215244
static const char* kCast = "presto.default.$operator$cast";
216245
static const char* kTryCast = "presto.default.try_cast";
217246
static const char* kJsonToArrayCast =
@@ -242,7 +271,10 @@ std::optional<TypedExprPtr> tryConvertCast(
242271
return std::make_shared<CastTypedExpr>(
243272
type,
244273
std::vector<TypedExprPtr>{std::make_shared<CallTypedExpr>(
245-
velox::JSON(), args, "presto.default.json_parse")},
274+
velox::JSON(),
275+
args,
276+
util::addDefaultNamespacePrefix(
277+
prestoDefaultNamespacePrefix, "json_parse"))},
246278
false);
247279
} else {
248280
return std::nullopt;
@@ -279,7 +311,6 @@ std::optional<TypedExprPtr> tryConvertTry(
279311
const std::vector<TypedExprPtr>& args,
280312
const TypeParser* typeParser) {
281313
static const char* kTry = "presto.default.$internal$try";
282-
283314
if (signature.kind != protocol::FunctionKind::SCALAR) {
284315
return std::nullopt;
285316
}
@@ -305,8 +336,11 @@ std::optional<TypedExprPtr> tryConvertLiteralArray(
305336
const std::vector<TypedExprPtr>& args,
306337
velox::memory::MemoryPool* pool,
307338
const TypeParser* typeParser) {
339+
static const std::string prestoDefaultNamespacePrefix =
340+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
308341
static const char* kLiteralArray = "presto.default.$literal$array";
309-
static const char* kFromBase64 = "presto.default.from_base64";
342+
static const std::string kFromBase64 = util::addDefaultNamespacePrefix(
343+
prestoDefaultNamespacePrefix, "from_base64");
310344

311345
if (signature.kind != protocol::FunctionKind::SCALAR) {
312346
return std::nullopt;
@@ -347,7 +381,10 @@ std::optional<TypedExprPtr> tryConvertLiteralArray(
347381

348382
std::optional<TypedExprPtr> VeloxExprConverter::tryConvertDate(
349383
const protocol::CallExpression& pexpr) const {
350-
static const char* kDate = "presto.default.date";
384+
static const std::string prestoDefaultNamespacePrefix =
385+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
386+
static const std::string kDate =
387+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "date");
351388

352389
auto builtin = std::static_pointer_cast<protocol::BuiltInFunctionHandle>(
353390
pexpr.functionHandle);
@@ -368,8 +405,12 @@ std::optional<TypedExprPtr> VeloxExprConverter::tryConvertDate(
368405

369406
std::optional<TypedExprPtr> VeloxExprConverter::tryConvertLike(
370407
const protocol::CallExpression& pexpr) const {
371-
static const char* kLike = "presto.default.like";
372-
static const char* kLikePatternType = "presto.default.like_pattern";
408+
static const std::string prestoDefaultNamespacePrefix =
409+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
410+
static const std::string kLike =
411+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "like");
412+
static const std::string kLikePatternType = util::addDefaultNamespacePrefix(
413+
prestoDefaultNamespacePrefix, "like_pattern");
373414
static const char* kLikeReturnType = "LikePattern";
374415
static const char* kCast = "presto.default.$operator$cast";
375416

@@ -505,9 +546,13 @@ bool isTrueConstant(const TypedExprPtr& expression) {
505546
std::shared_ptr<const CallTypedExpr> makeEqualsExpr(
506547
const TypedExprPtr& a,
507548
const TypedExprPtr& b) {
549+
static const std::string prestoDefaultNamespacePrefix =
550+
SystemConfig::instance()->prestoDefaultNamespacePrefix();
508551
std::vector<TypedExprPtr> inputs{a, b};
509552
return std::make_shared<CallTypedExpr>(
510-
velox::BOOLEAN(), std::move(inputs), "presto.default.eq");
553+
velox::BOOLEAN(),
554+
std::move(inputs),
555+
util::addDefaultNamespacePrefix(prestoDefaultNamespacePrefix, "eq"));
511556
}
512557

513558
std::shared_ptr<const CastTypedExpr> makeCastExpr(

0 commit comments

Comments
 (0)