fn parse_data_type(&mut self) -> Result<DataType<Raw>, ParserError> {
        let other = |name: &str| DataType::Other {
            name: RawName::Name(UnresolvedObjectName::unqualified(name)),
            typ_mod: vec![],
        };

        let mut data_type = match self.next_token() {
            Some(Token::Keyword(kw)) => match kw {
                // Text-like types
                CHAR | CHARACTER => {
                    let name = if self.parse_keyword(VARYING) {
                        "varchar"
                    } else {
                        "bpchar"
                    };
                    DataType::Other {
                        name: RawName::Name(UnresolvedObjectName::unqualified(name)),
                        typ_mod: match self.parse_optional_precision()? {
                            Some(u) => vec![u],
                            None => vec![],
                        },
                    }
                }
                BPCHAR => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("bpchar")),
                    typ_mod: match self.parse_optional_precision()? {
                        Some(u) => vec![u],
                        None => vec![],
                    },
                },
                VARCHAR => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("varchar")),
                    typ_mod: match self.parse_optional_precision()? {
                        Some(u) => vec![u],
                        None => vec![],
                    },
                },
                STRING => other("text"),

                // Number-like types
                BIGINT => other("int8"),
                SMALLINT => other("int2"),
                DEC | DECIMAL => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("numeric")),
                    typ_mod: self.parse_typ_mod()?,
                },
                DOUBLE => {
                    let _ = self.parse_keyword(PRECISION);
                    other("float8")
                }
                FLOAT => match self.parse_optional_precision()?.unwrap_or(53) {
                    v if v == 0 || v > 53 => {
                        return Err(self.error(
                            self.peek_prev_pos(),
                            "precision for type float must be within ([1-53])".into(),
                        ))
                    }
                    v if v < 25 => other("float4"),
                    _ => other("float8"),
                },
                INT | INTEGER => other("int4"),
                REAL => other("float4"),

                // Time-like types
                TIME => {
                    if self.parse_keyword(WITH) {
                        self.expect_keywords(&[TIME, ZONE])?;
                        other("timetz")
                    } else {
                        if self.parse_keyword(WITHOUT) {
                            self.expect_keywords(&[TIME, ZONE])?;
                        }
                        other("time")
                    }
                }
                TIMESTAMP => {
                    if self.parse_keyword(WITH) {
                        self.expect_keywords(&[TIME, ZONE])?;
                        other("timestamptz")
                    } else {
                        if self.parse_keyword(WITHOUT) {
                            self.expect_keywords(&[TIME, ZONE])?;
                        }
                        other("timestamp")
                    }
                }

                // MZ "proprietary" types
                MAP => {
                    return self.parse_map();
                }

                // Misc.
                BOOLEAN => other("bool"),
                BYTES => other("bytea"),
                JSON => other("jsonb"),
                _ => {
                    self.prev_token();
                    DataType::Other {
                        name: RawName::Name(self.parse_object_name()?),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
            },
            Some(Token::Ident(_)) => {
                self.prev_token();
                DataType::Other {
                    name: RawName::Name(self.parse_object_name()?),
                    typ_mod: self.parse_typ_mod()?,
                }
            }
            other => self.expected(self.peek_prev_pos(), "a data type name", other)?,
        };

        loop {
            match self.peek_token() {
                Some(Token::Keyword(LIST)) => {
                    self.next_token();
                    data_type = DataType::List(Box::new(data_type));
                }
                Some(Token::LBracket) => {
                    // Handle array suffixes. Note that `int[]`, `int[][][]`,
                    // and `int[2][2]` all parse to the same "int array" type.
                    self.next_token();
                    let _ = self.maybe_parse(|parser| parser.parse_number_value());
                    self.expect_token(&Token::RBracket)?;
                    if !matches!(data_type, DataType::Array(_)) {
                        data_type = DataType::Array(Box::new(data_type));
                    }
                }
                _ => break,
            }
        }
        Ok(data_type)
    }
/// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
Parse a SQL datatype ( in the context of a CREATE TABLE statement for example )
fn parse_data_type(&mut self) -> Result<DataType<Raw>, ParserError> {
        let other = |name: &str| DataType::Other {
            name: RawName::Name(UnresolvedObjectName::unqualified(name)),
            typ_mod: vec![],
        };
        let mut data_type = match self.next_token() {
            Some(Token::Keyword(kw)) => match kw {
                // Text-like types
                CHAR | CHARACTER => {
                    let name = if self.parse_keyword(VARYING) {
                        "varchar"
                    } else {
                        "bpchar"
                    };
                    DataType::Other {
                        name: RawName::Name(UnresolvedObjectName::unqualified(name)),
                        typ_mod: match self.parse_optional_precision()? {
                            Some(u) => vec![u],
                            None => vec![],
                        },
                    }
                }
                BPCHAR => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("bpchar")),
                    typ_mod: match self.parse_optional_precision()? {
                        Some(u) => vec![u],
                        None => vec![],
                    },
                },
                VARCHAR => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("varchar")),
                    typ_mod: match self.parse_optional_precision()? {
                        Some(u) => vec![u],
                        None => vec![],
                    },
                },
                STRING => other("text"),
                // Number-like types
                BIGINT => other("int8"),
                SMALLINT => other("int2"),
                DEC | DECIMAL => DataType::Other {
                    name: RawName::Name(UnresolvedObjectName::unqualified("numeric")),
                    typ_mod: self.parse_typ_mod()?,
                },
                DOUBLE => {
                    let _ = self.parse_keyword(PRECISION);
                    other("float8")
                }
                FLOAT => match self.parse_optional_precision()?.unwrap_or(53) {
                    v if v == 0 || v > 53 => {
                        return Err(self.error(
                            self.peek_prev_pos(),
                            "precision for type float must be within ([1-53])".into(),
                        ))
                    }
                    v if v < 25 => other("float4"),
                    _ => other("float8"),
                },
                INT | INTEGER => other("int4"),
                REAL => other("float4"),
                // Time-like types
                TIME => {
                    if self.parse_keyword(WITH) {
                        self.expect_keywords(&[TIME, ZONE])?;
                        other("timetz")
                    } else {
                        if self.parse_keyword(WITHOUT) {
                            self.expect_keywords(&[TIME, ZONE])?;
                        }
                        other("time")
                    }
                }
                TIMESTAMP => {
                    if self.parse_keyword(WITH) {
                        self.expect_keywords(&[TIME, ZONE])?;
                        other("timestamptz")
                    } else {
                        if self.parse_keyword(WITHOUT) {
                            self.expect_keywords(&[TIME, ZONE])?;
                        }
                        other("timestamp")
                    }
                }
                // MZ "proprietary" types
                MAP => {
                    return self.parse_map();
                }
                // Misc.
                BOOLEAN => other("bool"),
                BYTES => other("bytea"),
                JSON => other("jsonb"),
                _ => {
                    self.prev_token();
                    DataType::Other {
                        name: RawName::Name(self.parse_object_name()?),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
            },
            Some(Token::Ident(_)) => {
                self.prev_token();
                DataType::Other {
                    name: RawName::Name(self.parse_object_name()?),
                    typ_mod: self.parse_typ_mod()?,
                }
            }
            other => self.expected(self.peek_prev_pos(), "a data type name", other)?,
        };
        loop {
            match self.peek_token() {
                Some(Token::Keyword(LIST)) => {
                    self.next_token();
                    data_type = DataType::List(Box::new(data_type));
                }
                Some(Token::LBracket) => {
                    // Handle array suffixes. Note that `int[]`, `int[][][]`,
                    // and `int[2][2]` all parse to the same "int array" type.
                    self.next_token();
                    let _ = self.maybe_parse(|parser| parser.parse_number_value());
                    self.expect_token(&Token::RBracket)?;
                    if !matches!(data_type, DataType::Array(_)) {
                        data_type = DataType::Array(Box::new(data_type));
                    }
                }
                _ => break,
            }
        }
        Ok(data_type)
    }
fn parse_data_type ( & mut self ) -> Result < DataType < Raw > , ParserError > { let other = | name : & str | DataType :: Other { name : RawName :: Name ( UnresolvedObjectName :: unqualified ( name ) ) , typ_mod : vec ! [ ] , } ; let mut data_type = match self . next_token ( ) { Some ( Token :: Keyword ( kw ) ) => match kw { // Text-like types CHAR | CHARACTER => { let name = if self . parse_keyword ( VARYING ) { " " } else { " " } ; DataType :: Other { name : RawName :: Name ( UnresolvedObjectName :: unqualified ( name ) ) , typ_mod : match self . parse_optional_precision ( ) ? { Some ( u ) => vec ! [ u ] , None => vec ! [ ] , } , } } BPCHAR => DataType :: Other { name : RawName :: Name ( UnresolvedObjectName :: unqualified ( " " ) ) , typ_mod : match self . parse_optional_precision ( ) ? { Some ( u ) => vec ! [ u ] , None => vec ! [ ] , } , } , VARCHAR => DataType :: Other { name : RawName :: Name ( UnresolvedObjectName :: unqualified ( " " ) ) , typ_mod : match self . parse_optional_precision ( ) ? { Some ( u ) => vec ! [ u ] , None => vec ! [ ] , } , } , STRING => other ( " " ) , // Number-like types BIGINT => other ( " " ) , SMALLINT => other ( " " ) , DEC | DECIMAL => DataType :: Other { name : RawName :: Name ( UnresolvedObjectName :: unqualified ( " " ) ) , typ_mod : self . parse_typ_mod ( ) ? , } , DOUBLE => { let _ = self . parse_keyword ( PRECISION ) ; other ( " " ) } FLOAT => match self . parse_optional_precision ( ) ? . unwrap_or ( 53 ) { v if v == 0 || v > 53 => { return Err ( self . error ( self . peek_prev_pos ( ) , " " . into ( ) , ) ) } v if v < 25 => other ( " " ) , _ => other ( " " ) , } , INT | INTEGER => other ( " " ) , REAL => other ( " " ) , // Time-like types TIME => { if self . parse_keyword ( WITH ) { self . expect_keywords ( & [ TIME , ZONE ] ) ? ; other ( " " ) } else { if self . parse_keyword ( WITHOUT ) { self . expect_keywords ( & [ TIME , ZONE ] ) ? ; } other ( " " ) } } TIMESTAMP => { if self . parse_keyword ( WITH ) { self . expect_keywords ( & [ TIME , ZONE ] ) ? ; other ( " " ) } else { if self . parse_keyword ( WITHOUT ) { self . expect_keywords ( & [ TIME , ZONE ] ) ? ; } other ( " " ) } } // MZ "proprietary" types MAP => { return self . parse_map ( ) ; } // Misc. BOOLEAN => other ( " " ) , BYTES => other ( " " ) , JSON => other ( " " ) , _ => { self . prev_token ( ) ; DataType :: Other { name : RawName :: Name ( self . parse_object_name ( ) ? ) , typ_mod : self . parse_typ_mod ( ) ? , } } } , Some ( Token :: Ident ( _ ) ) => { self . prev_token ( ) ; DataType :: Other { name : RawName :: Name ( self . parse_object_name ( ) ? ) , typ_mod : self . parse_typ_mod ( ) ? , } } other => self . expected ( self . peek_prev_pos ( ) , " " , other ) ? , } ; loop { match self . peek_token ( ) { Some ( Token :: Keyword ( LIST ) ) => { self . next_token ( ) ; data_type = DataType :: List ( Box :: new ( data_type ) ) ; } Some ( Token :: LBracket ) => { // Handle array suffixes. Note that `int[]`, `int[][][]`, // and `int[2][2]` all parse to the same "int array" type. self . next_token ( ) ; let _ = self . maybe_parse ( | parser | parser . parse_number_value ( ) ) ; self . expect_token ( & Token :: RBracket ) ? ; if ! matches ! ( data_type DataType Array ( ) ) { data_type = DataType :: Array ( Box :: new ( data_type ) ) ; } } _ => break , } } Ok ( data_type ) }
Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
Parse a SQL datatype ( in the context of a CREATE TABLE statement for example )
&mut self
&mut self