use of org.apache.calcite.avatica.ColumnMetaData in project calcite-avatica by apache.
the class StructImplTest method structAccessor.
@Test
public void structAccessor() throws Exception {
// Define the struct type we're creating
ColumnMetaData intMetaData = MetaImpl.columnMetaData("MY_INT", 1, int.class, false);
ColumnMetaData stringMetaData = MetaImpl.columnMetaData("MY_STRING", 2, String.class, true);
StructType structType = ColumnMetaData.struct(Arrays.asList(intMetaData, stringMetaData));
// Create some structs
Struct struct1 = new StructImpl(Arrays.<Object>asList(1, "one"));
Struct struct2 = new StructImpl(Arrays.<Object>asList(2, "two"));
Struct struct3 = new StructImpl(Arrays.<Object>asList(3));
Struct struct4 = new StructImpl(Arrays.<Object>asList(4, "four", "ignored"));
ColumnMetaData structMetaData = MetaImpl.columnMetaData("MY_STRUCT", 1, structType, false);
List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(struct1), Collections.<Object>singletonList(struct2), Collections.<Object>singletonList(struct3), Collections.<Object>singletonList(struct4));
// Create four rows, each with one (struct) column
try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(structMetaData), Unsafe.localCalendar(), null);
assertEquals(1, accessors.size());
Accessor accessor = accessors.get(0);
assertTrue(cursor.next());
Struct s = accessor.getObject(Struct.class);
Object[] structData = s.getAttributes();
assertEquals(2, structData.length);
assertEquals(1, structData[0]);
assertEquals("one", structData[1]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(2, structData.length);
assertEquals(2, structData[0]);
assertEquals("two", structData[1]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(1, structData.length);
assertEquals(3, structData[0]);
assertTrue(cursor.next());
s = accessor.getObject(Struct.class);
structData = s.getAttributes();
assertEquals(3, structData.length);
assertEquals(4, structData[0]);
assertEquals("four", structData[1]);
// We didn't provide metadata, but we still expect to see it.
assertEquals("ignored", structData[2]);
}
}
use of org.apache.calcite.avatica.ColumnMetaData in project drill by apache.
the class DrillResultSetImpl method getObject.
@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);
// Drill 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);
}
use of org.apache.calcite.avatica.ColumnMetaData in project calcite-avatica by apache.
the class JdbcResultSet method frame.
/** Creates a frame containing a given number or unlimited number of rows
* from a result set. */
static Meta.Frame frame(StatementInfo info, ResultSet resultSet, long offset, int fetchMaxRowCount, Calendar calendar, Optional<Meta.Signature> sig) throws SQLException {
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
final int[] types = new int[columnCount];
Set<Integer> arrayOffsets = new HashSet<>();
for (int i = 0; i < types.length; i++) {
types[i] = metaData.getColumnType(i + 1);
if (Types.ARRAY == types[i]) {
arrayOffsets.add(i);
}
}
final List<Object> rows = new ArrayList<>();
// Meta prepare/prepareAndExecute 0 return 0 row and done
boolean done = fetchMaxRowCount == 0;
for (int i = 0; fetchMaxRowCount < 0 || i < fetchMaxRowCount; i++) {
final boolean hasRow;
if (null != info) {
hasRow = info.next();
} else {
hasRow = resultSet.next();
}
if (!hasRow) {
done = true;
resultSet.close();
break;
}
Object[] columns = new Object[columnCount];
for (int j = 0; j < columnCount; j++) {
columns[j] = getValue(resultSet, types[j], j, calendar);
if (arrayOffsets.contains(j)) {
// If we have an Array type, our Signature is lacking precision. We can't extract the
// component type of an Array from metadata, we have to update it as we're serializing
// the ResultSet.
final Array array = resultSet.getArray(j + 1);
// Only attempt to determine the component type for the array when non-null
if (null != array && sig.isPresent()) {
ColumnMetaData columnMetaData = sig.get().columns.get(j);
ArrayType arrayType = (ArrayType) columnMetaData.type;
SqlType componentSqlType = SqlType.valueOf(array.getBaseType());
// Avatica Server will always return non-primitives to ensure nullable is guaranteed.
ColumnMetaData.Rep rep = ColumnMetaData.Rep.serialRepOf(componentSqlType);
AvaticaType componentType = ColumnMetaData.scalar(array.getBaseType(), array.getBaseTypeName(), rep);
// Update the ArrayType from the Signature
arrayType.updateComponentType(componentType);
// We only need to update the array's type once.
arrayOffsets.remove(j);
}
}
}
rows.add(columns);
}
return new Meta.Frame(offset, done, rows);
}
use of org.apache.calcite.avatica.ColumnMetaData in project calcite-avatica by apache.
the class JdbcMeta method columns.
/**
* Converts from JDBC metadata to Avatica columns.
*/
protected static List<ColumnMetaData> columns(ResultSetMetaData metaData) throws SQLException {
if (metaData == null) {
return Collections.emptyList();
}
final List<ColumnMetaData> columns = new ArrayList<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
final SqlType sqlType = SqlType.valueOf(metaData.getColumnType(i));
final ColumnMetaData.Rep rep = ColumnMetaData.Rep.of(sqlType.internal);
final ColumnMetaData.AvaticaType t;
if (sqlType == SqlType.ARRAY || sqlType == SqlType.STRUCT || sqlType == SqlType.MULTISET) {
ColumnMetaData.AvaticaType arrayValueType = ColumnMetaData.scalar(Types.JAVA_OBJECT, metaData.getColumnTypeName(i), ColumnMetaData.Rep.OBJECT);
t = ColumnMetaData.array(arrayValueType, metaData.getColumnTypeName(i), rep);
} else {
t = ColumnMetaData.scalar(metaData.getColumnType(i), metaData.getColumnTypeName(i), rep);
}
ColumnMetaData md = new ColumnMetaData(i - 1, metaData.isAutoIncrement(i), metaData.isCaseSensitive(i), metaData.isSearchable(i), metaData.isCurrency(i), metaData.isNullable(i), metaData.isSigned(i), metaData.getColumnDisplaySize(i), metaData.getColumnLabel(i), metaData.getColumnName(i), metaData.getSchemaName(i), metaData.getPrecision(i), metaData.getScale(i), metaData.getTableName(i), metaData.getCatalogName(i), t, metaData.isReadOnly(i), metaData.isWritable(i), metaData.isDefinitelyWritable(i), metaData.getColumnClassName(i));
columns.add(md);
}
return columns;
}
use of org.apache.calcite.avatica.ColumnMetaData in project drill by apache.
the class DrillColumnMetaDataList method updateColumnMetaData.
public void updateColumnMetaData(String catalogName, String schemaName, String tableName, BatchSchema schema, List<Class<?>> getObjectClasses) {
final List<ColumnMetaData> newColumns = new ArrayList<>(schema.getFieldCount());
for (int colOffset = 0; colOffset < schema.getFieldCount(); colOffset++) {
final MaterializedField field = schema.getColumn(colOffset);
Class<?> objectClass = getObjectClasses.get(colOffset);
final String columnName = field.getPath();
final MajorType rpcDataType = field.getType();
final AvaticaType bundledSqlDataType = getAvaticaType(rpcDataType);
final String columnClassName = objectClass.getName();
final int nullability;
switch(field.getDataMode()) {
case OPTIONAL:
nullability = ResultSetMetaData.columnNullable;
break;
case REQUIRED:
nullability = ResultSetMetaData.columnNoNulls;
break;
// Should REPEATED still map to columnNoNulls? or to columnNullable?
case REPEATED:
nullability = ResultSetMetaData.columnNoNulls;
break;
default:
throw new AssertionError("Unexpected new DataMode value '" + field.getDataMode().name() + "'");
}
final boolean isSigned = Types.isJdbcSignedType(rpcDataType);
// TODO(DRILL-3355): TODO(DRILL-3356): When string lengths, precisions,
// interval kinds, etc., are available from RPC-level data, implement:
// - precision for ResultSetMetadata.getPrecision(...) (like
// getColumns()'s COLUMN_SIZE)
// - scale for getScale(...), and
// - and displaySize for getColumnDisplaySize(...).
final int precision = Types.getPrecision(rpcDataType);
final int scale = Types.getScale(rpcDataType);
final int displaySize = Types.getJdbcDisplaySize(rpcDataType);
ColumnMetaData col = new ColumnMetaData(// (zero-based ordinal (for Java arrays/lists).)
colOffset, false, /* autoIncrement */
false, /* caseSensitive */
true, /* searchable */
false, /* currency */
nullability, isSigned, displaySize, columnName, /* label */
columnName, /* columnName */
schemaName, precision, scale, tableName, catalogName, bundledSqlDataType, true, /* readOnly */
false, /* writable */
false, /* definitelyWritable */
columnClassName);
newColumns.add(col);
}
columns = newColumns;
}
Aggregations