Skip to content

Commit

Permalink
KQL plugin - Code cleanup (#116180)
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret authored and jozala committed Nov 13, 2024
1 parent a50cfa6 commit 5ee825c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugin/kql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ base {

dependencies {
compileOnly project(path: xpackModule('core'))
compileOnly "org.antlr:antlr4-runtime:${versions.antlr4}"
api "org.antlr:antlr4-runtime:${versions.antlr4}"

testImplementation "org.antlr:antlr4-runtime:${versions.antlr4}"
testImplementation project(':test:framework')
Expand Down
26 changes: 26 additions & 0 deletions x-pack/plugin/kql/licenses/antlr4-runtime-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[The "BSD license"]
Copyright (c) 2015 Terence Parr, Sam Harwell
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Empty file.
19 changes: 19 additions & 0 deletions x-pack/plugin/kql/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

module org.elasticsearch.kql {
requires org.elasticsearch.server;
requires org.elasticsearch.xcontent;
requires org.antlr.antlr4.runtime;
requires org.elasticsearch.base;
requires org.apache.lucene.queryparser;
requires org.elasticsearch.logging;
requires org.apache.lucene.core;

exports org.elasticsearch.xpack.kql;
exports org.elasticsearch.xpack.kql.parser;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.logging.log4j.util.Strings;
import org.apache.lucene.queryparser.classic.QueryParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Utility class for parsing and processing KQL expressions.
Expand Down Expand Up @@ -211,15 +211,15 @@ private static boolean isEscapedKeywordSequence(String input, int startIndex) {
if (startIndex + 1 >= input.length()) {
return false;
}
String remaining = Strings.toRootLowerCase(input.substring(startIndex));
String remaining = input.substring(startIndex).toLowerCase(Locale.ROOT);
return remaining.startsWith("and") || remaining.startsWith("or") || remaining.startsWith("not");
}

private static String handleKeywordSequence(String input, int startIndex) {
String remaining = input.substring(startIndex);
if (Strings.toRootLowerCase(remaining).startsWith("and")) return remaining.substring(0, 3);
if (Strings.toRootLowerCase(remaining).startsWith("or")) return remaining.substring(0, 2);
if (Strings.toRootLowerCase(remaining).startsWith("not")) return remaining.substring(0, 3);
if (remaining.toLowerCase(Locale.ROOT).startsWith("and")) return remaining.substring(0, 3);
if (remaining.toLowerCase(Locale.ROOT).startsWith("or")) return remaining.substring(0, 2);
if (remaining.toLowerCase(Locale.ROOT).startsWith("not")) return remaining.substring(0, 3);
return "";
}

Expand Down

0 comments on commit 5ee825c

Please sign in to comment.