|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +#![warn(clippy::all)] |
| 14 | + |
| 15 | +use sqlparser::ast::{BinaryOperator, Expr, Ident, Statement, TableConstraint}; |
| 16 | +use sqlparser::dialect::ArroyoDialect; |
| 17 | +use sqlparser::parser::Parser; |
| 18 | +use sqlparser::test_utils; |
| 19 | +use sqlparser::tokenizer::{Location, Span}; |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_watermark_with_expr() { |
| 23 | + let sql = "CREATE TABLE orders ( |
| 24 | + customer_id INT, |
| 25 | + order_id INT, |
| 26 | + date_string TEXT, |
| 27 | + timestamp TIMESTAMP GENERATED ALWAYS AS (CAST(date_string as TIMESTAMP)), |
| 28 | + WATERMARK FOR timestamp AS timestamp + 5 |
| 29 | + ) WITH ( |
| 30 | + connector = 'kafka', |
| 31 | + format = 'json', |
| 32 | + type = 'source', |
| 33 | + bootstrap_servers = 'localhost:9092', |
| 34 | + topic = 'order_topic' |
| 35 | + )"; |
| 36 | + |
| 37 | + let parse = Parser::parse_sql(&ArroyoDialect {}, sql).unwrap(); |
| 38 | + let Statement::CreateTable(ct) = parse.get(0).unwrap() else { |
| 39 | + panic!("not create table") |
| 40 | + }; |
| 41 | + |
| 42 | + assert_eq!( |
| 43 | + ct.constraints, |
| 44 | + vec![TableConstraint::Watermark { |
| 45 | + column_name: Ident::new("timestamp"), |
| 46 | + watermark_expr: Some(Expr::BinaryOp { |
| 47 | + left: Box::new(Expr::Identifier(Ident::new("timestamp"))), |
| 48 | + op: BinaryOperator::Plus, |
| 49 | + right: Box::new(Expr::Value(test_utils::number("5").with_span(Span::new( |
| 50 | + Location::new(5, 4), Location::new(5, 10) |
| 51 | + )))), |
| 52 | + }), |
| 53 | + }] |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_watermark_without_expr() { |
| 59 | + let sql = "CREATE TABLE users ( |
| 60 | + customer_id INT, |
| 61 | + timestamp TIMESTAMP, |
| 62 | + WATERMARK FOR timestamp |
| 63 | + ) WITH ( |
| 64 | + connector = 'kafka', |
| 65 | + format = 'json', |
| 66 | + type = 'source', |
| 67 | + bootstrap_servers = 'localhost:9092', |
| 68 | + topic = 'order_topic' |
| 69 | + )"; |
| 70 | + |
| 71 | + let parse = Parser::parse_sql(&ArroyoDialect {}, sql).unwrap(); |
| 72 | + let Statement::CreateTable(ct) = parse.get(0).unwrap() else { |
| 73 | + panic!("not create table") |
| 74 | + }; |
| 75 | + |
| 76 | + assert_eq!( |
| 77 | + ct.constraints, |
| 78 | + vec![TableConstraint::Watermark { |
| 79 | + column_name: Ident::new("timestamp"), |
| 80 | + watermark_expr: None, |
| 81 | + }] |
| 82 | + ); |
| 83 | +} |
0 commit comments