Java Code Examples for org.apache.calcite.avatica.Meta#CursorFactory

The following examples show how to use org.apache.calcite.avatica.Meta#CursorFactory . 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: CalcitePrepare.java    From Quicksql with MIT License 6 votes vote down vote up
public CalciteSignature(String sql,
    List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters,
    RelDataType rowType,
    List<ColumnMetaData> columns,
    Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema,
    List<RelCollation> collationList,
    long maxRowCount,
    Bindable<T> bindable,
    Meta.StatementType statementType) {
  super(columns, sql, parameterList, internalParameters, cursorFactory,
      statementType);
  this.rowType = rowType;
  this.rootSchema = rootSchema;
  this.collationList = collationList;
  this.maxRowCount = maxRowCount;
  this.bindable = bindable;
}
 
Example 2
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 6 votes vote down vote up
public CalciteSignature(String sql,
    List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters,
    RelDataType rowType,
    List<ColumnMetaData> columns,
    Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema,
    List<RelCollation> collationList,
    long maxRowCount,
    Bindable<T> bindable,
    Meta.StatementType statementType) {
  super(columns, sql, parameterList, internalParameters, cursorFactory,
      statementType);
  this.rowType = rowType;
  this.rootSchema = rootSchema;
  this.collationList = collationList;
  this.maxRowCount = maxRowCount;
  this.bindable = bindable;
}
 
Example 3
Source File: CalcitePrepare.java    From Quicksql with MIT License 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public CalciteSignature(String sql, List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters, RelDataType rowType,
    List<ColumnMetaData> columns, Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema, List<RelCollation> collationList,
    long maxRowCount, Bindable<T> bindable) {
  this(sql, parameterList, internalParameters, rowType, columns,
      cursorFactory, rootSchema, collationList, maxRowCount, bindable,
      null);
}
 
Example 4
Source File: CalcitePrepareImpl.java    From Quicksql with MIT License 5 votes vote down vote up
/** Quickly prepares a simple SQL statement, circumventing the usual
 * preparation process. */
private <T> CalciteSignature<T> simplePrepare(Context context, String sql) {
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  final RelDataType x =
      typeFactory.builder()
          .add(SqlUtil.deriveAliasFromOrdinal(0), SqlTypeName.INTEGER)
          .build();
  @SuppressWarnings("unchecked")
  final List<T> list = (List) ImmutableList.of(1);
  final List<String> origin = null;
  final List<List<String>> origins =
      Collections.nCopies(x.getFieldCount(), origin);
  final List<ColumnMetaData> columns =
      getColumnMetaDataList(typeFactory, x, x, origins);
  final Meta.CursorFactory cursorFactory =
      Meta.CursorFactory.deduce(columns, null);
  return new CalciteSignature<>(
      sql,
      ImmutableList.of(),
      ImmutableMap.of(),
      x,
      columns,
      cursorFactory,
      context.getRootSchema(),
      ImmutableList.of(),
      -1, dataContext -> Linq4j.asEnumerable(list),
      Meta.StatementType.SELECT);
}
 
Example 5
Source File: CalcitePrepareImpl.java    From Quicksql with MIT License 5 votes vote down vote up
public Bindable getBindable(final Meta.CursorFactory cursorFactory) {
  final String explanation = getCode();
  return dataContext -> {
    switch (cursorFactory.style) {
    case ARRAY:
      return Linq4j.singletonEnumerable(new String[] {explanation});
    case OBJECT:
    default:
      return Linq4j.singletonEnumerable(explanation);
    }
  };
}
 
Example 6
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public CalciteSignature(String sql, List<AvaticaParameter> parameterList,
    Map<String, Object> internalParameters, RelDataType rowType,
    List<ColumnMetaData> columns, Meta.CursorFactory cursorFactory,
    CalciteSchema rootSchema, List<RelCollation> collationList,
    long maxRowCount, Bindable<T> bindable) {
  this(sql, parameterList, internalParameters, rowType, columns,
      cursorFactory, rootSchema, collationList, maxRowCount, bindable,
      null);
}
 
Example 7
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Quickly prepares a simple SQL statement, circumventing the usual
 * preparation process. */
private <T> CalciteSignature<T> simplePrepare(Context context, String sql) {
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  final RelDataType x =
      typeFactory.builder()
          .add(SqlUtil.deriveAliasFromOrdinal(0), SqlTypeName.INTEGER)
          .build();
  @SuppressWarnings("unchecked")
  final List<T> list = (List) ImmutableList.of(1);
  final List<String> origin = null;
  final List<List<String>> origins =
      Collections.nCopies(x.getFieldCount(), origin);
  final List<ColumnMetaData> columns =
      getColumnMetaDataList(typeFactory, x, x, origins);
  final Meta.CursorFactory cursorFactory =
      Meta.CursorFactory.deduce(columns, null);
  return new CalciteSignature<>(
      sql,
      ImmutableList.of(),
      ImmutableMap.of(),
      x,
      columns,
      cursorFactory,
      context.getRootSchema(),
      ImmutableList.of(),
      -1, dataContext -> Linq4j.asEnumerable(list),
      Meta.StatementType.SELECT);
}
 
Example 8
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Bindable getBindable(final Meta.CursorFactory cursorFactory) {
  final String explanation = getCode();
  return dataContext -> {
    switch (cursorFactory.style) {
    case ARRAY:
      return Linq4j.singletonEnumerable(new String[] {explanation});
    case OBJECT:
    default:
      return Linq4j.singletonEnumerable(explanation);
    }
  };
}
 
Example 9
Source File: CalcitePrepareImpl.java    From Quicksql with MIT License 4 votes vote down vote up
@Override protected PreparedResult implement(RelRoot root) {
  RelDataType resultType = root.rel.getRowType();
  boolean isDml = root.kind.belongsTo(SqlKind.DML);
  final Bindable bindable;
  if (resultConvention == BindableConvention.INSTANCE) {
    bindable = Interpreters.bindable(root.rel);
  } else {
    EnumerableRel enumerable = (EnumerableRel) root.rel;
    if (!root.isRefTrivial()) {
      final List<RexNode> projects = new ArrayList<>();
      final RexBuilder rexBuilder = enumerable.getCluster().getRexBuilder();
      for (int field : Pair.left(root.fields)) {
        projects.add(rexBuilder.makeInputRef(enumerable, field));
      }
      RexProgram program = RexProgram.create(enumerable.getRowType(),
          projects, null, root.validatedRowType, rexBuilder);
      enumerable = EnumerableCalc.create(enumerable, program);
    }

    try {
      CatalogReader.THREAD_LOCAL.set(catalogReader);
      final SqlConformance conformance = context.config().conformance();
      internalParameters.put("_conformance", conformance);
      bindable = EnumerableInterpretable.toBindable(internalParameters,
          context.spark(), enumerable, prefer);
    } finally {
      CatalogReader.THREAD_LOCAL.remove();
    }
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end codegen");
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end compilation");
  }

  return new PreparedResultImpl(
      resultType,
      parameterRowType,
      fieldOrigins,
      root.collation.getFieldCollations().isEmpty()
          ? ImmutableList.of()
          : ImmutableList.of(root.collation),
      root.rel,
      mapTableModOp(isDml, root.kind),
      isDml) {
    public String getCode() {
      throw new UnsupportedOperationException();
    }

    public Bindable getBindable(Meta.CursorFactory cursorFactory) {
      return bindable;
    }

    public Type getElementType() {
      return ((Typed) bindable).getElementType();
    }
  };
}
 
Example 10
Source File: ProtobufTranslationImplTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a collection of Requests whose serialization will be tested.
 */
private static List<Request> getRequests() {
  LinkedList<Request> requests = new LinkedList<>();

  requests.add(new CatalogsRequest());
  requests.add(new DatabasePropertyRequest());
  requests.add(new SchemasRequest("connectionId", "catalog", "schemaPattern"));
  requests.add(
      new TablesRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern",
          Arrays.asList("STRING", "BOOLEAN", "INT")));
  requests.add(new TableTypesRequest());
  requests.add(
      new ColumnsRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern",
          "columnNamePattern"));
  requests.add(new TypeInfoRequest());
  requests.add(
      new PrepareAndExecuteRequest("connectionId", Integer.MAX_VALUE, "sql",
          Long.MAX_VALUE));
  requests.add(new PrepareRequest("connectionId", "sql", Long.MAX_VALUE));

  List<TypedValue> paramValues =
      Arrays.asList(TypedValue.create(Rep.BOOLEAN.name(), Boolean.TRUE),
          TypedValue.create(Rep.STRING.name(), "string"));
  FetchRequest fetchRequest = new FetchRequest("connectionId", Integer.MAX_VALUE,
      Long.MAX_VALUE, Integer.MAX_VALUE);
  requests.add(fetchRequest);

  requests.add(new CreateStatementRequest("connectionId"));
  requests.add(new CloseStatementRequest("connectionId", Integer.MAX_VALUE));
  Map<String, String> info = new HashMap<>();
  info.put("param1", "value1");
  info.put("param2", "value2");
  requests.add(new OpenConnectionRequest("connectionId", info));
  requests.add(new CloseConnectionRequest("connectionId"));
  requests.add(
      new ConnectionSyncRequest("connectionId",
          new ConnectionPropertiesImpl(Boolean.FALSE, Boolean.FALSE,
              Integer.MAX_VALUE, "catalog", "schema")));

  requests.add(new SyncResultsRequest("connectionId", 12345, getSqlQueryState(), 150));
  requests.add(new SyncResultsRequest("connectionId2", 54321, getMetadataQueryState1(), 0));
  requests.add(new SyncResultsRequest("connectionId3", 5, getMetadataQueryState2(), 10));

  requests.add(new CommitRequest("connectionId"));
  requests.add(new RollbackRequest("connectionId"));

  // ExecuteBatchRequest omitted because of the special protobuf conversion it does

  List<String> commands = Arrays.asList("command1", "command2", "command3");
  requests.add(new PrepareAndExecuteBatchRequest("connectionId", 12345, commands));


  List<ColumnMetaData> columns = Collections.emptyList();
  List<AvaticaParameter> params = Collections.emptyList();
  Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class,
      Collections.<String>emptyList());
  Signature signature = Signature.create(columns, "sql", params, cursorFactory,
      Meta.StatementType.SELECT);
  Meta.StatementHandle handle = new Meta.StatementHandle("1234", 1, signature);
  requests.add(new ExecuteRequest(handle, Arrays.<TypedValue>asList((TypedValue) null), 10));
  requests.add(new ExecuteRequest(handle, Arrays.asList(TypedValue.EXPLICIT_NULL), 10));

  return requests;
}
 
Example 11
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 12
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override protected PreparedResult implement(RelRoot root) {
  Hook.PLAN_BEFORE_IMPLEMENTATION.run(root);
  RelDataType resultType = root.rel.getRowType();
  boolean isDml = root.kind.belongsTo(SqlKind.DML);
  final Bindable bindable;
  if (resultConvention == BindableConvention.INSTANCE) {
    bindable = Interpreters.bindable(root.rel);
  } else {
    EnumerableRel enumerable = (EnumerableRel) root.rel;
    if (!root.isRefTrivial()) {
      final List<RexNode> projects = new ArrayList<>();
      final RexBuilder rexBuilder = enumerable.getCluster().getRexBuilder();
      for (int field : Pair.left(root.fields)) {
        projects.add(rexBuilder.makeInputRef(enumerable, field));
      }
      RexProgram program = RexProgram.create(enumerable.getRowType(),
          projects, null, root.validatedRowType, rexBuilder);
      enumerable = EnumerableCalc.create(enumerable, program);
    }

    try {
      CatalogReader.THREAD_LOCAL.set(catalogReader);
      final SqlConformance conformance = context.config().conformance();
      internalParameters.put("_conformance", conformance);
      bindable = EnumerableInterpretable.toBindable(internalParameters,
          context.spark(), enumerable, prefer);
    } finally {
      CatalogReader.THREAD_LOCAL.remove();
    }
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end codegen");
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end compilation");
  }

  return new PreparedResultImpl(
      resultType,
      parameterRowType,
      fieldOrigins,
      root.collation.getFieldCollations().isEmpty()
          ? ImmutableList.of()
          : ImmutableList.of(root.collation),
      root.rel,
      mapTableModOp(isDml, root.kind),
      isDml) {
    public String getCode() {
      throw new UnsupportedOperationException();
    }

    public Bindable getBindable(Meta.CursorFactory cursorFactory) {
      return bindable;
    }

    public Type getElementType() {
      return ((Typed) bindable).getElementType();
    }
  };
}
 
Example 13
Source File: Prepare.java    From Quicksql with MIT License 2 votes vote down vote up
/**
 * Executes the prepared result.
 *
 * @param cursorFactory How to map values into a cursor
 * @return producer of rows resulting from execution
 */
Bindable getBindable(Meta.CursorFactory cursorFactory);
 
Example 14
Source File: Prepare.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Executes the prepared result.
 *
 * @param cursorFactory How to map values into a cursor
 * @return producer of rows resulting from execution
 */
Bindable getBindable(Meta.CursorFactory cursorFactory);