-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expressions: Optimization for string expressions on the __time column. #5109
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,80 @@ public void tearDown() throws Exception | |
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void timeFormatUsingExpression(Blackhole blackhole) throws Exception | ||
| { | ||
| final Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors( | ||
| null, | ||
| index.getDataInterval(), | ||
| VirtualColumns.create( | ||
| ImmutableList.of( | ||
| new ExpressionVirtualColumn( | ||
| "v", | ||
| "timestamp_format(__time, 'yyyy-MM-dd')", | ||
| ValueType.LONG, | ||
| TestExprMacroTable.INSTANCE | ||
| ) | ||
| ) | ||
| ), | ||
| Granularities.ALL, | ||
| false, | ||
| null | ||
| ); | ||
|
|
||
| final List<?> results = Sequences.toList( | ||
| Sequences.map( | ||
| cursors, | ||
| cursor -> { | ||
| final DimensionSelector selector = cursor.getColumnSelectorFactory().makeDimensionSelector( | ||
| new DefaultDimensionSpec("v", "v", ValueType.STRING) | ||
| ); | ||
| consumeDimension(cursor, selector, blackhole); | ||
| return null; | ||
| } | ||
| ), | ||
| new ArrayList<>() | ||
| ); | ||
|
|
||
| blackhole.consume(results); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void timeFormatUsingExtractionFn(Blackhole blackhole) throws Exception | ||
| { | ||
| final Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors( | ||
| null, | ||
| index.getDataInterval(), | ||
| VirtualColumns.EMPTY, | ||
| Granularities.ALL, | ||
| false, | ||
| null | ||
| ); | ||
|
|
||
| final List<?> results = Sequences.toList( | ||
| Sequences.map( | ||
|
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. You can write |
||
| cursors, | ||
| cursor -> { | ||
| final DimensionSelector selector = cursor | ||
| .getColumnSelectorFactory() | ||
| .makeDimensionSelector( | ||
| new ExtractionDimensionSpec( | ||
| Column.TIME_COLUMN_NAME, | ||
| "v", | ||
| new TimeFormatExtractionFn("yyyy-MM-dd", null, null, null, false) | ||
| ) | ||
| ); | ||
|
|
||
| consumeDimension(cursor, selector, blackhole); | ||
| return null; | ||
| } | ||
| ), | ||
| new ArrayList<>() | ||
| ); | ||
|
|
||
| blackhole.consume(results); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void timeFloorUsingExpression(Blackhole blackhole) throws Exception | ||
| { | ||
|
|
||
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
85 changes: 85 additions & 0 deletions
85
...main/java/io/druid/segment/virtual/SingleLongInputCachingExpressionDimensionSelector.java
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,85 @@ | ||
| /* | ||
| * 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.segment.virtual; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import io.druid.java.util.common.ISE; | ||
| import io.druid.math.expr.Expr; | ||
| import io.druid.math.expr.Parser; | ||
| import io.druid.query.monomorphicprocessing.RuntimeShapeInspector; | ||
| import io.druid.segment.BaseLongColumnValueSelector; | ||
|
|
||
| public class SingleLongInputCachingExpressionDimensionSelector extends BaseSingleValueDimensionSelector | ||
| { | ||
| private final BaseLongColumnValueSelector selector; | ||
| private final Expr expression; | ||
| private final SingleInputBindings bindings = new SingleInputBindings(); | ||
|
|
||
| // Is this the first row? | ||
| private boolean first = true; | ||
|
|
||
| // Last read input value | ||
| private long lastInput; | ||
|
|
||
| // Last computed output value | ||
| private String lastOutput; | ||
|
|
||
| public SingleLongInputCachingExpressionDimensionSelector( | ||
| final BaseLongColumnValueSelector selector, | ||
| final Expr expression | ||
| ) | ||
| { | ||
| // Verify expression has just one binding. | ||
| if (Parser.findRequiredBindings(expression).size() != 1) { | ||
| throw new ISE("WTF?! Expected expression with just one binding"); | ||
| } | ||
|
|
||
| this.selector = Preconditions.checkNotNull(selector, "selector"); | ||
| this.expression = Preconditions.checkNotNull(expression, "expression"); | ||
| } | ||
|
|
||
| @Override | ||
| public void inspectRuntimeShape(final RuntimeShapeInspector inspector) | ||
|
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. Minor: I try to put this method the last or one of the last, because it's not the business logic. |
||
| { | ||
| inspector.visit("selector", selector); | ||
| inspector.visit("expression", expression); | ||
| } | ||
|
|
||
| @Override | ||
| public String getValue() | ||
| { | ||
| final long currentInput = selector.getLong(); | ||
|
|
||
| if (first || lastInput != currentInput) { | ||
| final String output = eval(currentInput); | ||
| lastInput = currentInput; | ||
| lastOutput = output; | ||
| first = false; | ||
| } | ||
|
|
||
| return lastOutput; | ||
| } | ||
|
|
||
| private String eval(final long value) | ||
| { | ||
| bindings.set(value); | ||
| return expression.eval(bindings).asString(); | ||
| } | ||
| } | ||
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.
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 method is always called with either
new ArrayList<>()orLists.newArrayList()as the second argument. The second parameter should be removed. Also, I suggest to add a default methodSequence.toList().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.
However I did it myself in #5175.