Skip to content

Commit

Permalink
New approach
Browse files Browse the repository at this point in the history
  • Loading branch information
tanclary committed Jan 30, 2024
1 parent b88eb0f commit 0ec73b1
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>SqlDialect</code> implementation for the Snowflake database.
Expand All @@ -40,27 +40,35 @@ 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<SqlKind, SqlOperator> OPERATOR_MAP;

static {
final ImmutableMap.Builder<SqlKind, SqlOperator> 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);
}

@Override public void unparseCall(final SqlWriter writer, final SqlCall call, final int leftPrec,
final int rightPrec) {
final HashMap<SqlKind, SqlOperator> 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);
}
}

Expand Down

0 comments on commit 0ec73b1

Please sign in to comment.