Skip to content

Commit

Permalink
internal: Remove once_cell dependency (#4627)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jun 17, 2024
1 parent eeaffdb commit a0f12b4
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 167 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion prqlc/prqlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ csv = "1.3.0"
enum-as-inner = {workspace = true}
itertools = {workspace = true}
log = {workspace = true}
once_cell = "1.19.0"
regex = "1.10.5"
semver = {workspace = true}
# serde is required for the `from_text` feature of PRQL, so we can't really put
Expand Down
2 changes: 1 addition & 1 deletion prqlc/prqlc/src/cli/docs_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn generate_markdown_docs(stmts: Vec<Stmt>) -> String {
Generated with [prqlc](https://prql-lang.org/) {}.
"#,
*prqlc::COMPILER_VERSION
*prqlc::compiler_version()
);

let mut docs = String::new();
Expand Down
30 changes: 18 additions & 12 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::sync::OnceLock;

use once_cell::sync::Lazy;
use prqlc_ast::expr::*;
use regex::Regex;

Expand Down Expand Up @@ -357,20 +357,26 @@ impl WriteSource for Ident {
}
}

pub static KEYWORDS: Lazy<HashSet<&str>> = Lazy::new(|| {
HashSet::from_iter([
"let", "into", "case", "prql", "type", "module", "internal", "func",
])
});
fn keywords() -> &'static HashSet<&'static str> {
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
KEYWORDS.get_or_init(|| {
HashSet::from_iter([
"let", "into", "case", "prql", "type", "module", "internal", "func",
])
})
}

pub static VALID_PRQL_IDENT: Lazy<Regex> = Lazy::new(|| {
// Pomsky expression (regex is to Pomsky what SQL is to PRQL):
// ^ ('*' | [ascii_alpha '_$'] [ascii_alpha ascii_digit '_$']* ) $
Regex::new(r"^(?:\*|[a-zA-Z_$][a-zA-Z0-9_$]*)$").unwrap()
});
fn valid_prql_ident() -> &'static Regex {
static VALID_PRQL_IDENT: OnceLock<Regex> = OnceLock::new();
VALID_PRQL_IDENT.get_or_init(|| {
// Pomsky expression (regex is to Pomsky what SQL is to PRQL):
// ^ ('*' | [ascii_alpha '_$'] [ascii_alpha ascii_digit '_$']* ) $
Regex::new(r"^(?:\*|[a-zA-Z_$][a-zA-Z0-9_$]*)$").unwrap()
})
}

pub fn write_ident_part(s: &str) -> String {
if VALID_PRQL_IDENT.is_match(s) && !KEYWORDS.contains(s) {
if valid_prql_ident().is_match(s) && !keywords().contains(s) {
s.to_string()
} else {
format!("`{}`", s)
Expand Down
10 changes: 7 additions & 3 deletions prqlc/prqlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@
// yak-shaving exercise in the future.
#![allow(clippy::result_large_err)]

use std::sync::OnceLock;
use std::{collections::HashMap, path::PathBuf, str::FromStr};

use anstream::adapter::strip_str;
pub use error_message::{ErrorMessage, ErrorMessages, SourceLocation};
pub use ir::Span;
use once_cell::sync::Lazy;
pub use prqlc_ast as ast;
use prqlc_parser::err::error::ErrorSource;
pub use prqlc_parser::err::error::{Error, Errors, MessageKind, Reason, WithErrorInfo};
Expand All @@ -123,8 +123,12 @@ mod utils;

pub type Result<T, E = Error> = core::result::Result<T, E>;

pub static COMPILER_VERSION: Lazy<Version> =
Lazy::new(|| Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid prqlc version number"));
pub fn compiler_version() -> &'static Version {
static COMPILER_VERSION: OnceLock<Version> = OnceLock::new();
COMPILER_VERSION.get_or_init(|| {
Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid prqlc version number")
})
}

/// Compile a PRQL string into a SQL string.
///
Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use enum_as_inner::EnumAsInner;
use itertools::Itertools;

use crate::ast::TyTupleField;
use crate::compiler_version;
use crate::ir::decl::{self, DeclKind, Module, RootModule, TableExpr};
use crate::ir::generic::{ColumnSort, WindowFrame};
use crate::ir::pl::{self, Ident, Lineage, LineageColumn, PlFold, QueryDef};
Expand All @@ -14,7 +15,6 @@ use crate::ir::rq::{
};
use crate::semantic::write_pl;
use crate::utils::{toposort, IdGenerator};
use crate::COMPILER_VERSION;
use crate::{
ast::generic::{InterpolateItem, Range, SwitchCase},
ir::pl::TableExternRef::LocalTable,
Expand Down Expand Up @@ -126,7 +126,7 @@ fn tuple_fields_to_relation_columns(columns: Vec<TyTupleField>) -> Vec<RelationC

fn validate_query_def(query_def: &QueryDef) -> Result<()> {
if let Some(requirement) = &query_def.version {
if !requirement.matches(&COMPILER_VERSION) {
if !requirement.matches(compiler_version()) {
return Err(Error::new_simple("This query uses a version of PRQL that is not supported by prqlc. Please upgrade the compiler."));
}
}
Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc/src/semantic/resolver/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::ir::pl::*;
use crate::semantic::ast_expand::{restrict_null_literal, try_restrict_range};
use crate::semantic::resolver::functions::expr_of_func;
use crate::semantic::{write_pl, NS_PARAM, NS_THIS};
use crate::{Error, Reason, Result, WithErrorInfo, COMPILER_VERSION};
use crate::{compiler_version, Error, Reason, Result, WithErrorInfo};

impl Resolver<'_> {
/// try to convert function call with enough args into transform
Expand Down Expand Up @@ -421,7 +421,7 @@ impl Resolver<'_> {

"prql_version" => {
// yes, this is not a transform, but this is the most appropriate place for it
let ver = COMPILER_VERSION.to_string();
let ver = compiler_version().to_string();
return Ok(Expr::new(ExprKind::Literal(Literal::String(ver))));
}

Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc/src/sql/gen_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::ir::generic::{ColumnSort, SortDirection, WindowFrame, WindowKind};
use crate::ir::pl::{self, Ident, Literal};
use crate::ir::rq::*;
use crate::sql::srq::context::ColumnDecl;
use crate::utils::{OrMap, VALID_IDENT};
use crate::utils::{valid_ident, OrMap};
use crate::{Error, Reason, Result, Span, WithErrorInfo};

pub(super) fn translate_expr(expr: Expr, ctx: &mut Context) -> Result<ExprOrSource> {
Expand Down Expand Up @@ -804,7 +804,7 @@ pub(super) fn translate_ident(
}

pub(super) fn translate_ident_part(ident: String, ctx: &Context) -> sql_ast::Ident {
let is_bare = VALID_IDENT.is_match(&ident);
let is_bare = valid_ident().is_match(&ident);

if is_bare && !keywords::is_keyword(&ident) {
sql_ast::Ident::new(ident)
Expand Down
47 changes: 25 additions & 22 deletions prqlc/prqlc/src/sql/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;

use once_cell::sync::Lazy;
use sqlparser::keywords::{
Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX, RESERVED_FOR_COLUMN_ALIAS, RESERVED_FOR_TABLE_ALIAS,
};
Expand All @@ -11,32 +11,35 @@ use sqlparser::keywords::{
pub(super) fn is_keyword(ident: &str) -> bool {
let ident = ident.to_ascii_uppercase();

SQL_KEYWORDS.contains(ident.as_str())
sql_keywords().contains(ident.as_str())
}

static SQL_KEYWORDS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
let mut m = HashSet::new();
m.extend(SQLITE_KEYWORDS);
fn sql_keywords() -> &'static HashSet<&'static str> {
static SQL_KEYWORDS: OnceLock<HashSet<&str>> = OnceLock::new();
SQL_KEYWORDS.get_or_init(|| {
let mut m = HashSet::new();
m.extend(SQLITE_KEYWORDS);

let reverse_index: HashMap<&Keyword, usize> = ALL_KEYWORDS_INDEX
.iter()
.enumerate()
.map(|(idx, kw)| (kw, idx))
.collect();

m.extend(
RESERVED_FOR_COLUMN_ALIAS
let reverse_index: HashMap<&Keyword, usize> = ALL_KEYWORDS_INDEX
.iter()
.map(|x| ALL_KEYWORDS[reverse_index[x]]),
);
.enumerate()
.map(|(idx, kw)| (kw, idx))
.collect();

m.extend(
RESERVED_FOR_TABLE_ALIAS
.iter()
.map(|x| ALL_KEYWORDS[reverse_index[x]]),
);
m
});
m.extend(
RESERVED_FOR_COLUMN_ALIAS
.iter()
.map(|x| ALL_KEYWORDS[reverse_index[x]]),
);

m.extend(
RESERVED_FOR_TABLE_ALIAS
.iter()
.map(|x| ALL_KEYWORDS[reverse_index[x]]),
);
m
})
}

const SQLITE_KEYWORDS: &[&str] = &[
"ABORT",
Expand Down
5 changes: 3 additions & 2 deletions prqlc/prqlc/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use self::dialect::DialectHandler;
use self::srq::ast::Cte;
use self::srq::context::AnchorContext;
use crate::Result;
use crate::{ir::rq::RelationalQuery, Options, COMPILER_VERSION};
use crate::{compiler_version, ir::rq::RelationalQuery, Options};

/// Translate a PRQL AST into a SQL string.
pub fn compile(query: RelationalQuery, options: &Options) -> Result<String> {
Expand Down Expand Up @@ -45,7 +45,8 @@ pub fn compile(query: RelationalQuery, options: &Options) -> Result<String> {
.unwrap_or_default();
let signature = format!(
"{pre}-- Generated by PRQL compiler version:{} {}(https://prql-lang.org){post}",
*COMPILER_VERSION, target,
*compiler_version(),
target,
);
sql + &signature
} else {
Expand Down
38 changes: 19 additions & 19 deletions prqlc/prqlc/src/sql/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::iter::zip;
use std::path::PathBuf;

use itertools::Itertools;
use once_cell::sync::Lazy;

use super::gen_expr::{translate_operand, ExprOrSource, SourceExpr};
use super::{Context, Dialect};
Expand All @@ -13,22 +12,23 @@ use crate::utils::Pluck;
use crate::Result;
use crate::{Error, WithErrorInfo};

static STD: Lazy<decl::Module> = Lazy::new(load_std_sql);

fn load_std_sql() -> decl::Module {
let std_lib = crate::SourceTree::new(
[(
PathBuf::from("std.prql"),
include_str!("./std.sql.prql").to_string(),
)],
None,
);
let ast = crate::parser::parse(&std_lib).unwrap();

let options = semantic::ResolverOptions {};

let context = semantic::resolve(ast, options).unwrap();
context.module
use std::sync::OnceLock;

fn std() -> &'static decl::Module {
static STD: OnceLock<decl::Module> = OnceLock::new();
STD.get_or_init(|| {
let std_lib = crate::SourceTree::new(
[(
PathBuf::from("std.prql"),
include_str!("./std.sql.prql").to_string(),
)],
None,
);
let ast = crate::parser::parse(&std_lib).unwrap();
let options = semantic::ResolverOptions {};
let context = semantic::resolve(ast, options).unwrap();
context.module
})
}

pub(super) fn translate_operator_expr(expr: rq::Expr, ctx: &mut Context) -> Result<ExprOrSource> {
Expand Down Expand Up @@ -131,7 +131,7 @@ fn find_operator_impl(
.collect::<Vec<_>>(),
);

let dialect_module = STD.get(&pl::Ident::from_name(dialect.to_string()));
let dialect_module = std().get(&pl::Ident::from_name(dialect.to_string()));

let mut func_def = None;

Expand All @@ -141,7 +141,7 @@ fn find_operator_impl(
}

if func_def.is_none() {
func_def = STD.get(&operator_ident);
func_def = std().get(&operator_ident);
}

let decl = func_def?;
Expand Down
26 changes: 15 additions & 11 deletions prqlc/prqlc/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod id_gen;
mod toposort;

use std::sync::OnceLock;

pub use id_gen::{IdGenerator, NameGenerator};
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
pub use toposort::toposort;

Expand Down Expand Up @@ -77,17 +78,20 @@ impl<T> BreakUp<T> for Vec<T> {
}
}

pub static VALID_IDENT: Lazy<Regex> = Lazy::new(|| {
// One of:
// - `*`
// - An ident starting with `a-z_\$` and containing other characters `a-z0-9_\$`
//
// We could replace this with pomsky (regex<>pomsky : sql<>prql)
// ^ ('*' | [ascii_lower '_$'] [ascii_lower ascii_digit '_$']* ) $
Regex::new(r"^((\*)|(^[a-z_\$][a-z0-9_\$]*))$").unwrap()
});
pub(crate) fn valid_ident() -> &'static Regex {
static VALID_IDENT: OnceLock<Regex> = OnceLock::new();
VALID_IDENT.get_or_init(|| {
// One of:
// - `*`
// - An ident starting with `a-z_\$` and containing other characters `a-z0-9_\$`
//
// We could replace this with pomsky (regex<>pomsky : sql<>prql)
// ^ ('*' | [ascii_lower '_$'] [ascii_lower ascii_digit '_$']* ) $
Regex::new(r"^((\*)|(^[a-z_\$][a-z0-9_\$]*))$").unwrap()
})
}

#[test]
fn test_write_ident_part() {
assert!(!VALID_IDENT.is_match(""));
assert!(!valid_ident().is_match(""));
}
Loading

0 comments on commit a0f12b4

Please sign in to comment.