plexus-engine 0.3.6

Engine integration traits for consuming Plexus plans
Documentation
use super::*;
use plexus_serde::serialize_plan;

#[test]
fn cypher_to_serialized_to_execute_smoke() {
    let cypher = "MATCH (n:Person)-[r:KNOWS]->(m:Person) WHERE m.age > 30 RETURN n.name AS name ORDER BY name ASC LIMIT 1";
    let query = parse(cypher).expect("parse");
    let plan = test_compiler::compile_query_to_plan(&query);
    let bytes = serialize_plan(&plan).expect("serialize");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(out.rows, vec![vec![Value::String("Alice".to_string())]]);
}

#[test]
fn cypher_to_bridge_bytes_to_execute_smoke() {
    let cypher = "MATCH (n:Person)-[r:KNOWS]->(m:Person) WHERE m.age > 30 RETURN n.name AS name ORDER BY name ASC LIMIT 1";
    let query = parse(cypher).expect("parse");
    let bytes = lower_to_flatbuffer(&query).expect("bridge lower_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(out.rows, vec![vec![Value::String("Alice".to_string())]]);
}

#[test]
fn cypher_optimized_serialized_deserialized_execute() {
    let cypher = "MATCH (n:Person)-[r:KNOWS]->(m:Person) WHERE m.age > 30 RETURN n.name AS name ORDER BY name ASC LIMIT 1";
    let query = parse(cypher).expect("parse");
    let ctx = make_context();
    let mut module = lower(&query, &ctx).expect("lower");
    optimize(&mut module, &ctx, PIPELINE_CORE).expect("optimize");
    let bytes = module_to_flatbuffer(&module).expect("module_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(out.rows, vec![vec![Value::String("Alice".to_string())]]);
}

#[test]
fn cypher_union_bridge_bytes_to_execute_smoke() {
    let cypher = "MATCH (n:Person) RETURN n UNION ALL MATCH (m:Company) RETURN m";
    let query = parse(cypher).expect("parse");
    let bytes = lower_to_flatbuffer(&query).expect("bridge lower_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(
        out.rows,
        vec![
            vec![Value::NodeRef(1)],
            vec![Value::NodeRef(2)],
            vec![Value::NodeRef(3)],
        ]
    );
}

#[test]
fn cypher_union_optimized_serialized_deserialized_execute() {
    let cypher = "MATCH (n:Person) RETURN n UNION ALL MATCH (m:Company) RETURN m";
    let query = parse(cypher).expect("parse");
    let ctx = make_context();
    let mut module = lower(&query, &ctx).expect("lower");
    optimize(&mut module, &ctx, PIPELINE_CORE).expect("optimize");
    let bytes = module_to_flatbuffer(&module).expect("module_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(
        out.rows,
        vec![
            vec![Value::NodeRef(1)],
            vec![Value::NodeRef(2)],
            vec![Value::NodeRef(3)],
        ]
    );
}

#[test]
fn cypher_optional_match_optimized_serialized_deserialized_execute() {
    let cypher = "OPTIONAL MATCH (n:Person)-[r:WORKS_AT]->(c:Company) RETURN c.name AS name ORDER BY name ASC";
    let query = parse(cypher).expect("parse");
    let ctx = make_context();
    let mut module = lower(&query, &ctx).expect("lower");
    optimize(&mut module, &ctx, PIPELINE_CORE).expect("optimize");
    let bytes = module_to_flatbuffer(&module).expect("module_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(
        out.rows,
        vec![vec![Value::Null], vec![Value::String("Acme".to_string())]]
    );
}

#[test]
fn cypher_unwind_optimized_serialized_deserialized_execute() {
    let cypher = "MATCH (n:Person) WITH COLLECT(n.name) AS names UNWIND names AS name RETURN name ORDER BY name ASC";
    let query = parse(cypher).expect("parse");
    let ctx = make_context();
    let mut module = lower(&query, &ctx).expect("lower");
    optimize(&mut module, &ctx, PIPELINE_CORE).expect("optimize");
    let bytes = module_to_flatbuffer(&module).expect("module_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(
        out.rows,
        vec![
            vec![Value::String("Alice".to_string())],
            vec![Value::String("Bob".to_string())],
        ]
    );
}

#[test]
fn cypher_case_and_arith_serialized_execute() {
    let cypher = "MATCH (n:Person) RETURN CASE WHEN n.age > 35 THEN \"senior\" ELSE \"junior\" END AS bucket, n.age * 2 AS age2 ORDER BY bucket ASC";
    let query = parse(cypher).expect("parse");
    let plan = test_compiler::compile_query_to_plan(&query);
    let bytes = serialize_plan(&plan).expect("serialize");

    let mut engine = InMemoryEngine::new(fixture_graph());
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(
        out.rows,
        vec![
            vec![Value::String("junior".to_string()), Value::Int(60)],
            vec![Value::String("senior".to_string()), Value::Int(80)],
        ]
    );
}

#[test]
fn cypher_param_serialized_execute() {
    let cypher = "MATCH (n:Person) WHERE n.name = $name RETURN n.age AS age";
    let query = parse(cypher).expect("parse");
    let bytes = lower_to_flatbuffer(&query).expect("bridge lower_to_flatbuffer");

    let mut engine = InMemoryEngine::new(fixture_graph());
    engine.set_param("name", Value::String("Bob".to_string()));
    let out = execute_serialized(&mut engine, &bytes).expect("execute");
    assert_eq!(out.rows, vec![vec![Value::Int(40)]]);
}