Search in sources :

Example 1 with TransformExpressionTree

use of com.linkedin.pinot.common.request.transform.TransformExpressionTree in project pinot by linkedin.

the class NoDictionaryGroupKeyGeneratorTest method testGroupKeyGenerator.

private void testGroupKeyGenerator(String[] groupByColumns, FieldSpec.DataType[] dataTypes) throws Exception {
    // Build the projection operator.
    MatchEntireSegmentOperator matchEntireSegmentOperator = new MatchEntireSegmentOperator(NUM_ROWS);
    BReusableFilteredDocIdSetOperator docIdSetOperator = new BReusableFilteredDocIdSetOperator(matchEntireSegmentOperator, NUM_ROWS, 10000);
    MProjectionOperator projectionOperator = new MProjectionOperator(_dataSourceMap, docIdSetOperator);
    TransformExpressionOperator transformOperator = new TransformExpressionOperator(projectionOperator, new ArrayList<TransformExpressionTree>());
    // Iterator over all projection blocks and generate group keys.
    TransformBlock transformBlock;
    int[] docIdToGroupKeys = new int[DocIdSetPlanNode.MAX_DOC_PER_CALL];
    GroupKeyGenerator groupKeyGenerator = null;
    while ((transformBlock = (TransformBlock) transformOperator.nextBlock()) != null) {
        if (groupKeyGenerator == null) {
            // Build the group key generator.
            groupKeyGenerator = (groupByColumns.length == 1) ? new NoDictionarySingleColumnGroupKeyGenerator(groupByColumns[0], dataTypes[0]) : new NoDictionaryMultiColumnGroupKeyGenerator(transformBlock, groupByColumns);
        }
        groupKeyGenerator.generateKeysForBlock(transformBlock, docIdToGroupKeys);
    }
    // Assert total number of group keys is as expected
    Assert.assertTrue(groupKeyGenerator != null);
    Set<String> expectedGroupKeys = getExpectedGroupKeys(_recordReader, groupByColumns);
    Assert.assertEquals(groupKeyGenerator.getCurrentGroupKeyUpperBound(), expectedGroupKeys.size(), "Number of group keys mis-match.");
    // Assert all group key values are as expected
    Iterator<GroupKeyGenerator.GroupKey> uniqueGroupKeys = groupKeyGenerator.getUniqueGroupKeys();
    while (uniqueGroupKeys.hasNext()) {
        GroupKeyGenerator.GroupKey groupKey = uniqueGroupKeys.next();
        String actual = groupKey.getStringKey();
        Assert.assertTrue(expectedGroupKeys.contains(actual), "Unexpected group key: " + actual);
    }
}
Also used : TransformExpressionOperator(com.linkedin.pinot.core.operator.transform.TransformExpressionOperator) MProjectionOperator(com.linkedin.pinot.core.operator.MProjectionOperator) MatchEntireSegmentOperator(com.linkedin.pinot.core.operator.filter.MatchEntireSegmentOperator) TransformBlock(com.linkedin.pinot.core.operator.blocks.TransformBlock) NoDictionarySingleColumnGroupKeyGenerator(com.linkedin.pinot.core.query.aggregation.groupby.NoDictionarySingleColumnGroupKeyGenerator) BReusableFilteredDocIdSetOperator(com.linkedin.pinot.core.operator.BReusableFilteredDocIdSetOperator) TransformExpressionTree(com.linkedin.pinot.common.request.transform.TransformExpressionTree) NoDictionaryMultiColumnGroupKeyGenerator(com.linkedin.pinot.core.query.aggregation.groupby.NoDictionaryMultiColumnGroupKeyGenerator) NoDictionarySingleColumnGroupKeyGenerator(com.linkedin.pinot.core.query.aggregation.groupby.NoDictionarySingleColumnGroupKeyGenerator) NoDictionaryMultiColumnGroupKeyGenerator(com.linkedin.pinot.core.query.aggregation.groupby.NoDictionaryMultiColumnGroupKeyGenerator) GroupKeyGenerator(com.linkedin.pinot.core.query.aggregation.groupby.GroupKeyGenerator)

Example 2 with TransformExpressionTree

use of com.linkedin.pinot.common.request.transform.TransformExpressionTree in project pinot by linkedin.

the class TransformExpressionTreeTest method test.

/**
   * This test validates an expression tree built by {@link TransformExpressionTree#buildTree(AstNode)}
   */
@Test
public void test() {
    TransformFunctionFactory.init(new String[] { TransformFunctionFactoryTest.foo.class.getName(), TransformFunctionFactoryTest.bar.class.getName() });
    Pql2Compiler compiler = new Pql2Compiler();
    String expression = "foo(bar('a', foo(b, 'c', d)), e)";
    TransformExpressionTree expressionTree = compiler.compileToExpressionTree(expression);
    TransformFunction rootTransform = TransformFunctionFactory.get(expressionTree.getTransformName());
    Assert.assertEquals(rootTransform.getName(), "foo");
    List<TransformExpressionTree> firstChildren = expressionTree.getChildren();
    Assert.assertEquals(firstChildren.size(), 2);
    TransformExpressionTree firstChild = firstChildren.get(0);
    Assert.assertEquals(firstChild.getTransformName(), "bar");
    Assert.assertEquals(firstChildren.get(1).toString(), "e");
    List<TransformExpressionTree> secondChildren = firstChild.getChildren();
    Assert.assertEquals(secondChildren.size(), 2);
    Assert.assertEquals(secondChildren.get(0).toString(), "a");
    Assert.assertEquals(secondChildren.get(1).getTransformName(), "foo");
    List<TransformExpressionTree> thirdChildren = secondChildren.get(1).getChildren();
    Assert.assertEquals(thirdChildren.get(0).toString(), "b");
    Assert.assertEquals(thirdChildren.get(1).toString(), "c");
    Assert.assertEquals(thirdChildren.get(2).toString(), "d");
}
Also used : TransformFunction(com.linkedin.pinot.core.operator.transform.function.TransformFunction) TransformExpressionTree(com.linkedin.pinot.common.request.transform.TransformExpressionTree) Pql2Compiler(com.linkedin.pinot.pql.parsers.Pql2Compiler) Test(org.testng.annotations.Test)

Example 3 with TransformExpressionTree

use of com.linkedin.pinot.common.request.transform.TransformExpressionTree in project pinot by linkedin.

the class DefaultExpressionEvaluator method evaluateExpression.

/**
   * Helper (recursive) method that walks the expression tree bottom up evaluating
   * transforms at each level.
   *
   * @param projectionBlock Projection block for which to evaluate the expression for
   * @param expressionTree Expression tree to evaluate
   * @return Result of the expression transform
   */
private BlockValSet evaluateExpression(ProjectionBlock projectionBlock, TransformExpressionTree expressionTree) {
    TransformFunction function = getTransformFunction(expressionTree.getTransformName());
    int numDocs = projectionBlock.getNumDocs();
    String expressionString = expressionTree.toString();
    TransformExpressionTree.ExpressionType expressionType = expressionTree.getExpressionType();
    switch(expressionType) {
        case FUNCTION:
            List<TransformExpressionTree> children = expressionTree.getChildren();
            int numChildren = children.size();
            BlockValSet[] transformArgs = new BlockValSet[numChildren];
            for (int i = 0; i < numChildren; i++) {
                transformArgs[i] = evaluateExpression(projectionBlock, children.get(i));
            }
            return new TransformBlockValSet(function, numDocs, transformArgs);
        case IDENTIFIER:
            return projectionBlock.getBlockValueSet(expressionString);
        case LITERAL:
            return new ConstantBlockValSet(expressionString, numDocs);
        default:
            throw new IllegalArgumentException("Illegal expression type in expression evaluator: " + expressionType);
    }
}
Also used : TransformFunction(com.linkedin.pinot.core.operator.transform.function.TransformFunction) TransformExpressionTree(com.linkedin.pinot.common.request.transform.TransformExpressionTree) BlockValSet(com.linkedin.pinot.core.common.BlockValSet) ConstantBlockValSet(com.linkedin.pinot.core.operator.docvalsets.ConstantBlockValSet) TransformBlockValSet(com.linkedin.pinot.core.operator.docvalsets.TransformBlockValSet) TransformBlockValSet(com.linkedin.pinot.core.operator.docvalsets.TransformBlockValSet) ConstantBlockValSet(com.linkedin.pinot.core.operator.docvalsets.ConstantBlockValSet)

Example 4 with TransformExpressionTree

use of com.linkedin.pinot.common.request.transform.TransformExpressionTree in project pinot by linkedin.

the class TransformExpressionOperatorTest method evaluateExpression.

/**
   * Helper method to evaluate one expression using the TransformOperator.
   * @param expression Expression to evaluate
   * @return Result of evaluation
   */
private double[] evaluateExpression(String expression) {
    Operator filterOperator = new MatchEntireSegmentOperator(_indexSegment.getSegmentMetadata().getTotalDocs());
    final BReusableFilteredDocIdSetOperator docIdSetOperator = new BReusableFilteredDocIdSetOperator(filterOperator, _indexSegment.getSegmentMetadata().getTotalDocs(), NUM_ROWS);
    final Map<String, BaseOperator> dataSourceMap = buildDataSourceMap(_indexSegment.getSegmentMetadata().getSchema());
    final MProjectionOperator projectionOperator = new MProjectionOperator(dataSourceMap, docIdSetOperator);
    Pql2Compiler compiler = new Pql2Compiler();
    List<TransformExpressionTree> expressionTrees = new ArrayList<>(1);
    expressionTrees.add(compiler.compileToExpressionTree(expression));
    TransformExpressionOperator transformOperator = new TransformExpressionOperator(projectionOperator, expressionTrees);
    transformOperator.open();
    TransformBlock transformBlock = (TransformBlock) transformOperator.getNextBlock();
    BlockValSet blockValueSet = transformBlock.getBlockValueSet(expression);
    double[] actual = blockValueSet.getDoubleValuesSV();
    transformOperator.close();
    return actual;
}
Also used : BaseOperator(com.linkedin.pinot.core.operator.BaseOperator) BReusableFilteredDocIdSetOperator(com.linkedin.pinot.core.operator.BReusableFilteredDocIdSetOperator) MatchEntireSegmentOperator(com.linkedin.pinot.core.operator.filter.MatchEntireSegmentOperator) Operator(com.linkedin.pinot.core.common.Operator) MProjectionOperator(com.linkedin.pinot.core.operator.MProjectionOperator) TransformExpressionOperator(com.linkedin.pinot.core.operator.transform.TransformExpressionOperator) BaseOperator(com.linkedin.pinot.core.operator.BaseOperator) TransformExpressionOperator(com.linkedin.pinot.core.operator.transform.TransformExpressionOperator) Pql2Compiler(com.linkedin.pinot.pql.parsers.Pql2Compiler) ArrayList(java.util.ArrayList) MProjectionOperator(com.linkedin.pinot.core.operator.MProjectionOperator) MatchEntireSegmentOperator(com.linkedin.pinot.core.operator.filter.MatchEntireSegmentOperator) TransformBlock(com.linkedin.pinot.core.operator.blocks.TransformBlock) BReusableFilteredDocIdSetOperator(com.linkedin.pinot.core.operator.BReusableFilteredDocIdSetOperator) TransformExpressionTree(com.linkedin.pinot.common.request.transform.TransformExpressionTree) BlockValSet(com.linkedin.pinot.core.common.BlockValSet)

Aggregations

TransformExpressionTree (com.linkedin.pinot.common.request.transform.TransformExpressionTree)4 BlockValSet (com.linkedin.pinot.core.common.BlockValSet)2 BReusableFilteredDocIdSetOperator (com.linkedin.pinot.core.operator.BReusableFilteredDocIdSetOperator)2 MProjectionOperator (com.linkedin.pinot.core.operator.MProjectionOperator)2 TransformBlock (com.linkedin.pinot.core.operator.blocks.TransformBlock)2 MatchEntireSegmentOperator (com.linkedin.pinot.core.operator.filter.MatchEntireSegmentOperator)2 TransformExpressionOperator (com.linkedin.pinot.core.operator.transform.TransformExpressionOperator)2 TransformFunction (com.linkedin.pinot.core.operator.transform.function.TransformFunction)2 Pql2Compiler (com.linkedin.pinot.pql.parsers.Pql2Compiler)2 Operator (com.linkedin.pinot.core.common.Operator)1 BaseOperator (com.linkedin.pinot.core.operator.BaseOperator)1 ConstantBlockValSet (com.linkedin.pinot.core.operator.docvalsets.ConstantBlockValSet)1 TransformBlockValSet (com.linkedin.pinot.core.operator.docvalsets.TransformBlockValSet)1 GroupKeyGenerator (com.linkedin.pinot.core.query.aggregation.groupby.GroupKeyGenerator)1 NoDictionaryMultiColumnGroupKeyGenerator (com.linkedin.pinot.core.query.aggregation.groupby.NoDictionaryMultiColumnGroupKeyGenerator)1 NoDictionarySingleColumnGroupKeyGenerator (com.linkedin.pinot.core.query.aggregation.groupby.NoDictionarySingleColumnGroupKeyGenerator)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1