Skip to content

Commit

Permalink
feat: Omit table name when only one ident in SELECT (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma authored and aljazerzen committed Nov 27, 2022
1 parent 48a9ea9 commit cb385db
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 45 deletions.
2 changes: 2 additions & 0 deletions prql-compiler/src/ir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod expr;
mod id_gen;
mod ir_fold;
mod table_counter;

pub use expr::{Expr, ExprKind, UnOp};
pub use id_gen::IdGenerator;
pub use ir_fold::*;
pub use table_counter::TableCounter;

use serde::{Deserialize, Serialize};

Expand Down
27 changes: 27 additions & 0 deletions prql-compiler/src/ir/table_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::HashSet;

use super::{IrFold, TId, Transform};

/// Folder that counts the number of table referenced in a PRQL query.
#[derive(Debug, Default)]
pub struct TableCounter {
tables: HashSet<TId>,
}

impl TableCounter {
pub fn count(&self) -> usize {
self.tables.len()
}
}

impl IrFold for TableCounter {
fn fold_transforms(&mut self, transforms: Vec<Transform>) -> anyhow::Result<Vec<Transform>> {
for transform in &transforms {
if let Transform::Join { with: tid, .. } | Transform::From(tid) = transform {
self.tables.insert(*tid);
}
}

Ok(transforms)
}
}
56 changes: 21 additions & 35 deletions prql-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ mod test {
use insta::{assert_display_snapshot, assert_snapshot};

#[test]
#[ignore]
fn test_stdlib() {
assert_snapshot!(compile(r###"
from employees
Expand Down Expand Up @@ -99,7 +98,6 @@ mod test {
}

#[test]
#[ignore]
fn test_precedence() {
assert_display_snapshot!((compile(r###"
from x
Expand Down Expand Up @@ -257,7 +255,6 @@ mod test {
}

#[test]
#[ignore]
fn test_quoting() {
// GH-#822
assert_display_snapshot!((compile(r###"
Expand All @@ -270,7 +267,7 @@ join some_schema.tablename [~id]
"###).unwrap()), @r###"
WITH "UPPER" AS (
SELECT
lower.*
*
FROM
lower
)
Expand Down Expand Up @@ -319,7 +316,7 @@ select `first name`

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
invoices.*
*
FROM
invoices
ORDER BY
Expand Down Expand Up @@ -350,7 +347,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_ranges() {
let query = r###"
from employees
Expand All @@ -359,7 +355,7 @@ select `first name`

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -381,7 +377,7 @@ select `first name`

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
events.*
*
FROM
events
WHERE
Expand All @@ -391,7 +387,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_interval() {
let query = r###"
from projects
Expand All @@ -400,7 +395,7 @@ select `first name`

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
projects.*,
*,
start + INTERVAL 10 DAY AS first_check_in
FROM
projects
Expand All @@ -419,7 +414,7 @@ select `first name`
]
"###).unwrap()), @r###"
SELECT
to_do_empty_table.*,
*,
DATE '2011-02-01' AS date,
TIMESTAMP '2011-02-01T10:00' AS timestamp,
TIME '14:00' AS time
Expand Down Expand Up @@ -643,7 +638,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_name_resolving() {
let query = r###"
from numbers
Expand Down Expand Up @@ -694,7 +688,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_filter() {
// https://github.com/prql/prql/issues/469
let query = r###"
Expand All @@ -709,7 +702,7 @@ select `first name`
filter age > 25 and age < 40
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -723,7 +716,7 @@ select `first name`
filter age < 40
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -733,7 +726,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_nulls() {
assert_display_snapshot!((compile(r###"
from employees
Expand All @@ -751,7 +743,7 @@ select `first name`
derive amount = amount + 2 ?? 3 * 5
"###).unwrap()), @r###"
SELECT
employees.*,
*,
COALESCE(amount + 2, 3 * 5) AS amount
FROM
employees
Expand All @@ -763,7 +755,7 @@ select `first name`
filter first_name == null and null == last_name
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -777,7 +769,7 @@ select `first name`
filter first_name != null and null != last_name
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -794,7 +786,7 @@ select `first name`
take ..10
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -806,7 +798,7 @@ select `first name`
take 5..10
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -818,7 +810,7 @@ select `first name`
take 5..
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees OFFSET 4
"###);
Expand All @@ -828,7 +820,7 @@ select `first name`
take 5..5
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -842,7 +834,7 @@ select `first name`
take 1..5
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand Down Expand Up @@ -982,7 +974,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_dbt_query() {
assert_display_snapshot!((compile(r###"
from {{ ref('stg_orders') }}
Expand Down Expand Up @@ -1048,7 +1039,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_f_string() {
let query = r###"
from employees
Expand Down Expand Up @@ -1113,7 +1103,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_sql_of_ast_2() {
let query = r###"
from employees
Expand All @@ -1133,7 +1122,6 @@ select [mng_name, managers.gender, salary_avg, salary_sd]"#;
}

#[test]
#[ignore]
fn test_prql_to_sql_1() {
let query = r#"
from employees
Expand Down Expand Up @@ -1420,7 +1408,6 @@ take 20
}

#[test]
#[ignore]
fn test_dialects() {
// Generic
let query = r###"
Expand Down Expand Up @@ -1486,15 +1473,14 @@ take 20

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
github_json.*,
github_json.`event.type` AS event_type_dotted
*,
`event.type` AS event_type_dotted
FROM
github_json
"###);
}

#[test]
#[ignore]
fn test_ident_escaping() {
// Generic
let query = r###"
Expand All @@ -1504,7 +1490,7 @@ take 20

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
"anim""ls".*,
*,
"BeeName" AS "čebela",
"bear's_name" AS medved
FROM
Expand All @@ -1521,7 +1507,7 @@ take 20

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
`anim"ls`.*,
*,
`BeeName` AS `čebela`,
`bear's_name` AS medved
FROM
Expand All @@ -1540,7 +1526,7 @@ take 20
assert_display_snapshot!(sql,
@r###"
SELECT
employees.*,
*,
true AS always_true
FROM
employees
Expand Down
1 change: 0 additions & 1 deletion prql-compiler/src/semantic/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ mod test {
}

#[test]
#[ignore]
fn test_func_call_resolve() {
assert_display_snapshot!(compile(r#"
from employees
Expand Down
15 changes: 9 additions & 6 deletions prql-compiler/src/sql/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,17 @@ pub(super) fn translate_ident(
context: &Context,
) -> Vec<sql_ast::Ident> {
let mut parts = Vec::with_capacity(4);
if let Some(relation) = relation_name {
// Special-case this for BigQuery, Ref #852
if matches!(context.dialect.dialect(), Dialect::BigQuery) {
parts.push(relation);
} else {
parts.extend(relation.split('.').map(|s| s.to_string()));
if !context.omit_ident_prefix || column.is_none() {
if let Some(relation) = relation_name {
// Special-case this for BigQuery, Ref #852
if matches!(context.dialect.dialect(), Dialect::BigQuery) {
parts.push(relation);
} else {
parts.extend(relation.split('.').map(|s| s.to_string()));
}
}
}

parts.extend(column);

parts
Expand Down
Loading

0 comments on commit cb385db

Please sign in to comment.