Skip to content

Commit

Permalink
Arithmetic type inference (#340)
Browse files Browse the repository at this point in the history
* Rewrite type inference for arithmetic operations

* Add program analysis test case

---------

Co-authored-by: Alex Ivliev <[email protected]>
  • Loading branch information
Alex Ivliev and aannleax authored Aug 16, 2023
1 parent afcae2f commit ba4034a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
50 changes: 27 additions & 23 deletions nemo-physical/src/management/type_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cmp::Ordering, collections::HashMap, fmt::Display};

use crate::{
columnar::operations::columnscan_arithmetic::ArithmeticOperation,
datatypes::{casting::PartialUpperBound, DataTypeName},
error::Error,
tabular::{operations::triescan_append::AppendInstruction, traits::table_schema::TableSchema},
Expand Down Expand Up @@ -298,35 +299,38 @@ impl TypeTree {
continue;
}

let operation_type =
if let Some(&first_index) = tree.input_indices().first() {
let operation_type = subtype_node
.schema
.get_entry(*first_index)
.partial_upper_bound();
let mut operation_type: Option<DataTypeName> = None;

for &column_index in tree.input_indices() {
let current_type = subtype_node
// We check whether the type of each leaf node has the same upper bound
for leaf in tree.leaves() {
let current_type = match leaf {
ArithmeticOperation::Constant(constant) => {
constant.get_type().partial_upper_bound()
}
ArithmeticOperation::ColumnScan(column_index) => {
subtype_node
.schema
.get_entry(column_index)
.partial_upper_bound();

if !Self::compatible(&operation_type, &current_type) {
return Err(Error::InvalidExecutionPlan);
}
.get_entry(*column_index)
.partial_upper_bound()
}
ArithmeticOperation::Addition
| ArithmeticOperation::Subtraction
| ArithmeticOperation::Multiplication
| ArithmeticOperation::Division => {
unreachable!("Not a leaf node")
}
};

operation_type
if let Some(operation_type) = operation_type {
if !Self::compatible(&operation_type, &current_type) {
return Err(Error::InvalidExecutionPlan);
}
} else {
// TODO: Revisit this path.

let operation_type =
subtype_node.schema.get_entry(0).partial_upper_bound();

operation_type
};
operation_type = Some(current_type);
}
}

new_schema.add_entry(operation_type);
new_schema.add_entry(operation_type.expect("operation_type will be assigned because the the operation tree must contain at least one leaf node."));
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions resources/testcases/program_analysis/run.rls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@declare ConstFolding(integer,string, integer, integer, string) .
@declare AssignOperFrom(string, integer, string).
@declare AssignOperFromConstant(string, integer, integer) .
@declare AssignNumConstant(string, integer, integer, string) .

AssignBinop("assign7",10,"l4A1A1104") .
AssignOperFrom("assign7",1,"l4A1102") .
AssignOperFromConstant("assign7",2,32) .
AssignNumConstant("assign6",8,256,"l4A1102").

ConstFolding(?val2 +?val1, ?ins, ?val1, ?val2,"+") :-
AssignOperFromConstant(?ins, ?pos1, ?val1) ,
AssignOperFrom(?ins, ?pos2, ?var2) ,
AssignNumConstant(?ins2, ?ln, ?val2, ?var2).

@output ConstFolding .
1 change: 1 addition & 0 deletions resources/testcases/program_analysis/run/ConstFolding.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
288,assign7,32,256,+

0 comments on commit ba4034a

Please sign in to comment.