Skip to content

Commit

Permalink
Merge branch 'SNOW-802269-dateaddcollectset' of https://github.com/sn…
Browse files Browse the repository at this point in the history
…owflakedb/snowpark-java-scala into SNOW-802269-dateaddcollectset
  • Loading branch information
sfc-gh-sjayabalan committed Sep 3, 2024
2 parents 0acec09 + 751dece commit 37af111
Show file tree
Hide file tree
Showing 10 changed files with 549 additions and 105 deletions.
161 changes: 159 additions & 2 deletions src/main/java/com/snowflake/snowpark_java/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,25 @@ public static Column pow(Column l, Column r) {
}

/**
* Returns rounded values for the specified column.
* Rounds the numeric values of the given column {@code e} to the {@code scale} decimal places
* using the half away from zero rounding mode.
*
* <p>Example:
*
* <pre>{@code
* DataFrame df = session.sql("select * from (values (-3.78), (-2.55), (1.23), (2.55), (3.78)) as T(a)");
* df.select(round(col("a"), lit(1)).alias("round")).show();
*
* -----------
* |"ROUND" |
* -----------
* |-3.8 |
* |-2.6 |
* |1.2 |
* |2.6 |
* |3.8 |
* -----------
* }</pre>
*
* @since 0.9.0
* @param e The input column
Expand All @@ -1042,7 +1060,25 @@ public static Column round(Column e, Column scale) {
}

/**
* Returns rounded values for the specified column.
* Rounds the numeric values of the given column {@code e} to 0 decimal places using the half away
* from zero rounding mode.
*
* <p>Example:
*
* <pre>{@code
* DataFrame df = session.sql("select * from (values (-3.7), (-2.5), (1.2), (2.5), (3.7)) as T(a)");
* df.select(round(col("a")).alias("round")).show();
*
* -----------
* |"ROUND" |
* -----------
* |-4 |
* |-3 |
* |1 |
* |3 |
* |4 |
* -----------
* }</pre>
*
* @since 0.9.0
* @param e The input column
Expand All @@ -1052,6 +1088,36 @@ public static Column round(Column e) {
return new Column(com.snowflake.snowpark.functions.round(e.toScalaColumn()));
}

/**
* Rounds the numeric values of the given column {@code e} to the {@code scale} decimal places
* using the half away from zero rounding mode.
*
* <p>Example:
*
* <pre>{@code
* DataFrame df = session.sql("select * from (values (-3.78), (-2.55), (1.23), (2.55), (3.78)) as T(a)");
* df.select(round(col("a"), 1).alias("round")).show();
*
* -----------
* |"ROUND" |
* -----------
* |-3.8 |
* |-2.6 |
* |1.2 |
* |2.6 |
* |3.8 |
* -----------
* }</pre>
*
* @param e The column of numeric values to round.
* @param scale The number of decimal places to which {@code e} should be rounded.
* @return A new column containing the rounded numeric values.
* @since 1.14.0
*/
public static Column round(Column e, int scale) {
return new Column(com.snowflake.snowpark.functions.round(e.toScalaColumn(), scale));
}

/**
* Shifts the bits for a numeric expression numBits positions to the left.
*
Expand Down Expand Up @@ -4671,6 +4737,97 @@ public static Column randn(long seed) {
return new Column(functions.randn(seed));
}

/**
* Shift the given value numBits left. If the given value is a long value, this function will
* return a long value else it will return an integer value.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values(1),(2),(3) as T(a)");
* df.select(Functions.shiftleft(Functions.col("a"), 1).as("shiftleft")).show();
* ---------------
* |"SHIFTLEFT" |
* ---------------
* |2 |
* |4 |
* |6 |
* ---------------
* }</pre>
*
* @since 1.14.0
* @return Column object.
*/
public static Column shiftleft(Column c, int numBits) {
return new Column(functions.shiftleft(c.toScalaColumn(), numBits));
}

/**
* Shift the given value numBits right. If the given value is a long value, it will return a long
* value else it will return an integer value.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values(1),(2),(3) as T(a)");
* df.select(Functions.shiftright(Functions.col("a"), 1).as("shiftright")).show();
* ---------------
* |"SHIFTRIGHT" |
* ---------------
* |0 |
* |1 |
* |1 |
* ---------------
* }</pre>
*
* @since 1.14.0
* @return Column object.
*/
public static Column shiftright(Column c, int numBits) {
return new Column(functions.shiftright(c.toScalaColumn(), numBits));
}

/**
* Computes hex value of the given column.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values(1),(2),(3) as T(a)");
* df.select(Functions.hex(Functions.col("a")).as("hex")).show();
* ---------
* |"HEX" |
* ---------
* |31 |
* |32 |
* |33 |
* ---------
* }</pre>
*
* @since 1.14.0
* @return Column object.
*/
public static Column hex(Column c) {
return new Column(functions.hex(c.toScalaColumn()));
}

/**
* Inverse of hex. Interprets each pair of characters as a hexadecimal number and converts to the
* byte representation of number.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values(31),(32),(33) as T(a)");
* df.select(Functions.unhex(Functions.col("a")).as("unhex")).show();
* -----------
* |"UNHEX" |
* -----------
* |1 |
* |2 |
* |3 |
* -----------
* }</pre>
*
* @since 1.14.0
* @return Column object.
*/
public static Column unhex(Column c) {
return new Column(functions.unhex(c.toScalaColumn()));
}

/**
* Calls a user-defined function (UDF) by name.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/types/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ public String asJsonString() {
}
}

/**
* Return the variant value as a JsonNode. This function allows to read the JSON object directly
* as JsonNode from variant column rather parsing it as String
*
* <pre>{@code - to get the first value from array for key "a"
*
* Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
* System.out.println(jv.asJsonNode().get("a").get(0));
*
* output
* 1
* }</pre>
*
* @return A valid JsonNode
* @since 1.14.0
*/
public JsonNode asJsonNode() {
return value;
}

/**
* Converts the variant as binary value.
*
Expand Down
Loading

0 comments on commit 37af111

Please sign in to comment.