Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions be/src/vec/functions/functions_logical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "vec/functions/functions_logical.h"

#include <bits/ranges_algo.h>
#include <glog/logging.h>

#include <utility>
Expand Down Expand Up @@ -208,9 +209,14 @@ Status FunctionAnyArityLogical<Impl, Name>::execute_impl(FunctionContext* contex
args_in.push_back(block.get_by_position(arg_index).column.get());

auto& result_info = block.get_by_position(result_index);
if (result_info.type->is_nullable()) {
null_execute_impl<Impl>(std::move(args_in), result_info, input_rows_count);
if constexpr (Impl::special_implementation_for_nulls()) {
if (result_info.type->is_nullable()) {
null_execute_impl<Impl>(std::move(args_in), result_info, input_rows_count);
} else {
basic_execute_impl<Impl>(std::move(args_in), result_info, input_rows_count);
}
} else {
DCHECK(std::ranges::all_of(args_in, [](const auto& arg) { return !arg->is_nullable(); }));
basic_execute_impl<Impl>(std::move(args_in), result_info, input_rows_count);
}
return Status::OK();
Expand Down Expand Up @@ -272,6 +278,7 @@ void register_function_logical(SimpleFunctionFactory& instance) {
instance.register_function<FunctionAnd>();
instance.register_function<FunctionOr>();
instance.register_function<FunctionNot>();
instance.register_function<FunctionXor>();
}

} // namespace doris::vectorized
15 changes: 15 additions & 0 deletions be/src/vec/functions/functions_logical.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ struct OrImpl {
static inline constexpr bool special_implementation_for_nulls() { return true; }
};

struct XorImpl {
using ResultType = UInt8;

static inline constexpr ResultType apply(UInt8 a, UInt8 b) { return a ^ b; }
// select null xor true , null xor false , false xor null , true xor null ;
static inline constexpr bool special_implementation_for_nulls() { return false; }
};

template <typename A>
struct NotImpl {
using ResultType = UInt8;
Expand Down Expand Up @@ -151,10 +159,17 @@ struct NameNot {
static constexpr auto name = "not";
};

struct NameXor {
static constexpr auto name = "xor";
};

using FunctionAnd =
FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::AndImpl, NameAnd>;
using FunctionOr =
FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::OrImpl, NameOr>;
using FunctionNot =
FunctionsLogicalDetail::FunctionUnaryLogical<FunctionsLogicalDetail::NotImpl, NameNot>;

using FunctionXor =
FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::XorImpl, NameXor>;
} // namespace doris::vectorized
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ ALTER: 'ALTER';
ANALYZE: 'ANALYZE';
ANALYZED: 'ANALYZED';
AND: 'AND';
XOR: 'XOR';
ANTI: 'ANTI';
APPEND: 'APPEND';
ARRAY: 'ARRAY';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ booleanExpression
| IS_NOT_NULL_PRED LEFT_PAREN valueExpression RIGHT_PAREN #is_not_null_pred
| valueExpression predicate? #predicated
| left=booleanExpression operator=(AND | LOGICALAND) right=booleanExpression #logicalBinary
| left=booleanExpression operator=XOR right=booleanExpression #logicalBinary
| left=booleanExpression operator=OR right=booleanExpression #logicalBinary
| left=booleanExpression operator=DOUBLEPIPES right=booleanExpression #doublePipes
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WidthBucket;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Xor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.XxHash32;
import org.apache.doris.nereids.trees.expressions.functions.scalar.XxHash64;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Year;
Expand Down Expand Up @@ -932,6 +933,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(WidthBucket.class, "width_bucket"),
scalar(XxHash32.class, "xxhash_32"),
scalar(XxHash64.class, "xxhash_64"),
scalar(Xor.class, "xor"),
scalar(Year.class, "year"),
scalar(YearCeil.class, "year_ceil"),
scalar(YearFloor.class, "year_floor"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Xor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearCeil;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearFloor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearsAdd;
Expand Down Expand Up @@ -1674,6 +1675,8 @@ private Expression expressionCombiner(Expression left, Expression right, Logical
return new And(left, right);
case DorisParser.OR:
return new Or(left, right);
case DorisParser.XOR:
return new Xor(left, right);
default:
throw new ParseException("Unsupported logical binary type: " + ctx.operator.getText(), ctx);
}
Expand Down
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.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.NullOrIdenticalSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BooleanType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* Xor predicate expression.
*/
public class Xor extends ScalarFunction
implements BinaryExpression, NullOrIdenticalSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BooleanType.INSTANCE).args(BooleanType.INSTANCE, BooleanType.INSTANCE));

/**
* constructor with 2 argument.
*/
public Xor(Expression arg0, Expression arg1) {
super("xor", arg0, arg1);
}

/**
* withChildren.
*/
@Override
public Xor withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new Xor(children.get(0), children.get(1));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitXor(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WidthBucket;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Xor;
import org.apache.doris.nereids.trees.expressions.functions.scalar.XxHash32;
import org.apache.doris.nereids.trees.expressions.functions.scalar.XxHash64;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Year;
Expand Down Expand Up @@ -2222,6 +2223,10 @@ default R visitMapValues(MapValues mapValues, C context) {
return visitScalarFunction(mapValues, context);
}

default R visitXor(Xor xor, C context) {
return visitScalarFunction(xor, context);
}

// struct function

default R visitCreateStruct(CreateStruct createStruct, C context) {
Expand Down
15 changes: 15 additions & 0 deletions regression-test/data/correctness_p0/test_xor_pred.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql_const --
false true true false \N \N \N \N \N

-- !sql_select --
\N \N \N \N
\N false \N \N
\N true \N \N
false \N \N \N
false false false true
false true true false
true \N \N \N
true false true false
true true false true

49 changes: 49 additions & 0 deletions regression-test/suites/correctness_p0/test_xor_pred.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

suite("test_xor_pred") {
sql """ drop table if exists dbxor; """
sql """
CREATE TABLE IF NOT EXISTS dbxor (
`k1` boolean NULL COMMENT "",
`k2` boolean NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`k1`)
DISTRIBUTED BY HASH(`k1`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"storage_format" = "V2"
);
"""
sql """set enable_nereids_planner=true,enable_fold_constant_by_be = false; """
qt_sql_const """
select true xor true,false xor true,true xor false,false xor false,null xor false,null xor true,true xor null ,false xor null,null xor null;
"""
sql """
insert into dbxor values(true,true),(true,false),(false,true),(false,false),(true,null),(false,null),(null,true),(null,false),(null,null);
"""
qt_sql_select """
select k1,k2 , k1 xor k2, not (k1 xor k2) from dbxor order by k1,k2;
"""

test {
sql """
select 1 xor 0;
"""
exception("Can not find the compatibility function signature: xor(TINYINT, TINYINT)")
}
}