Skip to content
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

Getting SELECT projection aliases #83

Closed
panarch opened this issue Sep 23, 2020 · 11 comments · Fixed by #103
Closed

Getting SELECT projection aliases #83

panarch opened this issue Sep 23, 2020 · 11 comments · Fixed by #103
Assignees
Labels
enhancement New feature or request

Comments

@panarch
Copy link
Member

panarch commented Sep 23, 2020

Related to #80 - due to long threads, it's hard to recognize what exact task is, so let's use this issue.

input:

SELECT a, b AS foo, SUM(id) AS bar FROM TableA;

output:

vec!["a", "foo", "bar"]

This issue is adding a new function get_projection_aliases to src/parse.rs which takes sqlparser::ast::Query reference and return column aliases, Vec<String>

fn get_projection_aliases(query: &sqlparser::ast::Query) -> Vec<String>
@panarch panarch added enhancement New feature or request good first issue Good for newcomers labels Sep 23, 2020
@willy610
Copy link

Great!
But if the column has no as
and is not a SelectItem::UnnamedExpression::Ident
one must probably generate a name like 'column_2'
Or generate a name whatever the expression is like 'SUM(id)'

I think MySQL just make a string of the expression

@panarch
Copy link
Member Author

panarch commented Sep 23, 2020

Good point, in the case of SelectItem::UnnamedExpr(Expr), Expr implemented fmt::Display trait so we can simply use that.

@willy610
Copy link

The suggested function get_projection_aliases can't be located in src/parse.rs

The type Query will be redefined.

I also discovered that all information is not present in the Ast

I'm at the moment working on adding projections names to the ```Payload::Select(projnames...,rows...)``

The content is produced in executor/select.rs

Works nice sofar

SELECT categoryid, 
       Count(*) 
FROM   categoryandrecipe 
GROUP  BY categoryid 
ORDER BY 2;

categoryid      Count(*)
5       3
6       5
7       20

@panarch
Copy link
Member Author

panarch commented Sep 29, 2020

@willy610 I don't get the point you mentioned, "all information is not present in the Ast".
You have any example sql for that?
SELECT categoryid, Count(*) FROM ... is obviously fetched only with AST, execution is not required.

@willy610
Copy link

Everything with wildards need to be resolved at execution I think

SelectItem::QualifiedWildcard(alias) 
 SelectItem::Wildcard

Like

SELECT * FROM ingredient 
WHERE energy > 400 
ORDER BY 1;

ingredientid    energy  price   note    ingredientname
15      523     5.0     'Alla tycker väl om bacon.'     'bacon'
46      544     10.0    '(Ingen notering)'      'mörk blockchoklad'
128     549     8.0     ''      'kycklingfilé'
181     900     8.0     ''      'olivolja'
253     717     2.0     ''      'smör'
256     406     1.2     ''      'socker'

and

SELECT * FROM ingredient AS XXXX 
WHERE XXXX.energy > 400 
ORDER BY 1;

XXXX.ingredientid       XXXX.energy     XXXX.price      XXXX.note       XXXX.ingredientname
15      523     5.0     'Alla tycker väl om bacon.'     'bacon'
46      544     10.0    '(Ingen notering)'      'mörk blockchoklad'
128     549     8.0     ''      'kycklingfilé'
181     900     8.0     ''      'olivolja'
253     717     2.0     ''      'smör'
256     406     1.2     ''      'socker'

@willy610
Copy link

Here we go with some code from executor/select.rs

            let aggregate = Aggregate::new(storage, projection, group_by, filter_context);
            let mut projcolumns: Vec<String> = Vec::new();
            for selitem in projection {
                match selitem {
                    SelectItem::UnnamedExpr(expr) => {
                        projcolumns.push(expr.to_string());
                    }
                    SelectItem::ExprWithAlias {
                        expr: _,
                        alias: falias,
                    } => {
                        projcolumns.push(falias.to_string());
                    }
                    SelectItem::QualifiedWildcard(alias) => {
                        let columns = fetch_columns(storage, get_name(alias)?)?;
                        for x in columns.iter() {
                            projcolumns.push(format!("{}.{}", alias.to_string(), x.to_string()));
                        }
                    }
                    SelectItem::Wildcard => {
                        let alias = table.get_alias_asprefix();
                        for x in columns.iter() {
                            projcolumns.push(format!("{}{}",alias,  x.to_string()));
                        }
                    }
                };
            }
            let blend = Blend::new(storage, projection);

@panarch panarch removed the good first issue Good for newcomers label Sep 29, 2020
@panarch
Copy link
Member Author

panarch commented Sep 29, 2020

Oh, I totally ignored Wildcard!
Thanks for providing sql example.
Your point is right, select function needs to return Vec<Row> with projection aliases.

I'll remove "good first issue" label, it looks certainly more than simple task.

If you are interested on taking this issue, you need to be aware of a few things.

  • No mut uses are allowed in execution layer.
  • Avoiding unnecessary string clone.
  • Reducing storage api call, like.. fetch_columns,

@willy610
Copy link

I can take this issue.
With awareness !!
It's only the outermost/final selection-projection which actually is required!
At the moment select-projection produces column-names

@willy610
Copy link

willy610 commented Oct 2, 2020

Made an horrible - I think - recursive solution in src/executor/select.rswithout mut

...assuming mut is not allowed in src/executor/

But what do you mean with

  • No mut uses are allowed in execution layer.

Is it a folder (like executor) ?

Is it ok to use src/utils/vector.rs as a mutabel collector?

In for example in a revisted src/executor/join.rs on RIGHT JOIN quiz #63

I really like the approach on mut

@panarch
Copy link
Member Author

panarch commented Oct 3, 2020

That's a good point. At first, you can wrap std Vec using src/utils/vector.rs.
Now allowing mut policy is making GlueSQL project code promising, so it does not care whatever other dependency is implemented.
Only important thing is that dependency is promising enough, so I think we can simply depend on std Vec.
But as you see, std vector does not provide immutable apis, so I simply wrapped it.
You can freely add wrapping methods when you need.

And you're right, you can assume no mut on src/executor/ and src/data/.

@willy610
Copy link

willy610 commented Oct 3, 2020

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants