-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-48530][SQL] Support for local variables in SQL Scripting #49445
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
Closed
dusantism-db
wants to merge
63
commits into
apache:master
from
dusantism-db:scripting-local-variables
Closed
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
73cb01b
first commit
dusantism-db 813d282
POC works
dusantism-db 1c08f57
make column res helper more functional
dusantism-db 18da02f
move variables map to SqlScriptingScope
dusantism-db cee5f1a
implement proper namespace (scope label name) for local variables
dusantism-db 47934ab
qualified names
dusantism-db 399d4e8
update todos
dusantism-db 6efe764
resolve catalogs + check for duplicates
dusantism-db 769607d
set variable and normalized identifiers
dusantism-db 6225956
resolve fully qualified session vars in tempvarManager only and updat…
dusantism-db 241fc05
tests first batch
dusantism-db 068e1ec
add more tests
dusantism-db 60335db
add error messages, more tests and some comments
dusantism-db 65b69d3
rename TempVariableManager.scala and add more tests
dusantism-db fe5dc7b
remove old logic for dropping variables, update tests and add more tests
dusantism-db 4f8d2c1
add cleanup for scripting execution, separate drop and create variabl…
dusantism-db ba5b8d2
fix resolvecatalogs and add more tests
dusantism-db 33f0aac
refactor to support properly setting variables
dusantism-db be6052f
add error message for system and session label names
dusantism-db 4b1e8e1
small fixes and cleanup
dusantism-db 90b106b
Fix duplicate detection for set variablwe
dusantism-db 7ba0923
Add test for DECLARE OR REPLACE but ignore it until FOR is fixed
dusantism-db cd4e932
execute immediate don't resolve vars from scripts. Problem remains wi…
dusantism-db fdf3c5a
cleanup
dusantism-db 52cbd17
Merge remote-tracking branch 'upstream/master' into scripting-local-v…
dusantism-db c134fd4
fix merge mistake
dusantism-db 3ea762d
fix merge mistake 2
dusantism-db 8e9352a
fix comments
dusantism-db 78042e3
Update CreateVar, SetVar and lookupVariable to work with Execute Imme…
dusantism-db 40ffa83
add enum for lookup variable mode
dusantism-db 4a546a4
convert scripting variable manager to threadlocal
dusantism-db 15d5554
fix e2e test
dusantism-db a2b20c5
add comment
dusantism-db e3077a4
add comment and regenerate golden files
dusantism-db 6ce8f9c
fix failing test
dusantism-db ccab52c
refactor SqlScriptingVariableManager to be LexicalThreadLocal singlet…
dusantism-db 370bf65
renames
dusantism-db 0cea838
tagging approach
dusantism-db 9895c69
Revert "tagging approach"
dusantism-db cd888dd
analysiscontext withExecuteImmediate
dusantism-db 4fe7ab5
remove into clause flag
dusantism-db 8a6b536
address comments
dusantism-db db573c1
remove parameter from lookupVariable
dusantism-db dadd517
Merge remote-tracking branch 'upstream/master' into scripting-local-v…
dusantism-db 680e5d7
resolve comments 1
dusantism-db 7d3008e
Merge remote-tracking branch 'upstream/master' into scripting-local-v…
dusantism-db 901aa6c
improve logic to work with exception handlers
dusantism-db 34677c7
add check for existing variable in set, and add comments to findVariable
dusantism-db b814b97
Introduce FakeLocalCatalog, remove sessionVariablesOnly flags and upd…
dusantism-db 8074c63
resolve comments
dusantism-db 220aeae
throw error if var not found in setvarexec
dusantism-db 45ca867
resolve comments again
dusantism-db e1f1098
update resolvecatalogs according to wenchens comments, and forbid dro…
dusantism-db 61a753f
change variableManager api to use only nameParts and VariableDefiniti…
dusantism-db f29a8fc
add test for drop session var
dusantism-db e184f8c
forbid session, builtin and sys* label names
dusantism-db 4f70b96
update lookupVariable to reuse isForbiddenLabelName function
dusantism-db 3f72c80
revert execute immediate changes as they are done in separate PR
dusantism-db 5217e0f
Merge remote-tracking branch 'upstream/master' into scripting-local-v…
dusantism-db f3aedc8
fix documentation
dusantism-db 7cb9580
fix scalastyle
dusantism-db c9b06d3
fix documentatiopn again
dusantism-db db988f0
add more checks to setvariableexec
dusantism-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
common/utils/src/main/scala/org/apache/spark/util/LexicalThreadLocal.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.util | ||
|
|
||
| /** | ||
| * Helper trait for defining thread locals with lexical scoping. With this helper, the thread local | ||
| * is private and can only be set by the [[Handle]]. The [[Handle]] only exposes the thread local | ||
| * value to functions passed into its runWith method. This pattern allows for | ||
| * the lifetime of the thread local value to be strictly controlled. | ||
| * | ||
| * Rather than calling `tl.set(...)` and `tl.remove()` you would get a handle and execute your code | ||
| * in `handle.runWith { ... }`. | ||
| * | ||
| * Example: | ||
| * {{{ | ||
| * object Credentials extends LexicalThreadLocal[Int] { | ||
| * def create(creds: Map[String, String]) = new Handle(Some(creds)) | ||
| * } | ||
| * ... | ||
| * val handle = Credentials.create(Map("key" -> "value")) | ||
| * assert(Credentials.get() == None) | ||
| * handle.runWith { | ||
| * assert(Credentials.get() == Some(Map("key" -> "value"))) | ||
| * } | ||
| * }}} | ||
| */ | ||
| trait LexicalThreadLocal[T] { | ||
| private val tl = new ThreadLocal[T] | ||
|
|
||
| private def set(opt: Option[T]): Unit = { | ||
| opt match { | ||
| case Some(x) => tl.set(x) | ||
| case None => tl.remove() | ||
| } | ||
| } | ||
|
|
||
| protected def createHandle(opt: Option[T]): Handle = new Handle(opt) | ||
|
|
||
| def get(): Option[T] = Option(tl.get) | ||
|
|
||
| /** Final class representing a handle to a thread local value. */ | ||
| final class Handle private[LexicalThreadLocal] (private val opt: Option[T]) { | ||
| def runWith[R](f: => R): R = { | ||
| val old = get() | ||
| set(opt) | ||
| try f finally { | ||
| set(old) | ||
| } | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...alyst/src/main/scala/org/apache/spark/sql/catalyst/SqlScriptingLocalVariableManager.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst | ||
|
|
||
| import org.apache.spark.sql.catalyst.catalog.VariableManager | ||
| import org.apache.spark.util.LexicalThreadLocal | ||
|
|
||
| object SqlScriptingLocalVariableManager extends LexicalThreadLocal[VariableManager] { | ||
| def create(variableManager: VariableManager): Handle = createHandle(Option(variableManager)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.