Skip to content

Functions with literals and params should type resolve #145

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

Merged
merged 2 commits into from
Mar 7, 2025
Merged
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
23 changes: 8 additions & 15 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DOCKER_CLI_HINTS = "false" # Please don't show us What's Next.
[tasks.proxy]
alias = 'p'
description = "Run the proxy (with `cargo run`)"
run = 'pwd && env | grep CS_ && cargo run {{option(name="extra-args",default="")}}'
run = 'pwd && env | grep CS_ && echo cargo run {{option(name="extra-args",default="")}} | bash'

[tasks."proxy:kill"]
alias = 'k'
Expand Down Expand Up @@ -71,20 +71,6 @@ cp {{config_root}}/target/{{ target }}/release/cipherstash-proxy {{config_root}}
# build a new container
mise run build:docker --platform {{docker_platform}}

# if [[ "{{arg(name="service",default="proxy")}}" =~ "proxy-tls" ]]; then
# if [ -z "${CS_TLS__CERTIFICATE}" ]; then
# echo "error: CS_TLS__CERTIFICATE is not set, and you are trying to bring up the proxy-tls container"
# fi
# if [ -z "${CS_TLS__PRIVATE_KEY}" ]; then
# echo "error: CS_TLS__PRIVATE_KEY is not set, and you are trying to bring up the proxy-tls container"
# fi
# if [ -z "${CS_TLS__TYPE}" ]; then
# echo "error: CS_TLS__TYPE is not set (either 'Pem' or 'Path' required), and you are trying to bring up the proxy-tls container"
# fi
# if [ -z "${CS_TLS__CERTIFICATE}" ] || [ -z "${CS_TLS__CERTIFICATE}" ] || [ -z "${CS_TLS__TYPE}" ];; then
# exit 67
# fi
# fi

# Make the default invocation with `mise run proxy:up` work.
# The default mise environment configuration works for Proxy running as a process, connecting via a port forward.
Expand Down Expand Up @@ -150,6 +136,13 @@ run = """
cargo nextest run --no-fail-fast --nocapture -p cipherstash-proxy-integration
"""

[tasks."test:local:mapper"]
alias = 'lm'
description = "Runs test/s"
run = """
cargo nextest run --no-fail-fast --nocapture -p eql-mapper
"""

[tasks."test:unit"]
description = "Runs test/s"
run = "mise run test:nextest {{arg(name='test',default='')}}"
Expand Down
13 changes: 3 additions & 10 deletions packages/eql-mapper/src/importer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
use std::{cell::RefCell, fmt::Debug, marker::PhantomData, ops::ControlFlow, rc::Rc, sync::Arc};

use sqlparser::ast::{Cte, Ident, Insert, TableAlias, TableFactor};
use sqltk::{Break, Visitable, Visitor};
use tracing::info;

use crate::{
inference::{
unifier::{Constructor, Type},
Expand All @@ -13,6 +7,9 @@ use crate::{
unifier::{Projection, ProjectionColumns},
Relation, ScopeError, ScopeTracker,
};
use sqlparser::ast::{Cte, Ident, Insert, TableAlias, TableFactor};
use sqltk::{Break, Visitable, Visitor};
use std::{cell::RefCell, fmt::Debug, marker::PhantomData, ops::ControlFlow, rc::Rc, sync::Arc};

/// `Importer` is a [`Visitor`] implementation that brings projections (from "FROM" clauses and subqueries) into lexical scope.
#[derive(Debug)]
Expand Down Expand Up @@ -60,8 +57,6 @@ impl<'ast> Importer<'ast> {
}

fn update_scope_for_cte(&mut self, cte: &'ast Cte) -> Result<(), ImportError> {
info!("update_scope_for_cte");

let Cte {
alias: TableAlias {
name: alias,
Expand Down Expand Up @@ -323,14 +318,12 @@ impl<'ast> Visitor<'ast> for Importer<'ast> {

fn exit<N: Visitable>(&mut self, node: &'ast N) -> ControlFlow<Break<Self::Error>> {
if let Some(cte) = node.downcast_ref::<Cte>() {
// info!("CTE {}", cte);
if let Err(err) = self.update_scope_for_cte(cte) {
return ControlFlow::Break(Break::Err(err));
}
};

if let Some(table_factor) = node.downcast_ref::<TableFactor>() {
// info!("TableFactor {}", table_factor);
if let Err(err) = self.update_scope_for_table_factor(table_factor) {
return ControlFlow::Break(Break::Err(err));
}
Expand Down
39 changes: 36 additions & 3 deletions packages/eql-mapper/src/inference/infer_type_impls/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,43 @@ impl<'ast> InferType<'ast, Function> for TypeInferencer<'ast> {
}
}
} else {
// All other functions:
// 1. no constraints are imposed on their arguments (they can be any type) (TODO: do we need a "do not care" type)
// 2. the return type is always native.
// All other functions: resolve to native
// EQL values will be rejected in function calls
self.unify_node_with_type(function, Type::any_native())?;

match args {
// Function called without any arguments.
// Used for functions like `CURRENT_TIMESTAMP` that do not require parentheses ()
// This is not the same as a function that has zero arguments (which would be an empty arg list)
FunctionArguments::None => {}

FunctionArguments::Subquery(query) => {
// The query must return a single column projection which has the same type as the result of the function
self.unify_node_with_type(
&**query,
Type::projection(&[(self.get_type(function), None)]),
)?;
}

FunctionArguments::List(args_list) => {
self.unify_node_with_type(function, Type::any_native())?;

for arg in &args_list.args {
match arg {
FunctionArg::Named { arg, .. } | FunctionArg::Unnamed(arg) => match arg
{
FunctionArgExpr::Expr(expr) => {
self.unify_node_with_type(expr, Type::any_native())?;
}
// Aggregate functions like COUNT(table.*)
FunctionArgExpr::QualifiedWildcard(_) => {}
// Aggregate functions like COUNT(*)
FunctionArgExpr::Wildcard => {}
},
}
}
}
}
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion packages/eql-mapper/src/inference/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub(crate) mod test_util {
};
use sqltk::{Break, Visitable, Visitor};
use std::{convert::Infallible, fmt::Debug, ops::ControlFlow};
use tracing::error;

use super::{NodeKey, TypeRegistry};

Expand All @@ -97,7 +98,7 @@ pub(crate) mod test_util {
pub(crate) fn dump_node<N: Display + Visitable + Debug>(&self, node: &N) {
let key = NodeKey::new_from_visitable(node);
if let Some(ty) = self.node_types.get(&key) {
eprintln!(
error!(
"TYPE<\nast: {}\nsyn: {}\nty: {}\n>",
std::any::type_name::<N>(),
node,
Expand Down
159 changes: 155 additions & 4 deletions packages/eql-mapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod test {
use sqlparser::ast::{self as ast};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::error;

fn resolver(schema: Schema) -> Arc<TableResolver> {
Arc::new(TableResolver::new_fixed(schema.into()))
Expand Down Expand Up @@ -119,7 +120,6 @@ mod test {

match type_check(schema, &statement) {
Ok(typed) => {
eprintln!("{:#?}", &typed.literals);
assert!(typed.literals.contains(&(
EqlValue(TableColumn {
table: id("users"),
Expand Down Expand Up @@ -151,7 +151,6 @@ mod test {

match type_check(schema, &statement) {
Ok(typed) => {
eprintln!("{:#?}", &typed.literals);
assert!(typed.literals.contains(&(
EqlValue(TableColumn {
table: id("users"),
Expand Down Expand Up @@ -184,7 +183,6 @@ mod test {

match type_check(schema, &statement) {
Ok(typed) => {
eprintln!("{:#?}", &typed.literals);
assert!(typed.literals.contains(&(
EqlValue(TableColumn {
table: id("users"),
Expand Down Expand Up @@ -718,7 +716,6 @@ mod test {
let typed = match type_check(schema, &statement) {
Ok(typed) => typed,
Err(err) => {
// eprintln!("Error: {}", err, err.source());
panic!("type check failed: {:#?}", err)
}
};
Expand Down Expand Up @@ -1143,6 +1140,7 @@ mod test {
#[test]
fn update_with_concat_regression() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
example: {
Expand All @@ -1164,4 +1162,157 @@ mod test {
let statement = parse("update example set other_str = other_str || 'a'");
type_check(schema, &statement).expect("expected type check to succeed, but it failed");
}

#[test]
fn function_with_literal() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
employees: {
id,
name,
salary (EQL),
}
}
});

let statement = parse(
r#"
select upper('x'), salary from employees;
"#,
);
let typed = type_check(schema, &statement).unwrap();

error!("{:?}", typed.projection);
assert_eq!(
typed.projection,
Some(projection![
(NATIVE as upper),
(EQL(employees.salary) as salary)
])
);
}

#[test]
fn function_with_wildcard() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
employees: {
id,
name,
salary (EQL),
}
}
});

let statement = parse(
r#"
select count(*), salary from employees group by salary;
"#,
);
let typed = type_check(schema, &statement).unwrap();

assert_eq!(
typed.projection,
Some(projection![
(NATIVE as count),
(EQL(employees.salary) as salary)
])
);
}

#[test]
fn function_with_column_and_literal() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
employees: {
id,
name,
salary (EQL),
}
}
});

let statement = parse(
r#"
select concat(name, 'x'), salary from employees;
"#,
);
let typed = type_check(schema, &statement).unwrap();

assert_eq!(
typed.projection,
Some(projection![
(NATIVE(employees.name) as concat),
(EQL(employees.salary) as salary)
])
);
}

#[test]
fn function_with_param() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
employees: {
id,
name,
salary (EQL),
}
}
});

let statement = parse(
r#"
select concat(name, $1), salary from employees;
"#,
);

let typed = type_check(schema, &statement).unwrap();

let a = Value::Native(NativeValue(Some(TableColumn {
table: id("employees"),
column: id("name"),
})));

assert_eq!(typed.params, vec![a]);

assert_eq!(
typed.projection,
Some(projection![
(NATIVE(employees.name) as concat),
(EQL(employees.salary) as salary)
])
);
}

#[test]
fn function_with_eql_column_and_literal() {
let _ = tracing_subscriber::fmt::try_init();

let schema = resolver(schema! {
tables: {
employees: {
id,
name (EQL),
salary (EQL),
}
}
});

let statement = parse(
r#"
select concat(name, 'x'), salary from employees;
"#,
);

type_check(schema, &statement)
.expect_err("eql columns in functions should be a type error");
}
}
Loading