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

[Feature] Support setting session vars in user property #48477

Conversation

yandongxiao
Copy link
Contributor

@yandongxiao yandongxiao commented Jul 17, 2024

Why I'm doing:

When the user connects to FE SQL, the Session variables need to be initialized according to the user's attribute information.

What I'm doing:

Support to CRUD user's properties.

Fixes: #48478

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@yandongxiao yandongxiao requested review from a team as code owners July 17, 2024 02:08
@wanpengfei-git wanpengfei-git requested a review from a team July 17, 2024 02:08
@alvin-celerdata
Copy link
Contributor

@yandongxiao
Please create a Feature-Request GitHub Issue, and fill the necessary design doc in it.

@nshangyiming nshangyiming self-assigned this Jul 17, 2024
@yandongxiao
Copy link
Contributor Author

@yandongxiao Please create a Feature-Request GitHub Issue, and fill the necessary design doc in it.

Done

@yandongxiao yandongxiao force-pushed the feature/support-session-var-in-user-property-next branch 2 times, most recently from b1db23f to 792180f Compare July 19, 2024 03:36
}
}

private UserIdentity getUserIdentityByName(String userName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

put it into AuthenticationMgr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@yandongxiao
Copy link
Contributor Author

Fixed all based on review comments

@HangyuanLiu HangyuanLiu self-assigned this Jul 25, 2024
@@ -122,7 +122,8 @@ public void testShowUserProperty() {

@Test
public void testSetUserProperty() {
String sql = "SET PROPERTY FOR 'tom' 'max_user_connections' = 'value', 'test' = 'true'";
String sql = "SET PROPERTY FOR 'tom' 'max_user_connections' = '100 " +
"git '";
Copy link
Contributor

Choose a reason for hiding this comment

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

error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch!

public void updateByUserProperty(UserProperty userProperty) {
try {
// set catalog and database
boolean dBHasBeenSetByUser = !getCurrentCatalog().equals(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
boolean dBHasBeenSetByUser = !getCurrentCatalog().equals(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME) ||
boolean dbHasBeenSetByUser = !getCurrentCatalog().equals(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME) ||

try {
// set catalog and database
boolean dBHasBeenSetByUser = !getCurrentCatalog().equals(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME) ||
!getDatabase().isEmpty();
Copy link
Contributor

Choose a reason for hiding this comment

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

Now there is a scenario where the default catalog of the global session variable is not default_catalog. Then does this user's property catalog become ineffective?

properties.containsKey(UserProperty.PROP_DATABASE)) {
// Authorizer.checkAnyActionOnOrInDb and Authorizer.checkAnyActionOnCatalog methods are not allowed to be called,
// because they assume the user has been created.
throw new PrivilegeException(String.format("User property %s and %s are not allowed to set in CREATE USER",
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to check permissions when creating a user. You can check permissions when connecting, and the behavior is similar to mysql --database. Because even if you can check permissions when altering user, what if the permissions are revoked later? How do you deal with it?


try {
if (!CatalogMgr.isInternalCatalog(value)) {
Authorizer.checkAnyActionOnCatalog(user, roleIds, value);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the same as above. No need check privilege

@SerializedName(value = "m")
private long maxConn = 1024;
private long maxConn = MAX_CONN_DEFAULT_VALUE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to manage these separately? Can we unify the coding with system variables? Just make sure that user properties and system variables do not have the same name (this is easy for us to do because user properties are also under our control). The current approach 1. It brings complexity to the code. 2. The logic is not unified. There is a catalog in the user property and there is also a catalog in the session variable, and it is called "session.catalog". How to maintain the relationship between these two pieces of data, and what is the meaning of these two identical pieces of data? We should use user properties as a supplement and overwrite of system variables.

nshangyiming
nshangyiming previously approved these changes Jul 29, 2024
@yandongxiao yandongxiao requested a review from a team as a code owner July 29, 2024 12:57
@yandongxiao yandongxiao force-pushed the feature/support-session-var-in-user-property-next branch from 6fd28cd to ee1d2d2 Compare July 30, 2024 04:11
nshangyiming
nshangyiming previously approved these changes Jul 31, 2024
HangyuanLiu
HangyuanLiu previously approved these changes Aug 1, 2024
Copy link

sonarcloud bot commented Aug 2, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

Copy link

github-actions bot commented Aug 2, 2024

[FE Incremental Coverage Report]

pass : 195 / 223 (87.44%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/ast/ShowUserPropertyStmt.java 0 6 00.00% [75, 76, 77, 78, 79, 80]
🔵 com/starrocks/persist/EditLog.java 1 2 50.00% [1078]
🔵 com/starrocks/persist/AlterUserInfo.java 3 4 75.00% [59]
🔵 com/starrocks/qe/VariableMgr.java 6 7 85.71% [673]
🔵 com/starrocks/authentication/AuthenticationMgr.java 25 29 86.21% [322, 323, 611, 622]
🔵 com/starrocks/authentication/UserProperty.java 121 134 90.30% [138, 165, 191, 206, 215, 246, 247, 250, 252, 256, 258, 262, 303]
🔵 com/starrocks/qe/ConnectContext.java 22 24 91.67% [1124, 1130]
🔵 com/starrocks/mysql/nio/AcceptListener.java 3 3 100.00% []
🔵 com/starrocks/qe/ExecuteAsExecutor.java 6 6 100.00% []
🔵 com/starrocks/sql/ast/AlterUserStmt.java 1 1 100.00% []
🔵 com/starrocks/privilege/AuthorizationMgr.java 1 1 100.00% []
🔵 com/starrocks/qe/SetExecutor.java 1 1 100.00% []
🔵 com/starrocks/sql/analyzer/ShowUserPropertyAnalyzer.java 1 1 100.00% []
🔵 com/starrocks/sql/ast/BaseCreateAlterUserStmt.java 2 2 100.00% []
🔵 com/starrocks/sql/ast/CreateUserStmt.java 1 1 100.00% []
🔵 com/starrocks/qe/DDLStmtExecutor.java 1 1 100.00% []

Copy link

github-actions bot commented Aug 2, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@andyziye andyziye enabled auto-merge (squash) August 2, 2024 06:08
@andyziye andyziye merged commit 17d1914 into StarRocks:main Aug 2, 2024
49 of 50 checks passed
@yandongxiao
Copy link
Contributor Author

@mergify backport 3.3

Copy link
Contributor

mergify bot commented Aug 15, 2024

backport 3.3

❌ No backport have been created

  • Backport to branch 3.3 failed

Git reported the following error:

fatal: couldn't find remote ref 3.3

@yandongxiao
Copy link
Contributor Author

@mergify backport-3.3

Copy link
Contributor

mergify bot commented Aug 15, 2024

backport -3.3

❌ No backport have been created

  • Backport to branch -3.3 failed

GitHub error: Branch not found

@yandongxiao
Copy link
Contributor Author

@mergify backport branch-3.3

Copy link
Contributor

mergify bot commented Aug 15, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Aug 15, 2024
Signed-off-by: yandongxiao <[email protected]>
(cherry picked from commit 17d1914)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/ast/AlterUserStmt.java
#	fe/fe-core/src/main/java/com/starrocks/sql/ast/BaseCreateAlterUserStmt.java
#	fe/fe-core/src/main/java/com/starrocks/sql/ast/CreateUserStmt.java
#	fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java
yandongxiao added a commit to yandongxiao/starrocks that referenced this pull request Aug 15, 2024
andyziye pushed a commit that referenced this pull request Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support setting session vars in user property
7 participants