Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Postgres SQL function cannot be used from T-SQL with CTE #3071

Open
1 task done
staticlibs opened this issue Nov 3, 2024 · 0 comments
Open
1 task done

[Bug]: Postgres SQL function cannot be used from T-SQL with CTE #3071

staticlibs opened this issue Nov 3, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@staticlibs
Copy link
Contributor

What happened?

If I create a PG function helper like this (a wrapper to xpath function that returns only the first element from the resulting array, and returns NULL for no results):

create function master_dbo.my_xpath_first(expr sys.nvarchar, doc xml, ns sys.nvarchar)
returns xml as $$
	with cte(arr) as (select xpath(expr::text, doc, ns::text[][]))
	select
		case when array_length(arr, 1) > 0
		then arr[1]
		else null
		end
	from cte
$$ language sql immutable 

It works from T-SQL in simple examples:

select my_xpath_first('//a:item/@id', cast('
<root xmlns="ns1">
    <item id="41"/>
    <item/>
    <item id="43"/>
</root>
' as xml), '{{a, ns1}}')
my_xpath_first
----------------------
41

But it breaks when CTE is used in T-SQL like this:

with items_cte (item) as (
    select unnest(xpath('//a:item', cast('
<root xmlns="ns1">
    <item id="41"/>
    <item/>
    <item id="43"/>
</root>   
    ' as xml), '{{a, ns1}}'))
    as item
)
select 
    my_xpath_first('/a:item/@id', item, '{{a, ns1}}') as id
from items_cte
Msg 33557097, Level 16, State 1, Line 10
zero-length delimited identifier at or near "[]"

There is a workaround to use language plpgsql instead of language sql, but I assume that SQL function is supposed to work too.

Version

BABEL_4_X_DEV (Default)

Extension

babelfishpg_tsql (Default)

Which flavor of Linux are you using when you see the bug?

Fedora

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct.
@staticlibs staticlibs added the bug Something isn't working label Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant