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

chore: [DO NOT MERGE] patch for prqlc 0.13.0 MSRV 1.69.0 #4916

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,028 changes: 587 additions & 441 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ license = "Apache-2.0"
repository = "https://github.com/PRQL/prql"
# This isn't tested since `cargo-msrv` doesn't support workspaces; instead we
# test `metadata.msrv` in `prqlc`
rust-version = "1.70.0"
rust-version = "1.69.0"
version = "0.13.0"

[profile.release]
Expand Down Expand Up @@ -55,8 +55,8 @@ pyo3 = {version = "0.20.1", features = ["abi3-py37", "anyhow"]}
pyo3-build-config = "0.22.2"
schemars = "1.0.0-alpha.2"
semver = {version = "1.0.23", features = ["serde"]}
serde = {version = "1.0.204", features = ["derive"]}
serde_json = "1.0.120"
serde_yaml = {version = "0.9.34"}
serde = {version = "1.0.210", features = ["derive"]}
serde_json = "1"
serde_yaml = {version = "0.9"}
similar = "2.6.0"
similar-asserts = "1.5.0"
2 changes: 1 addition & 1 deletion lutra/bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish = false
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
rust-version = "1.70"
version.workspace = true

[lib]
Expand Down
2 changes: 1 addition & 1 deletion lutra/lutra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "lutra"
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
rust-version = "1.70"
version.workspace = true

[features]
Expand Down
6 changes: 2 additions & 4 deletions prqlc/prqlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ repository.workspace = true
rust-version.workspace = true
version.workspace = true

# Required for `cargo-msrv`, which doesn't yet support workspaces
metadata.msrv = "1.70.0"

[features]
cli = [
"anyhow",
Expand All @@ -27,7 +24,7 @@ cli = [
"serde_yaml",
"walkdir",
]
default = ["cli"]
default = []
serde_yaml = [
"prqlc-parser/serde_yaml",
"dep:serde_yaml",
Expand All @@ -47,6 +44,7 @@ csv = "1.3.0"
enum-as-inner = {workspace = true}
itertools = {workspace = true}
log = {workspace = true}
once_cell = "1"
regex = "1.10.5"
schemars = {workspace = true}
semver = {workspace = true}
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: 12 additions & 18 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashSet;
use std::sync::OnceLock;
use once_cell::sync::Lazy;

use regex::Regex;

Expand Down Expand Up @@ -360,26 +360,20 @@ impl WriteSource for pr::Ident {
}
}

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 KEYWORDS: Lazy<HashSet<&str>> = Lazy::new(|| {
HashSet::from_iter([
"let", "into", "case", "prql", "type", "module", "internal", "func",
])
});

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 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()
});

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
2 changes: 1 addition & 1 deletion prqlc/prqlc/src/debug/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn log_start() {

*lock = Some(DebugLog {
started_at,
version: crate::compiler_version().to_string(),
version: crate::COMPILER_VERSION.to_string(),
entries: Vec::new(),

suppress_count: 0,
Expand Down
10 changes: 3 additions & 7 deletions prqlc/prqlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@
// 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;
use once_cell::sync::Lazy;
use semver::Version;
use serde::{Deserialize, Serialize};
use strum::VariantNames;
Expand All @@ -127,12 +127,8 @@ pub(crate) mod utils;

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

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")
})
}
pub static COMPILER_VERSION: Lazy<Version> =
Lazy::new(|| 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 @@ -7,7 +7,6 @@ use itertools::Itertools;
use prqlc_parser::generic::{InterpolateItem, Range, SwitchCase};
use prqlc_parser::lexer::lr::Literal;

use crate::compiler_version;
use crate::ir::decl::{self, DeclKind, Module, RootModule, TableExpr};
use crate::ir::generic::{ColumnSort, WindowFrame};
use crate::ir::pl::TableExternRef::LocalTable;
Expand All @@ -18,6 +17,7 @@ use crate::ir::rq::{
use crate::pr::TyTupleField;
use crate::semantic::write_pl;
use crate::utils::{toposort, IdGenerator};
use crate::COMPILER_VERSION;
use crate::{Error, Reason, Result, Span, WithErrorInfo};

/// Convert a resolved expression at path `main_path` relative to `root_mod`
Expand Down Expand Up @@ -125,7 +125,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::pr::{Ty, TyKind, TyTupleField};
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::{compiler_version, Error, Reason, Result, WithErrorInfo};
use crate::{Error, Reason, Result, WithErrorInfo, COMPILER_VERSION};

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::pq::context::ColumnDecl;
use crate::utils::{valid_ident, OrMap};
use crate::utils::{OrMap, VALID_IDENT};
use crate::{Error, Reason, Result, Span, WithErrorInfo};

pub(super) fn translate_expr(expr: rq::Expr, ctx: &mut Context) -> Result<ExprOrSource> {
Expand Down Expand Up @@ -810,7 +810,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: 21 additions & 26 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,35 +11,30 @@ 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())
}

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);
static SQL_KEYWORDS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
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();

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

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
})
}
.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: 2 additions & 3 deletions prqlc/prqlc/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use self::pq::context::AnchorContext;
use crate::debug;
use crate::ir::rq;
use crate::Result;
use crate::{compiler_version, Options};
use crate::{Options, COMPILER_VERSION};

/// Translate a PRQL AST into a SQL string.
pub fn compile(query: rq::RelationalQuery, options: &Options) -> Result<String> {
Expand Down Expand Up @@ -50,8 +50,7 @@ pub fn compile(query: rq::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
39 changes: 18 additions & 21 deletions prqlc/prqlc/src/sql/operators.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use std::collections::HashMap;
use std::iter::zip;
use std::path::PathBuf;
use std::sync::OnceLock;

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

use super::gen_expr::{translate_operand, ExprOrSource, SourceExpr};
use super::{Context, Dialect};
use crate::ir::{decl, pl, rq};
use crate::semantic;
use crate::utils::Pluck;
use crate::Result;
use crate::{debug, semantic};
use crate::{Error, WithErrorInfo};

fn std() -> &'static decl::Module {
static STD: OnceLock<decl::Module> = OnceLock::new();
STD.get_or_init(|| {
let _suppressed = debug::log_suppress();

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 context = semantic::resolve(ast).unwrap();

context.module
})
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 context = semantic::resolve(ast).unwrap();

context.module
}

pub(super) fn translate_operator_expr(expr: rq::Expr, ctx: &mut Context) -> Result<ExprOrSource> {
Expand Down Expand Up @@ -132,7 +129,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 @@ -142,7 +139,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
Loading