-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Enums support #1: base types and operators #14728
Conversation
7115366
to
29d596c
Compare
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/VarcharEnumType.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/VarcharType.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/metadata/BuiltInFunctionNamespaceManager.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/metadata/FunctionManager.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/metadata/MetadataManager.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/metadata/BuiltInFunctionNamespaceManager.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/EnumOperators.java
Outdated
Show resolved
Hide resolved
I'm not sure we should allow literals. They should only be a cast like: MOOD('Happy'). We don't want to create C++-like syntax where you need to know if something is a type before parsing. This is a going to create a lot of hassles and bug-prone. |
@kaikalur are you suggesting that we introduce a UDF with the same name as each enum type that takes in a varchar key and returns the matching enum object?
Can you elaborate on this? How is that syntax different from what we already support with eg |
DATE literal is actually a thing in the spec. And DATE is a keyword whereas here you will have to allow any enum type to precede the literal which is not very spec-like and not context-free grammar anymore. |
I'm not too attached to any particular syntax, but the generic type literal is already there in the Presto parser, I didn't change that: We already have the behavior of parsing type literals in a generic way when we encounter them and looking for a |
Wow! That's a bad idea. I haven't seen it before. Let me look.
Get Outlook for Android<https://aka.ms/ghei36>
…________________________________
From: Daniel Ohayon <notifications@github.com>
Sent: Friday, July 17, 2020 3:30:43 PM
To: prestodb/presto <presto@noreply.github.com>
Cc: Sreeni Viswanadha <viswanadha@fb.com>; Mention <mention@noreply.github.com>
Subject: Re: [prestodb/presto] Enums support #1: base types and operators (#14728)
I'm not too attached to any particular syntax, but the generic type literal is already there in the Presto parser, I didn't change that:
https://github.com/prestodb/presto/blob/master/presto-main/src/main/java/com/facebook/presto/sql/planner/LiteralInterpreter.java#L252
We already have the behavior of parsing type literals in a generic way when we encounter them and looking for a cast: varchar -> type so if you don't think that's a good idea, maybe that part should be removed from the parser too.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#14728 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAPNYAPOCGX7OQVSIALWSWTR4DGJHANCNFSM4OI4EEMQ>.
|
cf37c7d
to
cf63560
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add long and varchar enum types
looks good. Some nits only.
presto-common/src/main/java/com/facebook/presto/common/type/EnumTypeConstraint.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/LongEnumType.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/VarcharEnumType.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
11636ea
to
9833151
Compare
presto-common/src/main/java/com/facebook/presto/common/type/LongEnumType.java
Show resolved
Hide resolved
7da5d10
to
88a0f14
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to separate Add enum literals and operators
to two commits: Support enum literals
and Add enum operators
. They are not quite related.
presto-spi/src/main/java/com/facebook/presto/spi/function/TypeParameter.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionAnalyzer.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionAnalyzer.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Support type bound on @TypeParameter
, are you planning to make the signature look like <T extends SomeType>
? Please update the commit message to either use the exact term in type signature, or use generic description rather than the approximate Java syntax.
8faecd0
to
0b8e3e1
Compare
b56e3ef
to
d0e29b7
Compare
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Show resolved
Hide resolved
String enumKey = qualifiedName.getSuffix().toLowerCase(ENGLISH); | ||
Object enumValue = enumType.getEnumMap().get(enumKey); | ||
if (enumValue == null) { | ||
throw new SemanticException(INVALID_LITERAL, node, format("No key '%s' in enum '%s'", enumKey, enumType.getDisplayName())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is too low level to throw an SemanticException
. If you expect this function to be only called when node is a valid enum, it should throw IllegalArgumentException
. You can change these logic to a checkArgument
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it OK to have errors triggered by a checkArgument
surfaced to users?
This is the only place where we check whether the provided key exists in the enum, so it will be very possible that users see this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not OK, we should add a catch
at the correct level and translate the IllegalArgumentException
to a SemanticException
.
presto-main/src/main/java/com/facebook/presto/sql/analyzer/SemanticExceptions.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/TranslationMap.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/sanity/PlanChecker.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/sql/planner/TranslationMap.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/metadata/MetadataManager.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/util/JsonUtil.java
Outdated
Show resolved
Hide resolved
d0e29b7
to
555ed46
Compare
@rongrong could you have another look at |
ba08218
to
dfd4d95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The structure looks good overall. Feedback on some details. Please also add more tests about valid and invalid enum creation / serde.
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeUtils.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/VarcharEnumMap.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/VarcharEnumType.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/ParameterKind.java
Outdated
Show resolved
Hide resolved
dfd4d95
to
1c07c54
Compare
addressed latest feedback and added extra tests to |
90e139c
to
74e8a69
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First commit looks good. Only some nits.
presto-common/src/main/java/com/facebook/presto/common/type/TypeParameter.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeSignatureParameter.java
Outdated
Show resolved
Hide resolved
presto-common/src/main/java/com/facebook/presto/common/type/TypeSignatureParameter.java
Outdated
Show resolved
Hide resolved
presto-common/src/test/java/com/facebook/presto/common/type/TestTypeSignature.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/LongEnumParametricType.java
Outdated
Show resolved
Hide resolved
presto-main/src/main/java/com/facebook/presto/type/VarcharEnumParametricType.java
Outdated
Show resolved
Hide resolved
Add an is-a constraint for type variables, so we can describe generic types of a certain kind.
74e8a69
to
237f05b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support enum literals in queries
looks good. Only one nit.
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow this looks so clean now!
presto-main/src/main/java/com/facebook/presto/type/VarcharEnumOperators.java
Show resolved
Hide resolved
237f05b
to
252b098
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please fix the test failure and we are good to go!
presto-main/src/main/java/com/facebook/presto/sql/analyzer/ExpressionTreeUtils.java
Outdated
Show resolved
Hide resolved
Support common operators like `=` on enum types
252b098
to
4a60a66
Compare
This PR is the first in a series that aims to introduce support for user-defined types, and specifically user-defined enums, into Presto (see #14691 for an overview of the work).
This one focuses specifically on introducing the base types and generic operators that apply to enums.
We support
Mood.HAPPY
=
,!=
andIS_DISTINCT
IN (...)
andAPPROX_DISTINCT()
Implementation notes
Mood.HAPPY
are parsed as Dereferences in the AST (likemy_table.my_col
) and they are then rewritten as EnumLiterals in the TranslationMap's rewriter.<T extends LongEnumType> equals(T, T): bool
. This constraint is similar to theorderable
andcomparable
constraint we use to define the type signature of functions like the equal operator on arrays.In future PRs, I will introduce