diff --git a/core/src/main/java/org/apache/calcite/sql/dialect/SnowflakeSqlDialect.java b/core/src/main/java/org/apache/calcite/sql/dialect/SnowflakeSqlDialect.java index 637a2bd3a43..1c62b0c46d6 100644 --- a/core/src/main/java/org/apache/calcite/sql/dialect/SnowflakeSqlDialect.java +++ b/core/src/main/java/org/apache/calcite/sql/dialect/SnowflakeSqlDialect.java @@ -26,7 +26,7 @@ import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.parser.SqlParserPos; -import java.util.HashMap; +import com.google.common.collect.ImmutableMap; /** * A SqlDialect implementation for the Snowflake database. @@ -40,6 +40,22 @@ public class SnowflakeSqlDialect extends SqlDialect { public static final SqlDialect DEFAULT = new SnowflakeSqlDialect(DEFAULT_CONTEXT); + /* Maps a SqlKind to its Snowflake-specific operator. */ + public static final ImmutableMap OPERATOR_MAP; + + static { + final ImmutableMap.Builder builder = + ImmutableMap.builder(); + builder.put(SqlKind.BIT_AND, SqlLibraryOperators.BITAND_AGG); + builder.put(SqlKind.BIT_OR, SqlLibraryOperators.BITOR_AGG); + builder.put(SqlKind.CHAR_LENGTH, SqlLibraryOperators.LENGTH); + builder.put(SqlKind.ENDS_WITH, SqlLibraryOperators.ENDSWITH); + builder.put(SqlKind.STARTS_WITH, SqlLibraryOperators.STARTSWITH); + builder.put(SqlKind.MAX, SqlStdOperatorTable.MAX); + builder.put(SqlKind.MIN, SqlStdOperatorTable.MIN); + OPERATOR_MAP = builder.build(); + } + /** Creates a SnowflakeSqlDialect. */ public SnowflakeSqlDialect(Context context) { super(context); @@ -47,20 +63,12 @@ public SnowflakeSqlDialect(Context context) { @Override public void unparseCall(final SqlWriter writer, final SqlCall call, final int leftPrec, final int rightPrec) { - final HashMap map = new HashMap<>(); - map.put(SqlKind.BIT_AND, SqlLibraryOperators.BITAND_AGG); - map.put(SqlKind.BIT_OR, SqlLibraryOperators.BITOR_AGG); - map.put(SqlKind.CHAR_LENGTH, SqlLibraryOperators.LENGTH); - map.put(SqlKind.ENDS_WITH, SqlLibraryOperators.ENDSWITH); - map.put(SqlKind.STARTS_WITH, SqlLibraryOperators.STARTSWITH); - map.put(SqlKind.MAX, SqlStdOperatorTable.MAX); - map.put(SqlKind.MIN, SqlStdOperatorTable.MIN); - SqlOperator op = map.get(call.getKind()); + SqlOperator op = OPERATOR_MAP.get(call.getKind()); if (op != null) { - SqlCall newCall = op.createCall(SqlParserPos.ZERO, call.getOperandList()); - super.unparseCall(writer, newCall, leftPrec, rightPrec); + SqlCall newCall = op.createCall(SqlParserPos.ZERO, call.getOperandList()); + super.unparseCall(writer, newCall, leftPrec, rightPrec); } else { - super.unparseCall(writer, call, leftPrec, rightPrec); + super.unparseCall(writer, call, leftPrec, rightPrec); } }