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 4816a25 commit c4a49b2
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 @@ -351,7 +348,6 @@ select `first name`
}

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

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

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

#[test]
#[ignore]
fn test_interval() {
let query = r###"
from projects
Expand All @@ -401,7 +396,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 @@ -420,7 +415,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 @@ -644,7 +639,6 @@ select `first name`
}

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

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

#[test]
#[ignore]
fn test_nulls() {
assert_display_snapshot!((compile(r###"
from employees
Expand All @@ -752,7 +744,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 @@ -764,7 +756,7 @@ select `first name`
filter first_name == null and null == last_name
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -778,7 +770,7 @@ select `first name`
filter first_name != null and null != last_name
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
WHERE
Expand All @@ -795,7 +787,7 @@ select `first name`
take ..10
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -807,7 +799,7 @@ select `first name`
take 5..10
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -819,7 +811,7 @@ select `first name`
take 5..
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees OFFSET 4
"###);
Expand All @@ -829,7 +821,7 @@ select `first name`
take 5..5
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand All @@ -843,7 +835,7 @@ select `first name`
take 1..5
"###).unwrap()), @r###"
SELECT
employees.*
*
FROM
employees
LIMIT
Expand Down Expand Up @@ -983,7 +975,6 @@ select `first name`
}

#[test]
#[ignore]
fn test_dbt_query() {
assert_display_snapshot!((compile(r###"
from {{ ref('stg_orders') }}
Expand Down Expand Up @@ -1049,7 +1040,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 @@ -1114,7 +1104,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 @@ -1134,7 +1123,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 @@ -1421,7 +1409,6 @@ take 20
}

#[test]
#[ignore]
fn test_dialects() {
// Generic
let query = r###"
Expand Down Expand Up @@ -1487,15 +1474,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 @@ -1505,7 +1491,7 @@ take 20

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

assert_display_snapshot!((compile(query).unwrap()), @r###"
SELECT
`anim"ls`.*,
*,
`BeeName` AS `čebela`,
`bear's_name` AS medved
FROM
Expand All @@ -1541,7 +1527,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 c4a49b2

Please sign in to comment.