org.apache.calcite.avatica.util.Cursor Java Examples

The following examples show how to use org.apache.calcite.avatica.util.Cursor. 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: CalciteResultSet.java    From Quicksql with MIT License 6 votes vote down vote up
@Override public ResultSet create(ColumnMetaData.AvaticaType elementType,
    Iterable<Object> iterable) throws SQLException {
  final List<ColumnMetaData> columnMetaDataList;
  if (elementType instanceof ColumnMetaData.StructType) {
    columnMetaDataList = ((ColumnMetaData.StructType) elementType).columns;
  } else {
    columnMetaDataList =
        ImmutableList.of(ColumnMetaData.dummy(elementType, false));
  }
  final CalcitePrepare.CalciteSignature signature =
      (CalcitePrepare.CalciteSignature) this.signature;
  final CalcitePrepare.CalciteSignature<Object> newSignature =
      new CalcitePrepare.CalciteSignature<>(signature.sql,
          signature.parameters, signature.internalParameters,
          signature.rowType, columnMetaDataList, Meta.CursorFactory.ARRAY,
          signature.rootSchema, ImmutableList.of(), -1, null,
          statement.getStatementType());
  ResultSetMetaData subResultSetMetaData =
      new AvaticaResultSetMetaData(statement, null, newSignature);
  final CalciteResultSet resultSet =
      new CalciteResultSet(statement, signature, subResultSetMetaData,
          localCalendar.getTimeZone(), new Meta.Frame(0, true, iterable));
  final Cursor cursor = resultSet.createCursor(elementType, iterable);
  return resultSet.execute2(cursor, columnMetaDataList);
}
 
Example #2
Source File: QuarkResultSet.java    From quark with Apache License 2.0 6 votes vote down vote up
@Override
public ResultSet create(ColumnMetaData.AvaticaType elementType,
                        Iterable<Object> iterable) {
  final List<ColumnMetaData> columnMetaDataList;
  if (elementType instanceof ColumnMetaData.StructType) {
    columnMetaDataList = ((ColumnMetaData.StructType) elementType).columns;
  } else {
    columnMetaDataList =
        ImmutableList.of(ColumnMetaData.dummy(elementType, false));
  }
  final CalcitePrepare.CalciteSignature signature =
      (CalcitePrepare.CalciteSignature) this.signature;
  final CalcitePrepare.CalciteSignature<Object> newSignature =
      new CalcitePrepare.CalciteSignature<>(signature.sql,
          signature.parameters, signature.internalParameters,
          signature.rowType, columnMetaDataList, Meta.CursorFactory.ARRAY,
          signature.rootSchema, ImmutableList.<RelCollation>of(), -1, null);
  ResultSetMetaData subResultSetMetaData =
      new AvaticaResultSetMetaData(statement, null, newSignature);
  final QuarkResultSet resultSet =
      new QuarkResultSet(statement, signature, subResultSetMetaData,
          localCalendar.getTimeZone(), new Meta.Frame(0, true, iterable));
  final Cursor cursor = resultSet.createCursor(elementType, iterable);
  return resultSet.execute2(cursor, columnMetaDataList);
}
 
Example #3
Source File: CalciteResultSet.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public ResultSet create(ColumnMetaData.AvaticaType elementType,
    Iterable<Object> iterable) throws SQLException {
  final List<ColumnMetaData> columnMetaDataList;
  if (elementType instanceof ColumnMetaData.StructType) {
    columnMetaDataList = ((ColumnMetaData.StructType) elementType).columns;
  } else {
    columnMetaDataList =
        ImmutableList.of(ColumnMetaData.dummy(elementType, false));
  }
  final CalcitePrepare.CalciteSignature signature =
      (CalcitePrepare.CalciteSignature) this.signature;
  final CalcitePrepare.CalciteSignature<Object> newSignature =
      new CalcitePrepare.CalciteSignature<>(signature.sql,
          signature.parameters, signature.internalParameters,
          signature.rowType, columnMetaDataList, Meta.CursorFactory.ARRAY,
          signature.rootSchema, ImmutableList.of(), -1, null,
          statement.getStatementType());
  ResultSetMetaData subResultSetMetaData =
      new AvaticaResultSetMetaData(statement, null, newSignature);
  final CalciteResultSet resultSet =
      new CalciteResultSet(statement, signature, subResultSetMetaData,
          localCalendar.getTimeZone(), new Meta.Frame(0, true, iterable));
  final Cursor cursor = resultSet.createCursor(elementType, iterable);
  return resultSet.execute2(cursor, columnMetaDataList);
}
 
Example #4
Source File: CalciteResultSet.java    From Quicksql with MIT License 5 votes vote down vote up
private Cursor createCursor(ColumnMetaData.AvaticaType elementType,
    Iterable iterable) {
  final Enumerator enumerator = Linq4j.iterableEnumerator(iterable);
  //noinspection unchecked
  return !(elementType instanceof ColumnMetaData.StructType)
      || ((ColumnMetaData.StructType) elementType).columns.size() == 1
      ? new ObjectEnumeratorCursor(enumerator)
      : new ArrayEnumeratorCursor(enumerator);
}
 
Example #5
Source File: QuicksqlServerMeta.java    From Quicksql with MIT License 5 votes vote down vote up
protected QuicksqlServerResultSet getResultSet(StatementHandle h, String sql, int maxResNum, QueryResult result)
    throws Exception {
    final List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
    columnMetaDataList.addAll(result.columnMeta);
    final StatementInfo info = getStatementCache().getIfPresent(h.id);
    Signature signature = preparedSignature(result, sql);
    Cursor cursor = MetaImpl.createCursor(signature.cursorFactory, result.iterable);
    QuicksqlResultSet quickSqlResultSet = new QuicksqlResultSet((AvaticaStatement) info.statement, signature,
        new AvaticaResultSetMetaData((AvaticaStatement) info.statement, null, signature), TimeZone.getDefault(),
        null);
    quickSqlResultSet.execute2(cursor, columnMetaDataList);
    info.setResultSet(quickSqlResultSet);
    return QuicksqlServerResultSet.create(h.connectionId, h.id, quickSqlResultSet, signature, maxResNum);
}
 
Example #6
Source File: MetaImpl.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/** Uses a {@link org.apache.calcite.avatica.Meta.CursorFactory} to convert
 * an {@link Iterable} into a
 * {@link org.apache.calcite.avatica.util.Cursor}. */
public static Cursor createCursor(CursorFactory cursorFactory,
    Iterable<Object> iterable) {
  switch (cursorFactory.style) {
  case OBJECT:
    return new IteratorCursor<Object>(iterable.iterator()) {
      protected Getter createGetter(int ordinal) {
        return new ObjectGetter(ordinal);
      }
    };
  case ARRAY:
    @SuppressWarnings("unchecked") final Iterable<Object[]> iterable1 =
        (Iterable<Object[]>) (Iterable) iterable;
    return new ArrayIteratorCursor(iterable1.iterator());
  case RECORD:
    @SuppressWarnings("unchecked") final Class<Object> clazz =
        cursorFactory.clazz;
    return new RecordIteratorCursor<Object>(iterable.iterator(), clazz);
  case RECORD_PROJECTION:
    @SuppressWarnings("unchecked") final Class<Object> clazz2 =
        cursorFactory.clazz;
    return new RecordIteratorCursor<Object>(iterable.iterator(), clazz2,
        cursorFactory.fields);
  case LIST:
    @SuppressWarnings("unchecked") final Iterable<List<Object>> iterable2 =
        (Iterable<List<Object>>) (Iterable) iterable;
    return new ListIteratorCursor(iterable2.iterator());
  case MAP:
    @SuppressWarnings("unchecked") final Iterable<Map<String, Object>>
        iterable3 =
        (Iterable<Map<String, Object>>) (Iterable) iterable;
    return new MapIteratorCursor(iterable3.iterator(),
        cursorFactory.fieldNames);
  default:
    throw new AssertionError("unknown style: " + cursorFactory.style);
  }
}
 
Example #7
Source File: AvaticaResultSet.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the accessor for column with a given index.
 *
 * @param columnIndex 1-based column index
 * @return Accessor
 * @throws SQLException if index is not valid
 */
private Cursor.Accessor getAccessor(int columnIndex) throws SQLException {
  checkOpen();
  try {
    return accessorList.get(columnIndex - 1);
  } catch (IndexOutOfBoundsException e) {
    throw AvaticaConnection.HELPER.createException(
        "invalid column ordinal: " + columnIndex);
  }
}
 
Example #8
Source File: AvaticaResultSet.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
public void close() {
  closed = true;
  final Cursor cursor = this.cursor;
  if (cursor != null) {
    this.cursor = null;
    cursor.close();
  }
  statement.onResultSetClose(this);
}
 
Example #9
Source File: AvaticaResultSet.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
public AvaticaResultSet execute2(Cursor cursor,
    List<ColumnMetaData> columnMetaDataList) {
  this.cursor = cursor;
  this.accessorList =
      cursor.createAccessors(columnMetaDataList, localCalendar, this);
  this.row = 0;
  this.beforeFirst = true;
  this.afterLast = false;
  return this;
}
 
Example #10
Source File: DremioResultSetImpl.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObject( int columnIndex ) throws SQLException {
  throwIfClosed();

  final Cursor.Accessor accessor;
  try {
    accessor = accessorList.get(columnIndex - 1);
  } catch (IndexOutOfBoundsException e) {
    throw new SQLException("invalid column ordinal: " + columnIndex);
  }
  final ColumnMetaData metaData = columnMetaDataList.get(columnIndex - 1);
  // Dremio returns a float (4bytes) for a SQL Float whereas Calcite would return a double (8bytes)
  int typeId = (metaData.type.id != Types.FLOAT) ? metaData.type.id : Types.REAL;
  return AvaticaSite.get(accessor, typeId, localCalendar);
}
 
Example #11
Source File: QuarkResultSet.java    From quark with Apache License 2.0 5 votes vote down vote up
private Cursor createCursor(ColumnMetaData.AvaticaType elementType,
                            Iterable iterable) {
  final Enumerator enumerator = Linq4j.iterableEnumerator(iterable);
  //noinspection unchecked
  return !(elementType instanceof ColumnMetaData.StructType)
      || ((ColumnMetaData.StructType) elementType).columns.size() == 1
      ? new ObjectEnumeratorCursor(enumerator)
      : new ArrayEnumeratorCursor(enumerator);
}
 
Example #12
Source File: CalciteResultSet.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Cursor createCursor(ColumnMetaData.AvaticaType elementType,
    Iterable iterable) {
  final Enumerator enumerator = Linq4j.iterableEnumerator(iterable);
  //noinspection unchecked
  return !(elementType instanceof ColumnMetaData.StructType)
      || ((ColumnMetaData.StructType) elementType).columns.size() == 1
      ? new ObjectEnumeratorCursor(enumerator)
      : new ArrayEnumeratorCursor(enumerator);
}
 
Example #13
Source File: AvaticaResultSet.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
public Object getObject(int columnIndex) throws SQLException {
  final Cursor.Accessor accessor = getAccessor(columnIndex);
  final ColumnMetaData metaData = columnMetaDataList.get(columnIndex - 1);
  return AvaticaSite.get(accessor, metaData.type.id, localCalendar);
}
 
Example #14
Source File: AvaticaSite.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
/** Similar logic to {@link #setObject}. */
public static Object get(Cursor.Accessor accessor, int targetSqlType,
    Calendar localCalendar) throws SQLException {
  switch (targetSqlType) {
  case Types.CLOB:
  case Types.DATALINK:
  case Types.NCLOB:
  case Types.REF:
  case Types.SQLXML:
    throw notImplemented();
  case Types.STRUCT:
    return accessor.getStruct();
  case Types.ARRAY:
    return accessor.getArray();
  case Types.BIGINT:
    final long aLong = accessor.getLong();
    if (aLong == 0 && accessor.wasNull()) {
      return null;
    }
    return aLong;
  case Types.BINARY:
  case Types.LONGVARBINARY:
  case Types.VARBINARY:
    return accessor.getBytes();
  case Types.BIT:
  case Types.BOOLEAN:
    final boolean aBoolean = accessor.getBoolean();
    if (!aBoolean && accessor.wasNull()) {
      return null;
    }
    return aBoolean;
  case Types.BLOB:
    return accessor.getBlob();
  case Types.DATE:
    return accessor.getDate(localCalendar);
  case Types.DECIMAL:
  case Types.NUMERIC:
    return accessor.getBigDecimal();
  case Types.DISTINCT:
    throw notImplemented();
  case Types.DOUBLE:
  case Types.FLOAT: // yes really; SQL FLOAT is up to 8 bytes
    final double aDouble = accessor.getDouble();
    if (aDouble == 0 && accessor.wasNull()) {
      return null;
    }
    return aDouble;
  case Types.INTEGER:
    final int anInt = accessor.getInt();
    if (anInt == 0 && accessor.wasNull()) {
      return null;
    }
    return anInt;
  case Types.JAVA_OBJECT:
  case Types.OTHER:
    return accessor.getObject();
  case Types.LONGNVARCHAR:
  case Types.LONGVARCHAR:
  case Types.NVARCHAR:
  case Types.VARCHAR:
  case Types.CHAR:
  case Types.NCHAR:
    return accessor.getString();
  case Types.REAL:
    final float aFloat = accessor.getFloat();
    if (aFloat == 0 && accessor.wasNull()) {
      return null;
    }
    return aFloat;
  case Types.ROWID:
    throw notImplemented();
  case Types.SMALLINT:
    final short aShort = accessor.getShort();
    if (aShort == 0 && accessor.wasNull()) {
      return null;
    }
    return aShort;
  case Types.TIME:
    return accessor.getTime(localCalendar);
  case Types.TIMESTAMP:
    return accessor.getTimestamp(localCalendar);
  case Types.TINYINT:
    final byte aByte = accessor.getByte();
    if (aByte == 0 && accessor.wasNull()) {
      return null;
    }
    return aByte;
  default:
    throw notImplemented();
  }
}
 
Example #15
Source File: AvaticaResultSet.java    From calcite-avatica with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the accessor for column with a given label.
 *
 * @param columnLabel Column label
 * @return Accessor
 * @throws SQLException if there is no column with that label
 */
private Cursor.Accessor getAccessor(String columnLabel) throws SQLException {
  checkOpen();
  return accessorList.get(findColumn0(columnLabel));
}