org.apache.calcite.avatica.MetaImpl Java Examples

The following examples show how to use org.apache.calcite.avatica.MetaImpl. 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: CalciteMetaImpl.java    From Quicksql with MIT License 6 votes vote down vote up
@Override public Frame fetch(StatementHandle h, long offset,
    int fetchMaxRowCount) throws NoSuchStatementException {
  final CalciteConnectionImpl calciteConnection = getConnection();
  CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
  final Signature signature = stmt.getSignature();
  final Iterator<Object> iterator;
  if (stmt.getResultSet() == null) {
    final Iterable<Object> iterable =
        _createIterable(h, signature, null, null);
    iterator = iterable.iterator();
    stmt.setResultSet(iterator);
  } else {
    iterator = stmt.getResultSet();
  }
  final List rows =
      MetaImpl.collect(signature.cursorFactory,
          LimitIterator.of(iterator, fetchMaxRowCount),
          new ArrayList<List<Object>>());
  boolean done = fetchMaxRowCount == 0 || rows.size() < fetchMaxRowCount;
  @SuppressWarnings("unchecked") List<Object> rows1 = (List<Object>) rows;
  return new Meta.Frame(offset, done, rows1);
}
 
Example #2
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
@Override
public Frame fetch(StatementHandle h, long offset, int fetchMaxRowCount) {
  final QuarkConnectionImpl calciteConnection = getConnection();
  QuarkJdbcStatement stmt = calciteConnection.server.getStatement(h);
  final Signature signature = stmt.getSignature();
  final Iterator<Object> iterator;
  if (stmt.getResultSet() == null) {
    final Iterable<Object> iterable =
        Linq4j.emptyEnumerable();
    iterator = iterable.iterator();
    stmt.setResultSet(iterator);
  } else {
    iterator = stmt.getResultSet();
  }
  final List<List<Object>> list = new ArrayList<>();
  List<List<Object>> rows =
      MetaImpl.collect(signature.cursorFactory,
          LimitIterator.of(iterator, fetchMaxRowCount), list);
  boolean done = fetchMaxRowCount == 0 || list.size() < fetchMaxRowCount;
  return new Meta.Frame(offset, done, (List<Object>) (List) rows);
}
 
Example #3
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public Frame fetch(StatementHandle h, long offset,
    int fetchMaxRowCount) throws NoSuchStatementException {
  final CalciteConnectionImpl calciteConnection = getConnection();
  CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
  final Signature signature = stmt.getSignature();
  final Iterator<Object> iterator;
  if (stmt.getResultSet() == null) {
    final Iterable<Object> iterable =
        _createIterable(h, signature, null, null);
    iterator = iterable.iterator();
    stmt.setResultSet(iterator);
  } else {
    iterator = stmt.getResultSet();
  }
  final List rows =
      MetaImpl.collect(signature.cursorFactory,
          LimitIterator.of(iterator, fetchMaxRowCount),
          new ArrayList<List<Object>>());
  boolean done = fetchMaxRowCount == 0 || rows.size() < fetchMaxRowCount;
  @SuppressWarnings("unchecked") List<Object> rows1 = (List<Object>) rows;
  return new Meta.Frame(offset, done, rows1);
}
 
Example #4
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 #5
Source File: KylinResultSet.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected AvaticaResultSet execute() throws SQLException {

    // skip execution if result is already there (case of meta data lookup)
    if (this.firstFrame != null) {
        return super.execute();
    }

    String sql = signature.sql;
    List<AvaticaParameter> params = signature.parameters;
    List<Object> paramValues = null;
    if (!(statement instanceof KylinPreparedStatement)) {
        params = null;
    } else if (params != null && !params.isEmpty()) {
        paramValues = ((KylinPreparedStatement) statement).getParameterJDBCValues();
    }

    KylinConnection connection = (KylinConnection) statement.connection;
    IRemoteClient client = connection.getRemoteClient();

    Map<String, String> queryToggles = new HashMap<>();
    int maxRows = statement.getMaxRows();
    queryToggles.put("ATTR_STATEMENT_MAX_ROWS", String.valueOf(maxRows));
    addServerProps(queryToggles, connection);

    QueryResult result;
    try {
        result = client.executeQuery(sql, paramValues, queryToggles);
    } catch (IOException e) {
        throw new SQLException(e);
    }

    columnMetaDataList.clear();
    columnMetaDataList.addAll(result.columnMeta);

    cursor = MetaImpl.createCursor(signature.cursorFactory, result.iterable);
    return super.execute2(cursor, columnMetaDataList);
}
 
Example #6
Source File: KylinResultSet.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
protected AvaticaResultSet execute() throws SQLException {

    // skip execution if result is already there (case of meta data lookup)
    if (this.firstFrame != null) {
        return super.execute();
    }

    String sql = signature.sql;
    List<AvaticaParameter> params = signature.parameters;
    List<Object> paramValues = null;
    if (!(statement instanceof KylinPreparedStatement)) {
        params = null;
    } else if (params != null && !params.isEmpty()) {
        paramValues = ((KylinPreparedStatement) statement).getParameterJDBCValues();
    }

    KylinConnection connection = (KylinConnection) statement.connection;
    IRemoteClient client = connection.getRemoteClient();

    Map<String, String> queryToggles = new HashMap<>();
    int maxRows = statement.getMaxRows();
    queryToggles.put("ATTR_STATEMENT_MAX_ROWS", String.valueOf(maxRows));
    addServerProps(queryToggles, connection);

    QueryResult result;
    try {
        result = client.executeQuery(sql, paramValues, queryToggles);
    } catch (IOException e) {
        throw new SQLException(e);
    }

    columnMetaDataList.clear();
    columnMetaDataList.addAll(result.columnMeta);

    cursor = MetaImpl.createCursor(signature.cursorFactory, result.iterable);
    return super.execute2(cursor, columnMetaDataList);
}
 
Example #7
Source File: JdbcMeta.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
@Override public ExecuteResult execute(StatementHandle h,
    List<TypedValue> parameterValues, int maxRowsInFirstFrame) throws NoSuchStatementException {
  try {
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
      throw new SQLException("exception while executing query: unbound parameter");
    }

    final StatementInfo statementInfo = statementCache.getIfPresent(h.id);
    if (null == statementInfo) {
      throw new NoSuchStatementException(h);
    }
    final List<MetaResultSet> resultSets;
    final PreparedStatement preparedStatement =
        (PreparedStatement) statementInfo.statement;

    if (parameterValues != null) {
      for (int i = 0; i < parameterValues.size(); i++) {
        TypedValue o = parameterValues.get(i);
        preparedStatement.setObject(i + 1, o.toJdbc(calendar));
      }
    }

    if (preparedStatement.execute()) {
      final Signature signature2;
      if (preparedStatement.isWrapperFor(AvaticaPreparedStatement.class)) {
        signature2 = h.signature;
      } else {
        h.signature = signature(preparedStatement.getMetaData(),
            preparedStatement.getParameterMetaData(), h.signature.sql,
            Meta.StatementType.SELECT);
        signature2 = h.signature;
      }

      // Make sure we set this for subsequent fetch()'s to find the result set.
      statementInfo.setResultSet(preparedStatement.getResultSet());

      if (statementInfo.getResultSet() == null) {
        resultSets = Collections.<MetaResultSet>singletonList(
            JdbcResultSet.empty(h.connectionId, h.id, signature2));
      } else {
        resultSets = Collections.<MetaResultSet>singletonList(
            JdbcResultSet.create(h.connectionId, h.id, statementInfo.getResultSet(),
                maxRowsInFirstFrame, signature2));
      }
    } else {
      resultSets = Collections.<MetaResultSet>singletonList(
          JdbcResultSet.count(h.connectionId, h.id, preparedStatement.getUpdateCount()));
    }

    return new ExecuteResult(resultSets);
  } catch (SQLException e) {
    throw propagate(e);
  }
}
 
Example #8
Source File: MockProtobufService.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private Map<Request, Response> createMapping() {
  HashMap<Request, Response> mappings = new HashMap<>();

  // Add in mappings

  mappings.put(
      new OpenConnectionRequest(connectionId, new HashMap<String, String>()),
      new OpenConnectionResponse());

  // Get the schema, no.. schema..?
  mappings.put(
      new SchemasRequest(connectionId, null, null),
      // ownStatement=false just to avoid the extra close statement call.
      new ResultSetResponse(null, 1, false, null, Meta.Frame.EMPTY, -1, null));

  // Get the tables, no tables exist
  mappings.put(new TablesRequest(connectionId, null, null, null, Collections.<String>emptyList()),
      // ownStatement=false just to avoid the extra close statement call.
      new ResultSetResponse(null, 150, false, null, Meta.Frame.EMPTY, -1, null));

  // Create a statement, get back an id
  mappings.put(new CreateStatementRequest("0"), new CreateStatementResponse("0", 1, null));

  // Prepare and execute a query. Values and schema are returned
  mappings.put(
      new PrepareAndExecuteRequest(connectionId, 1,
          "select * from (\\n values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)", -1),
      new ResultSetResponse("0", 1, true,
          Meta.Signature.create(
              Arrays.<ColumnMetaData>asList(
                  MetaImpl.columnMetaData("C1", 0, Integer.class, true),
                  MetaImpl.columnMetaData("C2", 1, String.class, true)),
              null, null, Meta.CursorFactory.ARRAY, Meta.StatementType.SELECT),
          Meta.Frame.create(0, true,
              Arrays.<Object>asList(new Object[] {1, "a"},
                  new Object[] {null, "b"}, new Object[] {3, "c"})), -1, null));

  // Prepare a query. Schema for results are returned, but no values
  mappings.put(
      new PrepareRequest(connectionId,
          "select * from (\\n values(1, 'a'), (null, 'b'), (3, 'c')), as t (c1, c2)", -1),
      new ResultSetResponse("0", 1, true,
          Meta.Signature.create(
              Arrays.<ColumnMetaData>asList(
                  MetaImpl.columnMetaData("C1", 0, Integer.class, true),
                  MetaImpl.columnMetaData("C2", 1, String.class, true)),
              null, Collections.<AvaticaParameter>emptyList(),
              Meta.CursorFactory.ARRAY, Meta.StatementType.SELECT),
          null, -1, null));

  mappings.put(
      new ColumnsRequest(connectionId, null, null, "my_table", null),
      new ResultSetResponse("00000000-0000-0000-0000-000000000000", -1, true,
          Meta.Signature.create(
              Arrays.<ColumnMetaData>asList(
                  MetaImpl.columnMetaData("TABLE_NAME", 0, String.class, true),
                  MetaImpl.columnMetaData("ORDINAL_POSITION", 1, Long.class, true)), null,
              Collections.<AvaticaParameter>emptyList(), Meta.CursorFactory.ARRAY, null),
          Meta.Frame.create(0, true,
              Arrays.<Object>asList(new Object[] {new Object[]{"my_table", 10}})), -1, null));

  return Collections.unmodifiableMap(mappings);
}
 
Example #9
Source File: ArrayImplTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
@Test public void resultSetFromArray() throws Exception {
  // Define the struct type we're creating
  ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
  ArrayType arrayType = ColumnMetaData.array(intType, "INTEGER", Rep.INTEGER);
  ColumnMetaData arrayMetaData = MetaImpl.columnMetaData("MY_ARRAY", 1, arrayType, false);
  ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
  // Create some arrays from the structs
  Array array1 = factory.createArray(intType, Arrays.<Object>asList(1, 2));
  Array array2 = factory.createArray(intType, Arrays.<Object>asList(3));
  Array array3 = factory.createArray(intType, Arrays.<Object>asList(4, 5, 6));
  List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1),
      Collections.<Object>singletonList(array2), Collections.<Object>singletonList(array3));
  // Create two rows, each with one (array) column
  try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
    List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(arrayMetaData),
        Unsafe.localCalendar(), factory);
    assertEquals(1, accessors.size());
    Accessor accessor = accessors.get(0);

    assertTrue(cursor.next());
    Array actualArray = accessor.getArray();
    // An Array's result set has one row per array element.
    // Each row has two columns. Column 1 is the array offset (1-based), Column 2 is the value.
    ResultSet actualArrayResultSet = actualArray.getResultSet();
    assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
    assertTrue(actualArrayResultSet.next());
    // Order is Avatica implementation specific
    assertEquals(1, actualArrayResultSet.getInt(1));
    assertEquals(1, actualArrayResultSet.getInt(2));
    assertTrue(actualArrayResultSet.next());
    assertEquals(2, actualArrayResultSet.getInt(1));
    assertEquals(2, actualArrayResultSet.getInt(2));
    assertFalse(actualArrayResultSet.next());

    assertTrue(cursor.next());
    actualArray = accessor.getArray();
    actualArrayResultSet = actualArray.getResultSet();
    assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
    assertTrue(actualArrayResultSet.next());
    assertEquals(1, actualArrayResultSet.getInt(1));
    assertEquals(3, actualArrayResultSet.getInt(2));
    assertFalse(actualArrayResultSet.next());

    assertTrue(cursor.next());
    actualArray = accessor.getArray();
    actualArrayResultSet = actualArray.getResultSet();
    assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
    assertTrue(actualArrayResultSet.next());
    assertEquals(1, actualArrayResultSet.getInt(1));
    assertEquals(4, actualArrayResultSet.getInt(2));
    assertTrue(actualArrayResultSet.next());
    assertEquals(2, actualArrayResultSet.getInt(1));
    assertEquals(5, actualArrayResultSet.getInt(2));
    assertTrue(actualArrayResultSet.next());
    assertEquals(3, actualArrayResultSet.getInt(1));
    assertEquals(6, actualArrayResultSet.getInt(2));
    assertFalse(actualArrayResultSet.next());

    assertFalse(cursor.next());
  }
}
 
Example #10
Source File: ProtobufTranslationImplTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a collection of Responses whose serialization will be tested.
 */
private static List<Response> getResponses() {
  final RpcMetadataResponse rpcMetadata = new RpcMetadataResponse("localhost:8765");
  LinkedList<Response> responses = new LinkedList<>();

  // Nested classes (Signature, ColumnMetaData, CursorFactory, etc) are implicitly getting tested)

  // Stub out the metadata for a row
  ScalarType arrayComponentType = ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER);
  ColumnMetaData arrayColumnMetaData = getArrayColumnMetaData(arrayComponentType, 2, "counts");
  List<ColumnMetaData> columns =
      Arrays.asList(MetaImpl.columnMetaData("str", 0, String.class, true),
          MetaImpl.columnMetaData("count", 1, Integer.class, true),
          arrayColumnMetaData);
  List<AvaticaParameter> params =
      Arrays.asList(
          new AvaticaParameter(false, 10, 0, Types.VARCHAR, "VARCHAR",
              String.class.getName(), "str"));
  Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class,
      Arrays.asList("str", "count", "counts"));
  // The row values
  List<Object> rows = new ArrayList<>();
  rows.add(new Object[] {"str_value1", 50, Arrays.asList(1, 2, 3)});
  rows.add(new Object[] {"str_value2", 100, Arrays.asList(1)});

  // Create the signature and frame using the metadata and values
  Signature signature = Signature.create(columns, "sql", params, cursorFactory,
      Meta.StatementType.SELECT);
  Frame frame = Frame.create(Integer.MAX_VALUE, true, rows);

  // And then create a ResultSetResponse
  ResultSetResponse results1 = new ResultSetResponse("connectionId", Integer.MAX_VALUE, true,
      signature, frame, Long.MAX_VALUE, rpcMetadata);
  responses.add(results1);

  responses.add(new CloseStatementResponse(rpcMetadata));

  ConnectionPropertiesImpl connProps = new ConnectionPropertiesImpl(false, true,
      Integer.MAX_VALUE, "catalog", "schema");
  responses.add(new ConnectionSyncResponse(connProps, rpcMetadata));

  responses.add(new OpenConnectionResponse(rpcMetadata));
  responses.add(new CloseConnectionResponse(rpcMetadata));

  responses.add(new CreateStatementResponse("connectionId", Integer.MAX_VALUE, rpcMetadata));

  Map<Meta.DatabaseProperty, Object> propertyMap = new HashMap<>();
  for (Meta.DatabaseProperty prop : Meta.DatabaseProperty.values()) {
    propertyMap.put(prop, prop.defaultValue);
  }
  responses.add(new DatabasePropertyResponse(propertyMap, rpcMetadata));

  responses.add(
      new ExecuteResponse(Arrays.asList(results1, results1, results1), false, rpcMetadata));
  responses.add(new FetchResponse(frame, false, false, rpcMetadata));
  responses.add(new FetchResponse(frame, true, true, rpcMetadata));
  responses.add(new FetchResponse(frame, false, true, rpcMetadata));
  responses.add(
      new PrepareResponse(
          new Meta.StatementHandle("connectionId", Integer.MAX_VALUE, signature),
          rpcMetadata));

  StringWriter sw = new StringWriter();
  new Exception().printStackTrace(new PrintWriter(sw));
  responses.add(
      new ErrorResponse(Collections.singletonList(sw.toString()), "Test Error Message",
          ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE,
          AvaticaSeverity.WARNING, rpcMetadata));

  // No more results, statement not missing
  responses.add(new SyncResultsResponse(false, false, rpcMetadata));
  // Missing statement, no results
  responses.add(new SyncResultsResponse(false, true, rpcMetadata));
  // More results, no missing statement
  responses.add(new SyncResultsResponse(true, false, rpcMetadata));

  // Some tests to make sure ErrorResponse doesn't fail.
  responses.add(new ErrorResponse((List<String>) null, null, 0, null, null, null));
  responses.add(
      new ErrorResponse(Arrays.asList("stacktrace1", "stacktrace2"), null, 0, null, null, null));

  responses.add(new CommitResponse());
  responses.add(new RollbackResponse());

  long[] updateCounts = new long[]{1, 0, 1, 1};
  responses.add(
      new ExecuteBatchResponse("connectionId", 12345, updateCounts, false, rpcMetadata));

  return responses;
}
 
Example #11
Source File: DremioMetaImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
protected static ColumnMetaData.StructType fieldMetaData(Class<?> clazz) {
  return MetaImpl.fieldMetaData(clazz);
}
 
Example #12
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 4 votes vote down vote up
@Override
public ExecuteResult execute(StatementHandle h,
                             List<TypedValue> parameterValues, long maxRowCount) {
  try {
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
      throw new SQLException("exception while executing query: unbound parameter");
    }

    final StatementInfo statementInfo = Objects.requireNonNull(
        statementCache.getIfPresent(h.id),
        "Statement not found, potentially expired. " + h);
    final List<MetaResultSet> resultSets = new ArrayList<>();
    final PreparedStatement preparedStatement =
        (PreparedStatement) statementInfo.statement;

    if (parameterValues != null) {
      for (int i = 0; i < parameterValues.size(); i++) {
        TypedValue o = parameterValues.get(i);
        preparedStatement.setObject(i + 1, o.toJdbc(calendar));
      }
    }

    if (preparedStatement.execute()) {
      final Meta.Frame frame;
      final Signature signature2;
      if (preparedStatement.isWrapperFor(AvaticaPreparedStatement.class)) {
        signature2 = h.signature;
      } else {
        h.signature = signature(preparedStatement.getMetaData(),
            preparedStatement.getParameterMetaData(), h.signature.sql,
            Meta.StatementType.SELECT);
        signature2 = h.signature;
      }

      statementInfo.resultSet = preparedStatement.getResultSet();
      if (statementInfo.resultSet == null) {
        frame = Frame.EMPTY;
        resultSets.add(QuarkMetaResultSet.empty(h.connectionId, h.id, signature2));
      } else {
        resultSets.add(
            QuarkMetaResultSet.create(h.connectionId, h.id,
                statementInfo.resultSet, maxRowCount, signature2));
      }
    } else {
      resultSets.add(
          QuarkMetaResultSet.count(
              h.connectionId, h.id, preparedStatement.getUpdateCount()));
    }

    return new ExecuteResult(resultSets);
  } catch (SQLException e) {
    throw propagate(e);
  }
}