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

[Spotless] Applying Google Code Format for legacy directory (pt 2/4) #20 #1989

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,92 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.legacy.antlr.semantic.scope;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.opensearch.sql.legacy.antlr.semantic.types.Type;

/**
* Environment for symbol and its attribute (type) in the current scope
*/
/** Environment for symbol and its attribute (type) in the current scope */
public class Environment {

private final Environment parent;

private final SymbolTable symbolTable;
private final Environment parent;

public Environment(Environment parent) {
this.parent = parent;
this.symbolTable = new SymbolTable();
}
private final SymbolTable symbolTable;

/**
* Define symbol with the type
* @param symbol symbol to define
* @param type type
*/
public void define(Symbol symbol, Type type) {
symbolTable.store(symbol, type);
}
public Environment(Environment parent) {
this.parent = parent;
this.symbolTable = new SymbolTable();
}

/**
* Resolve symbol in the environment
* @param symbol symbol to look up
* @return type if exist
*/
public Optional<Type> resolve(Symbol symbol) {
Optional<Type> type = Optional.empty();
for (Environment cur = this; cur != null; cur = cur.parent) {
type = cur.symbolTable.lookup(symbol);
if (type.isPresent()) {
break;
}
}
return type;
}
/**
* Define symbol with the type
*
* @param symbol symbol to define
* @param type type
*/
public void define(Symbol symbol, Type type) {
symbolTable.store(symbol, type);
}

/**
* Resolve symbol definitions by a prefix.
* @param prefix a prefix of symbol
* @return all symbols with types that starts with the prefix
*/
public Map<String, Type> resolveByPrefix(Symbol prefix) {
Map<String, Type> typeByName = new HashMap<>();
for (Environment cur = this; cur != null; cur = cur.parent) {
typeByName.putAll(cur.symbolTable.lookupByPrefix(prefix));
}
return typeByName;
/**
* Resolve symbol in the environment
*
* @param symbol symbol to look up
* @return type if exist
*/
public Optional<Type> resolve(Symbol symbol) {
Optional<Type> type = Optional.empty();
for (Environment cur = this; cur != null; cur = cur.parent) {
type = cur.symbolTable.lookup(symbol);
if (type.isPresent()) {
break;
}
}
return type;
}

/**
* Resolve all symbols in the namespace.
* @param namespace a namespace
* @return all symbols in the namespace
*/
public Map<String, Type> resolveAll(Namespace namespace) {
Map<String, Type> result = new HashMap<>();
for (Environment cur = this; cur != null; cur = cur.parent) {
// putIfAbsent ensures inner most definition will be used (shadow outers)
cur.symbolTable.lookupAll(namespace).forEach(result::putIfAbsent);
}
return result;
/**
* Resolve symbol definitions by a prefix.
*
* @param prefix a prefix of symbol
* @return all symbols with types that starts with the prefix
*/
public Map<String, Type> resolveByPrefix(Symbol prefix) {
Map<String, Type> typeByName = new HashMap<>();
for (Environment cur = this; cur != null; cur = cur.parent) {
typeByName.putAll(cur.symbolTable.lookupByPrefix(prefix));
}
return typeByName;
}

/** Current environment is root and no any symbol defined */
public boolean isEmpty(Namespace namespace) {
for (Environment cur = this; cur != null; cur = cur.parent) {
if (!cur.symbolTable.isEmpty(namespace)) {
return false;
}
}
return true;
/**
* Resolve all symbols in the namespace.
*
* @param namespace a namespace
* @return all symbols in the namespace
*/
public Map<String, Type> resolveAll(Namespace namespace) {
Map<String, Type> result = new HashMap<>();
for (Environment cur = this; cur != null; cur = cur.parent) {
// putIfAbsent ensures inner most definition will be used (shadow outers)
cur.symbolTable.lookupAll(namespace).forEach(result::putIfAbsent);
}
return result;
}

public Environment getParent() {
return parent;
/** Current environment is root and no any symbol defined */
public boolean isEmpty(Namespace namespace) {
for (Environment cur = this; cur != null; cur = cur.parent) {
if (!cur.symbolTable.isEmpty(namespace)) {
return false;
}
}
return true;
}

public Environment getParent() {
return parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.legacy.antlr.semantic.scope;

/**
* Namespace of symbol to avoid naming conflict
*/
/** Namespace of symbol to avoid naming conflict */
public enum Namespace {
FIELD_NAME("Field"),
FUNCTION_NAME("Function"),
OPERATOR_NAME("Operator");

FIELD_NAME("Field"),
FUNCTION_NAME("Function"),
OPERATOR_NAME("Operator");

private final String name;

Namespace(String name) {
this.name = name;
}
private final String name;

@Override
public String toString() {
return name;
}
Namespace(String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.legacy.antlr.semantic.types.operator;

import static org.opensearch.sql.legacy.antlr.semantic.types.base.OpenSearchDataType.TYPE_ERROR;
Expand All @@ -13,35 +12,32 @@
import org.opensearch.sql.legacy.antlr.semantic.types.Type;
import org.opensearch.sql.legacy.antlr.semantic.types.base.OpenSearchIndex;

/**
* Join operator
*/
/** Join operator */
public enum JoinOperator implements Type {
JOIN;

@Override
public String getName() {
return name();
}

@Override
public Type construct(List<Type> others) {
Optional<Type> isAnyNonIndexType = others.stream().
filter(type -> !(type instanceof OpenSearchIndex)).
findAny();
if (isAnyNonIndexType.isPresent()) {
return TYPE_ERROR;
}
return others.get(0);
}

@Override
public String usage() {
return "Please join index with other index or its nested field.";
}

@Override
public String toString() {
return "Operator [" + getName() + "]";
JOIN;

@Override
public String getName() {
return name();
}

@Override
public Type construct(List<Type> others) {
Optional<Type> isAnyNonIndexType =
others.stream().filter(type -> !(type instanceof OpenSearchIndex)).findAny();
if (isAnyNonIndexType.isPresent()) {
return TYPE_ERROR;
}
return others.get(0);
}

@Override
public String usage() {
return "Please join index with other index or its nested field.";
}

@Override
public String toString() {
return "Operator [" + getName() + "]";
}
}
Loading
Loading