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
14 changes: 7 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ pub use self::query::{
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
TableIndexHintType, TableIndexHints, TableIndexType, TableSample, TableSampleBucket,
TableSampleKind, TableSampleMethod, TableSampleModifier, TableSampleQuantity, TableSampleSeed,
TableSampleSeedModifier, TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity,
UpdateTableFromKind, ValueTableMode, Values, WildcardAdditionalOptions, With, WithFill,
XmlNamespaceDefinition, XmlPassingArgument, XmlPassingClause, XmlTableColumn,
XmlTableColumnOption,
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexDirective,
TableIndexHintForClause, TableIndexHintType, TableIndexHints, TableIndexType, TableSample,
TableSampleBucket, TableSampleKind, TableSampleMethod, TableSampleModifier,
TableSampleQuantity, TableSampleSeed, TableSampleSeedModifier, TableSampleUnit, TableVersion,
TableWithJoins, Top, TopQuantity, UpdateTableFromKind, ValueTableMode, Values,
WildcardAdditionalOptions, With, WithFill, XmlNamespaceDefinition, XmlPassingArgument,
XmlPassingClause, XmlTableColumn, XmlTableColumnOption,
};

pub use self::trigger::{
Expand Down
35 changes: 29 additions & 6 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,30 @@ impl fmt::Display for TableIndexHints {
}
}

/// A table-level index-selection directive, see [`TableFactor::Table::index_directives`].
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum TableIndexDirective {
/// `{USE | IGNORE | FORCE} {INDEX | KEY} [FOR ...] (index_name, ...)`, see
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/index-hints.html)
MySql(TableIndexHints),
/// Require the named index with SQLite's [`INDEXED BY`](https://www.sqlite.org/lang_indexedby.html).
IndexedBy(Ident),
/// Prohibit index use except rowid lookups with SQLite's [`NOT INDEXED`](https://www.sqlite.org/lang_indexedby.html).
NotIndexed,
}

impl fmt::Display for TableIndexDirective {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TableIndexDirective::MySql(hint) => hint.fmt(f),
TableIndexDirective::IndexedBy(name) => write!(f, "INDEXED BY {name}"),
TableIndexDirective::NotIndexed => f.write_str("NOT INDEXED"),
}
}
}

/// A table name or a parenthesized subquery with an optional alias
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -1490,9 +1514,8 @@ pub enum TableFactor {
/// Optional table sample modifier
/// See: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
sample: Option<TableSampleKind>,
/// Optional index hints(mysql)
/// See: <https://dev.mysql.com/doc/refman/8.4/en/index-hints.html>
index_hints: Vec<TableIndexHints>,
/// Optional table-level index-selection hints or requirements.
index_directives: Vec<TableIndexDirective>,
},
/// A derived table (a parenthesized subquery), optionally `LATERAL`.
Derived {
Expand Down Expand Up @@ -2226,7 +2249,7 @@ impl fmt::Display for TableFactor {
with_ordinality,
json_path,
sample,
index_hints,
index_directives,
} => {
name.fmt(f)?;
if let Some(json_path) = json_path {
Expand Down Expand Up @@ -2255,8 +2278,8 @@ impl fmt::Display for TableFactor {
if let Some(alias) = alias {
write!(f, " {alias}")?;
}
if !index_hints.is_empty() {
write!(f, " {}", display_separated(index_hints, " "))?;
if !index_directives.is_empty() {
write!(f, " {}", display_separated(index_directives, " "))?;
}
if !with_hints.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_hints))?;
Expand Down
2 changes: 1 addition & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ impl Spanned for TableFactor {
partitions: _,
json_path: _,
sample: _,
index_hints: _,
index_directives: _,
} => union_spans(
name.0
.iter()
Expand Down
7 changes: 7 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,13 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports SQLite's `INDEXED BY` and `NOT INDEXED` index-selection clauses.
///
/// See <https://www.sqlite.org/lang_indexedby.html>
fn supports_indexed_by(&self) -> bool {
false
}

/// Returns true if this dialect requires a whitespace character after `--` to start a single line comment.
///
/// MySQL: <https://dev.mysql.com/doc/refman/8.4/en/ansi-diff-comments.html>
Expand Down
15 changes: 14 additions & 1 deletion src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alloc::boxed::Box;
use crate::ast::BinaryOperator;
use crate::ast::{Expr, Statement};
use crate::dialect::{Dialect, Precedence};
use crate::keywords::Keyword;
use crate::keywords::{self, Keyword};
use crate::parser::{Parser, ParserError};
use crate::tokenizer::Token;

Expand Down Expand Up @@ -160,4 +160,17 @@ impl Dialect for SQLiteDialect {
fn supports_unterminated_block_comments(&self) -> bool {
true
}

fn supports_indexed_by(&self) -> bool {
true
}

/// `INDEXED BY` and `NOT INDEXED` follow the table name or alias.
fn is_table_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
match kw {
Keyword::INDEXED => false,
Keyword::NOT => !parser.peek_keyword(Keyword::INDEXED),
_ => !keywords::RESERVED_FOR_TABLE_ALIAS.contains(kw),
}
}
}
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ define_keywords!(
INCREMENT,
INCREMENTAL,
INDEX,
INDEXED,
INDICATOR,
INHERIT,
INHERITS,
Expand Down
20 changes: 13 additions & 7 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13670,7 +13670,7 @@ impl<'a> Parser<'a> {
}
}

fn parse_table_index_hints(&mut self) -> Result<Vec<TableIndexHints>, ParserError> {
fn parse_table_index_hints(&mut self) -> Result<Vec<TableIndexDirective>, ParserError> {
let mut hints = vec![];
while let Some(hint_type) =
self.parse_one_of_keywords(&[Keyword::USE, Keyword::IGNORE, Keyword::FORCE])
Expand Down Expand Up @@ -13719,12 +13719,12 @@ impl<'a> Parser<'a> {
vec![]
};
self.expect_token(&Token::RParen)?;
hints.push(TableIndexHints {
hints.push(TableIndexDirective::MySql(TableIndexHints {
hint_type,
index_type,
for_clause,
index_names,
});
}));
}
Ok(hints)
}
Expand Down Expand Up @@ -17112,13 +17112,19 @@ impl<'a> Parser<'a> {

let alias = self.maybe_parse_table_alias()?;

// MYSQL-specific table hints:
let index_hints = if self.dialect.supports_table_hints() {
let mut index_directives = if self.dialect.supports_table_hints() {
self.maybe_parse(|p| p.parse_table_index_hints())?
.unwrap_or(vec![])
} else {
vec![]
};
if self.dialect.supports_indexed_by() {
if self.parse_keywords(&[Keyword::INDEXED, Keyword::BY]) {
index_directives.push(TableIndexDirective::IndexedBy(self.parse_identifier()?));
} else if self.parse_keywords(&[Keyword::NOT, Keyword::INDEXED]) {
index_directives.push(TableIndexDirective::NotIndexed);
}
}

// MSSQL-specific table hints:
let mut with_hints = vec![];
Expand Down Expand Up @@ -17148,7 +17154,7 @@ impl<'a> Parser<'a> {
with_ordinality,
json_path,
sample,
index_hints,
index_directives,
};

while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
Expand Down Expand Up @@ -17198,7 +17204,7 @@ impl<'a> Parser<'a> {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ pub fn table(name: impl Into<String>) -> TableFactor {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
}
}

Expand All @@ -405,7 +405,7 @@ pub fn table_from_name(name: ObjectName) -> TableFactor {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
}
}

Expand All @@ -424,7 +424,7 @@ pub fn table_with_alias(
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ fn parse_table_time_travel() {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
},
joins: vec![]
},]
Expand Down Expand Up @@ -1882,7 +1882,7 @@ fn parse_merge() {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
},
table
);
Expand All @@ -1897,7 +1897,7 @@ fn parse_merge() {
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
index_directives: vec![],
},
source
);
Expand Down
Loading
Loading