acezen commented on code in PR #616: URL: https://github.com/apache/incubator-graphar/pull/616#discussion_r1817980073
########## cli/src/graphar_cli/config.py: ########## @@ -0,0 +1,253 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from logging import getLogger +from pathlib import Path +from typing import Dict, List, Literal, Optional + +from pydantic import BaseModel, field_validator, model_validator +from typing_extensions import Self + +logger = getLogger("graphar_cli") + +# TODO: convert to constants +default_file_type = "parquet" +default_adj_list_type = "ordered_by_source" +default_regular_separator = "_" +default_validate_level = "weak" +default_version = "gar/v1" +support_file_types = {"parquet", "orc", "csv", "json"} + + +class GraphArConfig(BaseModel): + path: str + name: str + vertex_chunk_size: Optional[int] = 100 + edge_chunk_size: Optional[int] = 1024 + file_type: Literal["parquet", "orc", "csv", "json"] = default_file_type + adj_list_type: Literal[ + "ordered_by_source", "ordered_by_dest", "unordered_by_source", "unordered_by_dest" + ] = default_adj_list_type + validate_level: Literal["no", "weak", "strong"] = default_validate_level + version: Optional[str] = default_version + + @field_validator("path") + def check_path(cls, v): + path = Path(v).resolve().absolute() + if not path.exists(): + path.mkdir(parents=True, exist_ok=True) + elif any(path.iterdir()): + msg = f"Warning: Path {v} already exists and contains files." + logger.warning(msg) + return v + + +class Property(BaseModel): + name: str + data_type: Literal["bool", "int32", "int64", "float", "double", "string", "date", "timestamp"] + is_primary: bool = False + nullable: Optional[bool] = None + + @model_validator(mode="after") + def check_nullable(self) -> Self: + if self.is_primary and self.nullable: + msg = f"Primary key `{self.name}` must not be nullable." + raise ValueError(msg) + if self.is_primary: + self.nullable = False + elif self.nullable is None: + self.nullable = True + return self + + +class PropertyGroup(BaseModel): + properties: List[Property] + file_type: Optional[Literal["parquet", "orc", "csv", "json"]] = None + + @field_validator("properties") + def check_properties_length(cls, v): + if len(v) == 0: + msg = "properties must not be empty." + raise ValueError(msg) + return v + + +class Source(BaseModel): + file_type: Optional[Literal["parquet", "orc", "csv", "json"]] = None + path: str + delimiter: str = "," + columns: Dict[str, str] + + @field_validator("path") + def check_path(cls, v): + path = Path(v).resolve().absolute() + if not path.is_file(): + msg = f"'{path}' is not a file." + raise ValueError(msg) + return v + + @field_validator("delimiter") + def check_delimiter(cls, v): + if len(v) != 1: + msg = "delimiter must be a single character." + raise ValueError(msg) + return v + + @model_validator(mode="after") + def check_file_type(self) -> Self: + if not self.file_type: + file_type = Path(self.path).suffix.removeprefix(".") + if file_type == "": + msg = f"File {self.path} has no file type suffix" + raise ValueError(msg) + if file_type not in support_file_types: + msg = f"File type {file_type} not supported" + raise ValueError(msg) + self.file_type = file_type + return self + + +class Vertex(BaseModel): + type: str + labels: List[str] = [] + chunk_size: Optional[int] = None + validate_level: Optional[Literal["no", "weak", "strong"]] = None + prefix: Optional[str] = None + property_groups: List[PropertyGroup] + sources: List[Source] + + @field_validator("property_groups") + def check_property_groups_length(cls, v): + if len(v) == 0: + msg = "property_groups must not be empty." + raise ValueError(msg) + return v + + @field_validator("sources") + def check_sources_length(cls, v): + if len(v) == 0: + msg = "sources must not be empty." + raise ValueError(msg) + return v + + @model_validator(mode="after") + def check_vertex_prefix(self) -> Self: + prefix = self.prefix + type = self.type + if not prefix: + self.prefix = f"vertex/{type}/" + return self + + +class AdjList(BaseModel): + ordered: bool + aligned_by: Literal["src", "dst"] + file_type: Literal["parquet", "orc", "csv", "json"] + + +class Edge(BaseModel): + edge_type: str + src_type: str + src_prop: Optional[str] = None Review Comment: what are the `src_prop`and `dst_prop` mean here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
