diff --git a/sqlglot/dialects/e6.py b/sqlglot/dialects/e6.py index e8999a861c..6400387a05 100644 --- a/sqlglot/dialects/e6.py +++ b/sqlglot/dialects/e6.py @@ -2042,11 +2042,27 @@ def exists_sql(self, expression: exp.Exists) -> str: For E6: - Function form (with lambda): Quote as "EXISTS"(array, lambda) - Subquery form (no lambda): Keep as EXISTS(subquery) + - Special case: EXISTS (SELECT 1 WHERE condition) without FROM -> simplify to just condition """ # Check if this is the higher-order function form (has a lambda expression) if expression.expression: # Function form: exists(array, lambda) -> "EXISTS"(array, lambda) return f'"EXISTS"({self.sql(expression.this)}, {self.sql(expression.expression)})' + + # Check if simplification is enabled via environment variable + simplify_exists = os.getenv("SIMPLIFY_EXISTS_WITHOUT_FROM", "false").lower() == "true" + + if simplify_exists: + # Check for the pattern: EXISTS (SELECT ... WHERE condition) without FROM + subquery = expression.this + if isinstance(subquery, exp.Select): + where = subquery.args.get("where") + from_clause = subquery.args.get("from") + + # If SELECT ... WHERE condition (no FROM clause), simplify to just condition + if where and not from_clause: + return self.sql(where.this) + # Subquery form: EXISTS(subquery) -> EXISTS(subquery) return f"EXISTS {self.wrap(expression)}" diff --git a/tests/dialects/test_e6.py b/tests/dialects/test_e6.py index 5f4eaffe11..75361a317e 100644 --- a/tests/dialects/test_e6.py +++ b/tests/dialects/test_e6.py @@ -866,6 +866,40 @@ def test_E6(self): read={"databricks": "SELECT cast(col1 AS BINARY) FROM tab1"}, ) + def test_exists_simplification_with_env_flag(self): + """Test EXISTS simplification when SIMPLIFY_EXISTS_WITHOUT_FROM env flag is set""" + + # Test simple EXISTS without FROM clause + os.environ["SIMPLIFY_EXISTS_WITHOUT_FROM"] = "true" + try: + # Simple case - EXISTS without FROM should be simplified + self.validate_all( + "SELECT abc.* FROM main.experiments_db.array_test AS abc WHERE abc.id = 103", + read={ + "databricks": "SELECT abc.* FROM main.experiments_db.array_test abc WHERE EXISTS (SELECT 1 WHERE abc.id = 103)" + }, + ) + + # Complex case with multiple EXISTS clauses - EXISTS with FROM kept, EXISTS without FROM simplified + self.validate_all( + "SELECT * FROM \"entity_v\" AS ev1_0 LEFT JOIN EntityAttributeValue_v AS a1_0 ON ev1_0.entityID = a1_0.entityID JOIN OrgHierarchy AS oh1_0 ON oh1_0.orgID = ev1_0.orgID AND (oh1_0.isDeleted = FALSE) WHERE (ev1_0.UTCOffsetMinutes = 0) AND (EXISTS (SELECT \"1x\" FROM Issues_V_IssueManagementCurrentStageApprovers AS sa1_0 WHERE sa1_0.currentStageApproverId IN ('7a74d569-ec47-4634-a6c3-2efbb0c37c3d') AND ev1_0.entityID = sa1_0.issueId AND (sa1_0.UTCOffsetMinutes = 0)) OR EXISTS (SELECT 1 FROM EntityAttributeValue_v AS a2_0 WHERE a2_0.attributeName = 'Owners' AND a2_0.attributeValueID = '7a74d569-ec47-4634-a6c3-2efbb0c37c3d' AND ev1_0.entityID = a2_0.entityID) OR EXISTS (SELECT 1 FROM EntityAttributeValue_v AS a3_0 WHERE a3_0.attributeName = 'Approvers' AND a3_0.attributeValueID = '7a74d569-ec47-4634-a6c3-2efbb0c37c3d' AND ev1_0.entityID = a3_0.entityID) OR ev1_0.createdByUserEmail IN ('renato.chavez@zendesk.com') OR oh1_0.parentOrgID = '1c8870c0-5cdd-4838-9709-a7a5430471ab')", + read={ + "databricks": "SELECT * FROM `entity_v` ev1_0 LEFT JOIN EntityAttributeValue_v a1_0 ON ev1_0.entityID = a1_0.entityID JOIN OrgHierarchy oh1_0 ON oh1_0.orgID = ev1_0.orgID AND (oh1_0.isDeleted = false) WHERE (ev1_0.UTCOffsetMinutes = 0) AND (EXISTS(SELECT 1x FROM Issues_V_IssueManagementCurrentStageApprovers sa1_0 WHERE sa1_0.currentStageApproverId IN ('7a74d569-ec47-4634-a6c3-2efbb0c37c3d') AND ev1_0.entityID = sa1_0.issueId AND (sa1_0.UTCOffsetMinutes = 0)) OR EXISTS(SELECT 1 FROM EntityAttributeValue_v a2_0 WHERE a2_0.attributeName = 'Owners' AND a2_0.attributeValueID = '7a74d569-ec47-4634-a6c3-2efbb0c37c3d' AND ev1_0.entityID = a2_0.entityID) OR EXISTS(SELECT 1 FROM EntityAttributeValue_v a3_0 WHERE a3_0.attributeName = 'Approvers' AND a3_0.attributeValueID = '7a74d569-ec47-4634-a6c3-2efbb0c37c3d' AND ev1_0.entityID = a3_0.entityID) OR EXISTS(SELECT 1 WHERE ev1_0.createdByUserEmail IN ('renato.chavez@zendesk.com')) OR oh1_0.parentOrgID = '1c8870c0-5cdd-4838-9709-a7a5430471ab')" + }, + ) + finally: + # Clean up environment variable + if "SIMPLIFY_EXISTS_WITHOUT_FROM" in os.environ: + del os.environ["SIMPLIFY_EXISTS_WITHOUT_FROM"] + + # Test that without the flag, EXISTS remains unchanged + self.validate_all( + "SELECT abc.* FROM main.experiments_db.array_test AS abc WHERE EXISTS (SELECT 1 WHERE abc.id = 103)", + read={ + "databricks": "SELECT abc.* FROM main.experiments_db.array_test abc WHERE EXISTS (SELECT 1 WHERE abc.id = 103)" + }, + ) + def test_regex(self): self.validate_all( "REGEXP_REPLACE('abcd', 'ab', '')",