Skip to content

Commit

Permalink
Adding basic null predicate support (#4943)
Browse files Browse the repository at this point in the history
Adding null predicate support in reference to Issue #4230

This PR adds limited support for "IS NULL" and "IS NOT NULL" filter predicates. Currently this only works for leaf filter predicates.
  • Loading branch information
icefury71 authored and Jackie-Jiang committed Dec 21, 2019
1 parent 8619391 commit 09db4d9
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
/**
* Autogenerated by Thrift Compiler (0.12.0)
* Autogenerated by Thrift Compiler (0.13.0)
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
* @generated
Expand All @@ -30,7 +30,7 @@
* Filter Operator
*
*/
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.12.0)", date = "2019-07-19")
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.13.0)", date = "2019-12-19")
public enum FilterOperator implements org.apache.thrift.TEnum {
AND(0),
OR(1),
Expand All @@ -39,7 +39,9 @@ public enum FilterOperator implements org.apache.thrift.TEnum {
RANGE(4),
REGEXP_LIKE(5),
NOT_IN(6),
IN(7);
IN(7),
IS_NULL(8),
IS_NOT_NULL(9);

private final int value;

Expand Down Expand Up @@ -77,6 +79,10 @@ public static FilterOperator findByValue(int value) {
return NOT_IN;
case 7:
return IN;
case 8:
return IS_NULL;
case 9:
return IS_NOT_NULL;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.pinot.pql.parsers.pql2.ast.IdentifierAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.InPredicateAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.IntegerLiteralAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.IsPredicateAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.IsNullPredicateAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.LimitAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.OptionAstNode;
import org.apache.pinot.pql.parsers.pql2.ast.OptionsAstNode;
Expand Down Expand Up @@ -137,7 +137,7 @@ public void exitOutputColumnList(@NotNull PQL2Parser.OutputColumnListContext ctx

@Override
public void enterIsPredicate(@NotNull PQL2Parser.IsPredicateContext ctx) {
pushNode(new IsPredicateAstNode());
pushNode(new IsNullPredicateAstNode(ctx.isClause().NOT() != null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public enum FilterKind {
BETWEEN,
IN,
NOT_IN,
REGEXP_LIKE
REGEXP_LIKE,
IS_NULL,
IS_NOT_NULL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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.pql.parsers.pql2.ast;

import java.util.Collections;
import org.apache.pinot.common.request.Expression;
import org.apache.pinot.common.request.FilterOperator;
import org.apache.pinot.common.utils.request.FilterQueryTree;
import org.apache.pinot.common.utils.request.HavingQueryTree;
import org.apache.pinot.common.utils.request.RequestUtils;
import org.apache.pinot.pql.parsers.Pql2CompilationException;


/**
* AST node for IS predicates (foo IS NULL, foo IS NOT NULL).
*/
public class IsNullPredicateAstNode extends PredicateAstNode {

private final boolean _isNegation;

public IsNullPredicateAstNode(boolean notNodeExists) {
_isNegation = notNodeExists;
}

@Override
public void addChild(AstNode childNode) {
if (childNode instanceof IdentifierAstNode) {
if (_identifier == null) {
_identifier = ((IdentifierAstNode) childNode).getName();
} else {
throw new Pql2CompilationException("Only one column supported in IS predicate.");
}
} else if (childNode instanceof FunctionCallAstNode) {
throw new Pql2CompilationException("Function not supported in IS predicate");
} else if (childNode instanceof LiteralAstNode) {
throw new Pql2CompilationException("Constants not supported in IS predicate");
} else {
super.addChild(childNode);
}
}

@Override
public FilterQueryTree buildFilterQueryTree() {
if (_identifier == null) {
throw new Pql2CompilationException("IS predicate has no identifier");
}
if (_isNegation) {
return new FilterQueryTree(_identifier, Collections.EMPTY_LIST, FilterOperator.IS_NOT_NULL, null);
}
return new FilterQueryTree(_identifier, Collections.EMPTY_LIST, FilterOperator.IS_NULL, null);
}

@Override
public Expression buildFilterExpression() {
if (_identifier == null) {
throw new Pql2CompilationException("IS predicate has no identifier");
}
String filterName = _isNegation ? FilterKind.IS_NOT_NULL.name() : FilterKind.IS_NULL.name();
Expression expression = RequestUtils.getFunctionExpression(filterName);
expression.getFunctionCall().addToOperands(RequestUtils.createIdentifierExpression(_identifier));
return expression;
}

@Override
public HavingQueryTree buildHavingQueryTree() {
throw new Pql2CompilationException("IS NOT? NULL predicate is not supported in HAVING clause.");
}
}
4 changes: 3 additions & 1 deletion pinot-common/src/thrift/request.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ enum FilterOperator {
RANGE,
REGEXP_LIKE,
NOT_IN,
IN
IN,
IS_NULL,
IS_NOT_NULL
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.pinot.common.utils.request.FilterQueryTree;
import org.apache.pinot.core.common.predicate.EqPredicate;
import org.apache.pinot.core.common.predicate.InPredicate;
import org.apache.pinot.core.common.predicate.IsNotNullPredicate;
import org.apache.pinot.core.common.predicate.IsNullPredicate;
import org.apache.pinot.core.common.predicate.NEqPredicate;
import org.apache.pinot.core.common.predicate.NotInPredicate;
import org.apache.pinot.core.common.predicate.RangePredicate;
Expand All @@ -32,7 +34,7 @@
public abstract class Predicate {

public enum Type {
EQ, NEQ, REGEXP_LIKE, RANGE, IN, NOT_IN;
EQ, NEQ, REGEXP_LIKE, RANGE, IN, NOT_IN, IS_NULL, IS_NOT_NULL;

public boolean isExclusive() {
return this == NEQ || this == NOT_IN;
Expand Down Expand Up @@ -92,6 +94,12 @@ public static Predicate newPredicate(FilterQueryTree filterQueryTree) {
case IN:
predicate = new InPredicate(column, value);
break;
case IS_NULL:
predicate = new IsNullPredicate(column);
break;
case IS_NOT_NULL:
predicate = new IsNotNullPredicate(column);
break;
default:
throw new UnsupportedOperationException("Unsupported filterType:" + filterType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.common.predicate;

import org.apache.pinot.core.common.Predicate;


public class IsNotNullPredicate extends Predicate {
public IsNotNullPredicate(String column) {
super(column, Type.IS_NOT_NULL, null);
}

@Override
public String toString() {
return "Predicate: type: " + getType() + ", left : " + getLhs() + "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pinot.pql.parsers.pql2.ast;
package org.apache.pinot.core.common.predicate;

import org.apache.pinot.pql.parsers.Pql2CompilationException;
import org.apache.pinot.core.common.Predicate;


/**
* AST node for IS predicates (foo IS NULL, foo IS NOT NULL).
*/
public class IsPredicateAstNode extends BaseAstNode {
public IsPredicateAstNode() {
throw new Pql2CompilationException("IS predicate is not supported");
public class IsNullPredicate extends Predicate {
public IsNullPredicate(String column) {
super(column, Type.IS_NULL, null);
}

@Override
public String toString() {
return "Predicate: type: " + getType() + ", left : " + getLhs() + "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.pinot.core.common.Predicate;
import org.apache.pinot.core.indexsegment.IndexSegment;
import org.apache.pinot.core.operator.filter.BaseFilterOperator;
import org.apache.pinot.core.operator.filter.BitmapBasedFilterOperator;
import org.apache.pinot.core.operator.filter.EmptyFilterOperator;
import org.apache.pinot.core.operator.filter.ExpressionFilterOperator;
import org.apache.pinot.core.operator.filter.FilterOperatorUtils;
Expand All @@ -44,6 +45,7 @@
import org.apache.pinot.core.operator.transform.function.TransformFunction;
import org.apache.pinot.core.operator.transform.function.TransformFunctionFactory;
import org.apache.pinot.core.segment.index.readers.Dictionary;
import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -111,6 +113,15 @@ private static BaseFilterOperator constructPhysicalOperator(FilterQueryTree filt
// Leaf filter operator
Predicate predicate = Predicate.newPredicate(filterQueryTree);

// Check for null predicate
Predicate.Type type = predicate.getType();
if (type.equals(Predicate.Type.IS_NULL) || type.equals(Predicate.Type.IS_NOT_NULL)) {
DataSource dataSource = segment.getDataSource(filterQueryTree.getColumn());
ImmutableRoaringBitmap nullBitmap = dataSource.getNullValueVector().getNullBitmap();
boolean exclusive = (type == Predicate.Type.IS_NOT_NULL);
return new BitmapBasedFilterOperator(new ImmutableRoaringBitmap[]{nullBitmap}, 0, numDocs - 1, exclusive);
}

TransformExpressionTree expression = filterQueryTree.getExpression();
if (expression.getExpressionType() == TransformExpressionTree.ExpressionType.FUNCTION) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.realtime.impl;

import org.roaringbitmap.buffer.MutableRoaringBitmap;


/**
* Helper wrapper class for {@link MutableRoaringBitmap} to make it thread-safe.
*/
public class ThreadSafeMutableRoaringBitmap {
private MutableRoaringBitmap _mutableRoaringBitmap;

public ThreadSafeMutableRoaringBitmap() {
_mutableRoaringBitmap = new MutableRoaringBitmap();
}

public ThreadSafeMutableRoaringBitmap(int firstDocId) {
_mutableRoaringBitmap = new MutableRoaringBitmap();
_mutableRoaringBitmap.add(firstDocId);
}

public void checkAndAdd(int docId) {
if (!_mutableRoaringBitmap.contains(docId)) {
synchronized (this) {
_mutableRoaringBitmap.add(docId);
}
}
}

public boolean contains(int docId) {
return _mutableRoaringBitmap.contains(docId);
}

public synchronized MutableRoaringBitmap getMutableRoaringBitmap() {
return _mutableRoaringBitmap.clone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.pinot.core.realtime.impl.ThreadSafeMutableRoaringBitmap;
import org.apache.pinot.core.segment.index.readers.InvertedIndexReader;
import org.roaringbitmap.buffer.MutableRoaringBitmap;

Expand Down Expand Up @@ -81,28 +82,4 @@ public MutableRoaringBitmap getDocIds(int dictId) {
@Override
public void close() {
}

/**
* Helper wrapper class for {@link MutableRoaringBitmap} to make it thread-safe.
*/
private static class ThreadSafeMutableRoaringBitmap {
private MutableRoaringBitmap _mutableRoaringBitmap;

public ThreadSafeMutableRoaringBitmap(int firstDocId) {
_mutableRoaringBitmap = new MutableRoaringBitmap();
_mutableRoaringBitmap.add(firstDocId);
}

public void checkAndAdd(int docId) {
if (!_mutableRoaringBitmap.contains(docId)) {
synchronized (this) {
_mutableRoaringBitmap.add(docId);
}
}
}

public synchronized MutableRoaringBitmap getMutableRoaringBitmap() {
return _mutableRoaringBitmap.clone();
}
}
}
Loading

0 comments on commit 09db4d9

Please sign in to comment.