Skip to content

Commit 526eba0

Browse files
authored
Merge pull request #1384 from benesch/absorb-sqlparser
sql-parser: absorb separate sql parser repository
2 parents 15c841c + 63d2c24 commit 526eba0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+14306
-52
lines changed

Cargo.lock

Lines changed: 36 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,3 @@ members = [
2020

2121
[profile.release]
2222
debug = true
23-
24-
# This is a slightly confusing syntax because of the fact that ssh urls use `:`
25-
# instead of `/` to separate the domain, so this serves as an example
26-
#
27-
# [patch."ssh://[email protected]/MaterializeInc/sqlparser.git"]
28-
# sqlparser = { path = "../sqlparser" }

src/repr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pretty = "0.7.1"
1717
regex = "1.3.1"
1818
serde = { version = "1.0", features = ["derive"] }
1919
serde_regex = "0.4.0"
20-
sqlparser = { git = "ssh://[email protected]/MaterializeInc/sqlparser.git" }
20+
sql-parser = { path = "../sql-parser" }
2121
itertools = "0.8.2"
2222

2323
[dev-dependencies]

src/repr/qualname.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::str::FromStr;
1111

1212
use itertools::Itertools;
1313
use serde::{Deserialize, Serialize};
14-
use sqlparser::ast::{Ident, ObjectName};
14+
use sql_parser::ast::{Ident, ObjectName};
1515

1616
use crate::errors::{Error as ReprError, Result};
1717

@@ -29,7 +29,7 @@ use crate::errors::{Error as ReprError, Result};
2929
///
3030
/// ```
3131
/// use std::convert::TryFrom;
32-
/// use sqlparser::ast::{Ident, ObjectName};
32+
/// use sql_parser::ast::{Ident, ObjectName};
3333
/// use repr::QualName;
3434
///
3535
/// let with = Ident::with_quote('"', "one");
@@ -56,7 +56,7 @@ impl QualName {
5656
///
5757
/// ```
5858
/// use repr::QualName;
59-
/// use sqlparser::ast::Ident;
59+
/// use sql_parser::ast::Ident;
6060
///
6161
/// let (one, two) = (Ident::new("HoWdY"), Ident::with_quote('"', "wOwZeR"));
6262
/// assert_eq!(
@@ -136,7 +136,7 @@ impl QualName {
136136
///
137137
/// ```
138138
/// use repr::QualName;
139-
/// use sqlparser::ast::{ObjectName, Ident};
139+
/// use sql_parser::ast::{ObjectName, Ident};
140140
///
141141
/// let one = Ident::new("one");
142142
/// assert!(QualName::name_equals(ObjectName(vec![one.clone()]), "one"));

src/repr/scalar/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
1010
use failure::format_err;
1111
use ordered_float::OrderedFloat;
1212
use serde::{Deserialize, Serialize};
13-
use sqlparser::ast::Interval as SqlInterval;
13+
use sql_parser::ast::Interval as SqlInterval;
1414

1515
use self::decimal::Significand;
1616
use crate::{ColumnType, DatumDict, DatumList};

src/sql-parser/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "sql-parser"
3+
description = "The lexer and parser for Materialize's SQL dialect."
4+
version = "0.1.0"
5+
edition = "2018"
6+
7+
[dependencies]
8+
log = "0.4.5"
9+
10+
[dev-dependencies]
11+
simple_logger = "1.0.1"
12+
matches = "0.1"

src/sql-parser/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Materialize SQL parser
2+
3+
This parser is a fork of <https://github.com/andygrove/sqlparser-rs>, with
4+
some additional patches from <http://github.com/nickolay/sqlparser-rs>.
5+
6+
We decided to jettison compatibility with upstream so that we can perform
7+
large-scale refactors and rip out code for supporting other dialects, like
8+
MySQL and MSSQL, in favor of parsing just Materialize's SQL dialect.

src/sql-parser/src/ast/data_type.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2018 sqlparser-rs contributors. All rights reserved.
2+
// Copyright 2019 Materialize, Inc. All rights reserved.
3+
//
4+
// This file is derived from the sqlparser-rs project, available at
5+
// https://github.com/andygrove/sqlparser-rs. It was incorporated
6+
// directly into Materialize on December 21, 2019.
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License in the LICENSE file at the
11+
// root of this repository, or online at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
21+
use super::ObjectName;
22+
use std::fmt;
23+
24+
/// SQL data types
25+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
26+
pub enum DataType {
27+
/// Fixed-length character type e.g. CHAR(10)
28+
Char(Option<u64>),
29+
/// Variable-length character type e.g. VARCHAR(10)
30+
Varchar(Option<u64>),
31+
/// Uuid type
32+
Uuid,
33+
/// Large character object e.g. CLOB(1000)
34+
Clob(u64),
35+
/// Fixed-length binary type e.g. BINARY(10)
36+
Binary(u64),
37+
/// Variable-length binary type e.g. VARBINARY(10)
38+
Varbinary(u64),
39+
/// Large binary object e.g. BLOB(1000)
40+
Blob(u64),
41+
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
42+
Decimal(Option<u64>, Option<u64>),
43+
/// Floating point with optional precision e.g. FLOAT(8)
44+
Float(Option<u64>),
45+
/// Small integer
46+
SmallInt,
47+
/// Integer
48+
Int,
49+
/// Big integer
50+
BigInt,
51+
/// Floating point e.g. REAL
52+
Real,
53+
/// Double e.g. DOUBLE PRECISION
54+
Double,
55+
/// Boolean
56+
Boolean,
57+
/// Date
58+
Date,
59+
/// Time without time zone
60+
Time,
61+
/// Time with time zone
62+
TimeTz,
63+
/// Timestamp without time zone
64+
Timestamp,
65+
/// Timestamp with time zone
66+
TimestampTz,
67+
/// Interval
68+
Interval,
69+
/// Regclass used in postgresql serial
70+
Regclass,
71+
/// Text
72+
Text,
73+
/// Bytea
74+
Bytea,
75+
/// Custom type such as enums
76+
Custom(ObjectName),
77+
/// Arrays
78+
Array(Box<DataType>),
79+
}
80+
81+
impl fmt::Display for DataType {
82+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83+
match self {
84+
DataType::Char(size) => format_type_with_optional_length(f, "char", size),
85+
DataType::Varchar(size) => {
86+
format_type_with_optional_length(f, "character varying", size)
87+
}
88+
DataType::Uuid => write!(f, "uuid"),
89+
DataType::Clob(size) => write!(f, "clob({})", size),
90+
DataType::Binary(size) => write!(f, "binary({})", size),
91+
DataType::Varbinary(size) => write!(f, "varbinary({})", size),
92+
DataType::Blob(size) => write!(f, "blob({})", size),
93+
DataType::Decimal(precision, scale) => {
94+
if let Some(scale) = scale {
95+
write!(f, "numeric({},{})", precision.unwrap(), scale)
96+
} else {
97+
format_type_with_optional_length(f, "numeric", precision)
98+
}
99+
}
100+
DataType::Float(size) => format_type_with_optional_length(f, "float", size),
101+
DataType::SmallInt => write!(f, "smallint"),
102+
DataType::Int => write!(f, "int"),
103+
DataType::BigInt => write!(f, "bigint"),
104+
DataType::Real => write!(f, "real"),
105+
DataType::Double => write!(f, "double"),
106+
DataType::Boolean => write!(f, "boolean"),
107+
DataType::Date => write!(f, "date"),
108+
DataType::Time => write!(f, "time"),
109+
DataType::TimeTz => write!(f, "time with time zone"),
110+
DataType::Timestamp => write!(f, "timestamp"),
111+
DataType::TimestampTz => write!(f, "timestamp with time zone"),
112+
DataType::Interval => write!(f, "interval"),
113+
DataType::Regclass => write!(f, "regclass"),
114+
DataType::Text => write!(f, "text"),
115+
DataType::Bytea => write!(f, "bytea"),
116+
DataType::Array(ty) => write!(f, "{}[]", ty),
117+
DataType::Custom(ty) => write!(f, "{}", ty),
118+
}
119+
}
120+
}
121+
122+
fn format_type_with_optional_length(
123+
f: &mut fmt::Formatter,
124+
sql_type: &'static str,
125+
len: &Option<u64>,
126+
) -> fmt::Result {
127+
write!(f, "{}", sql_type)?;
128+
if let Some(len) = len {
129+
write!(f, "({})", len)?;
130+
}
131+
Ok(())
132+
}

0 commit comments

Comments
 (0)