<commit_before>/*
 * Copyright (c) 2007 Pentaho Corporation.  All rights reserved. 
 * This software was developed by Pentaho Corporation and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is Pentaho 
 * Data Integration.  The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/
package org.pentaho.di.trans;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.ObjectId;
import org.w3c.dom.Node;

public class ModPartitioner extends BasePartitioner {

	private String fieldName; 
	protected int partitionColumnIndex = -1;
	
	public ModPartitioner(  ) {
		super( );
	}

	public Partitioner getInstance() 
	{
		Partitioner partitioner = new ModPartitioner();
		partitioner.setId( getId() );
		partitioner.setDescription( getDescription() );
		return partitioner;
	}
	
	public ModPartitioner clone()
	{
		ModPartitioner modPartitioner = (ModPartitioner) super.clone();
		modPartitioner.fieldName = fieldName;
		
		return modPartitioner;
	}

	public String getDialogClassName()
	{
		return "org.pentaho.di.ui.trans.dialog.ModPartitionerDialog";
	}

		public int getPartition(RowMetaInterface rowMeta, Object[] row ) throws KettleException
		{
			init(rowMeta);

	        if (partitionColumnIndex < 0)
	        {
	            partitionColumnIndex = rowMeta.indexOfValue(fieldName);
	            if (partitionColumnIndex < 0) { 
	            	throw new KettleStepException("Unable to find partitioning field name [" + fieldName + "] in the output row..." + rowMeta); 
	            }
	        }

	        long value;
	        
	        ValueMetaInterface valueMeta = rowMeta.getValueMeta(partitionColumnIndex);
	        Object valueData = row[partitionColumnIndex];
	        
	        switch(valueMeta.getType()) {
	        case ValueMetaInterface.TYPE_INTEGER: 
	        	Long longValue = rowMeta.getInteger(row, partitionColumnIndex); 
	        	if (longValue==null) {
	        		value = valueMeta.hashCode(valueData);
	        	}
	        	else {
	        		value = longValue.longValue();
	        	}
	        default: 
	        	value = valueMeta.hashCode(valueData);
	        }
	        
	        /*
	        value = rowMeta.getInteger(row, partitionColumnIndex);
	        */
            
            int targetLocation = (int)(value % nrPartitions);

			return targetLocation;
		}

		public String getDescription() {
			String description = "Mod partitioner";
			if (!Const.isEmpty(fieldName)) {
				description+="(" + fieldName + ")";
			}
			return description;
		}

		public String getXML()
		{
	        StringBuilder xml = new StringBuilder(150);
	        xml.append("           ").append(XMLHandler.addTagValue("field_name", fieldName));
	        return xml.toString();
		}

		public void loadXML(Node partitioningMethodNode)
			throws KettleXMLException
		{<commit_msg> PDI-5514 : Partitioning on non-Integer columns or negative values in PDI sometimes causes an out-of-bounds execption<commit_after>/*
 * Copyright (c) 2007 Pentaho Corporation.  All rights reserved. 
 * This software was developed by Pentaho Corporation and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is Pentaho 
 * Data Integration.  The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/
package org.pentaho.di.trans;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.ObjectId;
import org.w3c.dom.Node;

public class ModPartitioner extends BasePartitioner {

	private String fieldName; 
	protected int partitionColumnIndex = -1;
	
	public ModPartitioner(  ) {
		super( );
	}

	public Partitioner getInstance() 
	{
		Partitioner partitioner = new ModPartitioner();
		partitioner.setId( getId() );
		partitioner.setDescription( getDescription() );
		return partitioner;
	}
	
	public ModPartitioner clone()
	{
		ModPartitioner modPartitioner = (ModPartitioner) super.clone();
		modPartitioner.fieldName = fieldName;
		
		return modPartitioner;
	}

	public String getDialogClassName()
	{
		return "org.pentaho.di.ui.trans.dialog.ModPartitionerDialog";
	}

		public int getPartition(RowMetaInterface rowMeta, Object[] row ) throws KettleException
		{
			init(rowMeta);

	        if (partitionColumnIndex < 0)
	        {
	            partitionColumnIndex = rowMeta.indexOfValue(fieldName);
	            if (partitionColumnIndex < 0) { 
	            	throw new KettleStepException("Unable to find partitioning field name [" + fieldName + "] in the output row..." + rowMeta); 
	            }
	        }

	        long value;
	        
	        ValueMetaInterface valueMeta = rowMeta.getValueMeta(partitionColumnIndex);
	        Object valueData = row[partitionColumnIndex];
	        
	        switch(valueMeta.getType()) {
	        case ValueMetaInterface.TYPE_INTEGER: 
	        	Long longValue = rowMeta.getInteger(row, partitionColumnIndex); 
	        	if (longValue==null) {
	        		value = valueMeta.hashCode(valueData);
	        	}
	        	else {
	        		value = longValue.longValue();
	        	}
	        default: 
	        	value = valueMeta.hashCode(valueData);
	        }
	        
	        /*
	        value = rowMeta.getInteger(row, partitionColumnIndex);
	        */
            
            int targetLocation = (int)(Math.abs(value) % nrPartitions);

			return targetLocation;
		}

		public String getDescription() {
			String description = "Mod partitioner";
			if (!Const.isEmpty(fieldName)) {
				description+="(" + fieldName + ")";
			}
			return description;
		}

		public String getXML()
		{
	        StringBuilder xml = new StringBuilder(150);
	        xml.append("           ").append(XMLHandler.addTagValue("field_name", fieldName));
	        return xml.toString();
		}

		public void loadXML(Node partitioningMethodNode)
			throws KettleXMLException
		{
{'fraction_non_alphanumeric': 0.09085963003264418, 'fraction_numerical': 0.004624591947769314, 'mean_word_length': 3.013646288209607, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6950803', 'n_tokens_mistral': 2227, 'n_tokens_neox': 1994, 'n_words': 551}