Skip to content

Commit

Permalink
Allow parsing sql expressions (#2389)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey authored Oct 19, 2023
1 parent c6100b9 commit bdd95cf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.finos.legend.engine.language.sql.grammar.from.antlr4.SqlBaseLexer;
import org.finos.legend.engine.language.sql.grammar.from.antlr4.SqlBaseParser;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.sql.metamodel.Expression;
import org.finos.legend.engine.protocol.sql.metamodel.Statement;

import java.util.BitSet;
Expand All @@ -51,6 +52,12 @@ public Statement parseStatement(String query)
return this.parse(query, "statement");
}

public Expression parseExpression(String expression)
{
SqlBaseParser parser = getSqlBaseParser(expression, "expression");
return (Expression) sqlVisitor.visitSingleExpression(parser.singleExpression());
}

private Statement parse(String query, String name)
{
SqlBaseParser parser = getSqlBaseParser(query, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,36 @@ public void testWhere()
check("SELECT * FROM myTable WHERE col1 = 1");
}

@Test
public void testWhereExpression()
{
checkExpression("col1 = 1");
}

@Test
public void testCompositeWhere()
{
check("SELECT * FROM myTable WHERE col1 = 1 AND col2 = 1");
}

@Test
public void testCompositeWhereExpression()
{
checkExpression("col1 = 1 AND col2 = 1");
}

@Test
public void testWhereQualified()
{
check("SELECT * FROM myTable WHERE myTable.col1 = 1");
}

@Test
public void testWhereQualifiedExpression()
{
checkExpression("myTable.col1 = 1");
}

@Test
public void testCompositeWhereQualifiedWithAlias()
{
Expand All @@ -155,6 +173,15 @@ public void testCompositeWhereOperators()
"col BETWEEN 0 AND 1");
}

@Test
public void testCompositeWhereOperatorsExpression()
{
checkExpression("col = 1 AND col > 1 AND col < 1 " +
"AND col >= 1 AND col <= 1 AND col IN (1, 2, 3) AND col IS NULL AND " +
"col IS NOT NULL AND col IS DISTINCT FROM 1 AND col IS NOT DISTINCT FROM 1 AND " +
"col BETWEEN 0 AND 1");
}

@Test
public void testGroupBy()
{
Expand Down Expand Up @@ -324,4 +351,19 @@ private void check(String sql, String expected)
String result = composer.renderNode(node);
MatcherAssert.assertThat(result.trim(), IsEqualIgnoringCase.equalToIgnoringCase(expected));
}

private void checkExpression(String expression)
{
checkExpression(expression, expression);
checkExpression(expression.toLowerCase(), expression);
}

private void checkExpression(String expression, String expected)
{
SQLGrammarParser parser = SQLGrammarParser.newInstance();
Node node = parser.parseExpression(expression);
SQLGrammarComposer composer = SQLGrammarComposer.newInstance();
String result = composer.renderNode(node);
MatcherAssert.assertThat(result.trim(), IsEqualIgnoringCase.equalToIgnoringCase(expected));
}
}

0 comments on commit bdd95cf

Please sign in to comment.