Skip to content
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

support hive create function #500

Merged
merged 3 commits into from
Jun 15, 2024
Merged
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
1 change: 1 addition & 0 deletions sql_metadata/keywords_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class TokenType(str, Enum):
"CREATETABLE": QueryType.CREATE,
"ALTERTABLE": QueryType.ALTER,
"DROPTABLE": QueryType.DROP,
"CREATEFUNCTION": QueryType.CREATE,
}

# all the keywords we care for - rest is ignored in assigning
Expand Down
20 changes: 19 additions & 1 deletion sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def query_type(self) -> str:
)
.position
)
if tokens[index].normalized in ["CREATE", "ALTER", "DROP"]:
if tokens[index].normalized == "CREATE":
switch = self._get_switch_by_create_query(tokens, index)
elif tokens[index].normalized in ("ALTER", "DROP"):
switch = tokens[index].normalized + tokens[index + 1].normalized
else:
switch = tokens[index].normalized
Expand Down Expand Up @@ -1079,3 +1081,19 @@ def _flatten_sqlparse(self):
yield tok
else:
yield token

@staticmethod
def _get_switch_by_create_query(tokens: List[SQLToken], index: int) -> str:
"""
Return the switch that creates query type.
"""
switch = tokens[index].normalized + tokens[index + 1].normalized

# Hive CREATE FUNCTION
if any(
index + i < len(tokens) and tokens[index + i].normalized == "FUNCTION"
for i in (1, 2)
):
switch = "CREATEFUNCTION"

return switch
19 changes: 19 additions & 0 deletions test/test_query_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,22 @@ def test_multiple_redundant_parentheses_create():
"""
parser = Parser(query)
assert parser.query_type == QueryType.CREATE


def test_hive_create_function():
query = """
CREATE FUNCTION simple_udf AS 'com.example.hive.udf.SimpleUDF'
USING JAR 'hdfs:///user/hive/udfs/simple-udf.jar'
WITH SERDEPROPERTIES (
"hive.udf.param1"="value1",
"hive.udf.param2"="value2"
);
"""
parser = Parser(query)
assert parser.query_type == QueryType.CREATE

query = """
CREATE TEMPORARY FUNCTION myudf AS 'com.udf.myudf';
"""
parser = Parser(query)
assert parser.query_type == QueryType.CREATE
Comment on lines +98 to +114
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks!