soumyava commented on code in PR #15182: URL: https://github.com/apache/druid/pull/15182#discussion_r1363075294
########## processing/src/main/java/org/apache/druid/query/filter/IsBooleanDimFilter.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.druid.query.filter; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.RangeSet; +import org.apache.druid.error.DruidException; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.segment.filter.IsBooleanFilter; + +import java.util.Objects; +import java.util.Set; + +/** + * Abstract SQL three-value logic wrapper for some child {@link DimFilter} to implement '{filter} IS TRUE' and + * '{filter} IS FALSE'. + * + * @see IsTrueDimFilter - IS TRUE + * @see IsFalseDimFilter - IS FALSE + * @see IsBooleanFilter - actual filtering logic + */ +public abstract class IsBooleanDimFilter extends AbstractOptimizableDimFilter +{ + private final DimFilter field; + private final boolean isTrue; + + public IsBooleanDimFilter( + DimFilter field, + boolean isTrue + ) + { + if (field == null) { + throw DruidException.forPersona(DruidException.Persona.USER) Review Comment: maybe replace with InvalidInput.exception() ########## sql/src/main/java/org/apache/druid/sql/calcite/expression/Expressions.java: ########## @@ -488,13 +512,23 @@ private static DimFilter toSimpleLeafFilter( final SqlKind kind = rexNode.getKind(); if (kind == SqlKind.IS_TRUE || kind == SqlKind.IS_NOT_FALSE) { + if (NullHandling.useThreeValueLogic()) { + // use expression filter to get istrue or notfalse expressions for correct 3vl behavior + return toExpressionLeafFilter(plannerContext, rowSignature, rexNode); + } + // legacy behavior return toSimpleLeafFilter( plannerContext, rowSignature, virtualColumnRegistry, Iterables.getOnlyElement(((RexCall) rexNode).getOperands()) ); } else if (kind == SqlKind.IS_FALSE || kind == SqlKind.IS_NOT_TRUE) { + if (NullHandling.useThreeValueLogic()) { + // use expression filter to get isfalse or nottrue expressions for correct 3vl behavior Review Comment: nit. isnottrue in place of nottrue ########## processing/src/test/java/org/apache/druid/segment/filter/EqualityFilterTests.java: ########## @@ -255,6 +257,26 @@ public void testSingleValueStringColumnWithNulls() NotDimFilter.of(new EqualityFilter("s0", ColumnType.STRING, "a", null)), ImmutableList.of("0", "2", "4") ); + // "(s0 = 'a') is not true", same rows as "s0 <> 'a'", but also with null rows + assertFilterMatches( + NotDimFilter.of(IsTrueDimFilter.of(new EqualityFilter("s0", ColumnType.STRING, "a", null))), + ImmutableList.of("0", "2", "3", "4") + ); + // "(s0 = 'a') is true", equivalent to "s0 = 'a'" + assertFilterMatches( + IsTrueDimFilter.of(new EqualityFilter("s0", ColumnType.STRING, "a", null)), + ImmutableList.of("1", "5") + ); + // "(s0 = 'a') is false", equivalent results to "s0 <> 'a'" + assertFilterMatches( + IsFalseDimFilter.of(new EqualityFilter("s0", ColumnType.STRING, "a", null)), + ImmutableList.of("0", "2", "4") + ); + // "(s0 = 'a') is not false", same rows as "s0 = 'a'", but also with null rows Review Comment: These comments are great ! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
