Skip to content
Closed
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
3 changes: 2 additions & 1 deletion velox/exec/fuzzer/PrestoQueryRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "velox/functions/prestosql/types/IPPrefixType.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/KHyperLogLogType.h"
#include "velox/functions/prestosql/types/PrestoTypeSql.h"
#include "velox/functions/prestosql/types/QDigestType.h"
#include "velox/functions/prestosql/types/SetDigestType.h"
#include "velox/functions/prestosql/types/SfmSketchType.h"
Expand Down Expand Up @@ -396,7 +397,7 @@ std::string PrestoQueryRunner::createTable(
for (auto i = 0; i < inputType->size(); ++i) {
appendComma(i, nullValues);
nullValues << fmt::format(
"cast(null as {})", toTypeSql(inputType->childAt(i)));
"cast(null as {})", toPrestoTypeSql(inputType->childAt(i)));
}

execute(fmt::format("DROP TABLE IF EXISTS {}", name));
Expand Down
39 changes: 6 additions & 33 deletions velox/exec/fuzzer/PrestoSql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "velox/exec/fuzzer/PrestoQueryRunner.h"
#include "velox/exec/fuzzer/ReferenceQueryRunner.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/PrestoTypeSql.h"
#include "velox/vector/SimpleVector.h"

namespace facebook::velox::exec::test {
Expand Down Expand Up @@ -71,36 +72,6 @@ void appendComma(int32_t i, std::stringstream& sql) {
}
}

// Returns the SQL string of the given type.
std::string toTypeSql(const TypePtr& type) {
switch (type->kind()) {
case TypeKind::ARRAY:
return fmt::format("ARRAY({})", toTypeSql(type->childAt(0)));
case TypeKind::MAP:
return fmt::format(
"MAP({}, {})",
toTypeSql(type->childAt(0)),
toTypeSql(type->childAt(1)));
case TypeKind::ROW: {
const auto& rowType = type->asRow();
std::stringstream sql;
sql << "ROW(";
for (auto i = 0; i < type->size(); ++i) {
appendComma(i, sql);
// TODO Field names may need to be quoted.
sql << rowType.nameOf(i) << " " << toTypeSql(type->childAt(i));
}
sql << ")";
return sql.str();
}
default:
if (type->isPrimitiveType()) {
return type->toString();
}
VELOX_UNSUPPORTED("Type is not supported: {}", type->toString());
}
}

std::string toLambdaSql(const core::LambdaTypedExprPtr& lambda) {
std::stringstream sql;
const auto& signature = lambda->signature();
Expand Down Expand Up @@ -346,7 +317,7 @@ std::string toCastSql(const core::CastTypedExpr& cast) {
sql << "cast(";
}
toCallInputsSql(cast.inputs(), sql);
sql << " as " << toTypeSql(cast.type());
sql << " as " << facebook::velox::toPrestoTypeSql(cast.type());
sql << ")";
return sql.str();
}
Expand All @@ -355,7 +326,9 @@ std::string toConcatSql(const core::ConcatTypedExpr& concat) {
std::stringstream input;
toCallInputsSql(concat.inputs(), input);
return fmt::format(
"cast(row({}) as {})", input.str(), toTypeSql(concat.type()));
"cast(row({}) as {})",
input.str(),
facebook::velox::toPrestoTypeSql(concat.type()));
}

template <typename T>
Expand All @@ -381,7 +354,7 @@ std::string getConstantValue(const core::ConstantTypedExpr& expr) {

std::string toConstantSql(const core::ConstantTypedExpr& constant) {
const auto& type = constant.type();
const auto typeSql = toTypeSql(type);
const auto typeSql = facebook::velox::toPrestoTypeSql(type);

std::stringstream sql;
if (constant.isNull()) {
Expand Down
3 changes: 0 additions & 3 deletions velox/exec/fuzzer/PrestoSql.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ namespace facebook::velox::exec::test {
/// than 0.
void appendComma(int32_t i, std::stringstream& sql);

/// Return the SQL string of type.
std::string toTypeSql(const TypePtr& type);

/// Converts input expressions into SQL string and appends to a given
/// stringstream.
void toCallInputsSql(
Expand Down
41 changes: 0 additions & 41 deletions velox/exec/fuzzer/tests/PrestoSqlTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,10 @@

#include "velox/common/base/tests/GTestUtils.h"
#include "velox/exec/fuzzer/PrestoSql.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/KHyperLogLogType.h"
#include "velox/functions/prestosql/types/QDigestType.h"
#include "velox/functions/prestosql/types/SetDigestType.h"
#include "velox/functions/prestosql/types/TDigestType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"

namespace facebook::velox::exec::test {
namespace {

TEST(PrestoSqlTest, toTypeSql) {
EXPECT_EQ(toTypeSql(BOOLEAN()), "BOOLEAN");
EXPECT_EQ(toTypeSql(TINYINT()), "TINYINT");
EXPECT_EQ(toTypeSql(SMALLINT()), "SMALLINT");
EXPECT_EQ(toTypeSql(INTEGER()), "INTEGER");
EXPECT_EQ(toTypeSql(BIGINT()), "BIGINT");
EXPECT_EQ(toTypeSql(REAL()), "REAL");
EXPECT_EQ(toTypeSql(DOUBLE()), "DOUBLE");
EXPECT_EQ(toTypeSql(VARCHAR()), "VARCHAR");
EXPECT_EQ(toTypeSql(VARBINARY()), "VARBINARY");
EXPECT_EQ(toTypeSql(TDIGEST(DOUBLE())), "TDIGEST(DOUBLE)");
EXPECT_EQ(toTypeSql(TIMESTAMP()), "TIMESTAMP");
EXPECT_EQ(toTypeSql(QDIGEST(DOUBLE())), "QDIGEST(DOUBLE)");
EXPECT_EQ(toTypeSql(QDIGEST(BIGINT())), "QDIGEST(BIGINT)");
EXPECT_EQ(toTypeSql(QDIGEST(REAL())), "QDIGEST(REAL)");
EXPECT_EQ(toTypeSql(SETDIGEST()), "SETDIGEST");
EXPECT_EQ(toTypeSql(KHYPERLOGLOG()), "KHYPERLOGLOG");
EXPECT_EQ(toTypeSql(DATE()), "DATE");
EXPECT_EQ(toTypeSql(TIMESTAMP_WITH_TIME_ZONE()), "TIMESTAMP WITH TIME ZONE");
EXPECT_EQ(toTypeSql(ARRAY(BOOLEAN())), "ARRAY(BOOLEAN)");
EXPECT_EQ(toTypeSql(MAP(BOOLEAN(), INTEGER())), "MAP(BOOLEAN, INTEGER)");
EXPECT_EQ(
toTypeSql(ROW({{"a", BOOLEAN()}, {"b", INTEGER()}})),
"ROW(a BOOLEAN, b INTEGER)");
EXPECT_EQ(
toTypeSql(
ROW({{"a_", BOOLEAN()}, {"b$", INTEGER()}, {"c d", INTEGER()}})),
"ROW(a_ BOOLEAN, b$ INTEGER, c d INTEGER)");
EXPECT_EQ(toTypeSql(JSON()), "JSON");
EXPECT_EQ(toTypeSql(UNKNOWN()), "UNKNOWN");
VELOX_ASSERT_THROW(
toTypeSql(FUNCTION({INTEGER()}, INTEGER())),
"Type is not supported: FUNCTION");
}

void toUnaryOperator(
const std::string& operatorName,
const std::string& expectedSql) {
Expand Down
2 changes: 2 additions & 0 deletions velox/functions/prestosql/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
# limitations under the License.

add_subdirectory(parser)

velox_add_library(
velox_presto_types
BigintEnumRegistration.cpp
BigintEnumType.cpp
PrestoTypeSql.cpp
BingTileRegistration.cpp
BingTileType.cpp
EnumTypeBase.cpp
Expand Down
124 changes: 124 additions & 0 deletions velox/functions/prestosql/types/PrestoTypeSql.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/

#include "velox/functions/prestosql/types/PrestoTypeSql.h"

#include <fmt/format.h>
#include <velox/common/base/Exceptions.h>
#include <iomanip>
#include <sstream>

namespace facebook::velox {

namespace {
bool isPlainArray(const Type& type) {
return type.isArray() && typeid(type) == typeid(ArrayType);
}

bool isPlainMap(const Type& type) {
return type.isMap() && typeid(type) == typeid(MapType);
}

bool isPlainRow(const Type& type) {
return type.isRow() && typeid(type) == typeid(RowType);
}

// Returns true if a ROW field name needs quoting in Presto SQL.
// Field names that are not simple identifiers (letters, digits, underscores
// starting with a letter or underscore) must be quoted.
bool needsQuoting(const std::string& name) {
if (name.empty()) {
return false;
}
if (name[0] != '_' && !std::isalpha(name[0])) {
return true;
}
for (auto c : name) {
if (c != '_' && !std::isalnum(c)) {
return true;
}
}
return false;
}

} // namespace

std::string toPrestoTypeSql(const TypePtr& type) {
if (isPlainArray(*type)) {
return fmt::format("ARRAY({})", toPrestoTypeSql(type->childAt(0)));
}

if (isPlainMap(*type)) {
return fmt::format(
"MAP({}, {})",
toPrestoTypeSql(type->childAt(0)),
toPrestoTypeSql(type->childAt(1)));
}

if (isPlainRow(*type)) {
const auto& rowType = type->asRow();
std::stringstream sql;
sql << "ROW(";
for (auto i = 0; i < type->size(); ++i) {
if (i > 0) {
sql << ", ";
}
const auto& name = rowType.nameOf(i);
if (!name.empty()) {
if (needsQuoting(name)) {
sql << std::quoted(name, '"', '"') << " ";
} else {
sql << name << " ";
}
}
sql << toPrestoTypeSql(type->childAt(i));
}
sql << ")";
return sql.str();
}

// Primitive types and custom types (e.g. IPPREFIX, BINGTILE).
// For parameterized types (e.g. TDIGEST(DOUBLE), DECIMAL(10, 2)),
// append parameters.
const auto& params = type->parameters();
if (params.empty()) {
return type->name();
}

std::stringstream sql;
sql << type->name() << "(";
for (auto i = 0; i < params.size(); ++i) {
if (i > 0) {
sql << ", ";
}
switch (params[i].kind) {
case TypeParameterKind::kType:
sql << toPrestoTypeSql(params[i].type);
break;
case TypeParameterKind::kLongLiteral:
sql << params[i].longLiteral.value();
break;
default:
VELOX_UNSUPPORTED(
"Unsupported type parameter kind: {}",
TypeParameterKindName::toName(params[i].kind));
}
}
sql << ")";
return sql.str();
}

} // namespace facebook::velox
25 changes: 25 additions & 0 deletions velox/functions/prestosql/types/PrestoTypeSql.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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

#include "velox/type/Type.h"

namespace facebook::velox {

/// Returns the Presto SQL string representation of the given type.
std::string toPrestoTypeSql(const TypePtr& type);

} // namespace facebook::velox
1 change: 1 addition & 0 deletions velox/functions/prestosql/types/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_executable(
UuidTypeTest.cpp
IPAddressTypeTest.cpp
IPPrefixTypeTest.cpp
PrestoTypeSqlTest.cpp
)

if(VELOX_ENABLE_GEO)
Expand Down
Loading
Loading