forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Implement CLP type parser to support special chars besides _ in field name (addresses #13).
#31
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
Draft
anlowee
wants to merge
5
commits into
y-scope:presto-0.293-clp-connector
Choose a base branch
from
anlowee:xwei/handle-special-chars
base: presto-0.293-clp-connector
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: Implement CLP type parser to support special chars besides _ in field name (addresses #13).
#31
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # 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. | ||
|
|
||
| if(${VELOX_ENABLE_CLP_CONNECTOR}) | ||
| if(${VELOX_BUILD_TESTING}) | ||
| add_subdirectory(tests) | ||
| endif() | ||
|
|
||
| bison_target( | ||
| ClpTypeParser ClpTypeParser.yy | ||
| ${CMAKE_CURRENT_BINARY_DIR}/ClpTypeParser.yy.cc | ||
| DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClpTypeParser.yy.h | ||
| COMPILE_FLAGS "-Werror -Wno-deprecated") | ||
|
|
||
| flex_target( | ||
| ClpTypeParserScanner ClpTypeParser.ll | ||
| ${CMAKE_CURRENT_BINARY_DIR}/ClpScanner.cpp | ||
| COMPILE_FLAGS "-Cf --prefix=veloxtpclp") | ||
|
|
||
| add_flex_bison_dependency(ClpTypeParserScanner ClpTypeParser) | ||
|
|
||
| if(VELOX_MONO_LIBRARY) | ||
| add_custom_target( | ||
| velox_type_fbclp_parser_gen_src | ||
| DEPENDS ${BISON_ClpTypeParser_OUTPUTS} | ||
| ${FLEX_ClpTypeParserScanner_OUTPUTS}) | ||
| add_dependencies(velox velox_type_fbclp_parser_gen_src) | ||
| endif() | ||
| velox_add_library(velox_type_fbclp_parser ${BISON_ClpTypeParser_OUTPUTS} | ||
| ${FLEX_ClpTypeParserScanner_OUTPUTS} ClpParserUtil.cpp) | ||
| velox_include_directories(velox_type_fbclp_parser | ||
| PRIVATE ${PROJECT_BINARY_DIR} ${FLEX_INCLUDE_DIRS}) | ||
| velox_link_libraries(velox_type_fbclp_parser velox_common_base) | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| endif() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * 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 <string> | ||
| #include "velox/type/Type.h" | ||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| namespace facebook::velox::type::fbclp { | ||
|
|
||
| TypePtr typeFromString( | ||
| const std::string& type, | ||
| bool failIfNotRegistered = true) { | ||
| auto upper = type; | ||
| std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper); | ||
| if (upper == "INT") { | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| upper = "INTEGER"; | ||
| } else if (upper == "DOUBLE PRECISION") { | ||
| upper = "DOUBLE"; | ||
| } | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto inferredType = getType(upper, {}); | ||
| if (failIfNotRegistered == true && inferredType == nullptr) { | ||
| VELOX_UNSUPPORTED("Failed to parse type [{}]. Type not registered.", type); | ||
| } | ||
| return inferredType; | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| TypePtr customTypeWithChildren( | ||
| const std::string& name, | ||
| const std::vector<TypePtr>& children) { | ||
| std::vector<TypeParameter> params; | ||
| params.reserve(children.size()); | ||
| for (auto& child : children) { | ||
| params.emplace_back(child); | ||
| } | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto type = getType(name, params); | ||
| VELOX_CHECK_NOT_NULL( | ||
| type, "Failed to parse custom type with children [{}]", name); | ||
| return type; | ||
| } | ||
|
|
||
| std::pair<std::string, std::shared_ptr<const Type>> inferTypeWithSpaces( | ||
| std::vector<std::string>& words, | ||
| bool cannotHaveFieldName = false) { | ||
| VELOX_CHECK_GE(words.size(), 2); | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const auto& fieldName = words[0]; | ||
| const auto allWords = folly::join(" ", words); | ||
| // Fail if cannotHaveFieldName = true. | ||
| auto type = typeFromString(allWords, cannotHaveFieldName); | ||
| if (type) { | ||
| return std::make_pair("", type); | ||
| } | ||
| return std::make_pair( | ||
| fieldName, typeFromString(allWords.substr(fieldName.size() + 1))); | ||
| } | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace facebook::velox::type::fbclp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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 <string> | ||
| #include "velox/type/Type.h" | ||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| namespace facebook::velox::type::fbclp { | ||
|
|
||
| /// Normalize Presto types such as INT and DOUBLE PRECISION and convert to Velox | ||
| /// type. | ||
| TypePtr typeFromString( | ||
| const std::string& type, | ||
| bool failIfNotRegistered = true); | ||
|
|
||
| TypePtr customTypeWithChildren( | ||
| const std::string& name, | ||
| const std::vector<TypePtr>& children); | ||
|
|
||
| /// Convert words with spaces to a Velox type. | ||
| /// First check if all the words are a Velox type. | ||
| /// Then check if the first word is a field name and the remaining words are a | ||
| /// Velox type. If cannotHaveFieldName = true, then all words must be a Velox | ||
| /// type. | ||
| std::pair<std::string, TypePtr> inferTypeWithSpaces( | ||
| std::vector<std::string>& words, | ||
| bool cannotHaveFieldName = false); | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace facebook::velox::type::fbclp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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 <cmath> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "velox/common/base/Exceptions.h" | ||
| #include "velox/type/Type.h" | ||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| namespace facebook::velox::type::fbclp { | ||
|
|
||
| class ClpScanner : public yyFlexLexer { | ||
| public: | ||
| ClpScanner( | ||
| std::istream& arg_yyin, | ||
| std::ostream& arg_yyout, | ||
| TypePtr& outputType, | ||
| const std::string_view input) | ||
| : yyFlexLexer(&arg_yyin, &arg_yyout), | ||
| outputType_(outputType), | ||
| input_(input){}; | ||
| int lex(ClpParser::semantic_type* yylval); | ||
|
|
||
| void setType(TypePtr type) { | ||
| outputType_ = std::move(type); | ||
| } | ||
|
|
||
| // Store input to print it as part of the error message. | ||
| std::string_view input() { | ||
| return input_; | ||
| } | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private: | ||
| TypePtr& outputType_; | ||
| const std::string_view input_; | ||
| }; | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace facebook::velox::type::fbclp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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 <string> | ||
| #include "velox/type/Type.h" | ||
|
|
||
| namespace facebook::velox::type::fbclp { | ||
|
|
||
| /// Parses a type string in Presto format to Velox type. | ||
| /// Example type strings: | ||
| /// row(col0 bigint, varchar) | ||
| /// array(bigint) | ||
| /// map(bigint, array(bigint)) | ||
| /// function(bigint,bigint,bigint) | ||
| /// The parsing is case-insensitive. i.e. 'Row' and 'row' are equal. | ||
| /// Field names for rows are optional. | ||
| /// Quoted field names are supported. | ||
| /// All custom types need to be registered. An error is thrown otherwise. | ||
| /// Uses the Type::getType API to convert a string to Velox type. | ||
| /// This CLP variant additionally supports unquoted field names containing | ||
| /// '-', '@', '#', '$', and '\\'. | ||
| TypePtr parseClpType(const std::string& typeText); | ||
|
|
||
| } // namespace facebook::velox::type::fbclp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| %{ | ||
| #include <vector> | ||
| #include <memory> | ||
|
|
||
| #include "velox/type/fbclp/ClpTypeParser.yy.h" // @manual | ||
| #include "velox/type/fbclp/ClpScanner.h" | ||
| #define YY_DECL int facebook::velox::type::fbclp::ClpScanner::lex(facebook::velox::type::fbclp::ClpParser::semantic_type *yylval) | ||
| %} | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| %option c++ noyywrap noyylineno nodefault caseless | ||
|
|
||
| A [A|a] | ||
| B [B|b] | ||
| C [C|c] | ||
| D [D|d] | ||
| E [E|e] | ||
| F [F|f] | ||
| G [G|g] | ||
| H [H|h] | ||
| I [I|i] | ||
| J [J|j] | ||
| K [K|k] | ||
| L [L|l] | ||
| M [M|m] | ||
| O [O|o] | ||
| P [P|p] | ||
| R [R|r] | ||
| S [S|s] | ||
| T [T|t] | ||
| U [U|u] | ||
| W [W|w] | ||
| X [X|x] | ||
| Y [Y|y] | ||
| Z [Z|z] | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| WORD ([[:alpha:][:alnum:]_\-@#\$\\]*) | ||
| QUOTED_ID (['"'][[:alnum:][:space:]_]*['"']) | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| NUMBER ([[:digit:]]+) | ||
| VARIABLE (VARCHAR|VARBINARY) | ||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| %% | ||
|
|
||
| "(" return ClpParser::token::LPAREN; | ||
| ")" return ClpParser::token::RPAREN; | ||
| "," return ClpParser::token::COMMA; | ||
| (ARRAY) return ClpParser::token::ARRAY; | ||
| (MAP) return ClpParser::token::MAP; | ||
| (FUNCTION) return ClpParser::token::FUNCTION; | ||
| (DECIMAL) return ClpParser::token::DECIMAL; | ||
| (ROW) return ClpParser::token::ROW; | ||
| {VARIABLE} yylval->build<std::string>(YYText()); return ClpParser::token::VARIABLE; | ||
| {NUMBER} yylval->build<long long>(folly::to<int>(YYText())); return ClpParser::token::NUMBER; | ||
| {WORD} yylval->build<std::string>(YYText()); return ClpParser::token::WORD; | ||
| {QUOTED_ID} yylval->build<std::string>(YYText()); return ClpParser::token::QUOTED_ID; | ||
| <<EOF>> return ClpParser::token::YYEOF; | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| . /* no action on unmatched input */ | ||
|
|
||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| %% | ||
|
|
||
| int yyFlexLexer::yylex() { | ||
| throw std::runtime_error("Bad call to yyFlexLexer::yylex()"); | ||
| } | ||
|
|
||
| #include "velox/type/fbclp/ClpTypeParser.h" | ||
|
|
||
| facebook::velox::TypePtr facebook::velox::type::fbclp::parseClpType(const std::string& typeText) | ||
| { | ||
| std::istringstream is(typeText); | ||
| std::ostringstream os; | ||
| facebook::velox::TypePtr type; | ||
| facebook::velox::type::fbclp::ClpScanner scanner{is, os, type, typeText}; | ||
| facebook::velox::type::fbclp::ClpParser parser{ &scanner }; | ||
| parser.parse(); | ||
| VELOX_CHECK(type, "Failed to parse type [{}]", typeText); | ||
| return type; | ||
| } | ||
anlowee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.