Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Fix ANTLR grammar for negative integer and floating point number #489

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/antlr/OpenDistroSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ STRING_USER_NAME: (
// Fragments for Literal primitives

fragment EXPONENT_NUM_PART: 'E' [-+]? DEC_DIGIT+;
fragment ID_LITERAL: [A-Z_$0-9@]*?[A-Z_$\-]+?[A-Z_$\-0-9]*;
fragment ID_LITERAL: [A-Z_$0-9@]*?[A-Z_$]+?[A-Z_$\-0-9]*;
fragment DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';
fragment SQUOTA_STRING: '\'' ('\\'. | '\'\'' | ~('\'' | '\\'))* '\'';
fragment BQUOTA_STRING: '`' ( '\\'. | '``' | ~('`'|'\\'))* '`';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 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.antlr.semantic;

import org.junit.Test;

public class SemanticAnalyzerConstantTest extends SemanticAnalyzerTestBase {

@Test
public void useNegativeIntegerShouldPass() {
validate("SELECT * FROM test WHERE age > -1");
}

@Test
public void useNegativeFloatingPointNumberShouldPass() {
validate("SELECT * FROM test WHERE balance > -1.23456");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
import com.amazon.opendistroforelasticsearch.sql.exception.SqlFeatureNotImplementedException;
import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException;
import com.amazon.opendistroforelasticsearch.sql.utils.StringUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -36,7 +32,6 @@
import static com.amazon.opendistroforelasticsearch.sql.plugin.SqlSettings.QUERY_ANALYSIS_ENABLED;
import static com.amazon.opendistroforelasticsearch.sql.plugin.SqlSettings.QUERY_ANALYSIS_SEMANTIC_SUGGESTION;
import static com.amazon.opendistroforelasticsearch.sql.plugin.SqlSettings.QUERY_ANALYSIS_SEMANTIC_THRESHOLD;
import static org.elasticsearch.common.xcontent.XContentType.JSON;
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.RestStatus.SERVICE_UNAVAILABLE;
Expand Down Expand Up @@ -251,6 +246,14 @@ public void queryWithUnsupportedFunctionShouldFail() {
);
}

@Test
public void useNegativeNumberConstantShouldPass() {
queryShouldPassAnalysis(
"SELECT * FROM elasticsearch-sql_test_index_bank " +
"WHERE age > -1 AND balance < -123.456789"
);
}

/** Run the query with cluster setting changed and cleaned after complete */
private void runWithClusterSetting(ClusterSetting setting, Runnable query) {
try {
Expand Down