简体   繁体   English

使用 Apache Spark 在 Databricks 中使用 SQL 查询进行 CASTING 问题

[英]CASTING issue with SQL query in Databricks with Apache Spark

I am trying to use the CAST function on a field in SQL to cast a Time field as a string, but I'm getting unexpected errors我正在尝试在 SQL 中的字段上使用 CAST function 将时间字段转换为字符串,但出现意外错误

The code is as follows:代码如下:

select DATE_ADD(cast(LAST_MOD_TIME as STRING) % 100 + CAST(LAST_MOD_TIME AS STRING) / 100 % 100 * 60 + CAST(LAST_MOD_TIME AS STRING) / 10000 * 3600, CAST(LAST_MOD_DATE AS STRING)) from DmWo

But I'm getting the error:但我收到错误:

Error in SQL statement: AnalysisException: cannot resolve 'date_add((((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) % CAST(100 AS DOUBLE)) + (((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(100 AS DOUBLE)) % CAST(100 AS DOUBLE)) * CAST(60 AS DOUBLE))) + ((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(10000 AS DOUBLE)) * CAST(3600 AS DOUBLE))), CAST(dmwo.`LAST_MOD_DATE` AS STRING))' due to data type mismatch: argument 1 requires date type, however, '(((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) % CAST(100 AS DOUBLE)) + (((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(100 AS DOUBLE)) % CAST(100 AS DOUBLE)) * CAST(60 AS DOUBLE))) + ((CAST(CAST(dmwo.`LAST_MOD_TIME` AS STRING) AS DOUBLE) / CAST(10000 AS DOUBLE)) * CAST(3600 AS DOUBLE)))' is of double type. argument 2 requires (int or smallint or tinyint) type, however, 'CAST(dmwo.`LAST_MOD_DATE` AS STRING)' is of string type.; line 1 pos 7;
'Project [unresolvedalias(date_add((((cast(cast(LAST_MOD_TIME#88591 as string) as double) % cast(100 as double)) + (((cast(cast(LAST_MOD_TIME#88591 as string) as double) / cast(100 as double)) % cast(100 as double)) * cast(60 as double))) + ((cast(cast(LAST_MOD_TIME#88591 as string) as double) / cast(10000 as double)) * cast(3600 as double))), cast(LAST_MOD_DATE#88590 as string)), None)]

Any thoughts on where I might be going wrong with the CAST function?关于 CAST function 可能出错的地方有什么想法吗?

There are several mistakes here:这里有几个错误:

  1. The first argument to DATE_ADD function is the start_date , and needs to be a date type, but you're passing in the time of the day in seconds based on the logic, and the type is double due to the division operator. DATE_ADD function 的第一个参数是start_date ,并且需要是date类型,但是您根据逻辑以秒为单位传递一天中的时间,并且由于除法运算符,该类型为double精度。
  2. The second argument to DATE_ADD function is the num_days after the starting date and needs to be int type. DATE_ADD function 的第二个参数是开始日期后的num_days ,需要是int类型。 You're passing in the starting date itself as a string type.您将开始日期本身作为string类型传递。
  3. DATE_ADD function returns the date that is num_days after start_date , but you are adding the date and the time of day counted in seconds. DATE_ADD function 返回start_date之后num_days的日期,但您要添加以秒为单位的日期和时间。 That's not what this function does for you.这不是这个 function 为你做的。

A simpler way is to use the to_timestamp() function:一种更简单的方法是使用to_timestamp() function:

select to_timestamp(concat(LAST_MOD_DATE, LAST_MOD_TIME), 'yyyyMMddHHmmss')
from DmWo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Databricks 的 Apache Spark 从 SQL 表中获取 stream 数据 - How to stream data from SQL Table with Apache Spark with Databricks 在Databricks中,SQL使用spark? - In Databricks, SQL uses spark? databricks 中 json 问题的 spark.readstream 和 writestream - spark.readstream and writestream for json issue in databricks Apache Spark function to_timestamp() 无法在 Databricks 上使用 PySpark - Apache Spark function to_timestamp() not working with PySpark on Databricks 使用 Databricks(和 Apache Spark)从 AWS Redshift 读取 - Read from AWS Redshift using Databricks (and Apache Spark) 如何使用 Databricks 在 Apache Spark 上编译 PySpark 中的 While 循环语句 - How to Compile a While Loop statement in PySpark on Apache Spark with Databricks 如何在 Databricks 的 Iceberg 表上执行 Spark SQL 合并语句? - How to execute a Spark SQL merge statement on an Iceberg table in Databricks? 试图写一个 pyspark function 连接到 SQL 服务器与 Databricks 在 Apache Spart - Attempting to write a pyspark function to connect to SQL Server with Databricks on Apache Spart Apache Spark Streaming 连接字符串错误,Databricks 连接到 Azure 事件中心 - Apache Spark Streaming Connection String error with Databricks connection to Azure Event Hub 无法使用 PySpark 与 Databricks 上的 apache spark function to_timestamp() 连接并添加一列 - Unable to concatenate with apache spark function to_timestamp() on Databricks using PySpark and add a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM