/*
 * Licensed 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 io.trino.plugin.hive.coercions;

import io.trino.spi.block.Block;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;

import static io.airlift.slice.Slices.utf8Slice;

public class IntegerNumberToVarcharCoercer<F extends Type>
        extends TypeCoercer<F, VarcharType>
{
    public IntegerNumberToVarcharCoercer(F fromType, VarcharType toType)
    {
        super(fromType, toType);
    }

    @Override
    protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position)
    {
        toType.writeSlice(blockBuilder, utf8Slice(String.valueOf(fromType.getLong(block, position))));
    }
}

@@ -597,16 +597,83 @@
 cions;%0A%0A
+import io.airlift.slice.Slice;%0Aimport io.trino.spi.TrinoException;%0A
 import i
@@ -847,19 +847,180 @@
 lice
-s.utf8Slice
+Utf8.countCodePoints;%0Aimport static io.airlift.slice.Slices.utf8Slice;%0Aimport static io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS;%0Aimport static java.lang.String.format
 ;%0A%0Ap
@@ -1365,100 +1365,399 @@
     
-toType.writeSlice(blockBuilder, utf8Slice(String.valueOf(fromType.getLong(block, position)))
+long value = fromType.getLong(block, position);%0A        Slice converted = utf8Slice(String.valueOf(value));%0A        if (!toType.isUnbounded() && countCodePoints(converted) %3E toType.getBoundedLength()) %7B%0A            throw new TrinoException(INVALID_ARGUMENTS, format(%22Varchar representation of %25s exceed %25s bounds%22, value, toType));%0A        %7D%0A        toType.writeSlice(blockBuilder, converted
 );%0A 

Handle overflow in IntegerNumberToVarcharCoercer