fn parse_data_type(&mut self) -> Result<UnresolvedDataType, ParserError> {
        let other = |name: &str| UnresolvedDataType::Other {
            name: RawObjectName::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"
                    };
                    UnresolvedDataType::Other {
                        name: RawObjectName::Name(UnresolvedObjectName::unqualified(name)),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
                BPCHAR => UnresolvedDataType::Other {
                    name: RawObjectName::Name(UnresolvedObjectName::unqualified("bpchar")),
                    typ_mod: self.parse_typ_mod()?,
                },
                VARCHAR => UnresolvedDataType::Other {
                    name: RawObjectName::Name(UnresolvedObjectName::unqualified("varchar")),
                    typ_mod: self.parse_typ_mod()?,
                },
                STRING => other("text"),

                // Number-like types
                BIGINT => other("int8"),
                SMALLINT => other("int2"),
                DEC | DECIMAL => UnresolvedDataType::Other {
                    name: RawObjectName::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();
                    UnresolvedDataType::Other {
                        name: RawObjectName::Name(self.parse_object_name()?),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
            },
            Some(Token::Ident(_) | Token::LBracket) => {
                self.prev_token();
                UnresolvedDataType::Other {
                    name: self.parse_raw_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 = UnresolvedDataType::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, UnresolvedDataType::Array(_)) {
                        data_type = UnresolvedDataType::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<UnresolvedDataType, ParserError> {
        let other = |name: &str| UnresolvedDataType::Other {
            name: RawObjectName::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"
                    };
                    UnresolvedDataType::Other {
                        name: RawObjectName::Name(UnresolvedObjectName::unqualified(name)),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
                BPCHAR => UnresolvedDataType::Other {
                    name: RawObjectName::Name(UnresolvedObjectName::unqualified("bpchar")),
                    typ_mod: self.parse_typ_mod()?,
                },
                VARCHAR => UnresolvedDataType::Other {
                    name: RawObjectName::Name(UnresolvedObjectName::unqualified("varchar")),
                    typ_mod: self.parse_typ_mod()?,
                },
                STRING => other("text"),
                // Number-like types
                BIGINT => other("int8"),
                SMALLINT => other("int2"),
                DEC | DECIMAL => UnresolvedDataType::Other {
                    name: RawObjectName::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();
                    UnresolvedDataType::Other {
                        name: RawObjectName::Name(self.parse_object_name()?),
                        typ_mod: self.parse_typ_mod()?,
                    }
                }
            },
            Some(Token::Ident(_) | Token::LBracket) => {
                self.prev_token();
                UnresolvedDataType::Other {
                    name: self.parse_raw_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 = UnresolvedDataType::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, UnresolvedDataType::Array(_)) {
                        data_type = UnresolvedDataType::Array(Box::new(data_type));
                    }
                }
                _ => break,
            }
        }
        Ok(data_type)
    }
fn parse_data_type ( & mut self ) -> Result < UnresolvedDataType , ParserError > { let other = | name : & str | UnresolvedDataType :: Other { name : RawObjectName :: 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 { " " } ; UnresolvedDataType :: Other { name : RawObjectName :: Name ( UnresolvedObjectName :: unqualified ( name ) ) , typ_mod : self . parse_typ_mod ( ) ? , } } BPCHAR => UnresolvedDataType :: Other { name : RawObjectName :: Name ( UnresolvedObjectName :: unqualified ( " " ) ) , typ_mod : self . parse_typ_mod ( ) ? , } , VARCHAR => UnresolvedDataType :: Other { name : RawObjectName :: Name ( UnresolvedObjectName :: unqualified ( " " ) ) , typ_mod : self . parse_typ_mod ( ) ? , } , STRING => other ( " " ) , // Number-like types BIGINT => other ( " " ) , SMALLINT => other ( " " ) , DEC | DECIMAL => UnresolvedDataType :: Other { name : RawObjectName :: 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 ( ) ; UnresolvedDataType :: Other { name : RawObjectName :: Name ( self . parse_object_name ( ) ? ) , typ_mod : self . parse_typ_mod ( ) ? , } } } , Some ( Token :: Ident ( _ ) | Token :: LBracket ) => { self . prev_token ( ) ; UnresolvedDataType :: Other { name : self . parse_raw_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 = UnresolvedDataType :: 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 UnresolvedDataType Array ( ) ) { data_type = UnresolvedDataType :: 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