All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickzetta.platform.catalyst.data.GenericRow Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.clickzetta.platform.catalyst.data;

import com.clickzetta.platform.util.DateUtil;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public final class GenericRow implements InternalRow, Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * The array to store the actual internal format values.
   */
  private final Object[] fields;

  public GenericRow(int arity) {
    this.fields = new Object[arity];
  }

  public GenericRow(Object[] fields) {
    this.fields = fields;
  }

  @Override
  public void setField(int pos, Object value) {
    this.fields[pos] = value;
  }

  @Override
  public boolean isNullAt(int pos) {
    return this.fields[pos] == null;
  }

  @Override
  public boolean getBoolean(int pos) {
    return (boolean) this.fields[pos];
  }

  @Override
  public byte getByte(int pos) {
    return (byte) this.fields[pos];
  }

  @Override
  public short getShort(int pos) {
    return (short) this.fields[pos];
  }

  @Override
  public int getInt(int pos) {
    return (int) this.fields[pos];
  }

  @Override
  public long getLong(int pos) {
    return (long) this.fields[pos];
  }

  @Override
  public float getFloat(int pos) {
    return (float) this.fields[pos];
  }

  @Override
  public double getDouble(int pos) {
    return (double) this.fields[pos];
  }

  @Override
  public String getString(int pos) {
    Object value = this.fields[pos];
    if (value instanceof byte[]) {
      return new String((byte[]) value);
    } else {
      return (String) value;
    }
  }

  @Override
  public byte[] getStringBytes(int pos) {
    Object value = this.fields[pos];
    if (value instanceof String) {
      return ((String) value).getBytes();
    } else {
      return (byte[]) value;
    }
  }

  @Override
  public int getDate(int pos) {
    Object value = this.fields[pos];
    if (value instanceof java.sql.Date) {
      return DateUtil.sqlDateToEpochDays((Date) value);
    } else {
      return (int) value;
    }
  }

  @Override
  public BigDecimal getDecimal(int pos, int precision, int scale) {
    return (BigDecimal) this.fields[pos];
  }

  @Override
  public Timestamp getTimestamp(int pos) {
    Object value = this.fields[pos];
    if (value instanceof Long) {
      return com.clickzetta.platform.catalyst.data.Timestamp.fromMicros((long) value);
    } else if (value instanceof java.sql.Timestamp) {
      return com.clickzetta.platform.catalyst.data.Timestamp.fromSQLTimestamp((java.sql.Timestamp) value);
    } else {
      return (com.clickzetta.platform.catalyst.data.Timestamp) value;
    }
  }

  @Override
  public byte[] getBinary(int pos) {
    return (byte[]) this.fields[pos];
  }

  @Override
  public InternalArray getArray(int pos) {
    Object value = this.fields[pos];
    Object[] data = null;
    if (value.getClass().isArray()) {
      data = (Object[]) value;
    } else {
      data = ((List) value).toArray(new Object[0]);
    }
    return new GenericArray(data);
  }

  @Override
  public InternalMap getMap(int pos) {
    Object value = this.fields[pos];
    return new GenericMap((Map) value);
  }

  @Override
  public InternalRow getRow(int pos, int numFields) {
    Object value = this.fields[pos];
    assert (!(value instanceof Map));
    Object[] rowData = null;
    if (value.getClass().isArray()) {
      rowData = (Object[]) value;
    } else {
      rowData = ((List) value).toArray(new Object[0]);
    }
    assert (rowData.length == numFields);
    return new GenericRow(rowData);
  }

  @Override
  public int getFieldCount() {
    return fields.length;
  }

  @Override
  public void reset() {
    for (int i = 0; i < getFieldCount(); i++) {
      fields[i] = null;
    }
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof GenericRow)) {
      return false;
    }
    GenericRow that = (GenericRow) o;
    return Arrays.deepEquals(fields, that.fields);
  }

  @Override
  public int hashCode() {
    return Arrays.deepHashCode(fields);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("GenericRow(");
    for (int i = 0; i < fields.length; i++) {
      if (i != 0) {
        sb.append(",");
      }
      sb.append(arrayAwareToString(fields[i]));
    }
    sb.append(")");
    return sb.toString();
  }

  private static String arrayAwareToString(Object o) {
    final String arrayString = Arrays.deepToString(new Object[]{o});
    return arrayString.substring(1, arrayString.length() - 1);
  }

  // ----------------------------------------------------------------------------------------
  // Utilities
  // ----------------------------------------------------------------------------------------

  /**
   * Creates an instance of {@link GenericRow} with given field values.
   *
   * 

Note: All fields of the row must be internal data structures. */ public static GenericRow of(Object... values) { GenericRow row = new GenericRow(values.length); for (int i = 0; i < values.length; ++i) { row.setField(i, values[i]); } return row; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy