Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub use self::mysql::MySqlDialect;
pub use self::oracle::OracleDialect;
pub use self::postgresql::PostgreSqlDialect;
pub use self::redshift::RedshiftSqlDialect;
pub use self::snowflake::parse_snowflake_stage_name;
pub use self::snowflake::SnowflakeDialect;
pub use self::sqlite::SQLiteDialect;
use crate::ast::{ColumnOption, Expr, GranteesType, Ident, ObjectNamePart, Statement};
Expand Down
4 changes: 3 additions & 1 deletion src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ pub fn parse_stage_name_identifier(parser: &mut Parser) -> Result<Ident, ParserE
parser.prev_token();
break;
}
Token::RParen => {
Token::LParen | Token::RParen => {
parser.prev_token();
break;
}
Expand All @@ -1167,6 +1167,8 @@ pub fn parse_stage_name_identifier(parser: &mut Parser) -> Result<Ident, ParserE
Ok(Ident::new(ident))
}

/// Parses a Snowflake stage name, which may start with `@` for internal stages.
/// Examples: `@mystage`, `@namespace.stage`, `schema.table`
pub fn parse_snowflake_stage_name(parser: &mut Parser) -> Result<ObjectName, ParserError> {
match parser.next_token().token {
Token::AtSign => {
Expand Down
46 changes: 46 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,11 @@ impl<'a> Parser<'a> {
// SQLite has single-quoted identifiers
id_parts.push(Ident::with_quote('\'', s))
}
Token::Placeholder(s) => {
// Snowflake uses $1, $2, etc. for positional column references
// in staged data queries like: SELECT t.$1 FROM @stage t
id_parts.push(Ident::new(s))
}
Token::Mul => {
return Ok(Expr::QualifiedWildcard(
ObjectName::from(id_parts),
Expand Down Expand Up @@ -1898,6 +1903,13 @@ impl<'a> Parser<'a> {
chain.push(AccessExpr::Dot(expr));
self.advance_token(); // The consumed string
}
Token::Placeholder(s) => {
// Snowflake uses $1, $2, etc. for positional column references
// in staged data queries like: SELECT t.$1 FROM @stage t
let expr = Expr::Identifier(Ident::with_span(next_token.span, s));
chain.push(AccessExpr::Dot(expr));
self.advance_token(); // The consumed placeholder
}
// Fallback to parsing an arbitrary expression.
_ => match self.parse_subexpr(self.dialect.prec_value(Precedence::Period))? {
// If we get back a compound field access or identifier,
Expand Down Expand Up @@ -15103,6 +15115,11 @@ impl<'a> Parser<'a> {
&& self.peek_keyword_with_tokens(Keyword::SEMANTIC_VIEW, &[Token::LParen])
{
self.parse_semantic_view_table_factor()
} else if dialect_of!(self is SnowflakeDialect)
&& self.peek_token_ref().token == Token::AtSign
{
// Snowflake stage reference: @mystage or @namespace.stage
self.parse_snowflake_stage_table_factor()
} else {
let name = self.parse_object_name(true)?;

Expand Down Expand Up @@ -15199,6 +15216,35 @@ impl<'a> Parser<'a> {
}
}

/// Parse a Snowflake stage reference as a table factor.
/// Handles syntax like: `@mystage1 (file_format => 'myformat', pattern => '...')`
fn parse_snowflake_stage_table_factor(&mut self) -> Result<TableFactor, ParserError> {
// Parse the stage name starting with @
let name = crate::dialect::parse_snowflake_stage_name(self)?;

// Parse optional stage options like (file_format => 'myformat', pattern => '...')
let args = if self.consume_token(&Token::LParen) {
Some(self.parse_table_function_args()?)
} else {
None
};

let alias = self.maybe_parse_table_alias()?;

Ok(TableFactor::Table {
name,
alias,
args,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
})
}

fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSample>>, ParserError> {
let modifier = if self.parse_keyword(Keyword::TABLESAMPLE) {
TableSampleModifier::TableSample
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4533,3 +4533,8 @@ fn test_alter_external_table() {
snowflake()
.verified_stmt("ALTER EXTERNAL TABLE IF EXISTS some_table REFRESH 'year=2025/month=12/'");
}

#[test]
fn test_select_dollar_column_from_stage() {
snowflake().verified_stmt("SELECT t.$1, t.$2 FROM @mystage1(file_format => 'myformat', pattern => '.*data.*[.]csv.gz') t");
}