syntax = "proto3";

package nominal.sql.v1;

import "buf/validate/validate.proto";
import "google/api/annotations.proto";
import "nominal/protos/gen/v1/alias.proto";

option java_multiple_files = true;
option java_package = "io.nominal.sql.v1";

message SqlServiceQueryRequest {
  string query = 1 [
    (buf.validate.field).required = true,
    (buf.validate.field).string.max_len = 100000
  ];

  optional int32 max_rows = 2 [
    (buf.validate.field).int32.gte = 1,
    (buf.validate.field).int32.lte = 5000
  ];

  string workspace_rid = 3 [
    (buf.validate.field).required = true,
    (nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.WorkspaceRid"}
  ];
}

message SqlServiceQueryResponse {
  bytes arrow_payload = 1;

  // Server-generated id for this query execution, also used as the ClickHouse query_id.
  string query_id = 2;
}

// Executes read-only SQL queries against Scout telemetry tables.
service SqlService {
  // Validate, authorize, lower, and execute a bounded SQL query.
  rpc Query(SqlServiceQueryRequest) returns (SqlServiceQueryResponse) {
    option (google.api.http) = {
      post: "/sql/v1/query"
      body: "*"
    };
  }
}
