Skip to content

Commit 372e5a6

Browse files
committed
Merge remote-tracking branch 'upstream/main' into internal_column
2 parents f284cdc + 851bd3a commit 372e5a6

File tree

56 files changed

+3523
-1171
lines changed

Some content is hidden

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

56 files changed

+3523
-1171
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
---
22
title: Arithmetic Operators
3-
title_includes: plus, minus, multiply, div, divide, mod, caret, modulo, negate, +, -, /, %, *, ^
3+
title_includes: plus, minus, multiply, div, divide, mod, caret, modulo, negate, +, -, /, %, *, ^, |/, ||/, @, &, |, #, ~, <<, >>
44
---
55

6-
| Operator | Description | Example | Result |
7-
|---------------|-------------------------------------------------------|-----------|--------|
8-
| **+ (unary)** | Returns `a` | **+5** | 5 |
9-
| **+** | Adds two numeric expressions | **4 + 1** | 5 |
10-
| **- (unary)** | Negates the numeric expression | **-5** | -5 |
11-
| **-** | Subtract two numeric expressions | **4 - 1** | 3 |
12-
| __*__ | Multiplies two numeric expressions | **4 * 1** | 4 |
13-
| **/** | Divides one numeric expression (`a`) by another (`b`) | **4 / 2** | 2 |
14-
| **%** | Computes the modulo of numeric expression | **4 % 2** | 0 |
15-
| **^** | Computes the exponentiation of numeric expression | **4 ^ 2** | 16 |
6+
| Operator | Description | Example | Result |
7+
|-----------------------|--------------------------------------------------------|----------------------------|--------|
8+
| **+ (unary)** | Returns `a` | **+5** | 5 |
9+
| **+** | Adds two numeric expressions | **4 + 1** | 5 |
10+
| **- (unary)** | Negates the numeric expression | **-5** | -5 |
11+
| **-** | Subtract two numeric expressions | **4 - 1** | 3 |
12+
| __*__ | Multiplies two numeric expressions | **4 * 1** | 4 |
13+
| **/** | Divides one numeric expression (`a`) by another (`b`) | **4 / 2** | 2 |
14+
| **%** | Computes the modulo of numeric expression | **4 % 2** | 0 |
15+
| **^** | Computes the exponentiation of numeric expression | **4 ^ 2** | 16 |
16+
| **&verbar;/** | Computes the square root of numeric expression | **&verbar;/ 25.0** | 5 |
17+
| **&verbar;&verbar;/** | Computes the cube root of numeric expression | **&verbar;&verbar;/ 27.0** | 3 |
18+
| **@** | Computes the abs of numeric expression | **@ -5.0** | 5 |
19+
| **&** | Computes the bitwise and of numeric expression | **91 & 15** | 11 |
20+
| **&verbar;** | Computes the bitwise or of numeric expression | **32 &verbar; 3** | 35 |
21+
| **#** | Computes the bitwise xor of numeric expression | **17 # 5** | 20 |
22+
| **~** | Computes the bitwise not of numeric expression | **~ 1** | ~2 |
23+
| **<<** | Computes the bitwise shift left of numeric expression | **1 << 4** | 16 |
24+
| **>>** | Computes the bitwise shift right of numeric expression | **8 >> 2** | 2 |

docs/doc/15-sql-functions/20-numeric-functions/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ The table below shows the available numeric functions.
3535
| **SIGN(x)** | Returns the sign of the argument as -1, 0, or 1, depending on whether x is negative, zero, or positive or NULL if the argument was NULL. | **SIGN(0)** | 0 |
3636
| **SIN(x)** | Returns the sine of x, where x is given in radians. | **SIN(90)** | 0.8939966636005579 |
3737
| **SQRT(x)** | Returns the square root of a nonnegative number x. Return Nan for negative input. | **SQRT(4)** | 2.0 |
38+
| **CBRT(x)** | Returns the cube root of a nonnegative number x. | **CURT(27)** | 3.0 |
3839
| **TAN(x)** | Returns the tangent of x, where x is given in radians. | **TAN(90)** | -1.995200412208242 |
39-
| **TRUNCATE(x, d)** | Returns the number x, truncated to d decimal places. If d is 0, the result has no decimal point or fractional part. d can be negative to cause d digits left of the decimal point of the value X to become zero. The maximum absolute value for d is 30; any digits in excess of 30 (or -30) are truncated. | **TRUNCATE(1.223, 1)** | 1.2 |
40+
| **TRUNCATE(x, d)** | Returns the number x, truncated to d decimal places. If d is 0, the result has no decimal point or fractional part. d can be negative to cause d digits left of the decimal point of the value X to become zero. The maximum absolute value for d is 30; any digits in excess of 30 (or -30) are truncated. | **TRUNCATE(1.223, 1)** | 1.2 |
41+
| **FACTORIAL(x)** | Returns the factorial logarithm of x. If x is less than or equal to 0, the function returns 0. | **FACTORIAL(5)** | 120 |

src/query/ast/benches/bench.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern crate criterion;
1818
use common_ast::parser::parse_expr;
1919
use common_ast::parser::parse_sql;
2020
use common_ast::parser::tokenize_sql;
21-
use common_ast::Backtrace;
2221
use common_ast::Dialect;
2322
use criterion::black_box;
2423
use criterion::Criterion;
@@ -31,35 +30,31 @@ fn bench(c: &mut Criterion) {
3130
b.iter(|| {
3231
let case = r#"explain SELECT SUM(count) FROM (SELECT ((((((((((((true)and(true)))or((('614')like('998831')))))or(false)))and((true IN (true, true, (-1014651046 NOT BETWEEN -1098711288 AND -1158262473))))))or((('780820706')=('')))) IS NOT NULL AND ((((((((((true)AND(true)))or((('614')like('998831')))))or(false)))and((true IN (true, true, (-1014651046 NOT BETWEEN -1098711288 AND -1158262473))))))OR((('780820706')=(''))))) ::INT64)as count FROM t0) as res;"#;
3332
let tokens = tokenize_sql(case).unwrap();
34-
let backtrace = Backtrace::new();
35-
let (stmt, _) = parse_sql(&tokens, Dialect::PostgreSQL, &backtrace).unwrap();
33+
let (stmt, _) = parse_sql(&tokens, Dialect::PostgreSQL).unwrap();
3634
black_box(stmt);
3735
})
3836
});
3937
group.bench_function("large_query", |b| {
4038
b.iter(|| {
4139
let case = r#"SELECT SUM(count) FROM (SELECT ((((((((((((true)and(true)))or((('614')like('998831')))))or(false)))and((true IN (true, true, (-1014651046 NOT BETWEEN -1098711288 AND -1158262473))))))or((('780820706')=('')))) IS NOT NULL AND ((((((((((true)AND(true)))or((('614')like('998831')))))or(false)))and((true IN (true, true, (-1014651046 NOT BETWEEN -1098711288 AND -1158262473))))))OR((('780820706')=(''))))) ::INT64)as count FROM t0) as res;"#;
4240
let tokens = tokenize_sql(case).unwrap();
43-
let backtrace = Backtrace::new();
44-
let (stmt, _) = parse_sql(&tokens, Dialect::PostgreSQL, &backtrace).unwrap();
41+
let (stmt, _) = parse_sql(&tokens, Dialect::PostgreSQL).unwrap();
4542
black_box(stmt);
4643
})
4744
});
4845
group.bench_function("wide_expr", |b| {
4946
b.iter(|| {
5047
let case = r#"a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a AND a"#;
5148
let tokens = tokenize_sql(case).unwrap();
52-
let backtrace = Backtrace::new();
53-
let expr = parse_expr(&tokens, Dialect::PostgreSQL, &backtrace).unwrap();
49+
let expr = parse_expr(&tokens, Dialect::PostgreSQL).unwrap();
5450
black_box(expr);
5551
})
5652
});
5753
group.bench_function("deep_expr", |b| {
5854
b.iter(|| {
5955
let case = r#"((((((((((((((((((((((((((((((1))))))))))))))))))))))))))))))"#;
6056
let tokens = tokenize_sql(case).unwrap();
61-
let backtrace = Backtrace::new();
62-
let expr = parse_expr(&tokens, Dialect::PostgreSQL, &backtrace).unwrap();
57+
let expr = parse_expr(&tokens, Dialect::PostgreSQL).unwrap();
6358
black_box(expr);
6459
})
6560
});

src/query/ast/src/ast/expr.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ pub enum BinaryOperator {
522522
BitwiseOr,
523523
BitwiseAnd,
524524
BitwiseXor,
525+
BitwiseShiftLeft,
526+
BitwiseShiftRight,
525527
}
526528

527529
impl BinaryOperator {
@@ -548,6 +550,9 @@ impl BinaryOperator {
548550
BinaryOperator::BitwiseOr => "bit_or".to_string(),
549551
BinaryOperator::BitwiseAnd => "bit_and".to_string(),
550552
BinaryOperator::BitwiseXor => "bit_xor".to_string(),
553+
BinaryOperator::BitwiseShiftLeft => "bit_shift_left".to_string(),
554+
BinaryOperator::BitwiseShiftRight => "bit_shift_right".to_string(),
555+
BinaryOperator::Caret => "pow".to_string(),
551556
_ => {
552557
let name = format!("{:?}", self);
553558
name.to_lowercase()
@@ -561,6 +566,25 @@ pub enum UnaryOperator {
561566
Plus,
562567
Minus,
563568
Not,
569+
Factorial,
570+
SquareRoot,
571+
CubeRoot,
572+
Abs,
573+
BitwiseNot,
574+
}
575+
576+
impl UnaryOperator {
577+
pub fn to_func_name(&self) -> String {
578+
match self {
579+
UnaryOperator::SquareRoot => "sqrt".to_string(),
580+
UnaryOperator::CubeRoot => "cbrt".to_string(),
581+
UnaryOperator::BitwiseNot => "bit_not".to_string(),
582+
_ => {
583+
let name = format!("{:?}", self);
584+
name.to_lowercase()
585+
}
586+
}
587+
}
564588
}
565589

566590
impl Expr {
@@ -637,6 +661,21 @@ impl Display for UnaryOperator {
637661
UnaryOperator::Not => {
638662
write!(f, "NOT")
639663
}
664+
UnaryOperator::SquareRoot => {
665+
write!(f, "|/")
666+
}
667+
UnaryOperator::CubeRoot => {
668+
write!(f, "||/")
669+
}
670+
UnaryOperator::Factorial => {
671+
write!(f, "!")
672+
}
673+
UnaryOperator::Abs => {
674+
write!(f, "@")
675+
}
676+
UnaryOperator::BitwiseNot => {
677+
write!(f, "~")
678+
}
640679
}
641680
}
642681
}
@@ -722,6 +761,12 @@ impl Display for BinaryOperator {
722761
BinaryOperator::BitwiseXor => {
723762
write!(f, "#")
724763
}
764+
BinaryOperator::BitwiseShiftLeft => {
765+
write!(f, "<<")
766+
}
767+
BinaryOperator::BitwiseShiftRight => {
768+
write!(f, ">>")
769+
}
725770
}
726771
}
727772
}
@@ -1006,7 +1051,15 @@ impl Display for Expr {
10061051
write!(f, " BETWEEN {low} AND {high}")?;
10071052
}
10081053
Expr::UnaryOp { op, expr, .. } => {
1009-
write!(f, "({op} {expr})")?;
1054+
match op {
1055+
// TODO (xieqijun) Maybe special attribute are provided to check whether the symbol is before or after.
1056+
UnaryOperator::Factorial => {
1057+
write!(f, "({expr} {op})")?;
1058+
}
1059+
_ => {
1060+
write!(f, "({op} {expr})")?;
1061+
}
1062+
}
10101063
}
10111064
Expr::BinaryOp {
10121065
op, left, right, ..

src/query/ast/src/ast/statements/stage.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ pub struct SelectStageOptions {
9090
pub connection: BTreeMap<String, String>,
9191
}
9292

93+
impl SelectStageOptions {
94+
pub fn is_empty(&self) -> bool {
95+
self.files.is_none()
96+
&& self.pattern.is_none()
97+
&& self.file_format.is_none()
98+
&& self.connection.is_empty()
99+
}
100+
}
101+
93102
// SELECT <columns> FROM
94103
// {@<stage_name>[/<path>] | '<uri>'} [(
95104
// [ PARTTERN => '<regex_pattern>']

src/query/ast/src/parser/expr.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ impl<'a, I: Iterator<Item = WithSpan<'a, ExprElement>>> PrattParser<I> for ExprP
369369

370370
UnaryOperator::Plus => Affix::Prefix(Precedence(50)),
371371
UnaryOperator::Minus => Affix::Prefix(Precedence(50)),
372+
UnaryOperator::BitwiseNot => Affix::Prefix(Precedence(50)),
373+
UnaryOperator::SquareRoot => Affix::Prefix(Precedence(60)),
374+
UnaryOperator::CubeRoot => Affix::Prefix(Precedence(60)),
375+
UnaryOperator::Abs => Affix::Prefix(Precedence(60)),
376+
UnaryOperator::Factorial => Affix::Postfix(Precedence(60)),
372377
},
373378
ExprElement::BinaryOp { op } => match op {
374379
BinaryOperator::Or => Affix::Infix(Precedence(5), Associativity::Left),
@@ -392,6 +397,13 @@ impl<'a, I: Iterator<Item = WithSpan<'a, ExprElement>>> PrattParser<I> for ExprP
392397
BinaryOperator::BitwiseAnd => Affix::Infix(Precedence(22), Associativity::Left),
393398
BinaryOperator::BitwiseXor => Affix::Infix(Precedence(22), Associativity::Left),
394399

400+
BinaryOperator::BitwiseShiftLeft => {
401+
Affix::Infix(Precedence(23), Associativity::Left)
402+
}
403+
BinaryOperator::BitwiseShiftRight => {
404+
Affix::Infix(Precedence(23), Associativity::Left)
405+
}
406+
395407
BinaryOperator::Xor => Affix::Infix(Precedence(24), Associativity::Left),
396408

397409
BinaryOperator::Plus => Affix::Infix(Precedence(30), Associativity::Left),
@@ -664,6 +676,11 @@ impl<'a, I: Iterator<Item = WithSpan<'a, ExprElement>>> PrattParser<I> for ExprP
664676
target_type,
665677
pg_style: true,
666678
},
679+
ExprElement::UnaryOp { op } => Expr::UnaryOp {
680+
span: transform_span(elem.span.0),
681+
op,
682+
expr: Box::new(lhs),
683+
},
667684
_ => unreachable!(),
668685
};
669686
Ok(expr)
@@ -1130,7 +1147,14 @@ pub fn window_frame_bound(i: Input) -> IResult<WindowFrameBound> {
11301147

11311148
pub fn unary_op(i: Input) -> IResult<UnaryOperator> {
11321149
// Plus and Minus are parsed as binary op at first.
1133-
value(UnaryOperator::Not, rule! { NOT })(i)
1150+
alt((
1151+
value(UnaryOperator::Not, rule! { NOT }),
1152+
value(UnaryOperator::Factorial, rule! { Factorial}),
1153+
value(UnaryOperator::SquareRoot, rule! { SquareRoot}),
1154+
value(UnaryOperator::BitwiseNot, rule! {BitWiseNot}),
1155+
value(UnaryOperator::CubeRoot, rule! { CubeRoot}),
1156+
value(UnaryOperator::Abs, rule! { Abs}),
1157+
))(i)
11341158
}
11351159

11361160
pub fn binary_op(i: Input) -> IResult<BinaryOperator> {
@@ -1161,9 +1185,11 @@ pub fn binary_op(i: Input) -> IResult<BinaryOperator> {
11611185
value(BinaryOperator::NotRegexp, rule! { NOT ~ REGEXP }),
11621186
value(BinaryOperator::RLike, rule! { RLIKE }),
11631187
value(BinaryOperator::NotRLike, rule! { NOT ~ RLIKE }),
1164-
value(BinaryOperator::BitwiseOr, rule! { "|" }),
1165-
value(BinaryOperator::BitwiseAnd, rule! { "&" }),
1166-
value(BinaryOperator::BitwiseXor, rule! { "#" }),
1188+
value(BinaryOperator::BitwiseOr, rule! { BitWiseOr }),
1189+
value(BinaryOperator::BitwiseAnd, rule! { BitWiseAnd }),
1190+
value(BinaryOperator::BitwiseXor, rule! { BitWiseXor }),
1191+
value(BinaryOperator::BitwiseShiftLeft, rule! { ShiftLeft }),
1192+
value(BinaryOperator::BitwiseShiftRight, rule! { ShiftRight }),
11671193
)),
11681194
))(i)
11691195
}

0 commit comments

Comments
 (0)