Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface#TYPE_NUMBER

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#TYPE_NUMBER . 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: ExcelWriterStepDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the output width to minimal width...
 *
 */
public void setMinimalWidth() {
  int nrNonEmptyFields = wFields.nrNonEmpty();
  for ( int i = 0; i < nrNonEmptyFields; i++ ) {
    TableItem item = wFields.getNonEmpty( i );

    int type = ValueMetaFactory.getIdForValueMeta( item.getText( 2 ) );
    switch ( type ) {
      case ValueMetaInterface.TYPE_STRING:
        item.setText( 3, "" );
        break;
      case ValueMetaInterface.TYPE_INTEGER:
        item.setText( 3, "0" );
        break;
      case ValueMetaInterface.TYPE_NUMBER:
        item.setText( 3, "0.#####" );
        break;
      case ValueMetaInterface.TYPE_DATE:
        break;
      default:
        break;
    }
  }
  wFields.optWidth( true );
}
 
Example 2
Source File: MongodbInputDiscoverFieldsImpl.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
protected static int mongoToKettleType( Object fieldValue ) {
  if ( fieldValue == null ) {
    return ValueMetaInterface.TYPE_STRING;
  }

  if ( fieldValue instanceof Symbol || fieldValue instanceof String || fieldValue instanceof Code
        || fieldValue instanceof ObjectId || fieldValue instanceof MinKey || fieldValue instanceof MaxKey ) {
    return ValueMetaInterface.TYPE_STRING;
  } else if ( fieldValue instanceof Date ) {
    return ValueMetaInterface.TYPE_DATE;
  } else if ( fieldValue instanceof Number ) {
    // try to parse as an Integer
    try {
      Integer.parseInt( fieldValue.toString() );
      return ValueMetaInterface.TYPE_INTEGER;
    } catch ( NumberFormatException e ) {
      return ValueMetaInterface.TYPE_NUMBER;
    }
  } else if ( fieldValue instanceof Binary ) {
    return ValueMetaInterface.TYPE_BINARY;
  } else if ( fieldValue instanceof BSONTimestamp ) {
    return ValueMetaInterface.TYPE_INTEGER;
  }

  return ValueMetaInterface.TYPE_STRING;
}
 
Example 3
Source File: PentahoOrcRecordWriter.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
private ValueMetaInterface getValueMetaInterface( String fieldName, int fieldType ) {
  switch ( fieldType ) {
    case ValueMetaInterface.TYPE_INET:
      return new ValueMetaInternetAddress( fieldName );
    case ValueMetaInterface.TYPE_STRING:
      return new ValueMetaString( fieldName );
    case ValueMetaInterface.TYPE_INTEGER:
      return new ValueMetaInteger( fieldName );
    case ValueMetaInterface.TYPE_NUMBER:
      return new ValueMetaNumber( fieldName );
    case ValueMetaInterface.TYPE_BIGNUMBER:
      return new ValueMetaBigNumber( fieldName );
    case ValueMetaInterface.TYPE_TIMESTAMP:
      return new ValueMetaTimestamp( fieldName );
    case ValueMetaInterface.TYPE_DATE:
      return new ValueMetaDate( fieldName );
    case ValueMetaInterface.TYPE_BOOLEAN:
      return new ValueMetaBoolean( fieldName );
    case ValueMetaInterface.TYPE_BINARY:
      return new ValueMetaBinary( fieldName );
  }
  return null;
}
 
Example 4
Source File: PentahoOrcReadWriteTest.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
private ValueMetaInterface getValueMetaInterface( String fieldName, int fieldType ) {
  switch ( fieldType ) {
    case ValueMetaInterface.TYPE_INET:
      return new ValueMetaInternetAddress( fieldName );
    case ValueMetaInterface.TYPE_STRING:
      return new ValueMetaString( fieldName );
    case ValueMetaInterface.TYPE_INTEGER:
      return new ValueMetaInteger( fieldName );
    case ValueMetaInterface.TYPE_NUMBER:
      return new ValueMetaNumber( fieldName );
    case ValueMetaInterface.TYPE_BIGNUMBER:
      return new ValueMetaBigNumber( fieldName );
    case ValueMetaInterface.TYPE_TIMESTAMP:
      return new ValueMetaTimestamp( fieldName );
    case ValueMetaInterface.TYPE_DATE:
      return new ValueMetaDate( fieldName );
    case ValueMetaInterface.TYPE_BOOLEAN:
      return new ValueMetaBoolean( fieldName );
    case ValueMetaInterface.TYPE_BINARY:
      return new ValueMetaBinary( fieldName );
  }
  return null;
}
 
Example 5
Source File: TableView.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private String[] getComboValues( TableItem row, ColumnInfo colinfo ) {
  if ( colinfo.getType() == ColumnInfo.COLUMN_TYPE_FORMAT ) {
    int type = ValueMetaFactory.getIdForValueMeta( row.getText( colinfo.getFieldTypeColumn() ) );
    switch ( type ) {
      case ValueMetaInterface.TYPE_DATE:
        return Const.getDateFormats();
      case ValueMetaInterface.TYPE_INTEGER:
      case ValueMetaInterface.TYPE_BIGNUMBER:
      case ValueMetaInterface.TYPE_NUMBER:
        return Const.getNumberFormats();
      case ValueMetaInterface.TYPE_STRING:
        return Const.getConversionFormats();
      default:
        return new String[0];
    }
  }
  return colinfo.getComboValues();
}
 
Example 6
Source File: ValueMetaConverter.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected Object convertFromNumberMetaInterface( int targetValueMetaType, Object value )
  throws ValueMetaConversionException {

  if ( value == null ) {
    return null;
  }
  if ( !( value instanceof Double ) ) {
    handleConversionError(
      "Error.  Expecting value of type Double.    actual value type = '" + value.getClass() + "'.    value = '"
        + value + "'." );
  }

  try {
    switch ( targetValueMetaType ) {
      case ValueMetaInterface.TYPE_STRING:
        return Double.toString( (Double) value );
      case ValueMetaInterface.TYPE_NUMBER:
        Double doubleValue = new Double( (Double) value );
        if ( getPrecision() > 0 ) {
          BigDecimal bigDecimal = new BigDecimal( doubleValue );
          bigDecimal = bigDecimal.setScale( getPrecision(), RoundingMode.HALF_UP );
          doubleValue = bigDecimal.doubleValue();
        }
        return doubleValue;
      case ValueMetaInterface.TYPE_INTEGER:
        return ( (Double) value ).longValue();
      case ValueMetaInterface.TYPE_BIGNUMBER:
        return new BigDecimal( (Double) value );
      default:
        throwBadConversionCombination( ValueMetaInterface.TYPE_NUMBER, targetValueMetaType, value );
    }
  } catch ( Exception e ) {
    throwErroredConversion( ValueMetaInterface.TYPE_NUMBER, targetValueMetaType, value, e );
  }
  return null;
}
 
Example 7
Source File: LucidDBBulkLoader.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
  meta = (LucidDBBulkLoaderMeta) smi;
  data = (LucidDBBulkLoaderData) sdi;

  if ( super.init( smi, sdi ) ) {
    data.quote = "\"".getBytes();
    data.separator = ",".getBytes();
    data.newline = Const.CR.getBytes();

    data.bulkTimestampMeta = new ValueMeta( "timestampMeta", ValueMetaInterface.TYPE_DATE );
    data.bulkTimestampMeta.setConversionMask( "yyyy-MM-dd HH:mm:ss" );
    data.bulkTimestampMeta.setStringEncoding( meta.getEncoding() );

    data.bulkDateMeta = new ValueMeta( "dateMeta", ValueMetaInterface.TYPE_DATE );
    data.bulkDateMeta.setConversionMask( "yyyy-MM-dd" );
    data.bulkDateMeta.setStringEncoding( meta.getEncoding() );

    data.bulkNumberMeta = new ValueMeta( "numberMeta", ValueMetaInterface.TYPE_NUMBER );
    data.bulkNumberMeta.setConversionMask( "#.#" );
    data.bulkNumberMeta.setGroupingSymbol( "," );
    data.bulkNumberMeta.setDecimalSymbol( "." );
    data.bulkNumberMeta.setStringEncoding( meta.getEncoding() );

    data.bufferSize = Const.toInt( environmentSubstitute( meta.getBufferSize() ), 100000 );

    // Allocate the buffer
    //
    data.rowBuffer = new byte[data.bufferSize][];
    data.bufferIndex = 0;

    // Schema-table combination...
    data.schemaTable =
      meta.getDatabaseMeta().getQuotedSchemaTableCombination(
        environmentSubstitute( meta.getSchemaName() ), environmentSubstitute( meta.getTableName() ) );

    return true;
  }
  return false;
}
 
Example 8
Source File: ValueMetaConverter.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected Object convertFromBigNumberMetaInterface( int targetValueMetaType, Object value )
  throws ValueMetaConversionException {

  if ( value == null ) {
    return null;
  }

  // value is expected to be of type BigDecimal
  if ( !( value instanceof BigDecimal ) ) {
    handleConversionError(
      "Error.  Expecting value of type BigNumber(BigDecimal).    actual value type = '" + value.getClass()
        + "'.    value = '" + value + "'." );
  }

  try {
    switch ( targetValueMetaType ) {
      case ValueMetaInterface.TYPE_STRING:
        return value.toString();
      case ValueMetaInterface.TYPE_NUMBER:
        Double doubleValue = ( (BigDecimal) value ).doubleValue();
        if ( getPrecision() > 0 ) {
          BigDecimal bigDecimal = new BigDecimal( doubleValue );
          bigDecimal = bigDecimal.setScale( getPrecision(), RoundingMode.HALF_UP );
          doubleValue = bigDecimal.doubleValue();
        }
        return doubleValue;
      case ValueMetaInterface.TYPE_BIGNUMBER:
        return new BigDecimal( ( (BigDecimal) value ).toString() );
      default:
        throwBadConversionCombination( ValueMetaInterface.TYPE_BIGNUMBER, targetValueMetaType, value );
    }
  } catch ( Exception e ) {
    throwErroredConversion( ValueMetaInterface.TYPE_BIGNUMBER, targetValueMetaType, value, e );
  }

  return value;
}
 
Example 9
Source File: AddXMLDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the output width to minimal width...
 * 
 */
public void setMinimalWidth() {
  int nrNonEmptyFields = wFields.nrNonEmpty();
  for ( int i = 0; i < nrNonEmptyFields; i++ ) {
    TableItem item = wFields.getNonEmpty( i );

    item.setText( 5, "" );
    item.setText( 6, "" );

    int type = ValueMeta.getType( item.getText( 2 ) );
    switch ( type ) {
      case ValueMetaInterface.TYPE_STRING:
        item.setText( 4, "" );
        break;
      case ValueMetaInterface.TYPE_INTEGER:
        item.setText( 4, "0" );
        break;
      case ValueMetaInterface.TYPE_NUMBER:
        item.setText( 4, "0.#####" );
        break;
      case ValueMetaInterface.TYPE_DATE:
        break;
      default:
        break;
    }
  }
  wFields.optWidth( true );
}
 
Example 10
Source File: RowMetaAndData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean isEmptyValue( String valueName ) throws KettleValueException {
  int idx = rowMeta.indexOfValue( valueName );
  if ( idx < 0 ) {
    throw new KettleValueException( "Unknown column '" + valueName + "'" );
  }

  ValueMetaInterface metaType = rowMeta.getValueMeta( idx );
  // find by source value type
  switch ( metaType.getType() ) {
    case ValueMetaInterface.TYPE_STRING:
      return rowMeta.getString( data, idx ) == null;
    case ValueMetaInterface.TYPE_BOOLEAN:
      return rowMeta.getBoolean( data, idx ) == null;
    case ValueMetaInterface.TYPE_INTEGER:
      return rowMeta.getInteger( data, idx ) == null;
    case ValueMetaInterface.TYPE_NUMBER:
      return rowMeta.getNumber( data, idx ) == null;
    case ValueMetaInterface.TYPE_BIGNUMBER:
      return rowMeta.getBigNumber( data, idx ) == null;
    case ValueMetaInterface.TYPE_BINARY:
      return rowMeta.getBinary( data, idx ) == null;
    case ValueMetaInterface.TYPE_DATE:
    case ValueMetaInterface.TYPE_TIMESTAMP:
      return rowMeta.getDate( data, idx ) == null;
    case ValueMetaInterface.TYPE_INET:
      return rowMeta.getString( data, idx ) == null;
  }
  throw new KettleValueException( "Unknown source type: " + metaType.getTypeDesc() );
}
 
Example 11
Source File: ValueMetaNumber.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public ValueMetaNumber( String name, int length, int precision ) {
  super( name, ValueMetaInterface.TYPE_NUMBER, length, precision );
}
 
Example 12
Source File: ExcelInputDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Processing excel workbook, filling fields
 *
 * @param fields   RowMetaInterface for filling fields
 * @param info     ExcelInputMeta
 * @param workbook excel workbook for processing
 * @throws KettlePluginException
 */
private void processingWorkbook( RowMetaInterface fields, ExcelInputMeta info, KWorkbook workbook )
  throws KettlePluginException {
  int nrSheets = workbook.getNumberOfSheets();
  for ( int j = 0; j < nrSheets; j++ ) {
    KSheet sheet = workbook.getSheet( j );

    // See if it's a selected sheet:
    int sheetIndex;
    if ( info.readAllSheets() ) {
      sheetIndex = 0;
    } else {
      sheetIndex = Const.indexOfString( sheet.getName(), info.getSheetName() );
    }
    if ( sheetIndex >= 0 ) {
      // We suppose it's the complete range we're looking for...
      //
      int rownr = 0;
      int startcol = 0;

      if ( info.readAllSheets() ) {
        if ( info.getStartColumn().length == 1 ) {
          startcol = info.getStartColumn()[ 0 ];
        }
        if ( info.getStartRow().length == 1 ) {
          rownr = info.getStartRow()[ 0 ];
        }
      } else {
        rownr = info.getStartRow()[ sheetIndex ];
        startcol = info.getStartColumn()[ sheetIndex ];
      }

      boolean stop = false;
      for ( int colnr = startcol; !stop; colnr++ ) {
        try {
          String fieldname = null;
          int fieldtype = ValueMetaInterface.TYPE_NONE;

          KCell cell = sheet.getCell( colnr, rownr );
          if ( cell == null ) {
            stop = true;
          } else {
            if ( cell.getType() != KCellType.EMPTY ) {
              // We found a field.
              fieldname = cell.getContents();
            }

            // System.out.println("Fieldname = "+fieldname);

            KCell below = sheet.getCell( colnr, rownr + 1 );

            if ( below != null ) {
              if ( below.getType() == KCellType.BOOLEAN ) {
                fieldtype = ValueMetaInterface.TYPE_BOOLEAN;
              } else if ( below.getType() == KCellType.DATE ) {
                fieldtype = ValueMetaInterface.TYPE_DATE;
              } else if ( below.getType() == KCellType.LABEL ) {
                fieldtype = ValueMetaInterface.TYPE_STRING;
              } else if ( below.getType() == KCellType.NUMBER ) {
                fieldtype = ValueMetaInterface.TYPE_NUMBER;
              } else {
                fieldtype = ValueMetaInterface.TYPE_STRING;
              }
            } else {
              fieldtype = ValueMetaInterface.TYPE_STRING;
            }

            if ( Utils.isEmpty( fieldname ) ) {
              stop = true;
            } else {
              if ( fieldtype != ValueMetaInterface.TYPE_NONE ) {
                ValueMetaInterface field = ValueMetaFactory.createValueMeta( fieldname, fieldtype );
                fields.addValueMeta( field );
              }
            }
          }
        } catch ( ArrayIndexOutOfBoundsException aioobe ) {
          // System.out.println("index out of bounds at column "+colnr+" : "+aioobe.toString());
          stop = true;
        }
      }
    }
  }
}
 
Example 13
Source File: ExcelInput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private void checkType( KCell cell, ValueMetaInterface v ) throws KettleException {
  if ( !meta.isStrictTypes() ) {
    return;
  }
  switch ( cell.getType() ) {
    case BOOLEAN:
      if ( !( v.getType() == ValueMetaInterface.TYPE_STRING || v.getType() == ValueMetaInterface.TYPE_NONE || v
        .getType() == ValueMetaInterface.TYPE_BOOLEAN ) ) {
        throw new KettleException( BaseMessages.getString( PKG, "ExcelInput.Exception.InvalidTypeBoolean", v
          .getTypeDesc() ) );
      }
      break;

    case DATE:
      if ( !( v.getType() == ValueMetaInterface.TYPE_STRING || v.getType() == ValueMetaInterface.TYPE_NONE || v
        .getType() == ValueMetaInterface.TYPE_DATE ) ) {
        throw new KettleException( BaseMessages.getString( PKG, "ExcelInput.Exception.InvalidTypeDate", cell
          .getContents(), v.getTypeDesc() ) );
      }
      break;

    case LABEL:
      if ( v.getType() == ValueMetaInterface.TYPE_BOOLEAN
        || v.getType() == ValueMetaInterface.TYPE_DATE || v.getType() == ValueMetaInterface.TYPE_INTEGER
        || v.getType() == ValueMetaInterface.TYPE_NUMBER ) {
        throw new KettleException( BaseMessages.getString( PKG, "ExcelInput.Exception.InvalidTypeLabel", cell
          .getContents(), v.getTypeDesc() ) );
      }
      break;

    case EMPTY:
      // OK
      break;

    case NUMBER:
      if ( !( v.getType() == ValueMetaInterface.TYPE_STRING
        || v.getType() == ValueMetaInterface.TYPE_NONE || v.getType() == ValueMetaInterface.TYPE_INTEGER
        || v.getType() == ValueMetaInterface.TYPE_BIGNUMBER || v.getType() == ValueMetaInterface.TYPE_NUMBER ) ) {
        throw new KettleException( BaseMessages.getString( PKG, "ExcelInput.Exception.InvalidTypeNumber", cell
          .getContents(), v.getTypeDesc() ) );
      }
      break;

    default:
      throw new KettleException( BaseMessages.getString( PKG, "ExcelInput.Exception.UnsupportedType", cell
        .getType().getDescription(), cell.getContents() ) );
  }
}
 
Example 14
Source File: ValueMetaBaseTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testHashCodes() throws Exception {
  ValueMetaBase valueMetaString = new ValueMetaBase( );

  valueMetaString.type = ValueMetaInterface.TYPE_BOOLEAN;
  assertEquals( valueMetaString.hashCode( true ),  1231 );

  SimpleDateFormat sdf = new SimpleDateFormat( "dd/M/yyyy" );
  String dateInString = "1/1/2018";
  Date dateObj = sdf.parse( dateInString );
  valueMetaString.type = ValueMetaInterface.TYPE_DATE;
  assertEquals( valueMetaString.hashCode( dateObj ),  -1358655136 );

  Double numberObj = Double.valueOf( 5.1 );
  valueMetaString.type = ValueMetaInterface.TYPE_NUMBER;
  assertEquals( valueMetaString.hashCode( numberObj ),  645005312 );

  valueMetaString.type = ValueMetaInterface.TYPE_STRING;
  assertEquals( valueMetaString.hashCode( "test" ),  3556498 );

  Long longObj = 123L;
  valueMetaString.type = ValueMetaInterface.TYPE_INTEGER;
  assertEquals( valueMetaString.hashCode( longObj ), 123 );

  BigDecimal bDecimalObj = new BigDecimal( 123.1 );
  valueMetaString.type = ValueMetaInterface.TYPE_BIGNUMBER;
  assertEquals( valueMetaString.hashCode( bDecimalObj ),  465045870 );

  byte[] bBinary = new byte[2];
  bBinary[0] = 1;
  bBinary[1] = 0;
  valueMetaString.type = ValueMetaInterface.TYPE_BINARY;
  assertEquals( valueMetaString.hashCode( bBinary ),  992 );

  Timestamp timestampObj = Timestamp.valueOf( "2018-01-01 10:10:10.000000000" );
  valueMetaString.type = ValueMetaInterface.TYPE_TIMESTAMP;
  assertEquals( valueMetaString.hashCode( timestampObj ),  -1322045776 );

  byte[] ipAddr = new byte[]{127, 0, 0, 1};
  InetAddress addrObj = InetAddress.getByAddress( ipAddr );
  valueMetaString.type = ValueMetaInterface.TYPE_INET;
  assertEquals( valueMetaString.hashCode( addrObj ),  2130706433 );

  valueMetaString.type = ValueMetaInterface.TYPE_NONE;
  assertEquals( valueMetaString.hashCode( "any" ),  0 );
}
 
Example 15
Source File: OrcConverter.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
protected static Object convertFromSourceToTargetDataType( ColumnVector columnVector, int currentBatchRow,
                                                           int orcValueMetaInterface ) {

  if ( columnVector.isNull[ currentBatchRow ] ) {
    return null;
  }
  switch ( orcValueMetaInterface ) {
    case ValueMetaInterface.TYPE_INET:
      try {
        return InetAddress.getByName( new String( ( (BytesColumnVector) columnVector ).vector[ currentBatchRow ],
          ( (BytesColumnVector) columnVector ).start[ currentBatchRow ],
          ( (BytesColumnVector) columnVector ).length[ currentBatchRow ] ) );
      } catch ( UnknownHostException e ) {
        e.printStackTrace();
      }

    case ValueMetaInterface.TYPE_STRING:
      return new String( ( (BytesColumnVector) columnVector ).vector[ currentBatchRow ],
        ( (BytesColumnVector) columnVector ).start[ currentBatchRow ],
        ( (BytesColumnVector) columnVector ).length[ currentBatchRow ] );

    case ValueMetaInterface.TYPE_INTEGER:
      return (long) ( (LongColumnVector) columnVector ).vector[ currentBatchRow ];

    case ValueMetaInterface.TYPE_NUMBER:
      return ( (DoubleColumnVector) columnVector ).vector[ currentBatchRow ];

    case ValueMetaInterface.TYPE_BIGNUMBER:
      HiveDecimalWritable obj = ( (DecimalColumnVector) columnVector ).vector[ currentBatchRow ];
      return obj.getHiveDecimal().bigDecimalValue();

    case ValueMetaInterface.TYPE_TIMESTAMP:
      Timestamp timestamp = new Timestamp( ( (TimestampColumnVector) columnVector ).time[ currentBatchRow ] );
      timestamp.setNanos( ( (TimestampColumnVector) columnVector ).nanos[ currentBatchRow ] );
      return timestamp;

    case ValueMetaInterface.TYPE_DATE:
      LocalDate localDate =
        LocalDate.ofEpochDay( 0 ).plusDays( ( (LongColumnVector) columnVector ).vector[ currentBatchRow ] );
      Date dateValue = Date.from( localDate.atStartOfDay( ZoneId.systemDefault() ).toInstant() );
      return dateValue;

    case ValueMetaInterface.TYPE_BOOLEAN:
      return ( (LongColumnVector) columnVector ).vector[ currentBatchRow ] == 0 ? false : true;

    case ValueMetaInterface.TYPE_BINARY:
      byte[] origBytes = ( (BytesColumnVector) columnVector ).vector[ currentBatchRow ];
      int startPos = ( (BytesColumnVector) columnVector ).start[ currentBatchRow ];
      byte[] newBytes = Arrays.copyOfRange( origBytes, startPos,
        startPos + ( (BytesColumnVector) columnVector ).length[ currentBatchRow ] );
      return newBytes;
  }

  //if none of the cases match return a null
  return null;
}
 
Example 16
Source File: KingbaseESDatabaseMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public String getFieldDefinition( ValueMetaInterface v, String tk, String pk, boolean useAutoinc,
                                  boolean addFieldName, boolean addCr ) {
  String retval = "";

  String fieldname = v.getName();
  int length = v.getLength();
  int precision = v.getPrecision();

  if ( addFieldName ) {
    retval += fieldname + " ";
  }

  int type = v.getType();
  switch ( type ) {
    case ValueMetaInterface.TYPE_TIMESTAMP:
    case ValueMetaInterface.TYPE_DATE:
      retval += "TIMESTAMP";
      break;
    case ValueMetaInterface.TYPE_BOOLEAN:
      if ( supportsBooleanDataType() ) {
        retval += "BOOLEAN";
      } else {
        retval += "CHAR(1)";
      }
      break;
    case ValueMetaInterface.TYPE_NUMBER:
    case ValueMetaInterface.TYPE_INTEGER:
    case ValueMetaInterface.TYPE_BIGNUMBER:
      if ( fieldname.equalsIgnoreCase( tk ) || // Technical key
        fieldname.equalsIgnoreCase( pk ) // Primary key
      ) {
        retval += "BIGSERIAL";
      } else {
        if ( length > 0 ) {
          if ( precision > 0 || length > 18 ) {
            retval += "NUMERIC(" + length + ", " + precision + ")";
          } else {
            if ( length > 9 ) {
              retval += "BIGINT";
            } else {
              if ( length < 5 ) {
                retval += "SMALLINT";
              } else {
                retval += "INTEGER";
              }
            }
          }

        } else {
          retval += "DOUBLE PRECISION";
        }
      }
      break;
    case ValueMetaInterface.TYPE_STRING:
      if ( length < 1 || length >= DatabaseMeta.CLOB_LENGTH ) {
        retval += "TEXT";
      } else {
        retval += "VARCHAR(" + length + ")";
      }
      break;
    default:
      retval += " UNKNOWN";
      break;
  }

  if ( addCr ) {
    retval += Const.CR;
  }

  return retval;
}
 
Example 17
Source File: AS400DatabaseMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public String getFieldDefinition( ValueMetaInterface v, String tk, String pk, boolean useAutoinc,
                                  boolean addFieldName, boolean addCr ) {
  String retval = "";

  String fieldname = v.getName();
  int length = v.getLength();
  int precision = v.getPrecision();

  if ( addFieldName ) {
    retval += fieldname + " ";
  }

  int type = v.getType();
  switch ( type ) {
    case ValueMetaInterface.TYPE_TIMESTAMP:
    case ValueMetaInterface.TYPE_DATE:
      retval += "TIMESTAMP";
      break;
    case ValueMetaInterface.TYPE_BOOLEAN:
      retval += "CHAR(1)";
      break;
    case ValueMetaInterface.TYPE_NUMBER:
    case ValueMetaInterface.TYPE_INTEGER:
    case ValueMetaInterface.TYPE_BIGNUMBER:
      if ( length <= 0 && precision <= 0 ) {
        retval += "DOUBLE";
      } else {
        retval += "DECIMAL";
        if ( length > 0 ) {
          retval += "(" + length;
          if ( precision > 0 ) {
            retval += ", " + precision;
          }
          retval += ")";
        }
      }
      break;
    case ValueMetaInterface.TYPE_STRING:
      if ( length > getMaxVARCHARLength() || length >= DatabaseMeta.CLOB_LENGTH ) {
        retval += "CLOB";
      } else {
        retval += "VARCHAR";
        if ( length > 0 ) {
          retval += "(" + length;
        } else {
          retval += "("; // Maybe use some default DB String length?
        }
        retval += ")";
      }
      break;
    default:
      retval += " UNKNOWN";
      break;
  }

  if ( addCr ) {
    retval += Const.CR;
  }

  return retval;
}
 
Example 18
Source File: AccessOutputMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static final List<Column> getColumns( RowMetaInterface row ) {
  List<Column> list = new ArrayList<Column>();

  for ( int i = 0; i < row.size(); i++ ) {
    ValueMetaInterface value = row.getValueMeta( i );

    Column column = new Column();
    column.setName( value.getName() );

    int length = value.getLength();

    switch ( value.getType() ) {
      case ValueMetaInterface.TYPE_INTEGER:
        if ( length < 3 ) {
          column.setType( DataType.BYTE );
          length = DataType.BYTE.getFixedSize();
        } else {
          if ( length < 5 ) {
            column.setType( DataType.INT );
            length = DataType.INT.getFixedSize();
          } else {
            column.setType( DataType.LONG );
            length = DataType.LONG.getFixedSize();
          }
        }
        break;
      case ValueMetaInterface.TYPE_NUMBER:
        column.setType( DataType.DOUBLE );
        length = DataType.DOUBLE.getFixedSize();
        break;
      case ValueMetaInterface.TYPE_DATE:
        column.setType( DataType.SHORT_DATE_TIME );
        length = DataType.SHORT_DATE_TIME.getFixedSize();
        break;
      case ValueMetaInterface.TYPE_STRING:
        if ( length < 255 ) {
          column.setType( DataType.TEXT );
          length *= DataType.TEXT.getUnitSize();
        } else {
          column.setType( DataType.MEMO );
          length *= DataType.MEMO.getUnitSize();
        }
        break;
      case ValueMetaInterface.TYPE_BINARY:
        column.setType( DataType.BINARY );
        break;
      case ValueMetaInterface.TYPE_BOOLEAN:
        column.setType( DataType.BOOLEAN );
        length = DataType.BOOLEAN.getFixedSize();
        break;
      case ValueMetaInterface.TYPE_BIGNUMBER:
        column.setType( DataType.NUMERIC );
        length = DataType.NUMERIC.getFixedSize();
        break;
      default:
        break;
    }

    if ( length >= 0 ) {
      column.setLength( (short) length );
    }
    if ( value.getPrecision() >= 1 && value.getPrecision() <= 28 ) {
      column.setPrecision( (byte) value.getPrecision() );
    }

    list.add( column );
  }

  return list;
}
 
Example 19
Source File: AvroNestedReader.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
/**
 * Perform Kettle type conversions for the Avro leaf field value.
 *
 * @param fieldValue the leaf value from the Avro structure
 * @return an Object of the appropriate Kettle type
 * @throws KettleException if a problem occurs
 */
protected Object getKettleValue( AvroInputField avroInputField, Object fieldValue ) throws KettleException {

  switch ( avroInputField.getTempValueMeta().getType() ) {
    case ValueMetaInterface.TYPE_BIGNUMBER:
      return avroInputField.getTempValueMeta().getBigNumber( fieldValue );
    case ValueMetaInterface.TYPE_BINARY:
      return avroInputField.getTempValueMeta().getBinary( fieldValue );
    case ValueMetaInterface.TYPE_BOOLEAN:
      return avroInputField.getTempValueMeta().getBoolean( fieldValue );
    case ValueMetaInterface.TYPE_DATE:
      if ( avroInputField.getAvroType().getBaseType() == AvroSpec.DataType.INTEGER.getBaseType() ) {
        LocalDate localDate = LocalDate.ofEpochDay( 0 ).plusDays( (Long) fieldValue );
        return Date.from( localDate.atStartOfDay( ZoneId.systemDefault() ).toInstant() );
      } else if ( avroInputField.getAvroType().getBaseType() == AvroSpec.DataType.STRING.getBaseType() ) {
        Object pentahoData = null;
        String dateFormatStr = avroInputField.getStringFormat();
        if ( ( dateFormatStr == null ) || ( dateFormatStr.trim().length() == 0 ) ) {
          dateFormatStr = ValueMetaBase.DEFAULT_DATE_FORMAT_MASK;
        }
        SimpleDateFormat datePattern = new SimpleDateFormat( dateFormatStr );
        try {
          return datePattern.parse( fieldValue.toString() );
        } catch ( Exception e ) {
          return null;
        }
      }
      return avroInputField.getTempValueMeta().getDate( fieldValue );
    case ValueMetaInterface.TYPE_TIMESTAMP:
      return new Timestamp( (Long) fieldValue );
    case ValueMetaInterface.TYPE_INTEGER:
      return avroInputField.getTempValueMeta().getInteger( fieldValue );
    case ValueMetaInterface.TYPE_NUMBER:
      return avroInputField.getTempValueMeta().getNumber( fieldValue );
    case ValueMetaInterface.TYPE_STRING:
      return avroInputField.getTempValueMeta().getString( fieldValue );
    case ValueMetaInterface.TYPE_INET:
      try {
        return InetAddress.getByName( fieldValue.toString() );
      } catch ( UnknownHostException ex ) {
        return null;
      }
    default:
      return null;
  }
}
 
Example 20
Source File: MySQLDatabaseMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override public String getFieldDefinition( ValueMetaInterface v, String tk, String pk, boolean useAutoinc,
                                            boolean addFieldName, boolean addCr ) {
  String retval = "";

  String fieldname = v.getName();
  if ( v.getLength() == DatabaseMeta.CLOB_LENGTH ) {
    v.setLength( getMaxTextFieldLength() );
  }
  int length = v.getLength();
  int precision = v.getPrecision();

  if ( addFieldName ) {
    retval += fieldname + " ";
  }

  int type = v.getType();
  switch ( type ) {
    case ValueMetaInterface.TYPE_TIMESTAMP:
    case ValueMetaInterface.TYPE_DATE:
      retval += "DATETIME";
      break;
    case ValueMetaInterface.TYPE_BOOLEAN:
      if ( supportsBooleanDataType() ) {
        retval += "BOOLEAN";
      } else {
        retval += "CHAR(1)";
      }
      break;

    case ValueMetaInterface.TYPE_NUMBER:
    case ValueMetaInterface.TYPE_INTEGER:
    case ValueMetaInterface.TYPE_BIGNUMBER:
      if ( fieldname.equalsIgnoreCase( tk ) || // Technical key
        fieldname.equalsIgnoreCase( pk ) // Primary key
      ) {
        if ( useAutoinc ) {
          retval += "BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY";
        } else {
          retval += "BIGINT NOT NULL PRIMARY KEY";
        }
      } else {
        // Integer values...
        if ( precision == 0 ) {
          if ( length > 9 ) {
            if ( length < 19 ) {
              // can hold signed values between -9223372036854775808 and 9223372036854775807
              // 18 significant digits
              retval += "BIGINT";
            } else {
              retval += "DECIMAL(" + length + ")";
            }
          } else {
            retval += "INT";
          }
        } else {
          // Floating point values...
          if ( length > 15 ) {
            retval += "DECIMAL(" + length;
            if ( precision > 0 ) {
              retval += ", " + precision;
            }
            retval += ")";
          } else {
            // A double-precision floating-point number is accurate to approximately 15 decimal places.
            // http://mysql.mirrors-r-us.net/doc/refman/5.1/en/numeric-type-overview.html
            retval += "DOUBLE";
          }
        }
      }
      break;
    case ValueMetaInterface.TYPE_STRING:
      if ( length > 0 ) {
        if ( length == 1 ) {
          retval += "CHAR(1)";
        } else if ( length < 256 ) {
          retval += "VARCHAR(" + length + ")";
        } else if ( length < 65536 ) {
          retval += "TEXT";
        } else if ( length < 16777216 ) {
          retval += "MEDIUMTEXT";
        } else {
          retval += "LONGTEXT";
        }
      } else {
        retval += "TINYTEXT";
      }
      break;
    case ValueMetaInterface.TYPE_BINARY:
      retval += "LONGBLOB";
      break;
    default:
      retval += " UNKNOWN";
      break;
  }

  if ( addCr ) {
    retval += Const.CR;
  }

  return retval;
}