-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expressions: Add ExprMacros. #4365
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. Metamarkets licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| package io.druid.math.expr; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ExprMacroTable | ||
| { | ||
| private static final ExprMacroTable NIL = new ExprMacroTable(Collections.emptyList()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why "nil"? Looks more like "empty"
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I named it nil similar to how the empty list is named nil in Lisp and in Scala, in honor of the macro systems in those languages. |
||
|
|
||
| private final Map<String, ExprMacro> macroMap; | ||
|
|
||
| public ExprMacroTable(final List<ExprMacro> macros) | ||
| { | ||
| this.macroMap = macros.stream().collect( | ||
| Collectors.toMap( | ||
| m -> m.name().toLowerCase(), | ||
| m -> m | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public static ExprMacroTable nil() | ||
| { | ||
| return NIL; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an expr corresponding to a function call if this table has an entry for {@code functionName}. | ||
| * Otherwise, returns null. | ||
| * | ||
| * @param functionName function name | ||
| * @param args function arguments | ||
| * | ||
| * @return expr for this function call, or null | ||
| */ | ||
| @Nullable | ||
| public Expr get(final String functionName, final List<Expr> args) | ||
| { | ||
| final ExprMacro exprMacro = macroMap.get(functionName.toLowerCase()); | ||
| if (exprMacro == null) { | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why silently consume the situation when the function is not found? Maybe throw exception?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In ExprListenerImpl, the macro table is checked first, then built-in functions. null return makes this pattern simpler; if this threw an exception we'd have to catch it and ignore it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I annotated this with |
||
| } | ||
|
|
||
| return exprMacro.apply(args); | ||
| } | ||
|
|
||
| public interface ExprMacro | ||
| { | ||
| String name(); | ||
|
|
||
| Expr apply(final List<Expr> args); | ||
| } | ||
| } | ||
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.
How about adding
LiteralExprextendingExprand having this method? If so, other types of exprs don't have to have this method.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.
I wanted to avoid having
instanceofchecks and casts all over the place.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.
Ok.