Skip to content

Commit f464bf5

Browse files
committed
chore: cleanup
1 parent 19a8ad1 commit f464bf5

File tree

6 files changed

+105
-150
lines changed

6 files changed

+105
-150
lines changed

crates/codegen/src/get_location.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ pub fn get_location_mod(proto_file: &ProtoFile) -> proc_macro2::TokenStream {
5050
} else {
5151
None
5252
},
53+
NodeEnum::NullTest(n) => if n.arg.is_some() {
54+
get_location_internal(&n.arg.as_ref().unwrap().node.as_ref().unwrap())
55+
} else {
56+
Some(n.location)
57+
},
5358
#(NodeEnum::#node_identifiers(n) => #location_idents),*
5459
};
5560
if location.is_some() && location.unwrap() < 0 {

crates/codegen/src/get_node_properties.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,34 @@ fn custom_handlers(node: &Node) -> TokenStream {
166166
},
167167
"BoolExpr" => quote! {
168168
match n.boolop {
169+
// AndExpr = 1
169170
1 => tokens.push(TokenProperty::from(Token::And)),
171+
// OrExpr = 2
170172
2 => tokens.push(TokenProperty::from(Token::Or)),
173+
// NotExpr = 3
174+
3 => tokens.push(TokenProperty::from(Token::Not)),
171175
_ => panic!("Unknown BoolExpr {:#?}", n.boolop),
172176
}
173177
},
174178
"JoinExpr" => quote! {
175179
tokens.push(TokenProperty::from(Token::Join));
176180
tokens.push(TokenProperty::from(Token::On));
181+
match n.jointype {
182+
// JoinInner = 1
183+
1 => tokens.push(TokenProperty::from(Token::InnerP)),
184+
// JoinLeft = 2
185+
2 => tokens.push(TokenProperty::from(Token::Left)),
186+
// JoinFull = 3
187+
3 => tokens.push(TokenProperty::from(Token::Full)),
188+
// JoinRight = 4
189+
4 => tokens.push(TokenProperty::from(Token::Right)),
190+
// JoinSemi = 5
191+
// JoinAnti = 6
192+
// JoinUniqueOuter = 7
193+
// JoinUniqueInner = 8
194+
_ => panic!("Unknown JoinExpr jointype {:#?}", n.jointype),
195+
}
196+
177197
},
178198
"ResTarget" => quote! {
179199
if n.name.len() > 0 {
@@ -195,6 +215,30 @@ fn custom_handlers(node: &Node) -> TokenStream {
195215
"CollateClause" => quote! {
196216
tokens.push(TokenProperty::from(Token::Collate));
197217
},
218+
"AExpr" => quote! {
219+
match n.kind {
220+
// AexprOp = 1,
221+
1 => {
222+
// do nothing
223+
},
224+
// AexprOpAny = 2,
225+
2 => tokens.push(TokenProperty::from(Token::Any)),
226+
// AexprOpAll = 3,
227+
// AexprDistinct = 4,
228+
// AexprNotDistinct = 5,
229+
// AexprNullif = 6,
230+
// AexprIn = 7,
231+
7 => tokens.push(TokenProperty::from(Token::InP)),
232+
// AexprLike = 8,
233+
// AexprIlike = 9,
234+
// AexprSimilar = 10,
235+
// AexprBetween = 11,
236+
// AexprNotBetween = 12,
237+
// AexprBetweenSym = 13,
238+
// AexprNotBetweenSym = 14,
239+
_ => panic!("Unknown AExpr kind {:#?}", n.kind),
240+
}
241+
},
198242
"WindowDef" => quote! {
199243
if n.partition_clause.len() > 0 || n.order_clause.len() > 0 {
200244
tokens.push(TokenProperty::from(Token::Window));
@@ -406,6 +450,22 @@ fn custom_handlers(node: &Node) -> TokenStream {
406450
"CaseExpr" => quote! {
407451
tokens.push(TokenProperty::from(Token::Case));
408452
tokens.push(TokenProperty::from(Token::EndP));
453+
if n.defresult.is_some() {
454+
tokens.push(TokenProperty::from(Token::Else));
455+
}
456+
},
457+
"NullTest" => quote! {
458+
match n.nulltesttype {
459+
// IsNull
460+
1 => tokens.push(TokenProperty::from(Token::Is)),
461+
// IsNotNull
462+
2 => {
463+
tokens.push(TokenProperty::from(Token::Is));
464+
tokens.push(TokenProperty::from(Token::Not));
465+
},
466+
_ => panic!("Unknown NullTest {:#?}", n.nulltesttype),
467+
}
468+
tokens.push(TokenProperty::from(Token::NullP));
409469
},
410470
"CreateFunctionStmt" => quote! {
411471
tokens.push(TokenProperty::from(Token::Create));

crates/parser/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ mod tests {
2525
};
2626

2727
let node_graph = get_nodes(&pg_query_root.unwrap(), 0);
28-
assert_eq!(node_graph.node_count(), 13);
28+
assert_eq!(node_graph.node_count(), 14);
2929
}
3030
}

crates/parser/src/parse/libpg_query_node.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
codegen::{get_nodes, Node, SyntaxKind},
55
lexer::TokenType,
66
};
7-
use log::debug;
7+
use log::{debug, log_enabled};
88
use petgraph::{
99
stable_graph::{DefaultIx, NodeIndex, StableGraph},
1010
visit::{Bfs, Dfs},
@@ -67,6 +67,9 @@ impl<'p> LibpgQueryNodeParser<'p> {
6767
debug!("current node: {:#?}", self.current_node);
6868
debug!("current token: {:#?}", self.current_token());
6969
debug!("current location: {:#?}", self.current_location());
70+
if log_enabled!(log::Level::Debug) {
71+
debug!("node graph: {:#?}", self.node_graph);
72+
}
7073
if self.at_whitespace() {
7174
debug!("skipping whitespace");
7275
self.parser.advance();
@@ -140,10 +143,9 @@ impl<'p> LibpgQueryNodeParser<'p> {
140143
self.parser.advance();
141144
} else {
142145
panic!(
143-
"could not find node for token {:?} at depth {} in {:#?}",
146+
"could not find node for token {:?} at depth {}",
144147
self.current_token(),
145148
self.parser.depth,
146-
self.node_graph
147149
);
148150
}
149151
}
@@ -231,7 +233,7 @@ impl<'p> LibpgQueryNodeParser<'p> {
231233
debug!("found node with location at current location");
232234
// check if the location of the node is the current location
233235
// do a depth-first search to find the first node that either has a location that
234-
// is not the current one, or has properties that are not the current token
236+
// is not the current one, or has the current token as a property
235237
let mut dfs = Dfs::new(&self.node_graph, nx);
236238
let mut target_nx = nx;
237239
while let Some(node_idx) = dfs.next(&self.node_graph) {
@@ -377,7 +379,10 @@ impl<'p> LibpgQueryNodeParser<'p> {
377379
}
378380

379381
fn current_location(&self) -> usize {
380-
usize::from(self.current_token().span.start())
382+
usize::from(
383+
self.current_token().span.start()
384+
- self.parser.tokens[self.token_range.start].span.start(),
385+
)
381386
}
382387

383388
fn current_token(&self) -> &crate::lexer::Token {
@@ -389,7 +394,6 @@ impl<'p> LibpgQueryNodeParser<'p> {
389394
}
390395

391396
fn at_whitespace(&self) -> bool {
392-
// TODO: merge whitespace token def with whitespace def in parser
393397
self.current_token().token_type == TokenType::Whitespace
394398
}
395399

crates/parser/src/parse/statement.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::Parser;
1111
pub fn statement(parser: &mut Parser, kind: SyntaxKind) {
1212
let token_range = collect_statement_token_range(parser, kind);
1313
let tokens = parser.tokens.get(token_range.clone()).unwrap().to_vec();
14-
println!("tokens: {:?}", tokens);
1514
match pg_query::parse(
1615
tokens
1716
.iter()

crates/parser/src/parser.rs

Lines changed: 29 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -293,116 +293,36 @@ mod tests {
293293
}
294294

295295
#[test]
296-
fn test_parser_beefy() {
296+
fn test_parser_anonther() {
297297
init();
298298

299-
panic_after(Duration::from_millis(1000), || {
300-
let input = "SELECT
301-
c.oid::int8 AS id,
302-
nc.nspname AS schema,
303-
c.relname AS name,
304-
c.relrowsecurity AS rls_enabled,
305-
c.relforcerowsecurity AS rls_forced,
306-
CASE
307-
WHEN c.relreplident = 'd' THEN 'DEFAULT'
308-
WHEN c.relreplident = 'i' THEN 'INDEX'
309-
WHEN c.relreplident = 'f' THEN 'FULL'
310-
ELSE 'NOTHING'
311-
END AS replica_identity,
312-
pg_total_relation_size(format('%I.%I', nc.nspname, c.relname)) :: int8 AS bytes,
313-
pg_size_pretty(
314-
pg_total_relation_size(format('%I.%I', nc.nspname, c.relname))
315-
) AS size,
316-
pg_stat_get_live_tuples(c.oid) AS live_rows_estimate,
317-
pg_stat_get_dead_tuples(c.oid) AS dead_rows_estimate,
318-
obj_description(c.oid) AS comment,
319-
coalesce(pk.primary_keys, '[]') as primary_keys,
320-
coalesce(
321-
jsonb_agg(relationships) filter (where relationships is not null),
322-
'[]'
323-
) as relationships
324-
FROM
325-
pg_namespace nc
326-
JOIN pg_class c ON nc.oid = c.relnamespace
327-
left join (
328-
select
329-
table_id,
330-
jsonb_agg(_pk.*) as primary_keys
331-
from (
332-
select
333-
n.nspname as schema,
334-
c.relname as table_name,
335-
a.attname as name,
336-
c.oid :: int8 as table_id
337-
from
338-
pg_index i,
339-
pg_class c,
340-
pg_attribute a,
341-
pg_namespace n
342-
where
343-
i.indrelid = c.oid
344-
and c.relnamespace = n.oid
345-
and a.attrelid = c.oid
346-
and a.attnum = any (i.indkey)
347-
and i.indisprimary
348-
) as _pk
349-
group by table_id
350-
) as pk
351-
on pk.table_id = c.oid
352-
left join (
353-
select
354-
c.oid :: int8 as id,
355-
c.conname as constraint_name,
356-
nsa.nspname as source_schema,
357-
csa.relname as source_table_name,
358-
sa.attname as source_column_name,
359-
nta.nspname as target_table_schema,
360-
cta.relname as target_table_name,
361-
ta.attname as target_column_name
362-
from
363-
pg_constraint c
364-
join (
365-
pg_attribute sa
366-
join pg_class csa on sa.attrelid = csa.oid
367-
join pg_namespace nsa on csa.relnamespace = nsa.oid
368-
) on sa.attrelid = c.conrelid and sa.attnum = any (c.conkey)
369-
join (
370-
pg_attribute ta
371-
join pg_class cta on ta.attrelid = cta.oid
372-
join pg_namespace nta on cta.relnamespace = nta.oid
373-
) on ta.attrelid = c.confrelid and ta.attnum = any (c.confkey)
374-
where
375-
c.contype = 'f'
376-
) as relationships
377-
on (relationships.source_schema = nc.nspname and relationships.source_table_name = c.relname)
378-
or (relationships.target_table_schema = nc.nspname and relationships.target_table_name = c.relname)
379-
WHERE
380-
c.relkind IN ('r', 'p')
381-
AND NOT pg_is_other_temp_schema(nc.oid)
382-
AND (
383-
pg_has_role(c.relowner, 'USAGE')
384-
OR has_table_privilege(
385-
c.oid,
386-
'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'
387-
)
388-
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES')
389-
)
390-
group by
391-
c.oid,
392-
c.relname,
393-
c.relrowsecurity,
394-
c.relforcerowsecurity,
395-
c.relreplident,
396-
nc.nspname,
397-
pk.primary_keys";
398-
399-
let mut p = Parser::new(lex(input));
400-
source(&mut p);
401-
let result = p.finish();
299+
let input = "SELECT 1 from contact c
300+
JOIN pg_class c ON nc.oid = c.relnamespace
301+
left join (
302+
select
303+
table_id,
304+
jsonb_agg(_pk.*) as primary_keys
305+
from (
306+
select 1
307+
from
308+
pg_index i,
309+
pg_class c,
310+
pg_attribute a,
311+
pg_namespace n
312+
where
313+
i.indrelid = c.oid
314+
and c.relnamespace = n.oid
315+
) as _pk
316+
group by table_id
317+
) as pk
318+
on pk.table_id = c.oid;";
319+
320+
let mut p = Parser::new(lex(input));
321+
source(&mut p);
322+
let result = p.finish();
402323

403-
dbg!(&result.cst);
404-
println!("{:#?}", result.errors);
405-
})
324+
dbg!(&result.cst);
325+
println!("{:#?}", result.errors);
406326
}
407327

408328
#[test]
@@ -425,49 +345,16 @@ group by
425345
fn test_parser_simple() {
426346
init();
427347

428-
panic_after(Duration::from_millis(100), || {
429-
let input = "alter table x rename to y \n alter table x alter column z set default 1";
348+
let input = "alter table x rename to y \n alter table x alter column z set default 1";
430349

431-
let mut p = Parser::new(lex(input));
432-
source(&mut p);
433-
let result = p.finish();
434-
435-
dbg!(&result.cst);
436-
println!("{:#?}", result.errors);
437-
})
438-
}
439-
440-
#[test]
441-
fn test_parser_temp() {
442-
init();
443-
444-
let mut p = Parser::new(lex("select 1;"));
350+
let mut p = Parser::new(lex(input));
445351
source(&mut p);
446352
let result = p.finish();
447353

448354
dbg!(&result.cst);
449355
println!("{:#?}", result.errors);
450356
}
451357

452-
#[test]
453-
fn test_parser_playground() {
454-
init();
455-
456-
let res = pg_query::parse(
457-
" CREATE TABLE weather (
458-
city varchar(80) references cities(name),
459-
temp_lo int,
460-
temp_hi int,
461-
prcp real,
462-
date date
463-
);",
464-
)
465-
.unwrap();
466-
res.protobuf.nodes().iter().for_each(|node| {
467-
println!("{:#?}", node);
468-
});
469-
}
470-
471358
fn panic_after<T, F>(d: Duration, f: F) -> T
472359
where
473360
T: Send + 'static,

0 commit comments

Comments
 (0)