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

Fix flint response handler json structure #2377

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.data.model;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;

/** Expression array Value. */
@RequiredArgsConstructor
public class ExprArrayValue extends AbstractExprValue {
private final List<ExprValue> value;

@Override
public boolean isArray() {
return true;
}

@Override
public Object value() {
return value;
}

@Override
public ExprType type() {
return ExprCoreType.ARRAY;
}

@Override
public String stringValue() {
return value.stream().map(ExprValue::stringValue).collect(Collectors.joining(","));
}

@Override
public List<ExprValue> arrayValue() {
return value;
}

@Override
public String toString() {
return String.format("%s", stringValue());
}

@Override
public int compare(ExprValue other) {
return stringValue().compareTo(other.stringValue());
}

@Override
public boolean equal(ExprValue other) {
return stringValue().equals(other.stringValue());
}

@Override
public int hashCode() {
return Objects.hashCode(stringValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.opensearch.sql.data.model;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -72,20 +71,25 @@ public ExprValue keyValue(String key) {
public boolean equal(ExprValue o) {
if (!(o instanceof ExprTupleValue)) {
return false;
} else {
ExprTupleValue other = (ExprTupleValue) o;
Iterator<Entry<String, ExprValue>> thisIterator = this.valueMap.entrySet().iterator();
Iterator<Entry<String, ExprValue>> otherIterator = other.valueMap.entrySet().iterator();
while (thisIterator.hasNext() && otherIterator.hasNext()) {
Entry<String, ExprValue> thisEntry = thisIterator.next();
Entry<String, ExprValue> otherEntry = otherIterator.next();
if (!(thisEntry.getKey().equals(otherEntry.getKey())
&& thisEntry.getValue().equals(otherEntry.getValue()))) {
return false;
}
}

ExprTupleValue other = (ExprTupleValue) o;
if (this.valueMap.size() != other.valueMap.size()) {
return false;
}

for (Map.Entry<String, ExprValue> entry : this.valueMap.entrySet()) {
String key = entry.getKey();
ExprValue value = entry.getValue();
if (!other.valueMap.containsKey(key)) {
return false;
}
ExprValue otherValue = other.valueMap.get(key);
if (!value.equals(otherValue)) {
return false;
}
return !(thisIterator.hasNext() || otherIterator.hasNext());
}
return true;
}

/** Only compare the size of the map. */
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/org/opensearch/sql/data/model/ExprValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
return false;
}

/**
* Is Array value.
*
* @return true: is a array value, otherwise false
*/
default boolean isArray() {
return false;
}

/** Get the {@link BindingTuple}. */
default BindingTuple bindingTuples() {
return BindingTuple.EMPTY;
Expand Down Expand Up @@ -108,6 +117,12 @@
"invalid to get stringValue from value of type " + type());
}

/** Get array value. */
default List<ExprValue> arrayValue() {
throw new ExpressionEvaluationException(
"invalid to get arrayValue from value of type " + type());

Check warning on line 123 in core/src/main/java/org/opensearch/sql/data/model/ExprValue.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/opensearch/sql/data/model/ExprValue.java#L122-L123

Added lines #L122 - L123 were not covered by tests
}

/** Get boolean value. */
default Boolean booleanValue() {
throw new ExpressionEvaluationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.opensearch.sql.data.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.data.type.ExprCoreType;

public class ExprArrayValueTest {
@Test
public void testIsArrayFalse() {
assertFalse(new ExprStringValue("test").isArray());
}
@Test
public void testIsArray() {
ExprArrayValue exprArrayValue = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertFalse(exprArrayValue.isArray());
}

@Test
public void testValue() {
List<ExprValue> value =
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2"));
ExprArrayValue exprArrayValue = new ExprArrayValue(value);
assertEquals(value, exprArrayValue.value());
}

@Test
public void testType() {
ExprArrayValue exprArrayValue = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertEquals(ExprCoreType.ARRAY, exprArrayValue.type());
}

@Test
public void testStringValue() {
ExprArrayValue exprArrayValue =
new ExprArrayValue(
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2")));
assertEquals("test1,test2", exprArrayValue.stringValue());
}

@Test
public void testArrayValue() {
List<ExprValue> value =
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2"));
ExprArrayValue exprArrayValue = new ExprArrayValue(value);
assertEquals(value, exprArrayValue.arrayValue());
}

@Test
public void testToString() {
ExprArrayValue exprArrayValue = new ExprArrayValue(List.of(new ExprStringValue("test")));
assertEquals("test", exprArrayValue.toString());
}

@Test
public void testCompare() {
ExprArrayValue exprArrayValue1 = new ExprArrayValue(Arrays.asList(new ExprStringValue("a")));
ExprArrayValue exprArrayValue2 = new ExprArrayValue(Arrays.asList(new ExprStringValue("b")));
assertThat(exprArrayValue1.compare(exprArrayValue2), lessThan(0));
}

@Test
public void testEqual() {
ExprArrayValue exprArrayValue1 = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
ExprArrayValue exprArrayValue2 = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertTrue(exprArrayValue1.equal(exprArrayValue2));
}

@Test
public void testHashCode() {
ExprArrayValue exprArrayValue = new ExprArrayValue(List.of(new ExprStringValue("test")));
assertEquals(exprArrayValue.hashCode(), Objects.hashCode("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.model.ExprTupleValue.fromExprValueMap;
import static org.opensearch.sql.utils.ComparisonUtil.compare;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.exception.ExpressionEvaluationException;

Expand All @@ -30,6 +32,27 @@ public void tuple_compare_int() {
assertFalse(tupleValue.equals(intValue));
}

@Test
public void compare_tuple_with_same_key_different_order() {
assertEquals(
fromExprValueMap(
Map.of(
"column1",
new ExprStringValue("value1"),
"column2",
new ExprIntegerValue(123),
"column3",
ExprBooleanValue.of(true))),
fromExprValueMap(
Map.of(
"column2",
new ExprIntegerValue(123),
"column1",
new ExprStringValue("value1"),
"column3",
ExprBooleanValue.of(true))));
}

@Test
public void compare_tuple_with_different_key() {
ExprValue tupleValue1 = ExprValueUtils.tupleValue(ImmutableMap.of("value", 2));
Expand All @@ -44,8 +67,8 @@ public void compare_tuple_with_different_size() {
ExprValue tupleValue1 = ExprValueUtils.tupleValue(ImmutableMap.of("integer_value", 2));
ExprValue tupleValue2 =
ExprValueUtils.tupleValue(ImmutableMap.of("integer_value", 2, "float_value", 1f));
assertFalse(tupleValue1.equals(tupleValue2));
assertFalse(tupleValue2.equals(tupleValue1));
assertNotEquals(tupleValue1, tupleValue2);
assertNotEquals(tupleValue2, tupleValue1);
}

@Test
Expand Down
10 changes: 10 additions & 0 deletions spark/src/main/antlr/FlintSparkSqlExtensions.g4
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ createSkippingIndexStatement
: CREATE SKIPPING INDEX (IF NOT EXISTS)?
ON tableName
LEFT_PAREN indexColTypeList RIGHT_PAREN
whereClause?
(WITH LEFT_PAREN propertyList RIGHT_PAREN)?
;

Expand Down Expand Up @@ -58,6 +59,7 @@ createCoveringIndexStatement
: CREATE INDEX (IF NOT EXISTS)? indexName
ON tableName
LEFT_PAREN indexColumns=multipartIdentifierPropertyList RIGHT_PAREN
whereClause?
(WITH LEFT_PAREN propertyList RIGHT_PAREN)?
;

Expand Down Expand Up @@ -115,6 +117,14 @@ materializedViewQuery
: .+?
;

whereClause
: WHERE filterCondition
;

filterCondition
: .+?
;

indexColTypeList
: indexColType (COMMA indexColType)*
;
Expand Down
1 change: 1 addition & 0 deletions spark/src/main/antlr/SparkSqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ SHOW: 'SHOW';
TRUE: 'TRUE';
VIEW: 'VIEW';
VIEWS: 'VIEWS';
WHERE: 'WHERE';
WITH: 'WITH';


Expand Down
Loading
Loading