-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ZEPPELIN-3377] Passing Z variables to JDBC interpreter #2903
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
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2868825
ZEPPELIN-1967: Initial updates
sanjaydasgupta b461c82
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta ced295c
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta a3215fc
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta f43fd99
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 3beebce
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 3b30ea2
Reversing previous incorrect update
sanjaydasgupta 1718e79
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 5268803
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta b7ddf6b
Implementing configuration (global enable/disable interpolation) foll…
sanjaydasgupta a1703b8
Changes suggested in Felix Cheung's review https://github.com/apache/…
sanjaydasgupta 3dd3dd8
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 1e2c87d
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta cc3727f
Changes due the Jeff Zhang's comments at https://github.com/apache/ze…
sanjaydasgupta d600d86
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 5f8505b
Changes due to Felix Cheung's comments at https://github.com/apache/z…
sanjaydasgupta 77738aa
Changes to comply with Felix Cheung's comment at https://github.com/a…
sanjaydasgupta a19e998
Merge branch 'master' of https://github.com/apache/zeppelin into ZEPP…
sanjaydasgupta 0f49867
Merge branch 'master' of https://github.com/apache/zeppelin into zepp…
sanjaydasgupta eb9194d
ZEPPELIN-3377 Updates Initial Load
sanjaydasgupta 315a9ad
Corrected use of rlike in SQL statement
sanjaydasgupta df99ab0
Revisions after Felix Cheung's review https://github.com/apache/zeppe…
sanjaydasgupta 07561f5
Revisions after Felix Cheung's review https://github.com/apache/zeppe…
sanjaydasgupta 094d3ce
Reduced indentation to remove check-style errors
sanjaydasgupta 9947d36
Expanded * imports to remove check-style errors
sanjaydasgupta 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
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
181 changes: 181 additions & 0 deletions
181
jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterInterpolationTest.java
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,181 @@ | ||
| /** | ||
| * 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.zeppelin.jdbc; | ||
|
|
||
| import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter; | ||
| import org.apache.zeppelin.interpreter.InterpreterContext; | ||
| import org.apache.zeppelin.interpreter.InterpreterResult; | ||
| import org.apache.zeppelin.resource.LocalResourcePool; | ||
| import org.apache.zeppelin.resource.ResourcePool; | ||
| import org.apache.zeppelin.user.AuthenticationInfo; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.Statement; | ||
| import java.util.Properties; | ||
|
|
||
| import static java.lang.String.format; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * JDBC interpreter Z-variable interpolation unit tests. | ||
| */ | ||
| public class JDBCInterpreterInterpolationTest extends BasicJDBCTestCaseAdapter { | ||
|
|
||
| private static String jdbcConnection; | ||
| private InterpreterContext interpreterContext; | ||
| private ResourcePool resourcePool; | ||
|
|
||
| private String getJdbcConnection() throws IOException { | ||
| if (null == jdbcConnection) { | ||
| Path tmpDir = Files.createTempDirectory("h2-test-"); | ||
| tmpDir.toFile().deleteOnExit(); | ||
| jdbcConnection = format("jdbc:h2:%s", tmpDir); | ||
| } | ||
| return jdbcConnection; | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| Class.forName("org.h2.Driver"); | ||
| Connection connection = DriverManager.getConnection(getJdbcConnection()); | ||
| Statement statement = connection.createStatement(); | ||
| statement.execute( | ||
| "DROP TABLE IF EXISTS test_table; " + | ||
| "CREATE TABLE test_table(id varchar(255), name varchar(255));"); | ||
|
|
||
| Statement insertStatement = connection.createStatement(); | ||
| insertStatement.execute("insert into test_table(id, name) values " + | ||
| "('pro', 'processor')," + | ||
| "('mem', 'memory')," + | ||
| "('key', 'keyboard')," + | ||
| "('mou', 'mouse');"); | ||
| resourcePool = new LocalResourcePool("JdbcInterpolationTest"); | ||
|
|
||
| interpreterContext = new InterpreterContext("", "1", null, "", "", | ||
| new AuthenticationInfo("testUser"), null, null, null, null, resourcePool, null, null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnableDisableProperty() throws IOException { | ||
| Properties properties = new Properties(); | ||
| properties.setProperty("common.max_count", "1000"); | ||
| properties.setProperty("common.max_retry", "3"); | ||
| properties.setProperty("default.driver", "org.h2.Driver"); | ||
| properties.setProperty("default.url", getJdbcConnection()); | ||
| properties.setProperty("default.user", ""); | ||
| properties.setProperty("default.password", ""); | ||
|
|
||
| resourcePool.put("zid", "mem"); | ||
| String sqlQuery = "select * from test_table where id = '{zid}'"; | ||
|
|
||
| // | ||
| // Empty result expected because "zeppelin.jdbc.interpolation" is false by default ... | ||
| // | ||
| JDBCInterpreter t = new JDBCInterpreter(properties); | ||
| t.open(); | ||
| InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext); | ||
| assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); | ||
| assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); | ||
| assertEquals(1, interpreterResult.message().size()); | ||
| assertEquals("ID\tNAME\n", interpreterResult.message().get(0).getData()); | ||
|
|
||
| // | ||
| // 1 result expected because "zeppelin.jdbc.interpolation" set to "true" ... | ||
| // | ||
| properties.setProperty("zeppelin.jdbc.interpolation", "true"); | ||
| t = new JDBCInterpreter(properties); | ||
| t.open(); | ||
| interpreterResult = t.interpret(sqlQuery, interpreterContext); | ||
| assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); | ||
| assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); | ||
| assertEquals(1, interpreterResult.message().size()); | ||
| assertEquals("ID\tNAME\nmem\tmemory\n", | ||
| interpreterResult.message().get(0).getData()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNormalQueryInterpolation() throws IOException { | ||
| Properties properties = new Properties(); | ||
| properties.setProperty("common.max_count", "1000"); | ||
| properties.setProperty("common.max_retry", "3"); | ||
| properties.setProperty("default.driver", "org.h2.Driver"); | ||
| properties.setProperty("default.url", getJdbcConnection()); | ||
| properties.setProperty("default.user", ""); | ||
| properties.setProperty("default.password", ""); | ||
|
|
||
| properties.setProperty("zeppelin.jdbc.interpolation", "true"); | ||
|
|
||
| JDBCInterpreter t = new JDBCInterpreter(properties); | ||
| t.open(); | ||
|
|
||
| // | ||
| // Empty result expected because "kbd" is not defined ... | ||
| // | ||
| String sqlQuery = "select * from test_table where id = '{kbd}'"; | ||
| InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext); | ||
| assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); | ||
| assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); | ||
| assertEquals(1, interpreterResult.message().size()); | ||
| assertEquals("ID\tNAME\n", interpreterResult.message().get(0).getData()); | ||
|
|
||
| resourcePool.put("itemId", "key"); | ||
|
|
||
| // | ||
| // 1 result expected because z-variable 'item' is 'key' ... | ||
| // | ||
| sqlQuery = "select * from test_table where id = '{itemId}'"; | ||
| interpreterResult = t.interpret(sqlQuery, interpreterContext); | ||
| assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); | ||
| assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); | ||
| assertEquals(1, interpreterResult.message().size()); | ||
| assertEquals("ID\tNAME\nkey\tkeyboard\n", | ||
| interpreterResult.message().get(0).getData()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEscapedInterpolationPattern() throws IOException { | ||
| Properties properties = new Properties(); | ||
| properties.setProperty("common.max_count", "1000"); | ||
| properties.setProperty("common.max_retry", "3"); | ||
| properties.setProperty("default.driver", "org.h2.Driver"); | ||
| properties.setProperty("default.url", getJdbcConnection()); | ||
| properties.setProperty("default.user", ""); | ||
| properties.setProperty("default.password", ""); | ||
|
|
||
| properties.setProperty("zeppelin.jdbc.interpolation", "true"); | ||
|
|
||
| JDBCInterpreter t = new JDBCInterpreter(properties); | ||
| t.open(); | ||
|
|
||
| // | ||
| // 2 rows (keyboard and mouse) expected when searching names with 2 consecutive vowels ... | ||
| // The 'regexp' keyword is specific to H2 database | ||
| // | ||
| String sqlQuery = "select * from test_table where name regexp '[aeiou]{{2}}'"; | ||
| InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext); | ||
| assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); | ||
| assertEquals(InterpreterResult.Type.TABLE, interpreterResult.message().get(0).getType()); | ||
| assertEquals(1, interpreterResult.message().size()); | ||
| assertEquals("ID\tNAME\nkey\tkeyboard\nmou\tmouse\n", | ||
| interpreterResult.message().get(0).getData()); | ||
| } | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is different, right, why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a different thing. I have mentioned under What is this PR for?:
Since I was editing jdbc.md, I thought correcting the error would be useful for readers. The changed text works as intended under the current implementation, and is also according to the documentation for Dynamic Forms.
Please let me know if corrections should be handled in a different way.
Thanks for noticing this update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok thanks