This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PPL] add basic parser support (#415)
* add basic parser support * Reduce the permission by removing java.security.AllPermission
- Loading branch information
Showing
24 changed files
with
972 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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,14 @@ | ||
plugins { | ||
id 'java' | ||
id "io.freefair.lombok" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile "org.antlr:antlr4-runtime:4.7.1" | ||
|
||
testCompile group: 'junit', name: 'junit', version: '4.12' | ||
} |
This file contains 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,2 @@ | ||
# This file is generated by the 'io.freefair.lombok' Gradle plugin | ||
config.stopBubbling = true |
82 changes: 82 additions & 0 deletions
82
...ava/com/amazon/opendistroforelasticsearch/sql/common/antlr/CaseInsensitiveCharStream.java
This file contains 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,82 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.common.antlr; | ||
|
||
import org.antlr.v4.runtime.CharStream; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.misc.Interval; | ||
|
||
/** | ||
* Custom stream to convert character to upper case for case insensitive grammar before sending to lexer. | ||
*/ | ||
public class CaseInsensitiveCharStream implements CharStream { | ||
|
||
/** Character stream */ | ||
private final CharStream charStream; | ||
|
||
public CaseInsensitiveCharStream(String sql) { | ||
this.charStream = CharStreams.fromString(sql); | ||
} | ||
|
||
@Override | ||
public String getText(Interval interval) { | ||
return charStream.getText(interval); | ||
} | ||
|
||
@Override | ||
public void consume() { | ||
charStream.consume(); | ||
} | ||
|
||
@Override | ||
public int LA(int i) { | ||
int c = charStream.LA(i); | ||
if (c <= 0) { | ||
return c; | ||
} | ||
return Character.toUpperCase(c); | ||
} | ||
|
||
@Override | ||
public int mark() { | ||
return charStream.mark(); | ||
} | ||
|
||
@Override | ||
public void release(int marker) { | ||
charStream.release(marker); | ||
} | ||
|
||
@Override | ||
public int index() { | ||
return charStream.index(); | ||
} | ||
|
||
@Override | ||
public void seek(int index) { | ||
charStream.seek(index); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return charStream.size(); | ||
} | ||
|
||
@Override | ||
public String getSourceName() { | ||
return charStream.getSourceName(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...a/com/amazon/opendistroforelasticsearch/sql/common/antlr/SyntaxAnalysisErrorListener.java
This file contains 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,74 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.common.antlr; | ||
|
||
import org.antlr.v4.runtime.BaseErrorListener; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.RecognitionException; | ||
import org.antlr.v4.runtime.Recognizer; | ||
import org.antlr.v4.runtime.Token; | ||
import org.antlr.v4.runtime.misc.IntervalSet; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Syntax analysis error listener that handles any syntax error by throwing exception with useful information. | ||
*/ | ||
public class SyntaxAnalysisErrorListener extends BaseErrorListener { | ||
|
||
@Override | ||
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, | ||
int line, int charPositionInLine, String msg, | ||
RecognitionException e) { | ||
|
||
CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream(); | ||
Token offendingToken = (Token) offendingSymbol; | ||
String query = tokens.getText(); | ||
|
||
throw new RuntimeException( | ||
String.format(Locale.ROOT, | ||
"Failed to parse query due to offending symbol [%s] at: '%s' <--- HERE... More details: %s", | ||
getOffendingText(offendingToken), | ||
truncateQueryAtOffendingToken(query, offendingToken), | ||
getDetails(recognizer, msg, e) | ||
) | ||
); | ||
} | ||
|
||
private String getOffendingText(Token offendingToken) { | ||
return offendingToken.getText(); | ||
} | ||
|
||
private String truncateQueryAtOffendingToken(String query, Token offendingToken) { | ||
return query.substring(0, offendingToken.getStopIndex() + 1); | ||
} | ||
|
||
/** | ||
* As official JavaDoc says, e=null means parser was able to recover from the error. | ||
* In other words, "msg" argument includes the information we want. | ||
*/ | ||
private String getDetails(Recognizer<?, ?> recognizer, String msg, RecognitionException e) { | ||
String details; | ||
if (e == null) { | ||
details = msg; | ||
} else { | ||
IntervalSet followSet = e.getExpectedTokens(); | ||
details = "Expecting tokens in " + followSet.toString(recognizer.getVocabulary()); | ||
} | ||
return details; | ||
} | ||
|
||
} |
This file contains 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,10 @@ | ||
apply plugin: 'java' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile group: 'org.elasticsearch', name: 'elasticsearch', version: "${es_version}" | ||
testCompile group: 'junit', name: 'junit', version: '4.12' | ||
} |
38 changes: 38 additions & 0 deletions
38
...java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/security/SecurityAccess.java
This file contains 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,38 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.elasticsearch.security; | ||
|
||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.io.IOException; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
|
||
/** | ||
* Ref: https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugin-authors.html#_java_security_permissions | ||
*/ | ||
public class SecurityAccess { | ||
|
||
public static <T> T doPrivileged(final PrivilegedExceptionAction<T> operation) throws IOException { | ||
SpecialPermission.check(); | ||
try { | ||
return AccessController.doPrivileged(operation); | ||
} catch (final PrivilegedActionException e) { | ||
throw (IOException) e.getCause(); | ||
} | ||
} | ||
} |
This file contains 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 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 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
58 changes: 58 additions & 0 deletions
58
...java/com/amazon/opendistroforelasticsearch/sql/plugin/request/PPLQueryRequestFactory.java
This file contains 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,58 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.plugin.request; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.ppl.domain.PPLQueryRequest; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class PPLQueryRequestFactory { | ||
private static final String PPL_URL_PARAM_KEY = "ppl"; | ||
private static final String PPL_FIELD_NAME = "query"; | ||
|
||
public static PPLQueryRequest getPPLRequest(RestRequest request) { | ||
switch (request.method()) { | ||
case GET: | ||
return parsePPLRequestFromUrl(request); | ||
case POST: | ||
return parsePPLRequestFromPayload(request); | ||
default: | ||
throw new IllegalArgumentException("ES PPL doesn't supported HTTP " + request.method().name()); | ||
} | ||
} | ||
|
||
private static PPLQueryRequest parsePPLRequestFromUrl(RestRequest restRequest) { | ||
String ppl; | ||
|
||
ppl = restRequest.param(PPL_URL_PARAM_KEY); | ||
if (ppl == null) { | ||
throw new IllegalArgumentException("Cannot find ppl parameter from the URL"); | ||
} | ||
return new PPLQueryRequest(ppl, null); | ||
} | ||
|
||
private static PPLQueryRequest parsePPLRequestFromPayload(RestRequest restRequest) { | ||
String content = restRequest.content().utf8ToString(); | ||
JSONObject jsonContent; | ||
try { | ||
jsonContent = new JSONObject(content); | ||
} catch (JSONException e) { | ||
throw new IllegalArgumentException("Failed to parse request payload", e); | ||
} | ||
return new PPLQueryRequest(jsonContent.getString(PPL_FIELD_NAME), jsonContent); | ||
} | ||
} |
Oops, something went wrong.