Add some tests for codegen optimizations
This adds tests for the perf issues fixed in and
@@ -22,8 +22,11 @@ import os.path
from edb.testbase import lang as tb
from edb.tools import test
+from edb.common.ast import visitor as ast_visitor
+
from edb.edgeql import compiler
from edb.edgeql import parser as qlparser
+from edb.pgsql import ast as pgast
from edb.pgsql import compiler as pg_compiler
@@ -36,6 +39,9 @@ class TestEdgeQLSQLCodegen(tb.BaseEdgeQLCompilerTest):
SCHEMA = os.path.join(os.path.dirname(__file__), 'schemas',
'cards.esdl')
+ SCHEMA_ISSUES = os.path.join(os.path.dirname(__file__), 'schemas',
+ 'issues.esdl')
+
def _compile_to_tree(self, source):
qltree = qlparser.parse(source)
ir = compiler.compile_ast_to_ir(
@@ -69,3 +75,51 @@ class TestEdgeQLSQLCodegen(tb.BaseEdgeQLCompilerTest):
self.assertEqual(
sql.count(user_id_str), 1, "User table referenced more than once"
)
+
+ def test_codegen_elide_optional_wrapper(self):
+ sql = self._compile('''
+ with module issues
+ select Issue { te := .time_estimate ?? -1 }
+ ''')
+
+ # One distinguishing characteristic of an optional wrapper is
+ # selecting '("m~1" = first_value("m~1") OVER ())'
+ self.assertNotIn(
+ "OVER ()", sql,
+ "optional wrapper generated when it shouldn't be needed"
+ )
+
+ def test_codegen_order_by_not_subquery_01(self):
+ sql = self._compile_to_tree('''
+ select User order by .name
+ ''')
+ child = ast_visitor.find_children(
+ sql,
+ lambda x: isinstance(x, pgast.SelectStmt) and x.sort_clause,
+ terminate_early=True
+ )
+
+ # Make sure that a simple order by on a property is not compiled
+ # as a subquery in the ORDER BY, which pg fails to use an index for.
+ self.assertIsInstance(
+ child.sort_clause[0].node, pgast.ColumnRef,
+ "simple sort clause is not a column ref",
+ )
+
+ def test_codegen_order_by_not_subquery_02(self):
+ # Same as above but a bit more involved
+ sql = self._compile_to_tree('''
+ select User { z := .name ++ "!" } order by .z
+ ''')
+ child = ast_visitor.find_children(
+ sql,
+ lambda x: isinstance(x, pgast.SelectStmt) and x.sort_clause,
+ terminate_early=True
+ )
+
+ # Make sure that a simple order by on a property is not compiled
+ # as a subquery in the ORDER BY, which pg fails to use an index for.
+ self.assertIsInstance(
+ child.sort_clause[0].node, pgast.Expr,
+ "simple sort clause is not a op expr",
+ )
