// 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.

//go:build go1.18

package compute

import (
	

	
	
	
)

type compareFunction struct {
	ScalarFunction
}

func ( *compareFunction) ( context.Context,  FunctionOptions,  ...Datum) (Datum, error) {
	return execInternal(, , , -1, ...)
}

func ( *compareFunction) ( ...arrow.DataType) (exec.Kernel, error) {
	if  := .checkArity(len());  != nil {
		return nil, 
	}

	if hasDecimal(...) {
		if  := castBinaryDecimalArgs(decPromoteAdd, ...);  != nil {
			return nil, 
		}
	}

	if ,  := .DispatchExact(...);  == nil {
		return , nil
	}

	ensureDictionaryDecoded(...)
	ensureNoExtensionType(...)
	replaceNullWithOtherType(...)

	if  := commonNumeric(...);  != nil {
		replaceTypes(, ...)
	} else if  := commonTemporal(...);  != nil {
		replaceTypes(, ...)
	} else if  := commonBinary(...);  != nil {
		replaceTypes(, ...)
	}

	return .DispatchExact(...)
}

type flippedData struct {
	*kernels.CompareData

	unflippedExec exec.ArrayKernelExec
}

func flippedCompare( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	 := .Kernel.(*exec.ScalarKernel)
	 := .Data.(*flippedData)

	 := exec.ExecSpan{
		Len:    .Len,
		Values: []exec.ExecValue{.Values[1], .Values[0]},
	}
	return .unflippedExec(, &, )
}

func makeFlippedCompare( string,  *compareFunction,  FunctionDoc) *compareFunction {
	 := &compareFunction{*NewScalarFunction(, Binary(), )}
	for ,  := range .kernels {
		 := 
		if .Data != nil {
			 := .Data.(*kernels.CompareData)
			.Data = &flippedData{CompareData: ,
				unflippedExec: .ExecFn}
		} else {
			.Data = &flippedData{unflippedExec: .ExecFn}
		}
		.ExecFn = flippedCompare
		.AddKernel()
	}
	return 
}

func ( FunctionRegistry) {
	 := &compareFunction{*NewScalarFunction("equal", Binary(), EmptyFuncDoc)}
	for ,  := range kernels.CompareKernels(kernels.CmpEQ) {
		if  := .AddKernel();  != nil {
			panic()
		}
	}
	.AddFunction(, false)

	 := &compareFunction{*NewScalarFunction("not_equal", Binary(), EmptyFuncDoc)}
	for ,  := range kernels.CompareKernels(kernels.CmpNE) {
		if  := .AddKernel();  != nil {
			panic()
		}
	}
	.AddFunction(, false)

	 := &compareFunction{*NewScalarFunction("greater", Binary(), EmptyFuncDoc)}
	for ,  := range kernels.CompareKernels(kernels.CmpGT) {
		if  := .AddKernel();  != nil {
			panic()
		}
	}
	.AddFunction(, false)

	 := &compareFunction{*NewScalarFunction("greater_equal", Binary(), EmptyFuncDoc)}
	for ,  := range kernels.CompareKernels(kernels.CmpGE) {
		if  := .AddKernel();  != nil {
			panic()
		}
	}
	.AddFunction(, false)

	 := makeFlippedCompare("less", , EmptyFuncDoc)
	.AddFunction(, false)
	 := makeFlippedCompare("less_equal", , EmptyFuncDoc)
	.AddFunction(, false)

	 := kernels.IsNullNotNullKernels()
	 := &compareFunction{*NewScalarFunction("is_null", Unary(), EmptyFuncDoc)}
	if  := .AddKernel([0]);  != nil {
		panic()
	}

	 := &compareFunction{*NewScalarFunction("is_not_null", Unary(), EmptyFuncDoc)}
	if  := .AddKernel([1]);  != nil {
		panic()
	}

	.AddFunction(, false)
	.AddFunction(, false)

	 := &compareFunction{*NewScalarFunction("is_nan", Unary(), EmptyFuncDoc)}
	for ,  := range kernels.IsNaNKernels() {
		if  := .AddKernel();  != nil {
			panic()
		}
	}
	.AddFunction(, false)
}