Skip to content

Commit

Permalink
[native] Add support for parsing FUNCTION types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasmanova committed Oct 12, 2023
1 parent c7818f1 commit da7b0f2
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 166 deletions.
10 changes: 7 additions & 3 deletions presto-native-execution/presto_cpp/main/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ Details on how to create a lexer and parser using antlr4 are available here:
https://github.com/antlr/antlr4/blob/master/doc/cpp-target.md

**TypeSignature.g4** file specifies the grammar. Any new rules must be added to this file.
presto_cpp requires the antlr4 **visitor** and does not need the antlr4 **listener**.
antlr files are generated using the following command:
presto_cpp requires the antlr4 **visitor** and does not need the antlr4 **listener**.

antlr4 -Dlanguage=Cpp -no-listener -visitor TypeSignature.g4 -o antlr
To generate antlr files:

+ Download https://repo1.maven.org/maven2/org/antlr/antlr4/4.9.3/antlr4-4.9.3-complete.jar
+ Run

java -jar <path/to>/antlr4-4.9.3-complete.jar -Dlanguage=Cpp -package facebook::presto::type -visitor -no-listener -o antlr TypeSignature.g4

The following files are generated inside the antlr directory:
+ TypeSignature.interp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ grammar TypeSignature;
#define isArrayToken() (UpCase(Token()) == "ARRAY")
#define isVarToken() (UpCase(Token()) == "VARCHAR" || UpCase(Token()) == "VARBINARY")
#define isDecimalToken() (UpCase(Token()) == "DECIMAL")
#define isFunctionToken() (UpCase(Token()) == "FUNCTION")
}

start : type_spec EOF ;
Expand All @@ -33,6 +34,7 @@ type :
| array_type
| map_type
| row_type
| function_type
;

simple_type :
Expand Down Expand Up @@ -61,6 +63,9 @@ map_type :
array_type :
{ isArrayToken() }? WORD '(' type ')' ;

function_type :
{ isFunctionToken() }? WORD '(' type (',' type)* ')' ;

identifier :
QUOTED_ID
| WORD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ antlrcpp::Any TypeSignatureTypeConverter::visitArray_type(
return arrayFromType(visit(ctx->type()).as<TypePtr>());
}

antlrcpp::Any TypeSignatureTypeConverter::visitFunction_type(
TypeSignatureParser::Function_typeContext* ctx) {
const auto numArgs = ctx->type().size() - 1;

std::vector<TypePtr> argumentTypes;
argumentTypes.reserve(numArgs);
for (auto i = 0; i < numArgs; ++i) {
argumentTypes.push_back(visit(ctx->type()[i]).as<TypePtr>());
}

auto returnType = visit(ctx->type().back()).as<TypePtr>();

TypePtr functionType = FUNCTION(std::move(argumentTypes), returnType);
return functionType;
}

antlrcpp::Any TypeSignatureTypeConverter::visitIdentifier(
TypeSignatureParser::IdentifierContext* ctx) {
if (ctx->WORD()) {
Expand Down Expand Up @@ -193,4 +209,5 @@ TypePtr mapFromKeyValueType(TypePtr keyType, TypePtr valueType) {
TypePtr arrayFromType(TypePtr valueType) {
return TypeFactory<TypeKind::ARRAY>::create(valueType);
}

} // namespace facebook::presto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class TypeSignatureTypeConverter : TypeSignatureBaseVisitor {
TypeSignatureParser::Map_typeContext* ctx) override;
virtual antlrcpp::Any visitArray_type(
TypeSignatureParser::Array_typeContext* ctx) override;
virtual antlrcpp::Any visitFunction_type(
TypeSignatureParser::Function_typeContext* ctx) override;
virtual antlrcpp::Any visitIdentifier(
TypeSignatureParser::IdentifierContext* ctx) override;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit da7b0f2

Please sign in to comment.