question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CROSS and OUTER APPLY when using hana provider

See original GitHub issue

Hello ! Nested many to many associations end up generating this query but APPLY is not supported by HANA

     DECLARE @p_1  -- Int32
      SET     @p_1 = 50
      
      SELECT
        "key_data_result"."KUNNR",
        "key_data_result"."MANDT",
        "t2"."Id",
        "t2"."Name",
        "t2"."BUKRS",
        "t2"."MANDT",
        "t2"."Name_1"
      FROM
        (
                SELECT DISTINCT
                        "t1"."KUNNR",
                        "t1"."MANDT"
                FROM
                        (
                                SELECT
                                        "p"."KUNNR",
                                        "p"."MANDT"
                                FROM
                                        "SAPABAP1"."KNA1" "p"
                                WHERE
                                        "p"."MANDT" = '100' AND Lower("p"."SORTL") = 'webportal'
                                ORDER BY
                                        "p"."ERDAT" DESC
                                LIMIT :"p_1"
                        ) "t1"
        ) "key_data_result"
                CROSS APPLY (
                        SELECT
                                "detail"."WERKS" as "Id",
                                "detail"."NAME1" as "Name",
                                "c_1"."BUKRS",
                                "c_1"."MANDT",
                                "c_1"."BUTXT" as "Name_1"
                        FROM
                                "SAPABAP1"."T001W" "detail"
                                        LEFT JOIN "SAPABAP1"."T001K" "a_CompanyAgencyAssociation" ON "detail"."WERKS" = "a_CompanyAgencyAssociation"."BWKEY" AND "detail"."MANDT" = "a_CompanyAgencyAssociation"."MANDT"
                                        LEFT JOIN "SAPABAP1"."T001" "c_1" ON "c_1"."BUKRS" = "a_CompanyAgencyAssociation"."BUKRS" AND "c_1"."MANDT" = "a_CompanyAgencyAssociation"."MANDT"
                        WHERE
                                EXISTS(
                                        SELECT
                                                *
                                        FROM
                                                "SAPABAP1"."KNVV" "e"
                                        WHERE
                                                "key_data_result"."KUNNR" = "e"."KUNNR" AND
                                                "key_data_result"."MANDT" = "e"."MANDT" AND
                                                "e"."VKORG" = "detail"."WERKS" AND
                                                "e"."MANDT" = "detail"."MANDT"
                                )
                ) "t2"
        // class Customer
        [GraphQLIgnore]
        [Association(ThisKey = "Id, Mandt", OtherKey = "CustomerId, Mandt")]
        public CustomerAgencyAssociation[] CustomerAgencyAssociation { get; set; }

        [Association(QueryExpressionMethod = nameof(AgencyExpression), CanBeNull = true)]
        public Agency[] Agencies { get; set; }

        public static Expression<Func<Customer, IDataContext, IQueryable<Agency>>> AgencyExpression =>
        (c, db) => db.GetTable<Agency>().Where(a => c.CustomerAgencyAssociation.Any(e => e.AgencyId == a.Id && e.Mandt == a.Mandt));
        // class Agency

        [GraphQLIgnore]
        [Association(ThisKey = "Id, Mandt", OtherKey = "AgencyId, Mandt")]
        public CompanyAgencyAssociation CompanyAgencyAssociation { get; set; }

        [Association(QueryExpressionMethod = nameof(AgencyExpression), CanBeNull = true)]
        public Agency[] Agencies { get; set; }

        public static Expression<Func<Agency, IDataContext, IQueryable<Company>>> CompanyExpression =>
        (r, db) => db.GetTable<Company>().Where(c => c.Id == r.CompanyAgencyAssociation.CompanyId && c.Mandt == r.CompanyAgencyAssociation.Mandt);

            var test = _connection.Customers.LoadWith(e => e.Agencies).ThenLoad(e => e.Company).ToList();

image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Aug 3, 2021

Looks like SAP HANA 2.0 supports LATERAL join. We can correct SQL generation for such database if you are using latest SAP HANA version.

0reactions
MaceWinducommented, Aug 7, 2021

@lucas-garrido could you provide correct mappings for your query?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SAP HANA alternative for cross apply - sql
I have a sql query that I don't want to use as sub-query or join (too long and same query will be used...
Read more >
From the Archives: Cross and Outer Apply
The semantics of CROSS APPLY are similar to Cartesian product or inner join. For left-outer join semantics, one can utilize OUTER APPLY. As...
Read more >
The Difference between CROSS APPLY and OUTER ...
1) CROSS APPLY and 2) OUTER APPLY. The CROSS APPLY operator is semantically similar to INNER JOIN operator.
Read more >
HANA SQL join alternatives to CROSS APPLY / LATERAL
I am working with SAP Hana version 2.00.037.02.1562323855 (System -> Status). Is there an equivalent SAP HANA SQL statement / function that is ......
Read more >
Hana sql cross apply
You can use the cross join in SAP HANA, that's not a problem. ... Web3 Aug 2021 · CROSS and OUTER APPLY when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found