Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
akrambek committed Jan 23, 2025
1 parent 2244de1 commit b3dc0ce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void syntaxError(
RecognitionException e)
{
//Only for debugging
//System.err.println("Syntax error at line " + line + ":" + charPositionInLine + " " + msg);
System.err.println("Syntax error at line " + line + ":" + charPositionInLine + " " + msg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void enterFrom_clause(
public void enterWhere_clause(
PostgreSqlParser.Where_clauseContext ctx)
{
whereClause = tokens.getText(ctx);
whereClause = tokens.getText(ctx).replaceAll("(?i)\\bwhere\\s", "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,56 +453,62 @@ public void shouldParseShowZtables()
public void shouldParseCreateZfunctionWithTableReturnType()
{
String sql = """
CREATE ZFUNCTION send_payment_handler(
user_id VARCHAR,
amount DOUBLE PRECISION)
CREATE ZFUNCTION send_payment_handler(
user_id VARCHAR,
request_id VARCHAR,
amount DOUBLE PRECISION,
notes VARCHAR)
RETURNS TABLE(
event VARCHAR,
user_id VARCHAR,
amount DOUBLE PRECISION)
event VARCHAR,
user_id VARCHAR,
request_id VARCHAR,
amount DOUBLE PRECISION,
notes VARCHAR)
LANGUAGE SQL AS $$
SELECT
CASE
WHEN balance >= args.amount THEN "PaymentSent"
ELSE "PaymentDeclined"
END AS event,
SELECT
CASE
WHEN balance >= args.amount THEN 'PaymentSent'
ELSE 'PaymentDeclined'
END AS event,
args.user_id,
args.amount
FROM balance WHERE user_id = args.user_id;
args.request_id,
args.amount,
args.notes
FROM balance b WHERE b.user_id = args.user_id;
$$
WITH(
EVENTS = 'app_events'
)
);
""";
CreateZfunction function = parser.parseCreateZfunction(sql);
assertNotNull(function);

assertEquals("send_payment_handler", function.name());
assertEquals(2, function.arguments().size());
assertEquals(4, function.arguments().size());
assertEquals("user_id", function.arguments().get(0).name());
assertEquals("VARCHAR", function.arguments().get(0).type());
assertEquals("amount", function.arguments().get(1).name());
assertEquals("DOUBLE PRECISION", function.arguments().get(1).type());
assertEquals("request_id", function.arguments().get(1).name());
assertEquals("VARCHAR", function.arguments().get(1).type());

assertEquals(3, function.returnTypes().size());
assertEquals(5, function.returnTypes().size());
assertEquals("event", function.returnTypes().get(0).name());
assertEquals("VARCHAR", function.returnTypes().get(0).type());
assertEquals("user_id", function.returnTypes().get(1).name());
assertEquals("VARCHAR", function.returnTypes().get(1).type());
assertEquals("amount", function.returnTypes().get(2).name());
assertEquals("DOUBLE PRECISION", function.returnTypes().get(2).type());
assertEquals("request_id", function.returnTypes().get(2).name());
assertEquals("VARCHAR", function.returnTypes().get(2).type());

Select select = function.select();
assertNotNull(select);
assertEquals(3, select.columns().size());
assertEquals(5, select.columns().size());
assertEquals("CASE\n" +
" WHEN balance >= args.amount THEN \"PaymentSent\"\n" +
" ELSE \"PaymentDeclined\"\n" +
" WHEN balance >= args.amount THEN 'PaymentSent'\n" +
" ELSE 'PaymentDeclined'\n" +
" END AS event", select.columns().get(0));
assertEquals("args.user_id", select.columns().get(1));
assertEquals("args.amount", select.columns().get(2));
assertEquals("balance", select.from());
assertEquals("WHERE user_id = args.user_id", select.whereClause());
assertEquals("args.request_id", select.columns().get(2));
assertEquals("balance b", select.from());
assertEquals("b.user_id = args.user_id", select.whereClause());

assertEquals("app_events", function.events());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public void onStarted(

if (select.whereClause() != null)
{
join = String.format("%s ON %s", join, select.whereClause().replace("args", "c"));
join = String.format("%s ON %s", join, select.whereClause()
.replace("args", "c"));
}
}

Expand Down

0 comments on commit b3dc0ce

Please sign in to comment.