-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add CLPDecodeRewriter to make it easier to call clpDecode with a column-group name rather than the individual columns. #11006
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
3 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
177 changes: 177 additions & 0 deletions
177
pinot-common/src/main/java/org/apache/pinot/sql/parsers/rewriter/CLPDecodeRewriter.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,177 @@ | ||
| /** | ||
| * 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.sql.parsers.rewriter; | ||
|
|
||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
| import org.apache.pinot.common.function.TransformFunctionType; | ||
| import org.apache.pinot.common.request.Expression; | ||
| import org.apache.pinot.common.request.ExpressionType; | ||
| import org.apache.pinot.common.request.Function; | ||
| import org.apache.pinot.common.request.Identifier; | ||
| import org.apache.pinot.common.request.Literal; | ||
| import org.apache.pinot.common.request.PinotQuery; | ||
| import org.apache.pinot.sql.parsers.SqlCompilationException; | ||
|
|
||
|
|
||
| /** | ||
| * Query rewriter to rewrite clpDecode so that users can pass in the name of a CLP-encoded column group instead of the | ||
| * names of all the columns in the group. | ||
| * <p> | ||
| * Usage: | ||
| * <pre> | ||
| * clpDecode("columnGroupName"[, defaultValue]) | ||
| * </pre> | ||
| * which will be rewritten to: | ||
| * <pre> | ||
| * clpDecode("columnGroupName_logtype", "columnGroupName_dictionaryVars", "columnGroupName_encodedVars"[, | ||
| * defaultValue]) | ||
| * </pre> | ||
| * The "defaultValue" is optional. See | ||
| * {@link org.apache.pinot.core.operator.transform.function.CLPDecodeTransformFunction} for its description. | ||
| * <p> | ||
| * Sample queries: | ||
| * <pre> | ||
| * SELECT clpDecode("message") FROM table | ||
| * SELECT clpDecode("message", 'null') FROM table | ||
| * </pre> | ||
| * See {@link org.apache.pinot.core.operator.transform.function.CLPDecodeTransformFunction} for details about the | ||
| * underlying clpDecode transformer. | ||
| */ | ||
| public class CLPDecodeRewriter implements QueryRewriter { | ||
| public static final String LOGTYPE_COLUMN_SUFFIX = "_logtype"; | ||
| public static final String DICTIONARY_VARS_COLUMN_SUFFIX = "_dictionaryVars"; | ||
| public static final String ENCODED_VARS_COLUMN_SUFFIX = "_encodedVars"; | ||
|
|
||
| private static final String _CLPDECODE_LOWERCASE_TRANSFORM_NAME = | ||
| TransformFunctionType.CLPDECODE.getName().toLowerCase(); | ||
|
|
||
| @Override | ||
| public PinotQuery rewrite(PinotQuery pinotQuery) { | ||
| List<Expression> selectExpressions = pinotQuery.getSelectList(); | ||
| if (null != selectExpressions) { | ||
| for (Expression e : selectExpressions) { | ||
| tryRewritingExpression(e); | ||
| } | ||
| } | ||
| List<Expression> groupByExpressions = pinotQuery.getGroupByList(); | ||
| if (null != groupByExpressions) { | ||
| for (Expression e : groupByExpressions) { | ||
| tryRewritingExpression(e); | ||
| } | ||
| } | ||
| List<Expression> orderByExpressions = pinotQuery.getOrderByList(); | ||
| if (null != orderByExpressions) { | ||
| for (Expression e : orderByExpressions) { | ||
| tryRewritingExpression(e); | ||
| } | ||
| } | ||
| tryRewritingExpression(pinotQuery.getFilterExpression()); | ||
| tryRewritingExpression(pinotQuery.getHavingExpression()); | ||
| return pinotQuery; | ||
| } | ||
|
|
||
| /** | ||
| * Rewrites any instances of clpDecode in the given expression | ||
| * @param expression Expression which may contain instances of clpDecode | ||
| */ | ||
| private void tryRewritingExpression(Expression expression) { | ||
| if (null == expression) { | ||
| return; | ||
| } | ||
| Function function = expression.getFunctionCall(); | ||
| if (null == function) { | ||
| return; | ||
| } | ||
|
|
||
| String functionName = function.getOperator(); | ||
| if (functionName.equals(_CLPDECODE_LOWERCASE_TRANSFORM_NAME)) { | ||
| rewriteCLPDecodeFunction(expression); | ||
| return; | ||
| } | ||
|
|
||
| // Function isn't a CLP function that needs rewriting, but the arguments might be, so we recursively process them. | ||
| for (Expression op : function.getOperands()) { | ||
| tryRewritingExpression(op); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rewrites the given instance of clpDecode as described in the class' Javadoc | ||
| * @param expression clpDecode function expression | ||
| */ | ||
| private void rewriteCLPDecodeFunction(Expression expression) { | ||
| Function function = expression.getFunctionCall(); | ||
| List<Expression> arguments = function.getOperands(); | ||
|
|
||
| // Validate clpDecode's arguments | ||
| int numArgs = arguments.size(); | ||
| if (numArgs < 1 || numArgs > 2) { | ||
| // Too few/many args for this rewriter, so do nothing and let it pass through to the clpDecode transform function | ||
| return; | ||
| } | ||
|
|
||
| Expression arg0 = arguments.get(0); | ||
| if (ExpressionType.IDENTIFIER != arg0.getType()) { | ||
| throw new SqlCompilationException("clpDecode: 1st argument must be a column group name (identifier)."); | ||
| } | ||
| String columnGroupName = arg0.getIdentifier().getName(); | ||
|
|
||
| Literal defaultValueLiteral = null; | ||
| if (numArgs > 1) { | ||
| Expression arg1 = arguments.get(1); | ||
| if (ExpressionType.LITERAL != arg1.getType()) { | ||
| throw new SqlCompilationException("clpDecode: 2nd argument must be a default value (literal)."); | ||
| } | ||
| defaultValueLiteral = arg1.getLiteral(); | ||
| } | ||
|
|
||
| // Replace the columnGroup with the individual columns | ||
| arguments.clear(); | ||
| addCLPDecodeOperands(columnGroupName, defaultValueLiteral, function); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the CLPDecode transform function's operands to the given function | ||
| * @param columnGroupName Name of the CLP-encoded column group | ||
| * @param defaultValueLiteral Optional default value to pass through to the transform function | ||
| * @param clpDecode The function to add the operands to | ||
| */ | ||
| private void addCLPDecodeOperands(String columnGroupName, @Nullable Literal defaultValueLiteral, Function clpDecode) { | ||
| Expression e; | ||
|
|
||
| e = new Expression(ExpressionType.IDENTIFIER); | ||
| e.setIdentifier(new Identifier(columnGroupName + LOGTYPE_COLUMN_SUFFIX)); | ||
| clpDecode.addToOperands(e); | ||
|
|
||
| e = new Expression(ExpressionType.IDENTIFIER); | ||
| e.setIdentifier(new Identifier(columnGroupName + DICTIONARY_VARS_COLUMN_SUFFIX)); | ||
| clpDecode.addToOperands(e); | ||
|
|
||
| e = new Expression(ExpressionType.IDENTIFIER); | ||
| e.setIdentifier(new Identifier(columnGroupName + ENCODED_VARS_COLUMN_SUFFIX)); | ||
| clpDecode.addToOperands(e); | ||
|
|
||
| if (null != defaultValueLiteral) { | ||
| e = new Expression(ExpressionType.LITERAL); | ||
| e.setLiteral(defaultValueLiteral); | ||
| clpDecode.addToOperands(e); | ||
| } | ||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
pinot-common/src/test/java/org/apache/pinot/sql/parsers/rewriter/CLPDecodeRewriterTest.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,65 @@ | ||
| /** | ||
| * 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.sql.parsers.rewriter; | ||
|
|
||
| import org.apache.pinot.sql.parsers.CalciteSqlParser; | ||
| import org.apache.pinot.sql.parsers.SqlCompilationException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertThrows; | ||
|
|
||
|
|
||
| public class CLPDecodeRewriterTest { | ||
| private static final QueryRewriter _QUERY_REWRITER = new CLPDecodeRewriter(); | ||
|
|
||
| @Test | ||
| public void testCLPDecodeRewrite() { | ||
| // clpDecode rewrite from column group to individual columns | ||
| testQueryRewrite("SELECT clpDecode(message) FROM clpTable", | ||
| "SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars) FROM clpTable"); | ||
| testQueryRewrite("SELECT clpDecode(message, 'null') FROM clpTable", | ||
| "SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars, 'null') FROM clpTable"); | ||
|
|
||
| // clpDecode passthrough | ||
| testQueryRewrite("SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars) FROM clpTable", | ||
| "SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars) FROM clpTable"); | ||
| testQueryRewrite( | ||
| "SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars, 'null') FROM clpTable", | ||
| "SELECT clpDecode(message_logtype, message_dictionaryVars, message_encodedVars, 'null') FROM clpTable"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnsupportedCLPDecodeQueries() { | ||
| testUnsupportedQuery("SELECT clpDecode('message') FROM clpTable"); | ||
| testUnsupportedQuery("SELECT clpDecode('message', 'default') FROM clpTable"); | ||
| testUnsupportedQuery("SELECT clpDecode('message', default) FROM clpTable"); | ||
| testUnsupportedQuery("SELECT clpDecode(message, default) FROM clpTable"); | ||
| } | ||
|
|
||
| private void testQueryRewrite(String original, String expected) { | ||
| assertEquals(_QUERY_REWRITER.rewrite(CalciteSqlParser.compileToPinotQuery(original)), | ||
| CalciteSqlParser.compileToPinotQuery(expected)); | ||
| } | ||
|
|
||
| private void testUnsupportedQuery(String query) { | ||
| assertThrows(SqlCompilationException.class, | ||
| () -> _QUERY_REWRITER.rewrite(CalciteSqlParser.compileToPinotQuery(query))); | ||
| } | ||
| } |
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
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.
nit: return here and save the else branch.