question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is DateTime type supported?

See original GitHub issue

OS: Windows

Expected behavior

write to file

Actual behavior

Unhandled exception. System.InvalidCastException: Unable to cast object of type ‘System.Nullable1[System.DateTime][]' to type 'System.Nullable1[System.DateTimeOffset][]’. at Parquet.Data.BasicPrimitiveDataTypeHandler1.PackDefinitions(Array data, Int32 maxDefinitionLevel, Int32[]& definitions, Int32& definitionsLength, Int32& nullCount) at Parquet.Data.DataColumn.PackDefinitions(Int32 maxDefinitionLevel, Int32[]& pooledDefinitionLevels, Int32& definitionLevelCount, Int32& nullCount) at Parquet.File.DataColumnWriter.WriteColumn(DataColumn column, SchemaElement tse, IDataTypeHandler dataTypeHandler, Int32 maxRepetitionLevel, Int32 maxDefinitionLevel) at Parquet.File.DataColumnWriter.Write(List1 path, DataColumn column, IDataTypeHandler dataTypeHandler) at Parquet.ParquetRowGroupWriter.WriteColumn(DataColumn column) at testNulls.Program.Main(String[] args) in C:\Work\Parquet\testNulls2\Program.cs:line 43

Code snippet reproducing the behavior

using System;
using Parquet;
using Parquet.Data;
using System.Data;

namespace testdt
{
    class Program
    {
        static void Main(string[] args)
        {
            var dtArray = new DateTime?[] { };
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataColumn column = new System.Data.DataColumn("dtm", System.Type.GetType("System.DateTime"));
            dt.Columns.Add(column);
            DataRow row = dt.NewRow();
            row["dtm"] = DateTime.Today;
            dt.Rows.Add(row);

            Parquet.Data.DataField[] df = new Parquet.Data.DataField[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                switch (System.Type.GetTypeCode(dt.Columns[i].DataType))
                {
                    case TypeCode.DateTime: df[i] = new Parquet.Data.DateTimeDataField(dt.Columns[i].ColumnName, DateTimeFormat.Date, hasNulls: true); break;
                }
            }
            Schema schema = new Schema(df);
            using (Stream fileStream = System.IO.File.Create("C:\\Work\\Parquet\\test.parquet"))
            {
                using (ParquetWriter parquetWriter = new ParquetWriter(schema, fileStream))
                {
                   using (ParquetRowGroupWriter groupWriter = parquetWriter.CreateRowGroup())
                   {
                       for (int i = 0; i < df.Length; i++)
                       {
                           Parquet.Data.DataColumn clmn = null;
                           switch (System.Type.GetTypeCode(dt.Columns[i].DataType))
                           {
                               case TypeCode.DateTime: dtArray = dt.AsEnumerable().Select(d => d.Field<DateTime?>(df[i].Name)).ToArray(); clmn = new Parquet.Data.DataColumn(df[i], dtArray); break;
                               default: Console.WriteLine("Not found " + df[i].DataType.ToString()); break;
                           }
                           groupWriter.WriteColumn(clmn);
                       }
                   }
                }
            }
            System.Environment.Exit(0);
        }
    }
}

Thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anthirascommented, Jul 15, 2022

I have the same issue. It seems Parquet.Net is trying to cast DateTime to DateTimeOffset.

Minimal example to reproduce the issue:

using Parquet;
using Parquet.Data;

var dataField = new DataField<DateTime>("D");
var schema = new Schema(dataField);
using var stream = new MemoryStream();
using var parquetWriter = new ParquetWriter(schema, stream);
using var groupWriter = parquetWriter.CreateRowGroup();
groupWriter.WriteColumn(new DataColumn(dataField, new[]{DateTime.Now}));

Output:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.DateTime[]' to type 'System.DateTimeOffset[]'.
   at Parquet.Data.ArrayView.GetValuesAndReturnArray[T](DataColumnStatistics statistics, IEqualityComparer`1 equalityComparer, IComparer`1 comparer)+MoveNext()
   at Parquet.Data.Concrete.DateTimeOffsetDataTypeHandler.WriteAsInt96(BinaryWriter writer, ArrayView values, DataColumnStatistics dataColumnStatistics)
   at Parquet.Data.Concrete.DateTimeOffsetDataTypeHandler.Write(SchemaElement tse, BinaryWriter writer, ArrayView values, DataColumnStatistics statistics)
   at Parquet.File.DataColumnWriter.WriteColumn(DataColumn column, SchemaElement tse, IDataTypeHandler dataTypeHandler, Int32 maxRepetitionLevel, Int32 maxDefinitionLevel)
   at Parquet.File.DataColumnWriter.Write(List`1 path, DataColumn column, IDataTypeHandler dataTypeHandler)
   at Parquet.ParquetRowGroupWriter.WriteColumn(DataColumn column)
   at Program.<Main>$(String[] args) in [...]/Program.cs:line 9
0reactions
dburtsevcommented, Dec 1, 2022
using System.Data.SqlClient;
namespace testdt
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString ="Data Source=(local);Integrated Security=true";
            string queryString = "SELECT GETDATE() AS dt, CAST(GETDATE() AS datetimeoffset) AS dto;";
            System.Data.DataTable dt = new System.Data.DataTable();            
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = new SqlCommand(queryString, connection);
                adapter.Fill(dt);
            }
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                Console.WriteLine((Type.GetTypeCode(dt.Columns[i].DataType)).ToString());
            }
        }   
    }
}

Output: DateTime Object

Read more comments on GitHub >

github_iconTop Results From Across the Web

datetime — Basic date and time types
The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is...
Read more >
11.2.2 The DATE, DATETIME, and TIMESTAMP Types
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in '...
Read more >
Supported Date and Time Formats - Manual
This section describes all the different formats in a BNF-like format, that the DateTimeImmutable, DateTime, date_create_immutable(), date_create(), date_parse ...
Read more >
4 Datetime Data Types and Time Zone Support
Oracle Database DATE columns always contain fields for both date and time. If your queries use a date format without a time portion,...
Read more >
DateTime Struct (System)
Represents an instant in time, typically expressed as a date and time of day. ... You may need to format dates in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found