<commit_before>package org.apache.derby.iapi.types;

import java.math.BigDecimal;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.shared.common.reference.SQLState;
import org.joda.time.DateTime;

/**
 * Utility functions for trunc( {date | timestamp | number } ) functionality.
 *
 * year - the year, from 1 to 9999 (a Derby restriction).<br/>
 * monthOfYear - the month of the year, from 1 to 12.<br/>
 * dayOfMonth - the day of the month, from 1 to 31.<br/>
 * hourOfDay - the hour of the day, from 0 to 23.<br/>
 * minuteOfHour - the minute of the hour, from 0 to 59.<br/>
 * secondOfMinute - the second of the minute, from 0 to 59.<br/>
 * millisOfSecond - the millisecond of the second, from 0 to 999.<br/>
 *
 * @author <NAME>
 *         Date: 2/6/15
 */
public class TruncateFunctionUtil {

    /**
     * Truncate the given timestamp at the given truncValue.<br/>
     * Syntax: <code>TRUNC[ATE](&LT;SQLTimestamp&GT;[,&LT;{@link org.apache.derby.iapi.types.TruncateFunctionUtil.DateTruncValue truncValue}&GT;])</code>
     * <p/>
     * Example output for timestamp <code>2000-06-07 17:12:30.0</code>:
     * <ul>
     *  <li>null (default to DAY) = <code>2000-06-07 00:00:00.0</code></li>
     *  <li>YEAR = <code>2000-01-01 00:00:00.0</code></li>
     *  <li>MONTH = <code>2000-06-01 00:00:00.0</code></li>
     *  <li>DAY = <code>2000-06-07 00:00:00.0</code></li>
     *  <li>HOUR = <code>2000-06-07 17:00:00.0</code></li>
     *  <li>MINUTE = <code>2000-06-07 17:12:00.0</code></li>
     *  <li>SECOND = <code>2000-06-07 17:12:30.0</code></li>
     * </ul>
     * @param timestamp DVD containing a SQLTimestamp representing the date/time to truncate.
     * @param truncValue MONTH, DAY, YEAR, MINUTE, etc., the value which will represent the
     *                   floor of the returned date/time. If null, the default is DAY.
     * @return the truncated SQLTimestamp
     * @throws StandardException
     */
    public static DateTimeDataValue truncTimestamp(DataValueDescriptor timestamp,
                                                   DataValueDescriptor truncValue) throws StandardException {
        DateTimeDataValue truncd = new SQLTimestamp();
        return doTrunc(timestamp, truncValue, truncd, true);
    }

    /**
     * Truncate the given date at the given truncValue.<br/>
     * Syntax: <code>TRUNC[ATE](&LT;SQLDate&GT;[,&LT;{@link org.apache.derby.iapi.types.TruncateFunctionUtil.DateTruncValue truncValue}&GT;])</code>
     * <p/>
     * Example output for date <code>2000-06-07</code>:
     * <ul>
     *  <li>null (default to DAY) = <code>2000-06-07</code></li>
     *  <li>YEAR = <code>2000-01-01</code></li>
     *  <li>MONTH = <code>2000-06-01</code></li>
     *  <li>DAY = <code>2000-06-07</code></li>
     * </ul>
     * @param date DVD containing a SQLDate representing the date to truncate.
     * @param truncValue MONTH, DAY, YEAR, MINUTE, etc., the value which will represent the
     *                   floor of the returned date. If null, the default is DAY.
     * @return the truncated SQLDate
     * @throws StandardException
     */
    public static DateTimeDataValue truncDate(DataValueDescriptor date, DataValueDescriptor truncValue)
        throws StandardException {

        DateTimeDataValue truncd = new SQLDate();
        return doTrunc(date, truncValue, truncd, false);
    }

    /**
     * TODO
     * @param numeric
     * @param truncValue
     * @return
     * @throws StandardException
     */
    public static NumberDataValue truncDecimal(DataValueDescriptor numeric, DataValueDescriptor truncValue)
        throws StandardException {

        // TODO JC: yow, this will be a perf hit...
        NumberDataValue returnVal = (NumberDataValue)numeric.cloneValue(false);
        double x = numeric.getDouble();
        if (x > 0) {
            double y = new BigDecimal(String.valueOf(x)).setScale(truncValue.getInt(), BigDecimal.ROUND_FLOOR).doubleValue();
            returnVal.setValue(y);
        } else {
            double y = new BigDecimal(String.valueOf(x)).setScale(truncValue.getInt(), BigDecimal.ROUND_CEILING).doubleValue();
            returnVal.setValue(y);
        }

        return returnVal;
    }

    //==================================================================================================================
    // Helpers
    //==================================================================================================================

    /**
     * Possible truncate constants.
     */<commit_msg>Tried setting BigDecimal directly on cloned value instead of converting to double first. No help.<commit_after>package org.apache.derby.iapi.types;

import java.math.BigDecimal;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.shared.common.reference.SQLState;
import org.joda.time.DateTime;

/**
 * Utility functions for trunc( {date | timestamp | number } ) functionality.
 *
 * year - the year, from 1 to 9999 (a Derby restriction).<br/>
 * monthOfYear - the month of the year, from 1 to 12.<br/>
 * dayOfMonth - the day of the month, from 1 to 31.<br/>
 * hourOfDay - the hour of the day, from 0 to 23.<br/>
 * minuteOfHour - the minute of the hour, from 0 to 59.<br/>
 * secondOfMinute - the second of the minute, from 0 to 59.<br/>
 * millisOfSecond - the millisecond of the second, from 0 to 999.<br/>
 *
 * @author <NAME>
 *         Date: 2/6/15
 */
public class TruncateFunctionUtil {

    /**
     * Truncate the given timestamp at the given truncValue.<br/>
     * Syntax: <code>TRUNC[ATE](&LT;SQLTimestamp&GT;[,&LT;{@link org.apache.derby.iapi.types.TruncateFunctionUtil.DateTruncValue truncValue}&GT;])</code>
     * <p/>
     * Example output for timestamp <code>2000-06-07 17:12:30.0</code>:
     * <ul>
     *  <li>null (default to DAY) = <code>2000-06-07 00:00:00.0</code></li>
     *  <li>YEAR = <code>2000-01-01 00:00:00.0</code></li>
     *  <li>MONTH = <code>2000-06-01 00:00:00.0</code></li>
     *  <li>DAY = <code>2000-06-07 00:00:00.0</code></li>
     *  <li>HOUR = <code>2000-06-07 17:00:00.0</code></li>
     *  <li>MINUTE = <code>2000-06-07 17:12:00.0</code></li>
     *  <li>SECOND = <code>2000-06-07 17:12:30.0</code></li>
     * </ul>
     * @param timestamp DVD containing a SQLTimestamp representing the date/time to truncate.
     * @param truncValue MONTH, DAY, YEAR, MINUTE, etc., the value which will represent the
     *                   floor of the returned date/time. If null, the default is DAY.
     * @return the truncated SQLTimestamp
     * @throws StandardException
     */
    public static DateTimeDataValue truncTimestamp(DataValueDescriptor timestamp,
                                                   DataValueDescriptor truncValue) throws StandardException {
        DateTimeDataValue truncd = new SQLTimestamp();
        return doTrunc(timestamp, truncValue, truncd, true);
    }

    /**
     * Truncate the given date at the given truncValue.<br/>
     * Syntax: <code>TRUNC[ATE](&LT;SQLDate&GT;[,&LT;{@link org.apache.derby.iapi.types.TruncateFunctionUtil.DateTruncValue truncValue}&GT;])</code>
     * <p/>
     * Example output for date <code>2000-06-07</code>:
     * <ul>
     *  <li>null (default to DAY) = <code>2000-06-07</code></li>
     *  <li>YEAR = <code>2000-01-01</code></li>
     *  <li>MONTH = <code>2000-06-01</code></li>
     *  <li>DAY = <code>2000-06-07</code></li>
     * </ul>
     * @param date DVD containing a SQLDate representing the date to truncate.
     * @param truncValue MONTH, DAY, YEAR, MINUTE, etc., the value which will represent the
     *                   floor of the returned date. If null, the default is DAY.
     * @return the truncated SQLDate
     * @throws StandardException
     */
    public static DateTimeDataValue truncDate(DataValueDescriptor date, DataValueDescriptor truncValue)
        throws StandardException {

        DateTimeDataValue truncd = new SQLDate();
        return doTrunc(date, truncValue, truncd, false);
    }

    /**
     * TODO
     * @param numeric
     * @param truncValue
     * @return
     * @throws StandardException
     */
    public static NumberDataValue truncDecimal(DataValueDescriptor numeric, DataValueDescriptor truncValue)
        throws StandardException {

        // TODO JC: yow, this will be a perf hit...
        NumberDataValue returnVal = (NumberDataValue)numeric.cloneValue(false);
        double x = numeric.getDouble();
        BigDecimal y;
        if (x > 0) {
            y = new BigDecimal(String.valueOf(x)).setScale(truncValue.getInt(), BigDecimal.ROUND_FLOOR);
        } else {
            y = new BigDecimal(String.valueOf(x)).setScale(truncValue.getInt(), BigDecimal.ROUND_CEILING);
        }
        y.precision();
        returnVal.setBigDecimal(y);
        return returnVal;
    }

    //==================================================================================================================
    // Helpers
    //==================================================================================================================

    /**
     * Possible truncate constants.
     */
{'fraction_non_alphanumeric': 0.17655429071803852, 'fraction_numerical': 0.04137478108581436, 'mean_word_length': 2.894714407502131, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1564603', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2736, 'n_words': 700}