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

	
	
	
	
	
	
)

var (
	castTable map[arrow.Type]*castFunction
	castInit  sync.Once

	castDoc = FunctionDoc{
		Summary:         "cast values to another data type",
		Description:     "Behavior when values wouldn't fit in the target type\ncan be controlled through CastOptions.",
		ArgNames:        []string{"input"},
		OptionsType:     "CastOptions",
		OptionsRequired: true,
	}
	castMetaFunc = NewMetaFunction("cast", Unary(), castDoc,
		func( context.Context,  FunctionOptions,  ...Datum) (Datum, error) {
			 := .(*CastOptions)
			if  == nil || .ToType == nil {
				return nil, fmt.Errorf("%w: cast requires that options be passed with a ToType", arrow.ErrInvalid)
			}

			if arrow.TypeEqual([0].(ArrayLikeDatum).Type(), .ToType) {
				return NewDatum([0]), nil
			}

			// Coalesce multi-buffer view inputs. exec.ArraySpan's fixed
			// [3]BufferSpan cannot carry BinaryView/StringView arrays with
			// more than one overflow data buffer, so SetMembers would panic
			// before any kernel runs. Rebuild such inputs with a single data
			// buffer up front.
			,  := coalesceMultiBufferViewDatum(, [0])
			if  != nil {
				return nil, 
			}
			if  != nil {
				defer .Release()
				 = append([]Datum(nil), ...)
				[0] = 
			}

			,  := getCastFunction(.ToType)
			if  != nil {
				return nil, fmt.Errorf("%w from %s", , [0].(ArrayLikeDatum).Type())
			}

			return .Execute(, , ...)
		})
)

func ( FunctionRegistry) {
	.AddFunction(castMetaFunc, false)
}

// coalesceMultiBufferViewDatum returns a new Datum whose view arrays each
// have a single overflow data buffer, or nil if the input does not need
// coalescing. Both ArrayDatum and ChunkedDatum view inputs are handled,
// including dictionaries whose values are multi-buffer view arrays (the
// recursive check matches what exec.ArraySpan.SetMembers traverses).
// Other datums are never coalesced. Callers are responsible for
// releasing the returned datum when it is non-nil.
func coalesceMultiBufferViewDatum( context.Context,  Datum) (Datum, error) {
	switch v := .(type) {
	case *ArrayDatum:
		if !needsViewCoalesce(.Value) {
			return nil, nil
		}
		 := exec.GetAllocator()
		,  := coalesceArrayData(, .Value)
		if  != nil {
			return nil, 
		}
		return &ArrayDatum{Value: }, nil

	case *ChunkedDatum:
		 := .Value.Chunks()
		 := false
		for ,  := range  {
			if needsViewCoalesce(.Data()) {
				 = true
				break
			}
		}
		if ! {
			return nil, nil
		}
		 := exec.GetAllocator()
		 := make([]arrow.Array, len())
		for ,  := range  {
			if !needsViewCoalesce(.Data()) {
				.Retain()
				[] = 
				continue
			}
			,  := coalesceArrayData(, .Data())
			if  != nil {
				for  := 0;  < ; ++ {
					[].Release()
				}
				return nil, 
			}
			[] = array.MakeFromData()
			.Release()
		}
		 := arrow.NewChunked(.Value.DataType(), )
		for ,  := range  {
			.Release()
		}
		return &ChunkedDatum{Value: }, nil
	}
	return nil, nil
}

// needsViewCoalesce reports whether data carries a view array whose
// payload spans more than the single overflow data buffer exec.ArraySpan
// can carry. The check recurses into dictionary values and ordinary
// child arrays because exec.ArraySpan.SetMembers recursively spans both;
// extension inputs reuse their storage layout in place, so they are
// treated as their StorageType for this traversal.
func needsViewCoalesce( arrow.ArrayData) bool {
	 := .DataType()
	if ,  := .(arrow.ExtensionType);  {
		 = .StorageType()
	}
	switch .ID() {
	case arrow.BINARY_VIEW, arrow.STRING_VIEW:
		return len(.Buffers()) > 3
	case arrow.DICTIONARY:
		return (.Dictionary())
	}
	for ,  := range .Children() {
		if () {
			return true
		}
	}
	return false
}

// coalesceArrayData rebuilds data so that every view descendant lives in
// a single overflow buffer. View arrays are rebuilt via
// rebuildViewSingleBuffer; dictionaries keep their index buffers and
// recursively rebuild their values; nested types (list, struct, etc.)
// keep their own buffers and recursively rebuild each child. Extension
// inputs are rebuilt at their storage layer and then re-wrapped so the
// original extension datatype survives. Shares already-compliant
// children by retaining them rather than copying.
func coalesceArrayData( memory.Allocator,  arrow.ArrayData) (arrow.ArrayData, error) {
	if ,  := .DataType().(arrow.ExtensionType);  {
		,  := reshapeArrayDataType(, .StorageType())
		if  != nil {
			return nil, 
		}
		defer .Release()
		,  := (, )
		if  != nil {
			return nil, 
		}
		defer .Release()
		return reshapeArrayDataType(, .DataType())
	}

	switch .DataType().ID() {
	case arrow.BINARY_VIEW, arrow.STRING_VIEW:
		return rebuildViewSingleBuffer(, )
	case arrow.DICTIONARY:
		,  := (, .Dictionary())
		if  != nil {
			return nil, 
		}
		defer .Release()
		,  := .(*array.Data)
		if ! {
			return nil, fmt.Errorf("%w: unexpected dictionary values data type %T", arrow.ErrInvalid, )
		}
		return array.NewDataWithDictionary(.DataType(), .Len(),
			.Buffers(), .NullN(), .Offset(), ), nil
	}

	 := .Children()
	if len() == 0 {
		return nil, fmt.Errorf("%w: coalesceArrayData: no view descendants in %s", arrow.ErrInvalid, .DataType())
	}
	 := make([]arrow.ArrayData, len())
	for ,  := range  {
		if !needsViewCoalesce() {
			.Retain()
			[] = 
			continue
		}
		,  := (, )
		if  != nil {
			for  := 0;  < ; ++ {
				[].Release()
			}
			return nil, 
		}
		[] = 
	}
	 := array.NewData(.DataType(), .Len(), .Buffers(),
		, .NullN(), .Offset())
	for ,  := range  {
		.Release()
	}
	return , nil
}

// reshapeArrayDataType returns a new ArrayData that shares src's buffers,
// children, nulls, offset, and dictionary but reports the given datatype.
// Used by the extension unwrap/rewrap path in coalesceArrayData so the
// Dictionary() member survives both directions (extension<->storage)
// when the extension's storage type is a dictionary.
func reshapeArrayDataType( arrow.ArrayData,  arrow.DataType) (arrow.ArrayData, error) {
	if  := .Dictionary();  != nil {
		,  := .(*array.Data)
		if ! {
			return nil, fmt.Errorf("%w: unexpected dictionary data type %T", arrow.ErrInvalid, )
		}
		return array.NewDataWithDictionary(, .Len(), .Buffers(),
			.NullN(), .Offset(), ), nil
	}
	return array.NewData(, .Len(), .Buffers(),
		.Children(), .NullN(), .Offset()), nil
}

// rebuildViewSingleBuffer rebuilds a BinaryView/StringView ArrayData so that
// all non-inline payload lives in a single contiguous overflow buffer. The
// caller owns the returned ArrayData.
func rebuildViewSingleBuffer( memory.Allocator,  arrow.ArrayData) (arrow.ArrayData, error) {
	 := array.MakeFromData()
	defer .Release()

	var (
		  func(int) int
		 func( int,  func([]byte))
	)
	switch a := .(type) {
	case *array.BinaryView:
		 = .ValueLen
		 = func( int,  func([]byte)) { (.Value()) }
	case *array.StringView:
		 = .ValueLen
		 = func( int,  func([]byte)) {
			 := .Value()
			([]byte())
		}
	default:
		return nil, fmt.Errorf("%w: unexpected view array type %T", arrow.ErrInvalid, )
	}
	,  := kernels.SumOutOfLineBytes(.Len(), .IsNull, )
	if  != nil {
		return nil, 
	}

	 := array.NewBuilder(, .DataType())
	defer .Release()
	.Reserve(.Len())

	switch b := .(type) {
	case *array.BinaryViewBuilder:
		if  > 0 {
			.ReserveData(int())
		}
	case *array.StringViewBuilder:
		if  > 0 {
			.ReserveData(int())
		}
	default:
		return nil, fmt.Errorf("%w: unexpected view builder type %T", arrow.ErrInvalid, )
	}

	 := func([]byte) {}
	switch b := .(type) {
	case *array.BinaryViewBuilder:
		 = .Append
	case *array.StringViewBuilder:
		 = .BinaryViewBuilder.Append
	}

	for  := 0;  < .Len(); ++ {
		if .IsNull() {
			.AppendNull()
			continue
		}
		(, )
	}

	 := .NewArray()
	defer .Release()
	 := .Data()
	.Retain()
	return , nil
}

type castFunction struct {
	ScalarFunction

	inIDs []arrow.Type
	out   arrow.Type
}

func newCastFunction( string,  arrow.Type) *castFunction {
	return &castFunction{
		ScalarFunction: *NewScalarFunction(, Unary(), EmptyFuncDoc),
		out:            ,
		inIDs:          make([]arrow.Type, 0, 1),
	}
}

func ( *castFunction) ( arrow.Type,  exec.ScalarKernel) error {
	.Init = exec.OptionsInit[kernels.CastState]
	if  := .AddKernel();  != nil {
		return 
	}
	.inIDs = append(.inIDs, )
	return nil
}

func ( *castFunction) ( arrow.Type,  []exec.InputType,  exec.OutputType,
	 exec.ArrayKernelExec,  exec.NullHandling,  exec.MemAlloc) error {

	 := exec.NewScalarKernel(, , , nil)
	.NullHandling = 
	.MemAlloc = 
	return .AddTypeCast(, )
}

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

	 := make([]*exec.ScalarKernel, 0, 1)
	for  := range .kernels {
		if .kernels[].Signature.MatchesInputs() {
			 = append(, &.kernels[])
		}
	}

	if len() == 0 {
		return nil, fmt.Errorf("%w: unsupported cast from %s to %s using function %s",
			arrow.ErrNotImplemented, [0], .out, .name)
	}

	if len() == 1 {
		// one match!
		return [0], nil
	}

	// in this situation we may have both an EXACT type and
	// a SAME_TYPE_ID match. So we will see if there is an exact
	// match among the candidates and if not, we just return the
	// first one
	for ,  := range  {
		 := .Signature.InputTypes[0]
		if .Kind == exec.InputExact {
			// found one!
			return , nil
		}
	}

	// just return some kernel that matches since we didn't find an exact
	return [0], nil
}

func unpackDictionary( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	var (
		  = .Values[0].Array.MakeArray().(*array.Dictionary)
		     = .State.(kernels.CastState)
		 = .DataType().(*arrow.DictionaryType)
		   = .ToType
	)
	defer .Release()

	if !arrow.TypeEqual(, ) && !CanCast(, ) {
		return fmt.Errorf("%w: cast type %s incompatible with dictionary type %s",
			arrow.ErrInvalid, , )
	}

	var (
		 arrow.Array
		      error
	)
	switch .Dictionary().DataType().ID() {
	case arrow.STRING_VIEW, arrow.BINARY_VIEW:
		// array_take has no view kernel, so unpack view-typed dictionaries
		// directly into a fresh view array instead of going through TakeArray.
		,  = unpackViewDictionary(exec.GetAllocator(.Ctx), )
	default:
		,  = TakeArray(.Ctx, .Dictionary(), .Indices())
	}
	if  != nil {
		return 
	}
	defer .Release()

	if !arrow.TypeEqual(, ) {
		,  = CastArray(.Ctx, , &)
		if  != nil {
			return 
		}
		defer .Release()
	}

	.TakeOwnership(.Data())
	return nil
}

// unpackViewDictionary materializes a dictionary whose values are a view
// type (string_view or binary_view) into a flat view array of the same
// type, preserving per-element nulls from the dictionary indices and
// per-slot nulls from the dictionary values themselves. Out-of-line
// payload is pre-reserved on the builder so the resulting array lives
// in a single overflow data buffer; exec.ArraySpan's fixed [3]BufferSpan
// cannot carry view arrays that span multiple data buffers. This exists
// because array_take has no view kernel; the cast meta function still
// needs to expand dictionaries as part of DICTIONARY -> X casts.
func unpackViewDictionary( memory.Allocator,  *array.Dictionary) (arrow.Array, error) {
	switch vals := .Dictionary().(type) {
	case *array.StringView:
		 := array.NewStringViewBuilder()
		defer .Release()
		if  := unpackViewDictionaryIntoBuilder(, , .ValueLen, viewDictBuilderAdapter{
			builder:     ,
			reserveData: .ReserveData,
			appendValue: func( int) { .Append(.Value()) },
			appendNull:  .AppendNull,
		});  != nil {
			return nil, 
		}
		return .NewArray(), nil
	case *array.BinaryView:
		 := array.NewBinaryViewBuilder()
		defer .Release()
		if  := unpackViewDictionaryIntoBuilder(, , .ValueLen, viewDictBuilderAdapter{
			builder:     ,
			reserveData: .ReserveData,
			appendValue: func( int) { .Append(.Value()) },
			appendNull:  .AppendNull,
		});  != nil {
			return nil, 
		}
		return .NewArray(), nil
	default:
		return nil, fmt.Errorf("%w: unpackViewDictionary: expected view-typed dictionary values, got %s",
			arrow.ErrInvalid, .DataType())
	}
}

// viewValuesNull is the null-check half of the view dictionary values
// interface; exposed separately because *array.StringView and
// *array.BinaryView both satisfy it while their Value() return types
// differ (string vs []byte) and cannot be expressed in one interface.
type viewValuesNull interface {
	IsNull(int) bool
}

// viewDictBuilderAdapter packages the concrete builder + typed closures
// needed by unpackViewDictionaryIntoBuilder; boxing them keeps the
// helper's parameter list tight and lets each caller express the
// builder-specific Append in one place.
type viewDictBuilderAdapter struct {
	builder     array.Builder
	reserveData func(int)
	appendValue func(idx int)
	appendNull  func()
}

// unpackViewDictionaryIntoBuilder runs the shared reserve/walk pipeline
// for both view-typed dictionary branches of unpackViewDictionary. The
// caller owns the builder lifecycle and supplies the adapter so Append
// can stay typed to the concrete builder (StringViewBuilder.Append takes
// string, BinaryViewBuilder.Append takes []byte).
func unpackViewDictionaryIntoBuilder( *array.Dictionary,  viewValuesNull,  func(int) int,  viewDictBuilderAdapter) error {
	,  := kernels.SumOutOfLineBytes(.Len(),
		func( int) bool {
			if .IsNull() {
				return true
			}
			return .IsNull(.GetValueIndex())
		},
		func( int) int { return (.GetValueIndex()) },
	)
	if  != nil {
		return 
	}
	.builder.Reserve(.Len())
	if  > 0 {
		.reserveData(int())
	}
	buildFromDictionary(, .IsNull, .appendValue, .appendNull)
	return nil
}

// buildFromDictionary walks dictArr and routes each position to either
// appendValue(valueIndex) or appendNull(). A position is null when the
// dictionary index itself is null or when the referenced value is null.
func buildFromDictionary( *array.Dictionary,  func(int) bool,  func( int),  func()) {
	for  := 0;  < .Len(); ++ {
		if .IsNull() {
			()
			continue
		}
		 := .GetValueIndex()
		if () {
			()
			continue
		}
		()
	}
}

func ( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	 := .State.(kernels.CastState)

	 := .Values[0].Array.MakeArray().(array.ExtensionArray)
	defer .Release()

	 := CastOptions()
	,  := CastArray(.Ctx, .Storage(), &)
	if  != nil {
		return 
	}
	defer .Release()

	.TakeOwnership(.Data())
	return nil
}

func [,  int32 | int64]( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	var (
		       = .State.(kernels.CastState)
		  = .Type.(arrow.NestedType).Fields()[0].Type
		      = &.Values[0].Array
		    = exec.GetSpanOffsets[](, 1)
		 = kernels.SizeOf[]() > kernels.SizeOf[]()
	)

	.Buffers[0] = .Buffers[0]
	.Buffers[1] = .Buffers[1]

	if .Offset != 0 && len(.Buffers[0].Buf) > 0 {
		.Buffers[0].WrapBuffer(.AllocateBitmap(.Len))
		bitutil.CopyBitmap(.Buffers[0].Buf, int(.Offset), int(.Len),
			.Buffers[0].Buf, 0)
	}

	// Handle list offsets
	// Several cases possible:
	//	- The source offset is non-zero, in which case we slice the
	//	  underlying values and shift the list offsets (regardless of
	//	  their respective types)
	//	- the source offset is zero but the source and destination types
	//	  have different list offset types, in which case we cast the offsets
	//  - otherwise we simply keep the original offsets
	if  {
		if [.Len] > (kernels.MaxOf[]()) {
			return fmt.Errorf("%w: array of type %s too large to convert to %s",
				arrow.ErrInvalid, .Type, .Type)
		}
	}

	 := .Children[0].MakeArray()
	defer .Release()

	if .Offset != 0 {
		.Buffers[1].WrapBuffer(
			.Allocate(.Type.(arrow.OffsetsDataType).
				OffsetTypeTraits().BytesRequired(int(.Len) + 1)))

		 := exec.GetSpanOffsets[](, 1)
		for  := 0;  < int(.Len)+1; ++ {
			[] = ([] - [0])
		}

		 = array.NewSlice(, int64([0]), int64([.Len]))
		defer .Release()
	} else if kernels.SizeOf[]() != kernels.SizeOf[]() {
		.Buffers[1].WrapBuffer(.Allocate(.Type.(arrow.OffsetsDataType).
			OffsetTypeTraits().BytesRequired(int(.Len) + 1)))

		kernels.DoStaticCast(exec.GetSpanOffsets[](, 1),
			exec.GetSpanOffsets[](, 1))
	}

	// handle values
	.ToType = 

	,  := CastArray(.Ctx, , &)
	if  != nil {
		return 
	}
	defer .Release()

	.Children = make([]exec.ArraySpan, 1)
	.Children[0].SetMembers(.Data())
	for ,  := range .Children[0].Buffers {
		if .Owner != nil && .Owner != .Data().Buffers()[] {
			.Owner.Retain()
			.SelfAlloc = true
		}
	}
	return nil
}

func ( *exec.KernelCtx,  *exec.ExecSpan,  *exec.ExecResult) error {
	var (
		          = .State.(kernels.CastState)
		        = .Values[0].Array.Type.(*arrow.StructType)
		       = .Type.(*arrow.StructType)
		  = .NumFields()
		 = .NumFields()
	)

	 := make([]int, )
	for  := range  {
		[] = -1
	}

	 := 0
	for  := 0;  <  &&  < ; ++ {
		 := .Field()
		 := .Field()
		if .Name == .Name {
			[] = 
			++
		}
	}

	if  <  {
		return fmt.Errorf("%w: struct fields don't match or are in the wrong order: Input: %s Output: %s",
			arrow.ErrType, , )
	}

	 := &.Values[0].Array
	if len(.Buffers[0].Buf) > 0 {
		.Buffers[0].WrapBuffer(.AllocateBitmap(.Len))
		bitutil.CopyBitmap(.Buffers[0].Buf, int(.Offset), int(.Len),
			.Buffers[0].Buf, 0)
	}

	.ResizeChildren()
	for ,  := range  {
		 := .Children[].MakeArray()
		defer .Release()
		 = array.NewSlice(, .Offset, .Len)
		defer .Release()

		,  := .Field(), .Field()
		if .Nullable && !.Nullable && .NullN() > 0 {
			return fmt.Errorf("%w: field '%s' of type %s has nulls. Can't cast to non-nullable field '%s' of type %s",
				arrow.ErrInvalid, .Name, .Type, .Name, .Type)
		}

		.ToType = .Type
		,  := CastArray(.Ctx, , &)
		if  != nil {
			return 
		}
		defer .Release()

		.Children[].TakeOwnership(.Data())
	}
	return nil
}

func addListCast[,  int32 | int64]( *castFunction,  arrow.Type) error {
	 := exec.NewScalarKernel([]exec.InputType{exec.NewIDInput()},
		kernels.OutputTargetType, CastList[, ], nil)
	.NullHandling = exec.NullComputedNoPrealloc
	.MemAlloc = exec.MemNoPrealloc
	return .AddTypeCast(, )
}

func addStructToStructCast( *castFunction) error {
	 := exec.NewScalarKernel([]exec.InputType{exec.NewIDInput(arrow.STRUCT)},
		kernels.OutputTargetType, CastStruct, nil)
	.NullHandling = exec.NullComputedNoPrealloc
	return .AddTypeCast(arrow.STRUCT, )
}

func addCastFuncs( []*castFunction) {
	for ,  := range  {
		.AddNewTypeCast(arrow.EXTENSION, []exec.InputType{exec.NewIDInput(arrow.EXTENSION)},
			.kernels[0].Signature.OutType, CastFromExtension,
			exec.NullComputedNoPrealloc, exec.MemNoPrealloc)
		castTable[.out] = 
	}
}

func initCastTable() {
	castTable = make(map[arrow.Type]*castFunction)
	addCastFuncs(getBooleanCasts())
	addCastFuncs(getNumericCasts())
	addCastFuncs(getBinaryLikeCasts())
	addCastFuncs(getTemporalCasts())
	addCastFuncs(getNestedCasts())

	 := newCastFunction("cast_extension", arrow.EXTENSION)
	.AddNewTypeCast(arrow.NULL, []exec.InputType{exec.NewExactInput(arrow.Null)},
		kernels.OutputTargetType, kernels.CastFromNull, exec.NullComputedNoPrealloc, exec.MemNoPrealloc)
	castTable[arrow.EXTENSION] = 
}

func getCastFunction( arrow.DataType) (*castFunction, error) {
	castInit.Do(initCastTable)

	,  := castTable[.ID()]
	if  {
		return , nil
	}

	return nil, fmt.Errorf("%w: unsupported cast to %s", arrow.ErrNotImplemented, )
}

func getNestedCasts() []*castFunction {
	 := make([]*castFunction, 0)

	 := func( *castFunction,  []exec.ScalarKernel) {
		for ,  := range  {
			if  := .AddTypeCast(.Signature.InputTypes[0].MatchID(), );  != nil {
				panic()
			}
		}
	}

	 := newCastFunction("cast_list", arrow.LIST)
	(, kernels.GetCommonCastKernels(arrow.LIST, kernels.OutputTargetType))
	if  := addListCast[int32, int32](, arrow.LIST);  != nil {
		panic()
	}
	if  := addListCast[int64, int32](, arrow.LARGE_LIST);  != nil {
		panic()
	}
	 = append(, )

	 := newCastFunction("cast_large_list", arrow.LARGE_LIST)
	(, kernels.GetCommonCastKernels(arrow.LARGE_LIST, kernels.OutputTargetType))
	if  := addListCast[int32, int64](, arrow.LIST);  != nil {
		panic()
	}
	if  := addListCast[int64, int64](, arrow.LARGE_LIST);  != nil {
		panic()
	}
	 = append(, )

	 := newCastFunction("cast_fixed_size_list", arrow.FIXED_SIZE_LIST)
	(, kernels.GetCommonCastKernels(arrow.FIXED_SIZE_LIST, kernels.OutputTargetType))
	 = append(, )

	 := newCastFunction("cast_struct", arrow.STRUCT)
	(, kernels.GetCommonCastKernels(arrow.STRUCT, kernels.OutputTargetType))
	if  := addStructToStructCast();  != nil {
		panic()
	}
	 = append(, )

	return 
}

func getBooleanCasts() []*castFunction {
	 := newCastFunction("cast_boolean", arrow.BOOL)
	 := kernels.GetBooleanCastKernels()

	for ,  := range  {
		if  := .AddTypeCast(.Signature.InputTypes[0].Type.ID(), );  != nil {
			panic()
		}
	}

	return []*castFunction{}
}

func getTemporalCasts() []*castFunction {
	 := make([]*castFunction, 0)
	 := func( string,  arrow.Type,  []exec.ScalarKernel) {
		 := newCastFunction(, )
		for ,  := range  {
			if  := .AddTypeCast(.Signature.InputTypes[0].MatchID(), );  != nil {
				panic()
			}
		}
		.AddNewTypeCast(arrow.DICTIONARY, []exec.InputType{exec.NewIDInput(arrow.DICTIONARY)},
			[0].Signature.OutType, unpackDictionary, exec.NullComputedNoPrealloc, exec.MemNoPrealloc)
		 = append(, )
	}

	("cast_timestamp", arrow.TIMESTAMP, kernels.GetTimestampCastKernels())
	("cast_date32", arrow.DATE32, kernels.GetDate32CastKernels())
	("cast_date64", arrow.DATE64, kernels.GetDate64CastKernels())
	("cast_time32", arrow.TIME32, kernels.GetTime32CastKernels())
	("cast_time64", arrow.TIME64, kernels.GetTime64CastKernels())
	("cast_duration", arrow.DURATION, kernels.GetDurationCastKernels())
	("cast_month_day_nano_interval", arrow.INTERVAL_MONTH_DAY_NANO, kernels.GetIntervalCastKernels())
	return 
}

func getNumericCasts() []*castFunction {
	 := make([]*castFunction, 0)

	 := func( string,  arrow.Type,  []exec.ScalarKernel) *castFunction {
		 := newCastFunction(, )
		for ,  := range  {
			if  := .AddTypeCast(.Signature.InputTypes[0].MatchID(), );  != nil {
				panic()
			}
		}

		.AddNewTypeCast(arrow.DICTIONARY, []exec.InputType{exec.NewIDInput(arrow.DICTIONARY)},
			[0].Signature.OutType, unpackDictionary, exec.NullComputedNoPrealloc, exec.MemNoPrealloc)

		return 
	}

	 = append(, ("cast_int8", arrow.INT8, kernels.GetCastToInteger[int8](arrow.PrimitiveTypes.Int8)))
	 = append(, ("cast_int16", arrow.INT16, kernels.GetCastToInteger[int16](arrow.PrimitiveTypes.Int16)))

	 := ("cast_int32", arrow.INT32, kernels.GetCastToInteger[int32](arrow.PrimitiveTypes.Int32))
	.AddTypeCast(arrow.DATE32,
		kernels.GetZeroCastKernel(arrow.DATE32,
			exec.NewExactInput(arrow.FixedWidthTypes.Date32),
			exec.NewOutputType(arrow.PrimitiveTypes.Int32)))
	.AddTypeCast(arrow.TIME32,
		kernels.GetZeroCastKernel(arrow.TIME32,
			exec.NewIDInput(arrow.TIME32), exec.NewOutputType(arrow.PrimitiveTypes.Int32)))
	 = append(, )

	 := ("cast_int64", arrow.INT64, kernels.GetCastToInteger[int64](arrow.PrimitiveTypes.Int64))
	.AddTypeCast(arrow.DATE64,
		kernels.GetZeroCastKernel(arrow.DATE64,
			exec.NewIDInput(arrow.DATE64),
			exec.NewOutputType(arrow.PrimitiveTypes.Int64)))
	.AddTypeCast(arrow.TIME64,
		kernels.GetZeroCastKernel(arrow.TIME64,
			exec.NewIDInput(arrow.TIME64),
			exec.NewOutputType(arrow.PrimitiveTypes.Int64)))
	.AddTypeCast(arrow.DURATION,
		kernels.GetZeroCastKernel(arrow.DURATION,
			exec.NewIDInput(arrow.DURATION),
			exec.NewOutputType(arrow.PrimitiveTypes.Int64)))
	.AddTypeCast(arrow.TIMESTAMP,
		kernels.GetZeroCastKernel(arrow.TIMESTAMP,
			exec.NewIDInput(arrow.TIMESTAMP),
			exec.NewOutputType(arrow.PrimitiveTypes.Int64)))
	 = append(, )

	 = append(, ("cast_uint8", arrow.UINT8, kernels.GetCastToInteger[uint8](arrow.PrimitiveTypes.Uint8)))
	 = append(, ("cast_uint16", arrow.UINT16, kernels.GetCastToInteger[uint16](arrow.PrimitiveTypes.Uint16)))
	 = append(, ("cast_uint32", arrow.UINT32, kernels.GetCastToInteger[uint32](arrow.PrimitiveTypes.Uint32)))
	 = append(, ("cast_uint64", arrow.UINT64, kernels.GetCastToInteger[uint64](arrow.PrimitiveTypes.Uint64)))

	 = append(, ("cast_half_float", arrow.FLOAT16, kernels.GetCommonCastKernels(arrow.FLOAT16, exec.NewOutputType(arrow.FixedWidthTypes.Float16))))
	 = append(, ("cast_float", arrow.FLOAT32, kernels.GetCastToFloating[float32](arrow.PrimitiveTypes.Float32)))
	 = append(, ("cast_double", arrow.FLOAT64, kernels.GetCastToFloating[float64](arrow.PrimitiveTypes.Float64)))

	// cast to decimal128
	 = append(, ("cast_decimal", arrow.DECIMAL128, kernels.GetCastToDecimal128()))
	// cast to decimal256
	 = append(, ("cast_decimal256", arrow.DECIMAL256, kernels.GetCastToDecimal256()))
	return 
}

func getBinaryLikeCasts() []*castFunction {
	 := make([]*castFunction, 0)

	 := func( string,  arrow.Type,  []exec.ScalarKernel) {
		 := newCastFunction(, )
		for ,  := range  {
			if  := .AddTypeCast(.Signature.InputTypes[0].MatchID(), );  != nil {
				panic()
			}
		}

		.AddNewTypeCast(arrow.DICTIONARY, []exec.InputType{exec.NewIDInput(arrow.DICTIONARY)},
			[0].Signature.OutType, unpackDictionary, exec.NullComputedNoPrealloc, exec.MemNoPrealloc)

		 = append(, )
	}

	("cast_binary", arrow.BINARY, kernels.GetToBinaryKernels(arrow.BinaryTypes.Binary))
	("cast_large_binary", arrow.LARGE_BINARY, kernels.GetToBinaryKernels(arrow.BinaryTypes.LargeBinary))
	("cast_string", arrow.STRING, kernels.GetToBinaryKernels(arrow.BinaryTypes.String))
	("cast_large_string", arrow.LARGE_STRING, kernels.GetToBinaryKernels(arrow.BinaryTypes.LargeString))
	("cast_binary_view", arrow.BINARY_VIEW, kernels.GetToBinaryKernels(arrow.BinaryTypes.BinaryView))
	("cast_string_view", arrow.STRING_VIEW, kernels.GetToBinaryKernels(arrow.BinaryTypes.StringView))
	("cast_fixed_sized_binary", arrow.FIXED_SIZE_BINARY, kernels.GetFsbCastKernels())
	return 
}

// CastDatum is a convenience function for casting a Datum to another type.
// It is equivalent to calling CallFunction(ctx, "cast", opts, Datum) and
// should work for Scalar, Array or ChunkedArray Datums.
func ( context.Context,  Datum,  *CastOptions) (Datum, error) {
	return CallFunction(, "cast", , )
}

// CastArray is a convenience function for casting an Array to another type.
// It is equivalent to constructing a Datum for the array and using
// CallFunction(ctx, "cast", ...).
func ( context.Context,  arrow.Array,  *CastOptions) (arrow.Array, error) {
	 := NewDatum()
	defer .Release()

	,  := CastDatum(, , )
	if  != nil {
		return nil, 
	}

	defer .Release()
	return .(*ArrayDatum).MakeArray(), nil
}

// CastToType is a convenience function equivalent to calling
// CastArray(ctx, val, compute.SafeCastOptions(toType))
func ( context.Context,  arrow.Array,  arrow.DataType) (arrow.Array, error) {
	return CastArray(, , SafeCastOptions())
}

// CanCast returns true if there is an implementation for casting an array
// or scalar value from the specified DataType to the other data type.
func (,  arrow.DataType) bool {
	,  := getCastFunction()
	if  != nil {
		return false
	}

	for ,  := range .inIDs {
		if .ID() ==  {
			return true
		}
	}
	return false
}