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

Commit

Permalink
Add Text and Keyword Data Type (#620)
Browse files Browse the repository at this point in the history
* Add Text and Keyword Data Type

* update

* address comments
  • Loading branch information
penghuo authored Jul 29, 2020
1 parent bee2ca2 commit b258fda
Show file tree
Hide file tree
Showing 46 changed files with 1,366 additions and 511 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* 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.data.model;

import com.google.common.base.Objects;
import lombok.RequiredArgsConstructor;

/**
* Expression Number Value.
*/
@RequiredArgsConstructor
public abstract class AbstractExprNumberValue extends AbstractExprValue {
private final Number value;

@Override
public Integer integerValue() {
return value.intValue();
}

@Override
public Long longValue() {
return value.longValue();
}

@Override
public Float floatValue() {
return value.floatValue();
}

@Override
public Double doubleValue() {
return value.doubleValue();
}

@Override
public int hashCode() {
return Objects.hashCode(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
*
* 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.data.model;

import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException;

/**
* Abstract ExprValue.
*/
public abstract class AbstractExprValue implements ExprValue {
/**
* The customize compareTo logic.
*/
@Override
public int compareTo(ExprValue other) {
if (this.isNull() || this.isMissing()) {
return this.compare(other);
} else if (other.isNull() || other.isMissing()) {
return -other.compareTo(this);
}
if (!this.type().equals(other.type())) {
throw new ExpressionEvaluationException(
String.format(
"compare expected value have same type, but with [%s, %s]",
this.type(), other.type()));
}
return compare(other);
}

/**
* The customize equals logic.
* The table below list the NULL and MISSING handling logic.
* A B A == B
* NULL NULL TRUE
* NULL MISSING FALSE
* MISSING NULL FALSE
* MISSING MISSING TRUE
*/
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ExprValue)) {
return false;
}
ExprValue other = (ExprValue) o;
if (this.isNull() || this.isMissing()) {
return equal(other);
} else if (other.isNull() || other.isMissing()) {
return other.equals(this);
} else {
return equal(other);
}
}

/**
* The expression value compare.
*/
public abstract int compare(ExprValue other);

/**
* The expression value equal.
*/
public abstract boolean equal(ExprValue other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.google.common.base.Objects;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class ExprBooleanValue implements ExprValue {
/**
* Expression Boolean Value.
*/
public class ExprBooleanValue extends AbstractExprValue {
private static final ExprBooleanValue TRUE = new ExprBooleanValue(true);
private static final ExprBooleanValue FALSE = new ExprBooleanValue(false);

Expand All @@ -39,12 +43,32 @@ public Object value() {
}

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

@Override
public Boolean booleanValue() {
return value;
}

@Override
public String toString() {
return value.toString();
}

@Override
public int compare(ExprValue other) {
return Boolean.compare(value, other.booleanValue());
}

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

@Override
public int hashCode() {
return Objects.hashCode(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.google.common.base.Objects;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

@EqualsAndHashCode
/**
* Expression Collection Value.
*/
@RequiredArgsConstructor
public class ExprCollectionValue implements ExprValue {
public class ExprCollectionValue extends AbstractExprValue {
private final List<ExprValue> valueList;

@Override
Expand All @@ -32,14 +38,52 @@ public Object value() {
}

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

@Override
public List<ExprValue> collectionValue() {
return valueList;
}

@Override
public String toString() {
return valueList.stream()
.map(Object::toString)
.collect(Collectors.joining(",", "[", "]"));
}

@Override
public boolean equal(ExprValue o) {
if (!(o instanceof ExprCollectionValue)) {
return false;
} else {
ExprCollectionValue other = (ExprCollectionValue) o;
Iterator<ExprValue> thisIterator = this.valueList.iterator();
Iterator<ExprValue> otherIterator = other.valueList.iterator();

while (thisIterator.hasNext() && otherIterator.hasNext()) {
ExprValue thisEntry = thisIterator.next();
ExprValue otherEntry = otherIterator.next();
if (!thisEntry.equals(otherEntry)) {
return false;
}
}
return !(thisIterator.hasNext() || otherIterator.hasNext());
}
}

/**
* Only compare the size of the list.
*/
@Override
public int compare(ExprValue other) {
return Integer.compare(valueList.size(), other.collectionValue().size());
}

@Override
public int hashCode() {
return Objects.hashCode(valueList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
import com.google.common.base.Objects;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;

/**
* Date Value.
* Expression Date Value.
*/
@EqualsAndHashCode
@RequiredArgsConstructor
public class ExprDateValue implements ExprValue {
public class ExprDateValue extends AbstractExprValue {
/**
* todo. only support UTC now.
*/
Expand All @@ -59,16 +59,32 @@ public String value() {
}

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

@Override
public ZonedDateTime dateValue() {
return date.atZone(ZONE);
}

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

public ZonedDateTime getDate() {
return date.atZone(ZONE);
@Override
public int compare(ExprValue other) {
return date.compareTo(other.dateValue().toInstant());
}

@Override
public boolean equal(ExprValue other) {
return date.atZone(ZONE).equals(other.dateValue());
}

@Override
public int hashCode() {
return Objects.hashCode(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,39 @@
package com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;

@EqualsAndHashCode
@RequiredArgsConstructor
public class ExprDoubleValue implements ExprValue {
private final Double value;
/**
* Expression Double Value.
*/
public class ExprDoubleValue extends AbstractExprNumberValue {

public ExprDoubleValue(Number value) {
super(value);
}

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

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

@Override
public String toString() {
return value.toString();
return doubleValue().toString();
}

@Override
public int compare(ExprValue other) {
return Double.compare(doubleValue(), other.doubleValue());
}

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

0 comments on commit b258fda

Please sign in to comment.