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

import (
	
	
	
)

type CastOptions struct {
	ToType               arrow.DataType `compute:"to_type"`
	AllowIntOverflow     bool           `compute:"allow_int_overflow"`
	AllowTimeTruncate    bool           `compute:"allow_time_truncate"`
	AllowTimeOverflow    bool           `compute:"allow_time_overflow"`
	AllowDecimalTruncate bool           `compute:"allow_decimal_truncate"`
	AllowFloatTruncate   bool           `compute:"allow_float_truncate"`
	AllowInvalidUtf8     bool           `compute:"allow_invalid_utf8"`
}

func (CastOptions) () string { return "CastOptions" }

// CastState is the kernel state for Cast functions, it is an alias to
// the CastOptions object.
type CastState = CastOptions

// ZeroCopyCastExec is a kernel for performing a cast which can be executed
// as a zero-copy operation. It simply forwards the buffers to the output.
//
// This can be used for casting a type to itself, or for casts between
// equivalent representations such as Int32 and Date32.
func ( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	.Release()
	 := .Type
	* = .Values[0].Array
	.Type = 
	return nil
}

func recursiveSetSelfAlloc( *exec.ArraySpan) {
	for  := range .Buffers {
		if len(.Buffers[].Buf) > 0 {
			.Buffers[].SelfAlloc = true
			if .Buffers[].Owner != nil {
				.Buffers[].Owner.Retain()
			}
		}
	}

	for  := range .Children {
		(&.Children[])
	}
}

// CastFromNull is a simple kernel for constructing an array of null values
// for the requested data type, allowing casting of an arrow.Null typed value
// to any other arbitrary data type.
func ( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	 := array.MakeArrayOfNull(exec.GetAllocator(.Ctx), .Type, int(.Len))
	defer .Release()

	.SetMembers(.Data())
	recursiveSetSelfAlloc()
	return nil
}

// OutputAllNull is a simple kernel that initializes the output as an array
// whose output is all null by setting nulls to the length.
func ( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	.Nulls = .Len
	return nil
}

func ( arrow.Type) bool {
	return arrow.IsPrimitive() || arrow.IsBaseBinary() || arrow.IsFixedSizeBinary()
}

// GetZeroCastKernel returns a kernel for performing ZeroCast execution using
// the ZeroCopyCastExec kernel function.
func ( arrow.Type,  exec.InputType,  exec.OutputType) exec.ScalarKernel {
	 := exec.NewScalarKernel([]exec.InputType{}, , ZeroCopyCastExec, nil)
	.NullHandling = exec.NullComputedNoPrealloc
	.MemAlloc = exec.MemNoPrealloc
	return 
}

// GetCommonCastKernels returns the list of kernels common to all types
// such as casting from null or from Extension types of the appropriate
// underlying type.
func ( arrow.Type,  exec.OutputType) ( []exec.ScalarKernel) {
	 = make([]exec.ScalarKernel, 0, 2)

	 := exec.NewScalarKernel([]exec.InputType{exec.NewExactInput(arrow.Null)}, ,
		CastFromNull, nil)
	.NullHandling = exec.NullComputedNoPrealloc
	.MemAlloc = exec.MemNoPrealloc
	 = append(, )

	return
}