// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package greptime.v1;

option java_package = "io.greptime.v1";
option java_outer_classname = "Database";
option go_package = "github.com/GreptimeTeam/greptime-proto/go/greptime/v1";

import "greptime/v1/ddl.proto";
import "greptime/v1/column.proto";
import "greptime/v1/row.proto";
import "greptime/v1/prom.proto";
import "greptime/v1/common.proto";

service GreptimeDatabase {
  rpc Handle(GreptimeRequest) returns (GreptimeResponse);

  rpc HandleRequests(stream GreptimeRequest) returns (GreptimeResponse);
}

message GreptimeRequest {
  RequestHeader header = 1;
  oneof request {
    InsertRequests inserts = 2;
    QueryRequest query = 3;
    DdlRequest ddl = 4;
    DeleteRequests deletes = 5;
    RowInsertRequests row_inserts = 6;
    RowDeleteRequests row_deletes = 7;
  }
}

message GreptimeResponse {
  ResponseHeader header = 1;
  oneof response { AffectedRows affected_rows = 2; }
}

message QueryRequest {
  oneof query {
    string sql = 1;
    bytes logical_plan = 2;
    PromRangeQuery prom_range_query = 3;
    InsertIntoPlan insert_into_plan = 4;
  }
}

// A temporary solution for executing insert into table SELECT .. with logical plan
// since substrait to logical plan doesn't support dml yet.
message InsertIntoPlan {
  TableName table_name = 1;
  bytes logical_plan = 2;
}

message InsertRequests { repeated InsertRequest inserts = 1; }

message InsertRequest {
  string table_name = 1;

  // Data is represented here.
  repeated Column columns = 3;

  // The row_count of all columns, which include null and non-null values.
  //
  // Note: the row_count of all columns in a InsertRequest must be same.
  uint32 row_count = 4;
}

message DeleteRequests { repeated DeleteRequest deletes = 1; }

message DeleteRequest {
  // The table name to delete from. Catalog name and schema name are in the
  // `RequestHeader`.
  string table_name = 1;

  // The data to delete, indexed by key columns.
  repeated Column key_columns = 3;

  // The row count of all columns above.
  uint32 row_count = 4;
}

message RowInsertRequests { repeated RowInsertRequest inserts = 1; }

message RowInsertRequest {
  string table_name = 1;

  // Data is represented here.
  Rows rows = 2;
}

message RowDeleteRequests { repeated RowDeleteRequest deletes = 1; }

message RowDeleteRequest {
  // The table name to delete from. Catalog name and schema name are in the
  // `RequestHeader`.
  string table_name = 1;

  // The data to delete.
  Rows rows = 2;
}
