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

Add table function to execute stored procedure in SQLServer #16696

Merged

Conversation

Praveen2112
Copy link
Member

@Praveen2112 Praveen2112 commented Mar 23, 2023

Description

A dedicated table function which allows us to run stored procedures in SQLServer. We can't use existing query table function - as it wraps with inside a query like SELECT * FROM (EXECUTE my_procedure) o - so we are handing them via a different function. All the pushdown are disabled for JdbcProcedureHandle resolved by this Procedure.

Slightly different implementation from #15982

Limitation

  • Stored procedures with multiple ResultSet are not supported.
  • Stored procedures with output variables are not supported.
  • Non Select statements are not supported.

Example

SELECT * FROM TABLE(system.procedure(query => 'EXECUTE my_schema.my_new_procedure')

Release notes

( ) This is not user-visible or docs only and no release notes are required.
(x) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:

# SQLServer 
* Add support for executing stored procedures using `procedure` table function. 

These methods can be reused for Procedures PTF
- Extract building columns from ResultSetMetaData as a separate method.
- Extract creating connection based on session
@Praveen2112 Praveen2112 force-pushed the praveen/stored_procedure_ptf_3 branch from d676f4a to a584b86 Compare March 23, 2023 15:10
@github-actions github-actions bot added the docs label Mar 23, 2023
Copy link
Member

@kokosing kokosing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

% nits

The ``procedure`` function allows you to run stored procedures on the underlying
database directly. It requires syntax native to SQL Server, because the full query
is pushed down and processed in SQL Server. In order to use this table function we
need to set ``sqlserver.stored-procedure-table-function.enabled`` to ``true``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write why this toggle exists. To mention because of security reasons and the fact it is experimental its signature may change in future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it as a warning

@Praveen2112 Praveen2112 force-pushed the praveen/stored_procedure_ptf_3 branch from a584b86 to 77fc2a9 Compare March 27, 2023 15:05
@Praveen2112
Copy link
Member Author

@kokosing AC

@jhlodin jhlodin self-requested a review March 27, 2023 16:37
Copy link
Contributor

@jhlodin jhlodin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple quick changes needed to the docs, otherwise the docs LGTM.

The ``procedure`` function allows you to run stored procedures on the underlying
database directly. It requires syntax native to SQL Server, because the full query
is pushed down and processed in SQL Server. In order to use this table function we
need to set ``sqlserver.stored-procedure-table-function.enabled`` to ``true``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"we need to set ... to true" -> "set the ... catalog configuration property to true"


.. warning::

This feature is experimental only. The function signature might change in the future and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"signature" -> "syntax"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "in the future"

.. warning::

This feature is experimental only. The function signature might change in the future and
it could be backward incompatible.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "it could"



The follow example runs the stored procedure ``employee_sp`` in the ``example`` catalog and the
``example_schema`` in the underlying SQL Server database::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"example_schema in the" -> "example_schema schema in the"

FROM
TABLE(
example.system.procedure(
query => 'EXECUTE example_schema.employee_sp 0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this added whitespace after query needed or was that added in error?

)
);

If the stored procedure ``employee_sp`` requires any input ::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a bit more descriptive, like "requires any input, append the parameter value to the procedure statement::"

Copy link
Member

@skrzypo987 skrzypo987 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, lgtm

@@ -152,6 +160,10 @@ public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTabl
@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle table, Constraint constraint)
{
if (table instanceof JdbcProcedureHandle) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd extract table instanceof JdbcProcedureHandle as a simple static method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added,

return storedProcedureTableFunctionEnabled;
}

@Config("sqlserver.stored-procedure-table-function.enabled")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs say it's experimental, and there is no experimental prefix here. Can you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out. Added prefix.

@Test
public void testProcedureWithInsertOperation()
{
try (TestTable table = new TestTable(onRemoteDatabase(), "table_to_insert", "(id BIGINT)");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. You stated in PR description that non-select statements are not supported. And here we have insert, delete and update...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a test that fails for these operations and also check if doesn't modify the underlying data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally missed the fact that these are negative assertions.

@Praveen2112 Praveen2112 force-pushed the praveen/stored_procedure_ptf_3 branch from 77fc2a9 to 104f7e9 Compare March 28, 2023 07:16
@Praveen2112
Copy link
Member Author

@jhlodin AC

@Praveen2112 Praveen2112 force-pushed the praveen/stored_procedure_ptf_3 branch from 104f7e9 to bd76ee3 Compare March 28, 2023 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants