-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add SQL dialect support with Spark SQL dialect #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChunxuTang
merged 6 commits into
lance-format:main
from
yuchen-pipi:feat/spark-sql-dialect
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8306436
feat: add SQL dialect support with Spark SQL dialect
1bc5623
refactor: address PR review comments for SQL dialect support
6eaffe7
refactor: move DialectUnparser to query.rs and box large enum variant
d450021
feat: add lance-graph-cli crate with lgraph binary for Cypher-to-SQL …
d1bd741
Revert "feat: add lance-graph-cli crate with lgraph binary for Cypher…
0d1a864
style: apply rustfmt formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright The Lance Authors | ||
|
|
||
| //! Spark SQL dialect for the DataFusion unparser. | ||
| //! | ||
| //! This module provides a Spark SQL dialect built using DataFusion's | ||
| //! [`CustomDialectBuilder`]. | ||
| //! | ||
| //! Key Spark SQL differences from standard SQL: | ||
| //! - Backtick (`` ` ``) identifier quoting | ||
| //! - `EXTRACT(field FROM expr)` for date field extraction | ||
| //! - `STRING` type for casting (not `VARCHAR`) | ||
| //! - `BIGINT`/`INT` for integer types | ||
| //! - `TIMESTAMP` for all timestamp types (no timezone info in cast) | ||
| //! - `LENGTH()` instead of `CHARACTER_LENGTH()` | ||
| //! - Subqueries in FROM require aliases | ||
|
|
||
| use datafusion_sql::sqlparser::ast::{self, Ident, ObjectName, TimezoneInfo}; | ||
| use datafusion_sql::unparser::dialect::{ | ||
| CharacterLengthStyle, CustomDialect, CustomDialectBuilder, DateFieldExtractStyle, | ||
| }; | ||
|
|
||
| /// Build a Spark SQL dialect using DataFusion's `CustomDialectBuilder`. | ||
| pub fn build_spark_dialect() -> CustomDialect { | ||
| CustomDialectBuilder::new() | ||
| .with_identifier_quote_style('`') | ||
| .with_supports_nulls_first_in_sort(true) | ||
| .with_use_timestamp_for_date64(true) | ||
| .with_utf8_cast_dtype(ast::DataType::Custom( | ||
| ObjectName::from(vec![Ident::new("STRING")]), | ||
| vec![], | ||
| )) | ||
| .with_large_utf8_cast_dtype(ast::DataType::Custom( | ||
| ObjectName::from(vec![Ident::new("STRING")]), | ||
| vec![], | ||
| )) | ||
| .with_date_field_extract_style(DateFieldExtractStyle::Extract) | ||
| .with_character_length_style(CharacterLengthStyle::Length) | ||
| .with_int64_cast_dtype(ast::DataType::BigInt(None)) | ||
| .with_int32_cast_dtype(ast::DataType::Int(None)) | ||
| .with_timestamp_cast_dtype( | ||
| ast::DataType::Timestamp(None, TimezoneInfo::None), | ||
| ast::DataType::Timestamp(None, TimezoneInfo::None), | ||
| ) | ||
| .with_date32_cast_dtype(ast::DataType::Date) | ||
| .with_supports_column_alias_in_table_alias(true) | ||
| .with_requires_derived_table_alias(true) | ||
| .with_full_qualified_col(false) | ||
| .with_unnest_as_table_factor(false) | ||
| .with_float64_ast_dtype(ast::DataType::Double(ast::ExactNumberInfo::None)) | ||
| .build() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use datafusion_sql::unparser::dialect::Dialect; | ||
|
|
||
| #[test] | ||
| fn test_spark_dialect_identifier_quoting() { | ||
| let dialect = build_spark_dialect(); | ||
| assert_eq!(dialect.identifier_quote_style("table_name"), Some('`')); | ||
| assert_eq!(dialect.identifier_quote_style("column"), Some('`')); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_spark_dialect_type_mappings() { | ||
| let dialect = build_spark_dialect(); | ||
| assert!(matches!( | ||
| dialect.utf8_cast_dtype(), | ||
| ast::DataType::Custom(..) | ||
| )); | ||
| assert!(matches!( | ||
| dialect.int64_cast_dtype(), | ||
| ast::DataType::BigInt(None) | ||
| )); | ||
| assert!(matches!( | ||
| dialect.int32_cast_dtype(), | ||
| ast::DataType::Int(None) | ||
| )); | ||
| assert!(matches!(dialect.date32_cast_dtype(), ast::DataType::Date)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_spark_dialect_requires_derived_table_alias() { | ||
| let dialect = build_spark_dialect(); | ||
| assert!(dialect.requires_derived_table_alias()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_spark_dialect_extract_style() { | ||
| let dialect = build_spark_dialect(); | ||
| assert!(matches!( | ||
| dialect.date_field_extract_style(), | ||
| DateFieldExtractStyle::Extract | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_spark_dialect_character_length_style() { | ||
| let dialect = build_spark_dialect(); | ||
| assert!(matches!( | ||
| dialect.character_length_style(), | ||
| CharacterLengthStyle::Length | ||
| )); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.