<commit_before>/*
 * 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.phoenix.pig.util;

import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.phoenix.pig.writable.PhoenixPigDBWritable;
import org.apache.phoenix.schema.types.PBinary;
import org.apache.phoenix.schema.types.PBoolean;
import org.apache.phoenix.schema.types.PChar;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PDate;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
import org.apache.phoenix.schema.types.PFloat;
import org.apache.phoenix.schema.types.PInteger;
import org.apache.phoenix.schema.types.PLong;
import org.apache.phoenix.schema.types.PSmallint;
import org.apache.phoenix.schema.types.PTime;
import org.apache.phoenix.schema.types.PTimestamp;
import org.apache.phoenix.schema.types.PTinyint;
import org.apache.phoenix.schema.types.PUnsignedDate;
import org.apache.phoenix.schema.types.PUnsignedDouble;
import org.apache.phoenix.schema.types.PUnsignedFloat;
import org.apache.phoenix.schema.types.PUnsignedInt;
import org.apache.phoenix.schema.types.PUnsignedLong;
import org.apache.phoenix.schema.types.PUnsignedSmallint;
import org.apache.phoenix.schema.types.PUnsignedTime;
import org.apache.phoenix.schema.types.PUnsignedTimestamp;
import org.apache.phoenix.schema.types.PUnsignedTinyint;
import org.apache.phoenix.schema.types.PVarbinary;
import org.apache.phoenix.schema.types.PVarchar;
import org.apache.pig.PigException;
import org.apache.pig.ResourceSchema.ResourceFieldSchema;
import org.apache.pig.backend.hadoop.hbase.HBaseBinaryConverter;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
import org.joda.time.DateTime;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public final class TypeUtil {

    private static final Log LOG = LogFactory.getLog(TypeUtil.class);
    private static final HBaseBinaryConverter BINARY_CONVERTER = new HBaseBinaryConverter();
    private static final ImmutableMap<PDataType, Byte> PHOENIX_TO_PIG_TYPE = init();
    private static final TupleFactory TUPLE_FACTORY = TupleFactory.getInstance();

    private TypeUtil() {}

    /**
     * A map of Phoenix to Pig data types.
     * 
     * @return
     */
    private static ImmutableMap<PDataType, Byte> init() {
        final ImmutableMap.Builder<PDataType, Byte> builder = new Builder<PDataType, Byte>();
        builder.put(PLong.INSTANCE, DataType.LONG);
        builder.put(PVarbinary.INSTANCE, DataType.BYTEARRAY);
        builder.put(PChar.INSTANCE, DataType.CHARARRAY);
        builder.put(PVarchar.INSTANCE, DataType.CHARARRAY);
        builder.put(PDouble.INSTANCE, DataType.DOUBLE);
        builder.put(PFloat.INSTANCE, DataType.FLOAT);
        builder.put(PInteger.INSTANCE, DataType.INTEGER);
        builder.put(PTinyint.INSTANCE, DataType.INTEGER);
        builder.put(PSmallint.INSTANCE, DataType.INTEGER);
        builder.put(PDecimal.INSTANCE, DataType.BIGDECIMAL);
        builder.put(PTime.INSTANCE, DataType.DATETIME);
        builder.put(PTimestamp.INSTANCE, DataType.DATETIME);
        builder.put(PBoolean.INSTANCE, DataType.BOOLEAN);
        builder.put(PDate.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedDate.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedDouble.INSTANCE, DataType.DOUBLE);
        builder.put(PUnsignedFloat.INSTANCE, DataType.FLOAT);
        builder.put(PUnsignedInt.INSTANCE, DataType.INTEGER);
        builder.put(PUnsignedLong.INSTANCE, DataType.LONG);
        builder.put(PUnsignedSmallint.INSTANCE, DataType.INTEGER);
        builder.put(PUnsignedTime.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedTimestamp.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedTinyint.INSTANCE, DataType.INTEGER);
        return builder.build();
    }

    /**
     * This method returns the most appropriate PDataType associated with the incoming Pig type. Note for Pig DataType
     * DATETIME, returns DATE as inferredSqlType. This is later used to make a cast to targetPhoenixType accordingly.
     * See {@link #castPigTypeToPhoenix(Object, byte, PDataType)}
     * 
     * @param obj
     * @return PDataType
     */
    public static PDataType getType(Object obj, byte type) {
        if (obj == null) { return null; }
        PDataType sqlType;

        switch (type) {
        case DataType.BYTEARRAY:
            sqlType = PVarbinary.INSTANCE;
            break;
        case DataType.CHARARRAY:
            sqlType = PVarchar.INSTANCE;
            break;
        case DataType.DOUBLE:
        case DataType.BIGDECIMAL:
            sqlType = PDouble.INSTANCE;
            break;
        case DataType.FLOAT:
            sqlType = PFloat.INSTANCE;
            break;
        case DataType.INTEGER:
            sqlType = PInteger.INSTANCE;
            break;
        case DataType.LONG:
        case DataType.BIGINTEGER:
            sqlType = PLong.INSTANCE;
            break;
        case DataType.BOOLEAN:
            sqlType = PBoolean.INSTANCE;
            break;
        case DataType.DATETIME:
            sqlType = PDate.INSTANCE;
            break;
        case DataType.BYTE:
            sqlType = PTinyint.INSTANCE;
            break;
        default:
            throw new RuntimeException("Unknown type " + obj.getClass().getName() + " passed to PhoenixHBaseStorage");
        }

        return sqlType;

    }

    /**
     * This method encodes a value with Phoenix data type. It begins with checking whether an object is BINARY and makes
     * a call to {@link #castBytes(Object, PDataType)} to convery bytes to targetPhoenixType
     * 
     * @param o
     * @param targetPhoenixType
     * @return Object
     */
    public static Object castPigTypeToPhoenix(Object o, byte objectType, PDataType targetPhoenixType) {<commit_msg>minor changes based on jesses feedback<commit_after>/*
 * 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.phoenix.pig.util;

import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.phoenix.pig.writable.PhoenixPigDBWritable;
import org.apache.phoenix.schema.types.PBinary;
import org.apache.phoenix.schema.types.PBoolean;
import org.apache.phoenix.schema.types.PChar;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PDate;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
import org.apache.phoenix.schema.types.PFloat;
import org.apache.phoenix.schema.types.PInteger;
import org.apache.phoenix.schema.types.PLong;
import org.apache.phoenix.schema.types.PSmallint;
import org.apache.phoenix.schema.types.PTime;
import org.apache.phoenix.schema.types.PTimestamp;
import org.apache.phoenix.schema.types.PTinyint;
import org.apache.phoenix.schema.types.PUnsignedDate;
import org.apache.phoenix.schema.types.PUnsignedDouble;
import org.apache.phoenix.schema.types.PUnsignedFloat;
import org.apache.phoenix.schema.types.PUnsignedInt;
import org.apache.phoenix.schema.types.PUnsignedLong;
import org.apache.phoenix.schema.types.PUnsignedSmallint;
import org.apache.phoenix.schema.types.PUnsignedTime;
import org.apache.phoenix.schema.types.PUnsignedTimestamp;
import org.apache.phoenix.schema.types.PUnsignedTinyint;
import org.apache.phoenix.schema.types.PVarbinary;
import org.apache.phoenix.schema.types.PVarchar;
import org.apache.pig.PigException;
import org.apache.pig.ResourceSchema.ResourceFieldSchema;
import org.apache.pig.backend.hadoop.hbase.HBaseBinaryConverter;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
import org.joda.time.DateTime;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public final class TypeUtil {

    private static final Log LOG = LogFactory.getLog(TypeUtil.class);
    private static final HBaseBinaryConverter BINARY_CONVERTER = new HBaseBinaryConverter();
    private static final ImmutableMap<PDataType, Byte> PHOENIX_TO_PIG_TYPE = init();
    private static final TupleFactory TUPLE_FACTORY = TupleFactory.getInstance();

    private TypeUtil() {}

    /**
     * @return map of Phoenix to Pig data types.
     */
    private static ImmutableMap<PDataType, Byte> init() {
        final ImmutableMap.Builder<PDataType, Byte> builder = new Builder<PDataType, Byte>();
        builder.put(PLong.INSTANCE, DataType.LONG);
        builder.put(PVarbinary.INSTANCE, DataType.BYTEARRAY);
        builder.put(PChar.INSTANCE, DataType.CHARARRAY);
        builder.put(PVarchar.INSTANCE, DataType.CHARARRAY);
        builder.put(PDouble.INSTANCE, DataType.DOUBLE);
        builder.put(PFloat.INSTANCE, DataType.FLOAT);
        builder.put(PInteger.INSTANCE, DataType.INTEGER);
        builder.put(PTinyint.INSTANCE, DataType.INTEGER);
        builder.put(PSmallint.INSTANCE, DataType.INTEGER);
        builder.put(PDecimal.INSTANCE, DataType.BIGDECIMAL);
        builder.put(PTime.INSTANCE, DataType.DATETIME);
        builder.put(PTimestamp.INSTANCE, DataType.DATETIME);
        builder.put(PBoolean.INSTANCE, DataType.BOOLEAN);
        builder.put(PDate.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedDate.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedDouble.INSTANCE, DataType.DOUBLE);
        builder.put(PUnsignedFloat.INSTANCE, DataType.FLOAT);
        builder.put(PUnsignedInt.INSTANCE, DataType.INTEGER);
        builder.put(PUnsignedLong.INSTANCE, DataType.LONG);
        builder.put(PUnsignedSmallint.INSTANCE, DataType.INTEGER);
        builder.put(PUnsignedTime.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedTimestamp.INSTANCE, DataType.DATETIME);
        builder.put(PUnsignedTinyint.INSTANCE, DataType.INTEGER);
        return builder.build();
    }

    /**
     * This method returns the most appropriate PDataType associated with the incoming Pig type. Note for Pig DataType
     * DATETIME, returns DATE as inferredSqlType. This is later used to make a cast to targetPhoenixType accordingly.
     * See {@link #castPigTypeToPhoenix(Object, byte, PDataType)}
     * 
     * @param obj
     * @return PDataType
     */
    public static PDataType getType(Object obj, byte type) {
        if (obj == null) { return null; }
        PDataType sqlType;

        switch (type) {
        case DataType.BYTEARRAY:
            sqlType = PVarbinary.INSTANCE;
            break;
        case DataType.CHARARRAY:
            sqlType = PVarchar.INSTANCE;
            break;
        case DataType.DOUBLE:
        case DataType.BIGDECIMAL:
            sqlType = PDouble.INSTANCE;
            break;
        case DataType.FLOAT:
            sqlType = PFloat.INSTANCE;
            break;
        case DataType.INTEGER:
            sqlType = PInteger.INSTANCE;
            break;
        case DataType.LONG:
        case DataType.BIGINTEGER:
            sqlType = PLong.INSTANCE;
            break;
        case DataType.BOOLEAN:
            sqlType = PBoolean.INSTANCE;
            break;
        case DataType.DATETIME:
            sqlType = PDate.INSTANCE;
            break;
        case DataType.BYTE:
            sqlType = PTinyint.INSTANCE;
            break;
        default:
            throw new RuntimeException("Unknown type " + obj.getClass().getName() + " passed to PhoenixHBaseStorage");
        }

        return sqlType;

    }

    /**
     * This method encodes a value with Phoenix data type. It begins with checking whether an object is BINARY and makes
     * a call to {@link #castBytes(Object, PDataType)} to convert bytes to targetPhoenixType. It returns a {@link RuntimeException}
     * when object can not be coerced.
     * 
     * @param o
     * @param targetPhoenixType
     * @return Object
     */
    public static Object castPigTypeToPhoenix(Object o, byte objectType, PDataType targetPhoenixType) {
{'fraction_non_alphanumeric': 0.09430731916107861, 'fraction_numerical': 0.0005706948209444999, 'mean_word_length': 3.368650669990651, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3751064', 'n_tokens_mistral': 4127, 'n_tokens_neox': 4227, 'n_words': 955}