Package org.apache.druid.math.expr
Interface Expr
-
- All Superinterfaces:
Cacheable
- All Known Subinterfaces:
ExprMacroTable.ExprMacroFunctionExpr
- All Known Implementing Classes:
ExprMacroTable.BaseMacroFunctionExpr
,ExprMacroTable.BaseScalarMacroFunctionExpr
,TimestampExtractExprMacro.TimestampExtractDynamicExpr
,TimestampExtractExprMacro.TimestampExtractExpr
,TimestampFloorExprMacro.TimestampFloorDynamicExpr
,TimestampFloorExprMacro.TimestampFloorExpr
@SubclassesMustOverrideEqualsAndHashCode public interface Expr extends Cacheable
Base interface of Druid expression language abstract syntax tree nodes. AllExpr
implementations are immutable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Expr.BindingAnalysis
Information about the context in whichIdentifierExpr
are used in a greaterExpr
, listing the 'free variables' (total set of required input columns or values) and distinguishing between which identifiers are used as scalar inputs and which are used as array inputs.static interface
Expr.InputBindingInspector
Mechanism to supply input types for the bindings which will backIdentifierExpr
, to use in the aid of inferring the output type of an expression withgetOutputType(org.apache.druid.math.expr.Expr.InputBindingInspector)
.static interface
Expr.ObjectBinding
Mechanism to supply values to backIdentifierExpr
during expression evaluationstatic interface
Expr.Shuttle
Mechanism to rewrite anExpr
, implementing aExpr.Shuttle
allows visiting all children of anExpr
, and replacing them as desired.static interface
Expr.VectorInputBinding
Mechanism to supply batches of input values to aExprVectorProcessor
for optimized processing.static interface
Expr.VectorInputBindingInspector
Expr.InputBindingInspector
+ vectorizations stuff forasVectorProcessor(org.apache.druid.math.expr.Expr.VectorInputBindingInspector)
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.base.Joiner
ARG_JOINER
static String
NULL_LITERAL
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Expr.BindingAnalysis
analyzeInputs()
default ColumnIndexSupplier
asColumnIndexSupplier(ColumnIndexSelector columnIndexSelector, ColumnType outputType)
default Expr
asSingleThreaded(Expr.InputBindingInspector inspector)
default <T> ExprVectorProcessor<T>
asVectorProcessor(Expr.VectorInputBindingInspector inspector)
Builds a 'vectorized' expression processor, that can operate on batches of input values for use in vectorized query engines.default boolean
canVectorize(Expr.InputBindingInspector inspector)
Check if an expression can be 'vectorized', for a given set of inputs.default void
decorateCacheKeyBuilder(CacheKeyBuilder builder)
Decorates theCacheKeyBuilder
for the default implementation ofgetCacheKey()
.ExprEval
eval(Expr.ObjectBinding bindings)
default String
getBindingIfIdentifier()
Returns the string key to use to get a value fromExpr.ObjectBinding
of anIdentifierExpr
, else null.default byte[]
getCacheKey()
Get a byte array used as a cache key.default org.apache.druid.math.expr.IdentifierExpr
getIdentifierExprIfIdentifierExpr()
Returns anIdentifierExpr
if it is one, else nulldefault String
getIdentifierIfIdentifier()
Returns the string identifier of anIdentifierExpr
, else null.default Object
getLiteralValue()
Returns the value of expr if expr is a literal, or throws an exception otherwise.default ExpressionType
getOutputType(Expr.InputBindingInspector inspector)
Given anExpr.InputBindingInspector
, compute what the outputExpressionType
will be for this expression.default boolean
isIdentifier()
default boolean
isLiteral()
Indicates expression is a constant whose literal value can be extracted bygetLiteralValue()
, making evaluating with arguments and bindings unecessarydefault boolean
isNullLiteral()
static Expr
singleThreaded(Expr expr, Expr.InputBindingInspector inspector)
Returns the single-threaded version of the given expression tree.String
stringify()
Convert theExpr
back into parseable string that when parsed withParser.parse(String, ExprMacroTable)
will produce an equivalentExpr
.Expr
visit(Expr.Shuttle shuttle)
Programatically rewrite theExpr
tree with aExpr.Shuttle
.
-
-
-
Field Detail
-
NULL_LITERAL
static final String NULL_LITERAL
- See Also:
- Constant Field Values
-
ARG_JOINER
static final com.google.common.base.Joiner ARG_JOINER
-
-
Method Detail
-
isLiteral
default boolean isLiteral()
Indicates expression is a constant whose literal value can be extracted bygetLiteralValue()
, making evaluating with arguments and bindings unecessary
-
isNullLiteral
default boolean isNullLiteral()
-
isIdentifier
default boolean isIdentifier()
-
getLiteralValue
@Nullable default Object getLiteralValue()
Returns the value of expr if expr is a literal, or throws an exception otherwise.- Returns:
ConstantExpr
's literal value- Throws:
IllegalStateException
- if expr is not a literal
-
getIdentifierExprIfIdentifierExpr
@Nullable default org.apache.druid.math.expr.IdentifierExpr getIdentifierExprIfIdentifierExpr()
Returns anIdentifierExpr
if it is one, else null
-
getIdentifierIfIdentifier
@Nullable default String getIdentifierIfIdentifier()
Returns the string identifier of anIdentifierExpr
, else null. Use this method to analyze anExpr
tree when trying to distinguish between differentIdentifierExpr
with the sameIdentifierExpr.binding
. Do NOT use this method to analyze the input binding (e.g. backing column name), usegetBindingIfIdentifier()
instead.
-
getBindingIfIdentifier
@Nullable default String getBindingIfIdentifier()
Returns the string key to use to get a value fromExpr.ObjectBinding
of anIdentifierExpr
, else null. Use this method to analyze the inputs required to anExpr
tree (e.g. backing column name).
-
eval
ExprEval eval(Expr.ObjectBinding bindings)
-
stringify
String stringify()
Convert theExpr
back into parseable string that when parsed withParser.parse(String, ExprMacroTable)
will produce an equivalentExpr
.
-
visit
Expr visit(Expr.Shuttle shuttle)
Programatically rewrite theExpr
tree with aExpr.Shuttle
. EachExpr
is responsible for ensuring theExpr.Shuttle
can visit all of itsExpr
children, as well as updating its childrenExpr
with the results from theExpr.Shuttle
, before finally visiting an updated form of itself. When this Expr is the result ofExprMacroTable.ExprMacro.apply(java.util.List<org.apache.druid.math.expr.Expr>)
, all of the original arguments to the macro must be visited, including arguments that may have been "baked in" to this Expr.
-
analyzeInputs
Expr.BindingAnalysis analyzeInputs()
-
getOutputType
@Nullable default ExpressionType getOutputType(Expr.InputBindingInspector inspector)
Given anExpr.InputBindingInspector
, compute what the outputExpressionType
will be for this expression. In the vectorized expression engine, ifcanVectorize(InputBindingInspector)
returns true, a return value of null MUST ONLY indicate that the expression has all null inputs (non-existent columns) or null constants for the entire expression. Otherwise, all vectorizable expressions must produce an output type to correctly operate with the vectorized engine. Outside the context of vectorized expressions, a return value of null can also indicate that the given type information was not enough to resolve the output type, so the expression must be evaluated using defaulteval(org.apache.druid.math.expr.Expr.ObjectBinding)
handling where types are only known after evaluation, throughExprEval.type()
, such as transform expressions at ingestion time
-
canVectorize
default boolean canVectorize(Expr.InputBindingInspector inspector)
Check if an expression can be 'vectorized', for a given set of inputs. If this method returns true,asVectorProcessor(org.apache.druid.math.expr.Expr.VectorInputBindingInspector)
is expected to produce aExprVectorProcessor
which can evaluate values in batches to use with vectorized query engines.- Parameters:
inspector
-
-
asSingleThreaded
default Expr asSingleThreaded(Expr.InputBindingInspector inspector)
Possibly convert theExpr
into an optimized, possibly not thread-safeExpr
. Does not convert childExpr
. Most callers should usesingleThreaded(Expr, InputBindingInspector)
to convert an entire tree, which delegates to this method to translate individual nodes.
-
asVectorProcessor
default <T> ExprVectorProcessor<T> asVectorProcessor(Expr.VectorInputBindingInspector inspector)
Builds a 'vectorized' expression processor, that can operate on batches of input values for use in vectorized query engines.- Parameters:
inspector
-
-
asColumnIndexSupplier
@Nullable default ColumnIndexSupplier asColumnIndexSupplier(ColumnIndexSelector columnIndexSelector, @Nullable ColumnType outputType)
-
decorateCacheKeyBuilder
default void decorateCacheKeyBuilder(CacheKeyBuilder builder)
Decorates theCacheKeyBuilder
for the default implementation ofgetCacheKey()
. The default cache key implementation includes the output ofstringify()
and then uses aExpr.Shuttle
to call this method on all children. The stringified representation is sufficient for most expressions, but for any which rely on external state that might change, this method allows the cache key to change when the state does, even if the expression itself is otherwise the same.
-
getCacheKey
default byte[] getCacheKey()
Description copied from interface:Cacheable
Get a byte array used as a cache key.- Specified by:
getCacheKey
in interfaceCacheable
- Returns:
- a cache key
-
singleThreaded
static Expr singleThreaded(Expr expr, Expr.InputBindingInspector inspector)
Returns the single-threaded version of the given expression tree. Nested expressions in the subtree are also optimized.
-
-