Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spec): implement data field #21

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion crates/paimon/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::spec::types::DataType;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Display};

/// The table schema for paimon table.
///
Expand Down Expand Up @@ -51,3 +51,129 @@ pub struct DataField {
typ: DataType,
description: Option<String>,
}

impl DataField {
pub fn new(id: i32, name: String, typ: DataType) -> Self {
Self {
id,
name,
typ,
description: None,
}
}

pub fn id(&self) -> i32 {
self.id
}

pub fn name(&self) -> &str {
&self.name
}

pub fn data_type(&self) -> &DataType {
&self.typ
}

pub fn description(&self) -> Option<&str> {
self.description.as_deref()
}

pub fn with_id(mut self, new_id: i32) -> Self {
self.id = new_id;
self
}

pub fn with_name(mut self, new_name: String) -> Self {
self.name = new_name;
self
}

pub fn with_description(mut self, new_description: Option<String>) -> Self {
self.description = new_description;
self
}
}

impl Display for DataField {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

pub fn escape_identifier(identifier: &str) -> String {
identifier.replace('"', "\"\"")
}

pub fn escape_single_quotes(text: &str) -> String {
text.replace('\'', "''")
}

#[cfg(test)]
mod tests {
use crate::spec::IntType;

use super::*;

#[test]
fn test_create_data_field() {
let id = 1;
let name = "field1".to_string();
let typ = DataType::Int(IntType::new());
let description = "test description".to_string();

let data_field = DataField::new(id, name.clone(), typ.clone())
.with_description(Some(description.clone()));

assert_eq!(data_field.id(), id);
assert_eq!(data_field.name(), name);
assert_eq!(data_field.data_type(), &typ);
assert_eq!(data_field.description(), Some(description).as_deref());
}

#[test]
fn test_new_id() {
let d_type = DataType::Int(IntType::new());
let new_data_field = DataField::new(1, "field1".to_string(), d_type.clone()).with_id(2);

assert_eq!(new_data_field.id(), 2);
assert_eq!(new_data_field.name(), "field1");
assert_eq!(new_data_field.data_type(), &d_type);
assert_eq!(new_data_field.description(), None);
}

#[test]
fn test_new_name() {
let d_type = DataType::Int(IntType::new());
let new_data_field =
DataField::new(1, "field1".to_string(), d_type.clone()).with_name("field2".to_string());

assert_eq!(new_data_field.id(), 1);
assert_eq!(new_data_field.name(), "field2");
assert_eq!(new_data_field.data_type(), &d_type);
assert_eq!(new_data_field.description(), None);
}

#[test]
fn test_new_description() {
let d_type = DataType::Int(IntType::new());
let new_data_field = DataField::new(1, "field1".to_string(), d_type.clone())
.with_description(Some("new description".to_string()));

assert_eq!(new_data_field.id(), 1);
assert_eq!(new_data_field.name(), "field1");
assert_eq!(new_data_field.data_type(), &d_type);
assert_eq!(new_data_field.description(), Some("new description"));
}

#[test]
fn test_escape_identifier() {
let escaped_identifier = escape_identifier("\"identifier\"");
assert_eq!(escaped_identifier, "\"\"identifier\"\"");
}

#[test]
fn test_escape_single_quotes() {
let escaped_text = escape_single_quotes("text with 'single' quotes");
assert_eq!(escaped_text, "text with ''single'' quotes");
}
}