-
Notifications
You must be signed in to change notification settings - Fork 4
Add sqlite reader and adjust SQL queries to work there #182
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
Open
georgestagg
wants to merge
10
commits into
main
Choose a base branch
from
sqlite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
938fffb
Add sqlite reader
georgestagg 24a1b94
Portable GREATEST/LEAST and various sqlite tweaks
georgestagg 349f32a
Drop remaining __ggsql_stat_ columns not consumed by remappings
georgestagg 4c68435
Use chrono ToSql/FromSql in sqlite reader
georgestagg fd8affb
Add sqlite flags
georgestagg de092b9
Use portable SQL
georgestagg 1f2795c
Minor tweaks to naming and features
georgestagg b3d6b49
Add SQL dialect function overriding for duckdb
georgestagg adf591c
cargo fmt
georgestagg ec946f7
Fixup grammar: subquery as function argument
georgestagg 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
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 |
|---|---|---|
|
|
@@ -4,9 +4,9 @@ | |
| //! transformations, stat transforms, and post-query operations. | ||
|
|
||
| use crate::plot::{ | ||
| AestheticValue, DefaultAestheticValue, Layer, ParameterValue, Scale, Schema, SqlTypeNames, | ||
| StatResult, | ||
| AestheticValue, DefaultAestheticValue, Layer, ParameterValue, Scale, Schema, StatResult, | ||
| }; | ||
| use crate::reader::SqlDialect; | ||
| use crate::{naming, DataFrame, GgsqlError, Result}; | ||
| use polars::prelude::DataType; | ||
| use std::collections::{HashMap, HashSet}; | ||
|
|
@@ -150,6 +150,17 @@ pub fn apply_remappings_post_query(df: DataFrame, layer: &Layer) -> Result<DataF | |
| } | ||
| } | ||
|
|
||
| // Drop any remaining __ggsql_stat_* columns that weren't consumed by remappings. | ||
| let stat_cols: Vec<String> = df | ||
| .get_column_names() | ||
| .into_iter() | ||
| .filter(|name| naming::is_stat_column(name)) | ||
| .map(|name| name.to_string()) | ||
| .collect(); | ||
| if !stat_cols.is_empty() { | ||
| df = df.drop_many(stat_cols); | ||
| } | ||
|
|
||
| Ok(df) | ||
| } | ||
|
|
||
|
|
@@ -183,14 +194,14 @@ pub fn literal_to_series(name: &str, lit: &ParameterValue, len: usize) -> polars | |
| /// * `layer` - The layer configuration | ||
| /// * `schema` - The layer's schema (used for column dtype lookup) | ||
| /// * `scales` - All resolved scales | ||
| /// * `type_names` - SQL type names for the database backend | ||
| /// * `dialect` - SQL dialect for the database backend | ||
| pub fn apply_pre_stat_transform( | ||
| query: &str, | ||
| layer: &Layer, | ||
| full_schema: &Schema, | ||
| aesthetic_schema: &Schema, | ||
| scales: &[Scale], | ||
| type_names: &SqlTypeNames, | ||
| dialect: &dyn SqlDialect, | ||
| ) -> String { | ||
| let mut transform_exprs: Vec<(String, String)> = vec![]; | ||
| let mut transformed_columns: HashSet<String> = HashSet::new(); | ||
|
|
@@ -226,7 +237,7 @@ pub fn apply_pre_stat_transform( | |
| // Get pre-stat SQL transformation from scale type (if applicable) | ||
| // Each scale type's pre_stat_transform_sql() returns None if not applicable | ||
| if let Some(sql) = | ||
| scale_type.pre_stat_transform_sql(&aes_col_name, &col_dtype, scale, type_names) | ||
| scale_type.pre_stat_transform_sql(&aes_col_name, &col_dtype, scale, dialect) | ||
| { | ||
| transformed_columns.insert(aes_col_name.clone()); | ||
| transform_exprs.push((aes_col_name, sql)); | ||
|
|
@@ -336,7 +347,7 @@ pub fn build_layer_base_query( | |
| /// * `base_query` - The base query from build_layer_base_query | ||
| /// * `schema` - The layer's schema (with min/max from base_query) | ||
| /// * `scales` - All resolved scales | ||
| /// * `type_names` - SQL type names for the database backend | ||
| /// * `dialect` - SQL dialect for the database backend | ||
| /// * `execute_query` - Function to execute queries (needed for some stat transforms) | ||
| /// | ||
| /// # Returns | ||
|
|
@@ -347,7 +358,7 @@ pub fn apply_layer_transforms<F>( | |
| base_query: &str, | ||
| schema: &Schema, | ||
| scales: &[Scale], | ||
| type_names: &SqlTypeNames, | ||
| dialect: &dyn SqlDialect, | ||
| execute_query: &F, | ||
| ) -> Result<String> | ||
| where | ||
|
|
@@ -387,7 +398,7 @@ where | |
| schema, | ||
| &aesthetic_schema, | ||
| scales, | ||
| type_names, | ||
| dialect, | ||
| ); | ||
|
|
||
| // Build group_by columns from partition_by | ||
|
|
@@ -416,6 +427,7 @@ where | |
| &group_by, | ||
| &layer.parameters, | ||
| execute_query, | ||
| dialect, | ||
| )?; | ||
|
|
||
| // Apply literal default remappings from geom defaults (e.g., y2 => 0.0 for bar baseline). | ||
|
|
@@ -518,12 +530,6 @@ where | |
| if stat_rename_exprs.is_empty() { | ||
| transformed_query | ||
| } else { | ||
| let stat_col_names: Vec<String> = stat_columns | ||
| .iter() | ||
| .map(|s| naming::stat_column(s)) | ||
| .collect(); | ||
| let exclude_clause = format!("EXCLUDE ({})", stat_col_names.join(", ")); | ||
|
|
||
| // If the transformed query uses CTEs (WITH ... SELECT ...), | ||
| // we can't wrap it in a subquery because Polars SQL doesn't | ||
| // support CTEs inside subqueries. Instead, split into CTE | ||
|
|
@@ -536,16 +542,14 @@ where | |
| .and_then(super::cte::split_with_query) | ||
| { | ||
| format!( | ||
| "{}, __ggsql_stat__ AS ({}) SELECT * {}, {} FROM __ggsql_stat__", | ||
| "{}, __ggsql_stat__ AS ({}) SELECT *, {} FROM __ggsql_stat__", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are they being excluded somewhere else or do we just keep them around until we filter the final DataFrame? |
||
| cte_prefix, | ||
| trailing_select, | ||
| exclude_clause, | ||
| stat_rename_exprs.join(", ") | ||
| ) | ||
| } else { | ||
| format!( | ||
| "SELECT * {}, {} FROM ({}) AS __ggsql_stat__", | ||
| exclude_clause, | ||
| "SELECT *, {} FROM ({}) AS __ggsql_stat__", | ||
| stat_rename_exprs.join(", "), | ||
| transformed_query | ||
| ) | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tweak to features is just prep for Wasm, not related to sqlite.