Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface#compare()

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonInputTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public void rowWrittenEvent( RowMetaInterface rowMeta, Object[] row ) throws KettleStepException {
  if ( rowNbr >= data.length ) {
    throw new ComparisonFailure( "too many output rows", "" + data.length, "" + ( rowNbr + 1 ) );
  } else {
    for ( int i = 0; i < data[ rowNbr ].length; i++ ) {
      try {
        boolean eq = true;
        if ( comparators.containsKey( i ) ) {
          Comparison<Object> comp = comparators.get( i );
          if ( comp != null ) {
            eq = comp.equals( data[ rowNbr ][ i ], row[ i ] );
          }
        } else {
          ValueMetaInterface valueMeta = rowMeta.getValueMeta( i );
          eq = valueMeta.compare( data[ rowNbr ][ i ], row[ i ] ) == 0;
        }
        if ( !eq ) {
          throw new ComparisonFailure( String.format( "Mismatch row %d, column %d", rowNbr, i ),
            rowMeta.getString( data[ rowNbr ] ), rowMeta.getString( row ) );
        }
      } catch ( Exception e ) {
        throw new AssertionError( String.format( "Value type at row %d, column %d", rowNbr, i ), e );
      }
    }
    rowNbr++;
  }
}
 
Example 2
Source File: DimensionCache.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Compare 2 rows of data using the natural keys and indexes specified.
 *
 * @param o1
 * @param o2
 * @return
 */
public int compare( Object[] o1, Object[] o2 ) {
  try {
    // First compare on the natural keys...
    //
    int cmp = rowMeta.compare( o1, o2, keyIndexes );
    if ( cmp != 0 ) {
      return cmp;
    }

    // Then see if the start of the date range of o2 falls between the start and end of o2
    //
    ValueMetaInterface fromDateMeta = rowMeta.getValueMeta( fromDateIndex );
    ValueMetaInterface toDateMeta = rowMeta.getValueMeta( toDateIndex );

    Date fromDate = fromDateMeta.getDate( o1[fromDateIndex] );
    Date toDate = toDateMeta.getDate( o1[toDateIndex] );
    Date lookupDate = fromDateMeta.getDate( o2[fromDateIndex] );

    int fromCmpLookup = 0;
    if ( fromDate == null ) {
      if ( lookupDate == null ) {
        fromCmpLookup = 0;
      } else {
        fromCmpLookup = -1;
      }
    } else {
      if ( lookupDate == null ) {
        fromCmpLookup = 1;
      } else {
        fromCmpLookup = fromDateMeta.compare( fromDate, lookupDate );
      }
    }
    if ( fromCmpLookup < 0 ) {
      if ( toDate != null ) {
        int toCmpLookup = toDateMeta.compare( toDate, lookupDate );
        if ( toCmpLookup > 0 ) {
          return 0;
        }
      }
    }
    return fromCmpLookup;
  } catch ( Exception e ) {
    throw new RuntimeException( e );
  }
}
 
Example 3
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Compare 2 values of the same data type
 *
 * @param data1
 *          the first value
 * @param meta2
 *          the second value's metadata
 * @param data2
 *          the second value
 * @return 0 if the values are equal, -1 if data1 is smaller than data2 and +1 if it's larger.
 * @throws KettleValueException
 *           In case we get conversion errors
 */
@Override
public int compare( Object data1, ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
  if ( meta2 == null ) {
    throw new KettleValueException( toStringMeta()
        + " : Second meta data (meta2) is null, please check one of the previous steps." );
  }

  try {
    // Before we can compare data1 to data2 we need to make sure they have the
    // same data type etc.
    //
    if ( getType() == meta2.getType() ) {
      if ( getStorageType() == meta2.getStorageType() ) {
        return compare( data1, data2 );
      }

      // Convert the storage type to compare the data.
      //
      switch ( getStorageType() ) {
        case STORAGE_TYPE_NORMAL:
          return compare( data1, meta2.convertToNormalStorageType( data2 ) );
        case STORAGE_TYPE_BINARY_STRING:
          if ( storageMetadata != null && storageMetadata.getConversionMask() != null && !meta2.isNumber() ) {
            // BACKLOG-18754 - if there is a storage conversion mask, we should use
            // it as the mask for meta2 (meta2 can have specific storage type and type, so
            // it can't be used directly to convert data2 to binary string)
            ValueMetaInterface meta2StorageMask = meta2.clone();
            meta2StorageMask.setConversionMask( storageMetadata.getConversionMask() );
            return compare( data1, meta2StorageMask.convertToBinaryStringStorageType( data2 ) );
          } else {
            return compare( data1, meta2.convertToBinaryStringStorageType( data2 ) );
          }
        case STORAGE_TYPE_INDEXED:
          switch ( meta2.getStorageType() ) {
            case STORAGE_TYPE_INDEXED:
              return compare( data1, data2 ); // not accessible, just to make sure.
            case STORAGE_TYPE_NORMAL:
              return -meta2.compare( data2, convertToNormalStorageType( data1 ) );
            case STORAGE_TYPE_BINARY_STRING:
              return -meta2.compare( data2, convertToBinaryStringStorageType( data1 ) );
            default:
              throw new KettleValueException( meta2.toStringMeta() + " : Unknown storage type : "
                  + meta2.getStorageType() );

          }
        default:
          throw new KettleValueException( toStringMeta() + " : Unknown storage type : " + getStorageType() );
      }
    } else if ( ValueMetaInterface.TYPE_INTEGER == getType() && ValueMetaInterface.TYPE_NUMBER == meta2.getType() ) {
      // BACKLOG-18738
      // compare Double to Integer
      return -meta2.compare( data2, meta2.convertData( this, data1 ) );
    }

    // If the data types are not the same, the first one is the driver...
    // The second data type is converted to the first one.
    //
    return compare( data1, convertData( meta2, data2 ) );
  } catch ( Exception e ) {
    throw new KettleValueException(
        toStringMeta() + " : Unable to compare with value [" + meta2.toStringMeta() + "]", e );
  }
}
 
Example 4
Source File: AggregateRows.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private synchronized void AddAggregate( RowMetaInterface rowMeta, Object[] r ) throws KettleValueException {
  for ( int i = 0; i < data.fieldnrs.length; i++ ) {
    ValueMetaInterface valueMeta = rowMeta.getValueMeta( data.fieldnrs[i] );
    Object valueData = r[data.fieldnrs[i]];

    if ( !valueMeta.isNull( valueData ) ) {
      data.counts[i]++; // only count non-zero values!
      switch ( meta.getAggregateType()[i] ) {
        case AggregateRowsMeta.TYPE_AGGREGATE_SUM:
        case AggregateRowsMeta.TYPE_AGGREGATE_AVERAGE:
          Double number = valueMeta.getNumber( valueData );
          if ( data.values[i] == null ) {
            data.values[i] = number;
          } else {
            data.values[i] = new Double( ( (Double) data.values[i] ).doubleValue() + number.doubleValue() );
          }

          break;
        case AggregateRowsMeta.TYPE_AGGREGATE_MIN:
          if ( data.values[i] == null ) {
            data.values[i] = valueData;
          } else {
            if ( valueMeta.compare( data.values[i], valueData ) < 0 ) {
              data.values[i] = valueData;
            }
          }

          break;
        case AggregateRowsMeta.TYPE_AGGREGATE_MAX:
          if ( data.values[i] == null ) {
            data.values[i] = valueData;
          } else {
            if ( valueMeta.compare( data.values[i], valueData ) > 0 ) {
              data.values[i] = valueData;
            }
          }

          break;
        case AggregateRowsMeta.TYPE_AGGREGATE_NONE:
        case AggregateRowsMeta.TYPE_AGGREGATE_FIRST:
          if ( data.values[i] == null ) {
            data.values[i] = valueData;
          }
          break;
        case AggregateRowsMeta.TYPE_AGGREGATE_LAST:
          data.values[i] = valueData;
          break;
        default:
          break;
      }
    }

    switch ( meta.getAggregateType()[i] ) {
      case AggregateRowsMeta.TYPE_AGGREGATE_FIRST_NULL: // First value, EVEN if it's NULL:
        if ( data.values[i] == null ) {
          data.values[i] = valueData;
        }
        break;
      case AggregateRowsMeta.TYPE_AGGREGATE_LAST_NULL: // Last value, EVEN if it's NULL:
        data.values[i] = valueData;
        break;
      default:
        break;
    }

  }
}