syntax = "proto3";

package ella.engine;

service EngineService {
  rpc GetTable(TableRef) returns (ResolvedTable);
  rpc CreateTable(CreateTableReq) returns (ResolvedTable);
  rpc CreateCatalog(CreateCatalogReq) returns (CatalogId);
  rpc CreateSchema(CreateSchemaReq) returns (SchemaId);

  rpc SetConfig(Config) returns (Config);
  rpc GetConfig(GetConfigReq) returns (Config);
}

message CreateTableReq {
  TableRef table = 1;
  TableInfo info = 2;
  bool if_not_exists = 3;
  bool or_replace = 4;
}

message CreateCatalogReq {
  string catalog = 1;
  bool if_not_exists = 2;
}

message CreateSchemaReq {
  optional string catalog = 1;
  string schema = 2;
  bool if_not_exists = 3;
}

enum ConfigScope {
  CONNECTION = 0;
  CLUSTER = 1;
}

message Config {
  ConfigScope scope = 1;
  bytes config = 2;
}

message GetConfigReq { ConfigScope scope = 1; }

message ResolvedTable {
  TableId table = 1;
  TableInfo info = 2;
}

message Empty {}

enum TensorType {
  UNKNOWN = 0;
  BOOL = 1;
  INT8 = 2;
  INT16 = 3;
  INT32 = 4;
  INT64 = 5;
  UINT8 = 6;
  UINT16 = 7;
  UINT32 = 8;
  UINT64 = 9;
  FLOAT32 = 10;
  FLOAT64 = 11;
  TIMESTAMP = 12;
  DURATION = 13;
  STRING = 14;
}

message Column {
  string name = 1;
  TensorType data_type = 2;
  repeated uint32 row_shape = 3;
  bool required = 4;
}

message TableIndex {
  string column = 1;
  bool ascending = 3;
}

message ViewInfo {
  bytes plan = 1;
  optional string definition = 2;
  bool materialized = 3;
  repeated TableIndex index = 4;
  optional bytes config = 5;
}

message TopicInfo {
  repeated Column columns = 1;
  bool temporary = 2;
  repeated TableIndex index = 3;
  optional bytes config = 4;
}

message TableInfo {
  oneof kind {
    ViewInfo view = 2;
    TopicInfo topic = 3;
  }
}

message TableId {
  string catalog = 1;
  string schema = 2;
  string table = 3;
}

message TableRef {
  optional string catalog = 1;
  optional string schema = 2;
  string table = 3;
}

message SchemaId {
  string catalog = 1;
  string schema = 2;
}

message CatalogId { string catalog = 1; }
