Skip to content

Commit 93bc80c

Browse files
authored
Only parse FROM identifier in CTE if using Hive (apache#2241)
1 parent 67d684b commit 93bc80c

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/dialect/hive.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ impl Dialect for HiveDialect {
7272
fn supports_group_by_with_modifier(&self) -> bool {
7373
true
7474
}
75+
76+
// TODO: The parsing of the FROM keyword seems wrong, as it happens within the CTE.
77+
// See https://github.com/apache/datafusion-sqlparser-rs/issues/2236 for more details.
78+
/// See <https://hive.apache.org/docs/latest/language/common-table-expression/>
79+
fn supports_from_first_insert(&self) -> bool {
80+
true
81+
}
7582
}

src/dialect/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ pub trait Dialect: Debug + Any {
658658
false
659659
}
660660

661+
/// Return true if the dialect supports "FROM-first" inserts.
662+
///
663+
/// Example:
664+
/// ```sql
665+
/// WITH cte AS (SELECT key FROM src)
666+
/// FROM cte
667+
/// INSERT OVERWRITE table my_table
668+
/// SELECT *
669+
///
670+
/// See <https://hive.apache.org/docs/latest/language/common-table-expression/>
671+
/// ```
672+
fn supports_from_first_insert(&self) -> bool {
673+
false
674+
}
675+
661676
/// Return true if the dialect supports pipe operator.
662677
///
663678
/// Example:

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14511,7 +14511,7 @@ impl<'a> Parser<'a> {
1451114511
materialized: is_materialized,
1451214512
closing_paren_token: closing_paren_token.into(),
1451314513
};
14514-
if self.parse_keyword(Keyword::FROM) {
14514+
if self.dialect.supports_from_first_insert() && self.parse_keyword(Keyword::FROM) {
1451514515
cte.from = Some(self.parse_identifier()?);
1451614516
}
1451714517
Ok(cte)

tests/sqlparser_common.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16218,10 +16218,37 @@ fn test_select_from_first() {
1621816218
pipe_operators: vec![],
1621916219
};
1622016220
assert_eq!(expected, ast);
16221-
assert_eq!(ast.to_string(), q);
1622216221
}
1622316222
}
1622416223

16224+
#[test]
16225+
fn test_select_from_first_with_cte() {
16226+
let dialects = all_dialects_where(|d| d.supports_from_first_select());
16227+
let q = "WITH test AS (FROM t SELECT a) FROM test SELECT 1";
16228+
16229+
let ast = dialects.verified_query(q);
16230+
16231+
let ast_select = ast.body.as_select().unwrap();
16232+
16233+
let expected_body_select_projection =
16234+
vec![SelectItem::UnnamedExpr(Expr::Value(ValueWithSpan {
16235+
value: test_utils::number("1"),
16236+
span: Span::empty(),
16237+
}))];
16238+
16239+
let expected_body_from = vec![TableWithJoins {
16240+
relation: table_from_name(ObjectName::from(vec![Ident {
16241+
value: "test".to_string(),
16242+
quote_style: None,
16243+
span: Span::empty(),
16244+
}])),
16245+
joins: vec![],
16246+
}];
16247+
16248+
assert_eq!(ast_select.projection, expected_body_select_projection);
16249+
assert_eq!(ast_select.from, expected_body_from);
16250+
}
16251+
1622516252
#[test]
1622616253
fn test_geometric_unary_operators() {
1622716254
// Number of points in path or polygon

0 commit comments

Comments
 (0)