-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for EXTRACT syntax and converts it to appropriate Pinot expression #9184
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dec524c
supporting EXTRACT syntax
3f1fcd1
fixed the licence header
cb9bb04
completed part1 -- updated CalciteSqlParser to support Extract query
9b3b27d
fixed based on the comments left
f11a5b2
adding ExtractTransformFunction
6062267
tmp
40837a0
adding changes in ExtractTransformFunction based on comments
c6f5183
added Field enum and updated based on comments
cc713c7
updating changes after reformatting
b880950
fixed some lint errors
7f0ad5c
fixing checkstyle errors
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
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
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
111 changes: 111 additions & 0 deletions
111
...main/java/org/apache/pinot/core/operator/transform/function/ExtractTransformFunction.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,111 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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 org.apache.pinot.core.operator.transform.function; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.pinot.core.operator.blocks.ProjectionBlock; | ||
| import org.apache.pinot.core.operator.transform.TransformResultMetadata; | ||
| import org.apache.pinot.segment.spi.datasource.DataSource; | ||
| import org.joda.time.Chronology; | ||
| import org.joda.time.DateTimeField; | ||
| import org.joda.time.chrono.ISOChronology; | ||
|
|
||
|
|
||
| public class ExtractTransformFunction extends BaseTransformFunction { | ||
| public static final String FUNCTION_NAME = "extract"; | ||
| private TransformFunction _mainTransformFunction; | ||
| protected Field _field; | ||
| protected Chronology _chronology = ISOChronology.getInstanceUTC(); | ||
|
|
||
| private enum Field { | ||
| YEAR, MONTH, DAY, HOUR, MINUTE, SECOND | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return FUNCTION_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) { | ||
| if (arguments.size() != 2) { | ||
| throw new IllegalArgumentException("Exactly 2 arguments are required for EXTRACT transform function"); | ||
| } | ||
|
|
||
| _field = Field.valueOf(((LiteralTransformFunction) arguments.get(0)).getLiteral()); | ||
|
|
||
| _mainTransformFunction = arguments.get(1); | ||
| } | ||
|
|
||
| @Override | ||
| public TransformResultMetadata getResultMetadata() { | ||
| return INT_SV_NO_DICTIONARY_METADATA; | ||
| } | ||
|
|
||
| @Override | ||
| public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) { | ||
| int numDocs = projectionBlock.getNumDocs(); | ||
|
|
||
| if (_intValuesSV == null || _intValuesSV.length < numDocs) { | ||
| _intValuesSV = new int[numDocs]; | ||
| } | ||
|
|
||
| long[] timestamps = _mainTransformFunction.transformToLongValuesSV(projectionBlock); | ||
|
|
||
| convert(timestamps, numDocs, _intValuesSV); | ||
|
|
||
| return _intValuesSV; | ||
| } | ||
|
|
||
| private void convert(long[] timestamps, int numDocs, int[] output) { | ||
| for (int i = 0; i < numDocs; i++) { | ||
| DateTimeField accessor; | ||
|
|
||
| switch (_field) { | ||
| case YEAR: | ||
| accessor = _chronology.year(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| case MONTH: | ||
| accessor = _chronology.monthOfYear(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| case DAY: | ||
| accessor = _chronology.dayOfMonth(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| case HOUR: | ||
| accessor = _chronology.hourOfDay(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| case MINUTE: | ||
| accessor = _chronology.minuteOfHour(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| case SECOND: | ||
| accessor = _chronology.secondOfMinute(); | ||
| output[i] = accessor.get(timestamps[i]); | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported FIELD type"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
66 changes: 66 additions & 0 deletions
66
.../java/org/apache/pinot/core/operator/transform/function/ExtractTransformFunctionTest.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,66 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF 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 org.apache.pinot.core.operator.transform.function; | ||
|
|
||
| import java.util.function.LongToIntFunction; | ||
| import org.apache.pinot.common.function.scalar.DateTimeFunctions; | ||
| import org.apache.pinot.common.request.context.ExpressionContext; | ||
| import org.apache.pinot.common.request.context.RequestContextUtils; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
|
|
||
|
|
||
| public class ExtractTransformFunctionTest extends BaseTransformFunctionTest { | ||
|
|
||
| @DataProvider | ||
| public static Object[][] testCases() { | ||
| return new Object[][]{ | ||
| //@formatter:off | ||
| {"year", (LongToIntFunction) DateTimeFunctions::year}, | ||
| {"month", (LongToIntFunction) DateTimeFunctions::month}, | ||
| {"day", (LongToIntFunction) DateTimeFunctions::dayOfMonth}, | ||
| {"hour", (LongToIntFunction) DateTimeFunctions::hour}, | ||
| {"minute", (LongToIntFunction) DateTimeFunctions::minute}, | ||
| {"second", (LongToIntFunction) DateTimeFunctions::second}, | ||
| // TODO: Need to add timezone_hour and timezone_minute | ||
| // "timezone_hour", | ||
tanmesh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // "timezone_minute", | ||
| //@formatter:on | ||
| }; | ||
| } | ||
|
|
||
| @Test(dataProvider = "testCases") | ||
| public void testExtractTransformFunction(String field, LongToIntFunction expected) { | ||
| // NOTE: functionality of ExtractTransformFunction is covered in ExtractTransformFunctionTest | ||
| // SELECT EXTRACT(YEAR FROM '2017-10-10') | ||
|
|
||
| ExpressionContext expression = | ||
| RequestContextUtils.getExpression(String.format("extract(%s FROM %s)", field, TIMESTAMP_COLUMN)); | ||
|
|
||
| TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap); | ||
| Assert.assertTrue(transformFunction instanceof ExtractTransformFunction); | ||
| int[] value = transformFunction.transformToIntValuesSV(_projectionBlock); | ||
| for (int i = 0; i < _projectionBlock.getNumDocs(); i++) { | ||
| assertEquals(value[i], expected.applyAsInt(_timeValues[i])); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.